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
18 #include "display_power_controller.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 constexpr uint32_t SLEEP_TIME_US = 100000;
27 }
28 class DisplayPowerControllerTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34
35 private:
36 std::recursive_mutex mutex_;
37 sptr<DisplayPowerController> dpc_ = nullptr;
38 };
39
SetUpTestCase()40 void DisplayPowerControllerTest::SetUpTestCase()
41 {
42 }
43
TearDownTestCase()44 void DisplayPowerControllerTest::TearDownTestCase()
45 {
46 }
47
SetUp()48 void DisplayPowerControllerTest::SetUp()
49 {
50 }
51
TearDown()52 void DisplayPowerControllerTest::TearDown()
53 {
54 usleep(SLEEP_TIME_US);
55 }
56
57 namespace {
58 /**
59 * @tc.name: SuspendBegin
60 * @tc.desc: test function : SuspendBegin
61 * @tc.type: FUNC
62 */
HWTEST_F(DisplayPowerControllerTest, SuspendBegin, Function | SmallTest | Level1)63 HWTEST_F(DisplayPowerControllerTest, SuspendBegin, Function | SmallTest | Level1)
64 {
65 dpc_ = new DisplayPowerController(mutex_, [](DisplayId, sptr<DisplayInfo>,
66 const std::map<DisplayId, sptr<DisplayInfo>>&, DisplayStateChangeType) {});
67
68 PowerStateChangeReason reason = PowerStateChangeReason{0};
69 auto ret = dpc_->SuspendBegin(reason);
70 ASSERT_EQ(true, ret);
71 }
72
73 /**
74 * @tc.name: SetDisplayState01
75 * @tc.desc: test function : SetDisplayState
76 * @tc.type: FUNC
77 */
HWTEST_F(DisplayPowerControllerTest, SetDisplayState01, Function | SmallTest | Level1)78 HWTEST_F(DisplayPowerControllerTest, SetDisplayState01, Function | SmallTest | Level1)
79 {
80 dpc_ = new DisplayPowerController(mutex_, [](DisplayId, sptr<DisplayInfo>,
81 const std::map<DisplayId, sptr<DisplayInfo>>&, DisplayStateChangeType) {});
82
83 DisplayState state = DisplayState::UNKNOWN;
84 auto ret = dpc_->SetDisplayState(state);
85 ASSERT_EQ(false, ret);
86 }
87
88 /**
89 * @tc.name: SetDisplayState02
90 * @tc.desc: test function : SetDisplayState
91 * @tc.type: FUNC
92 */
HWTEST_F(DisplayPowerControllerTest, SetDisplayState02, Function | SmallTest | Level1)93 HWTEST_F(DisplayPowerControllerTest, SetDisplayState02, Function | SmallTest | Level1)
94 {
95 dpc_ = new DisplayPowerController(mutex_, [](DisplayId, sptr<DisplayInfo>,
96 const std::map<DisplayId, sptr<DisplayInfo>>&, DisplayStateChangeType) {});
97
98 DisplayState state = DisplayState::ON;
99 auto ret = dpc_->SetDisplayState(state);
100 ASSERT_EQ(true, ret);
101 }
102
103 /**
104 * @tc.name: SetDisplayState03
105 * @tc.desc: test function : SetDisplayState
106 * @tc.type: FUNC
107 */
HWTEST_F(DisplayPowerControllerTest, SetDisplayState03, Function | SmallTest | Level1)108 HWTEST_F(DisplayPowerControllerTest, SetDisplayState03, Function | SmallTest | Level1)
109 {
110 dpc_ = new DisplayPowerController(mutex_, [](DisplayId, sptr<DisplayInfo>,
111 const std::map<DisplayId, sptr<DisplayInfo>>&, DisplayStateChangeType) {});
112
113 DisplayState state = DisplayState::OFF;
114 auto ret = dpc_->SetDisplayState(state);
115 ASSERT_EQ(true, ret);
116 }
117
118 /**
119 * @tc.name: GetDisplayState
120 * @tc.desc: test function : GetDisplayState
121 * @tc.type: FUNC
122 */
HWTEST_F(DisplayPowerControllerTest, GetDisplayState, Function | SmallTest | Level1)123 HWTEST_F(DisplayPowerControllerTest, GetDisplayState, Function | SmallTest | Level1)
124 {
125 dpc_ = new DisplayPowerController(mutex_, [](DisplayId, sptr<DisplayInfo>,
126 const std::map<DisplayId, sptr<DisplayInfo>>&, DisplayStateChangeType) {});
127
128 DisplayId displayId = 1;
129 auto ret = dpc_->GetDisplayState(displayId);
130 ASSERT_EQ(DisplayState::UNKNOWN, ret);
131 }
132
133 /**
134 * @tc.name: NotifyDisplayEvent01
135 * @tc.desc: test function : NotifyDisplayEvent
136 * @tc.type: FUNC
137 */
HWTEST_F(DisplayPowerControllerTest, NotifyDisplayEvent01, Function | SmallTest | Level1)138 HWTEST_F(DisplayPowerControllerTest, NotifyDisplayEvent01, Function | SmallTest | Level1)
139 {
140 dpc_ = new DisplayPowerController(mutex_, [](DisplayId, sptr<DisplayInfo>,
141 const std::map<DisplayId, sptr<DisplayInfo>>&, DisplayStateChangeType) {});
142
143 DisplayEvent event = DisplayEvent::UNLOCK;
144 dpc_->NotifyDisplayEvent(event);
145 ASSERT_EQ(false, dpc_->isKeyguardDrawn_);
146 }
147
148 /**
149 * @tc.name: NotifyDisplayEvent02
150 * @tc.desc: test function : NotifyDisplayEvent
151 * @tc.type: FUNC
152 */
HWTEST_F(DisplayPowerControllerTest, NotifyDisplayEvent02, Function | SmallTest | Level1)153 HWTEST_F(DisplayPowerControllerTest, NotifyDisplayEvent02, Function | SmallTest | Level1)
154 {
155 dpc_ = new DisplayPowerController(mutex_, [](DisplayId, sptr<DisplayInfo>,
156 const std::map<DisplayId, sptr<DisplayInfo>>&, DisplayStateChangeType) {});
157
158 DisplayEvent event = DisplayEvent::KEYGUARD_DRAWN;
159 dpc_->NotifyDisplayEvent(event);
160 ASSERT_EQ(true, dpc_->isKeyguardDrawn_);
161 }
162 }
163 }
164 }