1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "ability_foreground_state_observer_proxy.h"
19 #define private public
20 #include "app_state_observer_manager.h"
21 #undef private
22 #include "app_foreground_state_observer_proxy.h"
23 #include "app_foreground_state_observer_stub.h"
24 #include "application_state_observer_stub.h"
25 #include "iapplication_state_observer.h"
26 #include "iremote_broker.h"
27 #include "mock_ability_foreground_state_observer_server_stub.h"
28 #include "mock_i_remote_object.h"
29
30 using namespace testing;
31 using namespace testing::ext;
32
33 namespace OHOS {
34 namespace AppExecFwk {
35 namespace {
36 const int BUNDLE_NAME_LIST_MAX_SIZE = 128;
37 }
38 class MockApplicationStateObserver : public IApplicationStateObserver {
39 public:
40 MockApplicationStateObserver() = default;
41 virtual ~MockApplicationStateObserver() = default;
OnForegroundApplicationChanged(const AppStateData & appStateData)42 void OnForegroundApplicationChanged(const AppStateData &appStateData) override
43 {}
OnAbilityStateChanged(const AbilityStateData & abilityStateData)44 void OnAbilityStateChanged(const AbilityStateData &abilityStateData) override
45 {}
OnExtensionStateChanged(const AbilityStateData & abilityStateData)46 void OnExtensionStateChanged(const AbilityStateData &abilityStateData) override
47 {}
OnProcessCreated(const ProcessData & processData)48 void OnProcessCreated(const ProcessData &processData) override
49 {}
OnProcessStateChanged(const ProcessData & processData)50 void OnProcessStateChanged(const ProcessData &processData) override
51 {}
OnProcessDied(const ProcessData & processData)52 void OnProcessDied(const ProcessData &processData) override
53 {}
OnApplicationStateChanged(const AppStateData & appStateData)54 void OnApplicationStateChanged(const AppStateData &appStateData) override
55 {}
OnAppStateChanged(const AppStateData & appStateData)56 void OnAppStateChanged(const AppStateData &appStateData) override
57 {}
OnAppStarted(const AppStateData & appStateData)58 void OnAppStarted(const AppStateData &appStateData) override
59 {}
OnAppStopped(const AppStateData & appStateData)60 void OnAppStopped(const AppStateData &appStateData) override
61 {}
AsObject()62 sptr<IRemoteObject> AsObject() override
63 {
64 return {};
65 }
66 };
67 class AppForegroundStateObserver : public AppForegroundStateObserverStub {
68 public:
69 AppForegroundStateObserver() = default;
70 virtual ~AppForegroundStateObserver() = default;
OnAppStateChanged(const AppStateData & appStateData)71 void OnAppStateChanged(const AppStateData &appStateData) override
72 {}
73 };
74 class AppSpawnSocketTest : public testing::Test {
75 public:
76 static void SetUpTestCase();
77 static void TearDownTestCase();
78 void SetUp();
79 void TearDown();
80 std::shared_ptr<AppRunningRecord> MockAppRecord();
81 sptr<IApplicationStateObserver> observer_ {nullptr};
82 };
83
SetUpTestCase()84 void AppSpawnSocketTest::SetUpTestCase()
85 {}
86
TearDownTestCase()87 void AppSpawnSocketTest::TearDownTestCase()
88 {}
89
SetUp()90 void AppSpawnSocketTest::SetUp()
91 {
92 sptr<IApplicationStateObserver> observer_ = new MockApplicationStateObserver();
93 }
94
TearDown()95 void AppSpawnSocketTest::TearDown()
96 {}
97
MockAppRecord()98 std::shared_ptr<AppRunningRecord> AppSpawnSocketTest::MockAppRecord()
99 {
100 ApplicationInfo appInfo;
101 appInfo.accessTokenId = 1;
102 std::shared_ptr<ApplicationInfo> info = std::make_shared<ApplicationInfo>(appInfo);
103 info->accessTokenId = 1;
104 std::shared_ptr<AppRunningRecord> appRecord = std::make_shared<AppRunningRecord>(info, 0, "process");
105 std::shared_ptr<PriorityObject> priorityObject = std::make_shared<PriorityObject>();
106 priorityObject->SetPid(1);
107 appRecord->priorityObject_ = priorityObject;
108 appRecord->SetUid(1);
109 appRecord->SetState(ApplicationState::APP_STATE_CREATE);
110 appRecord->SetContinuousTaskAppState(false);
111 appRecord->SetKeepAliveEnableState(false);
112 appRecord->SetEmptyKeepAliveAppState(false);
113 appRecord->SetRequestProcCode(1);
114 appRecord->isFocused_ = false;
115 return appRecord;
116 }
117
118 /*
119 * Feature: AppStateObserverManager
120 * Function: RegisterApplicationStateObserver
121 * SubFunction: NA
122 * FunctionPoints: AppStateObserverManager RegisterApplicationStateObserver
123 * EnvConditions: NA
124 * CaseDescription: Verify RegisterApplicationStateObserver
125 */
126 HWTEST_F(AppSpawnSocketTest, RegisterApplicationStateObserver_001, TestSize.Level0)
127 {
128 auto manager = std::make_shared<AppStateObserverManager>();
129 vector<std::string> bundleNameList;
130 int32_t res = manager->RegisterApplicationStateObserver(nullptr, bundleNameList);
131 EXPECT_EQ(res, ERR_PERMISSION_DENIED);
132 }
133
134 /*
135 * Feature: AppStateObserverManager
136 * Function: RegisterApplicationStateObserver
137 * SubFunction: NA
138 * FunctionPoints: AppStateObserverManager RegisterApplicationStateObserver
139 * EnvConditions: NA
140 * CaseDescription: Verify RegisterApplicationStateObserver
141 */
142 HWTEST_F(AppSpawnSocketTest, RegisterApplicationStateObserver_002, TestSize.Level0)
143 {
144 auto manager = std::make_shared<AppStateObserverManager>();
145 vector<std::string> bundleNameList;
146 while (bundleNameList.size() <= BUNDLE_NAME_LIST_MAX_SIZE) {
147 bundleNameList.push_back("a");
148 }
149 int32_t res = manager->RegisterApplicationStateObserver(nullptr, bundleNameList);
150 EXPECT_EQ(res, ERR_INVALID_VALUE);
151 }
152
153 /*
154 * Feature: AppStateObserverManager
155 * Function: UnregisterApplicationStateObserver
156 * SubFunction: NA
157 * FunctionPoints: AppStateObserverManager UnregisterApplicationStateObserver
158 * EnvConditions: NA
159 * CaseDescription: Verify UnregisterApplicationStateObserver
160 */
161 HWTEST_F(AppSpawnSocketTest, UnregisterApplicationStateObserver_001, TestSize.Level0)
162 {
163 auto manager = std::make_shared<AppStateObserverManager>();
164 int32_t res = manager->UnregisterApplicationStateObserver(nullptr);
165 EXPECT_EQ(res, ERR_PERMISSION_DENIED);
166 }
167
168 /*
169 * Feature: AppStateObserverManager
170 * Function: OnAppStarted
171 * SubFunction: NA
172 * FunctionPoints: AppStateObserverManager OnAppStarted
173 * EnvConditions: NA
174 * CaseDescription: Verify OnAppStarted
175 */
176 HWTEST_F(AppSpawnSocketTest, OnAppStarted_001, TestSize.Level0)
177 {
178 auto manager = std::make_shared<AppStateObserverManager>();
179 ASSERT_NE(manager, nullptr);
180 std::shared_ptr<AppRunningRecord> appRecord;
181 manager->OnAppStarted(appRecord);
182 manager->Init();
183 manager->OnAppStarted(appRecord);
184 }
185
186 /*
187 * Feature: AppStateObserverManager
188 * Function: OnAppStopped
189 * SubFunction: NA
190 * FunctionPoints: AppStateObserverManager OnAppStopped
191 * EnvConditions: NA
192 * CaseDescription: Verify OnAppStopped
193 */
194 HWTEST_F(AppSpawnSocketTest, OnAppStopped_001, TestSize.Level0)
195 {
196 auto manager = std::make_shared<AppStateObserverManager>();
197 ASSERT_NE(manager, nullptr);
198 std::shared_ptr<AppRunningRecord> appRecord;
199 manager->OnAppStopped(appRecord);
200 manager->Init();
201 manager->OnAppStopped(appRecord);
202 }
203
204 /*
205 * Feature: AppStateObserverManager
206 * Function: OnAppStateChanged
207 * SubFunction: NA
208 * FunctionPoints: AppStateObserverManager OnAppStateChanged
209 * EnvConditions: NA
210 * CaseDescription: Verify OnAppStateChanged
211 */
212 HWTEST_F(AppSpawnSocketTest, OnAppStateChanged_001, TestSize.Level0)
213 {
214 auto manager = std::make_shared<AppStateObserverManager>();
215 ASSERT_NE(manager, nullptr);
216 std::shared_ptr<AppRunningRecord> appRecord;
217 ApplicationState state = ApplicationState::APP_STATE_CREATE;
218 bool needNotifyApp = false;
219 manager->OnAppStateChanged(appRecord, state, needNotifyApp, false);
220 }
221
222 /*
223 * Feature: AppStateObserverManager
224 * Function: OnAppStateChanged
225 * SubFunction: NA
226 * FunctionPoints: AppStateObserverManager OnAppStateChanged
227 * EnvConditions: NA
228 * CaseDescription: Verify OnAppStateChanged
229 */
230 HWTEST_F(AppSpawnSocketTest, OnAppStateChanged_002, TestSize.Level0)
231 {
232 auto manager = std::make_shared<AppStateObserverManager>();
233 ASSERT_NE(manager, nullptr);
234 std::shared_ptr<AppRunningRecord> appRecord;
235 ApplicationState state = ApplicationState::APP_STATE_CREATE;
236 bool needNotifyApp = false;
237 manager->Init();
238 manager->OnAppStateChanged(appRecord, state, needNotifyApp, false);
239 }
240
241 /*
242 * Feature: AppStateObserverManager
243 * Function: OnProcessDied
244 * SubFunction: NA
245 * FunctionPoints: AppStateObserverManager OnProcessDied
246 * EnvConditions: NA
247 * CaseDescription: Verify OnProcessDied
248 */
249 HWTEST_F(AppSpawnSocketTest, OnProcessDied_001, TestSize.Level0)
250 {
251 auto manager = std::make_shared<AppStateObserverManager>();
252 ASSERT_NE(manager, nullptr);
253 std::shared_ptr<AppRunningRecord> appRecord;
254 manager->OnProcessDied(appRecord);
255 manager->Init();
256 manager->OnProcessDied(appRecord);
257 }
258
259 /*
260 * Feature: AppStateObserverManager
261 * Function: OnRenderProcessDied
262 * SubFunction: NA
263 * FunctionPoints: AppStateObserverManager OnRenderProcessDied
264 * EnvConditions: NA
265 * CaseDescription: Verify OnRenderProcessDied
266 */
267 HWTEST_F(AppSpawnSocketTest, OnRenderProcessDied_001, TestSize.Level0)
268 {
269 auto manager = std::make_shared<AppStateObserverManager>();
270 ASSERT_NE(manager, nullptr);
271 std::shared_ptr<RenderRecord> renderRecord;
272 manager->OnRenderProcessDied(renderRecord);
273 manager->Init();
274 manager->OnRenderProcessDied(renderRecord);
275 }
276
277 /*
278 * Feature: AppStateObserverManager
279 * Function: OnProcessStateChanged
280 * SubFunction: NA
281 * FunctionPoints: AppStateObserverManager OnProcessStateChanged
282 * EnvConditions: NA
283 * CaseDescription: Verify OnProcessStateChanged
284 */
285 HWTEST_F(AppSpawnSocketTest, OnProcessStateChanged_001, TestSize.Level0)
286 {
287 auto manager = std::make_shared<AppStateObserverManager>();
288 ASSERT_NE(manager, nullptr);
289 std::shared_ptr<AppRunningRecord> appRecord;
290 manager->OnProcessStateChanged(appRecord);
291 manager->Init();
292 manager->OnProcessStateChanged(appRecord);
293 }
294
295 /*
296 * Feature: AppStateObserverManager
297 * Function: OnProcessCreated
298 * SubFunction: NA
299 * FunctionPoints: AppStateObserverManager OnProcessCreated
300 * EnvConditions: NA
301 * CaseDescription: Verify OnProcessCreated
302 */
303 HWTEST_F(AppSpawnSocketTest, OnProcessCreated_001, TestSize.Level0)
304 {
305 auto manager = std::make_shared<AppStateObserverManager>();
306 ASSERT_NE(manager, nullptr);
307 std::shared_ptr<AppRunningRecord> appRecord;
308 manager->OnProcessCreated(appRecord, false);
309 manager->Init();
310 manager->OnProcessCreated(appRecord, false);
311 }
312
313 /*
314 * Feature: AppStateObserverManager
315 * Function: OnRenderProcessCreated
316 * SubFunction: NA
317 * FunctionPoints: AppStateObserverManager OnRenderProcessCreated
318 * EnvConditions: NA
319 * CaseDescription: Verify OnRenderProcessCreated
320 */
321 HWTEST_F(AppSpawnSocketTest, OnRenderProcessCreated_001, TestSize.Level0)
322 {
323 auto manager = std::make_shared<AppStateObserverManager>();
324 ASSERT_NE(manager, nullptr);
325 std::shared_ptr<RenderRecord> renderRecord;
326 manager->OnRenderProcessCreated(renderRecord);
327 manager->Init();
328 manager->OnRenderProcessCreated(renderRecord);
329 }
330
331 /*
332 * Feature: AppStateObserverManager
333 * Function: StateChangedNotifyObserver
334 * SubFunction: NA
335 * FunctionPoints: AppStateObserverManager StateChangedNotifyObserver
336 * EnvConditions: NA
337 * CaseDescription: Verify StateChangedNotifyObserver
338 */
339 HWTEST_F(AppSpawnSocketTest, StateChangedNotifyObserver_001, TestSize.Level0)
340 {
341 auto manager = std::make_shared<AppStateObserverManager>();
342 ASSERT_NE(manager, nullptr);
343 AbilityStateData abilityStateData;
344 bool isAbility = false;
345 manager->StateChangedNotifyObserver(abilityStateData, isAbility, false);
346 manager->Init();
347 manager->StateChangedNotifyObserver(abilityStateData, isAbility, false);
348 }
349
350 /*
351 * Feature: AppStateObserverManager
352 * Function: HandleOnAppStarted
353 * SubFunction: NA
354 * FunctionPoints: AppStateObserverManager HandleOnAppStarted
355 * EnvConditions: NA
356 * CaseDescription: Verify HandleOnAppStarted
357 */
358 HWTEST_F(AppSpawnSocketTest, HandleOnAppStarted_001, TestSize.Level0)
359 {
360 auto manager = std::make_shared<AppStateObserverManager>();
361 ASSERT_NE(manager, nullptr);
362 manager->HandleOnAppStarted(nullptr);
363 std::vector<std::string> bundleNameList;
364 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
365 std::string bundleName = "com.ohos.unittest";
366 appRecord->mainBundleName_ = bundleName;
367 bundleNameList.push_back(bundleName);
368 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
369 manager->HandleOnAppStarted(appRecord);
370 }
371
372 /*
373 * Feature: AppStateObserverManager
374 * Function: HandleOnAppStarted
375 * SubFunction: NA
376 * FunctionPoints: AppStateObserverManager HandleOnAppStarted
377 * EnvConditions: NA
378 * CaseDescription: Verify HandleOnAppStarted
379 */
380 HWTEST_F(AppSpawnSocketTest, HandleOnAppStarted_002, TestSize.Level0)
381 {
382 auto manager = std::make_shared<AppStateObserverManager>();
383 ASSERT_NE(manager, nullptr);
384 std::vector<std::string> bundleNameList;
385 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
386 std::string bundleName = "com.ohos.unittest";
387 appRecord->mainBundleName_ = bundleName;
388 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
389 manager->HandleOnAppStarted(appRecord);
390 }
391
392 /*
393 * Feature: AppStateObserverManager
394 * Function: HandleOnAppStarted
395 * SubFunction: NA
396 * FunctionPoints: AppStateObserverManager HandleOnAppStarted
397 * EnvConditions: NA
398 * CaseDescription: Verify HandleOnAppStarted
399 */
400 HWTEST_F(AppSpawnSocketTest, HandleOnAppStarted_003, TestSize.Level0)
401 {
402 auto manager = std::make_shared<AppStateObserverManager>();
403 ASSERT_NE(manager, nullptr);
404 std::vector<std::string> bundleNameList;
405 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
406 std::string bundleName1 = "com.ohos.unittest1";
407 std::string bundleName2 = "com.ohos.unittest2";
408 appRecord->mainBundleName_ = bundleName1;
409 bundleNameList.push_back(bundleName2);
410 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
411 manager->HandleOnAppStarted(appRecord);
412 }
413
414 /*
415 * Feature: AppStateObserverManager
416 * Function: HandleOnAppStarted
417 * SubFunction: NA
418 * FunctionPoints: AppStateObserverManager HandleOnAppStarted
419 * EnvConditions: NA
420 * CaseDescription: Verify HandleOnAppStarted
421 */
422 HWTEST_F(AppSpawnSocketTest, HandleOnAppStarted_004, TestSize.Level0)
423 {
424 auto manager = std::make_shared<AppStateObserverManager>();
425 ASSERT_NE(manager, nullptr);
426 std::vector<std::string> bundleNameList;
427 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
428 std::string bundleName = "com.ohos.unittest";
429 appRecord->mainBundleName_ = bundleName;
430 bundleNameList.push_back(bundleName);
431 manager->appStateObserverMap_.emplace(nullptr, bundleNameList);
432 manager->HandleOnAppStarted(appRecord);
433 }
434
435 /*
436 * Feature: AppStateObserverManager
437 * Function: HandleOnAppStopped
438 * SubFunction: NA
439 * FunctionPoints: AppStateObserverManager HandleOnAppStopped
440 * EnvConditions: NA
441 * CaseDescription: Verify HandleOnAppStopped
442 */
443 HWTEST_F(AppSpawnSocketTest, HandleOnAppStopped_001, TestSize.Level0)
444 {
445 auto manager = std::make_shared<AppStateObserverManager>();
446 ASSERT_NE(manager, nullptr);
447 manager->HandleOnAppStopped(nullptr);
448 std::vector<std::string> bundleNameList;
449 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
450 std::string bundleName = "com.ohos.unittest";
451 appRecord->mainBundleName_ = bundleName;
452 bundleNameList.push_back(bundleName);
453 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
454 manager->HandleOnAppStopped(appRecord);
455 }
456
457 /*
458 * Feature: AppStateObserverManager
459 * Function: HandleOnAppStopped
460 * SubFunction: NA
461 * FunctionPoints: AppStateObserverManager HandleOnAppStopped
462 * EnvConditions: NA
463 * CaseDescription: Verify HandleOnAppStopped
464 */
465 HWTEST_F(AppSpawnSocketTest, HandleOnAppStopped_002, TestSize.Level0)
466 {
467 auto manager = std::make_shared<AppStateObserverManager>();
468 ASSERT_NE(manager, nullptr);
469 std::vector<std::string> bundleNameList;
470 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
471 std::string bundleName = "com.ohos.unittest";
472 appRecord->mainBundleName_ = bundleName;
473 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
474 manager->HandleOnAppStopped(appRecord);
475 }
476
477 /*
478 * Feature: AppStateObserverManager
479 * Function: HandleOnAppStopped
480 * SubFunction: NA
481 * FunctionPoints: AppStateObserverManager HandleOnAppStopped
482 * EnvConditions: NA
483 * CaseDescription: Verify HandleOnAppStopped
484 */
485 HWTEST_F(AppSpawnSocketTest, HandleOnAppStopped_003, TestSize.Level0)
486 {
487 auto manager = std::make_shared<AppStateObserverManager>();
488 ASSERT_NE(manager, nullptr);
489 std::vector<std::string> bundleNameList;
490 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
491 std::string bundleName1 = "com.ohos.unittest1";
492 std::string bundleName2 = "com.ohos.unittest2";
493 appRecord->mainBundleName_ = bundleName1;
494 bundleNameList.push_back(bundleName2);
495 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
496 manager->HandleOnAppStopped(appRecord);
497 }
498
499 /*
500 * Feature: AppStateObserverManager
501 * Function: HandleOnAppStopped
502 * SubFunction: NA
503 * FunctionPoints: AppStateObserverManager HandleOnAppStopped
504 * EnvConditions: NA
505 * CaseDescription: Verify HandleOnAppStopped
506 */
507 HWTEST_F(AppSpawnSocketTest, HandleOnAppStopped_004, TestSize.Level0)
508 {
509 auto manager = std::make_shared<AppStateObserverManager>();
510 ASSERT_NE(manager, nullptr);
511 std::vector<std::string> bundleNameList;
512 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
513 std::string bundleName = "com.ohos.unittest";
514 appRecord->mainBundleName_ = bundleName;
515 bundleNameList.push_back(bundleName);
516 manager->appStateObserverMap_.emplace(nullptr, bundleNameList);
517 manager->HandleOnAppStopped(appRecord);
518 }
519
520 /*
521 * Feature: AppStateObserverManager
522 * Function: HandleAppStateChanged
523 * SubFunction: NA
524 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
525 * EnvConditions: NA
526 * CaseDescription: Verify HandleAppStateChanged
527 */
528 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_001, TestSize.Level0)
529 {
530 auto manager = std::make_shared<AppStateObserverManager>();
531 ASSERT_NE(manager, nullptr);
532 std::vector<std::string> bundleNameList;
533 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
534 ApplicationState state = ApplicationState::APP_STATE_FOREGROUND;
535 bool needNotifyApp = true;
536 std::string bundleName = "com.ohos.unittest";
537 appRecord->mainBundleName_ = bundleName;
538 bundleNameList.push_back(bundleName);
539 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
540 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
541 }
542
543 /*
544 * Feature: AppStateObserverManager
545 * Function: HandleAppStateChanged
546 * SubFunction: NA
547 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
548 * EnvConditions: NA
549 * CaseDescription: Verify HandleAppStateChanged
550 */
551 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_002, TestSize.Level0)
552 {
553 auto manager = std::make_shared<AppStateObserverManager>();
554 ASSERT_NE(manager, nullptr);
555 std::vector<std::string> bundleNameList;
556 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
557 ApplicationState state = ApplicationState::APP_STATE_BACKGROUND;
558 bool needNotifyApp = false;
559 std::string bundleName = "com.ohos.unittest";
560 appRecord->mainBundleName_ = bundleName;
561 bundleNameList.push_back(bundleName);
562 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
563 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
564 }
565
566 /*
567 * Feature: AppStateObserverManager
568 * Function: HandleAppStateChanged
569 * SubFunction: NA
570 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
571 * EnvConditions: NA
572 * CaseDescription: Verify HandleAppStateChanged
573 */
574 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_003, TestSize.Level0)
575 {
576 auto manager = std::make_shared<AppStateObserverManager>();
577 ASSERT_NE(manager, nullptr);
578 std::vector<std::string> bundleNameList;
579 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
580 ApplicationState state = ApplicationState::APP_STATE_BACKGROUND;
581 bool needNotifyApp = false;
582 std::string bundleName = "com.ohos.unittest";
583 appRecord->mainBundleName_ = bundleName;
584 manager->appStateObserverMap_.emplace(nullptr, bundleNameList);
585 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
586 }
587
588 /*
589 * Feature: AppStateObserverManager
590 * Function: HandleAppStateChanged
591 * SubFunction: NA
592 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
593 * EnvConditions: NA
594 * CaseDescription: Verify HandleAppStateChanged
595 */
596 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_004, TestSize.Level0)
597 {
598 auto manager = std::make_shared<AppStateObserverManager>();
599 ASSERT_NE(manager, nullptr);
600 std::vector<std::string> bundleNameList;
601 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
602 ApplicationState state = ApplicationState::APP_STATE_CREATE;
603 bool needNotifyApp = false;
604 std::string bundleName = "com.ohos.unittest";
605 appRecord->mainBundleName_ = bundleName;
606 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
607 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
608 }
609
610 /*
611 * Feature: AppStateObserverManager
612 * Function: HandleAppStateChanged
613 * SubFunction: NA
614 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
615 * EnvConditions: NA
616 * CaseDescription: Verify HandleAppStateChanged
617 */
618 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_005, TestSize.Level0)
619 {
620 auto manager = std::make_shared<AppStateObserverManager>();
621 ASSERT_NE(manager, nullptr);
622 std::vector<std::string> bundleNameList;
623 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
624 ApplicationState state = ApplicationState::APP_STATE_TERMINATED;
625 bool needNotifyApp = false;
626 std::string bundleName = "com.ohos.unittest";
627 appRecord->mainBundleName_ = bundleName;
628 bundleNameList.push_back(bundleName);
629 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
630 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
631 }
632
633 /*
634 * Feature: AppStateObserverManager
635 * Function: HandleAppStateChanged
636 * SubFunction: NA
637 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
638 * EnvConditions: NA
639 * CaseDescription: Verify HandleAppStateChanged
640 */
641 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_006, TestSize.Level0)
642 {
643 auto manager = std::make_shared<AppStateObserverManager>();
644 ASSERT_NE(manager, nullptr);
645 std::vector<std::string> bundleNameList;
646 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
647 ApplicationState state = ApplicationState::APP_STATE_CREATE;
648 bool needNotifyApp = false;
649 std::string bundleName1 = "com.ohos.unittest1";
650 std::string bundleName2 = "com.ohos.unittest2";
651 appRecord->mainBundleName_ = bundleName1;
652 bundleNameList.push_back(bundleName2);
653 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
654 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
655 }
656
657 /*
658 * Feature: AppStateObserverManager
659 * Function: HandleAppStateChanged
660 * SubFunction: NA
661 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
662 * EnvConditions: NA
663 * CaseDescription: Verify HandleAppStateChanged
664 */
665 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_007, TestSize.Level0)
666 {
667 auto manager = std::make_shared<AppStateObserverManager>();
668 ASSERT_NE(manager, nullptr);
669 std::vector<std::string> bundleNameList;
670 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
671 ApplicationState state = ApplicationState::APP_STATE_CREATE;
672 bool needNotifyApp = false;
673 std::string bundleName = "com.ohos.unittest";
674 appRecord->mainBundleName_ = bundleName;
675 manager->appStateObserverMap_.emplace(nullptr, bundleNameList);
676 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
677 }
678
679 /*
680 * Feature: AppStateObserverManager
681 * Function: HandleAppStateChanged
682 * SubFunction: NA
683 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
684 * EnvConditions: NA
685 * CaseDescription: Verify HandleAppStateChanged
686 */
687 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_008, TestSize.Level0)
688 {
689 auto manager = std::make_shared<AppStateObserverManager>();
690 ASSERT_NE(manager, nullptr);
691 std::vector<std::string> bundleNameList;
692 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
693 ApplicationState state = ApplicationState::APP_STATE_END;
694 bool needNotifyApp = false;
695 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
696 }
697
698 /*
699 * Feature: AppStateObserverManager
700 * Function: HandleStateChangedNotifyObserver
701 * SubFunction: NA
702 * FunctionPoints: AppStateObserverManager HandleStateChangedNotifyObserver
703 * EnvConditions: NA
704 * CaseDescription: Verify HandleStateChangedNotifyObserver
705 */
706 HWTEST_F(AppSpawnSocketTest, HandleStateChangedNotifyObserver_001, TestSize.Level0)
707 {
708 auto manager = std::make_shared<AppStateObserverManager>();
709 ASSERT_NE(manager, nullptr);
710 AbilityStateData abilityStateData;
711 bool isAbility = true;
712 std::vector<std::string> bundleNameList;
713 std::string bundleName = "com.ohos.unittest";
714 abilityStateData.bundleName = bundleName;
715 bundleNameList.push_back(bundleName);
716 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
717 manager->HandleStateChangedNotifyObserver(abilityStateData, isAbility, false);
718 }
719
720 /*
721 * Feature: AppStateObserverManager
722 * Function: HandleStateChangedNotifyObserver
723 * SubFunction: NA
724 * FunctionPoints: AppStateObserverManager HandleStateChangedNotifyObserver
725 * EnvConditions: NA
726 * CaseDescription: Verify HandleStateChangedNotifyObserver
727 */
728 HWTEST_F(AppSpawnSocketTest, HandleStateChangedNotifyObserver_002, TestSize.Level0)
729 {
730 auto manager = std::make_shared<AppStateObserverManager>();
731 ASSERT_NE(manager, nullptr);
732 AbilityStateData abilityStateData;
733 bool isAbility = false;
734 std::vector<std::string> bundleNameList;
735 std::string bundleName = "com.ohos.unittest";
736 abilityStateData.bundleName = bundleName;
737 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
738 manager->HandleStateChangedNotifyObserver(abilityStateData, isAbility, false);
739 }
740
741 /*
742 * Feature: AppStateObserverManager
743 * Function: HandleStateChangedNotifyObserver
744 * SubFunction: NA
745 * FunctionPoints: AppStateObserverManager HandleStateChangedNotifyObserver
746 * EnvConditions: NA
747 * CaseDescription: Verify HandleStateChangedNotifyObserver
748 */
749 HWTEST_F(AppSpawnSocketTest, HandleStateChangedNotifyObserver_003, TestSize.Level0)
750 {
751 auto manager = std::make_shared<AppStateObserverManager>();
752 ASSERT_NE(manager, nullptr);
753 AbilityStateData abilityStateData;
754 bool isAbility = false;
755 std::vector<std::string> bundleNameList;
756 std::string bundleName1 = "com.ohos.unittest1";
757 std::string bundleName2 = "com.ohos.unittest2";
758 abilityStateData.bundleName = bundleName1;
759 bundleNameList.push_back(bundleName2);
760 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
761 manager->HandleStateChangedNotifyObserver(abilityStateData, isAbility, false);
762 }
763
764 /*
765 * Feature: AppStateObserverManager
766 * Function: HandleStateChangedNotifyObserver
767 * SubFunction: NA
768 * FunctionPoints: AppStateObserverManager HandleStateChangedNotifyObserver
769 * EnvConditions: NA
770 * CaseDescription: Verify HandleStateChangedNotifyObserver
771 */
772 HWTEST_F(AppSpawnSocketTest, HandleStateChangedNotifyObserver_004, TestSize.Level0)
773 {
774 auto manager = std::make_shared<AppStateObserverManager>();
775 ASSERT_NE(manager, nullptr);
776 AbilityStateData abilityStateData;
777 bool isAbility = false;
778 std::vector<std::string> bundleNameList;
779 std::string bundleName = "com.ohos.unittest";
780 abilityStateData.bundleName = bundleName;
781 bundleNameList.push_back(bundleName);
782 manager->appStateObserverMap_.emplace(nullptr, bundleNameList);
783 manager->HandleStateChangedNotifyObserver(abilityStateData, isAbility, false);
784 }
785
786 /*
787 * Feature: AppStateObserverManager
788 * Function: HandleOnAppProcessCreated
789 * SubFunction: NA
790 * FunctionPoints: AppStateObserverManager HandleOnAppProcessCreated
791 * EnvConditions: NA
792 * CaseDescription: Verify HandleOnAppProcessCreated
793 */
794 HWTEST_F(AppSpawnSocketTest, HandleOnAppProcessCreated_001, TestSize.Level0)
795 {
796 auto manager = std::make_shared<AppStateObserverManager>();
797 ASSERT_NE(manager, nullptr);
798 manager->HandleOnAppProcessCreated(nullptr, false);
799 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
800 manager->HandleOnAppProcessCreated(appRecord, false);
801 }
802
803 /*
804 * Feature: AppStateObserverManager
805 * Function: HandleOnRenderProcessCreated
806 * SubFunction: NA
807 * FunctionPoints: AppStateObserverManager HandleOnRenderProcessCreated
808 * EnvConditions: NA
809 * CaseDescription: Verify HandleOnRenderProcessCreated
810 */
811 HWTEST_F(AppSpawnSocketTest, HandleOnRenderProcessCreated_001, TestSize.Level0)
812 {
813 auto manager = std::make_shared<AppStateObserverManager>();
814 ASSERT_NE(manager, nullptr);
815 manager->HandleOnRenderProcessCreated(nullptr);
816 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
817 std::shared_ptr<RenderRecord> renderRecord =
818 std::make_shared<RenderRecord>(1, "param", 1, 1, 1, appRecord);
819 renderRecord->SetPid(1);
820 manager->HandleOnRenderProcessCreated(renderRecord);
821 }
822
823 /*
824 * Feature: AppStateObserverManager
825 * Function: HandleOnProcessCreated
826 * SubFunction: NA
827 * FunctionPoints: AppStateObserverManager HandleOnProcessCreated
828 * EnvConditions: NA
829 * CaseDescription: Verify HandleOnProcessCreated
830 */
831 HWTEST_F(AppSpawnSocketTest, HandleOnProcessCreated_001, TestSize.Level0)
832 {
833 auto manager = std::make_shared<AppStateObserverManager>();
834 ASSERT_NE(manager, nullptr);
835 ProcessData data;
836 std::vector<std::string> bundleNameList;
837 std::string bundleName = "com.ohos.unittest";
838 data.bundleName = bundleName;
839 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
840 manager->HandleOnProcessCreated(data);
841 }
842
843 /*
844 * Feature: AppStateObserverManager
845 * Function: HandleOnProcessCreated
846 * SubFunction: NA
847 * FunctionPoints: AppStateObserverManager HandleOnProcessCreated
848 * EnvConditions: NA
849 * CaseDescription: Verify HandleOnProcessCreated
850 */
851 HWTEST_F(AppSpawnSocketTest, HandleOnProcessCreated_002, TestSize.Level0)
852 {
853 auto manager = std::make_shared<AppStateObserverManager>();
854 ASSERT_NE(manager, nullptr);
855 ProcessData data;
856 std::vector<std::string> bundleNameList;
857 std::string bundleName = "com.ohos.unittest";
858 data.bundleName = bundleName;
859 bundleNameList.push_back(bundleName);
860 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
861 manager->HandleOnProcessCreated(data);
862 }
863
864 /*
865 * Feature: AppStateObserverManager
866 * Function: HandleOnProcessCreated
867 * SubFunction: NA
868 * FunctionPoints: AppStateObserverManager HandleOnProcessCreated
869 * EnvConditions: NA
870 * CaseDescription: Verify HandleOnProcessCreated
871 */
872 HWTEST_F(AppSpawnSocketTest, HandleOnProcessCreated_003, TestSize.Level0)
873 {
874 auto manager = std::make_shared<AppStateObserverManager>();
875 ASSERT_NE(manager, nullptr);
876 ProcessData data;
877 std::vector<std::string> bundleNameList;
878 std::string bundleName1 = "com.ohos.unittest";
879 std::string bundleName2 = "com.ohos.unittest";
880 data.bundleName = bundleName1;
881 bundleNameList.push_back(bundleName2);
882 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
883 manager->HandleOnProcessCreated(data);
884 }
885
886 /*
887 * Feature: AppStateObserverManager
888 * Function: HandleOnProcessCreated
889 * SubFunction: NA
890 * FunctionPoints: AppStateObserverManager HandleOnProcessCreated
891 * EnvConditions: NA
892 * CaseDescription: Verify HandleOnProcessCreated
893 */
894 HWTEST_F(AppSpawnSocketTest, HandleOnProcessCreated_004, TestSize.Level0)
895 {
896 auto manager = std::make_shared<AppStateObserverManager>();
897 ASSERT_NE(manager, nullptr);
898 ProcessData data;
899 std::vector<std::string> bundleNameList;
900 std::string bundleName = "com.ohos.unittest";
901 data.bundleName = bundleName;
902 bundleNameList.push_back(bundleName);
903 manager->appStateObserverMap_.emplace(nullptr, bundleNameList);
904 manager->HandleOnProcessCreated(data);
905 }
906
907 /*
908 * Feature: AppStateObserverManager
909 * Function: HandleOnProcessStateChanged
910 * SubFunction: NA
911 * FunctionPoints: AppStateObserverManager HandleOnProcessStateChanged
912 * EnvConditions: NA
913 * CaseDescription: Verify HandleOnProcessStateChanged
914 */
915 HWTEST_F(AppSpawnSocketTest, HandleOnProcessStateChanged_001, TestSize.Level0)
916 {
917 auto manager = std::make_shared<AppStateObserverManager>();
918 ASSERT_NE(manager, nullptr);
919 manager->HandleOnProcessStateChanged(nullptr);
920 }
921
922 /*
923 * Feature: AppStateObserverManager
924 * Function: HandleOnProcessStateChanged
925 * SubFunction: NA
926 * FunctionPoints: AppStateObserverManager HandleOnProcessStateChanged
927 * EnvConditions: NA
928 * CaseDescription: Verify HandleOnProcessStateChanged
929 */
930 HWTEST_F(AppSpawnSocketTest, HandleOnProcessStateChanged_002, TestSize.Level0)
931 {
932 auto manager = std::make_shared<AppStateObserverManager>();
933 ASSERT_NE(manager, nullptr);
934 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
935 std::vector<std::string> bundleNameList;
936 std::string bundleName = "com.ohos.unittest";
937 appRecord->mainBundleName_ = bundleName;
938 bundleNameList.push_back(bundleName);
939 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
940 manager->HandleOnProcessStateChanged(appRecord);
941 }
942
943 /*
944 * Feature: AppStateObserverManager
945 * Function: HandleOnProcessStateChanged
946 * SubFunction: NA
947 * FunctionPoints: AppStateObserverManager HandleOnProcessStateChanged
948 * EnvConditions: NA
949 * CaseDescription: Verify HandleOnProcessStateChanged
950 */
951 HWTEST_F(AppSpawnSocketTest, HandleOnProcessStateChanged_003, TestSize.Level0)
952 {
953 auto manager = std::make_shared<AppStateObserverManager>();
954 ASSERT_NE(manager, nullptr);
955 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
956 std::vector<std::string> bundleNameList;
957 std::string bundleName = "com.ohos.unittest";
958 appRecord->mainBundleName_ = bundleName;
959 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
960 manager->HandleOnProcessStateChanged(appRecord);
961 }
962
963 /*
964 * Feature: AppStateObserverManager
965 * Function: HandleOnProcessStateChanged
966 * SubFunction: NA
967 * FunctionPoints: AppStateObserverManager HandleOnProcessStateChanged
968 * EnvConditions: NA
969 * CaseDescription: Verify HandleOnProcessStateChanged
970 */
971 HWTEST_F(AppSpawnSocketTest, HandleOnProcessStateChanged_004, TestSize.Level0)
972 {
973 auto manager = std::make_shared<AppStateObserverManager>();
974 ASSERT_NE(manager, nullptr);
975 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
976 std::vector<std::string> bundleNameList;
977 std::string bundleName1 = "com.ohos.unittest1";
978 std::string bundleName2 = "com.ohos.unittest2";
979 appRecord->mainBundleName_ = bundleName1;
980 bundleNameList.push_back(bundleName2);
981 manager->appStateObserverMap_.emplace(nullptr, bundleNameList);
982 manager->HandleOnProcessStateChanged(appRecord);
983 }
984
985 /*
986 * Feature: AppStateObserverManager
987 * Function: HandleOnProcessStateChanged
988 * SubFunction: NA
989 * FunctionPoints: AppStateObserverManager HandleOnProcessStateChanged
990 * EnvConditions: NA
991 * CaseDescription: Verify HandleOnProcessStateChanged
992 */
993 HWTEST_F(AppSpawnSocketTest, HandleOnProcessStateChanged_005, TestSize.Level0)
994 {
995 auto manager = std::make_shared<AppStateObserverManager>();
996 ASSERT_NE(manager, nullptr);
997 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
998 std::vector<std::string> bundleNameList;
999 std::string bundleName = "com.ohos.unittest";
1000 appRecord->mainBundleName_ = bundleName;
1001 bundleNameList.push_back(bundleName);
1002 manager->appStateObserverMap_.emplace(nullptr, bundleNameList);
1003 manager->HandleOnProcessStateChanged(appRecord);
1004 }
1005
1006 /*
1007 * Feature: AppStateObserverManager
1008 * Function: HandleOnAppProcessDied
1009 * SubFunction: NA
1010 * FunctionPoints: AppStateObserverManager HandleOnAppProcessDied
1011 * EnvConditions: NA
1012 * CaseDescription: Verify HandleOnAppProcessDied
1013 */
1014 HWTEST_F(AppSpawnSocketTest, HandleOnAppProcessDied_001, TestSize.Level0)
1015 {
1016 auto manager = std::make_shared<AppStateObserverManager>();
1017 ASSERT_NE(manager, nullptr);
1018 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1019 manager->HandleOnAppProcessDied(nullptr);
1020 manager->HandleOnAppProcessDied(appRecord);
1021 }
1022
1023 /*
1024 * Feature: AppStateObserverManager
1025 * Function: HandleOnRenderProcessDied
1026 * SubFunction: NA
1027 * FunctionPoints: AppStateObserverManager HandleOnRenderProcessDied
1028 * EnvConditions: NA
1029 * CaseDescription: Verify HandleOnRenderProcessDied
1030 */
1031 HWTEST_F(AppSpawnSocketTest, HandleOnRenderProcessDied_001, TestSize.Level0)
1032 {
1033 auto manager = std::make_shared<AppStateObserverManager>();
1034 ASSERT_NE(manager, nullptr);
1035 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1036 std::shared_ptr<RenderRecord> renderRecord =
1037 std::make_shared<RenderRecord>(1, "param", 1, 1, 1, appRecord);
1038 renderRecord->SetPid(1);
1039 manager->HandleOnRenderProcessDied(nullptr);
1040 manager->HandleOnRenderProcessDied(renderRecord);
1041 }
1042
1043 /*
1044 * Feature: AppStateObserverManager
1045 * Function: HandleOnProcessDied
1046 * SubFunction: NA
1047 * FunctionPoints: AppStateObserverManager HandleOnProcessDied
1048 * EnvConditions: NA
1049 * CaseDescription: Verify HandleOnProcessDied
1050 */
1051 HWTEST_F(AppSpawnSocketTest, HandleOnProcessDied_001, TestSize.Level0)
1052 {
1053 auto manager = std::make_shared<AppStateObserverManager>();
1054 ASSERT_NE(manager, nullptr);
1055 ProcessData data;
1056 std::vector<std::string> bundleNameList;
1057 std::string bundleName = "com.ohos.unittest";
1058 data.bundleName = bundleName;
1059 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
1060 manager->HandleOnProcessDied(data);
1061 }
1062
1063 /*
1064 * Feature: AppStateObserverManager
1065 * Function: HandleOnProcessDied
1066 * SubFunction: NA
1067 * FunctionPoints: AppStateObserverManager HandleOnProcessDied
1068 * EnvConditions: NA
1069 * CaseDescription: Verify HandleOnProcessDied
1070 */
1071 HWTEST_F(AppSpawnSocketTest, HandleOnProcessDied_002, TestSize.Level0)
1072 {
1073 auto manager = std::make_shared<AppStateObserverManager>();
1074 ASSERT_NE(manager, nullptr);
1075 ProcessData data;
1076 std::vector<std::string> bundleNameList;
1077 std::string bundleName = "com.ohos.unittest";
1078 data.bundleName = bundleName;
1079 bundleNameList.push_back(bundleName);
1080 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
1081 manager->HandleOnProcessDied(data);
1082 }
1083
1084 /*
1085 * Feature: AppStateObserverManager
1086 * Function: HandleOnProcessDied
1087 * SubFunction: NA
1088 * FunctionPoints: AppStateObserverManager HandleOnProcessDied
1089 * EnvConditions: NA
1090 * CaseDescription: Verify HandleOnProcessDied
1091 */
1092 HWTEST_F(AppSpawnSocketTest, HandleOnProcessDied_003, TestSize.Level0)
1093 {
1094 auto manager = std::make_shared<AppStateObserverManager>();
1095 ASSERT_NE(manager, nullptr);
1096 ProcessData data;
1097 std::vector<std::string> bundleNameList;
1098 std::string bundleName1 = "com.ohos.unittest1";
1099 std::string bundleName2 = "com.ohos.unittest2";
1100 data.bundleName = bundleName1;
1101 bundleNameList.push_back(bundleName2);
1102 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
1103 manager->HandleOnProcessDied(data);
1104 }
1105
1106 /*
1107 * Feature: AppStateObserverManager
1108 * Function: HandleOnProcessDied
1109 * SubFunction: NA
1110 * FunctionPoints: AppStateObserverManager HandleOnProcessDied
1111 * EnvConditions: NA
1112 * CaseDescription: Verify HandleOnProcessDied
1113 */
1114 HWTEST_F(AppSpawnSocketTest, HandleOnProcessDied_004, TestSize.Level0)
1115 {
1116 auto manager = std::make_shared<AppStateObserverManager>();
1117 ASSERT_NE(manager, nullptr);
1118 ProcessData data;
1119 std::vector<std::string> bundleNameList;
1120 std::string bundleName1 = "com.ohos.unittest1";
1121 std::string bundleName2 = "com.ohos.unittest2";
1122 data.bundleName = bundleName1;
1123 bundleNameList.push_back(bundleName2);
1124 manager->appStateObserverMap_.emplace(nullptr, bundleNameList);
1125 manager->HandleOnProcessDied(data);
1126 }
1127
1128 /*
1129 * Feature: AppStateObserverManager
1130 * Function: ObserverExist
1131 * SubFunction: NA
1132 * FunctionPoints: AppStateObserverManager ObserverExist
1133 * EnvConditions: NA
1134 * CaseDescription: Verify ObserverExist
1135 */
1136 HWTEST_F(AppSpawnSocketTest, ObserverExist_001, TestSize.Level0)
1137 {
1138 auto manager = std::make_shared<AppStateObserverManager>();
1139 bool res = manager->ObserverExist(nullptr);
1140 EXPECT_FALSE(res);
1141 }
1142
1143 /*
1144 * Feature: AppStateObserverManager
1145 * Function: ObserverExist
1146 * SubFunction: NA
1147 * FunctionPoints: AppStateObserverManager ObserverExist
1148 * EnvConditions: NA
1149 * CaseDescription: Verify ObserverExist
1150 */
1151 HWTEST_F(AppSpawnSocketTest, ObserverExist_002, TestSize.Level0)
1152 {
1153 auto manager = std::make_shared<AppStateObserverManager>();
1154 sptr<IApplicationStateObserver> observer = new MockApplicationStateObserver();
1155 std::vector<std::string> bundleNameList;
1156 manager->appStateObserverMap_.emplace(observer, bundleNameList);
1157 bool res = manager->ObserverExist(observer);
1158 EXPECT_TRUE(res);
1159 }
1160
1161 /**
1162 * @tc.name: RegisterAbilityForegroundStateObserver_0100
1163 * @tc.desc: The test returns when the permission judgment is inconsistent.
1164 * @tc.type: FUNC
1165 */
1166 HWTEST_F(AppSpawnSocketTest, RegisterAbilityForegroundStateObserver_0100, TestSize.Level1)
1167 {
1168 auto manager = std::make_shared<AppStateObserverManager>();
1169 ASSERT_NE(manager, nullptr);
1170 sptr<IAbilityForegroundStateObserver> observer = new AbilityForegroundStateObserverProxy(nullptr);
1171 manager->abilityforegroundObserverSet_.emplace(observer);
1172 auto res = manager->RegisterAbilityForegroundStateObserver(observer);
1173 EXPECT_EQ(res, ERR_PERMISSION_DENIED);
1174 }
1175
1176 /**
1177 * @tc.name: UnregisterAbilityForegroundStateObserver_0100
1178 * @tc.desc: The test returns when the permission judgment is inconsistent.
1179 * @tc.type: FUNC
1180 */
1181 HWTEST_F(AppSpawnSocketTest, UnregisterAbilityForegroundStateObserver_0100, TestSize.Level1)
1182 {
1183 sptr<IAbilityForegroundStateObserver> observer = new AbilityForegroundStateObserverProxy(nullptr);
1184 auto manager = std::make_shared<AppStateObserverManager>();
1185 ASSERT_NE(manager, nullptr);
1186 manager->abilityforegroundObserverSet_.emplace(observer);
1187 auto res = manager->UnregisterAbilityForegroundStateObserver(observer);
1188 EXPECT_EQ(res, ERR_PERMISSION_DENIED);
1189 }
1190
1191 /**
1192 * @tc.name: IsAbilityForegroundObserverExist_0100
1193 * @tc.desc: Test return when abilityforegroundObserverSet_ is not empty and
1194 * the conditions within the loop are met.
1195 * @tc.type: FUNC
1196 */
1197 HWTEST_F(AppSpawnSocketTest, IsAbilityForegroundObserverExist_0100, TestSize.Level1)
1198 {
1199 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(nullptr);
1200 sptr<IAbilityForegroundStateObserver> observers = new AbilityForegroundStateObserverProxy(nullptr);
1201 auto manager = std::make_shared<AppStateObserverManager>();
1202 ASSERT_NE(manager, nullptr);
1203 manager->abilityforegroundObserverSet_.emplace(observers);
1204 auto res = manager->IsAbilityForegroundObserverExist(observer);
1205 EXPECT_EQ(res, true);
1206 }
1207
1208 /**
1209 * @tc.name: AddObserverDeathRecipient_0100
1210 * @tc.desc: Verify that AddObserverDeathRecipient can be called normally(type is APPLICATION_STATE_OBSERVER)
1211 * @tc.type: FUNC
1212 */
1213 HWTEST_F(AppSpawnSocketTest, AddObserverDeathRecipient_0100, TestSize.Level1)
1214 {
1215 auto observerStub = new MockAbilityForegroundStateObserverServerStub();
1216 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(observerStub);
1217 auto manager = std::make_shared<AppStateObserverManager>();
1218 ASSERT_NE(manager, nullptr);
1219 ObserverType type = ObserverType::APPLICATION_STATE_OBSERVER;
1220 manager->AddObserverDeathRecipient(observer, type);
1221 ASSERT_FALSE(manager->recipientMap_.empty());
1222 }
1223
1224 /**
1225 * @tc.name: AddObserverDeathRecipient_0200
1226 * @tc.desc: Verify that AddObserverDeathRecipient can be called normally(type is ABILITY_FOREGROUND_STATE_OBSERVER)
1227 * @tc.type: FUNC
1228 */
1229 HWTEST_F(AppSpawnSocketTest, AddObserverDeathRecipient_0200, TestSize.Level1)
1230 {
1231 auto observerStub = new MockAbilityForegroundStateObserverServerStub();
1232 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(observerStub);
1233 auto manager = std::make_shared<AppStateObserverManager>();
1234 ASSERT_NE(manager, nullptr);
1235 ObserverType type = ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER;
1236 manager->AddObserverDeathRecipient(observer, type);
1237 ASSERT_FALSE(manager->recipientMap_.empty());
1238 }
1239
1240 /**
1241 * @tc.name: AddObserverDeathRecipient_0300
1242 * @tc.desc: Verify that AddObserverDeathRecipient can be called normally(observer is nullptr)
1243 * @tc.type: FUNC
1244 */
1245 HWTEST_F(AppSpawnSocketTest, AddObserverDeathRecipient_0300, TestSize.Level1)
1246 {
1247 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(nullptr);
1248 auto manager = std::make_shared<AppStateObserverManager>();
1249 ASSERT_NE(manager, nullptr);
1250 ObserverType type = ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER;
1251 manager->AddObserverDeathRecipient(observer, type);
1252 ASSERT_TRUE(manager->recipientMap_.empty());
1253 }
1254
1255 /**
1256 * @tc.name: RemoveObserverDeathRecipient_0100
1257 * @tc.desc: Verify that RemoveObserverDeathRecipient can be called normally
1258 * @tc.type: FUNC
1259 */
1260 HWTEST_F(AppSpawnSocketTest, RemoveObserverDeathRecipient_0100, TestSize.Level1)
1261 {
1262 auto observerStub = new MockAbilityForegroundStateObserverServerStub();
1263 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(observerStub);
1264 auto manager = std::make_shared<AppStateObserverManager>();
1265 ASSERT_NE(manager, nullptr);
1266 manager->RemoveObserverDeathRecipient(observer);
1267 ASSERT_TRUE(manager->recipientMap_.empty());
1268 }
1269
1270 /**
1271 * @tc.name: RemoveObserverDeathRecipient_0200
1272 * @tc.desc: Verify that RemoveObserverDeathRecipient can be called normally(observer is nullptr)
1273 * @tc.type: FUNC
1274 */
1275 HWTEST_F(AppSpawnSocketTest, RemoveObserverDeathRecipient_0200, TestSize.Level1)
1276 {
1277 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(nullptr);
1278 auto manager = std::make_shared<AppStateObserverManager>();
1279 ASSERT_NE(manager, nullptr);
1280 manager->RemoveObserverDeathRecipient(observer);
1281 ASSERT_TRUE(manager->recipientMap_.empty());
1282 }
1283
1284 /**
1285 * @tc.name: RegisterAppForegroundStateObserver_0100
1286 * @tc.desc: Test when observer is not nullptr and without permission.
1287 * and observer not exist.
1288 * @tc.type: FUNC
1289 */
1290 HWTEST_F(AppSpawnSocketTest, RegisterAppForegroundStateObserver_0100, TestSize.Level1)
1291 {
1292 auto manager = std::make_shared<AppStateObserverManager>();
1293 sptr<IAppForegroundStateObserver> observer = new (std::nothrow) AppForegroundStateObserver();
1294 auto res = manager->RegisterAppForegroundStateObserver(observer);
1295 EXPECT_EQ(ERR_PERMISSION_DENIED, res);
1296 }
1297
1298 /**
1299 * @tc.name: UnregisterAppForegroundStateObserver_0100
1300 * @tc.desc: Test when observer is not nullptr and without permission.
1301 * @tc.type: FUNC
1302 */
1303 HWTEST_F(AppSpawnSocketTest, UnregisterAppForegroundStateObserver_0100, TestSize.Level1)
1304 {
1305 auto manager = std::make_shared<AppStateObserverManager>();
1306 sptr<IAppForegroundStateObserver> observer = new (std::nothrow) AppForegroundStateObserver();
1307 manager->appForegroundStateObserverSet_.emplace(observer);
1308 auto res = manager->UnregisterAppForegroundStateObserver(observer);
1309 EXPECT_EQ(ERR_PERMISSION_DENIED, res);
1310 }
1311
1312 /**
1313 * @tc.name: IsAppForegroundObserverExist_0100
1314 * @tc.desc: Test when observer and appForegroundStateObserverSet is not nullptr
1315 * and asObject of them is same.
1316 * @tc.type: FUNC
1317 */
1318 HWTEST_F(AppSpawnSocketTest, IsAppForegroundObserverExist_0100, TestSize.Level1)
1319 {
1320 auto manager = std::make_shared<AppStateObserverManager>();
1321 sptr<IAppForegroundStateObserver> observer = new (std::nothrow) AppForegroundStateObserver();
1322 manager->appForegroundStateObserverSet_.emplace(observer);
1323 auto res = manager->IsAppForegroundObserverExist(observer);
1324 EXPECT_EQ(true, res);
1325 }
1326
1327 /**
1328 * @tc.name: OnObserverDied_0100
1329 * @tc.desc: Test when observer is not nullptr and type is APPLICATION_STATE_OBSERVER.
1330 * @tc.type: FUNC
1331 */
1332 HWTEST_F(AppSpawnSocketTest, OnObserverDied_0100, TestSize.Level1)
1333 {
1334 sptr<IRemoteObject> remoteObject = new (std::nothrow) AppForegroundStateObserver();
1335 wptr<IRemoteObject> remote(remoteObject);
1336 ObserverType type = ObserverType::APPLICATION_STATE_OBSERVER;
1337 DelayedSingleton<AppStateObserverManager>::GetInstance()->OnObserverDied(remote, type);
1338 auto appStateObserverMapSize =
1339 DelayedSingleton<AppStateObserverManager>::GetInstance()->appStateObserverMap_.size();
1340 EXPECT_EQ(0, appStateObserverMapSize);
1341 }
1342
1343 /**
1344 * @tc.name: OnObserverDied_0200
1345 * @tc.desc: Test when observer is not nullptr and type is ABILITY_FOREGROUND_STATE_OBSERVER.
1346 * @tc.type: FUNC
1347 */
1348 HWTEST_F(AppSpawnSocketTest, OnObserverDied_0200, TestSize.Level1)
1349 {
1350 sptr<IRemoteObject> remoteObject = new (std::nothrow) AppForegroundStateObserver();
1351 wptr<IRemoteObject> remote(remoteObject);
1352 ObserverType type = ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER;
1353 DelayedSingleton<AppStateObserverManager>::GetInstance()->OnObserverDied(remote, type);
1354 auto abilityforegroundObserverSetSize =
1355 DelayedSingleton<AppStateObserverManager>::GetInstance()->abilityforegroundObserverSet_.size();
1356 EXPECT_EQ(0, abilityforegroundObserverSetSize);
1357 }
1358
1359 /**
1360 * @tc.name: OnObserverDied_0300
1361 * @tc.desc: Test when observer is not nullptr and type is ABILITY_FOREGROUND_STATE_OBSERVER.
1362 * @tc.type: FUNC
1363 */
1364 HWTEST_F(AppSpawnSocketTest, OnObserverDied_0300, TestSize.Level1)
1365 {
1366 sptr<IRemoteObject> remoteObject = new (std::nothrow) AppForegroundStateObserver();
1367 wptr<IRemoteObject> remote(remoteObject);
1368 ObserverType type = ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER;
1369 DelayedSingleton<AppStateObserverManager>::GetInstance()->OnObserverDied(remote, type);
1370 auto appForegroundStateObserverSetSize =
1371 DelayedSingleton<AppStateObserverManager>::GetInstance()->appForegroundStateObserverSet_.size();
1372 EXPECT_EQ(0, appForegroundStateObserverSetSize);
1373 }
1374
1375 /*
1376 * Feature: AppStateObserverManager
1377 * Function: OnAppCacheStateChanged
1378 * SubFunction: NA
1379 * FunctionPoints: AppStateObserverManager OnAppCacheStateChanged
1380 * EnvConditions: NA
1381 * CaseDescription: Verify OnAppCacheStateChanged
1382 */
1383 HWTEST_F(AppSpawnSocketTest, OnAppCacheStateChanged_001, TestSize.Level0)
1384 {
1385 auto manager = std::make_shared<AppStateObserverManager>();
1386 ASSERT_NE(manager, nullptr);
1387 std::shared_ptr<AppRunningRecord> appRecord;
1388 manager->OnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CACHED);
1389 manager->Init();
1390 manager->OnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CACHED);
1391 }
1392
1393 /*
1394 * Feature: AppStateObserverManager
1395 * Function: HandleOnAppCacheStateChanged
1396 * SubFunction: NA
1397 * FunctionPoints: AppStateObserverManager HandleOnAppCacheStateChanged
1398 * EnvConditions: NA
1399 * CaseDescription: Verify HandleOnAppCacheStateChanged
1400 */
1401 HWTEST_F(AppSpawnSocketTest, HandleOnAppCacheStateChanged_001, TestSize.Level0)
1402 {
1403 auto manager = std::make_shared<AppStateObserverManager>();
1404 ASSERT_NE(manager, nullptr);
1405 manager->HandleOnAppCacheStateChanged(nullptr, ApplicationState::APP_STATE_CREATE);
1406 std::vector<std::string> bundleNameList;
1407 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1408 std::string bundleName = "com.ohos.unittest";
1409 appRecord->mainBundleName_ = bundleName;
1410 bundleNameList.push_back(bundleName);
1411 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
1412 manager->HandleOnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CREATE);
1413 }
1414
1415 /*
1416 * Feature: AppStateObserverManager
1417 * Function: HandleOnAppCacheStateChanged
1418 * SubFunction: NA
1419 * FunctionPoints: AppStateObserverManager HandleOnAppCacheStateChanged
1420 * EnvConditions: NA
1421 * CaseDescription: Verify HandleOnAppCacheStateChanged
1422 */
1423 HWTEST_F(AppSpawnSocketTest, HandleOnAppCacheStateChanged_002, TestSize.Level0)
1424 {
1425 auto manager = std::make_shared<AppStateObserverManager>();
1426 ASSERT_NE(manager, nullptr);
1427 std::vector<std::string> bundleNameList;
1428 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1429 std::string bundleName = "com.ohos.unittest";
1430 appRecord->mainBundleName_ = bundleName;
1431 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
1432 manager->HandleOnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CREATE);
1433 }
1434
1435 /*
1436 * Feature: AppStateObserverManager
1437 * Function: HandleOnAppCacheStateChanged
1438 * SubFunction: NA
1439 * FunctionPoints: AppStateObserverManager HandleOnAppCacheStateChanged
1440 * EnvConditions: NA
1441 * CaseDescription: Verify HandleOnAppCacheStateChanged
1442 */
1443 HWTEST_F(AppSpawnSocketTest, HandleOnAppCacheStateChanged_003, TestSize.Level0)
1444 {
1445 auto manager = std::make_shared<AppStateObserverManager>();
1446 ASSERT_NE(manager, nullptr);
1447 std::vector<std::string> bundleNameList;
1448 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1449 std::string bundleName1 = "com.ohos.unittest1";
1450 std::string bundleName2 = "com.ohos.unittest2";
1451 appRecord->mainBundleName_ = bundleName1;
1452 bundleNameList.push_back(bundleName2);
1453 manager->appStateObserverMap_.emplace(observer_, bundleNameList);
1454 manager->HandleOnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CREATE);
1455 }
1456
1457 /*
1458 * Feature: AppStateObserverManager
1459 * Function: HandleOnAppCacheStateChanged
1460 * SubFunction: NA
1461 * FunctionPoints: AppStateObserverManager HandleOnAppCacheStateChanged
1462 * EnvConditions: NA
1463 * CaseDescription: Verify HandleOnAppCacheStateChanged
1464 */
1465 HWTEST_F(AppSpawnSocketTest, HandleOnAppCacheStateChanged_004, TestSize.Level0)
1466 {
1467 auto manager = std::make_shared<AppStateObserverManager>();
1468 ASSERT_NE(manager, nullptr);
1469 std::vector<std::string> bundleNameList;
1470 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1471 std::string bundleName = "com.ohos.unittest";
1472 appRecord->mainBundleName_ = bundleName;
1473 bundleNameList.push_back(bundleName);
1474 manager->appStateObserverMap_.emplace(nullptr, bundleNameList);
1475 manager->HandleOnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CREATE);
1476 }
1477 } // namespace AppExecFwk
1478 } // namespace OHOS
1479