1 /*
2  * Copyright (c) 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 #include "power_screenon_mock_test.h"
16 #include <fstream>
17 #include <thread>
18 #include <unistd.h>
19 
20 #ifdef POWERMGR_GTEST
21 #define private   public
22 #define protected public
23 #endif
24 
25 #include <datetime_ex.h>
26 #include <input_manager.h>
27 #include <securec.h>
28 
29 #include "permission.h"
30 #include "power_mgr_service.h"
31 #include "power_state_callback_stub.h"
32 #include "power_state_machine.h"
33 #include "setting_helper.h"
34 
35 using namespace testing::ext;
36 using namespace OHOS::PowerMgr;
37 using namespace OHOS;
38 using namespace std;
39 static sptr<PowerMgrService> g_service;
40 static constexpr int32_t TEST_MODEL = 2;
41 static constexpr int SLEEP_WAIT_TIME_S = 2;
42 
SetUpTestCase(void)43 void PowerScreenOnMockTest::SetUpTestCase(void)
44 {
45     g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
46     g_service->OnStart();
47 }
48 
TearDownTestCase(void)49 void PowerScreenOnMockTest::TearDownTestCase(void)
50 {
51     g_service->OnStop();
52     DelayedSpSingleton<PowerMgrService>::DestroyInstance();
53 }
54 
IsScreenOn(bool needPrintLog)55 bool PowerMgrService::IsScreenOn(bool needPrintLog)
56 {
57     static uint32_t count = 0;
58     bool ret = true;
59 
60     if ((count % TEST_MODEL)) {
61         ret = !ret;
62     }
63     count++;
64     return ret;
65 }
66 
IsSystem()67 bool Permission::IsSystem()
68 {
69     return true;
70 }
71 
72 namespace {
73 /**
74  * @tc.name: PowerScreenOnMockTest001
75  * @tc.desc: test ControlListener(exception)
76  * @tc.type: FUNC
77  * @tc.require: issueI7G6OY
78  */
79 HWTEST_F(PowerScreenOnMockTest, PowerScreenOnMockTest001, TestSize.Level0)
80 {
81     GTEST_LOG_(INFO) << "PowerScreenOnMockTest001: start";
82 
83     auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
84     if (pmsTest_ == nullptr) {
85         GTEST_LOG_(INFO) << "PowerScreenOnMockTest001: Failed to get PowerMgrService";
86     }
87     pmsTest_->Init();
88     pmsTest_->WakeupControllerInit();
89     pmsTest_->wakeupController_->stateMachine_->stateAction_->SetDisplayState(DisplayState::DISPLAY_OFF);
90     pmsTest_->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON);
91     EXPECT_TRUE(pmsTest_ != nullptr);
92 
93     sleep(SLEEP_WAIT_TIME_S);
94     GTEST_LOG_(INFO) << "PowerScreenOnMockTest001:  end";
95 }
96 } // namespace