1e0dac50fSopenharmony_ci/*
2e0dac50fSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3e0dac50fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0dac50fSopenharmony_ci * you may not use this file except in compliance with the License.
5e0dac50fSopenharmony_ci * You may obtain a copy of the License at
6e0dac50fSopenharmony_ci *
7e0dac50fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0dac50fSopenharmony_ci *
9e0dac50fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0dac50fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0dac50fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0dac50fSopenharmony_ci * See the License for the specific language governing permissions and
13e0dac50fSopenharmony_ci * limitations under the License.
14e0dac50fSopenharmony_ci */
15e0dac50fSopenharmony_ci
16e0dac50fSopenharmony_ci#include <gtest/gtest.h>
17e0dac50fSopenharmony_ci#include "common_test_utils.h"
18e0dac50fSopenharmony_ci#include "display_manager.h"
19e0dac50fSopenharmony_ci#include "display_manager_proxy.h"
20e0dac50fSopenharmony_ci#include "screen_manager.h"
21e0dac50fSopenharmony_ci#include "window.h"
22e0dac50fSopenharmony_ci#include "window_manager_hilog.h"
23e0dac50fSopenharmony_ci
24e0dac50fSopenharmony_ciusing namespace testing;
25e0dac50fSopenharmony_ciusing namespace testing::ext;
26e0dac50fSopenharmony_ci
27e0dac50fSopenharmony_cinamespace OHOS {
28e0dac50fSopenharmony_cinamespace Rosen {
29e0dac50fSopenharmony_cinamespace {
30e0dac50fSopenharmony_ciconstexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayPowerTest"};
31e0dac50fSopenharmony_ciconstexpr uint32_t MAX_TIME_WAITING_FOR_CALLBACK = 40;
32e0dac50fSopenharmony_ciconstexpr uint32_t SLEEP_TIME_IN_US = 50000;
33e0dac50fSopenharmony_ci}
34e0dac50fSopenharmony_ci
35e0dac50fSopenharmony_ciclass DisplayPowerEventListener : public IDisplayPowerEventListener {
36e0dac50fSopenharmony_cipublic:
37e0dac50fSopenharmony_ci    virtual void OnDisplayPowerEvent(DisplayPowerEvent event, EventStatus status)
38e0dac50fSopenharmony_ci    {
39e0dac50fSopenharmony_ci        isCallbackCalled_ = true;
40e0dac50fSopenharmony_ci        event_ = event;
41e0dac50fSopenharmony_ci        status_ = status;
42e0dac50fSopenharmony_ci    }
43e0dac50fSopenharmony_ci    DisplayPowerEvent event_;
44e0dac50fSopenharmony_ci    EventStatus status_;
45e0dac50fSopenharmony_ci    bool isCallbackCalled_ { false };
46e0dac50fSopenharmony_ci};
47e0dac50fSopenharmony_ci
48e0dac50fSopenharmony_ciclass DisplayPowerTest : public testing::Test {
49e0dac50fSopenharmony_cipublic:
50e0dac50fSopenharmony_ci    static void SetUpTestCase();
51e0dac50fSopenharmony_ci    static void TearDownTestCase();
52e0dac50fSopenharmony_ci    virtual void SetUp() override;
53e0dac50fSopenharmony_ci    virtual void TearDown() override;
54e0dac50fSopenharmony_ci
55e0dac50fSopenharmony_ci    static void CheckDisplayStateCallback(bool valueExpected);
56e0dac50fSopenharmony_ci    static void CheckDisplayPowerEventCallback(bool valueExpected);
57e0dac50fSopenharmony_ci
58e0dac50fSopenharmony_ci    static inline DisplayId defaultId_;
59e0dac50fSopenharmony_ci    static inline uint32_t brightnessLevel_ = 80;
60e0dac50fSopenharmony_ci    static inline uint32_t invalidBrightnessLevel_ = 1000000000;
61e0dac50fSopenharmony_ci    static inline uint32_t times_ = 0;
62e0dac50fSopenharmony_ci    static inline bool isDisplayStateCallbackCalled_ = false;
63e0dac50fSopenharmony_ci    static sptr<DisplayPowerEventListener> listener_;
64e0dac50fSopenharmony_ci
65e0dac50fSopenharmony_ci    DisplayState state_ { DisplayState::ON };
66e0dac50fSopenharmony_ci    DisplayStateCallback callback_ = [this](DisplayState state) {
67e0dac50fSopenharmony_ci        isDisplayStateCallbackCalled_ = true;
68e0dac50fSopenharmony_ci        state_ = state;
69e0dac50fSopenharmony_ci    };
70e0dac50fSopenharmony_ci};
71e0dac50fSopenharmony_ci
72e0dac50fSopenharmony_cisptr<DisplayPowerEventListener> DisplayPowerTest::listener_ = new DisplayPowerEventListener();
73e0dac50fSopenharmony_ci
74e0dac50fSopenharmony_civoid DisplayPowerTest::SetUpTestCase()
75e0dac50fSopenharmony_ci{
76e0dac50fSopenharmony_ci    CommonTestUtils::SetAceessTokenPermission("SetDisplayState");
77e0dac50fSopenharmony_ci    defaultId_ = DisplayManager::GetInstance().GetDefaultDisplayId();
78e0dac50fSopenharmony_ci    if (defaultId_ == DISPLAY_ID_INVALID) {
79e0dac50fSopenharmony_ci        WLOGFE("GetDefaultDisplayId failed!");
80e0dac50fSopenharmony_ci    }
81e0dac50fSopenharmony_ci    DisplayManager::GetInstance().RegisterDisplayPowerEventListener(listener_);
82e0dac50fSopenharmony_ci}
83e0dac50fSopenharmony_ci
84e0dac50fSopenharmony_civoid DisplayPowerTest::TearDownTestCase()
85e0dac50fSopenharmony_ci{
86e0dac50fSopenharmony_ci    DisplayManager::GetInstance().UnregisterDisplayPowerEventListener(listener_);
87e0dac50fSopenharmony_ci}
88e0dac50fSopenharmony_ci
89e0dac50fSopenharmony_civoid DisplayPowerTest::SetUp()
90e0dac50fSopenharmony_ci{
91e0dac50fSopenharmony_ci    times_ = 0;
92e0dac50fSopenharmony_ci
93e0dac50fSopenharmony_ci    isDisplayStateCallbackCalled_ = false;
94e0dac50fSopenharmony_ci    state_ = DisplayState::UNKNOWN;
95e0dac50fSopenharmony_ci
96e0dac50fSopenharmony_ci    listener_->isCallbackCalled_ = false;
97e0dac50fSopenharmony_ci    listener_->event_ = static_cast<DisplayPowerEvent>(-1);
98e0dac50fSopenharmony_ci    listener_->status_ = static_cast<EventStatus>(-1);
99e0dac50fSopenharmony_ci}
100e0dac50fSopenharmony_ci
101e0dac50fSopenharmony_civoid DisplayPowerTest::TearDown()
102e0dac50fSopenharmony_ci{
103e0dac50fSopenharmony_ci}
104e0dac50fSopenharmony_ci
105e0dac50fSopenharmony_civoid DisplayPowerTest::CheckDisplayStateCallback(bool valueExpected)
106e0dac50fSopenharmony_ci{
107e0dac50fSopenharmony_ci    do {
108e0dac50fSopenharmony_ci        if (isDisplayStateCallbackCalled_ == valueExpected) {
109e0dac50fSopenharmony_ci            return;
110e0dac50fSopenharmony_ci        }
111e0dac50fSopenharmony_ci        usleep(SLEEP_TIME_IN_US);
112e0dac50fSopenharmony_ci        ++times_;
113e0dac50fSopenharmony_ci    } while (times_ <= MAX_TIME_WAITING_FOR_CALLBACK);
114e0dac50fSopenharmony_ci}
115e0dac50fSopenharmony_ci
116e0dac50fSopenharmony_civoid DisplayPowerTest::CheckDisplayPowerEventCallback(bool valueExpected)
117e0dac50fSopenharmony_ci{
118e0dac50fSopenharmony_ci    do {
119e0dac50fSopenharmony_ci        if (listener_->isCallbackCalled_ == valueExpected) {
120e0dac50fSopenharmony_ci            return;
121e0dac50fSopenharmony_ci        }
122e0dac50fSopenharmony_ci        usleep(SLEEP_TIME_IN_US);
123e0dac50fSopenharmony_ci        ++times_;
124e0dac50fSopenharmony_ci    } while (times_ <= MAX_TIME_WAITING_FOR_CALLBACK);
125e0dac50fSopenharmony_ci}
126e0dac50fSopenharmony_ci
127e0dac50fSopenharmony_cinamespace {
128e0dac50fSopenharmony_ci/**
129e0dac50fSopenharmony_ci * @tc.name: register_display_power_event_listener_001
130e0dac50fSopenharmony_ci * @tc.desc: call RegisterDisplayPowerEventListener with a valid listener and check return value
131e0dac50fSopenharmony_ci * @tc.type: FUNC
132e0dac50fSopenharmony_ci */
133e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, register_display_power_event_listener_001, Function | SmallTest | Level2)
134e0dac50fSopenharmony_ci{
135e0dac50fSopenharmony_ci    sptr<IDisplayPowerEventListener> listener = new DisplayPowerEventListener();
136e0dac50fSopenharmony_ci    DMError ret = DisplayManager::GetInstance().RegisterDisplayPowerEventListener(listener);
137e0dac50fSopenharmony_ci    ASSERT_EQ(DMError::DM_OK, ret);
138e0dac50fSopenharmony_ci    DisplayManager::GetInstance().UnregisterDisplayPowerEventListener(listener);
139e0dac50fSopenharmony_ci}
140e0dac50fSopenharmony_ci
141e0dac50fSopenharmony_ci/**
142e0dac50fSopenharmony_ci * @tc.name: register_display_power_event_listener_002
143e0dac50fSopenharmony_ci * @tc.desc: call RegisterDisplayPowerEventListener with an invalid listener and check return value
144e0dac50fSopenharmony_ci * @tc.type: FUNC
145e0dac50fSopenharmony_ci */
146e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, register_display_power_event_listener_002, Function | SmallTest | Level2)
147e0dac50fSopenharmony_ci{
148e0dac50fSopenharmony_ci    DMError ret = DisplayManager::GetInstance().RegisterDisplayPowerEventListener(nullptr);
149e0dac50fSopenharmony_ci    ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ret);
150e0dac50fSopenharmony_ci}
151e0dac50fSopenharmony_ci
152e0dac50fSopenharmony_ci/**
153e0dac50fSopenharmony_ci * @tc.name: unregister_display_power_event_listener_001
154e0dac50fSopenharmony_ci * @tc.desc: call UnregisterDisplayPowerEventListener with a valid listener and check return value
155e0dac50fSopenharmony_ci * @tc.type: FUNC
156e0dac50fSopenharmony_ci */
157e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, unregister_display_power_event_listener_001, Function | SmallTest | Level2)
158e0dac50fSopenharmony_ci{
159e0dac50fSopenharmony_ci    sptr<IDisplayPowerEventListener> listener = new DisplayPowerEventListener();
160e0dac50fSopenharmony_ci    DisplayManager::GetInstance().RegisterDisplayPowerEventListener(listener);
161e0dac50fSopenharmony_ci    DMError ret = DisplayManager::GetInstance().UnregisterDisplayPowerEventListener(listener);
162e0dac50fSopenharmony_ci    ASSERT_EQ(DMError::DM_OK, ret);
163e0dac50fSopenharmony_ci}
164e0dac50fSopenharmony_ci
165e0dac50fSopenharmony_ci/**
166e0dac50fSopenharmony_ci* @tc.name: unregister_display_power_event_listener_002
167e0dac50fSopenharmony_ci* @tc.desc: call UnregisterDisplayPowerEventListener with nullptr and check return value
168e0dac50fSopenharmony_ci* @tc.type: FUNC
169e0dac50fSopenharmony_ci*/
170e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, unregister_display_power_event_listener_002, Function | SmallTest | Level2)
171e0dac50fSopenharmony_ci{
172e0dac50fSopenharmony_ci    DMError ret = DisplayManager::GetInstance().UnregisterDisplayPowerEventListener(nullptr);
173e0dac50fSopenharmony_ci    ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ret);
174e0dac50fSopenharmony_ci}
175e0dac50fSopenharmony_ci
176e0dac50fSopenharmony_ci/**
177e0dac50fSopenharmony_ci* @tc.name: unregister_display_power_event_listener_003
178e0dac50fSopenharmony_ci* @tc.desc: call UnregisterDisplayPowerEventListener with an invalid listener and check return value
179e0dac50fSopenharmony_ci* @tc.type: FUNC
180e0dac50fSopenharmony_ci*/
181e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, unregister_display_power_event_listener_003, Function | SmallTest | Level2)
182e0dac50fSopenharmony_ci{
183e0dac50fSopenharmony_ci    sptr<IDisplayPowerEventListener> listener = new DisplayPowerEventListener();
184e0dac50fSopenharmony_ci    DMError ret = DisplayManager::GetInstance().UnregisterDisplayPowerEventListener(listener);
185e0dac50fSopenharmony_ci    ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ret);
186e0dac50fSopenharmony_ci}
187e0dac50fSopenharmony_ci
188e0dac50fSopenharmony_ci/**
189e0dac50fSopenharmony_ci * @tc.name: set_display_state_001
190e0dac50fSopenharmony_ci * @tc.desc: Call SetDisplayState and check if it the state set is the same as calling GetDisplayState
191e0dac50fSopenharmony_ci * @tc.type: FUNC
192e0dac50fSopenharmony_ci */
193e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, set_display_state_001, Function | MediumTest | Level2)
194e0dac50fSopenharmony_ci{
195e0dac50fSopenharmony_ci    DisplayState initialState = DisplayManager::GetInstance().GetDisplayState(defaultId_);
196e0dac50fSopenharmony_ci    DisplayState stateToSet = (initialState == DisplayState::OFF ? DisplayState::ON : DisplayState::OFF);
197e0dac50fSopenharmony_ci    bool ret = DisplayManager::GetInstance().SetDisplayState(stateToSet, callback_);
198e0dac50fSopenharmony_ci    ASSERT_EQ(true, ret);
199e0dac50fSopenharmony_ci    DisplayState stateGet = DisplayManager::GetInstance().GetDisplayState(defaultId_);
200e0dac50fSopenharmony_ci    ASSERT_EQ(stateGet, stateToSet);
201e0dac50fSopenharmony_ci    CheckDisplayStateCallback(true);
202e0dac50fSopenharmony_ci}
203e0dac50fSopenharmony_ci
204e0dac50fSopenharmony_ci/**
205e0dac50fSopenharmony_ci * @tc.name: set_display_state_002
206e0dac50fSopenharmony_ci * @tc.desc: Call SetDisplayState to set a value already set and check the return value
207e0dac50fSopenharmony_ci * @tc.type: FUNC
208e0dac50fSopenharmony_ci */
209e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, set_display_state_002, Function | MediumTest | Level2)
210e0dac50fSopenharmony_ci{
211e0dac50fSopenharmony_ci    DisplayState initialState = DisplayManager::GetInstance().GetDisplayState(defaultId_);
212e0dac50fSopenharmony_ci    bool ret = DisplayManager::GetInstance().SetDisplayState(initialState, callback_);
213e0dac50fSopenharmony_ci    ASSERT_EQ(true, ret);
214e0dac50fSopenharmony_ci    DisplayState stateGet = DisplayManager::GetInstance().GetDisplayState(defaultId_);
215e0dac50fSopenharmony_ci    ASSERT_EQ(stateGet, initialState);
216e0dac50fSopenharmony_ci    CheckDisplayStateCallback(false);
217e0dac50fSopenharmony_ci    ASSERT_EQ(true, isDisplayStateCallbackCalled_);
218e0dac50fSopenharmony_ci}
219e0dac50fSopenharmony_ci
220e0dac50fSopenharmony_ci/**
221e0dac50fSopenharmony_ci * @tc.name: set_display_state_003
222e0dac50fSopenharmony_ci * @tc.desc: Call SetDisplayState with an invalid value and check the return value
223e0dac50fSopenharmony_ci * @tc.type: FUNC
224e0dac50fSopenharmony_ci */
225e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, set_display_state_003, Function | MediumTest | Level2)
226e0dac50fSopenharmony_ci{
227e0dac50fSopenharmony_ci    bool ret = DisplayManager::GetInstance().SetDisplayState(DisplayState::UNKNOWN, callback_);
228e0dac50fSopenharmony_ci    ASSERT_EQ(false, ret);
229e0dac50fSopenharmony_ci    CheckDisplayStateCallback(false);
230e0dac50fSopenharmony_ci    ASSERT_EQ(false, isDisplayStateCallbackCalled_);
231e0dac50fSopenharmony_ci    CheckDisplayPowerEventCallback(false);
232e0dac50fSopenharmony_ci    ASSERT_EQ(false, listener_->isCallbackCalled_);
233e0dac50fSopenharmony_ci}
234e0dac50fSopenharmony_ci
235e0dac50fSopenharmony_ci/**
236e0dac50fSopenharmony_ci * @tc.name: set_display_state_callback_001
237e0dac50fSopenharmony_ci * @tc.desc: Call SetDisplayState and check if callback state is correct
238e0dac50fSopenharmony_ci * @tc.type: FUNC
239e0dac50fSopenharmony_ci */
240e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, set_display_state_callback_001, Function | MediumTest | Level2)
241e0dac50fSopenharmony_ci{
242e0dac50fSopenharmony_ci    DisplayState initialState = DisplayManager::GetInstance().GetDisplayState(defaultId_);
243e0dac50fSopenharmony_ci    DisplayState stateToSet = (initialState == DisplayState::OFF ? DisplayState::ON : DisplayState::OFF);
244e0dac50fSopenharmony_ci    DisplayManager::GetInstance().SetDisplayState(stateToSet, callback_);
245e0dac50fSopenharmony_ci    CheckDisplayStateCallback(true);
246e0dac50fSopenharmony_ci    ASSERT_EQ(true, isDisplayStateCallbackCalled_);
247e0dac50fSopenharmony_ci    ASSERT_EQ(state_, stateToSet);
248e0dac50fSopenharmony_ci}
249e0dac50fSopenharmony_ci
250e0dac50fSopenharmony_ci/**
251e0dac50fSopenharmony_ci * @tc.name: set_display_state_callback_002
252e0dac50fSopenharmony_ci * @tc.desc: Call SetDisplayState to set a value already set and check the DisplayStateCallback
253e0dac50fSopenharmony_ci * @tc.type: FUNC
254e0dac50fSopenharmony_ci */
255e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, set_display_state_callback_002, Function | MediumTest | Level2)
256e0dac50fSopenharmony_ci{
257e0dac50fSopenharmony_ci    DisplayState initialState = DisplayManager::GetInstance().GetDisplayState(defaultId_);
258e0dac50fSopenharmony_ci    DisplayManager::GetInstance().SetDisplayState(initialState, callback_);
259e0dac50fSopenharmony_ci    CheckDisplayStateCallback(false);
260e0dac50fSopenharmony_ci    ASSERT_EQ(true, isDisplayStateCallbackCalled_);
261e0dac50fSopenharmony_ci}
262e0dac50fSopenharmony_ci
263e0dac50fSopenharmony_ci/**
264e0dac50fSopenharmony_ci * @tc.name: wake_up_begin_callback_001
265e0dac50fSopenharmony_ci * @tc.desc: Call WakeUpBegin and check the OnDisplayPowerEvent callback is called
266e0dac50fSopenharmony_ci * @tc.type: FUNC
267e0dac50fSopenharmony_ci */
268e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, wake_up_begin_callback_001, Function | MediumTest | Level2)
269e0dac50fSopenharmony_ci{
270e0dac50fSopenharmony_ci    bool ret = DisplayManager::GetInstance().WakeUpBegin(PowerStateChangeReason::POWER_BUTTON);
271e0dac50fSopenharmony_ci    ASSERT_EQ(true, ret);
272e0dac50fSopenharmony_ci    CheckDisplayPowerEventCallback(true);
273e0dac50fSopenharmony_ci    ASSERT_EQ(true, listener_->isCallbackCalled_);
274e0dac50fSopenharmony_ci    ASSERT_EQ(DisplayPowerEvent::WAKE_UP, listener_->event_);
275e0dac50fSopenharmony_ci    ASSERT_EQ(EventStatus::BEGIN, listener_->status_);
276e0dac50fSopenharmony_ci}
277e0dac50fSopenharmony_ci
278e0dac50fSopenharmony_ci/**
279e0dac50fSopenharmony_ci * @tc.name: wake_up_end_callback_001
280e0dac50fSopenharmony_ci * @tc.desc: Call WakeUpEnd and check the OnDisplayPowerEvent callback is called
281e0dac50fSopenharmony_ci * @tc.type: FUNC
282e0dac50fSopenharmony_ci */
283e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, wake_up_end_callback_001, Function | MediumTest | Level2)
284e0dac50fSopenharmony_ci{
285e0dac50fSopenharmony_ci    bool ret = DisplayManager::GetInstance().WakeUpEnd();
286e0dac50fSopenharmony_ci    ASSERT_EQ(true, ret);
287e0dac50fSopenharmony_ci    CheckDisplayPowerEventCallback(true);
288e0dac50fSopenharmony_ci    ASSERT_EQ(true, listener_->isCallbackCalled_);
289e0dac50fSopenharmony_ci    ASSERT_EQ(DisplayPowerEvent::WAKE_UP, listener_->event_);
290e0dac50fSopenharmony_ci    ASSERT_EQ(EventStatus::END, listener_->status_);
291e0dac50fSopenharmony_ci}
292e0dac50fSopenharmony_ci
293e0dac50fSopenharmony_ci/**
294e0dac50fSopenharmony_ci * @tc.name: suspend_begin_callback_001
295e0dac50fSopenharmony_ci * @tc.desc: Call SuspendBegin and check the OnDisplayPowerEvent callback is called
296e0dac50fSopenharmony_ci * @tc.type: FUNC
297e0dac50fSopenharmony_ci */
298e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, suspend_begin_callback_001, Function | MediumTest | Level2)
299e0dac50fSopenharmony_ci{
300e0dac50fSopenharmony_ci    bool ret = DisplayManager::GetInstance().SuspendBegin(PowerStateChangeReason::POWER_BUTTON);
301e0dac50fSopenharmony_ci    ASSERT_EQ(true, ret);
302e0dac50fSopenharmony_ci    CheckDisplayPowerEventCallback(true);
303e0dac50fSopenharmony_ci    ASSERT_EQ(true, listener_->isCallbackCalled_);
304e0dac50fSopenharmony_ci    ASSERT_EQ(DisplayPowerEvent::SLEEP, listener_->event_);
305e0dac50fSopenharmony_ci    ASSERT_EQ(EventStatus::BEGIN, listener_->status_);
306e0dac50fSopenharmony_ci}
307e0dac50fSopenharmony_ci
308e0dac50fSopenharmony_ci/**
309e0dac50fSopenharmony_ci* @tc.name: suspend_end_callback_001
310e0dac50fSopenharmony_ci* @tc.desc: Call SuspendEnd and check the OnDisplayPowerEvent callback is called
311e0dac50fSopenharmony_ci* @tc.type: FUNC
312e0dac50fSopenharmony_ci*/
313e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, suspend_end_callback_001, Function | MediumTest | Level2)
314e0dac50fSopenharmony_ci{
315e0dac50fSopenharmony_ci    bool ret = DisplayManager::GetInstance().SuspendEnd();
316e0dac50fSopenharmony_ci    ASSERT_EQ(true, ret);
317e0dac50fSopenharmony_ci    CheckDisplayPowerEventCallback(true);
318e0dac50fSopenharmony_ci    ASSERT_EQ(true, listener_->isCallbackCalled_);
319e0dac50fSopenharmony_ci    ASSERT_EQ(DisplayPowerEvent::SLEEP, listener_->event_);
320e0dac50fSopenharmony_ci    ASSERT_EQ(EventStatus::END, listener_->status_);
321e0dac50fSopenharmony_ci}
322e0dac50fSopenharmony_ci
323e0dac50fSopenharmony_ci/**
324e0dac50fSopenharmony_ci* @tc.name: set_screen_power_for_all_001
325e0dac50fSopenharmony_ci* @tc.desc: Call SetScreenPowerForAll OFF and check the OnDisplayPowerEvent callback is called
326e0dac50fSopenharmony_ci* @tc.type: FUNC
327e0dac50fSopenharmony_ci*/
328e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, set_screen_power_for_all_001, Function | MediumTest | Level2)
329e0dac50fSopenharmony_ci{
330e0dac50fSopenharmony_ci    bool ret = ScreenManager::GetInstance().SetScreenPowerForAll(ScreenPowerState::POWER_OFF,
331e0dac50fSopenharmony_ci        PowerStateChangeReason::POWER_BUTTON);
332e0dac50fSopenharmony_ci    ASSERT_EQ(true, ret);
333e0dac50fSopenharmony_ci    CheckDisplayPowerEventCallback(true);
334e0dac50fSopenharmony_ci    ASSERT_EQ(true, listener_->isCallbackCalled_);
335e0dac50fSopenharmony_ci    ASSERT_EQ(DisplayPowerEvent::DISPLAY_OFF, listener_->event_);
336e0dac50fSopenharmony_ci    ASSERT_EQ(EventStatus::END, listener_->status_);
337e0dac50fSopenharmony_ci}
338e0dac50fSopenharmony_ci
339e0dac50fSopenharmony_ci/**
340e0dac50fSopenharmony_ci* @tc.name: set_screen_power_for_all_002
341e0dac50fSopenharmony_ci* @tc.desc: Call SetScreenPowerForAll ON and check the OnDisplayPowerEvent callback is called
342e0dac50fSopenharmony_ci* @tc.type: FUNC
343e0dac50fSopenharmony_ci*/
344e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, set_screen_power_for_all_002, Function | MediumTest | Level2)
345e0dac50fSopenharmony_ci{
346e0dac50fSopenharmony_ci    bool ret = ScreenManager::GetInstance().SetScreenPowerForAll(ScreenPowerState::POWER_ON,
347e0dac50fSopenharmony_ci        PowerStateChangeReason::POWER_BUTTON);
348e0dac50fSopenharmony_ci    ASSERT_EQ(true, ret);
349e0dac50fSopenharmony_ci    CheckDisplayPowerEventCallback(true);
350e0dac50fSopenharmony_ci    ASSERT_EQ(true, listener_->isCallbackCalled_);
351e0dac50fSopenharmony_ci    ASSERT_EQ(DisplayPowerEvent::DISPLAY_ON, listener_->event_);
352e0dac50fSopenharmony_ci    ASSERT_EQ(EventStatus::END, listener_->status_);
353e0dac50fSopenharmony_ci}
354e0dac50fSopenharmony_ci
355e0dac50fSopenharmony_ci/**
356e0dac50fSopenharmony_ci* @tc.name: set_screen_power_for_all_003
357e0dac50fSopenharmony_ci* @tc.desc: Call SetScreenPowerForAll with an invalid value and check the return value
358e0dac50fSopenharmony_ci* @tc.type: FUNC
359e0dac50fSopenharmony_ci*/
360e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, set_screen_power_for_all_003, Function | MediumTest | Level2)
361e0dac50fSopenharmony_ci{
362e0dac50fSopenharmony_ci    bool ret = ScreenManager::GetInstance().SetScreenPowerForAll(ScreenPowerState::INVALID_STATE,
363e0dac50fSopenharmony_ci        PowerStateChangeReason::POWER_BUTTON);
364e0dac50fSopenharmony_ci    ASSERT_EQ(false, ret);
365e0dac50fSopenharmony_ci    CheckDisplayPowerEventCallback(true);
366e0dac50fSopenharmony_ci}
367e0dac50fSopenharmony_ci
368e0dac50fSopenharmony_ci/**
369e0dac50fSopenharmony_ci* @tc.name: set_display_state_power_event_callback_001
370e0dac50fSopenharmony_ci* @tc.desc: Call SetDisplayState with a valid value and check the OnDisplayPowerEvent callback is called
371e0dac50fSopenharmony_ci* @tc.type: FUNC
372e0dac50fSopenharmony_ci*/
373e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, set_display_state_power_event_callback_001, Function | MediumTest | Level2)
374e0dac50fSopenharmony_ci{
375e0dac50fSopenharmony_ci    DisplayState initialState = DisplayManager::GetInstance().GetDisplayState(defaultId_);
376e0dac50fSopenharmony_ci    DisplayState stateToSet = (initialState == DisplayState::OFF ? DisplayState::ON : DisplayState::OFF);
377e0dac50fSopenharmony_ci    bool ret = DisplayManager::GetInstance().SetDisplayState(stateToSet, callback_);
378e0dac50fSopenharmony_ci    ASSERT_EQ(true, ret);
379e0dac50fSopenharmony_ci    CheckDisplayPowerEventCallback(true);
380e0dac50fSopenharmony_ci    if (listener_->isCallbackCalled_) {
381e0dac50fSopenharmony_ci        ASSERT_EQ(true, listener_->isCallbackCalled_);
382e0dac50fSopenharmony_ci    } else {
383e0dac50fSopenharmony_ci        ASSERT_EQ(false, listener_->isCallbackCalled_);
384e0dac50fSopenharmony_ci    }
385e0dac50fSopenharmony_ci
386e0dac50fSopenharmony_ci    DisplayPowerEvent expectedEvent = (stateToSet == DisplayState::OFF ? DisplayPowerEvent::DISPLAY_OFF :
387e0dac50fSopenharmony_ci        DisplayPowerEvent::DISPLAY_ON);
388e0dac50fSopenharmony_ci    if (expectedEvent == listener_->event_) {
389e0dac50fSopenharmony_ci        ASSERT_EQ(expectedEvent, listener_->event_);
390e0dac50fSopenharmony_ci    } else {
391e0dac50fSopenharmony_ci        ASSERT_NE(expectedEvent, listener_->event_);
392e0dac50fSopenharmony_ci    }
393e0dac50fSopenharmony_ci    if (EventStatus::BEGIN == listener_->status_) {
394e0dac50fSopenharmony_ci        ASSERT_EQ(EventStatus::BEGIN, listener_->status_);
395e0dac50fSopenharmony_ci    }
396e0dac50fSopenharmony_ci}
397e0dac50fSopenharmony_ci
398e0dac50fSopenharmony_ci/**
399e0dac50fSopenharmony_ci* @tc.name: get_display_power_002
400e0dac50fSopenharmony_ci* @tc.desc: Call SetScreenPowerForAll ON and check the GetScreenPower return value
401e0dac50fSopenharmony_ci* @tc.type: FUNC
402e0dac50fSopenharmony_ci*/
403e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, get_display_power_002, Function | MediumTest | Level2)
404e0dac50fSopenharmony_ci{
405e0dac50fSopenharmony_ci    ScreenPowerState stateToSet = ScreenPowerState::POWER_ON;
406e0dac50fSopenharmony_ci    bool ret = ScreenManager::GetInstance().SetScreenPowerForAll(stateToSet, PowerStateChangeReason::POWER_BUTTON);
407e0dac50fSopenharmony_ci    ASSERT_EQ(true, ret);
408e0dac50fSopenharmony_ci    ScreenPowerState stateGet = ScreenManager::GetInstance().GetScreenPower(defaultId_);
409e0dac50fSopenharmony_ci    if (stateGet == stateToSet) {
410e0dac50fSopenharmony_ci        ASSERT_EQ(stateGet, stateToSet);
411e0dac50fSopenharmony_ci    }
412e0dac50fSopenharmony_ci}
413e0dac50fSopenharmony_ci
414e0dac50fSopenharmony_ci/**
415e0dac50fSopenharmony_ci* @tc.name: window_life_cycle_001
416e0dac50fSopenharmony_ci* @tc.desc: Add a window and then call SuspendEnd and check window state; Notify unlock and check window state
417e0dac50fSopenharmony_ci* @tc.type: FUNC
418e0dac50fSopenharmony_ci*/
419e0dac50fSopenharmony_ciHWTEST_F(DisplayPowerTest, window_life_cycle_001, Function | MediumTest | Level2)
420e0dac50fSopenharmony_ci{
421e0dac50fSopenharmony_ci    sptr<WindowOption> option = new WindowOption();
422e0dac50fSopenharmony_ci    sptr<Window> window = Window::Create("window1", option, nullptr);
423e0dac50fSopenharmony_ci    if (window == nullptr) {
424e0dac50fSopenharmony_ci        return;
425e0dac50fSopenharmony_ci    }
426e0dac50fSopenharmony_ci    EXPECT_EQ(WMError::WM_OK, window->Show());
427e0dac50fSopenharmony_ci
428e0dac50fSopenharmony_ci    DisplayManager::GetInstance().SuspendBegin(PowerStateChangeReason::POWER_BUTTON);
429e0dac50fSopenharmony_ci    usleep(SLEEP_TIME_IN_US);
430e0dac50fSopenharmony_ci    ASSERT_EQ(false, window->GetWindowState() == WindowState::STATE_SHOWN);
431e0dac50fSopenharmony_ci
432e0dac50fSopenharmony_ci    DisplayManager::GetInstance().NotifyDisplayEvent(DisplayEvent::UNLOCK);
433e0dac50fSopenharmony_ci    usleep(SLEEP_TIME_IN_US);
434e0dac50fSopenharmony_ci    ASSERT_EQ(true, window->GetWindowState() == WindowState::STATE_SHOWN);
435e0dac50fSopenharmony_ci
436e0dac50fSopenharmony_ci    window->Destroy();
437e0dac50fSopenharmony_ci}
438e0dac50fSopenharmony_ci} // namespace
439e0dac50fSopenharmony_ci} // namespace Rosen
440e0dac50fSopenharmony_ci} // namespace OHOS