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 #define private public
20 #include "rs_frame_rate_vote.h"
21 #undef private
22 #include "hgm_core.h"
23 #include "surface_buffer_impl.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace Rosen {
30 namespace {
31 }
32 class RSFrameRateVoteTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp();
37     void TearDown();
38 };
39 
SetUpTestCase()40 void RSFrameRateVoteTest::SetUpTestCase() {}
TearDownTestCase()41 void RSFrameRateVoteTest::TearDownTestCase() {}
SetUp()42 void RSFrameRateVoteTest::SetUp() {}
TearDown()43 void RSFrameRateVoteTest::TearDown() {}
44 
45 /**
46  * @tc.name: VideoFrameRateVote001
47  * @tc.desc: Verify the result of VideoFrameRateVote function
48  * @tc.type: FUNC
49  * @tc.require:
50  */
51 HWTEST_F(RSFrameRateVoteTest, VideoFrameRateVote001, Function | SmallTest | Level1)
52 {
53     sptr<SurfaceBuffer> nullBuffer = nullptr;
54     sptr<SurfaceBuffer> buffer = new SurfaceBufferImpl();
55     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = false;
56     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
57         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, nullBuffer);
58     usleep(500000);
59     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
60     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = true;
61     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
62         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, nullBuffer);
63     usleep(500000);
64     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
65     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = false;
66     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
67         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, nullBuffer);
68     usleep(500000);
69     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
70     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = false;
71     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
72         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, buffer);
73     usleep(500000);
74     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
75     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = false;
76     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
77         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, buffer);
78     usleep(500000);
79     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
80     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = true;
81     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
82         1000, OHSurfaceSource::OH_SURFACE_SOURCE_DEFAULT, buffer);
83     usleep(500000);
84     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
85     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = true;
86     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
87         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, nullBuffer);
88     usleep(500000);
89     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
90     DelayedSingleton<RSFrameRateVote>::GetInstance()->isSwitchOn_ = true;
91     DelayedSingleton<RSFrameRateVote>::GetInstance()->VideoFrameRateVote(
92         1000, OHSurfaceSource::OH_SURFACE_SOURCE_VIDEO, buffer);
93     usleep(500000);
94     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 1);
95 }
96 
97 /**
98  * @tc.name: ReleaseSurfaceMap001
99  * @tc.desc: Verify the result of ReleaseSurfaceMap function
100  * @tc.type: FUNC
101  * @tc.require:
102  */
103 HWTEST_F(RSFrameRateVoteTest, ReleaseSurfaceMap001, Function | SmallTest | Level1)
104 {
105     std::shared_ptr<RSVideoFrameRateVote> rsVideoFrameRateVote = std::make_shared<RSVideoFrameRateVote>(0,
106         nullptr, nullptr);
107     DelayedSingleton<RSFrameRateVote>::GetInstance()->frameRateMgr_ = nullptr;
108     DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.insert(
109         std::pair<uint64_t, std::shared_ptr<RSVideoFrameRateVote>>(1001, rsVideoFrameRateVote));
110     DelayedSingleton<RSFrameRateVote>::GetInstance()->ReleaseSurfaceMap(1000);
111     sleep(1);
112     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 1);
113     DelayedSingleton<RSFrameRateVote>::GetInstance()->ReleaseSurfaceMap(1001);
114     sleep(1);
115     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoFrameRateVote_.size(), 0);
116     rsVideoFrameRateVote = nullptr;
117 }
118 
119 /**
120  * @tc.name: SurfaceVideoVote001
121  * @tc.desc: Verify the result of SurfaceVideoVote function
122  * @tc.type: FUNC
123  * @tc.require:
124  */
125 HWTEST_F(RSFrameRateVoteTest, SurfaceVideoVote001, Function | SmallTest | Level1)
126 {
127     std::shared_ptr<RSVideoFrameRateVote> rsVideoFrameRateVote = std::make_shared<RSVideoFrameRateVote>(0,
128         nullptr, nullptr);
129     DelayedSingleton<RSFrameRateVote>::GetInstance()->frameRateMgr_ = nullptr;
130     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1000, 30);
131     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 1);
132     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_[1000], 30);
133     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 30);
134     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1001, 60);
135     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 2);
136     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_[1001], 60);
137     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 60);
138     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1002, 30);
139     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 3);
140     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_[1002], 30);
141     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 60);
142     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1000, 0);
143     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 2);
144     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 60);
145     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1001, 0);
146     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 1);
147     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 30);
148     DelayedSingleton<RSFrameRateVote>::GetInstance()->SurfaceVideoVote(1002, 0);
149     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->surfaceVideoRate_.size(), 0);
150     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->lastVotedRate_, 0);
151     rsVideoFrameRateVote = nullptr;
152 }
153 
154 /**
155  * @tc.name: VoteRate001
156  * @tc.desc: Verify the result of VoteRate function
157  * @tc.type: FUNC
158  * @tc.require:
159  */
160 HWTEST_F(RSFrameRateVoteTest, VoteRate001, Function | SmallTest | Level1)
161 {
162     DelayedSingleton<RSFrameRateVote>::GetInstance()->frameRateMgr_ = nullptr;
163     ASSERT_FALSE(DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_);
164     DelayedSingleton<RSFrameRateVote>::GetInstance()->VoteRate(DEFAULT_PID, "VOTER_VIDEO", 30);
165     ASSERT_TRUE(DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_);
166 }
167 
168 /**
169  * @tc.name: CancelVoteRate001
170  * @tc.desc: Verify the result of CancelVoteRate function
171  * @tc.type: FUNC
172  * @tc.require:
173  */
174 HWTEST_F(RSFrameRateVoteTest, CancelVoteRate001, Function | SmallTest | Level1)
175 {
176     DelayedSingleton<RSFrameRateVote>::GetInstance()->frameRateMgr_ = nullptr;
177     DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_ = true;
178     ASSERT_TRUE(DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_);
179     DelayedSingleton<RSFrameRateVote>::GetInstance()->CancelVoteRate(DEFAULT_PID, "VOTER_VIDEO");
180     ASSERT_FALSE(DelayedSingleton<RSFrameRateVote>::GetInstance()->isVoted_);
181 }
182 
183 /**
184  * @tc.name: NotifyRefreshRateEvent001
185  * @tc.desc: Verify the result of NotifyRefreshRateEvent function
186  * @tc.type: FUNC
187  * @tc.require:
188  */
189 HWTEST_F(RSFrameRateVoteTest, NotifyRefreshRateEvent001, Function | SmallTest | Level1)
190 {
191     DelayedSingleton<RSFrameRateVote>::GetInstance()->frameRateMgr_ =
192         OHOS::Rosen::HgmCore::Instance().GetFrameRateMgr();
193     ASSERT_NE(DelayedSingleton<RSFrameRateVote>::GetInstance()->frameRateMgr_, nullptr);
194     EventInfo eventInfo1 = {
195         .eventName = "VOTER_VIDEO",
196         .eventStatus = true,
197         .minRefreshRate = 60,
198         .maxRefreshRate = 60,
199     };
200     DelayedSingleton<RSFrameRateVote>::GetInstance()->NotifyRefreshRateEvent(DEFAULT_PID, eventInfo1);
201     EventInfo eventInfo2 = {
202         .eventName = "VOTER_VIDEO",
203         .eventStatus = false,
204     };
205     DelayedSingleton<RSFrameRateVote>::GetInstance()->NotifyRefreshRateEvent(DEFAULT_PID, eventInfo2);
206     DelayedSingleton<RSFrameRateVote>::GetInstance()->frameRateMgr_ = nullptr;
207     DelayedSingleton<RSFrameRateVote>::GetInstance()->NotifyRefreshRateEvent(DEFAULT_PID, eventInfo2);
208     ASSERT_EQ(DelayedSingleton<RSFrameRateVote>::GetInstance()->frameRateMgr_, nullptr);
209 }
210 } // namespace Rosen
211 } // namespace OHOS