1 /*
2 * Copyright (c) 2023 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 "power_state_level_framework_test.h"
17
18 #include <iostream>
19
20 #include <gtest/gtest.h>
21 #include <string_ex.h>
22
23 #include "power_mgr_service.h"
24 #include "power_state_machine.h"
25 #include "power_state_machine_info.h"
26
27 using namespace testing::ext;
28 using namespace OHOS::PowerMgr;
29 using namespace OHOS;
30 using namespace std;
31
32 namespace {
33 sptr<PowerMgrService> g_pmsTest;
34 std::shared_ptr<PowerStateMachine> g_stateMachineTest;
35 }
36
SetUpTestCase()37 void PowerStateLevelFrameworkTest::SetUpTestCase()
38 {
39 g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
40 EXPECT_TRUE(g_pmsTest != nullptr) << "PowerMgrService02 fail to get PowerMgrService";
41 g_pmsTest->OnStart();
42 g_stateMachineTest = g_pmsTest->GetPowerStateMachine();
43 }
44
45 namespace {
46 /**
47 * @tc.name: PowerStateLevelFramework001
48 * @tc.desc: test system-level sleep level enumeration
49 * @tc.type: FUNC
50 * @tc.require: issueI6JSKU
51 */
52 HWTEST_F (PowerStateLevelFrameworkTest, PowerStateLevelFramework001, TestSize.Level0)
53 {
54 GTEST_LOG_(INFO) << "PowerStateLevelFramework001: test system-level sleep level enumeration is start";
55 EXPECT_TRUE(static_cast<uint32_t>(PowerState::AWAKE) == AWAKE) << "PowerState AWAKE correct";
56 EXPECT_TRUE(static_cast<uint32_t>(PowerState::FREEZE) == FREEZE) << "PowerState FREEZE correct";
57 EXPECT_TRUE(static_cast<uint32_t>(PowerState::INACTIVE) == INACTIVE) << "PowerState INACTIVE correct";
58 EXPECT_TRUE(static_cast<uint32_t>(PowerState::STAND_BY) == STAND_BY) << "PowerState STAND_BY correct";
59 EXPECT_TRUE(static_cast<uint32_t>(PowerState::DOZE) == DOZE) << "PowerState DOZE correct";
60 EXPECT_TRUE(static_cast<uint32_t>(PowerState::SLEEP) == SLEEP) << "PowerState SLEEP correct";
61 EXPECT_TRUE(static_cast<uint32_t>(PowerState::HIBERNATE) == HIBERNATE) << "PowerState HIBERNATE correct";
62 EXPECT_TRUE(static_cast<uint32_t>(PowerState::SHUTDOWN) == SHUTDOWN) << "PowerState HIBERNATE correct";
63 GTEST_LOG_(INFO) << "PowerStateLevelFramework001: test system-level sleep level enumeration is end";
64 }
65
66 /**
67 * @tc.name: PowerStateLevelFramework002
68 * @tc.desc: test the conversion between power states
69 * @tc.type: FUNC
70 * @tc.require: issueI6OM1F
71 */
72 HWTEST_F (PowerStateLevelFrameworkTest, PowerStateLevelFramework002, TestSize.Level0)
73 {
74 GTEST_LOG_(INFO) << "PowerStateLevelFramework002: power state replacement is start";
75 auto ret = g_stateMachineTest->SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_INIT);
76 EXPECT_TRUE(ret);
77 EXPECT_TRUE(g_stateMachineTest->GetState() == PowerState::AWAKE);
78 GTEST_LOG_(INFO) << "PowerStateLevelFramework002: power state replacement is end";
79 }
80
81 /**
82 * @tc.name: PowerStateLevelFramework003
83 * @tc.desc: test the conversion between power states
84 * @tc.type: FUNC
85 * @tc.require: issueI6OM1F
86 */
87 HWTEST_F (PowerStateLevelFrameworkTest, PowerStateLevelFramework003, TestSize.Level0)
88 {
89 GTEST_LOG_(INFO) << "PowerStateLevelFramework003: power state replacement is start";
90 auto ret = g_stateMachineTest->SetState(PowerState::FREEZE, StateChangeReason::STATE_CHANGE_REASON_INIT);
91 EXPECT_TRUE(ret);
92 EXPECT_TRUE(g_stateMachineTest->GetState() == PowerState::FREEZE);
93 GTEST_LOG_(INFO) << "PowerStateLevelFramework003: power state replacement is end";
94 }
95
96 /**
97 * @tc.name: PowerStateLevelFramework004
98 * @tc.desc: test the conversion between power states
99 * @tc.type: FUNC
100 * @tc.require: issueI6OM1F
101 */
102 HWTEST_F (PowerStateLevelFrameworkTest, PowerStateLevelFramework004, TestSize.Level0)
103 {
104 GTEST_LOG_(INFO) << "PowerStateLevelFramework004: power state replacement is start";
105 auto ret = g_stateMachineTest->SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_INIT);
106 EXPECT_TRUE(ret);
107 EXPECT_TRUE(g_stateMachineTest->GetState() == PowerState::INACTIVE);
108 GTEST_LOG_(INFO) << "PowerStateLevelFramework004: power state replacement is end";
109 }
110
111 /**
112 * @tc.name: PowerStateLevelFramework005
113 * @tc.desc: test the conversion between power states
114 * @tc.type: FUNC
115 * @tc.require: issueI6OM1F
116 */
117 HWTEST_F (PowerStateLevelFrameworkTest, PowerStateLevelFramework005, TestSize.Level0)
118 {
119 GTEST_LOG_(INFO) << "PowerStateLevelFramework005: power state replacement is start";
120 auto ret = g_stateMachineTest->SetState(PowerState::STAND_BY, StateChangeReason::STATE_CHANGE_REASON_INIT);
121 EXPECT_TRUE(ret);
122 EXPECT_TRUE(g_stateMachineTest->GetState() == PowerState::STAND_BY);
123 GTEST_LOG_(INFO) << "PowerStateLevelFramework005: power state replacement is end";
124 }
125
126 /**
127 * @tc.name: PowerStateLevelFramework006
128 * @tc.desc: test the conversion between power states
129 * @tc.type: FUNC
130 * @tc.require: issueI6OM1F
131 */
132 HWTEST_F (PowerStateLevelFrameworkTest, PowerStateLevelFramework006, TestSize.Level0)
133 {
134 GTEST_LOG_(INFO) << "PowerStateLevelFramework006: power state replacement is start";
135 auto ret = g_stateMachineTest->SetState(PowerState::DOZE, StateChangeReason::STATE_CHANGE_REASON_INIT);
136 EXPECT_TRUE(ret);
137 EXPECT_TRUE(g_stateMachineTest->GetState() == PowerState::DOZE);
138 GTEST_LOG_(INFO) << "PowerStateLevelFramework006: power state replacement is end";
139 }
140
141 /**
142 * @tc.name: PowerStateLevelFramework007
143 * @tc.desc: test the conversion between power states
144 * @tc.type: FUNC
145 * @tc.require: issueI6OM1F
146 */
147 HWTEST_F (PowerStateLevelFrameworkTest, PowerStateLevelFramework007, TestSize.Level0)
148 {
149 GTEST_LOG_(INFO) << "PowerStateLevelFramework007: power state replacement is start";
150 auto ret = g_stateMachineTest->SetState(PowerState::SLEEP, StateChangeReason::STATE_CHANGE_REASON_INIT);
151 EXPECT_TRUE(ret);
152 EXPECT_TRUE(g_stateMachineTest->GetState() == PowerState::SLEEP);
153 GTEST_LOG_(INFO) << "PowerStateLevelFramework007: power state replacement is end";
154 }
155
156 /**
157 * @tc.name: PowerStateLevelFramework008
158 * @tc.desc: test the conversion between power states
159 * @tc.type: FUNC
160 * @tc.require: issueI6OM1F
161 */
162 HWTEST_F (PowerStateLevelFrameworkTest, PowerStateLevelFramework008, TestSize.Level0)
163 {
164 GTEST_LOG_(INFO) << "PowerStateLevelFramework008: power state replacement is start";
165 auto ret = g_stateMachineTest->SetState(PowerState::HIBERNATE, StateChangeReason::STATE_CHANGE_REASON_INIT);
166 EXPECT_TRUE(ret);
167 EXPECT_TRUE(g_stateMachineTest->GetState() == PowerState::HIBERNATE);
168 GTEST_LOG_(INFO) << "PowerStateLevelFramework008: power state replacement is end";
169 }
170
171 /**
172 * @tc.name: PowerStateLevelFramework009
173 * @tc.desc: test the conversion between power states
174 * @tc.type: FUNC
175 * @tc.require: issueI6OM1F
176 */
177 HWTEST_F (PowerStateLevelFrameworkTest, PowerStateLevelFramework009, TestSize.Level0)
178 {
179 GTEST_LOG_(INFO) << "PowerStateLevelFramework009: power state replacement is start";
180 auto ret = g_stateMachineTest->SetState(PowerState::SHUTDOWN, StateChangeReason::STATE_CHANGE_REASON_INIT);
181 EXPECT_TRUE(ret);
182 EXPECT_TRUE(g_stateMachineTest->GetState() == PowerState::SHUTDOWN);
183 GTEST_LOG_(INFO) << "PowerStateLevelFramework009: power state replacement is end";
184 }
185
186 /**
187 * @tc.name: PowerStateLevelFramework010
188 * @tc.desc: test the conversion between power states
189 * @tc.type: FUNC
190 * @tc.require: issueI6OM1F
191 */
192 HWTEST_F (PowerStateLevelFrameworkTest, PowerStateLevelFramework010, TestSize.Level0)
193 {
194 GTEST_LOG_(INFO) << "PowerStateLevelFramework010: power state replacement is start";
195 std::string result;
196 g_stateMachineTest->DumpInfo(result);
197 EXPECT_TRUE(!result.empty());
198 EXPECT_TRUE(result.find("AWAKE") != 0);
199 EXPECT_TRUE(result.find("FREEZE") != 0);
200 EXPECT_TRUE(result.find("INACTIVE") != 0);
201 EXPECT_TRUE(result.find("STAND_BY") != 0);
202 EXPECT_TRUE(result.find("DOZE") != 0);
203 EXPECT_TRUE(result.find("SLEEP") != 0);
204 EXPECT_TRUE(result.find("HIBERNATE") != 0);
205 EXPECT_TRUE(result.find("SHUTDOWN") != 0);
206 GTEST_LOG_(INFO) << "dumpinfo:" << result;
207 GTEST_LOG_(INFO) << "PowerStateLevelFramework010: power state replacement is end";
208 }
209 }