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