• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 
18 #include "power_log.h"
19 #define private public
20 #include "power_mgr_service.h"
21 #undef private
22 #include "power_hookmgr.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::PowerMgr;
26 using namespace OHOS;
27 using namespace std;
28 
29 namespace {
30 constexpr uint32_t AWAY_PROXIMITY_ACTION = 0;
31 constexpr uint32_t CLOSE_PROXIMITY_ACTION = 1;
32 constexpr uint32_t INVALID_PROXIMITY_ACTION = 2;
33 sptr<PowerMgrService> g_pmsTest {nullptr};
34 std::shared_ptr<IProximityController> g_controller {nullptr};
35 std::function<void(uint32_t)> g_action {nullptr};
36 bool g_isRealInstance = false;
SetInstanceReal()37 void SetInstanceReal()
38 {
39     g_isRealInstance = true;
40 }
41 } // namespace
42 
43 #ifdef POWER_MANAGER_INIT_PROXIMITY_CONTROLLER
HookMgrExecute(HOOK_MGR * hookMgr,int stage,void * context,const HOOK_EXEC_OPTIONS * options)44 int HookMgrExecute(HOOK_MGR* hookMgr, int stage, void* context, const HOOK_EXEC_OPTIONS* options)
45 {
46     if (static_cast<PowerHookStage>(stage) == PowerHookStage::POWER_PROXIMITY_CONTROLLER_INIT) {
47         ProximityControllerContext* proximityControllerContext = static_cast<ProximityControllerContext*>(context);
48         proximityControllerContext->controllerPtr = g_controller;
49         g_action = proximityControllerContext->action;
50     }
51     POWER_HILOGI(LABEL_TEST, "Mock HookMgrExecute stage is %{public}d", stage);
52     return 0;
53 }
54 #endif
55 
56 namespace OHOS {
57 namespace PowerMgr {
58 template<> sptr<PowerMgrService> DelayedSpSingleton<PowerMgrService>::instance_;
59 
GetInstance()60 template<> sptr<PowerMgrService> DelayedSpSingleton<PowerMgrService>::GetInstance()
61 {
62     if (g_isRealInstance) {
63         if (!instance_) {
64             std::lock_guard<std::mutex> lock(mutex_);
65             if (instance_ == nullptr) {
66                 instance_ = new PowerMgrService();
67             }
68         }
69         POWER_HILOGI(LABEL_TEST, "Power service 1");
70         return instance_;
71     } else {
72         POWER_HILOGI(LABEL_TEST, "Power service 0");
73         return nullptr;
74     }
75 }
76 
DestroyInstance()77 template<> void DelayedSpSingleton<PowerMgrService>::DestroyInstance()
78 {
79     std::lock_guard<std::mutex> lock(mutex_);
80     if (instance_) {
81         instance_.clear();
82         instance_ = nullptr;
83     }
84 }
85 
86 class RunningLockMgrAbnormalTest : public testing::Test {
87 public:
88     static void SetUpTestCase();
89     static void TearDownTestCase();
90     void SetUp();
91     void TearDown();
92 };
93 
SetUpTestCase()94 void RunningLockMgrAbnormalTest::SetUpTestCase()
95 {
96     g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
97     ASSERT_EQ(g_pmsTest, nullptr);
98 }
99 
TearDownTestCase()100 void RunningLockMgrAbnormalTest::TearDownTestCase()
101 {
102 }
103 
SetUp()104 void RunningLockMgrAbnormalTest::SetUp()
105 {
106 }
107 
TearDown()108 void RunningLockMgrAbnormalTest::TearDown()
109 {
110     DelayedSpSingleton<PowerMgrService>::DestroyInstance();
111     g_pmsTest = nullptr;
112     g_isRealInstance = false;
113     g_controller = nullptr;
114     g_action = nullptr;
115 }
116 } // namespace PowerMgr
117 } // namespace OHOS
118 
119 namespace {
120 /**
121  * @tc.name: RunningLockMgrAbnormalTest001
122  * @tc.desc: Test proximity running lock action HandleProximityAwayEvent abnormal
123  * @tc.type: FUNC
124  * @tc.require: ICGV1M
125  */
126 HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest001, TestSize.Level1)
127 {
128     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest001 function start!");
129     std::shared_ptr<RunningLockMgr> lockMgr = std::make_shared<RunningLockMgr>(nullptr);
130     lockMgr->HandleProximityAwayEvent();
131     EXPECT_EQ(g_pmsTest, nullptr);
132 
133     SetInstanceReal();
134     g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
135     lockMgr = std::make_shared<RunningLockMgr>(nullptr);
136     lockMgr->HandleProximityAwayEvent();
137     EXPECT_NE(g_pmsTest, nullptr);
138 
139     g_pmsTest->powerStateMachine_ = std::make_shared<PowerStateMachine>(nullptr);
140     lockMgr->HandleProximityAwayEvent();
141     EXPECT_NE(g_pmsTest->GetPowerStateMachine(), nullptr);
142     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest001 function end!");
143 }
144 
145 /**
146  * @tc.name: RunningLockMgrAbnormalTest002
147  * @tc.desc: Test proximity running lock action HandleProximityCloseEvent abnormal
148  * @tc.type: FUNC
149  * @tc.require: ICGV1M
150  */
151 HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest002, TestSize.Level1)
152 {
153     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest002 function start!");
154     std::shared_ptr<RunningLockMgr> lockMgr = std::make_shared<RunningLockMgr>(nullptr);
155     lockMgr->HandleProximityCloseEvent();
156     EXPECT_EQ(g_pmsTest, nullptr);
157 
158     SetInstanceReal();
159     g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
160     lockMgr = std::make_shared<RunningLockMgr>(nullptr);
161     lockMgr->HandleProximityCloseEvent();
162     EXPECT_NE(g_pmsTest, nullptr);
163     EXPECT_EQ(g_pmsTest->powerStateMachine_, nullptr);
164 
165     g_pmsTest->powerStateMachine_ = std::make_shared<PowerStateMachine>(nullptr);
166     lockMgr->HandleProximityCloseEvent();
167     EXPECT_NE(g_pmsTest->GetPowerStateMachine(), nullptr);
168     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest002 function end!");
169 }
170 
171 /**
172  * @tc.name: RunningLockMgrAbnormalTest003
173  * @tc.desc: Test proximity running lock action InitProximityController
174  * @tc.type: FUNC
175  * @tc.require: ICGV1M
176  */
177 HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest003, TestSize.Level1)
178 {
179     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest003 function start!");
180     std::shared_ptr<RunningLockMgr> lockMgr = std::make_shared<RunningLockMgr>(nullptr);
181     bool ret = lockMgr->InitProximityController();
182     EXPECT_TRUE(lockMgr->proximityController_ != nullptr);
183 
184     lockMgr = std::make_shared<RunningLockMgr>(nullptr);
185     g_controller = std::make_shared<RunningLockMgr::ProximityController>();
186     ret = lockMgr->InitProximityController();
187     EXPECT_TRUE(lockMgr->proximityController_ != nullptr);
188 
189     ret = lockMgr->InitProximityController();
190     EXPECT_TRUE(ret);
191     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest003 function end!");
192 }
193 
194 /**
195  * @tc.name: RunningLockMgrAbnormalTest004
196  * @tc.desc: Test proximity running lock action ProximityController OnAway abnormal
197  * @tc.type: FUNC
198  * @tc.require: ICGV1M
199  */
200 HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest004, TestSize.Level1)
201 {
202     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest004 function start!");
203     std::shared_ptr<RunningLockMgr> lockMgr = std::make_shared<RunningLockMgr>(nullptr);
204     lockMgr->InitProximityController();
205     lockMgr->proximityController_->SetEnabled(false);
206     lockMgr->proximityController_->OnAway();
207     EXPECT_FALSE(lockMgr->proximityController_->IsClose());
208 
209     lockMgr->proximityController_->SetEnabled(true);
210     lockMgr->proximityController_->SetClose(false);
211     lockMgr->proximityController_->OnAway();
212     EXPECT_FALSE(lockMgr->proximityController_->IsClose());
213 
214     lockMgr->proximityController_->SetClose(true);
215     lockMgr->proximityController_->OnAway();
216     EXPECT_TRUE(g_pmsTest == nullptr);
217     EXPECT_FALSE(lockMgr->proximityController_->IsClose());
218 
219     lockMgr->proximityController_->SetClose(true);
220     SetInstanceReal();
221     g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
222     lockMgr->proximityController_->OnAway();
223     EXPECT_TRUE(g_pmsTest != nullptr);
224     EXPECT_FALSE(lockMgr->proximityController_->IsClose());
225 
226     lockMgr->proximityController_->SetClose(true);
227     // for HandleProximityAwayEvent branch (powerStateMachine_ != nullptr)
228     g_pmsTest->powerStateMachine_ = std::make_shared<PowerStateMachine>(nullptr);
229     lockMgr->proximityController_->OnAway();
230     EXPECT_FALSE(lockMgr->proximityController_->IsClose());
231     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest004 function end!");
232 }
233 
234 /**
235  * @tc.name: RunningLockMgrAbnormalTest005
236  * @tc.desc: Test proximity running lock action ProximityController OnClose abnormal
237  * @tc.type: FUNC
238  * @tc.require: ICGV1M
239  */
240 HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest005, TestSize.Level1)
241 {
242     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest005 function start!");
243     std::shared_ptr<RunningLockMgr> lockMgr = std::make_shared<RunningLockMgr>(nullptr);
244     lockMgr->InitProximityController();
245     lockMgr->proximityController_->SetEnabled(false);
246     lockMgr->proximityController_->OnClose();
247     EXPECT_FALSE(lockMgr->proximityController_->IsClose());
248 
249     lockMgr->proximityController_->SetEnabled(true);
250     lockMgr->proximityController_->SetClose(true);
251     lockMgr->proximityController_->OnClose();
252     EXPECT_TRUE(lockMgr->proximityController_->IsClose());
253 
254     lockMgr->proximityController_->SetClose(false);
255     lockMgr->proximityController_->OnClose();
256     EXPECT_TRUE(g_pmsTest == nullptr);
257     EXPECT_TRUE(lockMgr->proximityController_->IsClose());
258 
259     lockMgr->proximityController_->SetClose(false);
260     SetInstanceReal();
261     g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
262     lockMgr->proximityController_->OnClose();
263     EXPECT_TRUE(g_pmsTest != nullptr);
264     EXPECT_TRUE(lockMgr->proximityController_->IsClose());
265 
266     lockMgr->proximityController_->SetClose(false);
267     // for HandleProximityCloseEvent branch (powerStateMachine_ != nullptr)
268     g_pmsTest->powerStateMachine_ = std::make_shared<PowerStateMachine>(nullptr);
269     lockMgr->proximityController_->OnClose();
270     EXPECT_TRUE(lockMgr->proximityController_->IsClose());
271     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest005 function end!");
272 }
273 
274 /**
275  * @tc.name: RunningLockMgrAbnormalTest006
276  * @tc.desc: Test proximity running lock action ProximityController RecordSensorCallback abnormal(pms == nullptr)
277  * @tc.type: FUNC
278  * @tc.require: ICGV1M
279  */
280 HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest006, TestSize.Level1)
281 {
282     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest006 function start!");
283     SensorEvent event = {.sensorTypeId = SENSOR_TYPE_ID_PROXIMITY};
284     RunningLockMgr::ProximityController::RecordSensorCallback(&event);
285     EXPECT_TRUE(g_pmsTest == nullptr);
286 
287     SetInstanceReal();
288     g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
289     RunningLockMgr::ProximityController::RecordSensorCallback(&event);
290     EXPECT_TRUE(g_pmsTest->GetRunningLockMgr() == nullptr);
291     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest006 function end!");
292 }
293 
294 /**
295  * @tc.name: RunningLockMgrAbnormalTest007
296  * @tc.desc: Test proximity running lock action InitLocksTypeProximity abnormal(activate_ lambda)
297  * @tc.type: FUNC
298  * @tc.require: ICGV1M
299  */
300 HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest007, TestSize.Level1)
301 {
302     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest007 function start!");
303     std::shared_ptr<RunningLockMgr> lockMgr = std::make_shared<RunningLockMgr>(nullptr);
304     lockMgr->InitLocksTypeProximity();
305     RunningLockParam lockParam;
306     auto iterator = lockMgr->lockCounters_.find(RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
307     if (iterator != lockMgr->lockCounters_.end()) {
308         iterator->second->activate_(false, lockParam);
309         SetInstanceReal();
310         g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
311         iterator->second->activate_(false, lockParam);
312         EXPECT_FALSE(lockMgr->proximityController_->IsEnabled());
313     } else {
314         GTEST_FAIL();
315     }
316     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest007 function end!");
317 }
318 
319 /**
320  * @tc.name: RunningLockMgrAbnormalTest008
321  * @tc.desc: Test proximity running lock action Lambda action
322  * @tc.type: FUNC
323  * @tc.require: ICGV1M
324  */
325 #ifdef POWER_MANAGER_INIT_PROXIMITY_CONTROLLER
326 HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest008, TestSize.Level1)
327 {
328     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest008 function start!");
329     g_controller = std::make_shared<RunningLockMgr::ProximityController>();
330     std::shared_ptr<RunningLockMgr> lockMgr = std::make_shared<RunningLockMgr>(nullptr);
331     lockMgr->InitLocksTypeProximity();
332     ASSERT_TRUE(g_action != nullptr) << "g_action == nullptr";
333 
334     g_action(INVALID_PROXIMITY_ACTION);
335     EXPECT_TRUE(g_pmsTest == nullptr);
336 
337     SetInstanceReal();
338     g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
339     EXPECT_TRUE(g_pmsTest != nullptr);
340     g_action(INVALID_PROXIMITY_ACTION);
341 
342     g_pmsTest->runningLockMgr_ = lockMgr;
343     EXPECT_TRUE(g_pmsTest->GetRunningLockMgr() != nullptr);
344     g_action(INVALID_PROXIMITY_ACTION);
345 
346     g_action(CLOSE_PROXIMITY_ACTION);
347 
348     g_action(AWAY_PROXIMITY_ACTION);
349     EXPECT_FALSE(lockMgr->IsProximityClose());
350     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest008 function end!");
351 }
352 #endif
353 
354 /**
355  * @tc.name: RunningLockMgrAbnormalTest009
356  * @tc.desc: Test SuspendController pms == nullptr
357  * @tc.type: FUNC
358  * @tc.require: ICGV1M
359  */
360 HWTEST_F(RunningLockMgrAbnormalTest, RunningLockMgrAbnormalTest009, TestSize.Level1)
361 {
362     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest009 function start!");
363     auto suspendController = std::make_shared<SuspendController>(nullptr, nullptr, nullptr);
364     EXPECT_TRUE(suspendController->NeedToSkipCurrentSuspend(
365         SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY, 0, 0));
366     POWER_HILOGI(LABEL_TEST, "RunningLockMgrAbnormalTest009 function end!");
367 }
368 } // namespace