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