1 /* 2 * Copyright (c) 2021-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_scenario_test.h" 17 18 #include <ipc_skeleton.h> 19 20 #include "actions/irunning_lock_action.h" 21 #include "power_log.h" 22 #include "power_mgr_service.h" 23 #include "running_lock_mgr.h" 24 25 using namespace testing::ext; 26 using namespace OHOS::PowerMgr; 27 using namespace OHOS; 28 using namespace std; 29 30 namespace { 31 constexpr int32_t US_PER_MS = 1000; 32 constexpr int32_t app0Uid = 8; 33 constexpr int32_t app1Uid = 9; 34 } 35 36 namespace { 37 /** 38 * @tc.name: RunningLockScenarioTest001 39 * @tc.desc: Test runninglock single app scenario 40 * @tc.type: FUNC 41 * @tc.require 42 */ 43 HWTEST_F (RunningLockScenarioTest, RunningLockScenarioTest001, TestSize.Level1) 44 { 45 POWER_HILOGI(LABEL_TEST, "RunningLockScenarioTest001 function start!"); 46 auto& powerMgrClient = PowerMgrClient::GetInstance(); 47 48 pid_t curUid = getuid(); 49 pid_t curPid = getpid(); 50 51 std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock( 52 "background.test019", RunningLockType::RUNNINGLOCK_BACKGROUND); 53 ASSERT_NE(runningLock, nullptr); 54 runningLock->Lock(); 55 56 std::vector<int32_t> workSource { app0Uid }; 57 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource) == 0); 58 EXPECT_TRUE(runningLock->IsUsed()); 59 60 runningLock->UnLock(); 61 EXPECT_FALSE(runningLock->IsUsed()); 62 POWER_HILOGI(LABEL_TEST, "RunningLockScenarioTest001 function end!"); 63 } 64 65 /** 66 * @tc.name: RunningLockScenarioTest002 67 * @tc.desc: Test runninglock single app scenario with proxy 68 * @tc.type: FUNC 69 * @tc.require 70 */ 71 HWTEST_F (RunningLockScenarioTest, RunningLockScenarioTest002, TestSize.Level1) 72 { 73 POWER_HILOGI(LABEL_TEST, "RunningLockScenarioTest002 function start!"); 74 auto& powerMgrClient = PowerMgrClient::GetInstance(); 75 76 pid_t curUid = getuid(); 77 pid_t curPid = getpid(); 78 79 std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock( 80 "background.test020", RunningLockType::RUNNINGLOCK_BACKGROUND); 81 ASSERT_NE(runningLock, nullptr); 82 runningLock->Lock(); 83 // open app 84 std::vector<int32_t> workSource1 { app0Uid }; 85 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource1) == 0); 86 usleep(100*1000); 87 EXPECT_TRUE(runningLock->IsUsed()); 88 // freeze app 89 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, app0Uid)); 90 EXPECT_FALSE(runningLock->IsUsed()); 91 // thaw app 92 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, app0Uid)); 93 EXPECT_TRUE(runningLock->IsUsed()); 94 // close app 95 std::vector<int32_t> workSource0 {}; 96 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource0) == 0); 97 usleep(100*1000); 98 EXPECT_TRUE(runningLock->IsUsed()); 99 100 runningLock->UnLock(); 101 EXPECT_FALSE(runningLock->IsUsed()); 102 POWER_HILOGI(LABEL_TEST, "RunningLockScenarioTest002 function end!"); 103 } 104 105 /** 106 * @tc.name: RunningLockScenarioTest003 107 * @tc.desc: Test runninglock multi apps scenario with proxy 108 * @tc.type: FUNC 109 * @tc.require 110 */ 111 HWTEST_F (RunningLockScenarioTest, RunningLockScenarioTest003, TestSize.Level1) 112 { 113 POWER_HILOGI(LABEL_TEST, "RunningLockScenarioTest003 function start!"); 114 auto& powerMgrClient = PowerMgrClient::GetInstance(); 115 116 pid_t curUid = getuid(); 117 pid_t curPid = getpid(); 118 119 std::vector<int32_t> workSource00 {}; 120 std::vector<int32_t> workSource10 { app0Uid }; 121 std::vector<int32_t> workSource01 { app1Uid }; 122 std::vector<int32_t> workSource11 { app0Uid, app1Uid }; 123 124 std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock( 125 "background.test021", RunningLockType::RUNNINGLOCK_BACKGROUND); 126 ASSERT_NE(runningLock, nullptr); 127 runningLock->Lock(); 128 // open app0 129 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource10) == 0); 130 EXPECT_TRUE(runningLock->IsUsed()); 131 // open app1 132 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource11) == 0); 133 EXPECT_TRUE(runningLock->IsUsed()); 134 // freeze app0 135 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, app0Uid)); 136 EXPECT_TRUE(runningLock->IsUsed()); 137 // freeze app1 138 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, app1Uid)); 139 EXPECT_FALSE(runningLock->IsUsed()); 140 // thaw app0 141 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, app0Uid)); 142 EXPECT_TRUE(runningLock->IsUsed()); 143 // thaw app1 144 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, app1Uid)); 145 EXPECT_TRUE(runningLock->IsUsed()); 146 // close app0 147 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource01) == 0); 148 EXPECT_TRUE(runningLock->IsUsed()); 149 // close app1 150 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource00) == 0); 151 EXPECT_TRUE(runningLock->IsUsed()); 152 153 runningLock->UnLock(); 154 EXPECT_FALSE(runningLock->IsUsed()); 155 POWER_HILOGI(LABEL_TEST, "RunningLockScenarioTest003 function end!"); 156 } 157 158 /** 159 * @tc.name: RunningLockScenarioTest004 160 * @tc.desc: Test runninglock multi apps scenario with proxy 161 * @tc.type: FUNC 162 * @tc.require 163 */ 164 HWTEST_F (RunningLockScenarioTest, RunningLockScenarioTest004, TestSize.Level1) 165 { 166 POWER_HILOGI(LABEL_TEST, "RunningLockScenarioTest004 function start!"); 167 auto& powerMgrClient = PowerMgrClient::GetInstance(); 168 169 pid_t curUid = getuid(); 170 pid_t curPid = getpid(); 171 172 std::vector<int32_t> workSource00 {}; 173 std::vector<int32_t> workSource10 { app0Uid }; 174 std::vector<int32_t> workSource11 { app0Uid, app1Uid }; 175 176 std::shared_ptr<RunningLock> runningLock = powerMgrClient.CreateRunningLock( 177 "background.test004", RunningLockType::RUNNINGLOCK_BACKGROUND); 178 ASSERT_NE(runningLock, nullptr); 179 runningLock->Lock(); 180 // open app0 181 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource10) == 0); 182 usleep(100*1000); 183 EXPECT_TRUE(runningLock->IsUsed()); 184 // freeze app0 185 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(true, curPid, app0Uid)); 186 EXPECT_FALSE(runningLock->IsUsed()); 187 // open app1 188 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource11) == 0); 189 usleep(100*1000); 190 EXPECT_TRUE(runningLock->IsUsed()); 191 // close app1 192 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource10) == 0); 193 usleep(100*1000); 194 EXPECT_FALSE(runningLock->IsUsed()); 195 // thaw app0 196 EXPECT_TRUE(powerMgrClient.ProxyRunningLock(false, curPid, app0Uid)); 197 EXPECT_TRUE(runningLock->IsUsed()); 198 // close app0 199 EXPECT_TRUE(runningLock->UpdateWorkSource(workSource00) == 0); 200 usleep(100*1000); 201 EXPECT_TRUE(runningLock->IsUsed()); 202 203 runningLock->UnLock(); 204 EXPECT_FALSE(runningLock->IsUsed()); 205 POWER_HILOGI(LABEL_TEST, "RunningLockScenarioTest004 function end!"); 206 } 207 } // namespace