• 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 
23 using namespace testing::ext;
24 using namespace OHOS::PowerMgr;
25 using namespace OHOS;
26 using namespace std;
27 
28 namespace {
29 sptr<PowerMgrService> g_pmsTest {nullptr};
30 constexpr int SLEEP_WAIT_TIME_MS = 1000;
31 constexpr int DELAY_WORK_WAIT_TIME_MS = 800;
32 constexpr int TRANSFER_NS_TO_MS = 1000;
33 constexpr int TRY_TIMES = 3;
34 } // namespace
35 
36 namespace OHOS {
37 namespace PowerMgr {
38 class ProximityRunningLockTest : public testing::Test {
39 public:
SetUpTestCase()40     static void SetUpTestCase() {};
TearDownTestCase()41     static void TearDownTestCase() {};
42     void SetUp();
43     void TearDown();
44 };
45 
SetUp()46 void ProximityRunningLockTest::SetUp()
47 {
48     g_pmsTest = DelayedSpSingleton<PowerMgrService>::GetInstance();
49     EXPECT_TRUE(g_pmsTest != nullptr) << "ProximityRunningLockTest fail to get PowerMgrService";
50     g_pmsTest->OnStart();
51     g_pmsTest->SuspendControllerInit();
52     g_pmsTest->WakeupControllerInit();
53 }
54 
TearDown()55 void ProximityRunningLockTest::TearDown()
56 {
57     g_pmsTest->OnStop();
58 }
59 } // namespace PowerMgr
60 } // namespace OHOS
61 
62 namespace {
63 /**
64  * @tc.name: ProximityRunningLockTest001
65  * @tc.desc: Test proximity running lock action RecordSensorCallback
66  * @tc.type: FUNC
67  * @tc.require: ICGV1M
68  */
69 HWTEST_F(ProximityRunningLockTest, ProximityRunningLockTest001, TestSize.Level1)
70 {
71     POWER_HILOGI(LABEL_TEST, "ProximityRunningLockTest001 function start!");
72     ProximityData closeData = {.distance = ProximityControllerBase::PROXIMITY_CLOSE_SCALAR};
73     SensorEvent closeEvent = {
74         .sensorTypeId = SENSOR_TYPE_ID_PROXIMITY,
75         .data = reinterpret_cast<uint8_t *>(&closeData) };
76     sptr<IRemoteObject> runningLockToken = sptr<RunningLockTokenStub>::MakeSptr();
77     RunningLockInfo info = {"ProximityRunningLockTest", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL};
78     g_pmsTest->CreateRunningLock(runningLockToken, info);
79 
80     ProximityData awayData = {.distance = ProximityControllerBase::PROXIMITY_AWAY_SCALAR};
81     SensorEvent awayEvent = {
82         .sensorTypeId = SENSOR_TYPE_ID_PROXIMITY,
83         .data = reinterpret_cast<uint8_t *>(&awayData) };
84     // Try three times
85     for (int i = 0; i < TRY_TIMES; ++i) {
86         g_pmsTest->Lock(runningLockToken);
87         RunningLockMgr::ProximityController::RecordSensorCallback(&closeEvent);
88         usleep((SLEEP_WAIT_TIME_MS + DELAY_WORK_WAIT_TIME_MS) * TRANSFER_NS_TO_MS);
89         POWER_HILOGI(LABEL_TEST, "ProximityRunningLockTest close IsScreenOn:%{public}d", g_pmsTest->IsScreenOn());
90         EXPECT_TRUE(g_pmsTest->GetRunningLockMgr()->proximityController_->IsClose());
91         EXPECT_FALSE(g_pmsTest->IsScreenOn());
92 
93         RunningLockMgr::ProximityController::RecordSensorCallback(&awayEvent);
94         usleep(SLEEP_WAIT_TIME_MS * TRANSFER_NS_TO_MS);
95         EXPECT_FALSE(g_pmsTest->GetRunningLockMgr()->proximityController_->IsClose());
96         POWER_HILOGI(LABEL_TEST, "ProximityRunningLockTest away IsScreenOn:%{public}d", g_pmsTest->IsScreenOn());
97         EXPECT_TRUE(g_pmsTest->IsScreenOn());
98 
99         if (i) {
100             g_pmsTest->isDuringCallStateEnable_ = true;
101             EXPECT_TRUE(g_pmsTest->IsDuringCallStateEnable());
102         }
103         g_pmsTest->GetPowerStateMachine()->SetDuringCallState(true);
104         g_pmsTest->GetRunningLockMgr()->HandleProximityAwayEvent();
105         g_pmsTest->GetPowerStateMachine()->SetDuringCallState(false);
106         EXPECT_TRUE(g_pmsTest->IsScreenOn());
107         g_pmsTest->UnLock(runningLockToken);
108         usleep(SLEEP_WAIT_TIME_MS * TRANSFER_NS_TO_MS / 10); // 100ms for unlock async screen on
109         if (i) {
110             g_pmsTest->isDuringCallStateEnable_ = false;
111             EXPECT_FALSE(g_pmsTest->IsDuringCallStateEnable());
112         }
113     }
114     POWER_HILOGI(LABEL_TEST, "ProximityRunningLockTest001 function end!");
115 }
116 } // namespace