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_system_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> g_service;
25 static MockStateAction* g_shutdownState;
26 static MockStateAction* g_powerState;
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_powerState = new MockStateAction();
34     g_shutdownState = new MockStateAction();
35     g_powerAction = new MockPowerAction();
36     g_lockAction = new MockLockAction();
37     g_service->EnableMock(g_powerState, g_shutdownState, g_powerAction, g_lockAction);
38 }
39 
SetUpTestCase(void)40 void PowerMgrMockSystemTest::SetUpTestCase(void)
41 {
42     // create singleton g_service object at the beginning
43     g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
44     g_service->OnStart();
45 }
46 
TearDownTestCase(void)47 void PowerMgrMockSystemTest::TearDownTestCase(void)
48 {
49     g_service->OnStop();
50     DelayedSpSingleton<PowerMgrService>::DestroyInstance();
51 }
52 
SetUp(void)53 void PowerMgrMockSystemTest::SetUp(void)
54 {
55     ResetMockAction();
56 }
57 
TearDown(void)58 void PowerMgrMockSystemTest::TearDown(void)
59 {
60 }
61 
62 namespace {
63 /**
64  * @tc.name: PowerMgrMock106
65  * @tc.desc: test proximity RunningLock by mock
66  * @tc.type: FUNC
67  * @tc.require: issueI5MJZJ
68  */
69 HWTEST_F(PowerMgrMockSystemTest, PowerMgrMock106, TestSize.Level2)
70 {
71     GTEST_LOG_(INFO) << "PowerMgrMock106: start";
72     POWER_HILOGI(LABEL_TEST, "PowerMgrMock106:Start");
73 
74     sptr<PowerMgrService> pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
75     if (pms == nullptr) {
76         GTEST_LOG_(INFO) << "PowerMgrMock106: Failed to get PowerMgrService";
77     }
78 
79     sptr<IRemoteObject> token = new RunningLockTokenStub();
80     RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
81     pms->CreateRunningLock(token, info);
82     pms->SetDisplaySuspend(true);
83     pms->Lock(token);
84     EXPECT_EQ(pms->IsUsed(token), true);
85     pms->UnLock(token);
86 
87     POWER_HILOGI(LABEL_TEST, "PowerMgrMock106:End");
88     GTEST_LOG_(INFO) << "PowerMgrMock106: end";
89 }
90 }