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 #include <memory>
18
19 #define private public
20 #define protected public
21 #include "ability_connect_manager.h"
22 #undef private
23 #undef protected
24
25 #include "ability_config.h"
26 #include "ability_manager_errors.h"
27 #include "ability_scheduler.h"
28 #include "ability_util.h"
29 #include "bundlemgr/mock_bundle_manager.h"
30 #include "hilog_tag_wrapper.h"
31 #include "mock_ability_connect_callback.h"
32 #include "mock_sa_call.h"
33 #include "mock_task_handler_wrap.h"
34 #include "sa_mgr_client.h"
35 #include "system_ability_definition.h"
36 #include "session/host/include/session.h"
37 #include <thread>
38 #include <chrono>
39
40 using namespace testing::ext;
41 using namespace OHOS::AppExecFwk;
42 using testing::_;
43 using testing::Invoke;
44 using testing::Return;
45 using testing::SetArgReferee;
46 using ::testing::DoAll;
47
48 namespace {
49 const std::string PARAM_RESV_CALLER_APP_ID("ohos.aafwk.param.callerAppId");
50 constexpr uint32_t FAKE_TOKENID = 111;
51 }
52
53 namespace OHOS {
54 namespace AAFwk {
55 class AbilityConnectManagerTest : public testing::Test {
56 public:
57 static void SetUpTestCase(void);
58 static void TearDownTestCase(void);
59 void SetUp();
60 void TearDown();
61
62 AbilityConnectManager* ConnectManager() const;
63 std::shared_ptr<MockTaskHandlerWrap> TaskHandler() const;
64 std::shared_ptr<EventHandlerWrap> EventHandler() const;
65
66 AbilityRequest GenerateAbilityRequest(const std::string& deviceName, const std::string& abilityName,
67 const std::string& appName, const std::string& bundleName, const std::string& moduleName);
68
69 sptr<SessionInfo> MockSessionInfo(int32_t persistentId);
70 std::shared_ptr<AbilityRecord> InitAbilityRecord();
71 std::shared_ptr<MockTaskHandlerWrap> taskHandler_;
72 protected:
73 AbilityRequest abilityRequest_{};
74 AbilityRequest abilityRequest1_{};
75 AbilityRequest abilityRequest2_{};
76 std::shared_ptr<AbilityRecord> serviceRecord_{ nullptr };
77 std::shared_ptr<AbilityRecord> serviceRecord1_{ nullptr };
78 std::shared_ptr<AbilityRecord> serviceRecord2_{ nullptr };
79 OHOS::sptr<Token> serviceToken_{ nullptr };
80 OHOS::sptr<Token> serviceToken1_{ nullptr };
81 OHOS::sptr<Token> serviceToken2_{ nullptr };
82 OHOS::sptr<IAbilityConnection> callbackA_{ nullptr };
83 OHOS::sptr<IAbilityConnection> callbackB_{ nullptr };
84
85 private:
86 std::shared_ptr<AbilityConnectManager> connectManager_;
87 std::shared_ptr<EventHandlerWrap> eventHandler_;
88 };
89
GenerateAbilityRequest(const std::string & deviceName,const std::string & abilityName,const std::string & appName,const std::string & bundleName,const std::string & moduleName)90 AbilityRequest AbilityConnectManagerTest::GenerateAbilityRequest(const std::string& deviceName,
91 const std::string& abilityName, const std::string& appName, const std::string& bundleName,
92 const std::string& moduleName)
93 {
94 ElementName element(deviceName, bundleName, abilityName, moduleName);
95 Want want;
96 want.SetElement(element);
97
98 AbilityInfo abilityInfo;
99 abilityInfo.visible = true;
100 abilityInfo.applicationName = appName;
101 abilityInfo.type = AbilityType::SERVICE;
102 abilityInfo.name = abilityName;
103 abilityInfo.bundleName = bundleName;
104 abilityInfo.moduleName = moduleName;
105 abilityInfo.deviceId = deviceName;
106 ApplicationInfo appinfo;
107 appinfo.name = appName;
108 abilityInfo.applicationInfo = appinfo;
109 AbilityRequest abilityRequest;
110 abilityRequest.want = want;
111 abilityRequest.abilityInfo = abilityInfo;
112 abilityRequest.appInfo = appinfo;
113 abilityInfo.process = bundleName;
114
115 return abilityRequest;
116 }
117
MockSessionInfo(int32_t persistentId)118 sptr<SessionInfo> AbilityConnectManagerTest::MockSessionInfo(int32_t persistentId)
119 {
120 sptr<SessionInfo> sessionInfo = new (std::nothrow) SessionInfo();
121 if (!sessionInfo) {
122 TAG_LOGE(AAFwkTag::TEST, "sessionInfo is nullptr");
123 return nullptr;
124 }
125 sessionInfo->persistentId = persistentId;
126 return sessionInfo;
127 }
128
InitAbilityRecord()129 std::shared_ptr<AbilityRecord> AbilityConnectManagerTest::InitAbilityRecord()
130 {
131 AbilityRequest abilityRequest;
132 abilityRequest.appInfo.bundleName = "com.example.unittest";
133 abilityRequest.abilityInfo.name = "MainAbility";
134 abilityRequest.abilityInfo.type = AbilityType::PAGE;
135 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
136 return abilityRecord;
137 }
138
SetUpTestCase(void)139 void AbilityConnectManagerTest::SetUpTestCase(void)
140 {}
141
TearDownTestCase(void)142 void AbilityConnectManagerTest::TearDownTestCase(void)
143 {}
144
SetUp(void)145 void AbilityConnectManagerTest::SetUp(void)
146 {
147 connectManager_ = std::make_unique<AbilityConnectManager>(0);
148 taskHandler_ = MockTaskHandlerWrap::CreateQueueHandler("AbilityConnectManagerTest");
149 eventHandler_ = std::make_shared<EventHandlerWrap>(taskHandler_);
150 // generate ability request
151 std::string deviceName = "device";
152 std::string abilityName = "ServiceAbility";
153 std::string appName = "hiservcie";
154 std::string bundleName = "com.ix.hiservcie";
155 std::string moduleName = "entry";
156 abilityRequest_ = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName);
157 serviceRecord_ = AbilityRecord::CreateAbilityRecord(abilityRequest_);
158 serviceToken_ = serviceRecord_->GetToken();
159 std::string deviceName1 = "device";
160 std::string abilityName1 = "musicServiceAbility";
161 std::string appName1 = "musicservcie";
162 std::string bundleName1 = "com.ix.musicservcie";
163 std::string moduleName1 = "entry";
164 abilityRequest1_ = GenerateAbilityRequest(deviceName1, abilityName1, appName1, bundleName1, moduleName1);
165 serviceRecord1_ = AbilityRecord::CreateAbilityRecord(abilityRequest1_);
166 std::string deviceName2 = "device";
167 std::string abilityName2 = "residentServiceAbility";
168 std::string appName2 = "residentservcie";
169 std::string bundleName2 = "com.ix.residentservcie";
170 std::string moduleName2 = "entry";
171 abilityRequest2_ = GenerateAbilityRequest(deviceName2, abilityName2, appName2, bundleName2, moduleName2);
172 serviceRecord2_ = AbilityRecord::CreateAbilityRecord(abilityRequest2_);
173 serviceToken2_ = serviceRecord_->GetToken();
174 serviceToken1_ = serviceRecord_->GetToken();
175 callbackA_ = new AbilityConnectCallback();
176 callbackB_ = new AbilityConnectCallback();
177 }
178
TearDown(void)179 void AbilityConnectManagerTest::TearDown(void)
180 {
181 // reset the callback count
182 AbilityConnectCallback::onAbilityConnectDoneCount = 0;
183 AbilityConnectCallback::onAbilityDisconnectDoneCount = 0;
184 serviceRecord_ = nullptr;
185 }
186
ConnectManager() const187 AbilityConnectManager* AbilityConnectManagerTest::ConnectManager() const
188 {
189 return connectManager_.get();
190 }
191
TaskHandler() const192 std::shared_ptr<MockTaskHandlerWrap> AbilityConnectManagerTest::TaskHandler() const
193 {
194 return taskHandler_;
195 }
196
EventHandler() const197 std::shared_ptr<EventHandlerWrap> AbilityConnectManagerTest::EventHandler() const
198 {
199 return eventHandler_;
200 }
201
202 /*
203 * Feature: AbilityConnectManager
204 * Function: GetUIExtensionBySessionInfo
205 */
206 HWTEST_F(AbilityConnectManagerTest, GetUIExtensionBySessionInfo_001, TestSize.Level1)
207 {
208 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionBySessionInfo_001 start");
209 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
210 EXPECT_NE(connectManager, nullptr);
211 connectManager->uiExtensionMap_.clear();
212 Rosen::SessionInfo info;
213 sptr<SessionInfo> sessionInfo(new SessionInfo());
214 sessionInfo->sessionToken = new Rosen::Session(info);
215 auto service = connectManager->GetUIExtensionBySessionInfo(sessionInfo);
216 std::string device = "device";
217 std::string abilityName1 = "uiExtensionAbility1";
218 std::string appName1 = "uiExtensionProvider1";
219 std::string bundleName1 = "com.ix.uiExtensionProvider1";
220 std::string moduleName1 = "entry";
221 auto request1 = GenerateAbilityRequest(device, abilityName1, appName1, bundleName1, moduleName1);
222 auto uiExtension1 = AbilityRecord::CreateAbilityRecord(request1);
223 EXPECT_NE(uiExtension1, nullptr);
224 uiExtension1->abilityInfo_.extensionAbilityType = ExtensionAbilityType::SYS_COMMON_UI;
225 uiExtension1 = nullptr;
226 connectManager->uiExtensionMap_.emplace(
227 sessionInfo->sessionToken, AbilityConnectManager::UIExtWindowMapValType(uiExtension1, sessionInfo));
228 service = connectManager->GetUIExtensionBySessionInfo(sessionInfo);
229 EXPECT_EQ(service, nullptr);
230 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionBySessionInfo_001 end");
231 }
232
233 /*
234 * Feature: AbilityConnectManager
235 * Function: GetUIExtensionBySessionInfo
236 */
237 HWTEST_F(AbilityConnectManagerTest, GetUIExtensionBySessionInfo_002, TestSize.Level1)
238 {
239 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionBySessionInfo_002 start");
240 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
241 EXPECT_NE(connectManager, nullptr);
242 connectManager->uiExtensionMap_.clear();
243 Rosen::SessionInfo info;
244 sptr<SessionInfo> sessionInfo(new SessionInfo());
245 sessionInfo->sessionToken = new Rosen::Session(info);
246 sessionInfo->callerToken = new Rosen::Session(info);
247 std::string device = "device";
248 std::string abilityName1 = "uiExtensionAbility1";
249 std::string appName1 = "uiExtensionProvider1";
250 std::string bundleName1 = "com.ix.uiExtensionProvider1";
251 std::string moduleName1 = "entry";
252 auto request1 = GenerateAbilityRequest(device, abilityName1, appName1, bundleName1, moduleName1);
253 auto uiExtension1 = AbilityRecord::CreateAbilityRecord(request1);
254 EXPECT_NE(uiExtension1, nullptr);
255 uiExtension1->abilityInfo_.extensionAbilityType = ExtensionAbilityType::SYS_COMMON_UI;
256 Rosen::SessionInfo infos;
257 sptr<SessionInfo> sessionInfo1(new SessionInfo());
258 sessionInfo1 = nullptr;
259 connectManager->uiExtensionMap_.emplace(
260 sessionInfo->sessionToken, AbilityConnectManager::UIExtWindowMapValType(uiExtension1, sessionInfo1));
261 auto service = connectManager->GetUIExtensionBySessionInfo(sessionInfo);
262 EXPECT_EQ(service, nullptr);
263 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionBySessionInfo_002 end");
264 }
265
266 /*
267 * Feature: AbilityConnectManager
268 * Function: GetUIExtensionBySessionInfo
269 */
270 HWTEST_F(AbilityConnectManagerTest, GetUIExtensionBySessionInfo_003, TestSize.Level1)
271 {
272 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionBySessionInfo_003 start");
273 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
274 EXPECT_NE(connectManager, nullptr);
275 connectManager->uiExtensionMap_.clear();
276 Rosen::SessionInfo info;
277 sptr<SessionInfo> sessionInfo(new SessionInfo());
278 sessionInfo->sessionToken = new Rosen::Session(info);
279 sessionInfo->callerToken = new Rosen::Session(info);
280 std::string device = "device";
281 std::string abilityName1 = "uiExtensionAbility1";
282 std::string appName1 = "uiExtensionProvider1";
283 std::string bundleName1 = "com.ix.uiExtensionProvider1";
284 std::string moduleName1 = "entry";
285 auto request1 = GenerateAbilityRequest(device, abilityName1, appName1, bundleName1, moduleName1);
286 auto uiExtension1 = AbilityRecord::CreateAbilityRecord(request1);
287 EXPECT_NE(uiExtension1, nullptr);
288 uiExtension1->abilityInfo_.extensionAbilityType = ExtensionAbilityType::SYS_COMMON_UI;
289
290 Rosen::SessionInfo infos;
291 sptr<SessionInfo> sessionInfo1(new SessionInfo());
292 sessionInfo1->sessionToken = new Rosen::Session(info);
293 sessionInfo1->callerToken = uiExtension1->GetToken();
294 connectManager->uiExtensionMap_.emplace(
295 sessionInfo->sessionToken, AbilityConnectManager::UIExtWindowMapValType(uiExtension1, sessionInfo1));
296 auto service = connectManager->GetUIExtensionBySessionInfo(sessionInfo);
297 EXPECT_EQ(service, nullptr);
298 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionBySessionInfo_003 end");
299 }
300
301 /*
302 * Feature: AbilityConnectManager
303 * Function: GetUIExtensionBySessionInfo
304 */
305 HWTEST_F(AbilityConnectManagerTest, GetUIExtensionBySessionInfo_004, TestSize.Level1)
306 {
307 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionBySessionInfo_004 start");
308 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
309 EXPECT_NE(connectManager, nullptr);
310 connectManager->uiExtensionMap_.clear();
311 Rosen::SessionInfo info;
312 sptr<SessionInfo> sessionInfo(new SessionInfo());
313 sessionInfo->sessionToken = new Rosen::Session(info);
314 std::string device = "device";
315 std::string abilityName1 = "uiExtensionAbility1";
316 std::string appName1 = "uiExtensionProvider1";
317 std::string bundleName1 = "com.ix.uiExtensionProvider1";
318 std::string moduleName1 = "entry";
319 auto request1 = GenerateAbilityRequest(device, abilityName1, appName1, bundleName1, moduleName1);
320 auto uiExtension1 = AbilityRecord::CreateAbilityRecord(request1);
321 EXPECT_NE(uiExtension1, nullptr);
322 uiExtension1->abilityInfo_.extensionAbilityType = ExtensionAbilityType::SYS_COMMON_UI;
323 Rosen::SessionInfo infos;
324 sptr<SessionInfo> sessionInfo1(new SessionInfo());
325 sessionInfo1->sessionToken = uiExtension1->GetToken();
326 sessionInfo1->callerToken = new Rosen::Session(info);
327 connectManager->uiExtensionMap_.emplace(
328 sessionInfo->sessionToken, AbilityConnectManager::UIExtWindowMapValType(uiExtension1, sessionInfo1));
329 auto service = connectManager->GetUIExtensionBySessionInfo(sessionInfo);
330 EXPECT_EQ(service, nullptr);
331 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionBySessionInfo_004 end");
332 }
333
334 /*
335 * Feature: AbilityConnectManager
336 * Function: HandleActiveAbility
337 */
338 HWTEST_F(AbilityConnectManagerTest, HandleActiveAbility_001, TestSize.Level1)
339 {
340 TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_001 start");
341 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
342 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
343 AbilityRequest abilityRequest;
344 sptr<IRemoteObject> callerToken = abilityRecord->GetToken();
345 OHOS::sptr<IAbilityConnection> callback1 = new AbilityConnectCallback();
346 std::shared_ptr<ConnectionRecord> connectRecord =
347 std::make_shared<ConnectionRecord>(callerToken, abilityRecord, callback1, nullptr);
348 std::shared_ptr<AbilityRecord> targetService = nullptr;
349 connectManager->HandleActiveAbility(targetService, connectRecord);
350 abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI_SERVICE;
351 Want want;
352 want.SetParam(PARAM_RESV_CALLER_APP_ID, std::string("app"));
353 abilityRecord->SetWant(want);
354 EXPECT_EQ(abilityRecord->GetWant().GetStringParam(PARAM_RESV_CALLER_APP_ID), "app");
355 TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_001 end");
356 }
357
358 /*
359 * Feature: AbilityConnectManager
360 * Function: HandleActiveAbility
361 */
362 HWTEST_F(AbilityConnectManagerTest, HandleActiveAbility_002, TestSize.Level1)
363 {
364 TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_002 start");
365 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
366 EXPECT_NE(connectManager, nullptr);
367 auto result = connectManager->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
368 EXPECT_EQ(0, result);
369 auto connectMap = connectManager->connectMap_;
370 auto connectRecordList = connectMap.at(callbackA_->AsObject());
371 EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
372 auto elementName = abilityRequest_.want.GetElement();
373 auto elementNameUri = elementName.GetURI();
374 auto serviceMap = connectManager->GetServiceMap();
375 std::shared_ptr<AbilityRecord> abilityRecord = serviceMap.at(elementNameUri);
376 //AbilityRequest abilityRequest;
377 sptr<IRemoteObject> callerToken = abilityRecord->GetToken();
378 Want want;
379 want.SetParam(PARAM_RESV_CALLER_APP_ID, std::string("app"));
380 abilityRecord->SetWant(want);
381 OHOS::sptr<IAbilityConnection> callback1 = new AbilityConnectCallback();
382 auto connectRecord = std::make_shared<ConnectionRecord>(callerToken, abilityRecord, callback1, nullptr);
383 abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI;
384 connectManager->HandleActiveAbility(abilityRecord, connectRecord);
385 EXPECT_EQ(abilityRecord->GetWant().GetStringParam(PARAM_RESV_CALLER_APP_ID), "app"); // no remove signatureInfo
386 TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_002 end");
387 }
388
389 /*
390 * Feature: AbilityConnectManager
391 * Function: HandleActiveAbility
392 */
393 HWTEST_F(AbilityConnectManagerTest, HandleActiveAbility_003, TestSize.Level1)
394 {
395 TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_003 start");
396 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
397 EXPECT_NE(connectManager, nullptr);
398
399 auto result = connectManager->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
400 EXPECT_EQ(0, result);
401
402 result = connectManager->ConnectAbilityLocked(abilityRequest_, callbackB_, nullptr);
403 EXPECT_EQ(0, result);
404
405 auto connectMap = connectManager->connectMap_;
406 auto connectRecordList = connectMap.at(callbackA_->AsObject());
407 EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
408
409 connectRecordList = connectMap.at(callbackB_->AsObject());
410 EXPECT_EQ(1, static_cast<int>(connectRecordList.size()));
411
412 auto elementName = abilityRequest_.want.GetElement();
413 std::string elementNameUri = elementName.GetURI();
414 auto serviceMap = connectManager->GetServiceMap();
415 auto abilityRecord = serviceMap.at(elementNameUri);
416 connectRecordList = abilityRecord->GetConnectRecordList();
417 EXPECT_EQ(2, static_cast<int>(connectRecordList.size()));
418 //AbilityRequest abilityRequest;
419 sptr<IRemoteObject> callerToken = abilityRecord->GetToken();
420 OHOS::sptr<IAbilityConnection> callback1 = new AbilityConnectCallback();
421 auto connectRecord = std::make_shared<ConnectionRecord>(callerToken, abilityRecord, callback1, nullptr);
422 auto mockTaskHandler = MockTaskHandlerWrap::CreateQueueHandler("HandleActiveAbility003");
423 EXPECT_CALL(*mockTaskHandler, SubmitTaskInner(_, _)).Times(0);
424 connectManager->taskHandler_ = mockTaskHandler;
425 abilityRecord->connRemoteObject_ = abilityRecord->GetToken();
426 connectManager->HandleActiveAbility(abilityRecord, connectRecord);
427 TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_003 end");
428 }
429
430 /*
431 * Feature: AbilityConnectManager
432 * Function: HandleRestartResidentTask
433 */
434 HWTEST_F(AbilityConnectManagerTest, HandleRestartResidentTask_001, TestSize.Level1)
435 {
436 TAG_LOGI(AAFwkTag::TEST, "HandleRestartResidentTask_001 start");
437 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
438 EXPECT_NE(connectManager, nullptr);
439
440 std::string deviceName = "device";
441 std::string abilityName = "TestAbility";
442 std::string appName = "hiservcie";
443 std::string bundleName = "com.example.bundle";
444 std::string moduleName = "entry";
445 abilityRequest_ = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName);
446 serviceRecord_ = AbilityRecord::CreateAbilityRecord(abilityRequest_);
447
448 connectManager->HandleRestartResidentTask(abilityRequest_);
449 ASSERT_TRUE(connectManager->restartResidentTaskList_.empty());
450 TAG_LOGI(AAFwkTag::TEST, "HandleRestartResidentTask_001 end");
451 }
452
453 /*
454 * Feature: AbilityConnectManager
455 * Function: HandleRestartResidentTask
456 */
457 HWTEST_F(AbilityConnectManagerTest, HandleRestartResidentTask_002, TestSize.Level1)
458 {
459 TAG_LOGI(AAFwkTag::TEST, "HandleRestartResidentTask_002 start");
460 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
461 EXPECT_NE(connectManager, nullptr);
462
463 connectManager->restartResidentTaskList_.clear();
464 std::string deviceName = "device";
465 std::string abilityName = "TestAbility";
466 std::string appName = "hiservcie";
467 std::string bundleName = "com.example.bundle";
468 std::string moduleName = "entry";
469 abilityRequest_ = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName);
470 serviceRecord_ = AbilityRecord::CreateAbilityRecord(abilityRequest_);
471
472 connectManager->HandleRestartResidentTask(abilityRequest_);
473 ASSERT_EQ(connectManager->restartResidentTaskList_.size(), 0);
474 TAG_LOGI(AAFwkTag::TEST, "HandleRestartResidentTask_002 end");
475 }
476
477 /*
478 * Feature: AbilityConnectManager
479 * Function: IsNeedToRestart
480 */
481 HWTEST_F(AbilityConnectManagerTest, IsNeedToRestart_001, TestSize.Level1)
482 {
483 TAG_LOGI(AAFwkTag::TEST, "IsNeedToRestart_001 start");
484 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
485 EXPECT_NE(connectManager, nullptr);
486
487 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
488 bool result = connectManager->IsNeedToRestart(abilityRecord, "com.example.bundle", "TestAbility");
489 ASSERT_TRUE(result);
490 TAG_LOGI(AAFwkTag::TEST, "IsNeedToRestart_001 end");
491 }
492
493 /*
494 * Feature: AbilityConnectManager
495 * Function: HandleUIExtensionDied
496 */
497 HWTEST_F(AbilityConnectManagerTest, HandleUIExtensionDied_001, TestSize.Level1)
498 {
499 TAG_LOGI(AAFwkTag::TEST, "HandleUIExtensionDied_001 start");
500 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
501 EXPECT_NE(connectManager, nullptr);
502
503 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
504 connectManager->HandleUIExtensionDied(abilityRecord);
505 EXPECT_EQ(connectManager->uiExtensionMap_.size(), 0);
506 TAG_LOGI(AAFwkTag::TEST, "HandleUIExtensionDied_001 end");
507 }
508
509 /*
510 * Feature: AbilityConnectManager
511 * Function: GetUIExtensionSessionInfo
512 */
513 HWTEST_F(AbilityConnectManagerTest, GetUIExtensionSessionInfo_001, TestSize.Level1)
514 {
515 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionSessionInfo_001 start");
516 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
517 EXPECT_NE(connectManager, nullptr);
518
519 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
520 AbilityRequest abilityRequest;
521 sptr<IRemoteObject> callerToken = abilityRecord->GetToken();
522 UIExtensionSessionInfo info;
523
524 int32_t result = connectManager->GetUIExtensionSessionInfo(callerToken, info);
525 ASSERT_EQ(result, OHOS::ERR_INVALID_VALUE);
526 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionSessionInfo_001 end");
527 }
528
529 /*
530 * Feature: AbilityConnectManager
531 * Function: GetUIExtensionSessionInfo
532 */
533 HWTEST_F(AbilityConnectManagerTest, GetUIExtensionSessionInfo_002, TestSize.Level1)
534 {
535 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionSessionInfo_002 start");
536 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
537 ASSERT_NE(connectManager, nullptr);
538
539 sptr<IRemoteObject> nullToken = nullptr;
540 UIExtensionSessionInfo sessionInfo;
541 std::string deviceName = "device";
542 std::string abilityName = "ServiceAbility";
543 std::string appName = "hiservcie";
544 std::string bundleName = "com.ix.hiservcie";
545 std::string moduleName = "entry";
546 abilityRequest_ = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName);
547 serviceRecord_ = AbilityRecord::CreateAbilityRecord(abilityRequest_);
548 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
549 sptr<IRemoteObject> token = abilityRecord->GetToken();
550
551 connectManager->uiExtensionAbilityRecordMgr_ = nullptr;
552 int32_t ret = connectManager->GetUIExtensionSessionInfo(token, sessionInfo);
553 EXPECT_NE(ret, OHOS::ERR_INVALID_VALUE);
554 TAG_LOGI(AAFwkTag::TEST, "GetUIExtensionSessionInfo_002 end");
555 }
556
557 /*
558 * Feature: AbilityConnectManager
559 * Function: IsStrictMode
560 */
561 HWTEST_F(AbilityConnectManagerTest, IsStrictMode_001, TestSize.Level1)
562 {
563 TAG_LOGI(AAFwkTag::TEST, "IsStrictMode_001 start");
564 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
565 EXPECT_NE(connectManager, nullptr);
566
567 bool result = connectManager->IsStrictMode(nullptr);
568 ASSERT_FALSE(result);
569 TAG_LOGI(AAFwkTag::TEST, "IsStrictMode_001 end");
570 }
571
572 /*
573 * Feature: AbilityConnectManager
574 * Function: IsStrictMode
575 */
576 HWTEST_F(AbilityConnectManagerTest, IsStrictMode_002, TestSize.Level1)
577 {
578 TAG_LOGI(AAFwkTag::TEST, "IsStrictMode_002 start");
579 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
580 EXPECT_NE(connectManager, nullptr);
581
582 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
583 abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::INPUTMETHOD;
584 Want want;
585 want.SetParam("strictMode", true);
586 abilityRecord->SetWant(want);
587
588 bool result = connectManager->IsStrictMode(abilityRecord);
589 ASSERT_TRUE(result);
590 TAG_LOGI(AAFwkTag::TEST, "IsStrictMode_002 end");
591 }
592
593 /*
594 * Feature: AbilityConnectManager
595 * Function: TerminateAbilityInner
596 */
597 HWTEST_F(AbilityConnectManagerTest, TerminateAbilityInner_001, TestSize.Level1)
598 {
599 TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_001 start");
600 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
601 EXPECT_NE(connectManager, nullptr);
602
603 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest_);
604 auto abilityInfo = abilityRecord->GetAbilityInfo();
605 abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI;
606 auto result = ConnectManager()->TerminateAbilityInner(abilityRecord->GetToken());
607 EXPECT_EQ(ERR_CONNECT_MANAGER_NULL_ABILITY_RECORD, result);
608 TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_001 end");
609 }
610
611 /*
612 * Feature: AbilityConnectManager
613 * Function: TerminateAbilityInner
614 */
615 HWTEST_F(AbilityConnectManagerTest, TerminateAbilityInner_002, TestSize.Level1)
616 {
617 TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_002 start");
618 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
619 EXPECT_NE(connectManager, nullptr);
620
621 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
622 auto abilityInfo = abilityRecord->GetAbilityInfo();
623 abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI;
624 OHOS::sptr<IAbilityConnection> callback = new AbilityConnectCallback();
625 std::shared_ptr<ConnectionRecord> connectionRe =
626 std::make_shared<ConnectionRecord>(abilityRecord->GetToken(), abilityRecord, callback, nullptr);
627 EXPECT_NE(connectionRe, nullptr);
628
629 abilityRecord->currentState_ = AbilityState::FOREGROUND;
630 abilityRecord->connRecordList_.push_back(connectionRe);
631 auto result = connectManager->TerminateAbilityInner(abilityRecord->GetToken());
632 EXPECT_EQ(ERR_CONNECT_MANAGER_NULL_ABILITY_RECORD, result);
633 TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_002 end");
634 }
635
636 /*
637 * Feature: AbilityConnectManager
638 * Function: TerminateAbilityInner
639 */
640 HWTEST_F(AbilityConnectManagerTest, TerminateAbilityInner_003, TestSize.Level1)
641 {
642 TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_003 start");
643 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
644 EXPECT_NE(connectManager, nullptr);
645
646 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
647 auto abilityInfo = abilityRecord->GetAbilityInfo();
648 abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI;
649 abilityRecord->connRecordList_.clear();
650 auto result = connectManager->TerminateAbilityInner(abilityRecord->GetToken());
651 EXPECT_EQ(ERR_CONNECT_MANAGER_NULL_ABILITY_RECORD, result);
652 TAG_LOGI(AAFwkTag::TEST, "TerminateAbilityInner_003 end");
653 }
654
655 /*
656 * Feature: AbilityConnectManager
657 * Function: IsWindowExtensionFocused
658 */
659 HWTEST_F(AbilityConnectManagerTest, IsWindowExtensionFocused_001, TestSize.Level1)
660 {
661 TAG_LOGI(AAFwkTag::TEST, "IsWindowExtensionFocused_001 start");
662 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
663 ASSERT_NE(connectManager, nullptr);
664
665 uint32_t extensionTokenId = 12345;
666 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
667 sptr<IRemoteObject> focusToken = abilityRecord->GetToken();
668 ASSERT_NE(focusToken, nullptr);
669
670 sptr<SessionInfo> sessionInfo = MockSessionInfo(1);
671 sessionInfo->callerToken = focusToken;
672 connectManager->windowExtensionMap_.emplace(focusToken, std::make_pair(extensionTokenId, sessionInfo));
673
674 bool result = connectManager->IsWindowExtensionFocused(extensionTokenId, focusToken);
675 EXPECT_TRUE(result);
676
677 uint32_t invalidTokenId = 54321;
678 result = connectManager->IsWindowExtensionFocused(invalidTokenId, focusToken);
679 EXPECT_FALSE(result);
680 TAG_LOGI(AAFwkTag::TEST, "IsWindowExtensionFocused_001 end");
681 }
682
683 /*
684 * Feature: AbilityConnectManager
685 * Function: IsWindowExtensionFocused
686 */
687 HWTEST_F(AbilityConnectManagerTest, IsWindowExtensionFocused_0002, TestSize.Level1)
688 {
689 TAG_LOGI(AAFwkTag::TEST, "IsWindowExtensionFocused_002 start");
690 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
691 ASSERT_NE(connectManager, nullptr);
692
693 uint32_t extensionTokenId = 12345;
694 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
695 sptr<IRemoteObject> focusToken = abilityRecord->GetToken();
696 ASSERT_NE(focusToken, nullptr);
697
698 sptr<SessionInfo> sessionInfo = MockSessionInfo(1);
699 sessionInfo->callerToken = focusToken;
700 connectManager->windowExtensionMap_.emplace(focusToken, std::make_pair(extensionTokenId, nullptr));
701
702 bool result = connectManager->IsWindowExtensionFocused(extensionTokenId, focusToken);
703 EXPECT_FALSE(result);
704 TAG_LOGI(AAFwkTag::TEST, "IsWindowExtensionFocused_002 end");
705 }
706
707 /*
708 * Feature: AbilityConnectManager
709 * Function: NeedExtensionControl
710 */
711 HWTEST_F(AbilityConnectManagerTest, NeedExtensionControl_001, TestSize.Level1)
712 {
713 TAG_LOGI(AAFwkTag::TEST, "NeedExtensionControl_001 start");
714 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
715 EXPECT_NE(connectManager, nullptr);
716
717 std::shared_ptr<AbilityRecord> nullRecord = nullptr;
718 bool result = connectManager->NeedExtensionControl(nullRecord);
719 EXPECT_FALSE(result);
720
721 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
722 abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::SERVICE;
723 result = connectManager->NeedExtensionControl(abilityRecord);
724 EXPECT_FALSE(result);
725
726 abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::DATASHARE;
727 result = connectManager->NeedExtensionControl(abilityRecord);
728 EXPECT_FALSE(result);
729 TAG_LOGI(AAFwkTag::TEST, "NeedExtensionControl_001 end");
730 }
731
732 /*
733 * Feature: AbilityConnectManager
734 * Function: NeedExtensionControl
735 */
736 HWTEST_F(AbilityConnectManagerTest, NeedExtensionControl_002, TestSize.Level1)
737 {
738 TAG_LOGI(AAFwkTag::TEST, "NeedExtensionControl_002 start");
739 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
740 EXPECT_NE(connectManager, nullptr);
741
742 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
743 abilityRecord->abilityInfo_.extensionAbilityType = AppExecFwk::ExtensionAbilityType::FORM;
744 abilityRecord->SetCustomProcessFlag("");
745 bool result = connectManager->NeedExtensionControl(abilityRecord);
746 EXPECT_TRUE(result);
747
748 abilityRecord->SetCustomProcessFlag("Test customProces");
749 uint32_t extensionProcessMode = 0;
750 abilityRecord->SetExtensionProcessMode(extensionProcessMode);
751 result = connectManager->NeedExtensionControl(abilityRecord);
752 EXPECT_FALSE(result);
753 TAG_LOGI(AAFwkTag::TEST, "NeedExtensionControl_002 end");
754 }
755
756 /*
757 * Feature: AbilityConnectManager
758 * Function: StartAbilityLocked
759 */
760 HWTEST_F(AbilityConnectManagerTest, StartAbilityLocked_001, TestSize.Level1)
761 {
762 TAG_LOGI(AAFwkTag::TEST, "StartAbilityLocked_001 start");
763 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
764 EXPECT_NE(connectManager, nullptr);
765
766 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
767 sptr<IRemoteObject> focusToken = abilityRecord->GetToken();
768 AbilityRequest abilityRequest;
769 abilityRequest.abilityInfo.deviceId = "id";
770 abilityRequest.abilityInfo.bundleName = "bundle";
771 abilityRequest.abilityInfo.name = "name";
772 abilityRequest.abilityInfo.moduleName = "module";
773 abilityRequest.abilityInfo.extensionAbilityType = ExtensionAbilityType::UI;
774 std::string stringUri = "id/bundle/module/name";
775 abilityRequest.callerToken = abilityRecord->GetToken();
776 AppExecFwk::ElementName element(abilityRequest.abilityInfo.deviceId, abilityRequest.abilityInfo.bundleName,
777 abilityRequest.abilityInfo.name, abilityRequest.abilityInfo.moduleName);
778 EXPECT_EQ(element.GetURI(), stringUri);
779 connectManager->uiExtensionAbilityRecordMgr_ = nullptr;
780
781 int result = connectManager->StartAbilityLocked(abilityRequest);
782 EXPECT_NE(result, ERR_OK);
783 TAG_LOGI(AAFwkTag::TEST, "StartAbilityLocked_001 end");
784 }
785
786 /*
787 * Feature: AbilityConnectManager
788 * Function: DoForegroundUIExtension
789 */
790 HWTEST_F(AbilityConnectManagerTest, DoForegroundUIExtension_001, TestSize.Level1)
791 {
792 TAG_LOGI(AAFwkTag::TEST, "DoForegroundUIExtension_001 start");
793 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
794 EXPECT_NE(connectManager, nullptr);
795
796 std::shared_ptr<AbilityRecord> nullRecord = nullptr;
797 AbilityRequest abilityRequest;
798 abilityRequest.abilityInfo.deviceId = "id";
799 abilityRequest.abilityInfo.bundleName = "bundle";
800 abilityRequest.abilityInfo.name = "name";
801 abilityRequest.abilityInfo.moduleName = "module";
802 abilityRequest.abilityInfo.extensionAbilityType = ExtensionAbilityType::UI;
803 connectManager->DoForegroundUIExtension(nullRecord, abilityRequest);
804
805 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
806 abilityRecord->isReady_ = true;
807 abilityRecord->currentState_ = AbilityState::FOREGROUND;
808
809 connectManager->DoForegroundUIExtension(abilityRecord, abilityRequest);
810 std::string expectName = "name";
811 EXPECT_EQ(expectName, abilityRequest.abilityInfo.name);
812 TAG_LOGI(AAFwkTag::TEST, "DoForegroundUIExtension_001 end");
813 }
814
815 /*
816 * Feature: AbilityConnectManager
817 * Function: GetOrCreateExtensionRecord
818 */
819 HWTEST_F(AbilityConnectManagerTest, GetOrCreateExtensionRecord_001, TestSize.Level1)
820 {
821 TAG_LOGI(AAFwkTag::TEST, "GetOrCreateExtensionRecord_001 start");
822 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
823 EXPECT_NE(connectManager, nullptr);
824
825 AbilityRequest abilityRequest;
826 abilityRequest.appInfo.bundleName = "com.example.unittest";
827 abilityRequest.abilityInfo.name = "MainAbility";
828 abilityRequest.abilityInfo.type = AbilityType::PAGE;
829 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
830 std::string hostBundName = "bundleName";
831 bool isCreate = false;
832 bool isLoadedAbility = false;
833 connectManager->uiExtensionAbilityRecordMgr_ = nullptr;
834
835 int32_t result = connectManager->GetOrCreateExtensionRecord(
836 abilityRequest, isCreate, hostBundName, abilityRecord, isLoadedAbility);
837 EXPECT_EQ(result, ERR_NULL_OBJECT);
838 TAG_LOGI(AAFwkTag::TEST, "GetOrCreateExtensionRecord_001 end");
839 }
840
841 /*
842 * Feature: AbilityConnectManager
843 * Function: GetOrCreateExtensionRecord
844 */
845 HWTEST_F(AbilityConnectManagerTest, GetOrCreateServiceRecord_001, TestSize.Level1)
846 {
847 TAG_LOGI(AAFwkTag::TEST, "GetOrCreateServiceRecord_001 start");
848 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
849 EXPECT_NE(connectManager, nullptr);
850
851 AbilityRequest abilityRequest;
852 abilityRequest.abilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI_SERVICE;
853 abilityRequest.abilityInfo.bundleName = "com.example.test";
854 abilityRequest.abilityInfo.name = "TestAbility";
855
856 std::shared_ptr<AbilityRecord> resultService = nullptr;
857 bool isLoadedAbility = false;
858 connectManager->GetOrCreateServiceRecord(abilityRequest, false, resultService, isLoadedAbility);
859
860 ASSERT_NE(resultService, nullptr);
861 EXPECT_EQ(resultService->GetAbilityInfo().name, "TestAbility");
862 TAG_LOGI(AAFwkTag::TEST, "GetOrCreateServiceRecord_001 end");
863 }
864
865 /*
866 * Feature: AbilityConnectManager
867 * Function: GetOrCreateExtensionRecord
868 */
869 HWTEST_F(AbilityConnectManagerTest, UnloadUIExtensionAbility_001, TestSize.Level1)
870 {
871 TAG_LOGI(AAFwkTag::TEST, "UnloadUIExtensionAbility_001 start");
872 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
873 EXPECT_NE(connectManager, nullptr);
874
875 AbilityRequest abilityRequest;
876 abilityRequest.abilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::WORK_SCHEDULER;
877 abilityRequest.abilityInfo.bundleName = "com.example.test";
878 abilityRequest.abilityInfo.name = "TestAbility";
879 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
880 OHOS::sptr<IAbilityConnection> callback = new AbilityConnectCallback();
881 std::shared_ptr<ConnectionRecord> connection =
882 std::make_shared<ConnectionRecord>(abilityRecord->GetToken(), abilityRecord, callback, nullptr);
883 sptr<IRemoteObject> token = abilityRecord->GetToken();
884 connection->SetConnectState(ConnectionState::DISCONNECTING);
885 abilityRecord->abilityInfo_.type = AbilityType::PAGE;
886 abilityRecord->SetAbilityState(AbilityState::INACTIVE);
887 abilityRecord->connRecordList_.push_back(connection);
888 std::string hostBundName = "bundleName";
889
890 int result = connectManager->UnloadUIExtensionAbility(abilityRecord, hostBundName);
891 EXPECT_NE(result, ERR_OK);
892 TAG_LOGI(AAFwkTag::TEST, "UnloadUIExtensionAbility_001 end");
893 }
894
895 /*
896 * Feature: AbilityConnectManager
897 * Function: HandleActiveAbility
898 */
899 HWTEST_F(AbilityConnectManagerTest, HandleActiveAbility_004, TestSize.Level1)
900 {
901 TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_004 start");
902 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
903 EXPECT_NE(connectManager, nullptr);
904
905 AbilityRequest abilityRequest;
906 abilityRequest.abilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::WORK_SCHEDULER;
907 abilityRequest.abilityInfo.bundleName = "com.example.test";
908 abilityRequest.abilityInfo.name = "TestAbility";
909 std::shared_ptr<AbilityRecord> targetService = AbilityRecord::CreateAbilityRecord(abilityRequest);
910 Want want;
911 want.SetParam(PARAM_RESV_CALLER_APP_ID, std::string("app"));
912 targetService->SetWant(want);
913 targetService->abilityInfo_.extensionAbilityType = ExtensionAbilityType::UI_SERVICE;
914 std::shared_ptr<ConnectionRecord> connectRecord = nullptr;
915
916 connectManager->HandleActiveAbility(targetService, connectRecord);
917 EXPECT_NE(targetService->GetWant().GetStringParam(PARAM_RESV_CALLER_APP_ID), "app");
918 TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_004 end");
919 }
920
921 /*
922 * Feature: AbilityConnectManager
923 * Function: HandleActiveAbility
924 */
925 HWTEST_F(AbilityConnectManagerTest, HandleActiveAbility_005, TestSize.Level1)
926 {
927 TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_005 start");
928 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
929 EXPECT_NE(connectManager, nullptr);
930
931 std::shared_ptr<AbilityRecord> abilityRecord = serviceRecord_;
932 AbilityRequest abilityRequest;
933 abilityRequest.abilityInfo.extensionAbilityType = AppExecFwk::ExtensionAbilityType::UI_SERVICE;
934 abilityRequest.abilityInfo.bundleName = "com.example.test";
935 abilityRequest.abilityInfo.name = "TestAbility";
936 std::shared_ptr<AbilityRecord> targetService = AbilityRecord::CreateAbilityRecord(abilityRequest);
937 Want want;
938 want.SetParam(PARAM_RESV_CALLER_APP_ID, std::string("app"));
939 targetService->SetWant(want);
940 sptr<IRemoteObject> callerToken = abilityRecord->GetToken();
941 OHOS::sptr<IAbilityConnection> callback1 = new AbilityConnectCallback();
942 std::shared_ptr<ConnectionRecord> connectRecord =
943 std::make_shared<ConnectionRecord>(callerToken, abilityRecord, callback1, nullptr);
944
945 connectManager->HandleActiveAbility(targetService, connectRecord);
946 EXPECT_NE(targetService->GetWant().GetStringParam(PARAM_RESV_CALLER_APP_ID), "app");
947 TAG_LOGI(AAFwkTag::TEST, "HandleActiveAbility_005 end");
948 }
949
950 /*
951 * Feature: AbilityConnectManager
952 * Function: SuspendExtensionAbilityLocked
953 */
954 HWTEST_F(AbilityConnectManagerTest, SuspendExtensionAbilityLocked_001, TestSize.Level1)
955 {
956 TAG_LOGI(AAFwkTag::TEST, "SuspendExtensionAbilityLocked start");
957 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
958 EXPECT_NE(connectManager, nullptr);
959
960 auto result = connectManager->SuspendExtensionAbilityLocked(callbackA_);
961 EXPECT_EQ(result, OHOS::AAFwk::CONNECTION_NOT_EXIST);
962
963 result = connectManager->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
964 EXPECT_EQ(0, result);
965
966 result = connectManager->SuspendExtensionAbilityLocked(callbackA_);
967 EXPECT_EQ(result, OHOS::AAFwk::INVALID_CONNECTION_STATE);
968
969 auto list = connectManager->GetConnectRecordListByCallback(callbackA_);
970 EXPECT_EQ(static_cast<int>(list.size()), 1);
971
972 for (auto& it : list) {
973 it->SetConnectState(ConnectionState::CONNECTED);
974 }
975
976 auto result3 = connectManager->SuspendExtensionAbilityLocked(callbackA_);
977 EXPECT_EQ(result3, OHOS::ERR_OK);
978 TAG_LOGI(AAFwkTag::TEST, "SuspendExtensionAbilityLocked end");
979 }
980
981 /*
982 * Feature: AbilityConnectManager
983 * Function: ResumeExtensionAbilityLocked
984 */
985 HWTEST_F(AbilityConnectManagerTest, ResumeExtensionAbilityLocked_001, TestSize.Level1)
986 {
987 TAG_LOGI(AAFwkTag::TEST, "ResumeExtensionAbilityLocked start");
988 std::shared_ptr<AbilityConnectManager> connectManager = std::make_shared<AbilityConnectManager>(0);
989 EXPECT_NE(connectManager, nullptr);
990
991 auto result = connectManager->ConnectAbilityLocked(abilityRequest_, callbackA_, nullptr);
992 EXPECT_EQ(0, result);
993
994 result = connectManager->ResumeExtensionAbilityLocked(callbackA_);
995 EXPECT_EQ(result, OHOS::AAFwk::INVALID_CONNECTION_STATE);
996
997 auto list = connectManager->GetConnectRecordListByCallback(callbackA_);
998 EXPECT_EQ(static_cast<int>(list.size()), 1);
999
1000 for (auto& it : list) {
1001 it->SetConnectState(ConnectionState::CONNECTED);
1002 }
1003
1004 connectManager->SuspendExtensionAbilityLocked(callbackA_);
1005 result = connectManager->ResumeExtensionAbilityLocked(callbackA_);
1006 EXPECT_EQ(result, OHOS::ERR_OK);
1007 TAG_LOGI(AAFwkTag::TEST, "ResumeExtensionAbilityLocked end");
1008 }
1009
1010 /*
1011 * Feature: AbilityConnectManager
1012 * Function: SetServiceAfterNewCreate
1013 */
1014 HWTEST_F(AbilityConnectManagerTest, SetServiceAfterNewCreate_001, TestSize.Level1)
1015 {
1016 TAG_LOGI(AAFwkTag::TEST, "SetServiceAfterNewCreate_001 start");
1017 auto connectManager = std::make_shared<AbilityConnectManager>(0);
1018 std::string deviceName = "device";
1019 std::string abilityName = AbilityConfig::LAUNCHER_ABILITY_NAME;
1020 std::string appName = "hiservcie";
1021 std::string bundleName = "com.ix.hiservcie";
1022 std::string moduleName = "entry";
1023 auto abilityRequest = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName);
1024 auto targetService = AbilityRecord::CreateAbilityRecord(abilityRequest);
1025
1026 connectManager->SetServiceAfterNewCreate(abilityRequest, *targetService);
1027 EXPECT_TRUE(targetService->IsLauncherRoot());
1028 }
1029
1030 /*
1031 * Feature: AbilityConnectManager
1032 * Function: SetServiceAfterNewCreate
1033 */
1034 HWTEST_F(AbilityConnectManagerTest, SetServiceAfterNewCreate_002, TestSize.Level1)
1035 {
1036 TAG_LOGI(AAFwkTag::TEST, "SetServiceAfterNewCreate_002 start");
1037 auto connectManager = std::make_shared<AbilityConnectManager>(0);
1038 std::string deviceName = "device";
1039 std::string abilityName = "ServiceAbility";
1040 std::string appName = "hiservcie";
1041 std::string bundleName = "com.ix.hiservcie";
1042 std::string moduleName = "entry";
1043 auto abilityRequest = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName);
1044 auto targetService = AbilityRecord::CreateAbilityRecord(abilityRequest);
1045 targetService->SetKeepAliveBundle(true);
1046
1047 connectManager->SetServiceAfterNewCreate(abilityRequest, *targetService);
1048 EXPECT_FALSE(targetService->IsLauncherRoot());
1049 }
1050
1051 /*
1052 * Feature: AbilityConnectManager
1053 * Function: SetServiceAfterNewCreate
1054 */
1055 HWTEST_F(AbilityConnectManagerTest, SetServiceAfterNewCreate_003, TestSize.Level1)
1056 {
1057 TAG_LOGI(AAFwkTag::TEST, "SetServiceAfterNewCreate_003 start");
1058 auto connectManager = std::make_shared<AbilityConnectManager>(0);
1059 std::string deviceName = "device";
1060 std::string abilityName = AbilityConfig::SCENEBOARD_ABILITY_NAME;
1061 std::string appName = "hiservcie";
1062 std::string bundleName = AbilityConfig::SCENEBOARD_BUNDLE_NAME;
1063 std::string moduleName = "entry";
1064 auto abilityRequest = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, moduleName);
1065 auto targetService = AbilityRecord::CreateAbilityRecord(abilityRequest);
1066 abilityRequest.appInfo.accessTokenId = FAKE_TOKENID;
1067 connectManager->SetServiceAfterNewCreate(abilityRequest, *targetService);
1068 EXPECT_EQ(connectManager->sceneBoardTokenId_, FAKE_TOKENID);
1069 }
1070 } // namespace AAFwk
1071 } // namespace OHOS
1072