• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "power_mgr_mock_test.h"
17 #include "power_mgr_client.h"
18 
19 using namespace testing::ext;
20 using namespace OHOS::PowerMgr;
21 using namespace OHOS;
22 using namespace std;
23 using ::testing::_;
24 
25 static sptr<PowerMgrService> g_service;
26 static MockStateAction* g_shutdownState;
27 static MockStateAction* g_stateAction;
28 static MockPowerAction* g_powerAction;
29 static MockLockAction* g_lockAction;
30 
ResetMockAction()31 static void ResetMockAction()
32 {
33     POWER_HILOGD(LABEL_TEST, "ResetMockAction:Start.");
34     g_stateAction = new MockStateAction();
35     g_shutdownState = new MockStateAction();
36     g_powerAction = new MockPowerAction();
37     g_lockAction = new MockLockAction();
38     g_service->EnableMock(g_stateAction, g_shutdownState, g_powerAction, g_lockAction);
39 }
40 
SetUpTestCase(void)41 void PowerMgrMockTest::SetUpTestCase(void)
42 {
43     // create singleton service object at the beginning
44     g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
45     g_service->OnStart();
46 }
47 
TearDownTestCase(void)48 void PowerMgrMockTest::TearDownTestCase(void)
49 {
50     g_service->OnStop();
51     DelayedSpSingleton<PowerMgrService>::DestroyInstance();
52 }
53 
SetUp(void)54 void PowerMgrMockTest::SetUp(void)
55 {
56     ResetMockAction();
57 }
58 
TearDown(void)59 void PowerMgrMockTest::TearDown(void)
60 {
61 }
62 
63 namespace {
64 /**
65  * @tc.name: PowerMgrMock001
66  * @tc.desc: test RebootDeviceForDeprecated by mock
67  * @tc.type: FUNC
68  * @tc.require: issueI6MWC0
69  */
70 HWTEST_F(PowerMgrMockTest, PowerMgrMock003, TestSize.Level2)
71 {
72     GTEST_LOG_(INFO) << "PowerMgrMock003: start.";
73     POWER_HILOGD(LABEL_TEST, "PowerMgrMock003:Start.");
74 
75     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
76     if (pms == nullptr) {
77         GTEST_LOG_(INFO) << "PowerMgrMock003: Failed to get PowerMgrService";
78     }
79 
80     EXPECT_CALL(*g_powerAction, Reboot(std::string("test"))).Times(1);
81     pms->RebootDeviceForDeprecated(std::string("test"));
82 
83     POWER_HILOGD(LABEL_TEST, "PowerMgrMock003:End.");
84     GTEST_LOG_(INFO) << "PowerMgrMock003: end.";
85     usleep(SLEEP_WAIT_TIME_MS * TRANSFER_NS_TO_MS);
86 }
87 }
88