brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <memory> |
| 12 | #include <string> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "api/video/i420_buffer.h" |
| 15 | #include "test/gtest.h" |
| 16 | #include "test/testsupport/fileutils.h" |
| 17 | #include "test/testsupport/frame_reader.h" |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | namespace test { |
| 21 | |
| 22 | namespace { |
| 23 | const std::string kInputFileContents = "bazouk"; |
| 24 | |
| 25 | const size_t kFrameWidth = 2; |
| 26 | const size_t kFrameHeight = 2; |
| 27 | const size_t kFrameLength = 3 * kFrameWidth * kFrameHeight / 2; // I420. |
| 28 | } // namespace |
| 29 | |
| 30 | class YuvFrameReaderTest : public testing::Test { |
| 31 | protected: |
| 32 | YuvFrameReaderTest() = default; |
| 33 | ~YuvFrameReaderTest() override = default; |
| 34 | |
| 35 | void SetUp() override { |
| 36 | temp_filename_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), |
| 37 | "yuv_frame_reader_unittest"); |
| 38 | FILE* dummy = fopen(temp_filename_.c_str(), "wb"); |
| 39 | fprintf(dummy, "%s", kInputFileContents.c_str()); |
| 40 | fclose(dummy); |
| 41 | |
| 42 | frame_reader_.reset( |
| 43 | new YuvFrameReaderImpl(temp_filename_, kFrameWidth, kFrameHeight)); |
| 44 | ASSERT_TRUE(frame_reader_->Init()); |
| 45 | } |
| 46 | |
| 47 | void TearDown() override { remove(temp_filename_.c_str()); } |
| 48 | |
| 49 | std::unique_ptr<FrameReader> frame_reader_; |
| 50 | std::string temp_filename_; |
| 51 | }; |
| 52 | |
| 53 | TEST_F(YuvFrameReaderTest, InitSuccess) {} |
| 54 | |
| 55 | TEST_F(YuvFrameReaderTest, FrameLength) { |
| 56 | EXPECT_EQ(kFrameLength, frame_reader_->FrameLength()); |
| 57 | } |
| 58 | |
| 59 | TEST_F(YuvFrameReaderTest, NumberOfFrames) { |
| 60 | EXPECT_EQ(1, frame_reader_->NumberOfFrames()); |
| 61 | } |
| 62 | |
| 63 | TEST_F(YuvFrameReaderTest, ReadFrame) { |
Magnus Jedvert | 90e3190 | 2017-06-07 11:32:50 +0200 | [diff] [blame] | 64 | rtc::scoped_refptr<I420BufferInterface> buffer = frame_reader_->ReadFrame(); |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 65 | ASSERT_TRUE(buffer); |
| 66 | // Expect I420 packed as YUV. |
| 67 | EXPECT_EQ(kInputFileContents[0], buffer->DataY()[0]); |
| 68 | EXPECT_EQ(kInputFileContents[1], buffer->DataY()[1]); |
| 69 | EXPECT_EQ(kInputFileContents[2], buffer->DataY()[2]); |
| 70 | EXPECT_EQ(kInputFileContents[3], buffer->DataY()[3]); |
| 71 | EXPECT_EQ(kInputFileContents[4], buffer->DataU()[0]); |
| 72 | EXPECT_EQ(kInputFileContents[5], buffer->DataV()[0]); |
| 73 | EXPECT_FALSE(frame_reader_->ReadFrame()); // End of file. |
| 74 | } |
| 75 | |
| 76 | TEST_F(YuvFrameReaderTest, ReadFrameUninitialized) { |
| 77 | YuvFrameReaderImpl file_reader(temp_filename_, kFrameWidth, kFrameHeight); |
| 78 | EXPECT_FALSE(file_reader.ReadFrame()); |
| 79 | } |
| 80 | |
| 81 | } // namespace test |
| 82 | } // namespace webrtc |