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 #include "power_suspend_parse_test.h"
16 #include <fstream>
17 #include <thread>
18 #include <unistd.h>
19
20 #ifdef THERMAL_GTEST
21 #define private public
22 #define protected public
23 #endif
24
25 #include <datetime_ex.h>
26 #include <input_manager.h>
27 #include <securec.h>
28
29 #include "power_log.h"
30 #include "power_mgr_service.h"
31 #include "power_state_machine.h"
32 #include "setting_helper.h"
33
34 using namespace testing::ext;
35 using namespace OHOS::PowerMgr;
36 using namespace OHOS;
37 using namespace std;
38 static sptr<PowerMgrService> g_service;
39
SetUpTestCase(void)40 void PowerSuspendParseTest::SetUpTestCase(void)
41 {
42 g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
43 g_service->OnStart();
44 }
45
TearDownTestCase(void)46 void PowerSuspendParseTest::TearDownTestCase(void)
47 {
48 g_service->OnStop();
49 DelayedSpSingleton<PowerMgrService>::DestroyInstance();
50 }
51
52 namespace {
53 static const std::string SYSTEM_POWER_SUSPEND_DB_STRING =
54 "{ \"powerkey\": { \"action\": 0, \"delayMs\": 1002 }, \"timeout\": { \"action\": 1, \"delayMs\": 2002 }, \"lid\": "
55 "{ \"action\": 1, \"delayMs\": 3002 }, \"switch\": { \"action\": 1, \"delayMs\": 4002 } }";
56 static const std::string SYSTEM_POWER_WAKEUP_FILE = "./power_suspend.json";
57 static const std::string SYSTEM_POWER_SUSPEND_FILE = "./power_suspend.json";
58
TestPowerSuspend(PowerMgrService * pmsTest_)59 void TestPowerSuspend(PowerMgrService* pmsTest_)
60 {
61 vector<SuspendSource>::iterator itb = pmsTest_->suspendController_->sourceList_.begin();
62 vector<SuspendSource>::iterator ite = pmsTest_->suspendController_->sourceList_.end();
63 vector<SuspendSource>::iterator it = itb;
64
65 for (; it != ite; it++) {
66 if (it->GetReason() == SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY) {
67 // judge action
68 if (it->GetAction() != 1) {
69 GTEST_LOG_(INFO) << "PowerSuspendParse001: Failed to parse action";
70 }
71 // judge delay
72 if (it->GetDelay() != 0) {
73 GTEST_LOG_(INFO) << "PowerSuspendParse001: Failed to parse delay";
74 }
75 } else if (it->GetReason() == SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT) {
76 // judge action
77 if (it->GetAction() != 1) {
78 GTEST_LOG_(INFO) << "PowerSuspendParse001: Failed to parse action";
79 }
80 // judge delay
81 if (it->GetDelay() != 0) {
82 GTEST_LOG_(INFO) << "PowerSuspendParse001: Failed to parse delay";
83 }
84 } else if (it->GetReason() == SuspendDeviceType::SUSPEND_DEVICE_REASON_LID) {
85 // judge action
86 if (it->GetAction() != 1) {
87 GTEST_LOG_(INFO) << "PowerSuspendParse001: Failed to parse action";
88 }
89 // judge delay
90 if (it->GetDelay() != 0) {
91 GTEST_LOG_(INFO) << "PowerSuspendParse001: Failed to parse delay";
92 }
93 } else if (it->GetReason() == SuspendDeviceType::SUSPEND_DEVICE_REASON_SWITCH) {
94 // judge action
95 if (it->GetAction() != 1) {
96 GTEST_LOG_(INFO) << "PowerSuspendParse001: Failed to parse action";
97 }
98 // judge delay
99 if (it->GetDelay() != 0) {
100 GTEST_LOG_(INFO) << "PowerSuspendParse001: Failed to parse delay";
101 }
102 }
103 }
104 }
105
106 /**
107 * @tc.name: PowerSuspendParse001
108 * @tc.desc: test Suspend source parse
109 * @tc.type: FUNC
110 */
111 HWTEST_F(PowerSuspendParseTest, PowerSuspendParse001, TestSize.Level0)
112 {
113 POWER_HILOGI(LABEL_TEST, "PowerSuspendParse001 start.");
114 GTEST_LOG_(INFO) << "PowerSuspendParse001: start";
115 std::string str = SYSTEM_POWER_SUSPEND_FILE;
116
117 auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
118 if (pmsTest_ == nullptr) {
119 GTEST_LOG_(INFO) << "PowerSuspendParse001: Failed to get PowerMgrService";
120 }
121
122 pmsTest_->Init();
123 pmsTest_->SuspendControllerInit();
124 pmsTest_->suspendController_->Init();
125
126 TestPowerSuspend(pmsTest_);
127 GTEST_LOG_(INFO) << "PowerSuspendParse001: end";
128 POWER_HILOGI(LABEL_TEST, "PowerSuspendParse001 end.");
129 }
130 } // namespace