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