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