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