blob: a1937425a2472b6f832201286937c60abce56a23 [file] [log] [blame]
brandtrb78bc752017-02-22 01:26:59 -08001/*
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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <stdint.h>
12#include <stdio.h>
brandtrb78bc752017-02-22 01:26:59 -080013#include <memory>
14#include <string>
15
Mirko Bonadeid9708072019-01-25 20:26:48 +010016#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "api/video/video_frame_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "test/testsupport/file_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "test/testsupport/frame_reader.h"
brandtrb78bc752017-02-22 01:26:59 -080022
23namespace webrtc {
24namespace test {
25
26namespace {
27const std::string kInputFileContents = "bazouk";
28
29const size_t kFrameWidth = 2;
30const size_t kFrameHeight = 2;
31const size_t kFrameLength = 3 * kFrameWidth * kFrameHeight / 2; // I420.
32} // namespace
33
Mirko Bonadei6a489f22019-04-09 15:11:12 +020034class YuvFrameReaderTest : public ::testing::Test {
brandtrb78bc752017-02-22 01:26:59 -080035 protected:
36 YuvFrameReaderTest() = default;
37 ~YuvFrameReaderTest() override = default;
38
39 void SetUp() override {
40 temp_filename_ = webrtc::test::TempFilename(webrtc::test::OutputPath(),
41 "yuv_frame_reader_unittest");
42 FILE* dummy = fopen(temp_filename_.c_str(), "wb");
43 fprintf(dummy, "%s", kInputFileContents.c_str());
44 fclose(dummy);
45
46 frame_reader_.reset(
47 new YuvFrameReaderImpl(temp_filename_, kFrameWidth, kFrameHeight));
48 ASSERT_TRUE(frame_reader_->Init());
49 }
50
51 void TearDown() override { remove(temp_filename_.c_str()); }
52
53 std::unique_ptr<FrameReader> frame_reader_;
54 std::string temp_filename_;
55};
56
57TEST_F(YuvFrameReaderTest, InitSuccess) {}
58
59TEST_F(YuvFrameReaderTest, FrameLength) {
60 EXPECT_EQ(kFrameLength, frame_reader_->FrameLength());
61}
62
63TEST_F(YuvFrameReaderTest, NumberOfFrames) {
64 EXPECT_EQ(1, frame_reader_->NumberOfFrames());
65}
66
67TEST_F(YuvFrameReaderTest, ReadFrame) {
Magnus Jedvert90e31902017-06-07 11:32:50 +020068 rtc::scoped_refptr<I420BufferInterface> buffer = frame_reader_->ReadFrame();
brandtrb78bc752017-02-22 01:26:59 -080069 ASSERT_TRUE(buffer);
70 // Expect I420 packed as YUV.
71 EXPECT_EQ(kInputFileContents[0], buffer->DataY()[0]);
72 EXPECT_EQ(kInputFileContents[1], buffer->DataY()[1]);
73 EXPECT_EQ(kInputFileContents[2], buffer->DataY()[2]);
74 EXPECT_EQ(kInputFileContents[3], buffer->DataY()[3]);
75 EXPECT_EQ(kInputFileContents[4], buffer->DataU()[0]);
76 EXPECT_EQ(kInputFileContents[5], buffer->DataV()[0]);
77 EXPECT_FALSE(frame_reader_->ReadFrame()); // End of file.
78}
79
80TEST_F(YuvFrameReaderTest, ReadFrameUninitialized) {
81 YuvFrameReaderImpl file_reader(temp_filename_, kFrameWidth, kFrameHeight);
82 EXPECT_FALSE(file_reader.ReadFrame());
83}
84
85} // namespace test
86} // namespace webrtc