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 16 #include <gtest/gtest.h> 17 #include <unistd.h> 18 19 #define private public 20 #define protected public 21 #include "shutdown_dialog.h" 22 #undef private 23 #undef protected 24 25 #include "ability_manager_client.h" 26 #include "iremote_object.h" 27 #include "mock_power_remote_object.h" 28 #include "power_mgr_service.h" 29 #include "power_log.h" 30 31 using namespace std; 32 using namespace testing::ext; 33 using namespace OHOS; 34 using namespace OHOS::AAFwk; 35 using namespace OHOS::PowerMgr; 36 37 namespace OHOS { 38 namespace PowerMgr { 39 class PowerMgrPowerDialog : public testing::Test { 40 public: SetUpTestCase()41 static void SetUpTestCase() 42 { 43 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 44 pms->OnStart(); 45 } TearDownTestCase()46 static void TearDownTestCase() 47 { 48 auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance(); 49 pms->OnStop(); 50 } 51 }; 52 } // namespace PowerMgr 53 } // namespace OHOS 54 55 namespace { 56 constexpr int32_t RESULT_CODE = -1; 57 constexpr int32_t SLEEP_WAIT_TIME_S = 1; 58 59 /** 60 * @tc.name: LongPressKeyMonitorInitTest 61 * @tc.desc: test KeyMonitorInit and KeyMonitorCancel 62 * @tc.type: FUNC 63 */ 64 HWTEST_F(PowerMgrPowerDialog, LongPressKeyMonitorInitTest, TestSize.Level2) 65 { 66 POWER_HILOGI(LABEL_TEST, "LongPressKeyMonitorInitTest start"); 67 ShutdownDialog shutdownDialog; 68 shutdownDialog.KeyMonitorInit(); 69 EXPECT_TRUE(shutdownDialog.longPressId_ >= OHOS::ERR_OK); 70 shutdownDialog.KeyMonitorCancel(); 71 EXPECT_TRUE(shutdownDialog.longPressId_ == OHOS::ERR_OK); 72 POWER_HILOGI(LABEL_TEST, "LongPressKeyMonitorInitTest end"); 73 } 74 75 /** 76 * @tc.name: ConnectSystemUiDialogShowTest 77 * @tc.desc: test power dialog has been show 78 * @tc.type: FUNC 79 */ 80 HWTEST_F(PowerMgrPowerDialog, ConnectSystemUiDialogShowTest, TestSize.Level2) 81 { 82 POWER_HILOGI(LABEL_TEST, "ConnectSystemUiDialogShowTest start"); 83 ShutdownDialog shutdownDialog; 84 sptr<IRemoteObject> sptrRemoteObj = new MockPowerRemoteObject(); 85 MockPowerRemoteObject::SetRequestValue(ERR_OK); 86 AppExecFwk::ElementName element; 87 shutdownDialog.dialogConnectionCallback_->OnAbilityConnectDone(element, sptrRemoteObj, RESULT_CODE); 88 sleep(SLEEP_WAIT_TIME_S); 89 EXPECT_TRUE(shutdownDialog.ConnectSystemUi()); 90 POWER_HILOGI(LABEL_TEST, "ConnectSystemUiDialogShowTest end"); 91 } 92 93 /** 94 * @tc.name: OnAbilityConnectDoneTestFail 95 * @tc.desc: test OnAbilityConnectDone SendRequest falied 96 * @tc.type: FUNC 97 */ 98 HWTEST_F(PowerMgrPowerDialog, OnAbilityConnectDoneTestFail, TestSize.Level2) 99 { 100 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTestFail start"); 101 ShutdownDialog shutdownDialog; 102 shutdownDialog.ResetLongPressFlag(); 103 AppExecFwk::ElementName element; 104 sptr<IRemoteObject> sptrRemoteObj = new MockPowerRemoteObject(); 105 MockPowerRemoteObject::SetRequestValue(ERR_NO_INIT); 106 shutdownDialog.dialogConnectionCallback_->OnAbilityConnectDone(element, sptrRemoteObj, RESULT_CODE); 107 sleep(SLEEP_WAIT_TIME_S); 108 EXPECT_FALSE(shutdownDialog.IsLongPress()); 109 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTestFail end"); 110 } 111 112 /** 113 * @tc.name: OnAbilityConnectDoneTestRemoteNull 114 * @tc.desc: test OnAbilityConnectDone remoteObj is null 115 * @tc.type: FUNC 116 */ 117 HWTEST_F(PowerMgrPowerDialog, OnAbilityConnectDoneTestRemoteNull, TestSize.Level2) 118 { 119 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTestRemoteNull start"); 120 ShutdownDialog shutdownDialog; 121 shutdownDialog.ResetLongPressFlag(); 122 AppExecFwk::ElementName element; 123 sptr<IRemoteObject> sptrRemoteObj = nullptr; 124 shutdownDialog.dialogConnectionCallback_->OnAbilityConnectDone(element, sptrRemoteObj, RESULT_CODE); 125 sleep(SLEEP_WAIT_TIME_S); 126 EXPECT_FALSE(shutdownDialog.IsLongPress()); 127 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTestRemoteNull end"); 128 } 129 130 /** 131 * @tc.name: OnAbilityConnectDoneTest 132 * @tc.desc: test OnAbilityConnectDone succ 133 * @tc.type: FUNC 134 */ 135 HWTEST_F(PowerMgrPowerDialog, OnAbilityConnectDoneTest, TestSize.Level2) 136 { 137 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTest start"); 138 ShutdownDialog shutdownDialog; 139 shutdownDialog.ResetLongPressFlag(); 140 AppExecFwk::ElementName element; 141 sptr<IRemoteObject> sptrRemoteObj = new MockPowerRemoteObject(); 142 MockPowerRemoteObject::SetRequestValue(ERR_OK); 143 shutdownDialog.dialogConnectionCallback_->OnAbilityConnectDone(element, sptrRemoteObj, RESULT_CODE); 144 sleep(SLEEP_WAIT_TIME_S); 145 EXPECT_TRUE(shutdownDialog.IsLongPress()); 146 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTest end"); 147 } 148 149 /** 150 * @tc.name: OnAbilityDisconnectDoneTest 151 * @tc.desc: test OnAbilityDisconnectDone succ 152 * @tc.type: FUNC 153 */ 154 HWTEST_F(PowerMgrPowerDialog, OnAbilityDisconnectDoneTest, TestSize.Level2) 155 { 156 POWER_HILOGI(LABEL_TEST, "OnAbilityDisconnectDoneTest start"); 157 ShutdownDialog shutdownDialog; 158 AppExecFwk::ElementName element; 159 shutdownDialog.dialogConnectionCallback_->OnAbilityDisconnectDone(element, RESULT_CODE); 160 sleep(SLEEP_WAIT_TIME_S); 161 EXPECT_FALSE(shutdownDialog.IsLongPress()); 162 POWER_HILOGI(LABEL_TEST, "OnAbilityDisconnectDoneTest end"); 163 } 164 } // namespace 165