1 /*
2  * Copyright (c) 2024 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, software
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 #include <gtest/gtest.h>
16 #include "frame_report.h"
17 
18 using namespace testing;
19 using namespace testing::ext;
20 
21 namespace {
22     static const int32_t FRT_GAME_ERROR_PID = -1;
23     static const int32_t FRT_GAME_PID = 1024;
24     static const int32_t FRT_GAME_ERROR_UNIQUEID = -1;
25     static const int32_t FRT_GAME_UNIQUEID = 1024;
26     static const int32_t FRT_GAME_ERROR_STATE = -1;
27     static const int32_t FRT_GAME_BACKGROUND = 0;
28     static const int32_t FRT_GAME_FOREGROUND = 1;
29     static const int32_t FRT_GAME_SCHED = 2;
30     static const int64_t FRT_GAME_BUFFER_TIME = 2048;
31     static std::string FRT_SURFACE_NAME_EMPTY = "";
32     static std::string FRT_SURFACE_NAME = "SurfaceTEST";
33 }
34 
35 namespace OHOS::Rosen {
36 class FrameReportTest : public testing::Test {
37 public:
38     static void SetUpTestSuite(void);
39     static void TearDownTestSuite(void);
40     void SetUp();
41     void TearDown();
42 };
43 
SetUpTestSuite(void)44 void FrameReportTest::SetUpTestSuite(void) {}
45 
TearDownTestSuite(void)46 void FrameReportTest::TearDownTestSuite(void) {}
47 
SetUp()48 void FrameReportTest::SetUp() {}
49 
TearDown()50 void FrameReportTest::TearDown() {}
51 
52 /*
53 * Function: SetGameScene
54 * Type: Function
55 * Rank: Important(2)
56 * EnvConditions: N/A
57 * CaseDescription: 1. call SetGameScene
58 *                  2. check ret
59  */
60 HWTEST_F(FrameReportTest, SetGameScene001, Function | MediumTest | Level2)
61 {
62     Rosen::FrameReport::GetInstance().SetGameScene(FRT_GAME_PID, FRT_GAME_BACKGROUND);
63     ASSERT_TRUE(Rosen::FrameReport::GetInstance().activelyPid_.load() == FR_DEFAULT_PID);
64 
65     Rosen::FrameReport::GetInstance().SetGameScene(FRT_GAME_PID, FRT_GAME_FOREGROUND);
66 
67     Rosen::FrameReport::GetInstance().SetGameScene(FRT_GAME_PID, FRT_GAME_SCHED);
68     ASSERT_TRUE(Rosen::FrameReport::GetInstance().activelyPid_.load() == FRT_GAME_PID);
69 
70     Rosen::FrameReport::GetInstance().SetGameScene(FRT_GAME_PID, FRT_GAME_BACKGROUND);
71     ASSERT_TRUE(Rosen::FrameReport::GetInstance().activelyPid_.load() == FR_DEFAULT_PID);
72 
73     Rosen::FrameReport::GetInstance().SetGameScene(FRT_GAME_PID, FRT_GAME_ERROR_STATE);
74     ASSERT_TRUE(Rosen::FrameReport::GetInstance().activelyPid_.load() == FR_DEFAULT_PID);
75 }
76 
77 /*
78 * Function: HasGameScene
79 * Type: Function
80 * Rank: Important(2)
81 * EnvConditions: N/A
82 * CaseDescription: 1. call HasGameScene
83 *                  2. check ret
84  */
85 HWTEST_F(FrameReportTest, HasGameScene001, Function | MediumTest | Level2)
86 {
87     Rosen::FrameReport::GetInstance().SetGameScene(FRT_GAME_PID, FRT_GAME_BACKGROUND);
88     bool result = Rosen::FrameReport::GetInstance().HasGameScene();
89     ASSERT_TRUE(!result);
90 
91     Rosen::FrameReport::GetInstance().SetGameScene(FRT_GAME_PID, FRT_GAME_SCHED);
92     result = Rosen::FrameReport::GetInstance().HasGameScene();
93     ASSERT_TRUE(result);
94 }
95 
96 /*
97 * Function: IsActiveGameWithPid
98 * Type: Function
99 * Rank: Important(2)
100 * EnvConditions: N/A
101 * CaseDescription: 1. call IsActiveGameWithPid
102 *                  2. check ret
103  */
104 HWTEST_F(FrameReportTest, IsActiveGameWithPid001, Function | MediumTest | Level2)
105 {
106     bool result = Rosen::FrameReport::GetInstance().IsActiveGameWithPid(FRT_GAME_ERROR_PID);
107     ASSERT_TRUE(!result);
108 
109     result = Rosen::FrameReport::GetInstance().IsActiveGameWithPid(FRT_GAME_PID);
110     ASSERT_TRUE(result);
111 }
112 
113 /*
114 * Function: IsActiveGameWithUniqueId
115 * Type: Function
116 * Rank: Important(2)
117 * EnvConditions: N/A
118 * CaseDescription: 1. call IsActiveGameWithUniqueId
119 *                  2. check ret
120  */
121 HWTEST_F(FrameReportTest, IsActiveGameWithUniqueId001, Function | MediumTest | Level2)
122 {
123     bool result = Rosen::FrameReport::GetInstance().IsActiveGameWithUniqueId(FRT_GAME_ERROR_UNIQUEID);
124     ASSERT_TRUE(!result);
125 
126     result = Rosen::FrameReport::GetInstance().IsActiveGameWithUniqueId(FRT_GAME_UNIQUEID);
127     ASSERT_TRUE(!result);
128 }
129 
130 /*
131 * Function: SetLastSwapBufferTime
132 * Type: Function
133 * Rank: Important(2)
134 * EnvConditions: N/A
135 * CaseDescription: 1. call SetLastSwapBufferTime
136 *                  2. check ret
137  */
138 HWTEST_F(FrameReportTest, SetLastSwapBufferTime001, Function | MediumTest | Level2)
139 {
140     Rosen::FrameReport::GetInstance().SetLastSwapBufferTime(FRT_GAME_BUFFER_TIME);
141     ASSERT_TRUE(Rosen::FrameReport::GetInstance().lastSwapBufferTime_.load() == FRT_GAME_BUFFER_TIME);
142 }
143 
144 /*
145 * Function: SetDequeueBufferTime
146 * Type: Function
147 * Rank: Important(2)
148 * EnvConditions: N/A
149 * CaseDescription: 1. call SetDequeueBufferTime
150 *                  2. check ret
151  */
152 HWTEST_F(FrameReportTest, SetDequeueBufferTime001, Function | MediumTest | Level2)
153 {
154     Rosen::FrameReport::GetInstance().SetDequeueBufferTime(FRT_SURFACE_NAME_EMPTY, FRT_GAME_BUFFER_TIME);
155     ASSERT_TRUE(Rosen::FrameReport::GetInstance().dequeueBufferTime_.load() == FRT_GAME_BUFFER_TIME);
156 
157     Rosen::FrameReport::GetInstance().SetDequeueBufferTime(FRT_SURFACE_NAME, FRT_GAME_BUFFER_TIME);
158     ASSERT_TRUE(Rosen::FrameReport::GetInstance().dequeueBufferTime_.load() == FRT_GAME_BUFFER_TIME);
159 }
160 
161 /*
162 * Function: SetQueueBufferTime
163 * Type: Function
164 * Rank: Important(2)
165 * EnvConditions: N/A
166 * CaseDescription: 1. call SetQueueBufferTime
167 *                  2. check ret
168  */
169 HWTEST_F(FrameReportTest, SetQueueBufferTime001, Function | MediumTest | Level2)
170 {
171     Rosen::FrameReport::GetInstance().SetQueueBufferTime(FRT_GAME_UNIQUEID, FRT_SURFACE_NAME_EMPTY,
172                                                          FRT_GAME_BUFFER_TIME);
173     ASSERT_TRUE(Rosen::FrameReport::GetInstance().queueBufferTime_.load() == FRT_GAME_BUFFER_TIME);
174 
175     Rosen::FrameReport::GetInstance().SetQueueBufferTime(FRT_GAME_UNIQUEID, FRT_SURFACE_NAME, FRT_GAME_BUFFER_TIME);
176     ASSERT_TRUE(Rosen::FrameReport::GetInstance().queueBufferTime_.load() == FRT_GAME_BUFFER_TIME);
177 }
178 
179 /*
180 * Function: SetPendingBufferNum
181 * Type: Function
182 * Rank: Important(2)
183 * EnvConditions: N/A
184 * CaseDescription: 1. call SetPendingBufferNum
185 *                  2. check ret
186  */
187 HWTEST_F(FrameReportTest, SetPendingBufferNum001, Function | MediumTest | Level2)
188 {
189     Rosen::FrameReport::GetInstance().SetPendingBufferNum(FRT_SURFACE_NAME_EMPTY, FRT_GAME_BUFFER_TIME);
190     ASSERT_TRUE(Rosen::FrameReport::GetInstance().pendingBufferNum_.load() == FRT_GAME_BUFFER_TIME);
191 
192     Rosen::FrameReport::GetInstance().SetPendingBufferNum(FRT_SURFACE_NAME, FRT_GAME_BUFFER_TIME);
193     ASSERT_TRUE(Rosen::FrameReport::GetInstance().pendingBufferNum_.load() == FRT_GAME_BUFFER_TIME);
194 }
195 
196 /*
197 * Function: Report
198 * Type: Function
199 * Rank: Important(2)
200 * EnvConditions: N/A
201 * CaseDescription: 1. call Report
202  */
203 HWTEST_F(FrameReportTest, Report001, Function | MediumTest | Level2)
204 {
205     Rosen::FrameReport::GetInstance().Report(FRT_SURFACE_NAME_EMPTY);
206     Rosen::FrameReport::GetInstance().Report(FRT_SURFACE_NAME);
207     ASSERT_TRUE(Rosen::FrameReport::GetInstance().pendingBufferNum_.load() == FRT_GAME_BUFFER_TIME);
208 }
209 
210 /*
211 * Function: CloseLibrary
212 * Type: Function
213 * Rank: Important(2)
214 * EnvConditions: N/A
215 * CaseDescription: 1. call CloseLibrary
216  */
217 HWTEST_F(FrameReportTest, CloseLibrary001, Function | MediumTest | Level2)
218 {
219     Rosen::FrameReport::GetInstance().CloseLibrary();
220     EXPECT_EQ(Rosen::FrameReport::GetInstance().notifyFrameInfoFunc_, nullptr);
221     EXPECT_EQ(Rosen::FrameReport::GetInstance().gameSoHandle_, nullptr);
222     EXPECT_EQ(Rosen::FrameReport::GetInstance().isGameSoLoaded_, false);
223 }
224 
225 /*
226 * Function: DeletePidInfo
227 * Type: Function
228 * Rank: Important(2)
229 * EnvConditions: N/A
230 * CaseDescription: 1. call DeletePidInfo
231 *                  2. check ret
232  */
233 HWTEST_F(FrameReportTest, DeletePidInfo001, Function | MediumTest | Level2)
234 {
235     Rosen::FrameReport::GetInstance().DeletePidInfo();
236     ASSERT_TRUE(Rosen::FrameReport::GetInstance().activelyPid_.load() == FR_DEFAULT_PID);
237 }
238 
239 /*
240 * Function: NotifyFrameInfo
241 * Type: Function
242 * Rank: Important(2)
243 * EnvConditions: N/A
244 * CaseDescription: 1. call NotifyFrameInfo
245 *                  2. check ret
246  */
247 HWTEST_F(FrameReportTest, NotifyFrameInfo001, Function | MediumTest | Level2)
248 {
249     Rosen::FrameReport::GetInstance().notifyFrameInfoFunc_ = nullptr;
250     Rosen::FrameReport::GetInstance().NotifyFrameInfo(FRT_GAME_PID, FRT_SURFACE_NAME, FRT_GAME_BUFFER_TIME,
251                                                       FRT_SURFACE_NAME_EMPTY);
252     ASSERT_TRUE(Rosen::FrameReport::GetInstance().notifyFrameInfoFunc_ == nullptr);
253 }
254 
255 } // namespace OHOS::Rosen
256