xref: /aosp_15_r20/external/libwebm/webm_parser/tests/callback_test.cc (revision 103e46e4cd4b6efcf6001f23fa8665fb110abf8d)
1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include "webm/callback.h"
9 
10 #include <cstdint>
11 
12 #include "gtest/gtest.h"
13 
14 #include "webm/buffer_reader.h"
15 #include "webm/element.h"
16 #include "webm/status.h"
17 
18 using webm::Action;
19 using webm::BufferReader;
20 using webm::Callback;
21 using webm::ElementMetadata;
22 using webm::Reader;
23 using webm::Status;
24 
25 namespace {
26 
TestCompletedOk(Status (Callback::* function)(const ElementMetadata &))27 void TestCompletedOk(Status (Callback::*function)(const ElementMetadata&)) {
28   Callback callback;
29   ElementMetadata metadata{};
30 
31   Status status = (callback.*function)(metadata);
32   EXPECT_EQ(Status::kOkCompleted, status.code);
33 }
34 
35 template <typename T>
TestCompletedOk(Status (Callback::* function)(const ElementMetadata &,const T &))36 void TestCompletedOk(Status (Callback::*function)(const ElementMetadata&,
37                                                   const T&)) {
38   Callback callback;
39   ElementMetadata metadata{};
40   T object{};
41 
42   Status status = (callback.*function)(metadata, object);
43   EXPECT_EQ(Status::kOkCompleted, status.code);
44 }
45 
TestAction(Status (Callback::* function)(const ElementMetadata &,Action *),Action expected)46 void TestAction(Status (Callback::*function)(const ElementMetadata&, Action*),
47                 Action expected) {
48   Callback callback;
49   ElementMetadata metadata{};
50   Action action;
51 
52   Status status = (callback.*function)(metadata, &action);
53   EXPECT_EQ(Status::kOkCompleted, status.code);
54   EXPECT_EQ(expected, action);
55 }
56 
57 template <typename T>
TestAction(Status (Callback::* function)(const ElementMetadata &,const T &,Action *),Action expected)58 void TestAction(Status (Callback::*function)(const ElementMetadata&, const T&,
59                                              Action*),
60                 Action expected) {
61   Callback callback;
62   ElementMetadata metadata{};
63   T t{};
64   Action action;
65 
66   Status status = (callback.*function)(metadata, t, &action);
67   EXPECT_EQ(Status::kOkCompleted, status.code);
68   EXPECT_EQ(expected, action);
69 }
70 
71 template <typename T>
TestRead(Status (Callback::* function)(const T &,Reader *,std::uint64_t *))72 void TestRead(Status (Callback::*function)(const T&, Reader*, std::uint64_t*)) {
73   Callback callback;
74   Status status;
75   T metadata{};
76   BufferReader reader = {0x00, 0x01, 0x02, 0x03};
77   std::uint64_t bytes_remaining = 4;
78 
79   status = (callback.*function)(metadata, &reader, &bytes_remaining);
80   EXPECT_EQ(Status::kOkCompleted, status.code);
81   EXPECT_EQ(static_cast<std::uint64_t>(0), bytes_remaining);
82 }
83 
84 class CallbackTest : public testing::Test {};
85 
TEST_F(CallbackTest,OnElementBegin)86 TEST_F(CallbackTest, OnElementBegin) {
87   TestAction(&Callback::OnElementBegin, Action::kRead);
88 }
89 
TEST_F(CallbackTest,OnUnknownElement)90 TEST_F(CallbackTest, OnUnknownElement) {
91   TestRead(&Callback::OnUnknownElement);
92 }
93 
TEST_F(CallbackTest,OnEbml)94 TEST_F(CallbackTest, OnEbml) { TestCompletedOk(&Callback::OnEbml); }
95 
TEST_F(CallbackTest,OnVoid)96 TEST_F(CallbackTest, OnVoid) { TestRead(&Callback::OnVoid); }
97 
TEST_F(CallbackTest,OnSegmentBegin)98 TEST_F(CallbackTest, OnSegmentBegin) {
99   TestAction(&Callback::OnSegmentBegin, Action::kRead);
100 }
101 
TEST_F(CallbackTest,OnSeek)102 TEST_F(CallbackTest, OnSeek) { TestCompletedOk(&Callback::OnSeek); }
103 
TEST_F(CallbackTest,OnInfo)104 TEST_F(CallbackTest, OnInfo) { TestCompletedOk(&Callback::OnInfo); }
105 
TEST_F(CallbackTest,OnClusterBegin)106 TEST_F(CallbackTest, OnClusterBegin) {
107   TestAction(&Callback::OnClusterBegin, Action::kRead);
108 }
109 
TEST_F(CallbackTest,OnSimpleBlockBegin)110 TEST_F(CallbackTest, OnSimpleBlockBegin) {
111   TestAction(&Callback::OnSimpleBlockBegin, Action::kRead);
112 }
113 
TEST_F(CallbackTest,OnSimpleBlockEnd)114 TEST_F(CallbackTest, OnSimpleBlockEnd) {
115   TestCompletedOk(&Callback::OnSimpleBlockEnd);
116 }
117 
TEST_F(CallbackTest,OnBlockGroupBegin)118 TEST_F(CallbackTest, OnBlockGroupBegin) {
119   TestAction(&Callback::OnBlockGroupBegin, Action::kRead);
120 }
121 
TEST_F(CallbackTest,OnBlockBegin)122 TEST_F(CallbackTest, OnBlockBegin) {
123   TestAction(&Callback::OnBlockBegin, Action::kRead);
124 }
125 
TEST_F(CallbackTest,OnBlockEnd)126 TEST_F(CallbackTest, OnBlockEnd) { TestCompletedOk(&Callback::OnBlockEnd); }
127 
TEST_F(CallbackTest,OnBlockGroupEnd)128 TEST_F(CallbackTest, OnBlockGroupEnd) {
129   TestCompletedOk(&Callback::OnBlockGroupEnd);
130 }
131 
TEST_F(CallbackTest,OnFrame)132 TEST_F(CallbackTest, OnFrame) { TestRead(&Callback::OnFrame); }
133 
TEST_F(CallbackTest,OnClusterEnd)134 TEST_F(CallbackTest, OnClusterEnd) { TestCompletedOk(&Callback::OnClusterEnd); }
135 
TEST_F(CallbackTest,OnTrackEntry)136 TEST_F(CallbackTest, OnTrackEntry) { TestCompletedOk(&Callback::OnTrackEntry); }
137 
TEST_F(CallbackTest,OnCuePoint)138 TEST_F(CallbackTest, OnCuePoint) { TestCompletedOk(&Callback::OnCuePoint); }
139 
TEST_F(CallbackTest,OnEditionEntry)140 TEST_F(CallbackTest, OnEditionEntry) {
141   TestCompletedOk(&Callback::OnEditionEntry);
142 }
143 
TEST_F(CallbackTest,OnTag)144 TEST_F(CallbackTest, OnTag) { TestCompletedOk(&Callback::OnTag); }
145 
TEST_F(CallbackTest,OnSegmentEnd)146 TEST_F(CallbackTest, OnSegmentEnd) { TestCompletedOk(&Callback::OnSegmentEnd); }
147 
148 }  // namespace
149