1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, Hardware
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include <test_header.h>
18
19 #include "frame_collector.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 class FrameCollectorTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp() override;
31 void TearDown() override;
32 };
33
SetUpTestCase()34 void FrameCollectorTest::SetUpTestCase() {}
TearDownTestCase()35 void FrameCollectorTest::TearDownTestCase() {}
SetUp()36 void FrameCollectorTest::SetUp() {}
TearDown()37 void FrameCollectorTest::TearDown() {}
38
39 /*
40 * Function: Singleton is single
41 * Type: Function
42 * EnvConditions: N/A
43 * CaseDescription: 1. call GetInstance twice
44 * 2. check instance should be same
45 */
46 HWTEST_F(FrameCollectorTest, Singleton, Function | SmallTest | Level4)
47 {
48 PART("CaseDescription") {
49 STEP("1. call GetInstance twice") {
50 auto &instance1 = FrameCollector::GetInstance();
51 auto &instance2 = FrameCollector::GetInstance();
52 STEP("2. check instance should be same") {
53 STEP_ASSERT_EQ(&instance1, &instance2);
54 }
55 }
56 }
57 }
58
59 /*
60 * Function: IsEnabled/SetEnabled
61 * Type: Function
62 * EnvConditions: N/A
63 * CaseDescription: 1. save IsEnabled as e
64 * 2. SetEnabled !e
65 * 3. check IsEnabled is !e
66 */
67 HWTEST_F(FrameCollectorTest, Enable, Function | SmallTest | Level2)
68 {
69 PART("CaseDescription") {
70 bool e = false;
71 STEP("1. save IsEnabled as e") {
72 e = FrameCollector::GetInstance().IsEnabled();
73 }
74
75 STEP("2. SetEnabled !e") {
76 FrameCollector::GetInstance().SetEnabled(!e);
77 }
78
79 STEP("3. save IsEnabled as e") {
80 STEP_ASSERT_EQ(FrameCollector::GetInstance().IsEnabled(), !e);
81 }
82 }
83 }
84
85 /*
86 * Function: MarkFrameEvent
87 * Type: Function
88 * EnvConditions: Enable and ClearEvents FrameCollector
89 * CaseDescription: 1. check queue size 0
90 * 2. mark UIMarks
91 * 3. check queue size 0
92 * 4. mark WaitVsyncStart
93 * 5. check queue size 1
94 * 6. mark WaitVsyncStart
95 * 7. check queue size 2
96 * 8. clear events
97 * 9. check queue size 0
98 */
99 HWTEST_F(FrameCollectorTest, MarkFrameEvent, Function | MediumTest | Level2)
100 {
101 auto &collector = FrameCollector::GetInstance();
102 PART("EnvConditions") {
103 STEP("Enable and ClearEvents FrameCollector") {
104 collector.SetEnabled(true);
105 collector.ClearEvents();
106 }
107 }
108
109 PART("CaseDescription") {
110 STEP("1. check queue size 0") {
111 STEP_ASSERT_EQ(collector.LockGetFrameQueue().GetSize(), 0);
112 collector.UnlockFrameQueue();
113 }
114
115 STEP("2. mark UIMarks") {
116 collector.MarkFrameEvent(FrameEventType::UploadStart);
117 collector.MarkFrameEvent(FrameEventType::UploadEnd);
118 collector.MarkFrameEvent(FrameEventType::AnimateStart);
119 collector.MarkFrameEvent(FrameEventType::AnimateEnd);
120 collector.MarkFrameEvent(FrameEventType::LayoutStart);
121 collector.MarkFrameEvent(FrameEventType::LayoutEnd);
122 collector.MarkFrameEvent(FrameEventType::DrawStart);
123 collector.MarkFrameEvent(FrameEventType::DrawEnd);
124 }
125
126 STEP("3. check queue size 0") {
127 STEP_ASSERT_EQ(collector.LockGetFrameQueue().GetSize(), 0);
128 collector.UnlockFrameQueue();
129 }
130
131 STEP("4. mark WaitVsyncStart") {
132 collector.MarkFrameEvent(FrameEventType::WaitVsyncStart);
133 }
134
135 STEP("5. check queue size 1") {
136 STEP_ASSERT_EQ(collector.LockGetFrameQueue().GetSize(), 1);
137 collector.UnlockFrameQueue();
138 }
139
140 STEP("6. mark WaitVsyncStart") {
141 collector.MarkFrameEvent(FrameEventType::WaitVsyncStart);
142 }
143
144 STEP("7. check queue size 2") {
145 STEP_ASSERT_EQ(collector.LockGetFrameQueue().GetSize(), 2);
146 collector.UnlockFrameQueue();
147 }
148
149 STEP("8. clear events") {
150 collector.ClearEvents();
151 }
152
153 STEP("9. check queue size 0") {
154 STEP_ASSERT_EQ(collector.LockGetFrameQueue().GetSize(), 0);
155 collector.UnlockFrameQueue();
156 }
157 }
158 }
159
160 /**
161 * @tc.name: FrameCollectorTest
162 * @tc.desc: Test RsNodeCostManagerTest.SetRepaintCallbackTest
163 * @tc.type: FUNC
164 * @tc.require: issueI6FZHQ
165 */
166 HWTEST_F(FrameCollectorTest, SetRepaintCallbackTest, TestSize.Level1)
167 {
168 auto& collector = FrameCollector::GetInstance();
169 std::function<void()> repaint = SetUpTestCase;
170 collector.SetRepaintCallback(repaint);
171 ASSERT_TRUE(collector.repaint_ != nullptr);
172 collector.repaint_ = nullptr;
173 }
174
175 /**
176 * @tc.name: FrameCollectorTest
177 * @tc.desc: Test RsNodeCostManagerTest.MarkFrameEvent2
178 * @tc.type: FUNC
179 * @tc.require: issueI6FZHQ
180 */
181 HWTEST_F(FrameCollectorTest, MarkFrameEvent2, TestSize.Level1)
182 {
183 auto& collector = FrameCollector::GetInstance();
184 collector.MarkFrameEvent(static_cast<FrameEventType>(19));
185 ASSERT_FALSE(collector.usingSaver_);
186 }
187
188 /**
189 * @tc.name: FrameCollectorTest
190 * @tc.desc: Test RsNodeCostManagerTest.MarkFrameEvent3
191 * @tc.type: FUNC
192 * @tc.require: issueI6FZHQ
193 */
194 HWTEST_F(FrameCollectorTest, MarkFrameEvent3, TestSize.Level1)
195 {
196 auto& collector = FrameCollector::GetInstance();
197 collector.usingSaver_ = false;
198 collector.MarkFrameEvent(FrameEventType::UploadStart, 1);
199 collector.MarkFrameEvent(FrameEventType::UploadEnd, 1);
200 collector.ClearEvents();
201 ASSERT_FALSE(collector.usingSaver_);
202 }
203
204 /**
205 * @tc.name: FrameCollectorTest
206 * @tc.desc: Test RsNodeCostManagerTest.ProcessFrameEvent1
207 * @tc.type: FUNC
208 * @tc.require: issueI6FZHQ
209 */
210 HWTEST_F(FrameCollectorTest, ProcessFrameEvent1, TestSize.Level1)
211 {
212 auto& collector = FrameCollector::GetInstance();
213 collector.haveAfterVsync_ = true;
214 collector.ProcessFrameEvent(12, 1);
215 ASSERT_FALSE(collector.usingSaver_);
216 collector.ClearEvents();
217 }
218
219 /**
220 * @tc.name: FrameCollectorTest
221 * @tc.desc: Test RsNodeCostManagerTest.ProcessFrameEvent2
222 * @tc.type: FUNC
223 * @tc.require: issueI6FZHQ
224 */
225 HWTEST_F(FrameCollectorTest, ProcessFrameEvent2, TestSize.Level1)
226 {
227 auto& collector = FrameCollector::GetInstance();
228 collector.haveAfterVsync_ = false;
229 FrameInfo info = FrameInfo();
230 collector.pbefore_ = &info;
231 collector.ProcessFrameEvent(14, 1);
232 collector.pbefore_ = nullptr;
233 ASSERT_FALSE(collector.usingSaver_);
234 collector.ClearEvents();
235 }
236
237 /**
238 * @tc.name: FrameCollectorTest
239 * @tc.desc: Test RsNodeCostManagerTest.ProcessFrameEvent3
240 * @tc.type: FUNC
241 * @tc.require: issueI6FZHQ
242 */
243 HWTEST_F(FrameCollectorTest, ProcessFrameEvent3, TestSize.Level1)
244 {
245 auto& collector = FrameCollector::GetInstance();
246 collector.haveAfterVsync_ = true;
247 collector.ProcessFrameEvent(17, 1);
248 ASSERT_FALSE(collector.usingSaver_);
249 collector.ClearEvents();
250 }
251
252 /**
253 * @tc.name: FrameCollectorTest
254 * @tc.desc: Test RsNodeCostManagerTest.SwitchFunction1
255 * @tc.type: FUNC
256 * @tc.require: issueI6FZHQ
257 */
258 HWTEST_F(FrameCollectorTest, SwitchFunction1, TestSize.Level1)
259 {
260 auto& that = FrameCollector::GetInstance();
261 that.enabled_ = true;
262 std::function<void()> repaint = SetUpTestCase;
263 that.SetRepaintCallback(repaint);
264 FrameCollector::SwitchFunction("debug.graphic.frame", "saver", &that);
265 that.enabled_ = false;
266 that.repaint_ = nullptr;
267 ASSERT_TRUE(that.usingSaver_);
268 }
269
270 /**
271 * @tc.name: FrameCollectorTest
272 * @tc.desc: Test RsNodeCostManagerTest.SwitchFunction2
273 * @tc.type: FUNC
274 * @tc.require: I6R1BQ
275 */
276 HWTEST_F(FrameCollectorTest, SwitchFunction2, TestSize.Level1)
277 {
278 auto& that = FrameCollector::GetInstance();
279 that.enabled_ = true;
280 FrameCollector::SwitchFunction("debug.graphic.frame", "paint", &that);
281 that.repaint_ = nullptr;
282 ASSERT_FALSE(that.usingSaver_);
283 }
284
285 } // namespace Rosen
286 } // namespace OHOS
287