• 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 "ohos.runningLock.proj.hpp"
19 #include "ohos.runningLock.impl.hpp"
20 #include "ohos.runningLock.user.hpp"
21 #include "taihe/runtime.hpp"
22 #include "power_mgr_errors.h"
23 #include "power_mgr_client.h"
24 #include "power_errors.h"
25 #include "power_log.h"
26 
27 using namespace taihe;
28 using namespace ohos::runningLock;
29 using namespace OHOS::PowerMgr;
30 
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace {
35 PowerErrors g_error = PowerErrors::ERR_OK;
36 std::shared_ptr<OHOS::PowerMgr::RunningLock> g_runningLock {nullptr};
37 bool g_pass = false;
38 int g_lockReturn = OHOS::ERR_OK;
39 const int32_t TIMEOUT = -1;
40 }
41 
42 namespace taihe {
set_business_error(int32_t err_code,taihe::string_view msg)43 void set_business_error(int32_t err_code, taihe::string_view msg)
44 {
45     (void)err_code;
46     (void)msg;
47 }
48 }
49 
50 namespace OHOS::PowerMgr {
CreateRunningLock(const std::string & name,RunningLockType type)51 std::shared_ptr<RunningLock> PowerMgrClient::CreateRunningLock(const std::string& name, RunningLockType type)
52 {
53     g_pass = true;
54     return g_runningLock;
55 }
56 
GetError()57 PowerErrors PowerMgrClient::GetError()
58 {
59     return g_error;
60 }
61 
Lock(int32_t timeOutMs)62 ErrCode RunningLock::Lock(int32_t timeOutMs)
63 {
64     return g_lockReturn;
65 }
66 
UnLock()67 ErrCode RunningLock::UnLock()
68 {
69     return g_lockReturn;
70 }
71 
IsUsed()72 bool RunningLock::IsUsed()
73 {
74     if (g_lockReturn == OHOS::PowerMgr::E_PERMISSION_DENIED) {
75         return false;
76     }
77     return true;
78 }
79 } // namespace OHOS::PowerMgr
80 
81 namespace {
82 class RunningLockTaiheNativeTest : public ::testing::Test {
83 public:
SetUpTestCase()84     static void SetUpTestCase() {}
TearDownTestCase()85     static void TearDownTestCase() {}
SetUp()86     void SetUp() {}
TearDown()87     void TearDown()
88     {
89         g_error = PowerErrors::ERR_OK;
90         g_runningLock = nullptr;
91         g_pass = false;
92     }
93 };
94 
95 /**
96  * @tc.name: RunningLockTaiheNativeTest_001
97  * @tc.desc: test running lock taihe native
98  * @tc.type: FUNC
99  * @tc.require: issue#ICAK9Z
100  */
101 HWTEST_F(RunningLockTaiheNativeTest, RunningLockTaiheNativeTest_001, TestSize.Level1)
102 {
103     POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_001 start");
104     const string_view name = "RunningLockTaiheNativeTest_001";
105     const ohos::runningLock::RunningLockType type(
106         ohos::runningLock::RunningLockType::key_t::PROXIMITY_SCREEN_CONTROL);
107     ohos::runningLock::RunningLock lock = CreateSync(name, type);
108     EXPECT_FALSE(lock.is_error());
109     lock->Hold(TIMEOUT);
110     bool isHolding = lock->IsHolding();
111     EXPECT_FALSE(isHolding);
112     lock->Unhold();
113 
114     g_error = PowerErrors::ERR_FAILURE;
115     CreateSync(name, type);
116 
117     g_error = PowerErrors::ERR_PERMISSION_DENIED;
118     CreateSync(name, type);
119     EXPECT_TRUE(g_pass);
120     POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_001 end");
121 }
122 
123 /**
124  * @tc.name: RunningLockTaiheNativeTest_002
125  * @tc.desc: test running lock taihe native
126  * @tc.type: FUNC
127  * @tc.require: issue#ICAK9Z
128  */
129 HWTEST_F(RunningLockTaiheNativeTest, RunningLockTaiheNativeTest_002, TestSize.Level1)
130 {
131     POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_002 start");
132     const string_view name = "RunningLockTaiheNativeTest_002";
133     const ohos::runningLock::RunningLockType type(
134         ohos::runningLock::RunningLockType::key_t::PROXIMITY_SCREEN_CONTROL);
135     g_runningLock = std::make_shared<OHOS::PowerMgr::RunningLock>(
136         nullptr, std::string(name), OHOS::PowerMgr::RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
137     ohos::runningLock::RunningLock lock = CreateSync(name, type);
138     EXPECT_FALSE(lock.is_error());
139     lock->Hold(TIMEOUT);
140     bool isHolding = lock->IsHolding();
141     EXPECT_TRUE(isHolding);
142     lock->Unhold();
143 
144     g_lockReturn = E_PERMISSION_DENIED;
145     lock->Hold(TIMEOUT);
146     isHolding = lock->IsHolding();
147     EXPECT_FALSE(isHolding);
148     lock->Unhold();
149     EXPECT_TRUE(g_pass);
150     POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_002 end");
151 }
152 
153 /**
154  * @tc.name: RunningLockTaiheNativeTest_003
155  * @tc.desc: test running lock taihe native
156  * @tc.type: FUNC
157  * @tc.require: issue#ICAK9Z
158  */
159 HWTEST_F(RunningLockTaiheNativeTest, RunningLockTaiheNativeTest_003, TestSize.Level1)
160 {
161     POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_003 start");
162     ohos::runningLock::RunningLockType type(ohos::runningLock::RunningLockType::key_t::PROXIMITY_SCREEN_CONTROL);
163     EXPECT_TRUE(IsSupported(type));
164     POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_003 end");
165 }
166 } // namespace