1 /* 2 * Copyright (c) 2023-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 #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 #include "power_log.h" 35 36 using namespace testing::ext; 37 using namespace OHOS::PowerMgr; 38 using namespace OHOS; 39 using namespace std; 40 static sptr<PowerMgrService> g_service; 41 static constexpr int32_t TEST_MODEL = 2; 42 static constexpr int SLEEP_WAIT_TIME_S = 2; 43 SetUpTestCase(void)44void PowerScreenOnMockTest::SetUpTestCase(void) 45 { 46 g_service = DelayedSpSingleton<PowerMgrService>::GetInstance(); 47 g_service->OnStart(); 48 } 49 TearDownTestCase(void)50void PowerScreenOnMockTest::TearDownTestCase(void) 51 { 52 g_service->OnStop(); 53 DelayedSpSingleton<PowerMgrService>::DestroyInstance(); 54 } 55 IsScreenOn(bool needPrintLog)56bool PowerMgrService::IsScreenOn(bool needPrintLog) 57 { 58 static uint32_t count = 0; 59 bool ret = true; 60 61 if ((count % TEST_MODEL)) { 62 ret = !ret; 63 } 64 count++; 65 return ret; 66 } 67 IsSystem()68bool Permission::IsSystem() 69 { 70 return true; 71 } 72 73 namespace { 74 /** 75 * @tc.name: PowerScreenOnMockTest001 76 * @tc.desc: test ControlListener(exception) 77 * @tc.type: FUNC 78 * @tc.require: issueI7G6OY 79 */ 80 HWTEST_F(PowerScreenOnMockTest, PowerScreenOnMockTest001, TestSize.Level0) 81 { 82 POWER_HILOGI(LABEL_TEST, "PowerScreenOnMockTest001 start."); 83 GTEST_LOG_(INFO) << "PowerScreenOnMockTest001: start"; 84 85 auto pmsTest_ = DelayedSpSingleton<PowerMgrService>::GetInstance(); 86 if (pmsTest_ == nullptr) { 87 GTEST_LOG_(INFO) << "PowerScreenOnMockTest001: Failed to get PowerMgrService"; 88 } 89 pmsTest_->Init(); 90 pmsTest_->WakeupControllerInit(); 91 pmsTest_->wakeupController_->stateMachine_->stateAction_->SetDisplayState(DisplayState::DISPLAY_OFF); 92 pmsTest_->wakeupController_->ControlListener(WakeupDeviceType ::WAKEUP_DEVICE_POWER_BUTTON); 93 EXPECT_TRUE(pmsTest_ != nullptr); 94 95 sleep(SLEEP_WAIT_TIME_S); 96 POWER_HILOGI(LABEL_TEST, "PowerScreenOnMockTest001 end."); 97 GTEST_LOG_(INFO) << "PowerScreenOnMockTest001: end"; 98 } 99 } // namespace