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 /**
80 * @tc.number: OnProcessDied_003
81 * @tc.name: OnProcessDied
82 * @tc.desc: Test whether OnProcessDied is called normally.
83 * @tc.type: FUNC
84 */
85 HWTEST_F(PreloadUIextStateObserverTest, OnProcessDied_003, Function | MediumTest | Level1)
86 {
87 GTEST_LOG_(INFO) << "OnProcessDied_003 start";
88 std::weak_ptr<AbilityRuntime::ExtensionRecord> extensionRecord;
89 PreLoadUIExtStateObserver preLoadUIExtStateObserver(extensionRecord);
90 AppExecFwk::ProcessData processData;
91 preLoadUIExtStateObserver.OnProcessDied(processData);
92 auto record = preLoadUIExtStateObserver.extensionRecord_.lock();
93 EXPECT_TRUE(record == nullptr);
94 GTEST_LOG_(INFO) << "OnProcessDied_003 end";
95 }
96
97 /**
98 * @tc.number: OnAppCacheStateChanged_001
99 * @tc.name: OnAppCacheStateChanged
100 * @tc.desc: Test whether OnAppCacheStateChanged is called normally.
101 * @tc.type: FUNC
102 */
103 HWTEST_F(PreloadUIextStateObserverTest, OnAppCacheStateChanged_001, Function | MediumTest | Level1)
104 {
105 GTEST_LOG_(INFO) << "OnAppCacheStateChanged_001 start";
106 std::weak_ptr<AbilityRuntime::ExtensionRecord> extensionRecord;
107 PreLoadUIExtStateObserver preLoadUIExtStateObserver(extensionRecord);
108 AppExecFwk::AppStateData appStateData;
109 preLoadUIExtStateObserver.OnAppCacheStateChanged(appStateData);
110 auto record = preLoadUIExtStateObserver.extensionRecord_.lock();
111 EXPECT_TRUE(record == nullptr);
112 GTEST_LOG_(INFO) << "OnAppCacheStateChanged_001 end";
113 }
114
115 /**
116 * @tc.number: OnAppCacheStateChanged_002
117 * @tc.name: OnAppCacheStateChanged
118 * @tc.desc: Test whether OnAppCacheStateChanged is called normally.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(PreloadUIextStateObserverTest, OnAppCacheStateChanged_002, Function | MediumTest | Level1)
122 {
123 GTEST_LOG_(INFO) << "OnAppCacheStateChanged_002 start";
124 Want want;
125 AppExecFwk::AbilityInfo abilityInfo;
126 AppExecFwk::ApplicationInfo applicationInfo;
127 std::shared_ptr<AAFwk::AbilityRecord> abilityRecord =
128 std::make_shared<AAFwk::AbilityRecord>(want, abilityInfo, applicationInfo);
129 auto extensionRecordSharedPtr = std::make_shared<AbilityRuntime::ExtensionRecord>(abilityRecord);
130 auto hostPid = extensionRecordSharedPtr->hostPid_ = 10;
131 std::weak_ptr<AbilityRuntime::ExtensionRecord> extensionRecord = extensionRecordSharedPtr;
132 PreLoadUIExtStateObserver preLoadUIExtStateObserver(extensionRecord);
133 AppExecFwk::AppStateData appStateData;
134 int32_t diedPid = appStateData.pid;
135 preLoadUIExtStateObserver.OnAppCacheStateChanged(appStateData);
136 auto record = preLoadUIExtStateObserver.extensionRecord_.lock();
137 EXPECT_TRUE(record != nullptr);
138 EXPECT_TRUE(record->hostPid_ != diedPid);
139 GTEST_LOG_(INFO) << "OnAppCacheStateChanged_002 end";
140 }
141
142 /**
143 * @tc.number: OnAppCacheStateChanged_003
144 * @tc.name: OnAppCacheStateChanged
145 * @tc.desc: Test whether OnAppCacheStateChanged is called normally.
146 * @tc.type: FUNC
147 */
148 HWTEST_F(PreloadUIextStateObserverTest, OnAppCacheStateChanged_003, Function | MediumTest | Level1)
149 {
150 GTEST_LOG_(INFO) << "OnAppCacheStateChanged_003 start";
151 Want want;
152 AppExecFwk::AbilityInfo abilityInfo;
153 AppExecFwk::ApplicationInfo applicationInfo;
154 std::shared_ptr<AAFwk::AbilityRecord> abilityRecord =
155 std::make_shared<AAFwk::AbilityRecord>(want, abilityInfo, applicationInfo);
156 auto extensionRecordSharedPtr = std::make_shared<AbilityRuntime::ExtensionRecord>(abilityRecord);
157 std::weak_ptr<AbilityRuntime::ExtensionRecord> extensionRecord = extensionRecordSharedPtr;
158 PreLoadUIExtStateObserver preLoadUIExtStateObserver(extensionRecord);
159 AppExecFwk::AppStateData appStateData;
160 int32_t diedPid = appStateData.pid;
161 preLoadUIExtStateObserver.OnAppCacheStateChanged(appStateData);
162 auto record = preLoadUIExtStateObserver.extensionRecord_.lock();
163 EXPECT_TRUE(record != nullptr);
164 EXPECT_TRUE(record->hostPid_ != diedPid);
165 GTEST_LOG_(INFO) << "OnAppCacheStateChanged_003 end";
166 }
167 } // namespace AbilityRuntime
168 } // namespace OHOS
169