1 /*
2 * Copyright (c) 2021 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_mgr_mock_test.h"
17
18 using namespace testing::ext;
19 using namespace OHOS::PowerMgr;
20 using namespace OHOS;
21 using namespace std;
22 using ::testing::_;
23
24 static sptr<PowerMgrService> service;
25 static MockStateAction* g_powerState;
26 static MockStateAction* g_stateAction;
27 static MockPowerAction* g_powerAction;
28 static MockLockAction* g_lockAction;
29
ResetMockAction()30 static void ResetMockAction()
31 {
32 POWER_HILOGI(LABEL_TEST, "ResetMockAction:Start.");
33 g_stateAction = new MockStateAction();
34 g_powerAction = new MockPowerAction();
35 g_lockAction = new MockLockAction();
36 service->EnableMock(g_powerState, g_stateAction, g_powerAction, g_lockAction);
37 }
38
SetUpTestCase(void)39 void PowerMgrMockTest::SetUpTestCase(void)
40 {
41 // create singleton service object at the beginning
42 service = DelayedSpSingleton<PowerMgrService>::GetInstance();
43 service->OnStart();
44 ResetMockAction();
45 }
46
TearDownTestCase(void)47 void PowerMgrMockTest::TearDownTestCase(void)
48 {
49 service->OnStop();
50 DelayedSpSingleton<PowerMgrService>::DestroyInstance();
51 delete g_powerState;
52 delete g_stateAction;
53 delete g_powerAction;
54 delete g_lockAction;
55 }
56
SetUp(void)57 void PowerMgrMockTest::SetUp(void) {}
58
TearDown(void)59 void PowerMgrMockTest::TearDown(void) {}
60
61 namespace {
62 /**
63 * @tc.name: PowerMgrFailCheck001
64 * @tc.desc: test SuspendDevice failed by mock
65 * @tc.type: FUNC
66 */
67 HWTEST_F(PowerMgrMockTest, PowerMgrFailCheck001, TestSize.Level2)
68 {
69 sleep(NEXT_WAIT_TIME_S);
70 GTEST_LOG_(INFO) << "PowerMgrFailCheck001: start.";
71 POWER_HILOGI(LABEL_TEST, "PowerMgrFailCheck001:Start.");
72
73 sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
74 if (pms == nullptr) {
75 GTEST_LOG_(INFO) << "PowerMgrFailCheck001: Failed to get PowerMgrService";
76 }
77
78 EXPECT_CALL(*g_stateAction, SetDisplayState(DisplayState::DISPLAY_OFF, ::testing::_))
79 .Times(1)
80 .WillOnce(::testing::Return(ActionResult::FAILED));
81 pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY, false);
82
83 std::vector<std::string> args;
84 std::string str("-s");
85 args.push_back(str);
86 std::string dumpInfo = pms->ShellDump(args, args.size());
87 GTEST_LOG_(INFO) << dumpInfo;
88
89 ResetMockAction();
90 POWER_HILOGI(LABEL_TEST, "PowerMgrFailCheck001:End.");
91 GTEST_LOG_(INFO) << "PowerMgrFailCheck001: end.";
92 }
93
94 /**
95 * @tc.name: PowerMgrFailCheck002
96 * @tc.desc: test WakeupDevice failed by mock
97 * @tc.type: FUNC
98 */
99 HWTEST_F(PowerMgrMockTest, PowerMgrFailCheck002, TestSize.Level2)
100 {
101 sleep(NEXT_WAIT_TIME_S);
102 GTEST_LOG_(INFO) << "PowerMgrFailCheck002: start.";
103 POWER_HILOGI(LABEL_TEST, "PowerMgrFailCheck002:Start.");
104
105 sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
106 if (pms == nullptr) {
107 GTEST_LOG_(INFO) << "PowerMgrFailCheck002: Failed to get PowerMgrService";
108 }
109
110 pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY, false);
111 EXPECT_CALL(*g_stateAction, SetDisplayState(DisplayState::DISPLAY_ON, ::testing::_))
112 .Times(1)
113 .WillOnce(::testing::Return(ActionResult::FAILED));
114 pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test"));
115
116 std::vector<std::string> args;
117 std::string str("-s");
118 args.push_back(str);
119 std::string dumpInfo = pms->ShellDump(args, args.size());
120 GTEST_LOG_(INFO) << dumpInfo;
121
122 ResetMockAction();
123 POWER_HILOGI(LABEL_TEST, "PowerMgrFailCheck002:End.");
124 GTEST_LOG_(INFO) << "PowerMgrFailCheck002: end.";
125 }
126 } // namespace