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