1 /*
2 * Copyright (c) 2023 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 "dms_continue_manager_test.h"
17
18 #include "dtbschedmgr_log.h"
19 #define private public
20 #include "mission/distributed_sched_continue_manager.h"
21 #undef private
22 #include "test_log.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace DistributedSchedule {
29 namespace {
30 const std::string TYPE = "type";
31 const std::string BUNDLENAME_01 = "bundleName01";
32 const std::string BUNDLENAME_02 = "bundleName02";
33 constexpr int32_t MISSIONID_01 = 1;
34 constexpr int32_t MISSIONID_02 = 2;
35 }
36
SetUpTestCase()37 void DMSContinueManagerTest::SetUpTestCase()
38 {
39 }
40
TearDownTestCase()41 void DMSContinueManagerTest::TearDownTestCase()
42 {
43 }
44
SetUp()45 void DMSContinueManagerTest::SetUp()
46 {
47 }
48
TearDown()49 void DMSContinueManagerTest::TearDown()
50 {
51 }
52
OnCallback(const uint32_t ContinueState,const std::string & srcDeviceId,const std::string & bundleName)53 void RemoteOnListenerStubTest::OnCallback(const uint32_t ContinueState, const std::string& srcDeviceId,
54 const std::string& bundleName)
55 {
56 }
57
58 /**
59 * @tc.name: testUnInit001
60 * @tc.desc: test UnInit
61 * @tc.type: FUNC
62 * @tc.require: I7F8KH
63 */
64 HWTEST_F(DMSContinueManagerTest, testUnInit001, TestSize.Level3)
65 {
66 DTEST_LOG << "DMSContinueManagerTest testUnInit001 begin" << std::endl;
67
68 DistributedSchedContinueManager::GetInstance().Init();
69 EXPECT_NE(DistributedSchedContinueManager::GetInstance().eventHandler_, nullptr);
70
71 /**
72 * @tc.steps: step1. test UnInit when eventHandler is not nullptr;
73 */
74 DistributedSchedContinueManager::GetInstance().UnInit();
75 EXPECT_EQ(DistributedSchedContinueManager::GetInstance().eventHandler_, nullptr);
76
77 /**
78 * @tc.steps: step2. test UnInit when eventHandler is nullptr;
79 */
80 DistributedSchedContinueManager::GetInstance().UnInit();
81 EXPECT_EQ(DistributedSchedContinueManager::GetInstance().eventHandler_, nullptr);
82
83 DTEST_LOG << "DMSContinueManagerTest testUnInit001 end" << std::endl;
84 }
85
86 /**
87 * @tc.name: testNotifyMissionFocused001
88 * @tc.desc: test NotifyMissionFocused
89 * @tc.type: FUNC
90 * @tc.require: I7F8KH
91 */
92 HWTEST_F(DMSContinueManagerTest, testNotifyMissionFocused001, TestSize.Level3)
93 {
94 DTEST_LOG << "DMSContinueManagerTest testNotifyMissionFocused001 begin" << std::endl;
95
96 DistributedSchedContinueManager::GetInstance().Init();
97
98 /**
99 * @tc.steps: step1. test NotifyMissionFocused when eventHandler is not nullptr;
100 */
101 DistributedSchedContinueManager::GetInstance().NotifyMissionFocused(0);
102 EXPECT_NE(DistributedSchedContinueManager::GetInstance().eventHandler_, nullptr);
103
104 /**
105 * @tc.steps: step2. test NotifyMissionFocused when eventHandler is nullptr;
106 */
107 DistributedSchedContinueManager::GetInstance().UnInit();
108 DistributedSchedContinueManager::GetInstance().NotifyMissionFocused(0);
109 EXPECT_EQ(DistributedSchedContinueManager::GetInstance().eventHandler_, nullptr);
110 DTEST_LOG << "DMSContinueManagerTest testNotifyMissionFocused001 end" << std::endl;
111 }
112
113 /**
114 * @tc.name: testNotifyMissionUnfocused001
115 * @tc.desc: test NotifyMissionUnfocused
116 * @tc.type: FUNC
117 * @tc.require: I7F8KH
118 */
119 HWTEST_F(DMSContinueManagerTest, testNotifyMissionUnfocused001, TestSize.Level3)
120 {
121 DTEST_LOG << "DMSContinueManagerTest testNotifyMissionUnfocused001 begin" << std::endl;
122
123 DistributedSchedContinueManager::GetInstance().Init();
124 /**
125 * @tc.steps: step1. test NotifyMissionUnfocused when eventHandler is not nullptr;
126 */
127 DistributedSchedContinueManager::GetInstance().NotifyMissionUnfocused(0);
128 EXPECT_NE(DistributedSchedContinueManager::GetInstance().eventHandler_, nullptr);
129
130 /**
131 * @tc.steps: step2. test NotifyMissionUnfocused when eventHandler is nullptr;
132 */
133 DistributedSchedContinueManager::GetInstance().UnInit();
134 DistributedSchedContinueManager::GetInstance().NotifyMissionUnfocused(0);
135 EXPECT_EQ(DistributedSchedContinueManager::GetInstance().eventHandler_, nullptr);
136 DTEST_LOG << "DMSContinueManagerTest testNotifyMissionUnfocused001 end" << std::endl;
137 }
138
139 /**
140 * @tc.name: testRegisterOnListener001
141 * @tc.desc: test RegisterOnListener
142 * @tc.type: FUNC
143 */
144 HWTEST_F(DMSContinueManagerTest, testRegisterOnListener001, TestSize.Level1)
145 {
146 DTEST_LOG << "DMSContinueManagerTest testRegisterOnListener001 start" << std::endl;
147 sptr<IRemoteObject> obj01 = new RemoteOnListenerStubTest();
148 int32_t ret = DistributedSchedContinueManager::GetInstance().RegisterOnListener(TYPE, obj01);
149 EXPECT_EQ(ret, ERR_OK);
150
151 ret = DistributedSchedContinueManager::GetInstance().RegisterOnListener(TYPE, obj01);
152 EXPECT_EQ(ret, NO_MISSION_INFO_FOR_MISSION_ID);
153
154 sptr<IRemoteObject> obj02 = new RemoteOnListenerStubTest();
155 ret = DistributedSchedContinueManager::GetInstance().RegisterOnListener(TYPE, obj02);
156 EXPECT_EQ(ret, ERR_OK);
157 DTEST_LOG << "DMSContinueManagerTest testRegisterOnListener001 end" << std::endl;
158 }
159
160 /**
161 * @tc.name: testRegisterOffListener001
162 * @tc.desc: test RegisterOffListener
163 * @tc.type: FUNC
164 */
165 HWTEST_F(DMSContinueManagerTest, testRegisterOffListener001, TestSize.Level1)
166 {
167 DTEST_LOG << "DMSContinueManagerTest testRegisterOffListener001 start" << std::endl;
168 sptr<IRemoteObject> obj01 = new RemoteOnListenerStubTest();
169 int32_t ret = DistributedSchedContinueManager::GetInstance().RegisterOnListener(TYPE, obj01);
170 ret = DistributedSchedContinueManager::GetInstance().RegisterOffListener(TYPE, obj01);
171 EXPECT_EQ(ret, ERR_OK);
172
173 ret = DistributedSchedContinueManager::GetInstance().RegisterOffListener(TYPE, nullptr);
174 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
175 DTEST_LOG << "DMSContinueManagerTest testRegisterOffListener001 end" << std::endl;
176 }
177
178 /**
179 * @tc.name: testRegisterOffListener002
180 * @tc.desc: test RegisterOffListener when iterItem->second is empty.
181 * @tc.type: FUNC
182 * @tc.require: I7F8KH
183 */
184 HWTEST_F(DMSContinueManagerTest, testRegisterOffListener002, TestSize.Level3)
185 {
186 DTEST_LOG << "DMSContinueManagerTest testRegisterOffListener002 start" << std::endl;
187 sptr<IRemoteObject> obj01 = new RemoteOnListenerStubTest();
188 int32_t ret = DistributedSchedContinueManager::GetInstance().RegisterOnListener(TYPE, obj01);
189 EXPECT_EQ(ret, ERR_OK);
190
191 {
192 std::lock_guard<std::mutex> registerOnListenerMapLock(
193 DistributedSchedContinueManager::GetInstance().eventMutex_);
194 DistributedSchedContinueManager::GetInstance().registerOnListener_[TYPE].clear();
195 }
196 ret = DistributedSchedContinueManager::GetInstance().RegisterOffListener(TYPE, obj01);
197 EXPECT_EQ(ret, ERR_OK);
198 DTEST_LOG << "DMSContinueManagerTest testRegisterOffListener002 end" << std::endl;
199 }
200
201 /**
202 * @tc.name: testGetMissionId001
203 * @tc.desc: test GetMissionId
204 * @tc.type: FUNC
205 */
206 HWTEST_F(DMSContinueManagerTest, testGetMissionId001, TestSize.Level1)
207 {
208 DTEST_LOG << "DMSContinueManagerTest testGetMissionId001 start" << std::endl;
209 DistributedSchedContinueManager::GetInstance().focusedMission_[BUNDLENAME_01] = MISSIONID_01;
210 int32_t missionId;
211 int32_t ret = DistributedSchedContinueManager::GetInstance().GetMissionId(BUNDLENAME_01, missionId);
212 EXPECT_EQ(missionId, MISSIONID_01);
213 EXPECT_EQ(ret, ERR_OK);
214
215 ret = DistributedSchedContinueManager::GetInstance().GetMissionId(BUNDLENAME_02, missionId);
216 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
217 DTEST_LOG << "DMSContinueManagerTest testGetMissionId001 end" << std::endl;
218 }
219
220 /**
221 * @tc.name: testDealFocusedBusiness001
222 * @tc.desc: test DealFocusedBusiness.
223 * @tc.type: FUNC
224 * @tc.require: I7F8KH
225 */
226 HWTEST_F(DMSContinueManagerTest, testDealFocusedBusiness001, TestSize.Level3)
227 {
228 DTEST_LOG << "DMSContinueManagerTest testDealFocusedBusiness001 start" << std::endl;
229
230 /**
231 * @tc.steps: step1. test DealFocusedBusiness when missionId is invalid;
232 */
233 int32_t ret = DistributedSchedContinueManager::GetInstance().DealFocusedBusiness(-1);
234 EXPECT_NE(ret, ERR_OK);
235
236 DTEST_LOG << "DMSContinueManagerTest testDealFocusedBusiness001 end" << std::endl;
237 }
238
239 /**
240 * @tc.name: testDealUnfocusedBusiness001
241 * @tc.desc: test DealUnfocusedBusiness.
242 * @tc.type: FUNC
243 * @tc.require: I7F8KH
244 */
245 HWTEST_F(DMSContinueManagerTest, testDealUnfocusedBusiness001, TestSize.Level3)
246 {
247 DTEST_LOG << "DMSContinueManagerTest testDealUnfocusedBusiness001 start" << std::endl;
248
249 /**
250 * @tc.steps: step1. test DealUnfocusedBusiness when missionId is invalid;
251 */
252 int32_t ret = DistributedSchedContinueManager::GetInstance().DealUnfocusedBusiness(-1);
253 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
254
255 /**
256 * @tc.steps: step2. test DealUnfocusedBusiness when missionId is not invalid;
257 */
258 DistributedSchedContinueManager::GetInstance().focusedMission_[BUNDLENAME_01] = MISSIONID_01;
259 ret = DistributedSchedContinueManager::GetInstance().DealUnfocusedBusiness(MISSIONID_01);
260 EXPECT_EQ(ret, CAN_NOT_FOUND_ABILITY_ERR);
261
262 /**
263 * @tc.steps: step3. test NotifyDeid when obj is nullptr;
264 */
265 sptr<IRemoteObject> obj01 = nullptr;
266 DistributedSchedContinueManager::GetInstance().NotifyDeid(obj01);
267
268 /**
269 * @tc.steps: step4. test NotifyDeid when iterItem->second is empty;
270 */
271 std::vector<sptr<IRemoteObject>> objs;
272 {
273 std::lock_guard<std::mutex> registerOnListenerMapLock(
274 DistributedSchedContinueManager::GetInstance().eventMutex_);
275 DistributedSchedContinueManager::GetInstance().registerOnListener_[TYPE] = objs;
276 }
277 obj01 = new RemoteOnListenerStubTest();
278 DistributedSchedContinueManager::GetInstance().NotifyDeid(obj01);
279
280 DTEST_LOG << "DMSContinueManagerTest testDealUnfocusedBusiness001 end" << std::endl;
281 }
282
283 /**
284 * @tc.name: testDealUnBroadcastdBusiness001
285 * @tc.desc: test DealUnBroadcastdBusiness.
286 * @tc.type: FUNC
287 * @tc.require: I7F8KH
288 */
289 HWTEST_F(DMSContinueManagerTest, testDealUnBroadcastdBusiness001, TestSize.Level3)
290 {
291 DTEST_LOG << "DMSContinueManagerTest testDealUnBroadcastdBusiness001 start" << std::endl;
292
293 /**
294 * @tc.steps: step1. test DealUnBroadcastdBusiness when senderNetworkId is invalid;
295 */
296 std::string senderNetworkId = "invalid senderNetworkId";
297 uint32_t accessTokenId = 0;
298 int32_t state = 0;
299 int32_t ret = DistributedSchedContinueManager::GetInstance().DealUnBroadcastdBusiness(
300 senderNetworkId, accessTokenId, state);
301 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
302
303 /**
304 * @tc.steps: step2. test NotifyRecvBroadcast when obj is nullptr;
305 */
306 sptr<IRemoteObject> obj = nullptr;
307 std::string networkId = "invalid networkId";
308 std::string bundleName = "invalid bundleName";
309 DistributedSchedContinueManager::GetInstance().NotifyRecvBroadcast(obj, networkId, bundleName, state);
310
311 /**
312 * @tc.steps: step3. test NotifyRecvBroadcast when missionId is invalid;
313 */
314 obj = new RemoteOnListenerStubTest();
315 DistributedSchedContinueManager::GetInstance().NotifyRecvBroadcast(obj, networkId, bundleName, state);
316
317 DTEST_LOG << "DMSContinueManagerTest testDealUnBroadcastdBusiness001 end" << std::endl;
318 }
319
320 /**
321 * @tc.name: testGetBundleName001
322 * @tc.desc: test GetBundleName
323 * @tc.type: FUNC
324 */
325 HWTEST_F(DMSContinueManagerTest, testGetBundleName001, TestSize.Level1)
326 {
327 DTEST_LOG << "DMSContinueManagerTest testGetBundleName001 start" << std::endl;
328 DistributedSchedContinueManager::GetInstance().focusedMission_[BUNDLENAME_01] = MISSIONID_01;
329 std::string bundleName;
330 int32_t ret = DistributedSchedContinueManager::GetInstance().GetBundleName(MISSIONID_01, bundleName);
331 EXPECT_EQ(bundleName, BUNDLENAME_01);
332 EXPECT_EQ(ret, ERR_OK);
333
334 ret = DistributedSchedContinueManager::GetInstance().GetBundleName(MISSIONID_02, bundleName);
335 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
336 DTEST_LOG << "DMSContinueManagerTest testGetBundleName001 end" << std::endl;
337 }
338
339 /**
340 * @tc.name: testIsContinue001
341 * @tc.desc: test IsContinue
342 * @tc.type: FUNC
343 */
344 HWTEST_F(DMSContinueManagerTest, testIsContinue001, TestSize.Level1)
345 {
346 DTEST_LOG << "DMSContinueManagerTest testIsContinue001 start" << std::endl;
347 DistributedSchedContinueManager::GetInstance().focusedMission_[BUNDLENAME_02] = MISSIONID_02;
348 DistributedSchedContinueManager::GetInstance().info_.currentMissionId = MISSIONID_01;
349 DistributedSchedContinueManager::GetInstance().info_.currentIsContibuable = true;
350 bool ret = DistributedSchedContinueManager::GetInstance().IsContinue(MISSIONID_02, BUNDLENAME_02);
351 EXPECT_EQ(ret, false);
352
353 ret = DistributedSchedContinueManager::GetInstance().IsContinue(MISSIONID_01, BUNDLENAME_01);
354 EXPECT_EQ(ret, true);
355
356 DistributedSchedContinueManager::GetInstance().info_.currentIsContibuable = false;
357 ret = DistributedSchedContinueManager::GetInstance().IsContinue(MISSIONID_01, BUNDLENAME_01);
358 EXPECT_EQ(ret, true);
359 DTEST_LOG << "DMSContinueManagerTest testIsContinue001 end" << std::endl;
360 }
361
362 /**
363 * @tc.name: testNotifyDeid001
364 * @tc.desc: test NotifyDeid
365 * @tc.type: FUNC
366 */
367 HWTEST_F(DMSContinueManagerTest, testNotifyDeid001, TestSize.Level1)
368 {
369 DTEST_LOG << "DMSContinueManagerTest testNotifyDeid001 start" << std::endl;
370 sptr<IRemoteObject> obj01 = new RemoteOnListenerStubTest();
371 int32_t ret = DistributedSchedContinueManager::GetInstance().RegisterOnListener(TYPE, obj01);
372 EXPECT_EQ(ret, DistributedSchedContinueManager::GetInstance().registerOnListener_.empty());
373 DistributedSchedContinueManager::GetInstance().NotifyDeid(obj01);
374 DTEST_LOG << "DMSContinueManagerTest testNotifyDeid001 end" << std::endl;
375 }
376
377 /**
378 * @tc.name: testSetMissionContinueState001
379 * @tc.desc: test SetMissionContinueState
380 * @tc.type: FUNC
381 */
382 HWTEST_F(DMSContinueManagerTest, testSetMissionContinueState001, TestSize.Level3)
383 {
384 DTEST_LOG << "DMSContinueManagerTest testSetMissionContinueState001 start" << std::endl;
385 DistributedSchedContinueManager::GetInstance().Init();
386 OHOS::AAFwk::ContinueState state = OHOS::AAFwk::ContinueState::CONTINUESTATE_ACTIVE;
387
388 /**
389 * @tc.steps: step1. test SetMissionContinueState when eventHandler is not nullptr;
390 */
391 DistributedSchedContinueManager::GetInstance().SetMissionContinueState(0, state);
392 EXPECT_NE(DistributedSchedContinueManager::GetInstance().eventHandler_, nullptr);
393
394 /**
395 * @tc.steps: step2. test SetMissionContinueState when eventHandler is nullptr;
396 */
397 DistributedSchedContinueManager::GetInstance().UnInit();
398 DistributedSchedContinueManager::GetInstance().SetMissionContinueState(0, state);
399 EXPECT_EQ(DistributedSchedContinueManager::GetInstance().eventHandler_, nullptr);
400 DTEST_LOG << "DMSContinueManagerTest testSetMissionContinueState001 end" << std::endl;
401 }
402
403 /**
404 * @tc.name: testDealSetMissionContinueStateBusiness001
405 * @tc.desc: test DealSetMissionContinueStateBusiness.
406 * @tc.type: FUNC
407 */
408 HWTEST_F(DMSContinueManagerTest, testDealSetMissionContinueStateBusiness001, TestSize.Level3)
409 {
410 DTEST_LOG << "DMSContinueManagerTest testDealSetMissionContinueStateBusiness001 start" << std::endl;
411 OHOS::AAFwk::ContinueState state = OHOS::AAFwk::ContinueState::CONTINUESTATE_ACTIVE;
412 DistributedSchedContinueManager::GetInstance().info_.currentMissionId = MISSIONID_01;
413
414 /**
415 * @tc.steps: step1. test DealSetMissionContinueStateBusiness when missionId is invalid;
416 */
417 int32_t ret = DistributedSchedContinueManager::GetInstance().DealSetMissionContinueStateBusiness(MISSIONID_02,
418 state);
419 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
420
421 /**
422 * @tc.steps: step2. test DealUnfocusedBusiness when mission is not continueable;
423 */
424 DistributedSchedContinueManager::GetInstance().info_.currentIsContibuable = false;
425 ret = DistributedSchedContinueManager::GetInstance().DealSetMissionContinueStateBusiness(MISSIONID_01, state);
426 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
427
428 DTEST_LOG << "DMSContinueManagerTest testDealSetMissionContinueStateBusiness001 end" << std::endl;
429 }
430 } // namespace DistributedSchedule
431 } // namespace OHOS