• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "app_state_observer.h"
19 #include "mmi_log.h"
20 
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "ApplicationStateObserverTest"
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 using namespace testing::ext;
27 } // namespace
28 
29 class ApplicationStateObserverTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void) {}
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
33 
SetUp()34     void SetUp() {}
TearDown()35     void TearDown() {}
36 };
37 
38 /**
39  * @tc.name: ApplicationStateObserverTest_ForegroundAppData_001
40  * @tc.desc: Verify the SetForegroundAppData and GetForegroundAppData functions
41  * @tc.type: FUNC
42  * @tc.require:
43  */
44 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_ForegroundAppData_001, TestSize.Level1)
45 {
46     CALL_DEBUG_ENTER;
47     std::vector<AppExecFwk::AppStateData> appDatas {};
48     APP_OBSERVER_MGR->SetForegroundAppData(appDatas);
49     auto result = APP_OBSERVER_MGR->GetForegroundAppData();
50     EXPECT_EQ(appDatas.size(), result.size());
51 }
52 
53 /**
54  * @tc.name: ApplicationStateObserverTest_InitAppStateObserver_001
55  * @tc.desc: Verify the action of the InitAppStateObserver function when hasInit_ is true and false respectively
56  * @tc.type: FUNC
57  * @tc.require:
58  */
59 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_InitAppStateObserver_001, TestSize.Level1)
60 {
61     CALL_DEBUG_ENTER;
62     APP_OBSERVER_MGR->hasInit_ = true;
63     ASSERT_NO_FATAL_FAILURE(APP_OBSERVER_MGR->InitAppStateObserver());
64     APP_OBSERVER_MGR->hasInit_ = false;
65     ASSERT_NO_FATAL_FAILURE(APP_OBSERVER_MGR->InitAppStateObserver());
66 }
67 
68 /**
69  * @tc.name: ApplicationStateObserverTest_GetAppMgr_001
70  * @tc.desc: Verify the first and non-first entry into the GetAppMgr function
71  * @tc.type: FUNC
72  * @tc.require:
73  */
74 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_GetAppMgr_001, TestSize.Level1)
75 {
76     CALL_DEBUG_ENTER;
77     ApplicationStateObserver obsever;
78     auto appManager = obsever.GetAppMgr();
79     EXPECT_NE(appManager, nullptr);
80     appManager = obsever.GetAppMgr();
81     EXPECT_NE(appManager, nullptr);
82 }
83 
84 /**
85  * @tc.name: ApplicationStateObserverTest_GetForegroundApplicationInfo_001
86  * @tc.desc: Verify the results of obtaining foreground application information
87  * @tc.type: FUNC
88  * @tc.require:
89  */
90 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_GetForegroundApplicationInfo_001, TestSize.Level1)
91 {
92     CALL_DEBUG_ENTER;
93     ApplicationStateObserver obsever;
94     std::vector<AppExecFwk::AppStateData> appDatas {};
95     int32_t ret = obsever.GetForegroundApplicationInfo(appDatas);
96     EXPECT_EQ(ret, RET_OK);
97     EXPECT_FALSE(appDatas.empty());
98     auto appManager = obsever.GetAppMgr();
99     EXPECT_NE(appManager, nullptr);
100     std::vector<AppExecFwk::AppStateData> appStateDatas {};
101     ret = obsever.GetForegroundApplicationInfo(appStateDatas);
102     EXPECT_EQ(ret, RET_OK);
103 }
104 
105 /**
106  * @tc.name: ApplicationStateObserverTest_GetAppMgr_002
107  * @tc.desc: Verify the first and non-first entry into the GetAppMgr function
108  * @tc.type: FUNC
109  * @tc.require:
110  */
111 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_GetAppMgr_002, TestSize.Level1)
112 {
113     CALL_DEBUG_ENTER;
114     ApplicationStateObserver observer;
115     auto appMgrFirst = observer.GetAppMgr();
116     auto appMgrSecond = observer.GetAppMgr();
117     EXPECT_EQ(appMgrFirst, appMgrSecond);
118     EXPECT_EQ(appMgrSecond, observer.GetAppMgr());
119     if (appMgrFirst != nullptr) {
120         EXPECT_NE(appMgrFirst->AsObject(), nullptr);
121     }
122 }
123 
124 /**
125  * @tc.name: ApplicationStateObserverTest_InitAppStateObserver_002
126  * @tc.desc: Verify the action of the InitAppStateObserver function when hasInit_ is true and false respectively
127  * @tc.type: FUNC
128  * @tc.require:
129  */
130 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_InitAppStateObserver_002, TestSize.Level1)
131 {
132     CALL_DEBUG_ENTER;
133     APP_OBSERVER_MGR->hasInit_ = true;
134     ASSERT_NO_FATAL_FAILURE(APP_OBSERVER_MGR->InitAppStateObserver());
135 
136     APP_OBSERVER_MGR->hasInit_ = false;
137     ASSERT_NO_FATAL_FAILURE(APP_OBSERVER_MGR->InitAppStateObserver());
138 }
139 
140 /**
141  * @tc.name: ApplicationStateObserverTest_InitAppStateObserver_003
142  * @tc.desc: Verify InitAppStateObserver when CheckSystemAbility returns nullptr
143  * @tc.type: FUNC
144  * @tc.require:
145  */
146 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_InitAppStateObserver_003, TestSize.Level1)
147 {
148     CALL_TEST_DEBUG;
149     APP_OBSERVER_MGR->hasInit_ = false;
150 
151     auto sysMgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
152     ASSERT_TRUE(sysMgr != nullptr);
153     ASSERT_NO_FATAL_FAILURE(APP_OBSERVER_MGR->InitAppStateObserver());
154 }
155 
156 /**
157  * @tc.name: ApplicationStateObserverTest_InitAppStateObserver_004
158  * @tc.desc: Verify InitAppStateObserver when iface_cast<IAppMgr>() returns nullptr
159  * @tc.type: FUNC
160  * @tc.require:
161  */
162 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_InitAppStateObserver_004, TestSize.Level1)
163 {
164     CALL_TEST_DEBUG;
165     APP_OBSERVER_MGR->hasInit_ = false;
166 
167     OHOS::sptr<OHOS::ISystemAbilityManager> sysMgr =
168         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
169     ASSERT_TRUE(sysMgr != nullptr);
170     OHOS::sptr<OHOS::IRemoteObject> obj = sysMgr->CheckSystemAbility(OHOS::APP_MGR_SERVICE_ID);
171     ASSERT_TRUE(obj != nullptr);
172     ASSERT_NO_FATAL_FAILURE(APP_OBSERVER_MGR->InitAppStateObserver());
173 }
174 
175 /**
176  * @tc.name: ApplicationStateObserverTest_InitAppStateObserver_005
177  * @tc.desc: Verify InitAppStateObserver when RegisterApplicationStateObserver fails
178  * @tc.type: FUNC
179  * @tc.require:
180  */
181 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_InitAppStateObserver_005, TestSize.Level1)
182 {
183     CALL_DEBUG_ENTER;
184     APP_OBSERVER_MGR->hasInit_ = false;
185     ASSERT_NO_FATAL_FAILURE(APP_OBSERVER_MGR->InitAppStateObserver());
186     EXPECT_FALSE(APP_OBSERVER_MGR->hasInit_);
187 }
188 
189 /**
190  * @tc.name: AppObserverManagerTest_SetForegroundAppData_001
191  * @tc.desc: Verify SetForegroundAppData handles a filled AppStateData list without error
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(ApplicationStateObserverTest, AppObserverManagerTest_SetForegroundAppData_001, TestSize.Level1)
196 {
197     CALL_TEST_DEBUG;
198     std::vector<AppExecFwk::AppStateData> inputList;
199 
200     AppExecFwk::AppStateData data1;
201     data1.isFocused = true;
202     data1.isSplitScreenMode = false;
203     data1.isFloatingWindowMode = false;
204     data1.isSpecifyTokenId = true;
205     data1.isPreloadModule = false;
206     data1.pid = 1234;
207     data1.uid = 1000;
208     data1.callerUid = 1001;
209     data1.state = 1;
210     data1.appIndex = 0;
211     data1.accessTokenId = 123456;
212     data1.extensionType = AppExecFwk::ExtensionAbilityType::SERVICE;
213     data1.renderPids = {2001, 2002};
214     data1.bundleName = "com.example.app1";
215     data1.callerBundleName = "com.example.caller1";
216 
217     AppExecFwk::AppStateData data2;
218     data2.isFocused = false;
219     data2.isSplitScreenMode = true;
220     data2.isFloatingWindowMode = true;
221     data2.isSpecifyTokenId = false;
222     data2.isPreloadModule = true;
223     data2.pid = 5678;
224     data2.uid = 2000;
225     data2.callerUid = 2001;
226     data2.state = 2;
227     data2.appIndex = 1;
228     data2.accessTokenId = 654321;
229     data2.extensionType = AppExecFwk::ExtensionAbilityType::UI;
230     data2.renderPids = {3001, 3002};
231     data2.bundleName = "com.example.app2";
232     data2.callerBundleName = "com.example.caller2";
233 
234     inputList.push_back(data1);
235     inputList.push_back(data2);
236 
237     ASSERT_NO_FATAL_FAILURE(APP_OBSERVER_MGR->SetForegroundAppData(inputList));
238 }
239 
240 /**
241  * @tc.name: AppObserverManagerTest_GetForegroundAppData_001
242  * @tc.desc: Verify GetForegroundAppData returns the expected list set by SetForegroundAppData
243  * @tc.type: FUNC
244  * @tc.require:
245  */
246 HWTEST_F(ApplicationStateObserverTest, AppObserverManagerTest_GetForegroundAppData_001, TestSize.Level1)
247 {
248     CALL_TEST_DEBUG;
249 
250     std::vector<AppExecFwk::AppStateData> inputList;
251 
252     AppExecFwk::AppStateData data;
253     data.isFocused = true;
254     data.pid = 1234;
255     data.bundleName = "com.test.app";
256     inputList.push_back(data);
257 
258     APP_OBSERVER_MGR->SetForegroundAppData(inputList);
259     std::vector<AppExecFwk::AppStateData> outputList = APP_OBSERVER_MGR->GetForegroundAppData();
260 
261     ASSERT_EQ(outputList.size(), 1);
262     EXPECT_EQ(outputList[0].isFocused, true);
263     EXPECT_EQ(outputList[0].pid, 1234);
264     EXPECT_EQ(outputList[0].bundleName, "com.test.app");
265 }
266 
267 /**
268  * @tc.name: ApplicationStateObserverTest_GetForegroundApplicationInfo_002
269  * @tc.desc: Verify GetForegroundApplicationInfo returns RET_ERR when appMgr is null
270  * @tc.type: FUNC
271  * @tc.require:
272  */
273 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_GetForegroundApplicationInfo_002, TestSize.Level1)
274 {
275     CALL_TEST_DEBUG;
276 
277     ApplicationStateObserver observer;
278     std::vector<AppExecFwk::AppStateData> list;
279     int32_t ret = observer.GetForegroundApplicationInfo(list);
280     EXPECT_TRUE(ret == RET_OK || ret == RET_ERR);
281 }
282 
283 /**
284  * @tc.name: ApplicationStateObserverTest_GetForegroundApplicationInfo_003
285  * @tc.desc: Verify GetForegroundApplicationInfo fills list when appMgr is available
286  * @tc.type: FUNC
287  * @tc.require:
288  */
289 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_GetForegroundApplicationInfo_003, TestSize.Level1)
290 {
291     CALL_TEST_DEBUG;
292 
293     ApplicationStateObserver observer;
294     std::vector<AppExecFwk::AppStateData> list;
295     int32_t ret = observer.GetForegroundApplicationInfo(list);
296     EXPECT_TRUE(ret == RET_OK || ret == RET_ERR);
297     if (ret == RET_OK) {
298         EXPECT_GT(list.size(), 0);
299         for (const auto& item : list) {
300             EXPECT_GE(item.pid, 0);
301             EXPECT_FALSE(item.bundleName.empty());
302         }
303         auto stored = APP_OBSERVER_MGR->GetForegroundAppData();
304         EXPECT_EQ(stored.size(), list.size());
305     }
306 }
307 
308 /**
309  * @tc.name: ApplicationStateObserverTest_OnProcessStateChanged_001
310  * @tc.desc: Verify OnProcessStateChanged handles valid processData and invokes GetForegroundApplicationInfo
311  * @tc.type: FUNC
312  * @tc.require:
313  */
314 HWTEST_F(ApplicationStateObserverTest, ApplicationStateObserverTest_OnProcessStateChanged_001, TestSize.Level1)
315 {
316     CALL_DEBUG_ENTER;
317     ApplicationStateObserver observer;
318     AppExecFwk::ProcessData processData;
319     processData.bundleName = "com.example.app";
320     processData.uid = 10001;
321     processData.pid = 1234;
322     processData.state = AppExecFwk::AppProcessState::APP_STATE_READY;
323     ASSERT_NO_FATAL_FAILURE(observer.OnProcessStateChanged(processData));
324 }
325 } // namespace MMI
326 } // namespace OHOS