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 MOCK_METHOD(void, OnProcessReused, (const ProcessData &processData), (override));
67 };
68 class AppForegroundStateObserver : public AppForegroundStateObserverStub {
69 public:
70 AppForegroundStateObserver() = default;
71 virtual ~AppForegroundStateObserver() = default;
OnAppStateChanged(const AppStateData & appStateData)72 void OnAppStateChanged(const AppStateData &appStateData) override
73 {}
74 };
75 class AppSpawnSocketTest : public testing::Test {
76 public:
77 static void SetUpTestCase();
78 static void TearDownTestCase();
79 void SetUp();
80 void TearDown();
81 std::shared_ptr<AppRunningRecord> MockAppRecord();
82 sptr<IApplicationStateObserver> observer_ {nullptr};
83 };
84
SetUpTestCase()85 void AppSpawnSocketTest::SetUpTestCase()
86 {}
87
TearDownTestCase()88 void AppSpawnSocketTest::TearDownTestCase()
89 {}
90
SetUp()91 void AppSpawnSocketTest::SetUp()
92 {
93 sptr<IApplicationStateObserver> observer_ = new MockApplicationStateObserver();
94 }
95
TearDown()96 void AppSpawnSocketTest::TearDown()
97 {}
98
MockAppRecord()99 std::shared_ptr<AppRunningRecord> AppSpawnSocketTest::MockAppRecord()
100 {
101 ApplicationInfo appInfo;
102 appInfo.accessTokenId = 1;
103 std::shared_ptr<ApplicationInfo> info = std::make_shared<ApplicationInfo>(appInfo);
104 info->accessTokenId = 1;
105 std::shared_ptr<AppRunningRecord> appRecord = std::make_shared<AppRunningRecord>(info, 0, "process");
106 std::shared_ptr<PriorityObject> priorityObject = std::make_shared<PriorityObject>();
107 priorityObject->SetPid(1);
108 appRecord->priorityObject_ = priorityObject;
109 appRecord->SetUid(1);
110 appRecord->SetState(ApplicationState::APP_STATE_CREATE);
111 appRecord->SetContinuousTaskAppState(false);
112 appRecord->SetKeepAliveEnableState(false);
113 appRecord->SetKeepAliveDkv(false);
114 appRecord->SetEmptyKeepAliveAppState(false);
115 appRecord->SetRequestProcCode(1);
116 appRecord->isFocused_ = false;
117 return appRecord;
118 }
119
120 /*
121 * Feature: AppStateObserverManager
122 * Function: RegisterApplicationStateObserver
123 * SubFunction: NA
124 * FunctionPoints: AppStateObserverManager RegisterApplicationStateObserver
125 * EnvConditions: NA
126 * CaseDescription: Verify RegisterApplicationStateObserver
127 */
128 HWTEST_F(AppSpawnSocketTest, RegisterApplicationStateObserver_001, TestSize.Level0)
129 {
130 auto manager = std::make_shared<AppStateObserverManager>();
131 vector<std::string> bundleNameList;
132 int32_t res = manager->RegisterApplicationStateObserver(nullptr, bundleNameList);
133 EXPECT_EQ(res, ERR_PERMISSION_DENIED);
134 }
135
136 /*
137 * Feature: AppStateObserverManager
138 * Function: RegisterApplicationStateObserver
139 * SubFunction: NA
140 * FunctionPoints: AppStateObserverManager RegisterApplicationStateObserver
141 * EnvConditions: NA
142 * CaseDescription: Verify RegisterApplicationStateObserver
143 */
144 HWTEST_F(AppSpawnSocketTest, RegisterApplicationStateObserver_002, TestSize.Level0)
145 {
146 auto manager = std::make_shared<AppStateObserverManager>();
147 vector<std::string> bundleNameList;
148 while (bundleNameList.size() <= BUNDLE_NAME_LIST_MAX_SIZE) {
149 bundleNameList.push_back("a");
150 }
151 int32_t res = manager->RegisterApplicationStateObserver(nullptr, bundleNameList);
152 EXPECT_EQ(res, ERR_INVALID_VALUE);
153 }
154
155 /*
156 * Feature: AppStateObserverManager
157 * Function: UnregisterApplicationStateObserver
158 * SubFunction: NA
159 * FunctionPoints: AppStateObserverManager UnregisterApplicationStateObserver
160 * EnvConditions: NA
161 * CaseDescription: Verify UnregisterApplicationStateObserver
162 */
163 HWTEST_F(AppSpawnSocketTest, UnregisterApplicationStateObserver_001, TestSize.Level0)
164 {
165 auto manager = std::make_shared<AppStateObserverManager>();
166 int32_t res = manager->UnregisterApplicationStateObserver(nullptr);
167 EXPECT_EQ(res, ERR_PERMISSION_DENIED);
168 }
169
170 /*
171 * Feature: AppStateObserverManager
172 * Function: OnAppStarted
173 * SubFunction: NA
174 * FunctionPoints: AppStateObserverManager OnAppStarted
175 * EnvConditions: NA
176 * CaseDescription: Verify OnAppStarted
177 */
178 HWTEST_F(AppSpawnSocketTest, OnAppStarted_001, TestSize.Level1)
179 {
180 auto manager = std::make_shared<AppStateObserverManager>();
181 ASSERT_NE(manager, nullptr);
182 std::shared_ptr<AppRunningRecord> appRecord;
183 manager->OnAppStarted(appRecord);
184 manager->Init();
185 manager->OnAppStarted(appRecord);
186 }
187
188 /*
189 * Feature: AppStateObserverManager
190 * Function: OnAppStopped
191 * SubFunction: NA
192 * FunctionPoints: AppStateObserverManager OnAppStopped
193 * EnvConditions: NA
194 * CaseDescription: Verify OnAppStopped
195 */
196 HWTEST_F(AppSpawnSocketTest, OnAppStopped_001, TestSize.Level1)
197 {
198 auto manager = std::make_shared<AppStateObserverManager>();
199 ASSERT_NE(manager, nullptr);
200 std::shared_ptr<AppRunningRecord> appRecord;
201 manager->OnAppStopped(appRecord);
202 manager->Init();
203 manager->OnAppStopped(appRecord);
204 }
205
206 /*
207 * Feature: AppStateObserverManager
208 * Function: OnAppStateChanged
209 * SubFunction: NA
210 * FunctionPoints: AppStateObserverManager OnAppStateChanged
211 * EnvConditions: NA
212 * CaseDescription: Verify OnAppStateChanged
213 */
214 HWTEST_F(AppSpawnSocketTest, OnAppStateChanged_001, TestSize.Level1)
215 {
216 auto manager = std::make_shared<AppStateObserverManager>();
217 ASSERT_NE(manager, nullptr);
218 std::shared_ptr<AppRunningRecord> appRecord;
219 ApplicationState state = ApplicationState::APP_STATE_CREATE;
220 bool needNotifyApp = false;
221 manager->OnAppStateChanged(appRecord, state, needNotifyApp, false);
222 }
223
224 /*
225 * Feature: AppStateObserverManager
226 * Function: OnAppStateChanged
227 * SubFunction: NA
228 * FunctionPoints: AppStateObserverManager OnAppStateChanged
229 * EnvConditions: NA
230 * CaseDescription: Verify OnAppStateChanged
231 */
232 HWTEST_F(AppSpawnSocketTest, OnAppStateChanged_002, TestSize.Level1)
233 {
234 auto manager = std::make_shared<AppStateObserverManager>();
235 ASSERT_NE(manager, nullptr);
236 std::shared_ptr<AppRunningRecord> appRecord;
237 ApplicationState state = ApplicationState::APP_STATE_CREATE;
238 bool needNotifyApp = false;
239 manager->Init();
240 manager->OnAppStateChanged(appRecord, state, needNotifyApp, false);
241 }
242
243 /*
244 * Feature: AppStateObserverManager
245 * Function: OnProcessDied
246 * SubFunction: NA
247 * FunctionPoints: AppStateObserverManager OnProcessDied
248 * EnvConditions: NA
249 * CaseDescription: Verify OnProcessDied
250 */
251 HWTEST_F(AppSpawnSocketTest, OnProcessDied_001, TestSize.Level1)
252 {
253 auto manager = std::make_shared<AppStateObserverManager>();
254 ASSERT_NE(manager, nullptr);
255 std::shared_ptr<AppRunningRecord> appRecord;
256 manager->OnProcessDied(appRecord);
257 manager->Init();
258 manager->OnProcessDied(appRecord);
259 }
260
261 /*
262 * Feature: AppStateObserverManager
263 * Function: OnRenderProcessDied
264 * SubFunction: NA
265 * FunctionPoints: AppStateObserverManager OnRenderProcessDied
266 * EnvConditions: NA
267 * CaseDescription: Verify OnRenderProcessDied
268 */
269 HWTEST_F(AppSpawnSocketTest, OnRenderProcessDied_001, TestSize.Level2)
270 {
271 auto manager = std::make_shared<AppStateObserverManager>();
272 ASSERT_NE(manager, nullptr);
273 std::shared_ptr<RenderRecord> renderRecord;
274 manager->OnRenderProcessDied(renderRecord);
275 manager->Init();
276 manager->OnRenderProcessDied(renderRecord);
277 }
278
279 /*
280 * Feature: AppStateObserverManager
281 * Function: OnProcessStateChanged
282 * SubFunction: NA
283 * FunctionPoints: AppStateObserverManager OnProcessStateChanged
284 * EnvConditions: NA
285 * CaseDescription: Verify OnProcessStateChanged
286 */
287 HWTEST_F(AppSpawnSocketTest, OnProcessStateChanged_001, TestSize.Level2)
288 {
289 auto manager = std::make_shared<AppStateObserverManager>();
290 ASSERT_NE(manager, nullptr);
291 std::shared_ptr<AppRunningRecord> appRecord;
292 manager->OnProcessStateChanged(appRecord);
293 manager->Init();
294 manager->OnProcessStateChanged(appRecord);
295 }
296
297 /*
298 * Feature: AppStateObserverManager
299 * Function: OnProcessCreated
300 * SubFunction: NA
301 * FunctionPoints: AppStateObserverManager OnProcessCreated
302 * EnvConditions: NA
303 * CaseDescription: Verify OnProcessCreated
304 */
305 HWTEST_F(AppSpawnSocketTest, OnProcessCreated_001, TestSize.Level2)
306 {
307 auto manager = std::make_shared<AppStateObserverManager>();
308 ASSERT_NE(manager, nullptr);
309 std::shared_ptr<AppRunningRecord> appRecord;
310 manager->OnProcessCreated(appRecord, false);
311 manager->Init();
312 manager->OnProcessCreated(appRecord, false);
313 }
314
315 /*
316 * Feature: AppStateObserverManager
317 * Function: OnWindowShow
318 * SubFunction: NA
319 * FunctionPoints: AppStateObserverManager OnWindowShow
320 * EnvConditions: NA
321 * CaseDescription: Verify OnWindowShow
322 */
323 HWTEST_F(AppSpawnSocketTest, OnWindowShow_001, TestSize.Level1)
324 {
325 auto manager = std::make_shared<AppStateObserverManager>();
326 ASSERT_NE(manager, nullptr);
327 std::shared_ptr<AppRunningRecord> appRecord;
328 manager->OnWindowShow(appRecord);
329 manager->Init();
330 manager->OnWindowShow(appRecord);
331 }
332
333 /*
334 * Feature: AppStateObserverManager
335 * Function: OnWindowHidden
336 * SubFunction: NA
337 * FunctionPoints: AppStateObserverManager OnWindowHidden
338 * EnvConditions: NA
339 * CaseDescription: Verify OnWindowHidden
340 */
341 HWTEST_F(AppSpawnSocketTest, OnWindowHidden_001, TestSize.Level1)
342 {
343 auto manager = std::make_shared<AppStateObserverManager>();
344 ASSERT_NE(manager, nullptr);
345 std::shared_ptr<AppRunningRecord> appRecord;
346 manager->OnWindowHidden(appRecord);
347 manager->Init();
348 manager->OnWindowHidden(appRecord);
349 }
350
351 /*
352 * Feature: AppStateObserverManager
353 * Function: HandleOnWindowShow
354 * SubFunction: NA
355 * FunctionPoints: AppStateObserverManager HandleOnWindowShow
356 * EnvConditions: NA
357 * CaseDescription: Verify HandleOnWindowShow
358 */
359 HWTEST_F(AppSpawnSocketTest, HandleOnWindowShow_001, TestSize.Level2)
360 {
361 auto manager = std::make_shared<AppStateObserverManager>();
362 ASSERT_NE(manager, nullptr);
363 manager->HandleOnWindowShow(nullptr);
364 }
365
366 /*
367 * Feature: AppStateObserverManager
368 * Function: HandleOnWindowShow
369 * SubFunction: NA
370 * FunctionPoints: AppStateObserverManager HandleOnWindowShow
371 * EnvConditions: NA
372 * CaseDescription: Verify HandleOnWindowShow
373 */
374 HWTEST_F(AppSpawnSocketTest, HandleOnWindowShow_002, TestSize.Level2)
375 {
376 auto manager = std::make_shared<AppStateObserverManager>();
377 ASSERT_NE(manager, nullptr);
378 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
379 std::vector<std::string> bundleNameList;
380 std::string bundleName = "com.ohos.unittest";
381 appRecord->mainBundleName_ = bundleName;
382 bundleNameList.push_back(bundleName);
383 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
384 manager->HandleOnWindowShow(appRecord);
385 }
386
387 /*
388 * Feature: AppStateObserverManager
389 * Function: HandleOnWindowHidden
390 * SubFunction: NA
391 * FunctionPoints: AppStateObserverManager HandleOnWindowHidden
392 * EnvConditions: NA
393 * CaseDescription: Verify HandleOnWindowHidden
394 */
395 HWTEST_F(AppSpawnSocketTest, HandleOnWindowHidden_001, TestSize.Level2)
396 {
397 auto manager = std::make_shared<AppStateObserverManager>();
398 ASSERT_NE(manager, nullptr);
399 manager->HandleOnWindowHidden(nullptr);
400 }
401
402 /*
403 * Feature: AppStateObserverManager
404 * Function: HandleOnWindowHidden
405 * SubFunction: NA
406 * FunctionPoints: AppStateObserverManager HandleOnWindowHidden
407 * EnvConditions: NA
408 * CaseDescription: Verify HandleOnWindowHidden
409 */
410 HWTEST_F(AppSpawnSocketTest, HandleOnWindowHidden_002, TestSize.Level2)
411 {
412 auto manager = std::make_shared<AppStateObserverManager>();
413 ASSERT_NE(manager, nullptr);
414 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
415 std::vector<std::string> bundleNameList;
416 std::string bundleName = "com.ohos.unittest";
417 appRecord->mainBundleName_ = bundleName;
418 bundleNameList.push_back(bundleName);
419 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
420 manager->HandleOnWindowHidden(appRecord);
421 }
422
423 /*
424 * Feature: AppStateObserverManager
425 * Function: OnRenderProcessCreated
426 * SubFunction: NA
427 * FunctionPoints: AppStateObserverManager OnRenderProcessCreated
428 * EnvConditions: NA
429 * CaseDescription: Verify OnRenderProcessCreated
430 */
431 HWTEST_F(AppSpawnSocketTest, OnRenderProcessCreated_001, TestSize.Level2)
432 {
433 auto manager = std::make_shared<AppStateObserverManager>();
434 ASSERT_NE(manager, nullptr);
435 std::shared_ptr<RenderRecord> renderRecord;
436 manager->OnRenderProcessCreated(renderRecord, false);
437 manager->Init();
438 manager->OnRenderProcessCreated(renderRecord, false);
439 }
440
441 /*
442 * Feature: AppStateObserverManager
443 * Function: StateChangedNotifyObserver
444 * SubFunction: NA
445 * FunctionPoints: AppStateObserverManager StateChangedNotifyObserver
446 * EnvConditions: NA
447 * CaseDescription: Verify StateChangedNotifyObserver
448 */
449 HWTEST_F(AppSpawnSocketTest, StateChangedNotifyObserver_001, TestSize.Level2)
450 {
451 auto manager = std::make_shared<AppStateObserverManager>();
452 ASSERT_NE(manager, nullptr);
453 AbilityStateData abilityStateData;
454 bool isAbility = false;
455 manager->StateChangedNotifyObserver(abilityStateData, isAbility, false);
456 manager->Init();
457 manager->StateChangedNotifyObserver(abilityStateData, isAbility, false);
458 }
459
460 /*
461 * Feature: AppStateObserverManager
462 * Function: HandleOnAppStarted
463 * SubFunction: NA
464 * FunctionPoints: AppStateObserverManager HandleOnAppStarted
465 * EnvConditions: NA
466 * CaseDescription: Verify HandleOnAppStarted
467 */
468 HWTEST_F(AppSpawnSocketTest, HandleOnAppStarted_001, TestSize.Level2)
469 {
470 auto manager = std::make_shared<AppStateObserverManager>();
471 ASSERT_NE(manager, nullptr);
472 manager->HandleOnAppStarted(nullptr);
473 std::vector<std::string> bundleNameList;
474 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
475 std::string bundleName = "com.ohos.unittest";
476 appRecord->mainBundleName_ = bundleName;
477 bundleNameList.push_back(bundleName);
478 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
479 manager->HandleOnAppStarted(appRecord);
480 }
481
482 /*
483 * Feature: AppStateObserverManager
484 * Function: HandleOnAppStarted
485 * SubFunction: NA
486 * FunctionPoints: AppStateObserverManager HandleOnAppStarted
487 * EnvConditions: NA
488 * CaseDescription: Verify HandleOnAppStarted
489 */
490 HWTEST_F(AppSpawnSocketTest, HandleOnAppStarted_002, TestSize.Level2)
491 {
492 auto manager = std::make_shared<AppStateObserverManager>();
493 ASSERT_NE(manager, nullptr);
494 std::vector<std::string> bundleNameList;
495 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
496 std::string bundleName = "com.ohos.unittest";
497 appRecord->mainBundleName_ = bundleName;
498 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
499 manager->HandleOnAppStarted(appRecord);
500 }
501
502 /*
503 * Feature: AppStateObserverManager
504 * Function: HandleOnAppStarted
505 * SubFunction: NA
506 * FunctionPoints: AppStateObserverManager HandleOnAppStarted
507 * EnvConditions: NA
508 * CaseDescription: Verify HandleOnAppStarted
509 */
510 HWTEST_F(AppSpawnSocketTest, HandleOnAppStarted_003, TestSize.Level2)
511 {
512 auto manager = std::make_shared<AppStateObserverManager>();
513 ASSERT_NE(manager, nullptr);
514 std::vector<std::string> bundleNameList;
515 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
516 std::string bundleName1 = "com.ohos.unittest1";
517 std::string bundleName2 = "com.ohos.unittest2";
518 appRecord->mainBundleName_ = bundleName1;
519 bundleNameList.push_back(bundleName2);
520 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
521 manager->HandleOnAppStarted(appRecord);
522 }
523
524 /*
525 * Feature: AppStateObserverManager
526 * Function: HandleOnAppStarted
527 * SubFunction: NA
528 * FunctionPoints: AppStateObserverManager HandleOnAppStarted
529 * EnvConditions: NA
530 * CaseDescription: Verify HandleOnAppStarted
531 */
532 HWTEST_F(AppSpawnSocketTest, HandleOnAppStarted_004, TestSize.Level2)
533 {
534 auto manager = std::make_shared<AppStateObserverManager>();
535 ASSERT_NE(manager, nullptr);
536 std::vector<std::string> bundleNameList;
537 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
538 std::string bundleName = "com.ohos.unittest";
539 appRecord->mainBundleName_ = bundleName;
540 bundleNameList.push_back(bundleName);
541 manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
542 manager->HandleOnAppStarted(appRecord);
543 }
544
545 /*
546 * Feature: AppStateObserverManager
547 * Function: HandleOnAppStopped
548 * SubFunction: NA
549 * FunctionPoints: AppStateObserverManager HandleOnAppStopped
550 * EnvConditions: NA
551 * CaseDescription: Verify HandleOnAppStopped
552 */
553 HWTEST_F(AppSpawnSocketTest, HandleOnAppStopped_001, TestSize.Level2)
554 {
555 auto manager = std::make_shared<AppStateObserverManager>();
556 ASSERT_NE(manager, nullptr);
557 manager->HandleOnAppStopped(nullptr);
558 std::vector<std::string> bundleNameList;
559 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
560 std::string bundleName = "com.ohos.unittest";
561 appRecord->mainBundleName_ = bundleName;
562 bundleNameList.push_back(bundleName);
563 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
564 manager->HandleOnAppStopped(appRecord);
565 }
566
567 /*
568 * Feature: AppStateObserverManager
569 * Function: HandleOnAppStopped
570 * SubFunction: NA
571 * FunctionPoints: AppStateObserverManager HandleOnAppStopped
572 * EnvConditions: NA
573 * CaseDescription: Verify HandleOnAppStopped
574 */
575 HWTEST_F(AppSpawnSocketTest, HandleOnAppStopped_002, TestSize.Level2)
576 {
577 auto manager = std::make_shared<AppStateObserverManager>();
578 ASSERT_NE(manager, nullptr);
579 std::vector<std::string> bundleNameList;
580 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
581 std::string bundleName = "com.ohos.unittest";
582 appRecord->mainBundleName_ = bundleName;
583 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
584 manager->HandleOnAppStopped(appRecord);
585 }
586
587 /*
588 * Feature: AppStateObserverManager
589 * Function: HandleOnAppStopped
590 * SubFunction: NA
591 * FunctionPoints: AppStateObserverManager HandleOnAppStopped
592 * EnvConditions: NA
593 * CaseDescription: Verify HandleOnAppStopped
594 */
595 HWTEST_F(AppSpawnSocketTest, HandleOnAppStopped_003, TestSize.Level2)
596 {
597 auto manager = std::make_shared<AppStateObserverManager>();
598 ASSERT_NE(manager, nullptr);
599 std::vector<std::string> bundleNameList;
600 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
601 std::string bundleName1 = "com.ohos.unittest1";
602 std::string bundleName2 = "com.ohos.unittest2";
603 appRecord->mainBundleName_ = bundleName1;
604 bundleNameList.push_back(bundleName2);
605 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
606 manager->HandleOnAppStopped(appRecord);
607 }
608
609 /*
610 * Feature: AppStateObserverManager
611 * Function: HandleOnAppStopped
612 * SubFunction: NA
613 * FunctionPoints: AppStateObserverManager HandleOnAppStopped
614 * EnvConditions: NA
615 * CaseDescription: Verify HandleOnAppStopped
616 */
617 HWTEST_F(AppSpawnSocketTest, HandleOnAppStopped_004, TestSize.Level2)
618 {
619 auto manager = std::make_shared<AppStateObserverManager>();
620 ASSERT_NE(manager, nullptr);
621 std::vector<std::string> bundleNameList;
622 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
623 std::string bundleName = "com.ohos.unittest";
624 appRecord->mainBundleName_ = bundleName;
625 bundleNameList.push_back(bundleName);
626 manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
627 manager->HandleOnAppStopped(appRecord);
628 }
629
630 /*
631 * Feature: AppStateObserverManager
632 * Function: HandleAppStateChanged
633 * SubFunction: NA
634 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
635 * EnvConditions: NA
636 * CaseDescription: Verify HandleAppStateChanged
637 */
638 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_001, TestSize.Level2)
639 {
640 auto manager = std::make_shared<AppStateObserverManager>();
641 ASSERT_NE(manager, nullptr);
642 std::vector<std::string> bundleNameList;
643 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
644 ApplicationState state = ApplicationState::APP_STATE_FOREGROUND;
645 bool needNotifyApp = true;
646 std::string bundleName = "com.ohos.unittest";
647 appRecord->mainBundleName_ = bundleName;
648 bundleNameList.push_back(bundleName);
649 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
650 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
651 }
652
653 /*
654 * Feature: AppStateObserverManager
655 * Function: HandleAppStateChanged
656 * SubFunction: NA
657 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
658 * EnvConditions: NA
659 * CaseDescription: Verify HandleAppStateChanged
660 */
661 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_002, TestSize.Level2)
662 {
663 auto manager = std::make_shared<AppStateObserverManager>();
664 ASSERT_NE(manager, nullptr);
665 std::vector<std::string> bundleNameList;
666 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
667 ApplicationState state = ApplicationState::APP_STATE_BACKGROUND;
668 bool needNotifyApp = false;
669 std::string bundleName = "com.ohos.unittest";
670 appRecord->mainBundleName_ = bundleName;
671 bundleNameList.push_back(bundleName);
672 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
673 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
674 }
675
676 /*
677 * Feature: AppStateObserverManager
678 * Function: HandleAppStateChanged
679 * SubFunction: NA
680 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
681 * EnvConditions: NA
682 * CaseDescription: Verify HandleAppStateChanged
683 */
684 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_003, TestSize.Level2)
685 {
686 auto manager = std::make_shared<AppStateObserverManager>();
687 ASSERT_NE(manager, nullptr);
688 std::vector<std::string> bundleNameList;
689 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
690 ApplicationState state = ApplicationState::APP_STATE_BACKGROUND;
691 bool needNotifyApp = false;
692 std::string bundleName = "com.ohos.unittest";
693 appRecord->mainBundleName_ = bundleName;
694 manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
695 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
696 }
697
698 /*
699 * Feature: AppStateObserverManager
700 * Function: HandleAppStateChanged
701 * SubFunction: NA
702 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
703 * EnvConditions: NA
704 * CaseDescription: Verify HandleAppStateChanged
705 */
706 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_004, TestSize.Level2)
707 {
708 auto manager = std::make_shared<AppStateObserverManager>();
709 ASSERT_NE(manager, nullptr);
710 std::vector<std::string> bundleNameList;
711 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
712 ApplicationState state = ApplicationState::APP_STATE_CREATE;
713 bool needNotifyApp = false;
714 std::string bundleName = "com.ohos.unittest";
715 appRecord->mainBundleName_ = bundleName;
716 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
717 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
718 }
719
720 /*
721 * Feature: AppStateObserverManager
722 * Function: HandleAppStateChanged
723 * SubFunction: NA
724 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
725 * EnvConditions: NA
726 * CaseDescription: Verify HandleAppStateChanged
727 */
728 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_005, TestSize.Level2)
729 {
730 auto manager = std::make_shared<AppStateObserverManager>();
731 ASSERT_NE(manager, nullptr);
732 std::vector<std::string> bundleNameList;
733 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
734 ApplicationState state = ApplicationState::APP_STATE_TERMINATED;
735 bool needNotifyApp = false;
736 std::string bundleName = "com.ohos.unittest";
737 appRecord->mainBundleName_ = bundleName;
738 bundleNameList.push_back(bundleName);
739 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
740 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
741 }
742
743 /*
744 * Feature: AppStateObserverManager
745 * Function: HandleAppStateChanged
746 * SubFunction: NA
747 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
748 * EnvConditions: NA
749 * CaseDescription: Verify HandleAppStateChanged
750 */
751 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_006, TestSize.Level2)
752 {
753 auto manager = std::make_shared<AppStateObserverManager>();
754 ASSERT_NE(manager, nullptr);
755 std::vector<std::string> bundleNameList;
756 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
757 ApplicationState state = ApplicationState::APP_STATE_CREATE;
758 bool needNotifyApp = false;
759 std::string bundleName1 = "com.ohos.unittest1";
760 std::string bundleName2 = "com.ohos.unittest2";
761 appRecord->mainBundleName_ = bundleName1;
762 bundleNameList.push_back(bundleName2);
763 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
764 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
765 }
766
767 /*
768 * Feature: AppStateObserverManager
769 * Function: HandleAppStateChanged
770 * SubFunction: NA
771 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
772 * EnvConditions: NA
773 * CaseDescription: Verify HandleAppStateChanged
774 */
775 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_007, TestSize.Level2)
776 {
777 auto manager = std::make_shared<AppStateObserverManager>();
778 ASSERT_NE(manager, nullptr);
779 std::vector<std::string> bundleNameList;
780 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
781 ApplicationState state = ApplicationState::APP_STATE_CREATE;
782 bool needNotifyApp = false;
783 std::string bundleName = "com.ohos.unittest";
784 appRecord->mainBundleName_ = bundleName;
785 manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
786 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
787 }
788
789 /*
790 * Feature: AppStateObserverManager
791 * Function: HandleAppStateChanged
792 * SubFunction: NA
793 * FunctionPoints: AppStateObserverManager HandleAppStateChanged
794 * EnvConditions: NA
795 * CaseDescription: Verify HandleAppStateChanged
796 */
797 HWTEST_F(AppSpawnSocketTest, HandleAppStateChanged_008, TestSize.Level2)
798 {
799 auto manager = std::make_shared<AppStateObserverManager>();
800 ASSERT_NE(manager, nullptr);
801 std::vector<std::string> bundleNameList;
802 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
803 ApplicationState state = ApplicationState::APP_STATE_END;
804 bool needNotifyApp = false;
805 manager->HandleAppStateChanged(appRecord, state, needNotifyApp, false);
806 }
807
808 /*
809 * Feature: AppStateObserverManager
810 * Function: HandleStateChangedNotifyObserver
811 * SubFunction: NA
812 * FunctionPoints: AppStateObserverManager HandleStateChangedNotifyObserver
813 * EnvConditions: NA
814 * CaseDescription: Verify HandleStateChangedNotifyObserver
815 */
816 HWTEST_F(AppSpawnSocketTest, HandleStateChangedNotifyObserver_001, TestSize.Level2)
817 {
818 auto manager = std::make_shared<AppStateObserverManager>();
819 ASSERT_NE(manager, nullptr);
820 AbilityStateData abilityStateData;
821 bool isAbility = true;
822 std::vector<std::string> bundleNameList;
823 std::string bundleName = "com.ohos.unittest";
824 abilityStateData.bundleName = bundleName;
825 bundleNameList.push_back(bundleName);
826 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
827 manager->HandleStateChangedNotifyObserver(abilityStateData, isAbility, false);
828 }
829
830 /*
831 * Feature: AppStateObserverManager
832 * Function: HandleStateChangedNotifyObserver
833 * SubFunction: NA
834 * FunctionPoints: AppStateObserverManager HandleStateChangedNotifyObserver
835 * EnvConditions: NA
836 * CaseDescription: Verify HandleStateChangedNotifyObserver
837 */
838 HWTEST_F(AppSpawnSocketTest, HandleStateChangedNotifyObserver_002, TestSize.Level2)
839 {
840 auto manager = std::make_shared<AppStateObserverManager>();
841 ASSERT_NE(manager, nullptr);
842 AbilityStateData abilityStateData;
843 bool isAbility = false;
844 std::vector<std::string> bundleNameList;
845 std::string bundleName = "com.ohos.unittest";
846 abilityStateData.bundleName = bundleName;
847 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
848 manager->HandleStateChangedNotifyObserver(abilityStateData, isAbility, false);
849 }
850
851 /*
852 * Feature: AppStateObserverManager
853 * Function: HandleStateChangedNotifyObserver
854 * SubFunction: NA
855 * FunctionPoints: AppStateObserverManager HandleStateChangedNotifyObserver
856 * EnvConditions: NA
857 * CaseDescription: Verify HandleStateChangedNotifyObserver
858 */
859 HWTEST_F(AppSpawnSocketTest, HandleStateChangedNotifyObserver_003, TestSize.Level2)
860 {
861 auto manager = std::make_shared<AppStateObserverManager>();
862 ASSERT_NE(manager, nullptr);
863 AbilityStateData abilityStateData;
864 bool isAbility = false;
865 std::vector<std::string> bundleNameList;
866 std::string bundleName1 = "com.ohos.unittest1";
867 std::string bundleName2 = "com.ohos.unittest2";
868 abilityStateData.bundleName = bundleName1;
869 bundleNameList.push_back(bundleName2);
870 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
871 manager->HandleStateChangedNotifyObserver(abilityStateData, isAbility, false);
872 }
873
874 /*
875 * Feature: AppStateObserverManager
876 * Function: HandleStateChangedNotifyObserver
877 * SubFunction: NA
878 * FunctionPoints: AppStateObserverManager HandleStateChangedNotifyObserver
879 * EnvConditions: NA
880 * CaseDescription: Verify HandleStateChangedNotifyObserver
881 */
882 HWTEST_F(AppSpawnSocketTest, HandleStateChangedNotifyObserver_004, TestSize.Level2)
883 {
884 auto manager = std::make_shared<AppStateObserverManager>();
885 ASSERT_NE(manager, nullptr);
886 AbilityStateData abilityStateData;
887 bool isAbility = false;
888 std::vector<std::string> bundleNameList;
889 std::string bundleName = "com.ohos.unittest";
890 abilityStateData.bundleName = bundleName;
891 bundleNameList.push_back(bundleName);
892 manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
893 manager->HandleStateChangedNotifyObserver(abilityStateData, isAbility, false);
894 }
895
896 /*
897 * Feature: AppStateObserverManager
898 * Function: HandleOnAppProcessCreated
899 * SubFunction: NA
900 * FunctionPoints: AppStateObserverManager HandleOnAppProcessCreated
901 * EnvConditions: NA
902 * CaseDescription: Verify HandleOnAppProcessCreated
903 */
904 HWTEST_F(AppSpawnSocketTest, HandleOnAppProcessCreated_001, TestSize.Level2)
905 {
906 auto manager = std::make_shared<AppStateObserverManager>();
907 ASSERT_NE(manager, nullptr);
908 manager->HandleOnAppProcessCreated(nullptr, false);
909 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
910 manager->HandleOnAppProcessCreated(appRecord, false);
911 }
912
913 /*
914 * Feature: AppStateObserverManager
915 * Function: HandleOnRenderProcessCreated
916 * SubFunction: NA
917 * FunctionPoints: AppStateObserverManager HandleOnRenderProcessCreated
918 * EnvConditions: NA
919 * CaseDescription: Verify HandleOnRenderProcessCreated
920 */
921 HWTEST_F(AppSpawnSocketTest, HandleOnRenderProcessCreated_001, TestSize.Level2)
922 {
923 auto manager = std::make_shared<AppStateObserverManager>();
924 ASSERT_NE(manager, nullptr);
925 manager->HandleOnRenderProcessCreated(nullptr, false);
926 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
927 std::shared_ptr<RenderRecord> renderRecord =
928 std::make_shared<RenderRecord>(1, "param", FdGuard(-1), FdGuard(-1), FdGuard(-1), appRecord);
929 renderRecord->SetPid(1);
930 manager->HandleOnRenderProcessCreated(renderRecord, false);
931 }
932
933 /*
934 * Feature: AppStateObserverManager
935 * Function: HandleOnProcessCreated
936 * SubFunction: NA
937 * FunctionPoints: AppStateObserverManager HandleOnProcessCreated
938 * EnvConditions: NA
939 * CaseDescription: Verify HandleOnProcessCreated
940 */
941 HWTEST_F(AppSpawnSocketTest, HandleOnProcessCreated_001, TestSize.Level2)
942 {
943 auto manager = std::make_shared<AppStateObserverManager>();
944 ASSERT_NE(manager, nullptr);
945 ProcessData data;
946 std::vector<std::string> bundleNameList;
947 std::string bundleName = "com.ohos.unittest";
948 data.bundleName = bundleName;
949 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
950 manager->HandleOnProcessCreated(data);
951 }
952
953 /*
954 * Feature: AppStateObserverManager
955 * Function: HandleOnProcessCreated
956 * SubFunction: NA
957 * FunctionPoints: AppStateObserverManager HandleOnProcessCreated
958 * EnvConditions: NA
959 * CaseDescription: Verify HandleOnProcessCreated
960 */
961 HWTEST_F(AppSpawnSocketTest, HandleOnProcessCreated_002, TestSize.Level2)
962 {
963 auto manager = std::make_shared<AppStateObserverManager>();
964 ASSERT_NE(manager, nullptr);
965 ProcessData data;
966 std::vector<std::string> bundleNameList;
967 std::string bundleName = "com.ohos.unittest";
968 data.bundleName = bundleName;
969 bundleNameList.push_back(bundleName);
970 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
971 manager->HandleOnProcessCreated(data);
972 }
973
974 /*
975 * Feature: AppStateObserverManager
976 * Function: HandleOnProcessCreated
977 * SubFunction: NA
978 * FunctionPoints: AppStateObserverManager HandleOnProcessCreated
979 * EnvConditions: NA
980 * CaseDescription: Verify HandleOnProcessCreated
981 */
982 HWTEST_F(AppSpawnSocketTest, HandleOnProcessCreated_003, TestSize.Level2)
983 {
984 auto manager = std::make_shared<AppStateObserverManager>();
985 ASSERT_NE(manager, nullptr);
986 ProcessData data;
987 std::vector<std::string> bundleNameList;
988 std::string bundleName1 = "com.ohos.unittest";
989 std::string bundleName2 = "com.ohos.unittest";
990 data.bundleName = bundleName1;
991 bundleNameList.push_back(bundleName2);
992 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
993 manager->HandleOnProcessCreated(data);
994 }
995
996 /*
997 * Feature: AppStateObserverManager
998 * Function: HandleOnProcessCreated
999 * SubFunction: NA
1000 * FunctionPoints: AppStateObserverManager HandleOnProcessCreated
1001 * EnvConditions: NA
1002 * CaseDescription: Verify HandleOnProcessCreated
1003 */
1004 HWTEST_F(AppSpawnSocketTest, HandleOnProcessCreated_004, TestSize.Level2)
1005 {
1006 auto manager = std::make_shared<AppStateObserverManager>();
1007 ASSERT_NE(manager, nullptr);
1008 ProcessData data;
1009 std::vector<std::string> bundleNameList;
1010 std::string bundleName = "com.ohos.unittest";
1011 data.bundleName = bundleName;
1012 bundleNameList.push_back(bundleName);
1013 manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
1014 manager->HandleOnProcessCreated(data);
1015 }
1016
1017 /*
1018 * Feature: AppStateObserverManager
1019 * Function: HandleOnProcessStateChanged
1020 * SubFunction: NA
1021 * FunctionPoints: AppStateObserverManager HandleOnProcessStateChanged
1022 * EnvConditions: NA
1023 * CaseDescription: Verify HandleOnProcessStateChanged
1024 */
1025 HWTEST_F(AppSpawnSocketTest, HandleOnProcessStateChanged_001, TestSize.Level2)
1026 {
1027 auto manager = std::make_shared<AppStateObserverManager>();
1028 ASSERT_NE(manager, nullptr);
1029 manager->HandleOnProcessStateChanged(nullptr);
1030 }
1031
1032 /*
1033 * Feature: AppStateObserverManager
1034 * Function: HandleOnProcessStateChanged
1035 * SubFunction: NA
1036 * FunctionPoints: AppStateObserverManager HandleOnProcessStateChanged
1037 * EnvConditions: NA
1038 * CaseDescription: Verify HandleOnProcessStateChanged
1039 */
1040 HWTEST_F(AppSpawnSocketTest, HandleOnProcessStateChanged_002, TestSize.Level2)
1041 {
1042 auto manager = std::make_shared<AppStateObserverManager>();
1043 ASSERT_NE(manager, nullptr);
1044 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1045 std::vector<std::string> bundleNameList;
1046 std::string bundleName = "com.ohos.unittest";
1047 appRecord->mainBundleName_ = bundleName;
1048 bundleNameList.push_back(bundleName);
1049 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
1050 manager->HandleOnProcessStateChanged(appRecord);
1051 }
1052
1053 /*
1054 * Feature: AppStateObserverManager
1055 * Function: HandleOnProcessStateChanged
1056 * SubFunction: NA
1057 * FunctionPoints: AppStateObserverManager HandleOnProcessStateChanged
1058 * EnvConditions: NA
1059 * CaseDescription: Verify HandleOnProcessStateChanged
1060 */
1061 HWTEST_F(AppSpawnSocketTest, HandleOnProcessStateChanged_003, TestSize.Level2)
1062 {
1063 auto manager = std::make_shared<AppStateObserverManager>();
1064 ASSERT_NE(manager, nullptr);
1065 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1066 std::vector<std::string> bundleNameList;
1067 std::string bundleName = "com.ohos.unittest";
1068 appRecord->mainBundleName_ = bundleName;
1069 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
1070 manager->HandleOnProcessStateChanged(appRecord);
1071 }
1072
1073 /*
1074 * Feature: AppStateObserverManager
1075 * Function: HandleOnProcessStateChanged
1076 * SubFunction: NA
1077 * FunctionPoints: AppStateObserverManager HandleOnProcessStateChanged
1078 * EnvConditions: NA
1079 * CaseDescription: Verify HandleOnProcessStateChanged
1080 */
1081 HWTEST_F(AppSpawnSocketTest, HandleOnProcessStateChanged_004, TestSize.Level2)
1082 {
1083 auto manager = std::make_shared<AppStateObserverManager>();
1084 ASSERT_NE(manager, nullptr);
1085 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1086 std::vector<std::string> bundleNameList;
1087 std::string bundleName1 = "com.ohos.unittest1";
1088 std::string bundleName2 = "com.ohos.unittest2";
1089 appRecord->mainBundleName_ = bundleName1;
1090 bundleNameList.push_back(bundleName2);
1091 manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
1092 manager->HandleOnProcessStateChanged(appRecord);
1093 }
1094
1095 /*
1096 * Feature: AppStateObserverManager
1097 * Function: HandleOnProcessStateChanged
1098 * SubFunction: NA
1099 * FunctionPoints: AppStateObserverManager HandleOnProcessStateChanged
1100 * EnvConditions: NA
1101 * CaseDescription: Verify HandleOnProcessStateChanged
1102 */
1103 HWTEST_F(AppSpawnSocketTest, HandleOnProcessStateChanged_005, TestSize.Level2)
1104 {
1105 auto manager = std::make_shared<AppStateObserverManager>();
1106 ASSERT_NE(manager, nullptr);
1107 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1108 std::vector<std::string> bundleNameList;
1109 std::string bundleName = "com.ohos.unittest";
1110 appRecord->mainBundleName_ = bundleName;
1111 bundleNameList.push_back(bundleName);
1112 manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
1113 manager->HandleOnProcessStateChanged(appRecord);
1114 }
1115
1116 /*
1117 * Feature: AppStateObserverManager
1118 * Function: HandleOnAppProcessDied
1119 * SubFunction: NA
1120 * FunctionPoints: AppStateObserverManager HandleOnAppProcessDied
1121 * EnvConditions: NA
1122 * CaseDescription: Verify HandleOnAppProcessDied
1123 */
1124 HWTEST_F(AppSpawnSocketTest, HandleOnAppProcessDied_001, TestSize.Level2)
1125 {
1126 auto manager = std::make_shared<AppStateObserverManager>();
1127 ASSERT_NE(manager, nullptr);
1128 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1129 manager->HandleOnAppProcessDied(nullptr);
1130 manager->HandleOnAppProcessDied(appRecord);
1131 }
1132
1133 /*
1134 * Feature: AppStateObserverManager
1135 * Function: HandleOnRenderProcessDied
1136 * SubFunction: NA
1137 * FunctionPoints: AppStateObserverManager HandleOnRenderProcessDied
1138 * EnvConditions: NA
1139 * CaseDescription: Verify HandleOnRenderProcessDied
1140 */
1141 HWTEST_F(AppSpawnSocketTest, HandleOnRenderProcessDied_001, TestSize.Level2)
1142 {
1143 auto manager = std::make_shared<AppStateObserverManager>();
1144 ASSERT_NE(manager, nullptr);
1145 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1146 std::shared_ptr<RenderRecord> renderRecord =
1147 std::make_shared<RenderRecord>(1, "param", FdGuard(-1), FdGuard(-1), FdGuard(-1), appRecord);
1148 renderRecord->SetPid(1);
1149 manager->HandleOnRenderProcessDied(nullptr);
1150 manager->HandleOnRenderProcessDied(renderRecord);
1151 }
1152
1153 /*
1154 * Feature: AppStateObserverManager
1155 * Function: HandleOnProcessDied
1156 * SubFunction: NA
1157 * FunctionPoints: AppStateObserverManager HandleOnProcessDied
1158 * EnvConditions: NA
1159 * CaseDescription: Verify HandleOnProcessDied
1160 */
1161 HWTEST_F(AppSpawnSocketTest, HandleOnProcessDied_001, TestSize.Level2)
1162 {
1163 auto manager = std::make_shared<AppStateObserverManager>();
1164 ASSERT_NE(manager, nullptr);
1165 ProcessData data;
1166 std::vector<std::string> bundleNameList;
1167 std::string bundleName = "com.ohos.unittest";
1168 data.bundleName = bundleName;
1169 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
1170 manager->HandleOnProcessDied(data);
1171 }
1172
1173 /*
1174 * Feature: AppStateObserverManager
1175 * Function: HandleOnProcessDied
1176 * SubFunction: NA
1177 * FunctionPoints: AppStateObserverManager HandleOnProcessDied
1178 * EnvConditions: NA
1179 * CaseDescription: Verify HandleOnProcessDied
1180 */
1181 HWTEST_F(AppSpawnSocketTest, HandleOnProcessDied_002, TestSize.Level2)
1182 {
1183 auto manager = std::make_shared<AppStateObserverManager>();
1184 ASSERT_NE(manager, nullptr);
1185 ProcessData data;
1186 std::vector<std::string> bundleNameList;
1187 std::string bundleName = "com.ohos.unittest";
1188 data.bundleName = bundleName;
1189 bundleNameList.push_back(bundleName);
1190 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
1191 manager->HandleOnProcessDied(data);
1192 }
1193
1194 /*
1195 * Feature: AppStateObserverManager
1196 * Function: HandleOnProcessDied
1197 * SubFunction: NA
1198 * FunctionPoints: AppStateObserverManager HandleOnProcessDied
1199 * EnvConditions: NA
1200 * CaseDescription: Verify HandleOnProcessDied
1201 */
1202 HWTEST_F(AppSpawnSocketTest, HandleOnProcessDied_003, TestSize.Level2)
1203 {
1204 auto manager = std::make_shared<AppStateObserverManager>();
1205 ASSERT_NE(manager, nullptr);
1206 ProcessData data;
1207 std::vector<std::string> bundleNameList;
1208 std::string bundleName1 = "com.ohos.unittest1";
1209 std::string bundleName2 = "com.ohos.unittest2";
1210 data.bundleName = bundleName1;
1211 bundleNameList.push_back(bundleName2);
1212 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
1213 manager->HandleOnProcessDied(data);
1214 }
1215
1216 /*
1217 * Feature: AppStateObserverManager
1218 * Function: HandleOnProcessDied
1219 * SubFunction: NA
1220 * FunctionPoints: AppStateObserverManager HandleOnProcessDied
1221 * EnvConditions: NA
1222 * CaseDescription: Verify HandleOnProcessDied
1223 */
1224 HWTEST_F(AppSpawnSocketTest, HandleOnProcessDied_004, TestSize.Level2)
1225 {
1226 auto manager = std::make_shared<AppStateObserverManager>();
1227 ASSERT_NE(manager, nullptr);
1228 ProcessData data;
1229 std::vector<std::string> bundleNameList;
1230 std::string bundleName1 = "com.ohos.unittest1";
1231 std::string bundleName2 = "com.ohos.unittest2";
1232 data.bundleName = bundleName1;
1233 bundleNameList.push_back(bundleName2);
1234 manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
1235 manager->HandleOnProcessDied(data);
1236 }
1237
1238 /*
1239 * Feature: AppStateObserverManager
1240 * Function: ObserverExist
1241 * SubFunction: NA
1242 * FunctionPoints: AppStateObserverManager ObserverExist
1243 * EnvConditions: NA
1244 * CaseDescription: Verify ObserverExist
1245 */
1246 HWTEST_F(AppSpawnSocketTest, ObserverExist_001, TestSize.Level2)
1247 {
1248 auto manager = std::make_shared<AppStateObserverManager>();
1249 bool res = manager->ObserverExist(nullptr);
1250 EXPECT_FALSE(res);
1251 }
1252
1253 /*
1254 * Feature: AppStateObserverManager
1255 * Function: ObserverExist
1256 * SubFunction: NA
1257 * FunctionPoints: AppStateObserverManager ObserverExist
1258 * EnvConditions: NA
1259 * CaseDescription: Verify ObserverExist
1260 */
1261 HWTEST_F(AppSpawnSocketTest, ObserverExist_002, TestSize.Level2)
1262 {
1263 auto manager = std::make_shared<AppStateObserverManager>();
1264 sptr<IApplicationStateObserver> observer = new MockApplicationStateObserver();
1265 std::vector<std::string> bundleNameList;
1266 manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, bundleNameList});
1267 bool res = manager->ObserverExist(observer);
1268 EXPECT_TRUE(res);
1269 }
1270
1271 /**
1272 * @tc.name: RegisterAbilityForegroundStateObserver_0100
1273 * @tc.desc: The test returns when the permission judgment is inconsistent.
1274 * @tc.type: FUNC
1275 */
1276 HWTEST_F(AppSpawnSocketTest, RegisterAbilityForegroundStateObserver_0100, TestSize.Level1)
1277 {
1278 auto manager = std::make_shared<AppStateObserverManager>();
1279 ASSERT_NE(manager, nullptr);
1280 sptr<IAbilityForegroundStateObserver> observer = new AbilityForegroundStateObserverProxy(nullptr);
1281 manager->abilityForegroundObserverMap_.emplace(observer, 0);
1282 auto res = manager->RegisterAbilityForegroundStateObserver(observer);
1283 EXPECT_EQ(res, ERR_PERMISSION_DENIED);
1284 }
1285
1286 /**
1287 * @tc.name: UnregisterAbilityForegroundStateObserver_0100
1288 * @tc.desc: The test returns when the permission judgment is inconsistent.
1289 * @tc.type: FUNC
1290 */
1291 HWTEST_F(AppSpawnSocketTest, UnregisterAbilityForegroundStateObserver_0100, TestSize.Level1)
1292 {
1293 sptr<IAbilityForegroundStateObserver> observer = new AbilityForegroundStateObserverProxy(nullptr);
1294 auto manager = std::make_shared<AppStateObserverManager>();
1295 ASSERT_NE(manager, nullptr);
1296 manager->abilityForegroundObserverMap_.emplace(observer, 0);
1297 auto res = manager->UnregisterAbilityForegroundStateObserver(observer);
1298 EXPECT_EQ(res, ERR_PERMISSION_DENIED);
1299 }
1300
1301 /**
1302 * @tc.name: IsAbilityForegroundObserverExist_0100
1303 * @tc.desc: Test return when abilityForegroundObserverMap_ is not empty and
1304 * the conditions within the loop are met.
1305 * @tc.type: FUNC
1306 */
1307 HWTEST_F(AppSpawnSocketTest, IsAbilityForegroundObserverExist_0100, TestSize.Level1)
1308 {
1309 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(nullptr);
1310 sptr<IAbilityForegroundStateObserver> observers = new AbilityForegroundStateObserverProxy(nullptr);
1311 auto manager = std::make_shared<AppStateObserverManager>();
1312 ASSERT_NE(manager, nullptr);
1313 manager->abilityForegroundObserverMap_.emplace(observers, 0);
1314 auto res = manager->IsAbilityForegroundObserverExist(observer);
1315 EXPECT_EQ(res, true);
1316 }
1317
1318 /**
1319 * @tc.name: AddObserverDeathRecipient_0100
1320 * @tc.desc: Verify that AddObserverDeathRecipient can be called normally(type is APPLICATION_STATE_OBSERVER)
1321 * @tc.type: FUNC
1322 */
1323 HWTEST_F(AppSpawnSocketTest, AddObserverDeathRecipient_0100, TestSize.Level1)
1324 {
1325 auto observerStub = new MockAbilityForegroundStateObserverServerStub();
1326 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(observerStub);
1327 auto manager = std::make_shared<AppStateObserverManager>();
1328 ASSERT_NE(manager, nullptr);
1329 ObserverType type = ObserverType::APPLICATION_STATE_OBSERVER;
1330 manager->AddObserverDeathRecipient(observer, type);
1331 ASSERT_FALSE(manager->recipientMap_.empty());
1332 }
1333
1334 /**
1335 * @tc.name: AddObserverDeathRecipient_0200
1336 * @tc.desc: Verify that AddObserverDeathRecipient can be called normally(type is ABILITY_FOREGROUND_STATE_OBSERVER)
1337 * @tc.type: FUNC
1338 */
1339 HWTEST_F(AppSpawnSocketTest, AddObserverDeathRecipient_0200, TestSize.Level1)
1340 {
1341 auto observerStub = new MockAbilityForegroundStateObserverServerStub();
1342 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(observerStub);
1343 auto manager = std::make_shared<AppStateObserverManager>();
1344 ASSERT_NE(manager, nullptr);
1345 ObserverType type = ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER;
1346 manager->AddObserverDeathRecipient(observer, type);
1347 ASSERT_FALSE(manager->recipientMap_.empty());
1348 }
1349
1350 /**
1351 * @tc.name: AddObserverDeathRecipient_0300
1352 * @tc.desc: Verify that AddObserverDeathRecipient can be called normally(observer is nullptr)
1353 * @tc.type: FUNC
1354 */
1355 HWTEST_F(AppSpawnSocketTest, AddObserverDeathRecipient_0300, TestSize.Level1)
1356 {
1357 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(nullptr);
1358 auto manager = std::make_shared<AppStateObserverManager>();
1359 ASSERT_NE(manager, nullptr);
1360 ObserverType type = ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER;
1361 manager->AddObserverDeathRecipient(observer, type);
1362 ASSERT_TRUE(manager->recipientMap_.empty());
1363 }
1364
1365 /**
1366 * @tc.name: RemoveObserverDeathRecipient_0100
1367 * @tc.desc: Verify that RemoveObserverDeathRecipient can be called normally
1368 * @tc.type: FUNC
1369 */
1370 HWTEST_F(AppSpawnSocketTest, RemoveObserverDeathRecipient_0100, TestSize.Level1)
1371 {
1372 auto observerStub = new MockAbilityForegroundStateObserverServerStub();
1373 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(observerStub);
1374 auto manager = std::make_shared<AppStateObserverManager>();
1375 ASSERT_NE(manager, nullptr);
1376 manager->RemoveObserverDeathRecipient(observer);
1377 ASSERT_TRUE(manager->recipientMap_.empty());
1378 }
1379
1380 /**
1381 * @tc.name: RemoveObserverDeathRecipient_0200
1382 * @tc.desc: Verify that RemoveObserverDeathRecipient can be called normally(observer is nullptr)
1383 * @tc.type: FUNC
1384 */
1385 HWTEST_F(AppSpawnSocketTest, RemoveObserverDeathRecipient_0200, TestSize.Level1)
1386 {
1387 sptr<IRemoteBroker> observer = new AppForegroundStateObserverProxy(nullptr);
1388 auto manager = std::make_shared<AppStateObserverManager>();
1389 ASSERT_NE(manager, nullptr);
1390 manager->RemoveObserverDeathRecipient(observer);
1391 ASSERT_TRUE(manager->recipientMap_.empty());
1392 }
1393
1394 /**
1395 * @tc.name: RegisterAppForegroundStateObserver_0100
1396 * @tc.desc: Test when observer is not nullptr and without permission.
1397 * and observer not exist.
1398 * @tc.type: FUNC
1399 */
1400 HWTEST_F(AppSpawnSocketTest, RegisterAppForegroundStateObserver_0100, TestSize.Level1)
1401 {
1402 auto manager = std::make_shared<AppStateObserverManager>();
1403 sptr<IAppForegroundStateObserver> observer = new (std::nothrow) AppForegroundStateObserver();
1404 auto res = manager->RegisterAppForegroundStateObserver(observer);
1405 EXPECT_EQ(ERR_PERMISSION_DENIED, res);
1406 }
1407
1408 /**
1409 * @tc.name: UnregisterAppForegroundStateObserver_0100
1410 * @tc.desc: Test when observer is not nullptr and without permission.
1411 * @tc.type: FUNC
1412 */
1413 HWTEST_F(AppSpawnSocketTest, UnregisterAppForegroundStateObserver_0100, TestSize.Level1)
1414 {
1415 auto manager = std::make_shared<AppStateObserverManager>();
1416 sptr<IAppForegroundStateObserver> observer = new (std::nothrow) AppForegroundStateObserver();
1417 manager->appForegroundStateObserverMap_.emplace(observer, 0);
1418 auto res = manager->UnregisterAppForegroundStateObserver(observer);
1419 EXPECT_EQ(ERR_PERMISSION_DENIED, res);
1420 }
1421
1422 /**
1423 * @tc.name: IsAppForegroundObserverExist_0100
1424 * @tc.desc: Test when observer and appForegroundStateObserverMap is not nullptr
1425 * and asObject of them is same.
1426 * @tc.type: FUNC
1427 */
1428 HWTEST_F(AppSpawnSocketTest, IsAppForegroundObserverExist_0100, TestSize.Level1)
1429 {
1430 auto manager = std::make_shared<AppStateObserverManager>();
1431 sptr<IAppForegroundStateObserver> observer = new (std::nothrow) AppForegroundStateObserver();
1432 manager->appForegroundStateObserverMap_.emplace(observer, 0);
1433 auto res = manager->IsAppForegroundObserverExist(observer);
1434 EXPECT_EQ(true, res);
1435 }
1436
1437 /**
1438 * @tc.name: OnObserverDied_0100
1439 * @tc.desc: Test when observer is not nullptr and type is APPLICATION_STATE_OBSERVER.
1440 * @tc.type: FUNC
1441 */
1442 HWTEST_F(AppSpawnSocketTest, OnObserverDied_0100, TestSize.Level1)
1443 {
1444 sptr<IRemoteObject> remoteObject = new (std::nothrow) AppForegroundStateObserver();
1445 wptr<IRemoteObject> remote(remoteObject);
1446 ObserverType type = ObserverType::APPLICATION_STATE_OBSERVER;
1447 DelayedSingleton<AppStateObserverManager>::GetInstance()->OnObserverDied(remote, type);
1448 auto appStateObserverMapSize =
1449 DelayedSingleton<AppStateObserverManager>::GetInstance()->appStateObserverMap_.size();
1450 EXPECT_EQ(0, appStateObserverMapSize);
1451 }
1452
1453 /**
1454 * @tc.name: OnObserverDied_0200
1455 * @tc.desc: Test when observer is not nullptr and type is ABILITY_FOREGROUND_STATE_OBSERVER.
1456 * @tc.type: FUNC
1457 */
1458 HWTEST_F(AppSpawnSocketTest, OnObserverDied_0200, TestSize.Level1)
1459 {
1460 sptr<IRemoteObject> remoteObject = new (std::nothrow) AppForegroundStateObserver();
1461 wptr<IRemoteObject> remote(remoteObject);
1462 ObserverType type = ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER;
1463 DelayedSingleton<AppStateObserverManager>::GetInstance()->OnObserverDied(remote, type);
1464 auto abilityforegroundObserverSetSize =
1465 DelayedSingleton<AppStateObserverManager>::GetInstance()->abilityForegroundObserverMap_.size();
1466 EXPECT_EQ(0, abilityforegroundObserverSetSize);
1467 }
1468
1469 /**
1470 * @tc.name: OnObserverDied_0300
1471 * @tc.desc: Test when observer is not nullptr and type is ABILITY_FOREGROUND_STATE_OBSERVER.
1472 * @tc.type: FUNC
1473 */
1474 HWTEST_F(AppSpawnSocketTest, OnObserverDied_0300, TestSize.Level1)
1475 {
1476 sptr<IRemoteObject> remoteObject = new (std::nothrow) AppForegroundStateObserver();
1477 wptr<IRemoteObject> remote(remoteObject);
1478 ObserverType type = ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER;
1479 DelayedSingleton<AppStateObserverManager>::GetInstance()->OnObserverDied(remote, type);
1480 auto appForegroundStateObserverSetSize =
1481 DelayedSingleton<AppStateObserverManager>::GetInstance()->appForegroundStateObserverMap_.size();
1482 EXPECT_EQ(0, appForegroundStateObserverSetSize);
1483 }
1484
1485 /**
1486 * @tc.name: OnObserverDied_0400
1487 * @tc.desc: Test when observer is not nullptr and type is APP_FOREGROUND_STATE_OBSERVER.
1488 * @tc.type: FUNC
1489 */
1490 HWTEST_F(AppSpawnSocketTest, OnObserverDied_0400, TestSize.Level1)
1491 {
1492 sptr<IRemoteObject> remoteObject = new (std::nothrow) AppForegroundStateObserver();
1493 wptr<IRemoteObject> remote(remoteObject);
1494 ObserverType type = ObserverType::APP_FOREGROUND_STATE_OBSERVER;
1495 DelayedSingleton<AppStateObserverManager>::GetInstance()->OnObserverDied(remote, type);
1496 auto appForegroundStateObserverSetSize =
1497 DelayedSingleton<AppStateObserverManager>::GetInstance()->appForegroundStateObserverMap_.size();
1498 EXPECT_EQ(0, appForegroundStateObserverSetSize);
1499 }
1500
1501 /**
1502 * @tc.name: OnObserverDied_0500
1503 * @tc.desc: Test when observer is not nullptr and type is undefined.
1504 * @tc.type: FUNC
1505 */
1506 HWTEST_F(AppSpawnSocketTest, OnObserverDied_0500, TestSize.Level1)
1507 {
1508 sptr<IRemoteObject> remoteObject = new (std::nothrow) AppForegroundStateObserver();
1509 wptr<IRemoteObject> remote(remoteObject);
1510 ObserverType type = ObserverType::ABILITY_FOREGROUND_STATE_OBSERVER;
1511 DelayedSingleton<AppStateObserverManager>::GetInstance()->OnObserverDied(remote, static_cast<ObserverType>(100));
1512 auto appForegroundStateObserverSetSize =
1513 DelayedSingleton<AppStateObserverManager>::GetInstance()->appForegroundStateObserverMap_.size();
1514 EXPECT_EQ(0, appForegroundStateObserverSetSize);
1515 }
1516
1517 /*
1518 * Feature: AppStateObserverManager
1519 * Function: OnAppCacheStateChanged
1520 * SubFunction: NA
1521 * FunctionPoints: AppStateObserverManager OnAppCacheStateChanged
1522 * EnvConditions: NA
1523 * CaseDescription: Verify OnAppCacheStateChanged
1524 */
1525 HWTEST_F(AppSpawnSocketTest, OnAppCacheStateChanged_001, TestSize.Level2)
1526 {
1527 auto manager = std::make_shared<AppStateObserverManager>();
1528 ASSERT_NE(manager, nullptr);
1529 std::shared_ptr<AppRunningRecord> appRecord;
1530 manager->OnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CACHED);
1531 manager->Init();
1532 manager->OnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CACHED);
1533 }
1534
1535 /*
1536 * Feature: AppStateObserverManager
1537 * Function: HandleOnAppCacheStateChanged
1538 * SubFunction: NA
1539 * FunctionPoints: AppStateObserverManager HandleOnAppCacheStateChanged
1540 * EnvConditions: NA
1541 * CaseDescription: Verify HandleOnAppCacheStateChanged
1542 */
1543 HWTEST_F(AppSpawnSocketTest, HandleOnAppCacheStateChanged_001, TestSize.Level2)
1544 {
1545 auto manager = std::make_shared<AppStateObserverManager>();
1546 ASSERT_NE(manager, nullptr);
1547 manager->HandleOnAppCacheStateChanged(nullptr, ApplicationState::APP_STATE_CREATE);
1548 std::vector<std::string> bundleNameList;
1549 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1550 std::string bundleName = "com.ohos.unittest";
1551 appRecord->mainBundleName_ = bundleName;
1552 bundleNameList.push_back(bundleName);
1553 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
1554 manager->HandleOnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CREATE);
1555 }
1556
1557 /*
1558 * Feature: AppStateObserverManager
1559 * Function: HandleOnAppCacheStateChanged
1560 * SubFunction: NA
1561 * FunctionPoints: AppStateObserverManager HandleOnAppCacheStateChanged
1562 * EnvConditions: NA
1563 * CaseDescription: Verify HandleOnAppCacheStateChanged
1564 */
1565 HWTEST_F(AppSpawnSocketTest, HandleOnAppCacheStateChanged_002, TestSize.Level2)
1566 {
1567 auto manager = std::make_shared<AppStateObserverManager>();
1568 ASSERT_NE(manager, nullptr);
1569 std::vector<std::string> bundleNameList;
1570 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1571 std::string bundleName = "com.ohos.unittest";
1572 appRecord->mainBundleName_ = bundleName;
1573 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
1574 manager->HandleOnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CREATE);
1575 }
1576
1577 /*
1578 * Feature: AppStateObserverManager
1579 * Function: HandleOnAppCacheStateChanged
1580 * SubFunction: NA
1581 * FunctionPoints: AppStateObserverManager HandleOnAppCacheStateChanged
1582 * EnvConditions: NA
1583 * CaseDescription: Verify HandleOnAppCacheStateChanged
1584 */
1585 HWTEST_F(AppSpawnSocketTest, HandleOnAppCacheStateChanged_003, TestSize.Level2)
1586 {
1587 auto manager = std::make_shared<AppStateObserverManager>();
1588 ASSERT_NE(manager, nullptr);
1589 std::vector<std::string> bundleNameList;
1590 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1591 std::string bundleName1 = "com.ohos.unittest1";
1592 std::string bundleName2 = "com.ohos.unittest2";
1593 appRecord->mainBundleName_ = bundleName1;
1594 bundleNameList.push_back(bundleName2);
1595 manager->appStateObserverMap_.emplace(observer_, AppStateObserverInfo{0, bundleNameList});
1596 manager->HandleOnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CREATE);
1597 }
1598
1599 /*
1600 * Feature: AppStateObserverManager
1601 * Function: HandleOnAppCacheStateChanged
1602 * SubFunction: NA
1603 * FunctionPoints: AppStateObserverManager HandleOnAppCacheStateChanged
1604 * EnvConditions: NA
1605 * CaseDescription: Verify HandleOnAppCacheStateChanged
1606 */
1607 HWTEST_F(AppSpawnSocketTest, HandleOnAppCacheStateChanged_004, TestSize.Level2)
1608 {
1609 auto manager = std::make_shared<AppStateObserverManager>();
1610 ASSERT_NE(manager, nullptr);
1611 std::vector<std::string> bundleNameList;
1612 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1613 std::string bundleName = "com.ohos.unittest";
1614 appRecord->mainBundleName_ = bundleName;
1615 bundleNameList.push_back(bundleName);
1616 manager->appStateObserverMap_.emplace(nullptr, AppStateObserverInfo{0, bundleNameList});
1617 manager->HandleOnAppCacheStateChanged(appRecord, ApplicationState::APP_STATE_CREATE);
1618 }
1619
1620 /*
1621 * Feature: AppStateObserverManager
1622 * Function: AddObserverCount
1623 * SubFunction: NA
1624 * FunctionPoints: AppStateObserverManager AddObserverCount
1625 * EnvConditions: NA
1626 * CaseDescription: Verify AddObserverCount
1627 */
1628 HWTEST_F(AppSpawnSocketTest, AddObserverCount_001, TestSize.Level2)
1629 {
1630 auto manager = std::make_shared<AppStateObserverManager>();
1631 sptr<IApplicationStateObserver> obs = new MockApplicationStateObserver();
1632 int32_t uid = 1000;
1633
1634 manager->AddObserverCount(uid);
1635 EXPECT_EQ(manager->observerCountMap_[uid], 1);
1636 EXPECT_EQ(manager->observerAmount_, 1);
1637 }
1638
1639 /*
1640 * Feature: AppStateObserverManager
1641 * Function: DecreaseObserverCount
1642 * SubFunction: NA
1643 * FunctionPoints: AppStateObserverManager DecreaseObserverCount
1644 * EnvConditions: NA
1645 * CaseDescription: Verify DecreaseObserverCount
1646 */
1647 HWTEST_F(AppSpawnSocketTest, DecreaseObserverCount_001, TestSize.Level2)
1648 {
1649 auto manager = std::make_shared<AppStateObserverManager>();
1650 sptr<IApplicationStateObserver> obs = new MockApplicationStateObserver();
1651 int32_t uid = 1000;
1652
1653 manager->AddObserverCount(uid);
1654 EXPECT_EQ(manager->observerCountMap_[uid], 1);
1655 EXPECT_EQ(manager->observerAmount_, 1);
1656
1657 manager->DecreaseObserverCount(uid);
1658 EXPECT_TRUE(manager->observerCountMap_.empty());
1659 EXPECT_EQ(manager->observerAmount_, 0);
1660 }
1661
1662 /*
1663 * Feature: AppStateObserverManager
1664 * Function: WrapAppStateData
1665 * SubFunction: NA
1666 * FunctionPoints: AppStateObserverManager WrapAppStateData
1667 * EnvConditions: NA
1668 * CaseDescription: Verify WrapAppStateData
1669 */
1670 HWTEST_F(AppSpawnSocketTest, WrapAppStateData_001, TestSize.Level2)
1671 {
1672 auto manager = std::make_shared<AppStateObserverManager>();
1673 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1674 appRecord->SetUid(1001);
1675 appRecord->mainBundleName_ = "com.test.app";
1676
1677 AppStateData data = manager->WrapAppStateData(appRecord, ApplicationState::APP_STATE_FOREGROUND);
1678
1679 EXPECT_EQ(data.uid, 1001);
1680 EXPECT_EQ(data.bundleName, "com.test.app");
1681 EXPECT_EQ(data.state, static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND));
1682 }
1683
1684 /*
1685 * Feature: AppStateObserverManager
1686 * Function: HandleOnProcessResued
1687 * SubFunction: NA
1688 * FunctionPoints: AppStateObserverManager HandleOnProcessResued
1689 * EnvConditions: NA
1690 * CaseDescription: Verify HandleOnProcessResued
1691 */
1692 HWTEST_F(AppSpawnSocketTest, HandleOnProcessResued_001, TestSize.Level2)
1693 {
1694 auto manager = std::make_shared<AppStateObserverManager>();
1695 auto* mockObserver = new MockApplicationStateObserver();
1696 sptr<IApplicationStateObserver> observer = mockObserver;
1697 manager->appStateObserverMap_[observer] = AppStateObserverInfo{0, {"com.example"}};
1698 EXPECT_CALL(*mockObserver, OnProcessReused(_)).Times(0);
1699 manager->HandleOnProcessResued(nullptr);
1700 }
1701
1702 /*
1703 * Feature: AppStateObserverManager
1704 * Function: HandleOnProcessResued
1705 * SubFunction: NA
1706 * FunctionPoints: AppStateObserverManager HandleOnProcessResued
1707 * EnvConditions: NA
1708 * CaseDescription: Verify HandleOnProcessResued
1709 */
1710 HWTEST_F(AppSpawnSocketTest, HandleOnProcessResued_002, TestSize.Level2)
1711 {
1712 auto manager = std::make_shared<AppStateObserverManager>();
1713 ASSERT_NE(manager, nullptr);
1714 auto mockObserver = new MockApplicationStateObserver();
1715 sptr<IApplicationStateObserver> observer(mockObserver);
1716 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1717 appRecord->mainBundleName_ = "com.ohos.unittest";
1718 std::vector<std::string> bundleNames{"com.ohos.unittest"};
1719 manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, bundleNames});
1720 EXPECT_CALL(*mockObserver, OnProcessReused(_)).Times(1);
1721 manager->HandleOnProcessResued(appRecord);
1722 }
1723
1724 /*
1725 * Feature: AppStateObserverManager
1726 * Function: HandleOnProcessResued
1727 * SubFunction: NA
1728 * FunctionPoints: AppStateObserverManager HandleOnProcessResued
1729 * EnvConditions: NA
1730 * CaseDescription: Verify HandleOnProcessResued
1731 */
1732 HWTEST_F(AppSpawnSocketTest, HandleOnProcessResued_003, TestSize.Level2)
1733 {
1734 auto manager = std::make_shared<AppStateObserverManager>();
1735 ASSERT_NE(manager, nullptr);
1736 auto mockObserver = new MockApplicationStateObserver();
1737 sptr<IApplicationStateObserver> observer(mockObserver);
1738 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1739 appRecord->mainBundleName_ = "com.ohos.unittest";
1740 std::vector<std::string> bundleNames;
1741 manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, bundleNames});
1742 EXPECT_CALL(*mockObserver, OnProcessReused(_)).Times(1);
1743 manager->HandleOnProcessResued(appRecord);
1744 }
1745
1746 /*
1747 * Feature: AppStateObserverManager
1748 * Function: HandleOnProcessResued
1749 * SubFunction: NA
1750 * FunctionPoints: AppStateObserverManager HandleOnProcessResued
1751 * EnvConditions: NA
1752 * CaseDescription: Verify HandleOnProcessResued
1753 */
1754 HWTEST_F(AppSpawnSocketTest, HandleOnProcessResued_004, TestSize.Level2)
1755 {
1756 auto manager = std::make_shared<AppStateObserverManager>();
1757 ASSERT_NE(manager, nullptr);
1758 auto mockObserver = new MockApplicationStateObserver();
1759 sptr<IApplicationStateObserver> observer(mockObserver);
1760 std::shared_ptr<AppRunningRecord> appRecord = MockAppRecord();
1761 appRecord->mainBundleName_ = "com.ohos.unittest";
1762 std::vector<std::string> bundleNames{"com.ohos.other"};
1763 manager->appStateObserverMap_.emplace(observer, AppStateObserverInfo{0, bundleNames});
1764 EXPECT_CALL(*mockObserver, OnProcessReused(_)).Times(0);
1765 manager->HandleOnProcessResued(appRecord);
1766 }
1767 } // namespace AppExecFwk
1768 } // namespace OHOS
1769