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