• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <gtest/gtest.h>
17 #include <test_header.h>
18 
19 #include "rs_frame_rate_vote.h"
20 #include "hgm_test_base.h"
21 #include "surface_buffer_impl.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Rosen {
28 class RSFrameRateVoteTest : public HgmTestBase {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp();
33     void TearDown();
34 };
35 
SetUpTestCase()36 void RSFrameRateVoteTest::SetUpTestCase()
37 {
38     HgmTestBase::SetUpTestCase();
39 }
TearDownTestCase()40 void RSFrameRateVoteTest::TearDownTestCase() {}
SetUp()41 void RSFrameRateVoteTest::SetUp() {}
TearDown()42 void RSFrameRateVoteTest::TearDown() {}
43 
44 /**
45  * @tc.name: SetTransactionFlags001
46  * @tc.desc: Verify the result of SetTransactionFlags function
47  * @tc.type: FUNC
48  * @tc.require:
49  */
50 HWTEST_F(RSFrameRateVoteTest, SetTransactionFlags001, Function | SmallTest | Level0)
51 {
52     std::string transactionFlags = "";
53     DelayedSingleton<RSFrameRateVote>::GetInstance()->SetTransactionFlags(transactionFlags);
54     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->transactionFlags_, transactionFlags);
55 }
56 
57 /**
58  * @tc.name: CheckSurfaceAndUi001
59  * @tc.desc: Verify the result of CheckSurfaceAndUi function
60  * @tc.type: FUNC
61  * @tc.require:
62  */
63 HWTEST_F(RSFrameRateVoteTest, CheckSurfaceAndUi001, Function | SmallTest | Level0)
64 {
65     std::shared_ptr<RSVideoFrameRateVote> rsVideoFrameRateVote = std::make_shared<RSVideoFrameRateVote>(0,
66         nullptr, nullptr);
67     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastSurfaceNodeId_ = 1002;
68     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_ = 100;
69     DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface = false;
70     DelayedSingleton<RSFrameRateVote>::GetInstance()->CheckSurfaceAndUi();
71     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface, false);
72     DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface = true;
73     DelayedSingleton<RSFrameRateVote>::GetInstance()->CheckSurfaceAndUi();
74     usleep(1000000);
75     DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface = true;
76     DelayedSingleton<RSFrameRateVote>::GetInstance()->CheckSurfaceAndUi();
77     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 0);
78     usleep(40000);
79     DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface = true;
80     DelayedSingleton<RSFrameRateVote>::GetInstance()->CheckSurfaceAndUi();
81     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 0);
82     usleep(40000);
83     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_ = 100;
84     DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface = true;
85     DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.insert(
86         std::pair<uint64_t, std::shared_ptr<RSVideoFrameRateVote>>(1002, nullptr));
87     DelayedSingleton<RSFrameRateVote>::GetInstance()->CheckSurfaceAndUi();
88     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 0);
89     usleep(40000);
90     DelayedSingleton<RSFrameRateVote>::GetInstance()->ReleaseSurfaceMap(1002);
91     sleep(1);
92     DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface = true;
93     DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.insert(
94         std::pair<uint64_t, std::shared_ptr<RSVideoFrameRateVote>>(1002, rsVideoFrameRateVote));
95     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_ = 100;
96     DelayedSingleton<RSFrameRateVote>::GetInstance()->CheckSurfaceAndUi();
97     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 0);
98     usleep(40000);
99     DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface = true;
100     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_ = 0;
101     DelayedSingleton<RSFrameRateVote>::GetInstance()->CheckSurfaceAndUi();
102     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 0);
103     DelayedSingleton<RSFrameRateVote>::GetInstance()->ReleaseSurfaceMap(1002);
104 }
105 
106 
107 HWTEST_F(RSFrameRateVoteTest, VideoFrameRateVote001, Function | SmallTest | Level0)
108 {
109     sptr<SurfaceBuffer> nullBuffer = nullptr;
110     sptr<SurfaceBuffer> buffer = new SurfaceBufferImpl();
111     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = false;
112     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
113         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, nullBuffer);
114     usleep(500000);
115     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
116     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = true;
117     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
118         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, nullBuffer);
119     usleep(500000);
120     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
121     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = false;
122     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
123         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, nullBuffer);
124     usleep(500000);
125     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
126     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = false;
127     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
128         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, buffer);
129     usleep(500000);
130     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
131     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = false;
132     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
133         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, buffer);
134     usleep(500000);
135     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
136     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = true;
137     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
138         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, buffer);
139     usleep(500000);
140     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
141     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = true;
142     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
143         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, nullBuffer);
144     usleep(500000);
145     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
146     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = true;
147     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
148         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, buffer);
149     usleep(500000);
150     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 1);
151 }
152 
153 /**
154  * @tc.name: VideoFrameRateVote002
155  * @tc.desc: Verify the result of VideoFrameRateVote function
156  * @tc.type: FUNC
157  * @tc.require:
158  */
159 HWTEST_F(RSFrameRateVoteTest, VideoFrameRateVote002, Function | SmallTest | Level0) {
160     sptr<SurfaceBuffer> nullBuffer = nullptr;
161     DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface = false;
162 
163     DelayedSingleton<RSFrameRateVote>::GetInstance()->transactionFlags_ = "[500,30]";
164     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedPid_ = 0;
165     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
166         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, nullBuffer);
167     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface, false);
168 
169     DelayedSingleton<RSFrameRateVote>::GetInstance()->transactionFlags_ = "";
170     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedPid_ = 0;
171     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
172         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, nullBuffer);
173     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface, false);
174 
175     DelayedSingleton<RSFrameRateVote>::GetInstance()->transactionFlags_ = "";
176     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedPid_ = 0;
177     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
178         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, nullBuffer);
179     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface, true);
180 
181     DelayedSingleton<RSFrameRateVote>::GetInstance()->transactionFlags_ = "[0,30]";
182     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedPid_ = 0;
183     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
184         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, nullBuffer);
185     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface, true);
186 
187     DelayedSingleton<RSFrameRateVote>::GetInstance()->transactionFlags_ = "[500,30]";
188     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedPid_ = 0;
189     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
190         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, nullBuffer);
191     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface, true);
192 
193     DelayedSingleton<RSFrameRateVote>::GetInstance()->transactionFlags_ = "[0,30]";
194     DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedPid_ = 0;
195     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
196         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, nullBuffer);
197     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->hasUiOrSurface, true);
198 }
199 
200 /**
201  * @tc.name: ReleaseSurfaceMap001
202  * @tc.desc: Verify the result of ReleaseSurfaceMap function
203  * @tc.type: FUNC
204  * @tc.require:
205  */
206 HWTEST_F(RSFrameRateVoteTest, ReleaseSurfaceMap001, Function | SmallTest | Level0)
207 {
208     std::shared_ptr<RSVideoFrameRateVote> rsVideoFrameRateVote = std::make_shared<RSVideoFrameRateVote>(0,
209         nullptr, nullptr);
210     DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.insert(
211         std::pair<uint64_t, std::shared_ptr<RSVideoFrameRateVote>>(1001, rsVideoFrameRateVote));
212     DelayedSingleton<RSFrameRateVote>::GetInstance()->ReleaseSurfaceMap(1000);
213     sleep(1);
214     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 1);
215     DelayedSingleton<RSFrameRateVote>::GetInstance()->ReleaseSurfaceMap(1001);
216     sleep(1);
217     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
218     rsVideoFrameRateVote = nullptr;
219 }
220 
221 /**
222  * @tc.name: SurfaceVideoVote001
223  * @tc.desc: Verify the result of SurfaceVideoVote function
224  * @tc.type: FUNC
225  * @tc.require:
226  */
227 HWTEST_F(RSFrameRateVoteTest, SurfaceVideoVote001, Function | SmallTest | Level0)
228 {
229     std::shared_ptr<RSVideoFrameRateVote> rsVideoFrameRateVote = std::make_shared<RSVideoFrameRateVote>(0,
230         nullptr, nullptr);
231     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1000, 30);
232     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 1);
233     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_[1000], 30);
234     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 30);
235     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1001, 60);
236     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 2);
237     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_[1001], 60);
238     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 60);
239     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1002, 30);
240     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 3);
241     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_[1002], 30);
242     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 60);
243     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1000, 0);
244     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 2);
245     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 60);
246     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1001, 0);
247     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 1);
248     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 30);
249     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1002, 0);
250     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 0);
251     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 0);
252     rsVideoFrameRateVote = nullptr;
253 }
254 
255 /**
256  * @tc.name: VoteRate001
257  * @tc.desc: Verify the result of VoteRate function
258  * @tc.type: FUNC
259  * @tc.require:
260  */
261 HWTEST_F(RSFrameRateVoteTest, VoteRate001, Function | SmallTest | Level0)
262 {
263     ASSERT_FALSE(DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_);
264     DelayedSingleton<RSFrameRateVote>::GetInstance()->VoteRate(DEFAULT_PID, "VOTER_VIDEO", 30);
265     ASSERT_TRUE(DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_);
266 }
267 
268 /**
269  * @tc.name: CancelVoteRate001
270  * @tc.desc: Verify the result of CancelVoteRate function
271  * @tc.type: FUNC
272  * @tc.require:
273  */
274 HWTEST_F(RSFrameRateVoteTest, CancelVoteRate001, Function | SmallTest | Level0)
275 {
276     DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_ = true;
277     ASSERT_TRUE(DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_);
278     DelayedSingleton<RSFrameRateVote>::GetInstance()->CancelVoteRate(DEFAULT_PID, "VOTER_VIDEO");
279     ASSERT_FALSE(DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_);
280 }
281 
282 /**
283  * @tc.name: NotifyRefreshRateEvent001
284  * @tc.desc: Verify the result of NotifyRefreshRateEvent function
285  * @tc.type: FUNC
286  * @tc.require:
287  */
288 HWTEST_F(RSFrameRateVoteTest, NotifyRefreshRateEvent001, Function | SmallTest | Level0)
289 {
290     EventInfo eventInfo1 = {
291         .eventName = "VOTER_VIDEO",
292         .eventStatus = true,
293         .minRefreshRate = 60,
294         .maxRefreshRate = 60,
295     };
296     if (DelayedSingleton<RSFrameRateVote>::GetInstance() == nullptr) {
297         return;
298     }
299     DelayedSingleton<RSFrameRateVote>::GetInstance()->NotifyRefreshRateEvent(DEFAULT_PID, eventInfo1);
300     ASSERT_FALSE(DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_);
301     EventInfo eventInfo2 = {
302         .eventName = "VOTER_VIDEO",
303         .eventStatus = false,
304     };
305     DelayedSingleton<RSFrameRateVote>::GetInstance()->NotifyRefreshRateEvent(DEFAULT_PID, eventInfo2);
306     ASSERT_FALSE(DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_);
307     DelayedSingleton<RSFrameRateVote>::GetInstance()->NotifyRefreshRateEvent(DEFAULT_PID, eventInfo2);
308     pid_t pid1 = 1;
309     DelayedSingleton<RSFrameRateVote>::GetInstance()->NotifyRefreshRateEvent(pid1, eventInfo2);
310 }
311 } // namespace Rosen
312 } // namespace OHOS