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 GTEST_LOG_(INFO) << "LongPressKeyMonitorInitTest start."; 67 POWER_HILOGI(LABEL_TEST, "LongPressKeyMonitorInitTest function start!"); 68 ShutdownDialog shutdownDialog; 69 shutdownDialog.KeyMonitorInit(); 70 EXPECT_TRUE(shutdownDialog.longPressId_ >= OHOS::ERR_OK); 71 shutdownDialog.KeyMonitorCancel(); 72 EXPECT_TRUE(shutdownDialog.longPressId_ == OHOS::ERR_OK); 73 POWER_HILOGI(LABEL_TEST, "LongPressKeyMonitorInitTest function end!"); 74 GTEST_LOG_(INFO) << "LongPressKeyMonitorInitTest end."; 75 } 76 77 /** 78 * @tc.name: ConnectSystemUiDialogShowTest 79 * @tc.desc: test power dialog has been show 80 * @tc.type: FUNC 81 */ 82 HWTEST_F(PowerMgrPowerDialog, ConnectSystemUiDialogShowTest, TestSize.Level2) 83 { 84 GTEST_LOG_(INFO) << "ConnectSystemUiDialogShowTest start."; 85 POWER_HILOGI(LABEL_TEST, "ConnectSystemUiDialogShowTest function start!"); 86 ShutdownDialog shutdownDialog; 87 sptr<IRemoteObject> sptrRemoteObj = new MockPowerRemoteObject(); 88 MockPowerRemoteObject::SetRequestValue(ERR_OK); 89 AppExecFwk::ElementName element; 90 shutdownDialog.dialogConnectionCallback_->OnAbilityConnectDone(element, sptrRemoteObj, RESULT_CODE); 91 sleep(SLEEP_WAIT_TIME_S); 92 EXPECT_TRUE(shutdownDialog.ConnectSystemUi()); 93 POWER_HILOGI(LABEL_TEST, "ConnectSystemUiDialogShowTest function end!"); 94 GTEST_LOG_(INFO) << "ConnectSystemUiDialogShowTest end."; 95 } 96 97 /** 98 * @tc.name: OnAbilityConnectDoneTestFail 99 * @tc.desc: test OnAbilityConnectDone SendRequest falied 100 * @tc.type: FUNC 101 */ 102 HWTEST_F(PowerMgrPowerDialog, OnAbilityConnectDoneTestFail, TestSize.Level2) 103 { 104 GTEST_LOG_(INFO) << "OnAbilityConnectDoneTestFail start."; 105 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTestFail function start!"); 106 ShutdownDialog shutdownDialog; 107 shutdownDialog.ResetLongPressFlag(); 108 AppExecFwk::ElementName element; 109 sptr<IRemoteObject> sptrRemoteObj = new MockPowerRemoteObject(); 110 MockPowerRemoteObject::SetRequestValue(ERR_NO_INIT); 111 shutdownDialog.dialogConnectionCallback_->OnAbilityConnectDone(element, sptrRemoteObj, RESULT_CODE); 112 sleep(SLEEP_WAIT_TIME_S); 113 EXPECT_FALSE(shutdownDialog.IsLongPress()); 114 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTestFail function end!"); 115 GTEST_LOG_(INFO) << "OnAbilityConnectDoneTestFail end."; 116 } 117 118 /** 119 * @tc.name: OnAbilityConnectDoneTestRemoteNull 120 * @tc.desc: test OnAbilityConnectDone remoteObj is null 121 * @tc.type: FUNC 122 */ 123 HWTEST_F(PowerMgrPowerDialog, OnAbilityConnectDoneTestRemoteNull, TestSize.Level2) 124 { 125 GTEST_LOG_(INFO) << "OnAbilityConnectDoneTestRemoteNull start."; 126 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTestRemoteNull function start!"); 127 ShutdownDialog shutdownDialog; 128 shutdownDialog.ResetLongPressFlag(); 129 AppExecFwk::ElementName element; 130 sptr<IRemoteObject> sptrRemoteObj = nullptr; 131 shutdownDialog.dialogConnectionCallback_->OnAbilityConnectDone(element, sptrRemoteObj, RESULT_CODE); 132 sleep(SLEEP_WAIT_TIME_S); 133 EXPECT_FALSE(shutdownDialog.IsLongPress()); 134 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTestRemoteNull function end!"); 135 GTEST_LOG_(INFO) << "OnAbilityConnectDoneTestRemoteNull end."; 136 } 137 138 /** 139 * @tc.name: OnAbilityConnectDoneTest 140 * @tc.desc: test OnAbilityConnectDone succ 141 * @tc.type: FUNC 142 */ 143 HWTEST_F(PowerMgrPowerDialog, OnAbilityConnectDoneTest, TestSize.Level2) 144 { 145 GTEST_LOG_(INFO) << "OnAbilityConnectDoneTest start."; 146 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTest function start!"); 147 ShutdownDialog shutdownDialog; 148 shutdownDialog.ResetLongPressFlag(); 149 AppExecFwk::ElementName element; 150 sptr<IRemoteObject> sptrRemoteObj = new MockPowerRemoteObject(); 151 MockPowerRemoteObject::SetRequestValue(ERR_OK); 152 shutdownDialog.dialogConnectionCallback_->OnAbilityConnectDone(element, sptrRemoteObj, RESULT_CODE); 153 sleep(SLEEP_WAIT_TIME_S); 154 EXPECT_TRUE(shutdownDialog.IsLongPress()); 155 POWER_HILOGI(LABEL_TEST, "OnAbilityConnectDoneTest function end!"); 156 GTEST_LOG_(INFO) << "OnAbilityConnectDoneTest end."; 157 } 158 159 /** 160 * @tc.name: OnAbilityDisconnectDoneTest 161 * @tc.desc: test OnAbilityDisconnectDone succ 162 * @tc.type: FUNC 163 */ 164 HWTEST_F(PowerMgrPowerDialog, OnAbilityDisconnectDoneTest, TestSize.Level2) 165 { 166 GTEST_LOG_(INFO) << "OnAbilityDisconnectDoneTest start."; 167 POWER_HILOGI(LABEL_TEST, "OnAbilityDisconnectDoneTest function start!"); 168 ShutdownDialog shutdownDialog; 169 AppExecFwk::ElementName element; 170 shutdownDialog.dialogConnectionCallback_->OnAbilityDisconnectDone(element, RESULT_CODE); 171 sleep(SLEEP_WAIT_TIME_S); 172 EXPECT_FALSE(shutdownDialog.IsLongPress()); 173 POWER_HILOGI(LABEL_TEST, "OnAbilityDisconnectDoneTest function end!"); 174 GTEST_LOG_(INFO) << "OnAbilityDisconnectDoneTest end."; 175 } 176 } // namespace 177