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.Level0)
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 uint32_t resType = 1;
208 int64_t value = 0;
209 std::string sceneInfo = "test_sceneInfo";
210 int32_t code = StandbyService::GetInstance()->ReportSceneInfo(resType, value, sceneInfo);
211 EXPECT_EQ(code, ERR_OK);
212 StandbyService::GetInstance()->SubscribeStandbyCallback(subscriber, subscriberName, moduleName);
213 StandbyService::GetInstance()->UnsubscribeStandbyCallback(subscriber);
214 StandbyService::GetInstance()->ApplyAllowResource(resourceRequest);
215 StandbyService::GetInstance()->UnapplyAllowResource(resourceRequest);
216 EXPECT_EQ(StandbyService::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0), ERR_OK);
217 EXPECT_EQ(StandbyService::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
218 StandbyService::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
219 }
220
221 /**
222 * @tc.name: StandbyServiceUnitTest_004
223 * @tc.desc: test unready not start of StandbyServiceImpl.
224 * @tc.type: FUNC
225 * @tc.require:
226 */
227 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_004, TestSize.Level0)
228 {
229 StandbyServiceImpl::GetInstance()->isServiceReady_ = false;
230 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
231 SleepForFC();
232 MockIpc::MockStartTimer(false);
233 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
234 SleepForFC();
235 MockIpc::MockStartTimer(true);
236 StandbyServiceImpl::GetInstance()->RemoveAppAllowRecord(DEFAULT_UID, DEFAULT_BUNDLENAME, true);
237 ResourceRequest resourceRequest;
238 StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
239 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
240 std::vector<AllowInfo> allowInfoList;
241 StandbyServiceImpl::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0);
242 bool isStandby {false};
243 EXPECT_NE(StandbyServiceImpl::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
244 std::vector<std::string> argsInStr {};
245 std::string result;
246 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
247 StandbyServiceImpl::GetInstance()->OnProcessStatusChanged(-1, -1, "", true);
248 StandbyServiceImpl::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
249 StandbyServiceImpl::GetInstance()->isServiceReady_ = true;
250 StandbyServiceImpl::GetInstance()->OnProcessStatusChanged(-1, -1, "", true);
251 StandbyServiceImpl::GetInstance()->ReportWorkSchedulerStatus(true, -1, "");
252 }
253
254 /**
255 * @tc.name: StandbyServiceUnitTest_005
256 * @tc.desc: test shelldump not start of StandbyService.
257 * @tc.type: FUNC
258 * @tc.require:
259 */
260 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_005, TestSize.Level1)
261 {
262 std::vector<std::u16string> args {};
263 std::vector<std::string> argsInStr {};
264 std::string result;
265 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
266 StandbyServiceImpl::GetInstance()->ShellDumpInner({}, result);
267 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-h"}, result);
268 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-v"}, result);
269 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D"}, result);
270 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D", "--config"}, result);
271 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D", "--conf"}, result);
272 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-E"}, result);
273 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-E", "working", "false"}, result);
274 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-E", "working", "true"}, result);
275 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A"}, result);
276 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--apply"}, result);
277 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--unapply"}, result);
278 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get"}, result);
279 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--apply", "0", "test", "127", "100", "0"}, result);
280 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--apply", "0", "test", "127", "100", "1"}, result);
281 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--unapply", "0", "test", "127", "100", "0"}, result);
282 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--unapply", "0", "test", "127", "100", "1"}, result);
283 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "true"}, result);
284 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "false"}, result);
285 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S"}, result);
286 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S", "--repeat"}, result);
287 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S", "--motion"}, result);
288 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-S", "--default"}, result);
289 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-O"}, result);
290 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T"}, result);
291 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "on"}, result);
292 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "off"}, result);
293 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "debug", "on"}, result);
294 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-C", "dark_timeout", "60"}, result);
295 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "on"}, result);
296 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "detect_motion", "off"}, result);
297 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-T", "debug", "off"}, result);
298 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-C"}, result);
299 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-C", "dark_timeout", "60"}, result);
300 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D"}, result);
301 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "false", "true"}, result);
302
303 auto allowRecord = std::make_shared<AllowRecord>(0, 0, "name", AllowType::NETWORK);
304 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
305 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
306 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-D"}, result);
307 SleepForFC();
308 EXPECT_NE(StandbyService::GetInstance()->Dump(-1, args), ERR_OK);
309 }
310
311 /**
312 * @tc.name: StandbyServiceUnitTest_006
313 * @tc.desc: test init of StandbyService.
314 * @tc.type: FUNC
315 * @tc.require:
316 */
317 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_006, TestSize.Level1)
318 {
319 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("test_standby.z.so"), ERR_OK);
320 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("libstandby_utils_policy.z.so"), ERR_OK);
321 }
322
323 /**
324 * @tc.name: StandbyServiceUnitTest_007
325 * @tc.desc: test init of StandbyService.
326 * @tc.type: FUNC
327 * @tc.require:
328 */
329 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_007, TestSize.Level1)
330 {
331 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("test_standby.z.so"), ERR_OK);
332 EXPECT_NE(StandbyServiceImpl::GetInstance()->RegisterPlugin("libstandby_utils_policy.z.so"), ERR_OK);
333 }
334
335 /**
336 * @tc.name: StandbyServiceUnitTest_008
337 * @tc.desc: test ParsePersistentData of StandbyService.
338 * @tc.type: FUNC
339 * @tc.require:
340 */
341 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_008, TestSize.Level0)
342 {
343 StandbyServiceImpl::GetInstance()->ParsePersistentData();
344 auto allowRecord = std::make_shared<AllowRecord>(0, 0, "name", AllowType::NETWORK);
345 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
346 allowRecord = std::make_shared<AllowRecord>(-1, -1, "test", AllowType::NETWORK);
347 allowRecord->allowTimeList_.emplace_back(AllowTime{-1, INT64_MAX, "test"});
348 allowRecord = std::make_shared<AllowRecord>(-1, -1, "test", AllowType::NETWORK);
349 allowRecord->allowTimeList_.emplace_back(AllowTime{-1, INT64_MAX, "test"});
350 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
351 StandbyServiceImpl::GetInstance()->DumpPersistantData();
352 StandbyServiceImpl::GetInstance()->ParsePersistentData();
353 StandbyServiceImpl::GetInstance()->RecoverTimeLimitedTask();
354 allowRecord->allowTimeList_.clear();
355 StandbyServiceImpl::GetInstance()->DumpPersistantData();
356 StandbyServiceImpl::GetInstance()->ParsePersistentData();
357 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->allowInfoMap_.empty());
358 IBundleManagerHelper::MockGetAllRunningProcesses(false);
359 StandbyServiceImpl::GetInstance()->ParsePersistentData();
360 IBundleManagerHelper::MockGetAllRunningProcesses(true);
361
362 auto emptyRecord = std::make_shared<AllowRecord>(0, 0, "name", 0);
363 emptyRecord->allowTimeList_.emplace_back(AllowTime{0, 0, "reason"});
364 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, emptyRecord);
365 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(0, "test", 0, true);
366 emptyRecord->allowTimeList_.emplace_back(AllowTime{1, 0, "reason"});
367 emptyRecord->allowTimeList_.emplace_back(AllowTime{2, 0, "reason"});
368 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(0, "test", AllowType::NETWORK, true);
369 }
370
371 /**
372 * @tc.name: StandbyServiceUnitTest_009
373 * @tc.desc: test ResetTimeObserver of StandbyService.
374 * @tc.type: FUNC
375 * @tc.require:
376 */
377 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_009, TestSize.Level1)
378 {
379 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
380 MockIpc::MockStartTimer(false);
381 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
382 MockIpc::MockStartTimer(true);
383 EXPECT_NE(StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_, 0);
384 }
385
386 /**
387 * @tc.name: StandbyServiceUnitTest_010
388 * @tc.desc: test UnInit of StandbyService.
389 * @tc.type: FUNC
390 * @tc.require:
391 */
392 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_010, TestSize.Level0)
393 {
394 StandbyServiceImpl::GetInstance()->registerPlugin_ = nullptr;
395 StandbyServiceImpl::GetInstance()->UninitReadyState();
396 StandbyServiceImpl::GetInstance()->UnInit();
397 StandbyServiceImpl::GetInstance()->RegisterPlugin(StandbyConfigManager::GetInstance()
398 ->GetPluginName());
399 StandbyServiceImpl::GetInstance()->InitReadyState();
400 SleepForFC();
401 EXPECT_NE(StandbyServiceImpl::GetInstance()->registerPlugin_, nullptr);
402 }
403
404 /**
405 * @tc.name: StandbyServiceUnitTest_011
406 * @tc.desc: test RemoveAppAllowRecord of StandbyService.
407 * @tc.type: FUNC
408 * @tc.require:
409 */
410 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_011, TestSize.Level1)
411 {
412 StandbyServiceImpl::GetInstance()->RemoveAppAllowRecord(DEFAULT_UID, DEFAULT_BUNDLENAME, true);
413 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
414 }
415
416 /**
417 * @tc.name: StandbyServiceUnitTest_012
418 * @tc.desc: test CheckCallerPermission of StandbyService.
419 * @tc.type: FUNC
420 * @tc.require:
421 */
422 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_012, TestSize.Level1)
423 {
424 StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_NATIVE_API);
425 EXPECT_EQ(StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_APP_API), ERR_OK);
426 MockIpc::MockGetTokenTypeFlag(false);
427 StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_NATIVE_API);
428 StandbyServiceImpl::GetInstance()->CheckCallerPermission(ReasonCodeEnum::REASON_APP_API);
429 MockIpc::MockGetTokenTypeFlag(true);
430 Security::AccessToken::AccessTokenID tokenId {};
431 StandbyServiceImpl::GetInstance()->IsSystemAppWithPermission(-1, tokenId, ReasonCodeEnum::REASON_APP_API);
432 StandbyServiceImpl::GetInstance()->IsSystemAppWithPermission(-1, tokenId, ReasonCodeEnum::REASON_NATIVE_API);
433 }
434
435 /**
436 * @tc.name: StandbyServiceUnitTest_013
437 * @tc.desc: test ApplyAllowResource of StandbyService.
438 * @tc.type: FUNC
439 * @tc.require:
440 */
441 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_013, TestSize.Level1)
442 {
443 ResourceRequest resourceRequest;
444 StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
445 SleepForFC();
446 StandbyServiceImpl::GetInstance()->ApplyAllowResInner(resourceRequest, -1);
447 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
448 }
449
450 /**
451 * @tc.name: StandbyServiceUnitTest_014
452 * @tc.desc: test UpdateRecord of StandbyService.
453 * @tc.type: FUNC
454 * @tc.require:
455 */
456 HWTEST_F(StandbyServiceUnitTest, UpdateRecord_014, TestSize.Level1)
457 {
458 std::shared_ptr<AllowRecord> allowRecord = std::make_shared<AllowRecord>();
459 ResourceRequest resourceRequest;
460 StandbyServiceImpl::GetInstance()->UpdateRecord(allowRecord, resourceRequest);
461 SleepForFC();
462 StandbyServiceImpl::GetInstance()->UpdateRecord(allowRecord, resourceRequest);
463 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
464 }
465
466 /**
467 * @tc.name: StandbyServiceUnitTest_015
468 * @tc.desc: test UpdateRecord of StandbyService.
469 * @tc.type: FUNC
470 * @tc.require:
471 */
472 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_015, TestSize.Level1)
473 {
474 std::shared_ptr<AllowRecord> allowRecord = std::make_shared<AllowRecord>();
475 std::shared_ptr<ResourceRequest> resourceRequest = std::make_shared<ResourceRequest>(MAX_ALLOW_TYPE_NUMBER,
476 DEFAULT_UID, DEFAULT_BUNDLENAME, 10, "reason", ReasonCodeEnum::REASON_APP_API);
477 StandbyServiceImpl::GetInstance()->UpdateRecord(allowRecord, *resourceRequest);
478 SleepForFC();
479 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
480 }
481
482 /**
483 * @tc.name: StandbyServiceUnitTest_016
484 * @tc.desc: test DayNightSwitchCallback of StandbyService.
485 * @tc.type: FUNC
486 * @tc.require:
487 */
488 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_016, TestSize.Level0)
489 {
490 StandbyServiceImpl::GetInstance()->standbyStateManager_->TransitToStateInner(StandbyState::WORKING);
491 SleepForFC();
492 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
493 SleepForFC();
494 StandbyServiceImpl::GetInstance()->standbyStateManager_->TransitToStateInner(StandbyState::SLEEP);
495 SleepForFC();
496 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
497 SleepForFC();
498 MockIpc::MockStartTimer(false);
499 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
500 SleepForFC();
501 MockIpc::MockStartTimer(true);
502 StandbyServiceImpl::GetInstance()->UnregisterTimeObserver();
503 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
504 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
505 SleepForFC();
506 EXPECT_NE(StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_, 0);
507 MockIpc::MockStartTimer(false);
508 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
509 MockIpc::MockStartTimer(true);
510 SleepForFC();
511 }
512
513 /**
514 * @tc.name: StandbyServiceUnitTest_017
515 * @tc.desc: test DayNightSwitchCallback of StandbyService.
516 * @tc.type: FUNC
517 * @tc.require:
518 */
519 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_017, TestSize.Level0)
520 {
521 auto allowRecord = std::make_shared<AllowRecord>(DEFAULT_UID, 0, DEFAULT_BUNDLENAME, AllowType::NETWORK);
522 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
523 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
524
525 std::vector<AllowInfo> allowInfoList;
526 StandbyServiceImpl::GetInstance()->GetAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
527 ReasonCodeEnum::REASON_APP_API);
528 StandbyServiceImpl::GetInstance()->GetAllowList(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
529 ReasonCodeEnum::REASON_NATIVE_API);
530 std::string result {""};
531 StandbyServiceImpl::GetInstance()->ShellDumpInner({"-A", "--get", "127", "true"}, result);
532 StandbyServiceImpl::GetInstance()->GetAllowListInner(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
533 ReasonCodeEnum::REASON_APP_API);
534 StandbyServiceImpl::GetInstance()->GetAllowListInner(MAX_ALLOW_TYPE_NUMBER, allowInfoList,
535 ReasonCodeEnum::REASON_NATIVE_API);
536 StandbyServiceImpl::GetInstance()->GetAllowListInner(0, allowInfoList,
537 ReasonCodeEnum::REASON_NATIVE_API);
538 StandbyServiceImpl::GetInstance()->GetTemporaryAllowList(MAX_ALLOW_TYPE_NUM, allowInfoList,
539 ReasonCodeEnum::REASON_APP_API);
540 StandbyServiceImpl::GetInstance()->GetTemporaryAllowList(MAX_ALLOW_TYPE_NUM, allowInfoList,
541 ReasonCodeEnum::REASON_NATIVE_API);
542 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUM, allowInfoList,
543 true, true);
544 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUM, allowInfoList,
545 true, false);
546 ResourceRequest resourceRequest;
547 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
548 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
549 SleepForFC();
550 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(DEFAULT_UID, DEFAULT_BUNDLENAME, 1, false);
551 StandbyServiceImpl::GetInstance()->UnapplyAllowResInner(DEFAULT_UID, DEFAULT_BUNDLENAME, 1, true);
552 allowRecord = std::make_shared<AllowRecord>(0, 0, "name", MAX_ALLOW_TYPE_NUMBER);
553 allowRecord->allowTimeList_.emplace_back(AllowTime{0, INT64_MAX, "reason"});
554 allowRecord->allowTimeList_.emplace_back(AllowTime{1, INT64_MAX, "reason"});
555 StandbyServiceImpl::GetInstance()->allowInfoMap_.emplace(DEFAULT_KEY, allowRecord);
556 StandbyServiceImpl::GetInstance()->GetTemporaryAllowList(MAX_ALLOW_TYPE_NUM, allowInfoList,
557 ReasonCodeEnum::REASON_NATIVE_API);
558 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUM, allowInfoList,
559 true, true);
560 StandbyServiceImpl::GetInstance()->GetPersistAllowList(MAX_ALLOW_TYPE_NUM, allowInfoList,
561 false, true);
562 StandbyServiceImpl::GetInstance()->allowInfoMap_.clear();
563 EXPECT_EQ(StandbyServiceImpl::GetInstance()->allowInfoMap_.size(), 0);
564 }
565
566 /**
567 * @tc.name: StandbyServiceUnitTest_018
568 * @tc.desc: test AddSubscriber of StandbyService.
569 * @tc.type: FUNC
570 * @tc.require:
571 */
572 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_018, TestSize.Level0)
573 {
574 std::vector<std::string> argsInStr {};
575 std::string result {};
576 StandbyStateSubscriber::GetInstance()->ShellDump(argsInStr, result);
577 StandbyStateSubscriber::GetInstance()->NotifyIdleModeByCallback(false, false);
578 StandbyStateSubscriber::GetInstance()->ReportAllowListChanged(DEFAULT_UID, DEFAULT_BUNDLENAME,
579 AllowType::NETWORK, true);
580 sptr<IStandbyServiceSubscriber> nullSubscriber = nullptr;
581 EXPECT_NE(StandbyStateSubscriber::GetInstance()->AddSubscriber(nullSubscriber), ERR_OK);
582 EXPECT_NE(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(nullSubscriber), ERR_OK);
583 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
584 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
585 StandbyStateSubscriber::GetInstance()->ShellDump(argsInStr, result);
586 EXPECT_NE(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
587 StandbyStateSubscriber::GetInstance()->NotifyIdleModeByCallback(false, false);
588 StandbyStateSubscriber::GetInstance()->ReportAllowListChanged(DEFAULT_UID, DEFAULT_BUNDLENAME,
589 AllowType::NETWORK, true);
590 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
591 StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber);
592 StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber);
593 auto remote = subscriber->AsObject();
594 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
595 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
596 StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber);
597
598 StandbyStateSubscriber::GetInstance()->deathRecipient_ = nullptr;
599 EXPECT_NE(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
600 EXPECT_NE(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
601 StandbyStateSubscriber::GetInstance()->deathRecipient_ = new (std::nothrow) SubscriberDeathRecipient();
602
603 remote = nullptr;
604 StandbyStateSubscriber::GetInstance()->deathRecipient_->OnRemoteDied(remote);
605 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
606 sptr<IRemoteObject> proxy {nullptr};
607 remote = proxy;
608 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
609 StandbyStateSubscriber::GetInstance()->HandleSubscriberDeath(remote);
610 }
611
612 /**
613 * @tc.name: StandbyServiceUnitTest_019
614 * @tc.desc: test AddSubscriber of StandbyService.
615 * @tc.type: FUNC
616 * @tc.require:
617 */
618 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_019, TestSize.Level1)
619 {
620 auto allowRecord = std::make_shared<AllowRecord>(DEFAULT_UID, 0, DEFAULT_BUNDLENAME, AllowType::NETWORK);
621 auto value = allowRecord->ParseToJson();
622 allowRecord->ParseFromJson(value);
623 allowRecord->allowTimeList_.emplace_back(AllowTime{0, 0, "reason"});
624 value = allowRecord->ParseToJson();
625 nlohmann::json emptyValue {};
626 allowRecord->ParseFromJson(emptyValue);
627 allowRecord->ParseFromJson(value);
628 EXPECT_FALSE(allowRecord->allowTimeList_.empty());
629 }
630
631 /**
632 * @tc.name: StandbyServiceUnitTest_020
633 * @tc.desc: test TimeProvider of StandbyService.
634 * @tc.type: FUNC
635 * @tc.require:
636 */
637 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_020, TestSize.Level1)
638 {
639 int64_t curSecTimeStamp = MiscServices::TimeServiceClient::GetInstance()->
640 GetWallTimeMs() / TimeConstant::MSEC_PER_SEC;
641 struct tm curLocalTime {};
642 EXPECT_TRUE(TimeProvider::ConvertTimeStampToLocalTime(curSecTimeStamp, curLocalTime));
643 TimeProvider::GetCondition();
644 int64_t timeDiff {0};
645 TimeProvider::TimeDiffToDayNightSwitch(timeDiff);
646 TimeProvider::DiffToFixedClock(0, 0, 0, timeDiff);
647 TimeProvider::GetNapTimeOut();
648 int low = 0;
649 int high = -1;
650 int32_t unexpectedValue = 999;
651 EXPECT_TRUE(TimeProvider::GetRandomDelay(low, high) != unexpectedValue);
652 }
653
654 /**
655 * @tc.name: StandbyServiceUnitTest_021
656 * @tc.desc: test TimedTask of StandbyService.
657 * @tc.type: FUNC
658 * @tc.require:
659 */
660 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_021, TestSize.Level1)
661 {
662 auto timedTask = std::make_shared<TimedTask>();
663 timedTask->OnTrigger();
__anon7f50dbcd0202()664 auto callBack = [](){};
665 timedTask->SetCallbackInfo(callBack);
666 timedTask->OnTrigger();
667 EXPECT_NE(timedTask, nullptr);
668 }
669
670 /**
671 * @tc.name: StandbyServiceUnitTest_022
672 * @tc.desc: test observer of StandbyService.
673 * @tc.type: FUNC
674 * @tc.require:
675 */
676 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_022, TestSize.Level0)
677 {
678 std::list<SystemProcessInfo> systemProcessInfos {};
679 AbilityManagerHelper::GetInstance()->GetRunningSystemProcess(systemProcessInfos);
680 std::vector<AppExecFwk::RunningProcessInfo> allAppProcessInfos {};
681 AppMgrHelper::GetInstance()->GetAllRunningProcesses(allAppProcessInfos);
682 AppMgrHelper::GetInstance()->appMgrProxy_ = nullptr;
683 AppMgrHelper::GetInstance()->GetAllRunningProcesses(allAppProcessInfos);
684
685 BundleManagerHelper::GetInstance()->GetClientBundleName(0);
686 BundleManagerHelper::GetInstance()->bundleMgr_ = nullptr;
687 BundleManagerHelper::GetInstance()->GetClientBundleName(0);
688 AppExecFwk::ApplicationInfo applicationInfo {};
689 BundleManagerHelper::GetInstance()->GetApplicationInfo(DEFAULT_BUNDLENAME,
690 AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, 0, applicationInfo);
691 EXPECT_EQ(BundleManagerHelper::GetInstance()->bundleMgr_, nullptr);
692
693 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
694 EXPECT_NE(StandbyServiceImpl::GetInstance()->commonEventObserver_, nullptr);
695 EventFwk::CommonEventData eventData = EventFwk::CommonEventData();
696 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
697 for (const auto& eventName : COMMON_EVENT_LIST) {
698 AAFwk::Want want = AAFwk::Want();
699 want.SetAction(eventName);
700 eventData.SetWant(want);
701 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
702 }
703 SleepForFC();
704 }
705
706 /**
707 * @tc.name: StandbyServiceUnitTest_023
708 * @tc.desc: test GetPidAndProcName of StandbyService.
709 * @tc.type: FUNC
710 * @tc.require:
711 */
712 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_023, TestSize.Level1)
713 {
714 std::unordered_map<int32_t, std::string> pidNameMap {};
715 StandbyServiceImpl::GetInstance()->GetPidAndProcName(pidNameMap);
716 EXPECT_NE(pidNameMap.size(), 0);
717 IBundleManagerHelper::MockGetAllRunningProcesses(false);
718 StandbyServiceImpl::GetInstance()->GetPidAndProcName(pidNameMap);
719 IBundleManagerHelper::MockGetAllRunningProcesses(true);
720 IBundleManagerHelper::MockGetRunningSystemProcess(false);
721 StandbyServiceImpl::GetInstance()->GetPidAndProcName(pidNameMap);
722 IBundleManagerHelper::MockGetRunningSystemProcess(true);
723 }
724
725 /**
726 * @tc.name: StandbyServiceUnitTest_024
727 * @tc.desc: test multithread OnReceiveEvent of StandbyService.
728 * @tc.type: FUNC
729 * @tc.require:
730 */
731 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_024, TestSize.Level1, 20)
732 {
733 EventFwk::CommonEventData eventData = EventFwk::CommonEventData();
734 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
735 for (const auto& event : COMMON_EVENT_LIST) {
736 AAFwk::Want want = AAFwk::Want();
737 want.SetAction(event);
738 eventData.SetWant(want);
739 StandbyServiceImpl::GetInstance()->commonEventObserver_->OnReceiveEvent(eventData);
740 }
741 StandbyServiceUnitTest::SleepForFC();
742 EXPECT_NE(StandbyServiceImpl::GetInstance()->commonEventObserver_, nullptr);
743 MockCommonEvent::MockSubscribeCommonEvent(false);
744 StandbyServiceImpl::GetInstance()->commonEventObserver_->Subscribe();
745 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->commonEventObserver_->Unsubscribe());
746 MockCommonEvent::MockSubscribeCommonEvent(true);
747 StandbyServiceImpl::GetInstance()->commonEventObserver_->Subscribe();
748 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->commonEventObserver_->Unsubscribe());
749 }
750
751 /**
752 * @tc.name: StandbyServiceUnitTest_025
753 * @tc.desc: test multithread init of StandbyService.
754 * @tc.type: FUNC
755 * @tc.require:
756 */
757 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_025, TestSize.Level1, 20)
758 {
759 StandbyServiceImpl::GetInstance()->InitReadyState();
760 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
761 StandbyServiceUnitTest::SleepForFC();
762 }
763
764 /**
765 * @tc.name: StandbyServiceUnitTest_027
766 * @tc.desc: test multithread DayNightSwitchCallback of StandbyService.
767 * @tc.type: FUNC
768 * @tc.require:
769 */
770 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_027, TestSize.Level1, 20)
771 {
772 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
773 StandbyServiceUnitTest::SleepForFC();
774 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
775 }
776
777 /**
778 * @tc.name: StandbyServiceUnitTest_028
779 * @tc.desc: test multithread ApplyAllowResource of StandbyService.
780 * @tc.type: FUNC
781 * @tc.require:
782 */
783 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_028, TestSize.Level1, 20)
784 {
785 ResourceRequest resourceRequest;
786 StandbyServiceImpl::GetInstance()->ApplyAllowResource(resourceRequest);
787 std::shared_ptr<ResourceRequest> validResRequest = std::make_shared<ResourceRequest>(AllowType::NETWORK,
788 0, "test_process", 100, "test", 1);
789 EXPECT_EQ(StandbyServiceImpl::GetInstance()->ApplyAllowResource(*validResRequest), ERR_OK);
790 std::shared_ptr<ResourceRequest> invalidResRequest = std::make_shared<ResourceRequest>(AllowType::NETWORK,
791 -1, "test_process", 100, "test", 1);
792 StandbyServiceImpl::GetInstance()->ApplyAllowResource(*invalidResRequest);
793 std::shared_ptr<ResourceRequest> negResRequest = std::make_shared<ResourceRequest>(AllowType::NETWORK,
794 0, "test_process", -1, "test", 1);
795 StandbyServiceImpl::GetInstance()->ApplyAllowResource(*negResRequest);
796 StandbyServiceUnitTest::SleepForFC();
797 }
798
799 /**
800 * @tc.name: StandbyServiceUnitTest_029
801 * @tc.desc: test multithread UnapplyAllowResource of StandbyService.
802 * @tc.type: FUNC
803 * @tc.require:
804 */
805 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_029, TestSize.Level1, 20)
806 {
807 ResourceRequest resourceRequest;
808 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(resourceRequest);
809 std::shared_ptr<ResourceRequest> validResRequest = std::make_shared<ResourceRequest>(AllowType::NETWORK,
810 0, "test_process", 100, "test", 1);
811 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnapplyAllowResource(*validResRequest), ERR_OK);
812 std::shared_ptr<ResourceRequest> invalidResRequest = std::make_shared<ResourceRequest>(AllowType::NETWORK,
813 -1, "test_process", 100, "test", 1);
814 StandbyServiceImpl::GetInstance()->UnapplyAllowResource(*invalidResRequest);
815 }
816
817 /**
818 * @tc.name: StandbyServiceUnitTest_030
819 * @tc.desc: test multithread GetAllowList of StandbyService.
820 * @tc.type: FUNC
821 * @tc.require:
822 */
823 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_030, TestSize.Level1, 20)
824 {
825 std::vector<AllowInfo> allowInfoList;
826 StandbyServiceImpl::GetInstance()->GetAllowList(AllowType::NETWORK, allowInfoList, 0);
827 EXPECT_EQ(allowInfoList.size(), 0);
828 }
829
830 /**
831 * @tc.name: StandbyServiceUnitTest_031
832 * @tc.desc: test multithread IsDeviceInStandby of StandbyService.
833 * @tc.type: FUNC
834 * @tc.require:
835 */
836 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_031, TestSize.Level1, 20)
837 {
838 bool isStandby {false};
839 EXPECT_EQ(StandbyServiceImpl::GetInstance()->IsDeviceInStandby(isStandby), ERR_OK);
840 }
841
842 /**
843 * @tc.name: StandbyServiceUnitTest_032
844 * @tc.desc: test multithread ShellDump of StandbyService.
845 * @tc.type: FUNC
846 * @tc.require:
847 */
848 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_032, TestSize.Level1, 20)
849 {
850 std::vector<std::string> argsInStr {};
851 std::string result;
852 StandbyServiceImpl::GetInstance()->ShellDump(argsInStr, result);
853 EXPECT_NE(result.size(), 0);
854 }
855
856 /**
857 * @tc.name: StandbyServiceUnitTest_033
858 * @tc.desc: test RegisterCommEventObserver of StandbyService.
859 * @tc.type: FUNC
860 * @tc.require:
861 */
862 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_033, TestSize.Level1, 20)
863 {
864 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
865 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnregisterCommEventObserver(), ERR_OK);
866 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
867 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
868 MockCommonEvent::MockSubscribeCommonEvent(false);
869 StandbyServiceImpl::GetInstance()->RegisterCommEventObserver();
870 StandbyServiceImpl::GetInstance()->UnregisterCommEventObserver();
871 MockCommonEvent::MockSubscribeCommonEvent(true);
872 }
873
874 /**
875 * @tc.name: StandbyServiceUnitTest_034
876 * @tc.desc: test RegisterCommEventObserver of StandbyService.
877 * @tc.type: FUNC
878 * @tc.require:
879 */
880 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_034, TestSize.Level1, 20)
881 {
882 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
883 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnregisterTimeObserver(), ERR_OK);
884 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
885 StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_ = 1;
886 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
887 StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_ = 0;
888 MockCommonEvent::MockSubscribeCommonEvent(false);
889 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
890 MockIpc::MockStartTimer(false);
891 StandbyServiceImpl::GetInstance()->dayNightSwitchTimerId_ = 0;
892 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
893 MockIpc::MockStartTimer(true);
894 MockCommonEvent::MockSubscribeCommonEvent(true);
895 }
896
897 /**
898 * @tc.name: StandbyServiceUnitTest_035
899 * @tc.desc: test AddSubscriber of StandbyService.
900 * @tc.type: FUNC
901 * @tc.require:
902 */
903 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_035, TestSize.Level1, 20)
904 {
905 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
906 StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber);
907 StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber);
908 MockCommonEvent::MockPublishCommonEvent(true);
909 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::WORKING);
910 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::NAP);
911 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::SLEEP);
912 MockCommonEvent::MockPublishCommonEvent(false);
913 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::WORKING);
914 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::NAP);
915 StandbyStateSubscriber::GetInstance()->ReportStandbyState(StandbyState::SLEEP);
916 MockCommonEvent::MockPublishCommonEvent(true);
917 EXPECT_TRUE(true);
918 }
919
920 /**
921 * @tc.name: StandbyServiceUnitTest_036
922 * @tc.desc: test OnRemoteRequestInner of StandbyStateSubscriber.
923 * @tc.type: FUNC
924 * @tc.require:
925 */
926 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_036, TestSize.Level1, 20)
927 {
928 sptr<StandbyServiceSubscriberStub> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
929 MessageParcel data {};
930 MessageParcel reply {};
931 MessageOption option {MessageOption::TF_ASYNC};
932 data.WriteInterfaceToken(IStandbyServiceSubscriber::GetDescriptor());
933 subscriber->OnRemoteRequest(
934 (static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_DEVICE_IDLE_MODE)), data, reply, option);
935 subscriber->OnRemoteRequest(
936 (static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED)), data, reply, option);
937 auto ret = subscriber->OnRemoteRequest(
938 static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED) + 1, data, reply, option);
939 EXPECT_NE(ret, ERR_OK);
940 }
941
942 /**
943 * @tc.name: StandbyServiceUnitTest_038
944 * @tc.desc: test TimedTask.
945 * @tc.type: FUNC
946 * @tc.require:
947 */
948 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_038, TestSize.Level1)
949 {
950 auto timedTask = std::make_shared<TimedTask>(false, 0, true);
951 timedTask = std::make_shared<TimedTask>(false, 0, false);
952 uint64_t timerId {0};
953 timedTask->StartDayNightSwitchTimer(timerId);
954 std::function<void()> callBack {};
955 MockIpc::MockStartTimer(false);
956 uint64_t zeroTimeId {0};
957 uint64_t negativeTimeId {-1};
958 EXPECT_TRUE(timedTask->RegisterDayNightSwitchTimer(zeroTimeId, false, 0, callBack));
959 EXPECT_TRUE(timedTask->RegisterDayNightSwitchTimer(negativeTimeId, false, 0, callBack));
960 timedTask->StartDayNightSwitchTimer(timerId);
961 TimedTask::CreateTimer(false, 0, false, false, callBack);
962 StandbyServiceImpl::GetInstance()->UnregisterTimeObserver();
963 StandbyServiceImpl::GetInstance()->DayNightSwitchCallback();
964 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
965 StandbyServiceUnitTest::SleepForFC();
966 StandbyServiceImpl::GetInstance()->ResetTimeObserver();
967 StandbyServiceUnitTest::SleepForFC();
968 MockIpc::MockStartTimer(true);
969 timedTask->RegisterDayNightSwitchTimer(zeroTimeId, false, 0, callBack);
970 timedTask->RegisterDayNightSwitchTimer(negativeTimeId, false, 0, callBack);
971 timedTask->SetType(zeroTimeId);
972 timedTask->SetRepeat(false);
973 timedTask->SetInterval(zeroTimeId);
974 timedTask->SetWantAgent(nullptr);
975 EXPECT_TRUE(timedTask->type == zeroTimeId);
976 EXPECT_TRUE(timedTask->repeat == false);
977 EXPECT_TRUE(timedTask->interval == zeroTimeId);
978 EXPECT_TRUE(timedTask->wantAgent == nullptr);
979 }
980
981 /**
982 * @tc.name: StandbyServiceUnitTest_041
983 * @tc.desc: test RegisterAppStateObserver of StandbyServiceImpl.
984 * @tc.type: FUNC
985 * @tc.require:
986 */
987 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_041, TestSize.Level1)
988 {
989 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
990 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
991 EXPECT_EQ(StandbyServiceImpl::GetInstance()->appStateObserver_, nullptr);
992 IBundleManagerHelper::MockSubscribeObserver(true);
993 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
994 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
995 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
996 IBundleManagerHelper::MockSubscribeObserver(false);
997 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
998 StandbyServiceImpl::GetInstance()->RegisterAppStateObserver();
999 StandbyServiceImpl::GetInstance()->UnregisterAppStateObserver();
1000 }
1001
1002 /**
1003 * @tc.name: StandbyServiceUnitTest_042
1004 * @tc.desc: test AppStateObserver.
1005 * @tc.type: FUNC
1006 * @tc.require:
1007 */
1008 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_042, TestSize.Level1)
1009 {
1010 auto appStateObserver = std::make_shared<AppStateObserver>(StandbyServiceImpl::GetInstance()->handler_);
1011 AppExecFwk::ProcessData processData {};
1012 appStateObserver->OnProcessDied(processData);
1013 processData.bundleName = "com.ohos.systemui";
1014 appStateObserver->OnProcessDied(processData);
1015
1016 AppExecFwk::AppStateData appStateData {};
1017 appStateData.uid = -1;
1018 appStateObserver->OnApplicationStateChanged(appStateData);
1019 appStateData.uid = SAMPLE_APP_UID;
1020 appStateObserver->OnApplicationStateChanged(appStateData);
1021
1022 appStateData.bundleName = SAMPLE_BUNDLE_NAME;
1023 appStateObserver->OnApplicationStateChanged(appStateData);
1024
1025 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_TERMINATED);
1026 appStateObserver->OnApplicationStateChanged(appStateData);
1027 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_END);
1028 appStateObserver->OnApplicationStateChanged(appStateData);
1029 appStateData.state = static_cast<int32_t>(AppExecFwk::ApplicationState::APP_STATE_FOCUS);
1030 appStateObserver->OnApplicationStateChanged(appStateData);
1031 appStateObserver->OnProcessCreated(processData);
1032 appStateObserver->OnForegroundApplicationChanged(appStateData);
1033
1034 AppExecFwk::PageStateData pageStateData {};
1035 pageStateData.bundleName = SAMPLE_BUNDLE_NAME;
1036 pageStateData.moduleName = SAMPLE_BUNDLE_NAME;
1037 pageStateData.abilityName = SAMPLE_BUNDLE_NAME;
1038 pageStateData.pageName = SAMPLE_BUNDLE_NAME;
1039 pageStateData.targetBundleName = SAMPLE_BUNDLE_NAME;
1040 pageStateData.targetModuleName = SAMPLE_BUNDLE_NAME;
1041 appStateObserver->OnPageShow(pageStateData);
1042 appStateObserver->OnPageHide(pageStateData);
1043 SleepForFC();
1044 EXPECT_TRUE(StandbyServiceImpl::GetInstance()->allowInfoMap_.empty());
1045 }
1046
1047 /**
1048 * @tc.name: StandbyServiceUnitTest_043
1049 * @tc.desc: test GetRestrictList.
1050 * @tc.type: FUNC
1051 * @tc.require:
1052 */
1053 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_043, TestSize.Level1)
1054 {
1055 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1056 uint32_t restrictType = 1;
1057 std::vector<AllowInfo> restrictInfoList;
1058 uint32_t reasonCode = 1;
1059 EXPECT_EQ(StandbyService::GetInstance()->GetRestrictList(restrictType, restrictInfoList, reasonCode), ERR_OK);
1060
1061 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1062 EXPECT_EQ(StandbyService::GetInstance()->
1063 GetRestrictList(restrictType, restrictInfoList, reasonCode), ERR_STANDBY_SYS_NOT_READY);
1064 }
1065
1066 /**
1067 * @tc.name: StandbyServiceUnitTest_044
1068 * @tc.desc: test IsStrategyEnabled.
1069 * @tc.type: FUNC
1070 * @tc.require:
1071 */
1072 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_044, TestSize.Level1)
1073 {
1074 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1075 std::string strategyName;
1076 bool isEnabled = false;
1077 EXPECT_EQ(StandbyService::GetInstance()->IsStrategyEnabled(strategyName, isEnabled), ERR_OK);
1078
1079 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1080 EXPECT_EQ(StandbyService::GetInstance()->IsStrategyEnabled(strategyName, isEnabled), ERR_STANDBY_SYS_NOT_READY);
1081 }
1082
1083 /**
1084 * @tc.name: StandbyServiceUnitTest_045
1085 * @tc.desc: test ReportDeviceStateChanged.
1086 * @tc.type: FUNC
1087 * @tc.require:
1088 */
1089 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_045, TestSize.Level1)
1090 {
1091 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1092 int32_t type = static_cast<int32_t>(DeviceStateType::DIS_COMP_CHANGE);
1093 bool enabled = true;
1094 EXPECT_EQ(StandbyService::GetInstance()->ReportDeviceStateChanged(type, enabled), ERR_OK);
1095
1096 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1097 EXPECT_EQ(StandbyService::GetInstance()->ReportDeviceStateChanged(type, enabled), ERR_STANDBY_SYS_NOT_READY);
1098 }
1099
1100 /**
1101 * @tc.name: StandbyServiceUnitTest_046
1102 * @tc.desc: test ReportDeviceStateChanged.
1103 * @tc.type: FUNC
1104 * @tc.require:
1105 */
1106 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_046, TestSize.Level1)
1107 {
1108 int32_t type = -1;
1109 DeviceStateCache::GetInstance()->deviceState_ = {true, true, false};
1110 EXPECT_EQ(DeviceStateCache::GetInstance()->GetDeviceState(type), false);
1111
1112 type = DeviceStateCache::DEVICE_STATE_NUM;
1113 EXPECT_EQ(DeviceStateCache::GetInstance()->GetDeviceState(type), false);
1114
1115 type = 1;
1116 EXPECT_EQ(DeviceStateCache::GetInstance()->GetDeviceState(type), true);
1117 }
1118
1119 /**
1120 * @tc.name: StandbyServiceUnitTest_047
1121 * @tc.desc: test ReportDeviceStateChanged.
1122 * @tc.type: FUNC
1123 * @tc.require:
1124 */
1125 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_047, TestSize.Level1)
1126 {
1127 int32_t type = -1;
1128 bool enabled = true;
1129 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), false);
1130
1131 type = DeviceStateCache::DEVICE_STATE_NUM;
1132 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), false);
1133
1134 type = 1;
1135 DeviceStateCache::GetInstance()->deviceState_ = {true, true, false};
1136 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), false);
1137
1138 type = 2;
1139 DeviceStateCache::GetInstance()->deviceState_ = {true, true, false};
1140 EXPECT_EQ(DeviceStateCache::GetInstance()->SetDeviceState(type, enabled), true);
1141 }
1142
1143 /**
1144 * @tc.name: StandbyServiceUnitTest_026
1145 * @tc.desc: test multithread uninit of StandbyService.
1146 * @tc.type: FUNC
1147 * @tc.require:
1148 */
1149 HWMTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_026, TestSize.Level1, 20)
1150 {
1151 StandbyServiceImpl::GetInstance()->UninitReadyState();
1152 EXPECT_FALSE(StandbyServiceImpl::GetInstance()->isServiceReady_.load());
1153 StandbyServiceUnitTest::SleepForFC();
1154 }
1155
1156 /**
1157 * @tc.name: StandbyServiceUnitTest_048
1158 * @tc.desc: test common event timer sa ability.
1159 * @tc.type: FUNC
1160 * @tc.require:
1161 */
1162 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_048, TestSize.Level1)
1163 {
1164 StandbyServiceImpl::GetInstance()->RegisterTimeObserver();
1165 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnregisterTimeObserver(), ERR_OK);
1166 }
1167
1168 /**
1169 * @tc.name: StandbyServiceUnitTest_049
1170 * @tc.desc: test resources state changed.
1171 * @tc.type: FUNC
1172 * @tc.require:
1173 */
1174 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_049, TestSize.Level1)
1175 {
1176 int64_t value = 0;
1177 std::string sceneInfo = "{\"bundleName\":\"com.timeradjust.test\",\"pid\":5569,"
1178 "\"resourceNumber\":4,\"uid\":20020139}";
1179 StandbyServiceImpl::GetInstance()->HandleResourcesStateChanged(value, sceneInfo);
1180 value = 1;
1181 StandbyServiceImpl::GetInstance()->HandleResourcesStateChanged(value, sceneInfo);
1182 sceneInfo = "{\"bundleNameTest\":\"com.timeradjust.test\",\"pid\":5569,"
1183 "\"resourceNumberTest\":4,\"uid\":20020139}";
1184 StandbyServiceImpl::GetInstance()->HandleResourcesStateChanged(value, sceneInfo);
1185 sceneInfo = "{\"bundleName\": 1,\"pid\":5569,"
1186 "\"resourceNumber\":4,\"uid\":20020139}";
1187 StandbyServiceImpl::GetInstance()->HandleResourcesStateChanged(value, sceneInfo);
1188 sceneInfo = "{\"bundleName\":\"com.timeradjust.test\",\"pid\":\"com.timeradjust.test\","
1189 "\"resourceNumber\":4,\"uid\":20020139}";
1190 StandbyServiceImpl::GetInstance()->HandleResourcesStateChanged(value, sceneInfo);
1191 StandbyServiceImpl::GetInstance()->HandleScreenStateChanged(value);
1192 value = 0;
1193 StandbyServiceImpl::GetInstance()->HandleScreenStateChanged(value);
1194 EXPECT_NE(StandbyServiceImpl::GetInstance(), nullptr);
1195 }
1196
1197 /**
1198 * @tc.name: StandbyServiceUnitTest_057
1199 * @tc.desc: test nat interval changed.
1200 * @tc.type: FUNC
1201 * @tc.require:
1202 */
1203 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_057, TestSize.Level1)
1204 {
1205 uint32_t type = 1;
1206 bool enable = true;
1207 uint32_t interval = 300;
1208 ErrCode code = DelayedSingleton<StandbyService>::GetInstance()->SetNatInterval(type, enable, interval);
1209 EXPECT_EQ(code, ERR_PERMISSION_DENIED);
1210 int64_t timestamp = 100;
1211 code = DelayedSingleton<StandbyService>::GetInstance()->DelayHeartBeat(timestamp);
1212 EXPECT_EQ(code, ERR_PERMISSION_DENIED);
1213 }
1214
1215 /**
1216 * @tc.name: StandbyServiceUnitTest_058
1217 * @tc.desc: test StandbyService stop/state.
1218 * @tc.type: FUNC
1219 * @tc.require:
1220 */
1221 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_058, TestSize.Level1)
1222 {
1223 StandbyServiceImpl::GetInstance()->listenerManager_ = std::make_shared<ListenerManagerAdapter>();
1224 StandbyServiceImpl::GetInstance()->debugMode_ = false;
1225 EXPECT_NE(DelayedSingleton<StandbyServiceImpl>::GetInstance()->GetListenerManager(), nullptr);
1226 EXPECT_EQ(DelayedSingleton<StandbyServiceImpl>::GetInstance()->IsDebugMode(), false);
1227 DelayedSingleton<StandbyService>::GetInstance()->OnStop();
1228 EXPECT_EQ(DelayedSingleton<StandbyService>::GetInstance()->state_, ServiceRunningState::STATE_NOT_START);
1229 }
1230
1231 /**
1232 * @tc.name: StandbyServiceUnitTest_059
1233 * @tc.desc: test CreateTimer of TimedTask.
1234 * @tc.type: FUNC
1235 * @tc.require:
1236 */
1237 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_059, TestSize.Level1)
1238 {
1239 auto timedTask = std::make_shared<TimedTask>(false, 0, WAKEUP_EXACT_TIMER_TYPE);
1240 uint64_t timerId {0};
1241 std::function<void()> callBack {};
1242 MockIpc::MockStartTimer(false);
1243 uint64_t zeroTimeId {0};
1244 timerId = TimedTask::CreateTimer(false, 0, WAKEUP_EXACT_TIMER_TYPE, callBack);
1245 EXPECT_EQ(timerId, 0);
1246 timedTask->StartDayNightSwitchTimer(timerId);
1247 StandbyServiceUnitTest::SleepForFC();
1248 MockIpc::MockStartTimer(true);
1249 timerId = TimedTask::CreateTimer(true, 0, WAKEUP_EXACT_TIMER_TYPE, callBack);
1250 EXPECT_NE(timerId, 0);
1251 timedTask->SetType(zeroTimeId);
1252 timedTask->SetRepeat(false);
1253 timedTask->SetInterval(zeroTimeId);
1254 timedTask->SetWantAgent(nullptr);
1255 EXPECT_TRUE(timedTask->type == zeroTimeId);
1256 EXPECT_TRUE(timedTask->repeat == false);
1257 EXPECT_TRUE(timedTask->interval == zeroTimeId);
1258 EXPECT_TRUE(timedTask->wantAgent == nullptr);
1259 timedTask->StartDayNightSwitchTimer(timerId);
1260 }
1261
1262 /**
1263 * @tc.name: StandbyServiceUnitTest_060
1264 * @tc.desc: test DumpOnPowerOverused.
1265 * @tc.type: FUNC
1266 * @tc.require:
1267 */
1268 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_060, TestSize.Level1)
1269 {
1270 std::string result {""};
1271 std::string TEST_MODULE_NAME = "TestModule";
1272 std::string TEST_SUB_NAME = "TestSubName";
1273 std::vector<std::string> argsInStr {"--poweroverused", TEST_MODULE_NAME, "1"};
1274
1275 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
1276 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
1277 subscriber->SetSubscriberName(TEST_SUB_NAME);
1278 subscriber->SetModuleName(TEST_MODULE_NAME);
1279 EXPECT_EQ(subscriber->GetModuleName(), TEST_MODULE_NAME);
1280 StandbyServiceImpl::GetInstance()->ShellDumpInner(argsInStr, result);
1281 EXPECT_EQ(result.size(), 0);
1282 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
1283 }
1284
1285 /**
1286 * @tc.name: StandbyServiceUnitTest_061
1287 * @tc.desc: test NotifyPowerOverusedByCallback.
1288 * @tc.type: FUNC
1289 * @tc.require:
1290 */
1291 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_061, TestSize.Level1)
1292 {
1293 std::string ANOTHER_MODULE_NAME = "ModuleX";
1294 std::string TEST_MODULE_NAME = "TestModule";
1295 std::string TEST_SUB_NAME = "TestSubName";
1296
1297 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
1298 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
1299 subscriber->SetSubscriberName(TEST_SUB_NAME);
1300 subscriber->SetModuleName(ANOTHER_MODULE_NAME);
1301 EXPECT_EQ(subscriber->GetModuleName(), ANOTHER_MODULE_NAME);
1302
1303 // not same module name, will not callback
1304 StandbyServiceImpl::GetInstance()->HandlePowerOverused(0, TEST_MODULE_NAME, 1);
1305 SUCCEED();
1306
1307 // change module name, will callback
1308 subscriber->SetModuleName(TEST_MODULE_NAME);
1309 EXPECT_EQ(subscriber->GetModuleName(), TEST_MODULE_NAME);
1310 StandbyStateSubscriber::GetInstance()->NotifyPowerOverusedByCallback(TEST_MODULE_NAME, 1);
1311 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
1312 }
1313
1314 /**
1315 * @tc.name: StandbyServiceUnitTest_062
1316 * @tc.desc: test ReportPowerOverused.
1317 * @tc.type: FUNC
1318 * @tc.require:
1319 */
1320 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_062, TestSize.Level1)
1321 {
1322 std::string TEST_MODULE_NAME = "TestModule";
1323 uint32_t level = static_cast<uint32_t>(PowerOverusedLevel::WARNING);
1324 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
1325 ;
1326 EXPECT_EQ(StandbyService::GetInstance()->ReportPowerOverused(TEST_MODULE_NAME, level), ERR_OK);
1327
1328 StandbyService::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
1329 level = static_cast<uint32_t>(PowerOverusedLevel::FATAL);
1330 EXPECT_EQ(StandbyService::GetInstance()->ReportPowerOverused(TEST_MODULE_NAME, level), ERR_STANDBY_SYS_NOT_READY);
1331 }
1332
1333 /**
1334 * @tc.name: StandbyServiceUnitTest_063
1335 * @tc.desc: test DumpOnActionChanged.
1336 * @tc.type: FUNC
1337 * @tc.require:
1338 */
1339 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_063, TestSize.Level1)
1340 {
1341 std::string result {""};
1342 std::string TEST_MODULE_NAME = "TestModule";
1343 std::string TEST_SUB_NAME = "TestSubName";
1344 std::vector<std::string> argsInStr {"--actionchanged", TEST_MODULE_NAME, "1"};
1345
1346 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
1347 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
1348 subscriber->SetSubscriberName(TEST_SUB_NAME);
1349 subscriber->SetModuleName(TEST_MODULE_NAME);
1350 EXPECT_EQ(subscriber->GetModuleName(), TEST_MODULE_NAME);
1351 StandbyServiceImpl::GetInstance()->ShellDumpInner(argsInStr, result);
1352 EXPECT_EQ(result.size(), 0);
1353 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
1354 }
1355
1356 /**
1357 * @tc.name: StandbyServiceUnitTest_064
1358 * @tc.desc: test NotifyLowpowerActionByCallback.
1359 * @tc.type: FUNC
1360 * @tc.require:
1361 */
1362 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_064, TestSize.Level1)
1363 {
1364 std::string ANOTHER_MODULE_NAME = "ModuleX";
1365 std::string TEST_MODULE_NAME = "TestModule";
1366 std::string TEST_SUB_NAME = "TestSubName";
1367
1368 sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
1369 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->AddSubscriber(subscriber), ERR_OK);
1370 subscriber->SetSubscriberName(TEST_SUB_NAME);
1371 subscriber->SetModuleName(ANOTHER_MODULE_NAME);
1372 EXPECT_EQ(subscriber->GetModuleName(), ANOTHER_MODULE_NAME);
1373
1374 // not same module name, will not callback
1375 StandbyServiceImpl::GetInstance()->HandleActionChanged(0, TEST_MODULE_NAME, 1);
1376 SUCCEED();
1377
1378 // change module name, will callback
1379 subscriber->SetModuleName(TEST_MODULE_NAME);
1380 EXPECT_EQ(subscriber->GetModuleName(), TEST_MODULE_NAME);
1381 StandbyStateSubscriber::GetInstance()->NotifyLowpowerActionByCallback(TEST_MODULE_NAME, 1);
1382 EXPECT_EQ(StandbyStateSubscriber::GetInstance()->RemoveSubscriber(subscriber), ERR_OK);
1383 }
1384
1385 /**
1386 * @tc.name: StandbyServiceUnitTest_065
1387 * @tc.desc: test OnExtension.
1388 * @tc.type: FUNC
1389 * @tc.require:
1390 */
1391 HWTEST_F(StandbyServiceUnitTest, StandbyServiceUnitTest_065, TestSize.Level1)
1392 {
1393 MessageParcel data;
1394 MessageParcel reply;
1395 EXPECT_EQ(StandbyService::GetInstance()->OnExtension("test", data, reply), ERR_OK);
1396
1397 StandbyServiceImpl::GetInstance()->onBackupFuncMap_.clear();
1398 StandbyServiceImpl::GetInstance()->onRestoreFuncMap_.clear();
1399 EXPECT_EQ(StandbyService::GetInstance()->OnExtension("backup", data, reply), ERR_INVALID_OPERATION);
1400 EXPECT_EQ(StandbyService::GetInstance()->OnExtension("restore", data, reply), ERR_INVALID_OPERATION);
1401
__anon7f50dbcd0302(std::vector<char> &buff) 1402 auto onBackupFunc = [](std::vector<char> &buff) -> ErrCode {
1403 std::string test = "test";
1404 buff.insert(buff.end(), test.begin(), test.end());
1405 return ERR_OK;
1406 };
__anon7f50dbcd0402(std::vector<char> &buff) 1407 auto onRestoreFunc = [](std::vector<char> &buff) -> ErrCode { return ERR_OK; };
1408 auto err = StandbyServiceImpl::GetInstance()->SubscribeBackupRestoreCallback("test", onBackupFunc, onRestoreFunc);
1409 EXPECT_EQ(err, ERR_OK);
1410 err = StandbyServiceImpl::GetInstance()->SubscribeBackupRestoreCallback("test", onBackupFunc, onRestoreFunc);
1411 EXPECT_EQ(err, ERR_INVALID_OPERATION);
1412
1413 EXPECT_EQ(StandbyService::GetInstance()->OnExtension("backup", data, reply), ERR_OK);
1414 data.WriteFileDescriptor(reply.ReadFileDescriptor());
1415 EXPECT_EQ(StandbyService::GetInstance()->OnExtension("restore", data, reply), ERR_OK);
1416
1417 EXPECT_EQ(StandbyServiceImpl::GetInstance()->UnsubscribeBackupRestoreCallback("test"), ERR_OK);
1418 }
1419
1420 } // namespace DevStandbyMgr
1421 } // namespace OHOS
1422