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_HILOGD(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 {
59 }
60
TearDown(void)61 void PowerMgrMockTest::TearDown(void)
62 {
63 }
64
65 namespace {
66 /**
67 * @tc.name: PowerMgrFailCheck001
68 * @tc.desc: test SuspendDevice failed by mock
69 * @tc.type: FUNC
70 */
71 HWTEST_F (PowerMgrMockTest, PowerMgrFailCheck001, TestSize.Level2)
72 {
73 sleep(NEXT_WAIT_TIME_S);
74 GTEST_LOG_(INFO) << "PowerMgrFailCheck001: start.";
75 POWER_HILOGD(LABEL_TEST, "PowerMgrFailCheck001:Start.");
76
77 sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
78 if (pms == nullptr) {
79 GTEST_LOG_(INFO) << "PowerMgrFailCheck001: Failed to get PowerMgrService";
80 }
81
82 EXPECT_CALL(*g_stateAction, SetDisplayState(DisplayState::DISPLAY_OFF, ::testing::_))
83 .Times(1)
84 .WillOnce(::testing::Return(ActionResult::FAILED));
85 pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_BUTTON, false);
86
87 std::vector<std::string> args;
88 std::string str("-s");
89 args.push_back(str);
90 std::string dumpInfo = pms->ShellDump(args, args.size());
91 GTEST_LOG_(INFO) << dumpInfo;
92
93 ResetMockAction();
94 POWER_HILOGD(LABEL_TEST, "PowerMgrFailCheck001:End.");
95 GTEST_LOG_(INFO) << "PowerMgrFailCheck001: end.";
96 }
97
98 /**
99 * @tc.name: PowerMgrFailCheck002
100 * @tc.desc: test WakeupDevice failed by mock
101 * @tc.type: FUNC
102 */
103 HWTEST_F (PowerMgrMockTest, PowerMgrFailCheck002, TestSize.Level2)
104 {
105 sleep(NEXT_WAIT_TIME_S);
106 GTEST_LOG_(INFO) << "PowerMgrFailCheck002: start.";
107 POWER_HILOGD(LABEL_TEST, "PowerMgrFailCheck002:Start.");
108
109 sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
110 if (pms == nullptr) {
111 GTEST_LOG_(INFO) << "PowerMgrFailCheck002: Failed to get PowerMgrService";
112 }
113
114 pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_BUTTON, false);
115 EXPECT_CALL(*g_stateAction, SetDisplayState(DisplayState::DISPLAY_ON, ::testing::_))
116 .Times(1)
117 .WillOnce(::testing::Return(ActionResult::FAILED));
118 pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test"));
119
120 std::vector<std::string> args;
121 std::string str("-s");
122 args.push_back(str);
123 std::string dumpInfo = pms->ShellDump(args, args.size());
124 GTEST_LOG_(INFO) << dumpInfo;
125
126 ResetMockAction();
127 POWER_HILOGD(LABEL_TEST, "PowerMgrFailCheck002:End.");
128 GTEST_LOG_(INFO) << "PowerMgrFailCheck002: end.";
129 }
130 }