• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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, 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 "display_power_mgr_service_test.h"
17 
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20 #include <vector>
21 
22 #include "display_power_mgr_client.h"
23 #include "display_log.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::DisplayPowerMgr;
28 
29 namespace {
30 DisplayState beforeDisplayState;
31 }
32 
SetUp()33 void DisplayPowerMgrServiceTest::SetUp()
34 {
35     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrServiceTest SetUp is start");
36     beforeDisplayState = DisplayPowerMgrClient::GetInstance().GetDisplayState();
37     DISPLAY_HILOGI(LABEL_TEST, "Get displaystate before run testcase, beforeDisplaystate: %{public}d",
38         beforeDisplayState);
39     return;
40 }
41 
TearDown()42 void DisplayPowerMgrServiceTest::TearDown()
43 {
44     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrServiceTest TearDown is start");
45     DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState();
46     DISPLAY_HILOGI(LABEL_TEST, "Get displaystate after run testcase, state: %{public}d", state);
47     if (state != beforeDisplayState) {
48         DisplayPowerMgrClient::GetInstance().SetDisplayState(beforeDisplayState);
49         DISPLAY_HILOGI(LABEL_TEST, "Set displaystate %{public}d to %{public}d",
50             state, beforeDisplayState);
51         return;
52     }
53     return;
54 }
55 
56 namespace {
57 /**
58  * @tc.name: DisplayPowerMgrService001
59  * @tc.desc: Test DisplayPowerMgrService service ready.
60  * @tc.type: FUNC
61  */
62 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService001, TestSize.Level0)
63 {
64     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService01 is start");
65     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
66     EXPECT_TRUE(sam != nullptr) << "DisplayPowerMgrService01 fail to get GetSystemAbilityManager";
67     sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(DISPLAY_MANAGER_SERVICE_ID);
68     EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed.";
69 }
70 
71 /**
72  * @tc.name: DisplayPowerMgrService002
73  * @tc.desc: Test set screen state off
74  * @tc.type: FUNC
75  */
76 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService002, TestSize.Level0)
77 {
78     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService002 is start");
79     auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF);
80     EXPECT_TRUE(ret);
81 }
82 
83 /**
84  * @tc.name: DisplayPowerMgrService003
85  * @tc.desc: Test set screen state on
86  * @tc.type: FUNC
87  */
88 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService003, TestSize.Level0)
89 {
90     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService003 is start");
91     auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_ON);
92     EXPECT_TRUE(ret);
93 }
94 
95 /**
96  * @tc.name: DisplayPowerMgrService004
97  * @tc.desc: Test set screen state dim
98  * @tc.type: FUNC
99  */
100 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService004, TestSize.Level0)
101 {
102     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService004 is start");
103     auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_DIM);
104     EXPECT_TRUE(ret);
105 }
106 
107 /**
108  * @tc.name: DisplayPowerMgrService005
109  * @tc.desc: Test set screen state suspend
110  * @tc.type: FUNC
111  */
112 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService005, TestSize.Level0)
113 {
114     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService005 is start");
115     auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_SUSPEND);
116     EXPECT_TRUE(ret);
117 }
118 
119 /**
120  * @tc.name: DisplayPowerMgrService006
121  * @tc.desc: Test set screen state unknown
122  * @tc.type: FUNC
123  */
124 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService006, TestSize.Level0)
125 {
126     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService006 is start");
127     auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_UNKNOWN);
128     EXPECT_TRUE(ret);
129 }
130 
131 /**
132  * @tc.name: DisplayPowerMgrService007
133  * @tc.desc: Test set screen state on
134  * @tc.type: FUNC
135  */
136 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService007, TestSize.Level0)
137 {
138     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService007 is start");
139     DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF);
140     auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_ON);
141     EXPECT_TRUE(ret);
142     DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState();
143     EXPECT_TRUE(state == DisplayState::DISPLAY_ON);
144 }
145 
146 /**
147  * @tc.name: DisplayPowerMgrService008
148  * @tc.desc: Test set screen state dim
149  * @tc.type: FUNC
150  */
151 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService008, TestSize.Level0)
152 {
153     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService008 is start");
154     auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_DIM);
155     EXPECT_TRUE(ret);
156     DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState();
157     EXPECT_TRUE(state == DisplayState::DISPLAY_DIM);
158 }
159 
160 /**
161  * @tc.name: DisplayPowerMgrService009
162  * @tc.desc: Test set screen state off
163  * @tc.type: FUNC
164  */
165 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService009, TestSize.Level0)
166 {
167     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService009 is start");
168     auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF);
169     EXPECT_TRUE(ret);
170     DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState();
171     EXPECT_TRUE(state == DisplayState::DISPLAY_OFF);
172 }
173 
174 /**
175  * @tc.name: DisplayPowerMgrService010
176  * @tc.desc: Test set screen state suspend
177  * @tc.type: FUNC
178  */
179 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService010, TestSize.Level0)
180 {
181     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService010 is start");
182     auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_SUSPEND);
183     EXPECT_TRUE(ret);
184     DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState();
185     EXPECT_TRUE(state == DisplayState::DISPLAY_SUSPEND);
186 }
187 
188 /**
189  * @tc.name: DisplayPowerMgrService011
190  * @tc.desc: Test set screen state unknown
191  * @tc.type: FUNC
192  */
193 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService011, TestSize.Level0)
194 {
195     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService011 is start");
196     auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_UNKNOWN);
197     EXPECT_TRUE(ret);
198     DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState();
199     EXPECT_TRUE(state == DisplayState::DISPLAY_UNKNOWN);
200 }
201 
202 /**
203  * @tc.name: DisplayPowerMgrService012
204  * @tc.desc: Test GetDisplayIds
205  * @tc.type: FUNC
206  */
207 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService012, TestSize.Level0)
208 {
209     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService012 is start");
210     std::vector<uint32_t> ret = DisplayPowerMgrClient::GetInstance().GetDisplayIds();
211     EXPECT_TRUE(ret.size() != 0);
212 }
213 
214 /**
215  * @tc.name: DisplayPowerMgrService013
216  * @tc.desc: Test GetMainDisplayId
217  * @tc.type: FUNC
218  */
219 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService013, TestSize.Level0)
220 {
221     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService013 is start");
222     int32_t ret = DisplayPowerMgrClient::GetInstance().GetMainDisplayId();
223     EXPECT_TRUE(ret == 0);
224 }
225 
226 /**
227  * @tc.name: DisplayPowerMgrService014
228  * @tc.desc: Test AutoAdjustBrightness
229  * @tc.type: FUNC
230  */
231 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService014, TestSize.Level0)
232 {
233     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService014 is start");
234     bool ret = DisplayPowerMgrClient::GetInstance().AutoAdjustBrightness(true);
235     if (ret) {
236         DISPLAY_HILOGI(LABEL_TEST, "AutoAdjustBrightness: is supported");
237         ret = DisplayPowerMgrClient::GetInstance().AutoAdjustBrightness(false);
238         EXPECT_TRUE(ret);
239     } else {
240         DISPLAY_HILOGI(LABEL_TEST, "AutoAdjustBrightness: is not supported");
241         EXPECT_FALSE(ret);
242     }
243 }
244 
245 /**
246  * @tc.name: DisplayPowerMgrService015
247  * @tc.desc: Test GetError
248  * @tc.type: FUNC
249  */
250 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService015, TestSize.Level0)
251 {
252     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService015 is start");
253     DisplayErrors error = DisplayPowerMgrClient::GetInstance().GetError();
254     DISPLAY_HILOGI(LABEL_TEST, "get error %{public}d", static_cast<int32_t>(error));
255     EXPECT_TRUE(error == DisplayErrors::ERR_OK);
256 }
257 
258 /**
259  * @tc.name: DisplayPowerMgrService016
260  * @tc.desc: Test RegisterCallback(Input callback is nullptr, function return false)
261  * @tc.type: FUNC
262  * @tc.require: issueI63PTF
263  */
264 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService016, TestSize.Level0)
265 {
266     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService016 is start");
267     bool result = DisplayPowerMgrClient::GetInstance().RegisterCallback(nullptr);
268     EXPECT_EQ(result, false);
269 }
270 
271 /**
272  * @tc.name: DisplayPowerMgrService017
273  * @tc.desc: Test OverrideDisplayOffDelay para
274  * @tc.type: FUNC
275  */
276 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService017, TestSize.Level0)
277 {
278     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService017 is start");
279     bool ret = DisplayPowerMgrClient::GetInstance().OverrideDisplayOffDelay(0);
280     EXPECT_FALSE(ret);
281 }
282 
283 /**
284  * @tc.name: DisplayPowerMgrService018
285  * @tc.desc: Test prohibitting writing delay screen off time in screen off state
286  * @tc.type: FUNC
287  */
288 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService018, TestSize.Level0)
289 {
290     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService018 is start");
291     DisplayState State = DisplayPowerMgrClient::GetInstance().GetDisplayState();
292     if (State != DisplayState::DISPLAY_OFF) {
293         DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF);
294     }
295     bool ret = DisplayPowerMgrClient::GetInstance().OverrideDisplayOffDelay(10);
296     EXPECT_FALSE(ret);
297 }
298 
299 /**
300  * @tc.name: DisplayPowerMgrService019
301  * @tc.desc: Test srceen delay off
302  * @tc.type: FUNC
303  */
304 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService019, TestSize.Level0)
305 {
306     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService019 is start");
307     DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_ON);
308     DisplayPowerMgrClient::GetInstance().OverrideDisplayOffDelay(10);
309     DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF);
310     DisplayState delayState = DisplayPowerMgrClient::GetInstance().GetDisplayState();
311     EXPECT_TRUE(delayState == DisplayState::DISPLAY_DELAY_OFF);
312     int sleepTime = 30000;
313     usleep(sleepTime);
314     DisplayState offState = DisplayPowerMgrClient::GetInstance().GetDisplayState();
315     EXPECT_TRUE(offState == DisplayState::DISPLAY_OFF);
316 }
317 
318 /**
319  * @tc.name: DisplayPowerMgrService020
320  * @tc.desc: Test srceen delay off interrupt
321  * @tc.type: FUNC
322  */
323 HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService020, TestSize.Level0)
324 {
325     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrService020 is start");
326     DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_ON);
327 
328     DisplayPowerMgrClient::GetInstance().OverrideDisplayOffDelay(10);
329     DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF);
330     int sleepTime = 5000;
331     usleep(sleepTime);
332     DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_ON);
333     sleepTime = 30000;
334     usleep(sleepTime);
335     DisplayState onState = DisplayPowerMgrClient::GetInstance().GetDisplayState();
336     EXPECT_TRUE(onState == DisplayState::DISPLAY_ON);
337 }
338 } // namespace
339