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_wakeup_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 #include "power_log.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 PowerWakeupParseTest::SetUpTestCase(void)
41 {
42 g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
43 g_service->OnStart();
44 }
45
TearDownTestCase(void)46 void PowerWakeupParseTest::TearDownTestCase(void)
47 {
48 g_service->OnStop();
49 DelayedSpSingleton<PowerMgrService>::DestroyInstance();
50 }
51
52 namespace {
53 static const constexpr uint32_t DOUBLC_CLICK_INNER = 2;
54 static const std::string SYSTEM_POWER_WAKEUP_DB_STRING =
55 "{\"powerkey\": {\"enable\": false},\"keyborad\": {\"enable\": false},\"mouse\": {\"enable\": "
56 "false},\"touchscreen\": {\"enable\": false,\"click\": 2},\"touchpad\": {\"enable\": false},\"pen\": {\"enable\": "
57 "false},\"lid\": {\"enable\": false},\"switch\": {\"enable\": false}}";
58 static const std::string SYSTEM_POWER_WAKEUP_FILE = "./power_wakeup.json";
59
TestPowerWakeup(PowerMgrService * pmsTest_)60 void TestPowerWakeup(PowerMgrService* pmsTest_)
61 {
62 vector<WakeupSource>::iterator itb = pmsTest_->wakeupController_->sourceList_.begin();
63 vector<WakeupSource>::iterator ite = pmsTest_->wakeupController_->sourceList_.end();
64 vector<WakeupSource>::iterator it = itb;
65
66 for (; it != ite; it++) {
67 if (it->GetReason() == WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON) {
68 if (it->IsEnable() != true) {
69 GTEST_LOG_(INFO) << "PowerWakeupParse001: Failed to parse Enable";
70 }
71 } else if (it->GetReason() == WakeupDeviceType::WAKEUP_DEVICE_KEYBOARD) {
72 if (it->IsEnable() != true) {
73 GTEST_LOG_(INFO) << "PowerWakeupParse001: Failed to parse Enable";
74 }
75 } else if (it->GetReason() == WakeupDeviceType::WAKEUP_DEVICE_TOUCH_SCREEN) {
76 if (it->IsEnable() != true) {
77 GTEST_LOG_(INFO) << "PowerWakeupParse001: Failed to parse Enable";
78 }
79 if (it->GetClick() != DOUBLC_CLICK_INNER) {
80 GTEST_LOG_(INFO) << "PowerWakeupParse001: Failed to parse Click";
81 }
82 } else if (it->GetReason() == WakeupDeviceType::WAKEUP_DEVICE_TOUCHPAD) {
83 if (it->IsEnable() != true) {
84 GTEST_LOG_(INFO) << "PowerWakeupParse001: Failed to parse Enable";
85 }
86 } else if (it->GetReason() == WakeupDeviceType::WAKEUP_DEVICE_PEN) {
87 if (it->IsEnable() != true) {
88 GTEST_LOG_(INFO) << "PowerWakeupParse001: Failed to parse Enable";
89 }
90 } else if (it->GetReason() == WakeupDeviceType::WAKEUP_DEVICE_LID) {
91 if (it->IsEnable() != true) {
92 GTEST_LOG_(INFO) << "PowerWakeupParse001: Failed to parse Enable";
93 }
94 } else if (it->GetReason() == WakeupDeviceType::WAKEUP_DEVICE_SWITCH) {
95 if (it->IsEnable() != true) {
96 GTEST_LOG_(INFO) << "PowerWakeupParse001: Failed to parse Enable";
97 }
98 } else if (it->GetReason() == WakeupDeviceType::WAKEUP_DEVICE_MOUSE) {
99 if (it->IsEnable() != true) {
100 GTEST_LOG_(INFO) << "PowerWakeupParse001: Failed to parse Enable";
101 }
102 }
103 }
104 }
105
106 /**
107 * @tc.name: PowerWakeupParse001
108 * @tc.desc: test Wakeup source parse
109 * @tc.type: FUNC
110 */
111 HWTEST_F(PowerWakeupParseTest, PowerWakeupParse001, TestSize.Level0)
112 {
113 POWER_HILOGI(LABEL_TEST, "PowerWakeupParse001 start");
114 GTEST_LOG_(INFO) << "PowerWakeupParse001: start";
115 std::string str = SYSTEM_POWER_WAKEUP_FILE;
116
117 auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
118 if (pmsTest_ == nullptr) {
119 GTEST_LOG_(INFO) << "PowerWakeupParse001: Failed to get PowerMgrService";
120 }
121
122 pmsTest_->Init();
123 pmsTest_->WakeupControllerInit();
124 pmsTest_->wakeupController_->Init();
125
126 TestPowerWakeup(pmsTest_);
127
128 GTEST_LOG_(INFO) << "PowerWakeupParse001: end";
129 POWER_HILOGI(LABEL_TEST, "PowerWakeupParse001 end");
130 }
131 } // namespace