1 /*
2  * Copyright (c) 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 "running_lock_timer_handler_test.h"
17 
18 #include <functional>
19 #include "power_mgr_client.h"
20 #include "running_lock.h"
21 #include "running_lock_token_stub.h"
22 
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace UnitTest {
26 namespace {
27 constexpr int32_t TIMEOUT_MS = 4000;
28 constexpr int32_t SLEEP_WAIT_TIME_S = 5;
29 constexpr int32_t COUNT_RESULT = 2;
30 }
31 using namespace testing::ext;
32 
33 /**
34  * @tc.name: RegisterRunningLockTimer
35  * @tc.desc: Test RegisterRunningLockTimer
36  * @tc.type: FUNC
37  * @tc.require: issueI9C4GG
38  */
39 HWTEST_F(RunningLockTimerHandlerTest, RegisterRunningLockTimer001, TestSize.Level0)
40 {
41     GTEST_LOG_(INFO) << "RegisterRunningLockTimer001: start";
42     sptr<IRemoteObject> token = new RunningLockTokenStub();
43     int count = 1;
__anon9f4458ee0202() 44     std::function<void()> task = [&]() {
45         count++;
46     };
47     bool ret = RunningLockTimerHandler::GetInstance().RegisterRunningLockTimer(token,
48         task, TIMEOUT_MS);
49     EXPECT_TRUE(ret);
50     sleep(SLEEP_WAIT_TIME_S);
51     EXPECT_EQ(COUNT_RESULT, count);
52     GTEST_LOG_(INFO) << "RegisterRunningLockTimer001:  end";
53 }
54 
55 
56 /**
57  * @tc.name: RegisterRunningLockTimer
58  * @tc.desc: Test RegisterRunningLockTimer
59  * @tc.type: FUNC
60  * @tc.require: issueI9C4GG
61  */
62 HWTEST_F(RunningLockTimerHandlerTest, RegisterRunningLockTimer002, TestSize.Level0)
63 {
64     GTEST_LOG_(INFO) << "RegisterRunningLockTimer002: start";
65     std::shared_ptr<RunningLock> runningLock1 =
66         std::make_shared<RunningLock>(nullptr, "runninglock_Timer_test1",
67             RunningLockType::RUNNINGLOCK_SCREEN);
68     std::shared_ptr<RunningLock> runningLock2 =
69         std::make_shared<RunningLock>(nullptr, "runninglock_Timer_test2",
70             RunningLockType::RUNNINGLOCK_BACKGROUND);
71     ASSERT_TRUE(runningLock1 != nullptr);
72     runningLock1->Init();
73     ASSERT_TRUE(runningLock2 != nullptr);
74     runningLock2->Init();
75     runningLock1->Lock();
76     runningLock2->Lock();
77     EXPECT_TRUE(!runningLock1->IsUsed());
78     EXPECT_TRUE(!runningLock2->IsUsed());
79     runningLock1->UnLock();
80     runningLock2->UnLock();
81     EXPECT_TRUE(!runningLock1->IsUsed());
82     EXPECT_TRUE(!runningLock2->IsUsed());
83     runningLock1->UnLock();
84     runningLock2->UnLock();
85     GTEST_LOG_(INFO) << "RegisterRunningLockTimer002:  end";
86 }
87 
88 /**
89  * @tc.name: RegisterRunningLockTimer
90  * @tc.desc: Test RegisterRunningLockTimer
91  * @tc.type: FUNC
92  * @tc.require: issueI9C4GG
93  */
94 HWTEST_F(RunningLockTimerHandlerTest, RegisterRunningLockTimer003, TestSize.Level0)
95 {
96     GTEST_LOG_(INFO) << "RegisterRunningLockTimer003: start";
97     std::shared_ptr<RunningLock> runningLock1 =
98         std::make_shared<RunningLock>(nullptr, "runninglock_Timer_test3",
99         RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
100     std::shared_ptr<RunningLock> runningLock2 =
101         std::make_shared<RunningLock>(nullptr, "runninglock_Timer_test4",
102         RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
103     ASSERT_TRUE(runningLock1 != nullptr);
104     runningLock1->Init();
105     ASSERT_TRUE(runningLock2 != nullptr);
106     runningLock2->Init();
107     runningLock1->Lock(TIMEOUT_MS);
108     runningLock2->Lock(0);
109     EXPECT_TRUE(!runningLock1->IsUsed());
110     EXPECT_TRUE(!runningLock2->IsUsed());
111     sleep(SLEEP_WAIT_TIME_S);
112     EXPECT_TRUE(!runningLock1->IsUsed());
113     EXPECT_TRUE(!runningLock2->IsUsed());
114     runningLock1->UnLock();
115     runningLock2->UnLock();
116     GTEST_LOG_(INFO) << "RegisterRunningLockTimer003:  end";
117 }
118 
119 /**
120  * @tc.name: UnregisterRunningLockTimer
121  * @tc.desc: Test UnregisterRunningLockTimer
122  * @tc.type: FUNC
123  * @tc.require: issueI9C4GG
124  */
125 HWTEST_F(RunningLockTimerHandlerTest, UnregisterRunningLockTimer001, TestSize.Level0)
126 {
127     GTEST_LOG_(INFO) << "UnregisterRunningLockTimer001: start";
128     sptr<IRemoteObject> token = new RunningLockTokenStub();
129     int count = 1;
__anon9f4458ee0302() 130     std::function<void()> task = [&]() {
131         count++;
132     };
133     bool ret = RunningLockTimerHandler::GetInstance().RegisterRunningLockTimer(token,
134         task, TIMEOUT_MS);
135     EXPECT_TRUE(ret);
136     ret = RunningLockTimerHandler::GetInstance().UnregisterRunningLockTimer(token);
137     EXPECT_TRUE(ret);
138     EXPECT_NE(COUNT_RESULT, count);
139     GTEST_LOG_(INFO) << "UnregisterRunningLockTimer001:  end";
140 }
141 
142 /**
143  * @tc.name: UnregisterRunningLockTimer
144  * @tc.desc: Test UnregisterRunningLockTimer
145  * @tc.type: FUNC
146  * @tc.require: issueI9C4GG
147  */
148 HWTEST_F(RunningLockTimerHandlerTest, UnregisterRunningLockTimer002, TestSize.Level0)
149 {
150     GTEST_LOG_(INFO) << "UnregisterRunningLockTimer002: start";
151     std::shared_ptr<RunningLockTimerHandler> runningLockTimerHandler =
152         std::make_shared<RunningLockTimerHandler>();
153     ASSERT_TRUE(runningLockTimerHandler != nullptr);
154     sptr<IRemoteObject> token1 = new RunningLockTokenStub();
155     sptr<IRemoteObject> token2 = new RunningLockTokenStub();
156     int count1 = 1;
__anon9f4458ee0402() 157     std::function<void()> task1 = [&]() {
158         count1++;
159     };
160     int count2 = 1;
__anon9f4458ee0502() 161     std::function<void()> task2 = [&]() {
162         count2++;
163     };
164     runningLockTimerHandler->RegisterRunningLockTimer(token1, task1, TIMEOUT_MS);
165     runningLockTimerHandler->RegisterRunningLockTimer(token2, task2, TIMEOUT_MS);
166     runningLockTimerHandler.reset();
167     EXPECT_NE(COUNT_RESULT, count1);
168     EXPECT_NE(COUNT_RESULT, count2);
169     GTEST_LOG_(INFO) << "UnregisterRunningLockTimer002:  end";
170 }
171 } // namespace UnitTest
172 } // namespace PowerMgr
173 } // namespace OHOS0.
174