1 /* 2 * Copyright (c) 2023, Alliance for Open Media. All rights reserved 3 * 4 * This source code is subject to the terms of the BSD 3-Clause Clear License 5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear 6 * License was not distributed with this source code in the LICENSE file, you 7 * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the 8 * Alliance for Open Media Patent License 1.0 was not distributed with this 9 * source code in the PATENTS file, you can obtain it at 10 * www.aomedia.org/license/patent. 11 */ 12 #ifndef TESTS_OBU_TEST_BASE_H_ 13 #define TESTS_OBU_TEST_BASE_H_ 14 15 #include <cstdint> 16 #include <memory> 17 #include <vector> 18 19 #include "gtest/gtest.h" 20 #include "iamf/common/leb_generator.h" 21 #include "iamf/common/utils/tests/test_utils.h" 22 #include "iamf/common/write_bit_buffer.h" 23 #include "iamf/obu/obu_header.h" 24 25 namespace iamf_tools { 26 27 class ObuTestBase { 28 public: 29 static const int kObuRedundantCopyBitMask = 4; 30 static const int kObuTrimmingStatusFlagBitMask = 2; 31 static const int kObuExtensionFlagBitMask = 1; 32 ObuTestBase(std::vector<uint8_t> expected_header,std::vector<uint8_t> expected_payload)33 ObuTestBase(std::vector<uint8_t> expected_header, 34 std::vector<uint8_t> expected_payload) 35 36 : header_(), 37 expected_header_(expected_header), 38 expected_payload_(expected_payload) {} 39 40 protected: 41 void InitAndTestWrite(bool only_validate_size = false) { 42 InitExpectOk(); 43 TestWriteExpectOk(only_validate_size); 44 } 45 TestWriteExpectOk(bool only_validate_size)46 void TestWriteExpectOk(bool only_validate_size) { 47 ASSERT_NE(leb_generator_, nullptr); 48 // Allocate space for the expected size of the OBU. 49 ASSERT_NE(leb_generator_, nullptr); 50 WriteBitBuffer wb(expected_header_.size() + expected_payload_.size(), 51 *leb_generator_); 52 // Write the OBU. 53 WriteObuExpectOk(wb); 54 55 // Validate either the size or byte-by-byte results match expected 56 // depending on the mode. 57 if (only_validate_size) { 58 EXPECT_EQ(wb.bit_buffer().size(), 59 expected_header_.size() + expected_payload_.size()); 60 } else { 61 ValidateObuWriteResults(wb, expected_header_, expected_payload_); 62 } 63 } 64 65 virtual void InitExpectOk() = 0; 66 virtual void WriteObuExpectOk(WriteBitBuffer& wb) = 0; 67 68 // Override this in subclasses to destroy the specific OBU. 69 virtual ~ObuTestBase() = 0; 70 71 std::unique_ptr<LebGenerator> leb_generator_ = 72 LebGenerator::Create(LebGenerator::GenerationMode::kMinimum); 73 ObuHeader header_; 74 // TODO(b/296044377): Find a way to initialize `expected_header_` in a less 75 // verbose way. Many tests manually configure it when the 76 // size of the OBU or flags in the `ObuHeader` vary. 77 std::vector<uint8_t> expected_header_; 78 std::vector<uint8_t> expected_payload_; 79 }; 80 81 } // namespace iamf_tools 82 83 #endif // TESTS_OBU_TEST_BASE_H_ 84