1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "ability_handler.h"
19 #define private public
20 #include "preload_uiext_state_observer.h"
21 #undef private
22 #include "ability_record.h"
23 #include "extension_record.h"
24
25 namespace OHOS {
26 namespace AbilityRuntime {
27 using namespace testing::ext;
28 using namespace AAFwk;
29 using namespace AbilityRuntime;
30
31 class PreloadUIextStateObserverTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 };
38
SetUpTestCase(void)39 void PreloadUIextStateObserverTest::SetUpTestCase(void)
40 {}
41
TearDownTestCase(void)42 void PreloadUIextStateObserverTest::TearDownTestCase(void)
43 {}
44
SetUp(void)45 void PreloadUIextStateObserverTest::SetUp(void)
46 {}
47
TearDown(void)48 void PreloadUIextStateObserverTest::TearDown(void)
49 {}
50
51 /**
52 * @tc.number: OnProcessDied_001
53 * @tc.name: OnProcessDied
54 * @tc.desc: Test whether OnProcessDied is called normally.
55 * @tc.type: FUNC
56 */
57 HWTEST_F(PreloadUIextStateObserverTest, OnProcessDied_001, Function | MediumTest | Level1)
58 {
59 GTEST_LOG_(INFO) << "OnProcessDied_001 start";
60 Want want;
61 AppExecFwk::AbilityInfo abilityInfo;
62 AppExecFwk::ApplicationInfo applicationInfo;
63 std::shared_ptr<AAFwk::AbilityRecord> abilityRecord =
64 std::make_shared<AAFwk::AbilityRecord>(want, abilityInfo, applicationInfo);
65 auto extensionRecordSharedPtr = std::make_shared<AbilityRuntime::ExtensionRecord>(abilityRecord);
66 auto hostPid = extensionRecordSharedPtr->hostPid_ = 10;
67 std::weak_ptr<AbilityRuntime::ExtensionRecord> extensionRecord = extensionRecordSharedPtr;
68 PreLoadUIExtStateObserver preLoadUIExtStateObserver(extensionRecord);
69 AppExecFwk::ProcessData processData;
70 int32_t diedPid = processData.pid;
71 preLoadUIExtStateObserver.OnProcessDied(processData);
72 auto record = preLoadUIExtStateObserver.extensionRecord_.lock();
73 EXPECT_TRUE(record != nullptr);
74 EXPECT_TRUE(record->hostPid_ != diedPid);
75 GTEST_LOG_(INFO) << "OnProcessDied_001 end";
76 }
77
78 /**
79 * @tc.number: OnProcessDied_002
80 * @tc.name: OnProcessDied
81 * @tc.desc: Test whether OnProcessDied is called normally.
82 * @tc.type: FUNC
83 */
84 HWTEST_F(PreloadUIextStateObserverTest, OnProcessDied_002, Function | MediumTest | Level1)
85 {
86 GTEST_LOG_(INFO) << "OnProcessDied_002 start";
87 Want want;
88 AppExecFwk::AbilityInfo abilityInfo;
89 AppExecFwk::ApplicationInfo applicationInfo;
90 std::shared_ptr<AAFwk::AbilityRecord> abilityRecord =
91 std::make_shared<AAFwk::AbilityRecord>(want, abilityInfo, applicationInfo);
92 auto extensionRecordSharedPtr = std::make_shared<AbilityRuntime::ExtensionRecord>(abilityRecord);
93 std::weak_ptr<AbilityRuntime::ExtensionRecord> extensionRecord = extensionRecordSharedPtr;
94 PreLoadUIExtStateObserver preLoadUIExtStateObserver(extensionRecord);
95 AppExecFwk::ProcessData processData;
96 int32_t diedPid = processData.pid;
97 preLoadUIExtStateObserver.OnProcessDied(processData);
98 auto record = preLoadUIExtStateObserver.extensionRecord_.lock();
99 EXPECT_TRUE(record != nullptr);
100 EXPECT_TRUE(record->hostPid_ == diedPid);
101 GTEST_LOG_(INFO) << "OnProcessDied_002 end";
102 }
103
104 /**
105 * @tc.number: OnProcessDied_003
106 * @tc.name: OnProcessDied
107 * @tc.desc: Test whether OnProcessDied is called normally.
108 * @tc.type: FUNC
109 */
110 HWTEST_F(PreloadUIextStateObserverTest, OnProcessDied_003, Function | MediumTest | Level1)
111 {
112 GTEST_LOG_(INFO) << "OnProcessDied_003 start";
113 std::weak_ptr<AbilityRuntime::ExtensionRecord> extensionRecord;
114 PreLoadUIExtStateObserver preLoadUIExtStateObserver(extensionRecord);
115 AppExecFwk::ProcessData processData;
116 preLoadUIExtStateObserver.OnProcessDied(processData);
117 auto record = preLoadUIExtStateObserver.extensionRecord_.lock();
118 EXPECT_TRUE(record == nullptr);
119 GTEST_LOG_(INFO) << "OnProcessDied_003 end";
120 }
121 } // namespace AbilityRuntime
122 } // namespace OHOS
123