1 /*
2 * Copyright (c) 2023-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 #include <functional>
16 #include <chrono>
17 #include <thread>
18 #include <message_parcel.h>
19 #include <climits>
20 #include <dlfcn.h>
21
22 #include "gtest/gtest.h"
23 #include "gtest/hwext/gtest-multithread.h"
24 #include "singleton.h"
25 #include "system_ability_definition.h"
26
27 #include "device_standby_switch.h"
28 #include "time_provider.h"
29 #include "common_event_support.h"
30 #include "common_event_observer.h"
31
32 #include "state_manager_adapter.h"
33 #include "constraint_manager_adapter.h"
34 #include "listener_manager_adapter.h"
35 #include "strategy_manager_adapter.h"
36
37 #include "standby_state.h"
38 #include "allow_type.h"
39 #include "standby_ipc_interface_code.h"
40 #include "standby_service_client.h"
41 #include "standby_service.h"
42 #include "ability_manager_helper.h"
43 #include "standby_service_impl.h"
44 #include "standby_state_subscriber.h"
45 #include "standby_state_subscriber.h"
46 #include "standby_service_subscriber_stub.h"
47 #include "bundle_manager_helper.h"
48 #include "standby_config_manager.h"
49 #include "app_state_observer.h"
50 #include "app_mgr_constants.h"
51 #include "mock_common_event.h"
52 #include "ibundle_manager_helper.h"
53 #include "mock_ipc.h"
54 using namespace testing::ext;
55 using namespace testing::mt;
56
57 namespace OHOS {
58 namespace DevStandbyMgr {
59 namespace {
60 const uint32_t ALL_DEPENDS_READY = 127;
61 constexpr int32_t DEFAULT_UID = 0;
62 constexpr int32_t SAMPLE_APP_UID = 10001;
63 const std::string SAMPLE_BUNDLE_NAME = "name";
64 const std::string DEFAULT_BUNDLENAME = "test";
65 const std::string DEFAULT_KEY = "0_test";
66 const vector<std::string> COMMON_EVENT_LIST = {
67 EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED,
68 EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED,
69 EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED,
70 EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED,
71 EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED,
72 EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED,
73 EventFwk::CommonEventSupport::COMMON_EVENT_NITZ_TIMEZONE_CHANGED,
74 EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED,
75 EventFwk::CommonEventSupport::COMMON_EVENT_NITZ_TIME_CHANGED
76 };
77 constexpr int32_t SLEEP_TIMEOUT = 500;
78 constexpr int32_t WAKEUP_EXACT_TIMER_TYPE = 6;
79 }
80
81 class StandbyServiceUnitTest : public testing::Test {
82 public:
83 static void SetUpTestCase();
84 static void TearDownTestCase();
SetUp()85 void SetUp() override {}
86 void TearDown() override;
87
SleepForFC()88 inline static void SleepForFC()
89 {
90 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIMEOUT));
91 }
92 };
93
SetUpTestCase()94 void StandbyServiceUnitTest::SetUpTestCase()
95 {
96 StandbyServiceImpl::GetInstance()->Init();
97
98 StandbyServiceImpl::GetInstance()->constraintManager_ = std::make_shared<ConstraintManagerAdapter>();
99 StandbyServiceImpl::GetInstance()->listenerManager_ = std::make_shared<ListenerManagerAdapter>();
100 StandbyServiceImpl::GetInstance()->strategyManager_ = std::make_shared<StrategyManagerAdapter>();
101 StandbyServiceImpl::GetInstance()->standbyStateManager_ = std::make_shared<StateManagerAdapter>();
102 StandbyServiceImpl::GetInstance()->InitReadyState();
103 SleepForFC();
104 }
105
TearDown()106 void StandbyServiceUnitTest::TearDown()
107 {
108 SleepForFC();
109 StandbyServiceImpl::GetInstance()->allowInfoMap_.clear();
110 }
111
TearDownTestCase()112 void StandbyServiceUnitTest::TearDownTestCase()
113 {
114 SleepForFC();
115 if (StandbyServiceImpl::GetInstance()->handler_) {
116 StandbyServiceImpl::GetInstance()->handler_->RemoveAllEvents();
117 auto runner = StandbyServiceImpl::GetInstance()->handler_->GetEventRunner();
118 if (runner) {
119 runner->Stop();
120 runner = nullptr;
121 }
122 StandbyServiceImpl::GetInstance()->handler_ = nullptr;
123 }
124 StandbyServiceImpl::GetInstance()->UnInit();
125 }
126
127 /**
128 * @tc.name: StandbyServiceUnitTest_001
129 * @tc.desc: test OnStart of StandbyService.
130 * @tc.type: FUNC
131 * @tc.require:
132 */
133 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_001, TestSize.Level1)
134 {
135 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
136 StandbyService::GetInstance()->OnStart();
137 EXPECT_NE(StandbyServiceImpl::GetInstance()->handler_, nullptr);
138 StandbyServiceImpl::GetInstance()->InitReadyState();
139 SleepForFC();
140 EXPECT_EQ(StandbyService::GetInstance()->state_, ServiceRunningState::STATE_RUNNING);
141 }
142
143 /**
144 * @tc.name: StandbyServiceUnitTest_002
145 * @tc.desc: test OnAddSystemAbility of StandbyService.
146 * @tc.type: FUNC
147 * @tc.require:
148 */
149 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_002, TestSize.Level1)
150 {
151 StandbyService::GetInstance()->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, "");
152 StandbyService::GetInstance()->OnAddSystemAbility(TIME_SERVICE_ID, "");
153 StandbyService::GetInstance()->OnAddSystemAbility(ABILITY_MGR_SERVICE_ID, "");
154 StandbyService::GetInstance()->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, "");
155 StandbyService::GetInstance()->OnAddSystemAbility(POWER_MANAGER_SERVICE_ID, "");
156 StandbyService::GetInstance()->OnAddSystemAbility(APP_MGR_SERVICE_ID, "");
157 StandbyService::GetInstance()->OnAddSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, "");
158 StandbyService::GetInstance()->OnAddSystemAbility(POWER_MANAGER_SERVICE_ID + 1, "");
159 StandbyService::GetInstance()->OnRemoveSystemAbility(POWER_MANAGER_SERVICE_ID + 1, "");
160 SleepForFC();
161 StandbyService::GetInstance()->OnAddSystemAbility(POWER_MANAGER_SERVICE_ID + 1, "");
162 StandbyService::GetInstance()->OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID, "");
163 StandbyService::GetInstance()->OnRemoveSystemAbility(APP_MGR_SERVICE_ID, "");
164 StandbyService::GetInstance()->OnRemoveSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, "");
165 StandbyService::GetInstance()->OnRemoveSystemAbility(TIME_SERVICE_ID, "");
166 StandbyService::GetInstance()->OnRemoveSystemAbility(ABILITY_MGR_SERVICE_ID, "");
167 StandbyService::GetInstance()->OnRemoveSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, "");
168 StandbyService::GetInstance()->OnRemoveSystemAbility(POWER_MANAGER_SERVICE_ID, "");
169 StandbyService::GetInstance()->OnRemoveSystemAbility(POWER_MANAGER_SERVICE_ID + 1, "");
170 StandbyService::GetInstance()->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, "");
171 StandbyService::GetInstance()->OnAddSystemAbility(TIME_SERVICE_ID, "");
172 StandbyService::GetInstance()->OnAddSystemAbility(ABILITY_MGR_SERVICE_ID, "");
173 StandbyService::GetInstance()->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, "");
174 StandbyService::GetInstance()->OnAddSystemAbility(POWER_MANAGER_SERVICE_ID, "");
175 StandbyService::GetInstance()->OnAddSystemAbility(APP_MGR_SERVICE_ID, "");
176 StandbyService::GetInstance()->OnAddSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, "");
177 EXPECT_EQ(StandbyServiceImpl::GetInstance()->dependsReady_, ALL_DEPENDS_READY);
178 StandbyServiceImpl::GetInstance()->InitReadyState();
179 }
180
181 /**
182 * @tc.name: StandbyServiceUnitTest_003
183 * @tc.desc: test state not start of StandbyService.
184 * @tc.type: FUNC
185 * @tc.require:
186 */
187 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_003, TestSize.Level1)
188 {
189 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
190 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
191 std::string subscriberName = "test_subscriberName";
192 std::string moduleName = "test_moduleName";
193 StandbyService::GetInstance()->SubscribeStandbyCallback(subscriber, subscriberName, moduleName);
194 StandbyService::GetInstance()->UnsubscribeStandbyCallback(subscriber);
195
196 ResourceRequest resourceRequest;
197 EXPECT_NE(StandbyService::GetInstance()->ApplyAllowResource(resourceRequest), ERR_OK);
198 EXPECT_NE(StandbyService::GetInstance()->UnapplyAllowResource(resourceRequest), ERR_OK);
199
200 std::vector<AllowInfo> allowInfoList;
201 EXPECT_NE(StandbyService::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0), ERR_OK);
202 bool isStandby {false};
203 EXPECT_NE(StandbyService::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
204 StandbyService::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
205 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
206 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
207 StandbyService::GetInstance()->SubscribeStandbyCallback(subscriber, subscriberName, moduleName);
208 StandbyService::GetInstance()->UnsubscribeStandbyCallback(subscriber);
209 StandbyService::GetInstance()->ApplyAllowResource(resourceRequest);
210 StandbyService::GetInstance()->UnapplyAllowResource(resourceRequest);
211 EXPECT_EQ(StandbyService::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0), ERR_OK);
212 EXPECT_EQ(StandbyService::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
213 StandbyService::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
214 }
215
216 /**
217 * @tc.name: StandbyServiceUnitTest_004
218 * @tc.desc: test unready not start of StandbyServiceImpl.
219 * @tc.type: FUNC
220 * @tc.require:
221 */
222 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_004, TestSize.Level1)
223 {
224 StandbyServiceImpl::GetInstance()->isServiceReady_ = false;
225 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
226 SleepForFC();
227 MockIpc::MockStartTimer(false);
228 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
229 SleepForFC();
230 MockIpc::MockStartTimer(true);
231 StandbyServiceImpl::GetInstance()->RemoveAppAllowRecord(DEFAULT_UID, DEFAULT_BUNDLENAME, true);
232 ResourceRequest resourceRequest;
233 StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
234 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
235 std::vector<AllowInfo> allowInfoList;
236 StandbyServiceImpl::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0);
237 bool isStandby {false};
238 EXPECT_NE(StandbyServiceImpl::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
239 std::vector<std::string> argsInStr {};
240 std::string result;
241 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
242 StandbyServiceImpl::GetInstance()->OnProcessStatusChanged(-1, -1, "", true);
243 StandbyServiceImpl::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
244 StandbyServiceImpl::GetInstance()->isServiceReady_ = true;
245 StandbyServiceImpl::GetInstance()->OnProcessStatusChanged(-1, -1, "", true);
246 StandbyServiceImpl::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
247 }
248
249 /**
250 * @tc.name: StandbyServiceUnitTest_005
251 * @tc.desc: test shelldump not start of StandbyService.
252 * @tc.type: FUNC
253 * @tc.require:
254 */
255 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_005, TestSize.Level1)
256 {
257 std::vector<std::u16string> args {};
258 std::vector<std::string> argsInStr {};
259 std::string result;
260 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
261 StandbyServiceImpl::GetInstance()->ShellDumpInner({}, result);
262 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-h"}, result);
263 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-v"}, result);
264 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D"}, result);
265 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D", "--config"}, result);
266 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D", "--conf"}, result);
267 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-E"}, result);
268 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-E", "working", "false"}, result);
269 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-E", "working", "true"}, result);
270 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A"}, result);
271 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--apply"}, result);
272 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--unapply"}, result);
273 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get"}, result);
274 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--apply", "0", "test", "127", "100", "0"}, result);
275 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--apply", "0", "test", "127", "100", "1"}, result);
276 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--unapply", "0", "test", "127", "100", "0"}, result);
277 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--unapply", "0", "test", "127", "100", "1"}, result);
278 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "true"}, result);
279 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "false"}, result);
280 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S"}, result);
281 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S", "--repeat"}, result);
282 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S", "--motion"}, result);
283 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S", "--default"}, result);
284 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-O"}, result);
285 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T"}, result);
286 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "on"}, result);
287 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "off"}, result);
288 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "debug", "on"}, result);
289 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-C", "dark_timeout", "60"}, result);
290 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "on"}, result);
291 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "off"}, result);
292 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "debug", "off"}, result);
293 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-C"}, result);
294 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-C", "dark_timeout", "60"}, result);
295 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D"}, result);
296 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "false", "true"}, result);
297
298 auto allowRecord = std::make_shared<AllowRecord>(0, 0, "name", AllowType::NETWORK);
299 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
300 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
301 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D"}, result);
302 SleepForFC();
303 EXPECT_NE(StandbyService::GetInstance()->Dump(-1, args), ERR_OK);
304 }
305
306 /**
307 * @tc.name: StandbyServiceUnitTest_006
308 * @tc.desc: test init of StandbyService.
309 * @tc.type: FUNC
310 * @tc.require:
311 */
312 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_006, TestSize.Level1)
313 {
314 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("test_standby.z.so"), ERR_OK);
315 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("libstandby_utils_policy.z.so"), ERR_OK);
316 }
317
318 /**
319 * @tc.name: StandbyServiceUnitTest_007
320 * @tc.desc: test init of StandbyService.
321 * @tc.type: FUNC
322 * @tc.require:
323 */
324 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_007, TestSize.Level1)
325 {
326 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("test_standby.z.so"), ERR_OK);
327 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("libstandby_utils_policy.z.so"), ERR_OK);
328 }
329
330 /**
331 * @tc.name: StandbyServiceUnitTest_008
332 * @tc.desc: test ParsePersistentData of StandbyService.
333 * @tc.type: FUNC
334 * @tc.require:
335 */
336 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_008, TestSize.Level1)
337 {
338 StandbyServiceImpl::GetInstance()->ParsePersistentData();
339 auto allowRecord = std::make_shared<AllowRecord>(0, 0, "name", AllowType::NETWORK);
340 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
341 allowRecord = std::make_shared<AllowRecord>(-1, -1, "test", AllowType::NETWORK);
342 allowRecord->allowTimeList_.emplace_back(AllowTime{-1, INT64_MAX, "test"});
343 allowRecord = std::make_shared<AllowRecord>(-1, -1, "test", AllowType::NETWORK);
344 allowRecord->allowTimeList_.emplace_back(AllowTime{-1, INT64_MAX, "test"});
345 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
346 StandbyServiceImpl::GetInstance()->DumpPersistantData();
347 StandbyServiceImpl::GetInstance()->ParsePersistentData();
348 StandbyServiceImpl::GetInstance()->RecoverTimeLimitedTask();
349 allowRecord->allowTimeList_.clear();
350 StandbyServiceImpl::GetInstance()->DumpPersistantData();
351 StandbyServiceImpl::GetInstance()->ParsePersistentData();
352 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->allowInfoMap_.empty());
353 IBundleManagerHelper::MockGetAllRunningProcesses(false);
354 StandbyServiceImpl::GetInstance()->ParsePersistentData();
355 IBundleManagerHelper::MockGetAllRunningProcesses(true);
356
357 auto emptyRecord = std::make_shared<AllowRecord>(0, 0, "name", 0);
358 emptyRecord->allowTimeList_.emplace_back(AllowTime{0, 0, "reason"});
359 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, emptyRecord);
360 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(0, "test", 0, true);
361 emptyRecord->allowTimeList_.emplace_back(AllowTime{1, 0, "reason"});
362 emptyRecord->allowTimeList_.emplace_back(AllowTime{2, 0, "reason"});
363 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(0, "test", AllowType::NETWORK, true);
364 }
365
366 /**
367 * @tc.name: StandbyServiceUnitTest_009
368 * @tc.desc: test ResetTimeObserver of StandbyService.
369 * @tc.type: FUNC
370 * @tc.require:
371 */
372 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_009, TestSize.Level1)
373 {
374 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
375 MockIpc::MockStartTimer(false);
376 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
377 MockIpc::MockStartTimer(true);
378 EXPECT_NE(StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_, 0);
379 }
380
381 /**
382 * @tc.name: StandbyServiceUnitTest_010
383 * @tc.desc: test UnInit of StandbyService.
384 * @tc.type: FUNC
385 * @tc.require:
386 */
387 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_010, TestSize.Level1)
388 {
389 StandbyServiceImpl::GetInstance()->registerPlugin_ = nullptr;
390 StandbyServiceImpl::GetInstance()->UninitReadyState();
391 StandbyServiceImpl::GetInstance()->UnInit();
392 StandbyServiceImpl::GetInstance()->RegisterPlugin(StandbyConfigManager::GetInstance()
393 ->GetPluginName());
394 StandbyServiceImpl::GetInstance()->InitReadyState();
395 SleepForFC();
396 EXPECT_NE(StandbyServiceImpl::GetInstance()->registerPlugin_, nullptr);
397 }
398
399 /**
400 * @tc.name: StandbyServiceUnitTest_011
401 * @tc.desc: test RemoveAppAllowRecord of StandbyService.
402 * @tc.type: FUNC
403 * @tc.require:
404 */
405 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_011, TestSize.Level1)
406 {
407 StandbyServiceImpl::GetInstance()->RemoveAppAllowRecord(DEFAULT_UID, DEFAULT_BUNDLENAME, true);
408 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
409 }
410
411 /**
412 * @tc.name: StandbyServiceUnitTest_012
413 * @tc.desc: test CheckCallerPermission of StandbyService.
414 * @tc.type: FUNC
415 * @tc.require:
416 */
417 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_012, TestSize.Level1)
418 {
419 StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_NATIVE_API);
420 EXPECT_EQ(StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_APP_API), ERR_OK);
421 MockIpc::MockGetTokenTypeFlag(false);
422 StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_NATIVE_API);
423 StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_APP_API);
424 MockIpc::MockGetTokenTypeFlag(true);
425 Security::AccessToken::AccessTokenID tokenId {};
426 StandbyServiceImpl::GetInstance()->IsSystemAppWithPermission(-1, tokenId, ReasonCodeEnum::REASON_APP_API);
427 StandbyServiceImpl::GetInstance()->IsSystemAppWithPermission(-1, tokenId, ReasonCodeEnum::REASON_NATIVE_API);
428 }
429
430 /**
431 * @tc.name: StandbyServiceUnitTest_013
432 * @tc.desc: test ApplyAllowResource of StandbyService.
433 * @tc.type: FUNC
434 * @tc.require:
435 */
436 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_013, TestSize.Level1)
437 {
438 ResourceRequest resourceRequest;
439 StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
440 SleepForFC();
441 StandbyServiceImpl::GetInstance()->ApplyAllowResInner(resourceRequest, -1);
442 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
443 }
444
445 /**
446 * @tc.name: StandbyServiceUnitTest_014
447 * @tc.desc: test UpdateRecord of StandbyService.
448 * @tc.type: FUNC
449 * @tc.require:
450 */
451 HWTEST_F(StandbyServiceUnitTest, UpdateRecord_014, TestSize.Level1)
452 {
453 std::shared_ptr<AllowRecord> allowRecord = std::make_shared<AllowRecord>();
454 ResourceRequest resourceRequest;
455 StandbyServiceImpl::GetInstance()->UpdateRecord(allowRecord, resourceRequest);
456 SleepForFC();
457 StandbyServiceImpl::GetInstance()->UpdateRecord(allowRecord, resourceRequest);
458 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
459 }
460
461 /**
462 * @tc.name: StandbyServiceUnitTest_015
463 * @tc.desc: test UpdateRecord of StandbyService.
464 * @tc.type: FUNC
465 * @tc.require:
466 */
467 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_015, TestSize.Level1)
468 {
469 std::shared_ptr<AllowRecord> allowRecord = std::make_shared<AllowRecord>();
470 std::shared_ptr<ResourceRequest> resourceRequest = std::make_shared<ResourceRequest>(MAX_ALLOW_TYPE_NUMBER,
471 DEFAULT_UID, DEFAULT_BUNDLENAME, 10, "reason", ReasonCodeEnum::REASON_APP_API);
472 StandbyServiceImpl::GetInstance()->UpdateRecord(allowRecord, *resourceRequest);
473 SleepForFC();
474 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
475 }
476
477 /**
478 * @tc.name: StandbyServiceUnitTest_016
479 * @tc.desc: test DayNightSwitchCallback of StandbyService.
480 * @tc.type: FUNC
481 * @tc.require:
482 */
483 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_016, TestSize.Level1)
484 {
485 StandbyServiceImpl::GetInstance()->standbyStateManager_->TransitToStateInner(StandbyState::WORKING);
486 SleepForFC();
487 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
488 SleepForFC();
489 StandbyServiceImpl::GetInstance()->standbyStateManager_->TransitToStateInner(StandbyState::SLEEP);
490 SleepForFC();
491 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
492 SleepForFC();
493 MockIpc::MockStartTimer(false);
494 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
495 SleepForFC();
496 MockIpc::MockStartTimer(true);
497 StandbyServiceImpl::GetInstance()->UnregisterTimeObserver();
498 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
499 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
500 SleepForFC();
501 EXPECT_NE(StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_, 0);
502 MockIpc::MockStartTimer(false);
503 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
504 MockIpc::MockStartTimer(true);
505 SleepForFC();
506 }
507
508 /**
509 * @tc.name: StandbyServiceUnitTest_017
510 * @tc.desc: test DayNightSwitchCallback of StandbyService.
511 * @tc.type: FUNC
512 * @tc.require:
513 */
514 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_017, TestSize.Level1)
515 {
516 auto allowRecord = std::make_shared<AllowRecord>(DEFAULT_UID, 0, DEFAULT_BUNDLENAME, AllowType::NETWORK);
517 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
518 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
519
520 std::vector<AllowInfo> allowInfoList;
521 StandbyServiceImpl::GetInstance()->GetAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
522 ReasonCodeEnum::REASON_APP_API);
523 StandbyServiceImpl::GetInstance()->GetAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
524 ReasonCodeEnum::REASON_NATIVE_API);
525 std::string result {""};
526 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "true"}, result);
527 StandbyServiceImpl::GetInstance()->GetAllowListInner(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
528 ReasonCodeEnum::REASON_APP_API);
529 StandbyServiceImpl::GetInstance()->GetAllowListInner(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
530 ReasonCodeEnum::REASON_NATIVE_API);
531 StandbyServiceImpl::GetInstance()->GetAllowListInner(0, allowInfoList,
532 ReasonCodeEnum::REASON_NATIVE_API);
533 StandbyServiceImpl::GetInstance()->GetTemporaryAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
534 ReasonCodeEnum::REASON_APP_API);
535 StandbyServiceImpl::GetInstance()->GetTemporaryAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
536 ReasonCodeEnum::REASON_NATIVE_API);
537 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
538 true, true);
539 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
540 true, false);
541 ResourceRequest resourceRequest;
542 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
543 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
544 SleepForFC();
545 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(DEFAULT_UID, DEFAULT_BUNDLENAME, 1, false);
546 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(DEFAULT_UID, DEFAULT_BUNDLENAME, 1, true);
547 allowRecord = std::make_shared<AllowRecord>(0, 0, "name", MAX_ALLOW_TYPE_NUMBER);
548 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
549 allowRecord->allowTimeList_.emplace_back(AllowTime{1, INT64_MAX, "reason"});
550 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
551 StandbyServiceImpl::GetInstance()->GetTemporaryAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
552 ReasonCodeEnum::REASON_NATIVE_API);
553 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
554 true, true);
555 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
556 false, true);
557 StandbyServiceImpl::GetInstance()->allowInfoMap_.clear();
558 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
559 }
560
561 /**
562 * @tc.name: StandbyServiceUnitTest_018
563 * @tc.desc: test AddSubscriber of StandbyService.
564 * @tc.type: FUNC
565 * @tc.require:
566 */
567 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_018, TestSize.Level1)
568 {
569 std::vector<std::string> argsInStr {};
570 std::string result {};
571 StandbyStateSubscriber::GetInstance()->ShellDump(argsInStr, result);
572 StandbyStateSubscriber::GetInstance()->NotifyIdleModeByCallback(false, false);
573 StandbyStateSubscriber::GetInstance()->ReportAllowListChanged(DEFAULT_UID, DEFAULT_BUNDLENAME,
574 AllowType::NETWORK, true);
575 sptr<IStandbyServiceSubscriber> nullSubscriber = nullptr;
576 EXPECT_NE(StandbyStateSubscriber::GetInstance()->AddSubscriber(nullSubscriber), ERR_OK);
577 EXPECT_NE(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(nullSubscriber), ERR_OK);
578 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
579 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
580 StandbyStateSubscriber::GetInstance()->ShellDump(argsInStr, result);
581 EXPECT_NE(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
582 StandbyStateSubscriber::GetInstance()->NotifyIdleModeByCallback(false, false);
583 StandbyStateSubscriber::GetInstance()->ReportAllowListChanged(DEFAULT_UID, DEFAULT_BUNDLENAME,
584 AllowType::NETWORK, true);
585 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
586 StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber);
587 StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber);
588 auto remote = subscriber->AsObject();
589 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
590 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
591 StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber);
592
593 StandbyStateSubscriber::GetInstance()->deathRecipient_ = nullptr;
594 EXPECT_NE(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
595 EXPECT_NE(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
596 StandbyStateSubscriber::GetInstance()->deathRecipient_ = new (std::nothrow) SubscriberDeathRecipient();
597
598 remote = nullptr;
599 StandbyStateSubscriber::GetInstance()->deathRecipient_->OnRemoteDied(remote);
600 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
601 sptr<IRemoteObject> proxy {nullptr};
602 remote = proxy;
603 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
604 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
605 }
606
607 /**
608 * @tc.name: StandbyServiceUnitTest_019
609 * @tc.desc: test AddSubscriber of StandbyService.
610 * @tc.type: FUNC
611 * @tc.require:
612 */
613 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_019, TestSize.Level1)
614 {
615 auto allowRecord = std::make_shared<AllowRecord>(DEFAULT_UID, 0, DEFAULT_BUNDLENAME, AllowType::NETWORK);
616 auto value = allowRecord->ParseToJson();
617 allowRecord->ParseFromJson(value);
618 allowRecord->allowTimeList_.emplace_back(AllowTime{0, 0, "reason"});
619 value = allowRecord->ParseToJson();
620 nlohmann::json emptyValue {};
621 allowRecord->ParseFromJson(emptyValue);
622 allowRecord->ParseFromJson(value);
623 EXPECT_FALSE(allowRecord->allowTimeList_.empty());
624 }
625
626 /**
627 * @tc.name: StandbyServiceUnitTest_020
628 * @tc.desc: test TimeProvider of StandbyService.
629 * @tc.type: FUNC
630 * @tc.require:
631 */
632 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_020, TestSize.Level1)
633 {
634 int64_t curSecTimeStamp = MiscServices::TimeServiceClient::GetInstance()->
635 GetWallTimeMs() / TimeConstant::MSEC_PER_SEC;
636 struct tm curLocalTime {};
637 EXPECT_TRUE(TimeProvider::ConvertTimeStampToLocalTime(curSecTimeStamp, curLocalTime));
638 TimeProvider::GetCondition();
639 int64_t timeDiff {0};
640 TimeProvider::TimeDiffToDayNightSwitch(timeDiff);
641 TimeProvider::DiffToFixedClock(0, 0, 0, timeDiff);
642 TimeProvider::GetNapTimeOut();
643 int low = 0;
644 int high = -1;
645 int32_t unexpectedValue = 999;
646 EXPECT_TRUE(TimeProvider::GetRandomDelay(low, high) != unexpectedValue);
647 }
648
649 /**
650 * @tc.name: StandbyServiceUnitTest_021
651 * @tc.desc: test TimedTask of StandbyService.
652 * @tc.type: FUNC
653 * @tc.require:
654 */
655 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_021, TestSize.Level1)
656 {
657 auto timedTask = std::make_shared<TimedTask>();
658 timedTask->OnTrigger();
__anoncff1772b0202()659 auto callBack = [](){};
660 timedTask->SetCallbackInfo(callBack);
661 timedTask->OnTrigger();
662 EXPECT_NE(timedTask, nullptr);
663 }
664
665 /**
666 * @tc.name: StandbyServiceUnitTest_022
667 * @tc.desc: test observer of StandbyService.
668 * @tc.type: FUNC
669 * @tc.require:
670 */
671 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_022, TestSize.Level1)
672 {
673 std::list<SystemProcessInfo> systemProcessInfos {};
674 AbilityManagerHelper::GetInstance()->GetRunningSystemProcess(systemProcessInfos);
675 std::vector<AppExecFwk::RunningProcessInfo> allAppProcessInfos {};
676 AppMgrHelper::GetInstance()->GetAllRunningProcesses(allAppProcessInfos);
677 AppMgrHelper::GetInstance()->appMgrProxy_ = nullptr;
678 AppMgrHelper::GetInstance()->GetAllRunningProcesses(allAppProcessInfos);
679
680 BundleManagerHelper::GetInstance()->GetClientBundleName(0);
681 BundleManagerHelper::GetInstance()->bundleMgr_ = nullptr;
682 BundleManagerHelper::GetInstance()->GetClientBundleName(0);
683 AppExecFwk::ApplicationInfo applicationInfo {};
684 BundleManagerHelper::GetInstance()->GetApplicationInfo(DEFAULT_BUNDLENAME,
685 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, 0, applicationInfo);
686 EXPECT_EQ(BundleManagerHelper::GetInstance()->bundleMgr_, nullptr);
687
688 EXPECT_NE(StandbyServiceImpl::GetInstance()->commonEventObserver_, nullptr);
689 EventFwk::CommonEventData eventData = EventFwk::CommonEventData();
690 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
691 for (const auto& eventName : COMMON_EVENT_LIST) {
692 AAFwk::Want want = AAFwk::Want();
693 want.SetAction(eventName);
694 eventData.SetWant(want);
695 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
696 }
697 SleepForFC();
698 }
699
700 /**
701 * @tc.name: StandbyServiceUnitTest_023
702 * @tc.desc: test GetPidAndProcName of StandbyService.
703 * @tc.type: FUNC
704 * @tc.require:
705 */
706 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_023, TestSize.Level1)
707 {
708 std::unordered_map<int32_t, std::string> pidNameMap {};
709 StandbyServiceImpl::GetInstance()->GetPidAndProcName(pidNameMap);
710 EXPECT_NE(pidNameMap.size(), 0);
711 IBundleManagerHelper::MockGetAllRunningProcesses(false);
712 StandbyServiceImpl::GetInstance()->GetPidAndProcName(pidNameMap);
713 IBundleManagerHelper::MockGetAllRunningProcesses(true);
714 IBundleManagerHelper::MockGetRunningSystemProcess(false);
715 StandbyServiceImpl::GetInstance()->GetPidAndProcName(pidNameMap);
716 IBundleManagerHelper::MockGetRunningSystemProcess(true);
717 }
718
719 /**
720 * @tc.name: StandbyServiceUnitTest_024
721 * @tc.desc: test multithread OnReceiveEvent of StandbyService.
722 * @tc.type: FUNC
723 * @tc.require:
724 */
725 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_024, TestSize.Level1, 20)
726 {
727 EventFwk::CommonEventData eventData = EventFwk::CommonEventData();
728 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
729 for (const auto& event : COMMON_EVENT_LIST) {
730 AAFwk::Want want = AAFwk::Want();
731 want.SetAction(event);
732 eventData.SetWant(want);
733 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
734 }
735 StandbyServiceUnitTest::SleepForFC();
736 EXPECT_NE(StandbyServiceImpl::GetInstance()->commonEventObserver_, nullptr);
737 MockCommonEvent::MockSubscribeCommonEvent(false);
738 StandbyServiceImpl::GetInstance()->commonEventObserver_->Subscribe();
739 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->commonEventObserver_->Unsubscribe());
740 MockCommonEvent::MockSubscribeCommonEvent(true);
741 StandbyServiceImpl::GetInstance()->commonEventObserver_->Subscribe();
742 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->commonEventObserver_->Unsubscribe());
743 }
744
745 /**
746 * @tc.name: StandbyServiceUnitTest_025
747 * @tc.desc: test multithread init of StandbyService.
748 * @tc.type: FUNC
749 * @tc.require:
750 */
751 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_025, TestSize.Level1, 20)
752 {
753 StandbyServiceImpl::GetInstance()->InitReadyState();
754 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
755 StandbyServiceUnitTest::SleepForFC();
756 }
757
758 /**
759 * @tc.name: StandbyServiceUnitTest_027
760 * @tc.desc: test multithread DayNightSwitchCallback of StandbyService.
761 * @tc.type: FUNC
762 * @tc.require:
763 */
764 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_027, TestSize.Level1, 20)
765 {
766 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
767 StandbyServiceUnitTest::SleepForFC();
768 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
769 }
770
771 /**
772 * @tc.name: StandbyServiceUnitTest_028
773 * @tc.desc: test multithread ApplyAllowResource of StandbyService.
774 * @tc.type: FUNC
775 * @tc.require:
776 */
777 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_028, TestSize.Level1, 20)
778 {
779 ResourceRequest resourceRequest;
780 StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
781 std::shared_ptr<ResourceRequest> validResRequest = std::make_shared<ResourceRequest>(AllowType::NETWORK,
782 0, "test_process", 100, "test", 1);
783 EXPECT_EQ(StandbyServiceImpl::GetInstance()->ApplyAllowResource(*validResRequest), ERR_OK);
784 std::shared_ptr<ResourceRequest> invalidResRequest = std::make_shared<ResourceRequest>(AllowType::NETWORK,
785 -1, "test_process", 100, "test", 1);
786 StandbyServiceImpl::GetInstance()->ApplyAllowResource(*invalidResRequest);
787 std::shared_ptr<ResourceRequest> negResRequest = std::make_shared<ResourceRequest>(AllowType::NETWORK,
788 0, "test_process", -1, "test", 1);
789 StandbyServiceImpl::GetInstance()->ApplyAllowResource(*negResRequest);
790 StandbyServiceUnitTest::SleepForFC();
791 }
792
793 /**
794 * @tc.name: StandbyServiceUnitTest_029
795 * @tc.desc: test multithread UnapplyAllowResource of StandbyService.
796 * @tc.type: FUNC
797 * @tc.require:
798 */
799 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_029, TestSize.Level1, 20)
800 {
801 ResourceRequest resourceRequest;
802 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
803 std::shared_ptr<ResourceRequest> validResRequest = std::make_shared<ResourceRequest>(AllowType::NETWORK,
804 0, "test_process", 100, "test", 1);
805 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnapplyAllowResource(*validResRequest), ERR_OK);
806 std::shared_ptr<ResourceRequest> invalidResRequest = std::make_shared<ResourceRequest>(AllowType::NETWORK,
807 -1, "test_process", 100, "test", 1);
808 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(*invalidResRequest);
809 }
810
811 /**
812 * @tc.name: StandbyServiceUnitTest_030
813 * @tc.desc: test multithread GetAllowList of StandbyService.
814 * @tc.type: FUNC
815 * @tc.require:
816 */
817 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_030, TestSize.Level1, 20)
818 {
819 std::vector<AllowInfo> allowInfoList;
820 StandbyServiceImpl::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0);
821 EXPECT_EQ(allowInfoList.size(), 0);
822 }
823
824 /**
825 * @tc.name: StandbyServiceUnitTest_031
826 * @tc.desc: test multithread IsDeviceInStandby of StandbyService.
827 * @tc.type: FUNC
828 * @tc.require:
829 */
830 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_031, TestSize.Level1, 20)
831 {
832 bool isStandby {false};
833 EXPECT_EQ(StandbyServiceImpl::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
834 }
835
836 /**
837 * @tc.name: StandbyServiceUnitTest_032
838 * @tc.desc: test multithread ShellDump of StandbyService.
839 * @tc.type: FUNC
840 * @tc.require:
841 */
842 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_032, TestSize.Level1, 20)
843 {
844 std::vector<std::string> argsInStr {};
845 std::string result;
846 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
847 EXPECT_NE(result.size(), 0);
848 }
849
850 /**
851 * @tc.name: StandbyServiceUnitTest_033
852 * @tc.desc: test RegisterCommEventObserver of StandbyService.
853 * @tc.type: FUNC
854 * @tc.require:
855 */
856 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_033, TestSize.Level1, 20)
857 {
858 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
859 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnregisterCommEventObserver(), ERR_OK);
860 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
861 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
862 MockCommonEvent::MockSubscribeCommonEvent(false);
863 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
864 StandbyServiceImpl::GetInstance()->UnregisterCommEventObserver();
865 MockCommonEvent::MockSubscribeCommonEvent(true);
866 }
867
868 /**
869 * @tc.name: StandbyServiceUnitTest_034
870 * @tc.desc: test RegisterCommEventObserver of StandbyService.
871 * @tc.type: FUNC
872 * @tc.require:
873 */
874 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_034, TestSize.Level1, 20)
875 {
876 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
877 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnregisterTimeObserver(), ERR_OK);
878 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
879 StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_ = 1;
880 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
881 StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_ = 0;
882 MockCommonEvent::MockSubscribeCommonEvent(false);
883 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
884 MockIpc::MockStartTimer(false);
885 StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_ = 0;
886 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
887 MockIpc::MockStartTimer(true);
888 MockCommonEvent::MockSubscribeCommonEvent(true);
889 }
890
891 /**
892 * @tc.name: StandbyServiceUnitTest_035
893 * @tc.desc: test AddSubscriber of StandbyService.
894 * @tc.type: FUNC
895 * @tc.require:
896 */
897 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_035, TestSize.Level1, 20)
898 {
899 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
900 StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber);
901 StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber);
902 MockCommonEvent::MockPublishCommonEvent(true);
903 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::WORKING);
904 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::NAP);
905 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::SLEEP);
906 MockCommonEvent::MockPublishCommonEvent(false);
907 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::WORKING);
908 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::NAP);
909 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::SLEEP);
910 MockCommonEvent::MockPublishCommonEvent(true);
911 EXPECT_TRUE(true);
912 }
913
914 /**
915 * @tc.name: StandbyServiceUnitTest_036
916 * @tc.desc: test OnRemoteRequestInner of StandbyStateSubscriber.
917 * @tc.type: FUNC
918 * @tc.require:
919 */
920 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_036, TestSize.Level1, 20)
921 {
922 sptr<StandbyServiceSubscriberStub> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
923 MessageParcel data {};
924 MessageParcel reply {};
925 MessageOption option {MessageOption::TF_ASYNC};
926 data.WriteInterfaceToken(IStandbyServiceSubscriber::GetDescriptor());
927 subscriber->OnRemoteRequest(
928 (static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_DEVICE_IDLE_MODE)), data, reply, option);
929 subscriber->OnRemoteRequest(
930 (static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED)), data, reply, option);
931 auto ret = subscriber->OnRemoteRequest(
932 static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED) + 1, data, reply, option);
933 EXPECT_NE(ret, ERR_OK);
934 }
935
936 /**
937 * @tc.name: StandbyServiceUnitTest_038
938 * @tc.desc: test TimedTask.
939 * @tc.type: FUNC
940 * @tc.require:
941 */
942 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_038, TestSize.Level1)
943 {
944 auto timedTask = std::make_shared<TimedTask>(false, 0, true);
945 timedTask = std::make_shared<TimedTask>(false, 0, false);
946 uint64_t timerId {0};
947 timedTask->StartDayNightSwitchTimer(timerId);
948 std::function<void()> callBack {};
949 MockIpc::MockStartTimer(false);
950 uint64_t zeroTimeId {0};
951 uint64_t negativeTimeId {-1};
952 EXPECT_TRUE(timedTask->RegisterDayNightSwitchTimer(zeroTimeId, false, 0, callBack));
953 EXPECT_TRUE(timedTask->RegisterDayNightSwitchTimer(negativeTimeId, false, 0, callBack));
954 timedTask->StartDayNightSwitchTimer(timerId);
955 TimedTask::CreateTimer(false, 0, false, false, callBack);
956 StandbyServiceImpl::GetInstance()->UnregisterTimeObserver();
957 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
958 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
959 StandbyServiceUnitTest::SleepForFC();
960 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
961 StandbyServiceUnitTest::SleepForFC();
962 MockIpc::MockStartTimer(true);
963 timedTask->RegisterDayNightSwitchTimer(zeroTimeId, false, 0, callBack);
964 timedTask->RegisterDayNightSwitchTimer(negativeTimeId, false, 0, callBack);
965 timedTask->SetType(zeroTimeId);
966 timedTask->SetRepeat(false);
967 timedTask->SetInterval(zeroTimeId);
968 timedTask->SetWantAgent(nullptr);
969 EXPECT_TRUE(timedTask->type == zeroTimeId);
970 EXPECT_TRUE(timedTask->repeat == false);
971 EXPECT_TRUE(timedTask->interval == zeroTimeId);
972 EXPECT_TRUE(timedTask->wantAgent == nullptr);
973 }
974
975 /**
976 * @tc.name: StandbyServiceUnitTest_041
977 * @tc.desc: test RegisterAppStateObserver of StandbyServiceImpl.
978 * @tc.type: FUNC
979 * @tc.require:
980 */
981 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_041, TestSize.Level1)
982 {
983 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
984 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
985 EXPECT_EQ(StandbyServiceImpl::GetInstance()->appStateObserver_, nullptr);
986 IBundleManagerHelper::MockSubscribeObserver(true);
987 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
988 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
989 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
990 IBundleManagerHelper::MockSubscribeObserver(false);
991 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
992 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
993 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
994 }
995
996 /**
997 * @tc.name: StandbyServiceUnitTest_042
998 * @tc.desc: test AppStateObserver.
999 * @tc.type: FUNC
1000 * @tc.require:
1001 */
1002 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_042, TestSize.Level1)
1003 {
1004 auto appStateObserver = std::make_shared<AppStateObserver>(StandbyServiceImpl::GetInstance()->handler_);
1005 AppExecFwk::ProcessData processData {};
1006 appStateObserver->OnProcessDied(processData);
1007 processData.bundleName = "com.ohos.systemui";
1008 appStateObserver->OnProcessDied(processData);
1009
1010 AppExecFwk::AppStateData appStateData {};
1011 appStateData.uid = -1;
1012 appStateObserver->OnApplicationStateChanged(appStateData);
1013 appStateData.uid = SAMPLE_APP_UID;
1014 appStateObserver->OnApplicationStateChanged(appStateData);
1015
1016 appStateData.bundleName = SAMPLE_BUNDLE_NAME;
1017 appStateObserver->OnApplicationStateChanged(appStateData);
1018
1019 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_TERMINATED);
1020 appStateObserver->OnApplicationStateChanged(appStateData);
1021 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_END);
1022 appStateObserver->OnApplicationStateChanged(appStateData);
1023 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOCUS);
1024 appStateObserver->OnApplicationStateChanged(appStateData);
1025 appStateObserver->OnProcessCreated(processData);
1026 appStateObserver->OnForegroundApplicationChanged(appStateData);
1027 SleepForFC();
1028 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->allowInfoMap_.empty());
1029 }
1030
1031 /**
1032 * @tc.name: StandbyServiceUnitTest_043
1033 * @tc.desc: test GetRestrictList.
1034 * @tc.type: FUNC
1035 * @tc.require:
1036 */
1037 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_043, TestSize.Level1)
1038 {
1039 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1040 uint32_t restrictType = 1;
1041 std::vector<AllowInfo> restrictInfoList;
1042 uint32_t reasonCode = 1;
1043 EXPECT_EQ(StandbyService::GetInstance()->GetRestrictList(restrictType, restrictInfoList, reasonCode), ERR_OK);
1044
1045 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1046 EXPECT_EQ(StandbyService::GetInstance()->
1047 GetRestrictList(restrictType, restrictInfoList, reasonCode), ERR_STANDBY_SYS_NOT_READY);
1048 }
1049
1050 /**
1051 * @tc.name: StandbyServiceUnitTest_044
1052 * @tc.desc: test IsStrategyEnabled.
1053 * @tc.type: FUNC
1054 * @tc.require:
1055 */
1056 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_044, TestSize.Level1)
1057 {
1058 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1059 std::string strategyName;
1060 bool isEnabled = false;
1061 EXPECT_EQ(StandbyService::GetInstance()->IsStrategyEnabled(strategyName, isEnabled), ERR_OK);
1062
1063 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1064 EXPECT_EQ(StandbyService::GetInstance()->IsStrategyEnabled(strategyName, isEnabled), ERR_STANDBY_SYS_NOT_READY);
1065 }
1066
1067 /**
1068 * @tc.name: StandbyServiceUnitTest_045
1069 * @tc.desc: test ReportDeviceStateChanged.
1070 * @tc.type: FUNC
1071 * @tc.require:
1072 */
1073 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_045, TestSize.Level1)
1074 {
1075 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1076 int32_t type = static_cast<int32_t>(DeviceStateType::DIS_COMP_CHANGE);
1077 bool enabled = true;
1078 EXPECT_EQ(StandbyService::GetInstance()->ReportDeviceStateChanged(type, enabled), ERR_OK);
1079
1080 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1081 EXPECT_EQ(StandbyService::GetInstance()->ReportDeviceStateChanged(type, enabled), ERR_STANDBY_SYS_NOT_READY);
1082 }
1083
1084 /**
1085 * @tc.name: StandbyServiceUnitTest_046
1086 * @tc.desc: test ReportDeviceStateChanged.
1087 * @tc.type: FUNC
1088 * @tc.require:
1089 */
1090 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_046, TestSize.Level1)
1091 {
1092 int32_t type = -1;
1093 DeviceStateCache::GetInstance()->deviceState_ = {true, true, false};
1094 EXPECT_EQ(DeviceStateCache::GetInstance()->GetDeviceState(type), false);
1095
1096 type = DeviceStateCache::DEVICE_STATE_NUM;
1097 EXPECT_EQ(DeviceStateCache::GetInstance()->GetDeviceState(type), false);
1098
1099 type = 1;
1100 EXPECT_EQ(DeviceStateCache::GetInstance()->GetDeviceState(type), true);
1101 }
1102
1103 /**
1104 * @tc.name: StandbyServiceUnitTest_047
1105 * @tc.desc: test ReportDeviceStateChanged.
1106 * @tc.type: FUNC
1107 * @tc.require:
1108 */
1109 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_047, TestSize.Level1)
1110 {
1111 int32_t type = -1;
1112 bool enabled = true;
1113 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), false);
1114
1115 type = DeviceStateCache::DEVICE_STATE_NUM;
1116 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), false);
1117
1118 type = 1;
1119 DeviceStateCache::GetInstance()->deviceState_ = {true, true, false};
1120 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), false);
1121
1122 type = 2;
1123 DeviceStateCache::GetInstance()->deviceState_ = {true, true, false};
1124 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), true);
1125 }
1126
1127 /**
1128 * @tc.name: StandbyServiceUnitTest_026
1129 * @tc.desc: test multithread uninit of StandbyService.
1130 * @tc.type: FUNC
1131 * @tc.require:
1132 */
1133 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_026, TestSize.Level1, 20)
1134 {
1135 StandbyServiceImpl::GetInstance()->UninitReadyState();
1136 EXPECT_FALSE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
1137 StandbyServiceUnitTest::SleepForFC();
1138 }
1139
1140 /**
1141 * @tc.name: StandbyServiceUnitTest_048
1142 * @tc.desc: test common event timer sa ability.
1143 * @tc.type: FUNC
1144 * @tc.require:
1145 */
1146 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_048, TestSize.Level1)
1147 {
1148 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
1149 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnregisterTimeObserver(), ERR_OK);
1150 }
1151
1152 /**
1153 * @tc.name: StandbyServiceUnitTest_049
1154 * @tc.desc: test resources state changed.
1155 * @tc.type: FUNC
1156 * @tc.require:
1157 */
1158 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_049, TestSize.Level1)
1159 {
1160 int64_t value = 0;
1161 std::string sceneInfo = "{\"bundleName\":\"com.timeradjust.test\",\"pid\":5569,"
1162 "\"resourceNumber\":4,\"uid\":20020139}";
1163 StandbyServiceImpl::GetInstance()->HandleResourcesStateChanged(value, sceneInfo);
1164 value = 1;
1165 StandbyServiceImpl::GetInstance()->HandleResourcesStateChanged(value, sceneInfo);
1166 sceneInfo = "{\"bundleNameTest\":\"com.timeradjust.test\",\"pid\":5569,"
1167 "\"resourceNumberTest\":4,\"uid\":20020139}";
1168 StandbyServiceImpl::GetInstance()->HandleResourcesStateChanged(value, sceneInfo);
1169 sceneInfo = "{\"bundleName\": 1,\"pid\":5569,"
1170 "\"resourceNumber\":4,\"uid\":20020139}";
1171 StandbyServiceImpl::GetInstance()->HandleResourcesStateChanged(value, sceneInfo);
1172 sceneInfo = "{\"bundleName\":\"com.timeradjust.test\",\"pid\":\"com.timeradjust.test\","
1173 "\"resourceNumber\":4,\"uid\":20020139}";
1174 StandbyServiceImpl::GetInstance()->HandleResourcesStateChanged(value, sceneInfo);
1175 StandbyServiceImpl::GetInstance()->HandleScreenStateChanged(value);
1176 value = 0;
1177 StandbyServiceImpl::GetInstance()->HandleScreenStateChanged(value);
1178 EXPECT_NE(StandbyServiceImpl::GetInstance(), nullptr);
1179 }
1180
1181 /**
1182 * @tc.name: StandbyServiceUnitTest_057
1183 * @tc.desc: test nat interval changed.
1184 * @tc.type: FUNC
1185 * @tc.require:
1186 */
1187 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_057, TestSize.Level1)
1188 {
1189 uint32_t type = 1;
1190 bool enable = true;
1191 uint32_t interval = 300;
1192 ErrCode code = DelayedSingleton<StandbyService>::GetInstance()->SetNatInterval(type, enable, interval);
1193 EXPECT_EQ(code, ERR_PERMISSION_DENIED);
1194 int64_t timestamp = 100;
1195 code = DelayedSingleton<StandbyService>::GetInstance()->DelayHeartBeat(timestamp);
1196 EXPECT_EQ(code, ERR_PERMISSION_DENIED);
1197 }
1198
1199 /**
1200 * @tc.name: StandbyServiceUnitTest_058
1201 * @tc.desc: test StandbyService stop/state.
1202 * @tc.type: FUNC
1203 * @tc.require:
1204 */
1205 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_058, TestSize.Level1)
1206 {
1207 StandbyServiceImpl::GetInstance()->listenerManager_ = std::make_shared<ListenerManagerAdapter>();
1208 StandbyServiceImpl::GetInstance()->debugMode_ = false;
1209 EXPECT_NE(DelayedSingleton<StandbyServiceImpl>::GetInstance()->GetListenerManager(), nullptr);
1210 EXPECT_EQ(DelayedSingleton<StandbyServiceImpl>::GetInstance()->IsDebugMode(), false);
1211 DelayedSingleton<StandbyService>::GetInstance()->OnStop();
1212 EXPECT_EQ(DelayedSingleton<StandbyService>::GetInstance()->state_, ServiceRunningState::STATE_NOT_START);
1213 }
1214
1215 /**
1216 * @tc.name: StandbyServiceUnitTest_059
1217 * @tc.desc: test CreateTimer of TimedTask.
1218 * @tc.type: FUNC
1219 * @tc.require:
1220 */
1221 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_059, TestSize.Level1)
1222 {
1223 auto timedTask = std::make_shared<TimedTask>(false, 0, WAKEUP_EXACT_TIMER_TYPE);
1224 uint64_t timerId {0};
1225 std::function<void()> callBack {};
1226 MockIpc::MockStartTimer(false);
1227 uint64_t zeroTimeId {0};
1228 timerId = TimedTask::CreateTimer(false, 0, WAKEUP_EXACT_TIMER_TYPE, callBack);
1229 EXPECT_EQ(timerId, 0);
1230 timedTask->StartDayNightSwitchTimer(timerId);
1231 StandbyServiceUnitTest::SleepForFC();
1232 MockIpc::MockStartTimer(true);
1233 timerId = TimedTask::CreateTimer(true, 0, WAKEUP_EXACT_TIMER_TYPE, callBack);
1234 EXPECT_NE(timerId, 0);
1235 timedTask->SetType(zeroTimeId);
1236 timedTask->SetRepeat(false);
1237 timedTask->SetInterval(zeroTimeId);
1238 timedTask->SetWantAgent(nullptr);
1239 EXPECT_TRUE(timedTask->type == zeroTimeId);
1240 EXPECT_TRUE(timedTask->repeat == false);
1241 EXPECT_TRUE(timedTask->interval == zeroTimeId);
1242 EXPECT_TRUE(timedTask->wantAgent == nullptr);
1243 timedTask->StartDayNightSwitchTimer(timerId);
1244 }
1245
1246 /**
1247 * @tc.name: StandbyServiceUnitTest_060
1248 * @tc.desc: test DumpOnPowerOverused.
1249 * @tc.type: FUNC
1250 * @tc.require:
1251 */
1252 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_060, TestSize.Level1)
1253 {
1254 std::string result {""};
1255 std::string TEST_MODULE_NAME = "TestModule";
1256 std::string TEST_SUB_NAME = "TestSubName";
1257 std::vector<std::string> argsInStr {"--poweroverused", TEST_MODULE_NAME, "1"};
1258
1259 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
1260 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
1261 subscriber->SetSubscriberName(TEST_SUB_NAME);
1262 subscriber->SetModuleName(TEST_MODULE_NAME);
1263 EXPECT_EQ(subscriber->GetModuleName(), TEST_MODULE_NAME);
1264 StandbyServiceImpl::GetInstance()->ShellDumpInner(argsInStr, result);
1265 EXPECT_EQ(result.size(), 0);
1266 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
1267 }
1268
1269 /**
1270 * @tc.name: StandbyServiceUnitTest_061
1271 * @tc.desc: test NotifyPowerOverusedByCallback.
1272 * @tc.type: FUNC
1273 * @tc.require:
1274 */
1275 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_061, TestSize.Level1)
1276 {
1277 std::string ANOTHER_MODULE_NAME = "ModuleX";
1278 std::string TEST_MODULE_NAME = "TestModule";
1279 std::string TEST_SUB_NAME = "TestSubName";
1280
1281 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
1282 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
1283 subscriber->SetSubscriberName(TEST_SUB_NAME);
1284 subscriber->SetModuleName(ANOTHER_MODULE_NAME);
1285 EXPECT_EQ(subscriber->GetModuleName(), ANOTHER_MODULE_NAME);
1286
1287 // not same module name, will not callback
1288 StandbyServiceImpl::GetInstance()->HandlePowerOverused(0, TEST_MODULE_NAME, 1);
1289 SUCCEED();
1290
1291 // change module name, will callback
1292 subscriber->SetModuleName(TEST_MODULE_NAME);
1293 EXPECT_EQ(subscriber->GetModuleName(), TEST_MODULE_NAME);
1294 StandbyStateSubscriber::GetInstance()->NotifyPowerOverusedByCallback(TEST_MODULE_NAME, 1);
1295 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
1296 }
1297
1298 /**
1299 * @tc.name: StandbyServiceUnitTest_062
1300 * @tc.desc: test ReportPowerOverused.
1301 * @tc.type: FUNC
1302 * @tc.require:
1303 */
1304 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_062, TestSize.Level1)
1305 {
1306 std::string TEST_MODULE_NAME = "TestModule";
1307 uint32_t level = static_cast<uint32_t>(PowerOverusedLevel::WARNING);
1308 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1309 ;
1310 EXPECT_EQ(StandbyService::GetInstance()->ReportPowerOverused(TEST_MODULE_NAME, level), ERR_OK);
1311
1312 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1313 level = static_cast<uint32_t>(PowerOverusedLevel::FATAL);
1314 EXPECT_EQ(StandbyService::GetInstance()->ReportPowerOverused(TEST_MODULE_NAME, level), ERR_STANDBY_SYS_NOT_READY);
1315 }
1316
1317 } // namespace DevStandbyMgr
1318 } // namespace OHOS
1319