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 "dsched_continue_sup_test.h"
17
18 #include "distributed_sched_service.h"
19 #include "dtbschedmgr_log.h"
20 #include "softbus_error_code.h"
21 #include "test_log.h"
22 #include "mission/distributed_bm_storage.h"
23 #include "continue_scene_session_handler.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace DistributedSchedule {
30
31 namespace {
32 const std::string BASEDIR = "/data/service/el1/public/database/DistributedSchedule";
33 const std::string BUNDLEMAME_1 = "bundleName";
34 const std::string CONTINUE_TYPE1 = "continueType1";
35 const std::string CONTINUE_TYPE2 = "continueType2";
36 const std::string CONTINUE_TYPE3 = "continueType3";
37 const std::string CONTINUE_TYPE1_QUICK = "continueType1_ContinueQuickStart";
38 const std::string MODULE_NAME1 = "moduleName1";
39 const std::string MODULE_NAME2 = "moduleName2";
40 const std::string MODULE_NAME3 = "moduleName3";
41 const std::string ABILITY_NAME_SAME_AS_CONTINUE_TYPE = CONTINUE_TYPE1;
42 const std::string ABILITY_NAME_DIFF_AS_CONTINUE_TYPE = "ability";
43 const int32_t WAITTIME = 2000;
44 const uint32_t DSCHED_BUFFER_SIZE = 1024;
45 }
SetUpTestCase()46 void DSchedContinueSupTest::SetUpTestCase()
47 {
48 mkdir(BASEDIR.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
49 DTEST_LOG << "DSchedContinueSupTest::SetUpTestCase" << std::endl;
50 DistributedSchedService::GetInstance().Init();
51
52 std::string deviceId = "123";
53 std::string bundleName = "test";
54 int32_t subType = CONTINUE_PULL;
55 int32_t direction = CONTINUE_SINK;
56 sptr<IRemoteObject> callback = nullptr;
57 OHOS::AAFwk::WantParams wantParams;
58 auto info = DSchedContinueInfo(deviceId, bundleName, deviceId, bundleName, "");
59 conti_ = std::make_shared<DSchedContinue>(subType, direction, callback, info);
60 }
61
TearDownTestCase()62 void DSchedContinueSupTest::TearDownTestCase()
63 {
64 (void)remove(BASEDIR.c_str());
65 conti_ = nullptr;
66 DTEST_LOG << "DSchedContinueSupTest::TearDownTestCase" << std::endl;
67 }
68
TearDown()69 void DSchedContinueSupTest::TearDown()
70 {
71 usleep(WAITTIME);
72 DTEST_LOG << "DSchedContinueSupTest::TearDown" << std::endl;
73 }
74
SetUp()75 void DSchedContinueSupTest::SetUp()
76 {
77 usleep(WAITTIME);
78 DTEST_LOG << "DSchedContinueSupTest::SetUp" << std::endl;
79 }
80
81 /**
82 * @tc.name: DSchedContinueSupTest_001_1
83 * @tc.desc: OnContinueMission and PostStartTask
84 * @tc.type: FUNC
85 */
86 HWTEST_F(DSchedContinueSupTest, DSchedContinueSupTest_001_1, TestSize.Level1)
87 {
88 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_001_1 begin" << std::endl;
89 ASSERT_NE(conti_, nullptr);
90 ASSERT_EQ(conti_->eventHandler_, nullptr);
91 OHOS::AAFwk::WantParams wantParams;
92 int32_t ret = conti_->OnContinueMission(wantParams);
93 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
94
95 ret = conti_->PostStartTask(wantParams);
96 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
97 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_001_1 end ret:" << ret << std::endl;
98 }
99
100 /**
101 * @tc.name: DSchedContinueSupTest_001_2
102 * @tc.desc: DSchedContinue Constructor
103 * @tc.type: FUNC
104 */
105 HWTEST_F(DSchedContinueSupTest, DSchedContinueSupTest_001_2, TestSize.Level1)
106 {
107 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_001_2 begin" << std::endl;
108 std::shared_ptr<DSchedContinueStartCmd> startCmd = nullptr;
109 int32_t sessionId = 1;
110 int32_t accountId = 1;
111
112 auto testInfo = std::make_shared<DSchedContinue>(startCmd, sessionId, accountId);
113 EXPECT_TRUE(testInfo->continueInfo_.sourceBundleName_.empty());
114
115 startCmd = std::make_shared<DSchedContinueStartCmd>();
116 auto testInfo2 = std::make_shared<DSchedContinue>(startCmd, sessionId, accountId);
117 EXPECT_EQ(testInfo2->accountId_, accountId);
118
119 startCmd->sourceMissionId_ = 1;
120 auto testInfo3 = std::make_shared<DSchedContinue>(startCmd, sessionId, accountId);
121 EXPECT_EQ(testInfo3->continueInfo_.missionId_, startCmd->sourceMissionId_);
122
123 startCmd->dstBundleName_ = "sinkBundleName";
124 auto testInfo4 = std::make_shared<DSchedContinue>(startCmd, sessionId, accountId);
125 EXPECT_EQ(testInfo4->continueInfo_.sinkBundleName_, startCmd->dstBundleName_);
126
127 startCmd->srcBundleName_ = "srcBundleName";
128 auto testInfo5 = std::make_shared<DSchedContinue>(startCmd, sessionId, accountId);
129 EXPECT_EQ(testInfo5->continueInfo_.sourceBundleName_, startCmd->srcBundleName_);
130 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_001_2 end" << std::endl;
131 }
132
133 /**
134 * @tc.name: DSchedContinueSupTest_002_1
135 * @tc.desc: OnStartCmd and PostCotinueAbilityTask
136 * @tc.type: FUNC
137 */
138 HWTEST_F(DSchedContinueSupTest, DSchedContinueSupTest_002_1, TestSize.Level1)
139 {
140 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_002_1 begin" << std::endl;
141 ASSERT_NE(conti_, nullptr);
142 ASSERT_EQ(conti_->eventHandler_, nullptr);
143 int32_t appVersion = 0;
144
145 // eventHandler_ is null
146 int32_t ret = conti_->OnStartCmd(appVersion);
147 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
148
149 ret = conti_->PostCotinueAbilityTask(appVersion);
150 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
151 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_002_1 end ret:" << ret << std::endl;
152 }
153
154 /**
155 * @tc.name: DSchedContinueSupTest_003
156 * @tc.desc: OnReplyCmd and PostReplyTask
157 * @tc.type: FUNC
158 */
159 HWTEST_F(DSchedContinueSupTest, DSchedContinueSupTest_003_1, TestSize.Level1)
160 {
161 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_003_1 begin" << std::endl;
162 ASSERT_NE(conti_, nullptr);
163 ASSERT_EQ(conti_->eventHandler_, nullptr);
164 auto cmd = std::make_shared<DSchedContinueReplyCmd>();
165 // eventHandler_ is null
166 cmd->replyCmd_ = DSCHED_CONTINUE_CMD_START;
167 int32_t ret = conti_->OnReplyCmd(cmd);
168 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
169
170 ret = conti_->PostReplyTask(cmd);
171 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
172 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_003_1 end ret:" << ret << std::endl;
173 }
174
175 /**
176 * @tc.name: DSchedContinueSupTest_004_1
177 * @tc.desc: OnStartContinuation and PostContinueSendTask
178 * @tc.type: FUNC
179 */
180 HWTEST_F(DSchedContinueSupTest, DSchedContinueSupTest_004_1, TestSize.Level1)
181 {
182 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_004_1 begin" << std::endl;
183 ASSERT_NE(conti_, nullptr);
184 ASSERT_EQ(conti_->eventHandler_, nullptr);
185 OHOS::AAFwk::Want want;
186 int32_t callerUid = 0;
187 int32_t status = ERR_OK;
188 uint32_t accessToken = 0;
189
190 // eventHandler_ is null
191 int32_t ret = conti_->OnStartContinuation(want, callerUid, status, accessToken);
192 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
193
194 ret = conti_->PostContinueSendTask(want, callerUid, status, accessToken);
195 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
196 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_004_1 end ret:" << ret << std::endl;
197 }
198
199 /**
200 * @tc.name: DSchedContinueSupTest_005
201 * @tc.desc: OnContinueDataCmd and PostContinueDataTask
202 * @tc.type: FUNC
203 */
204 HWTEST_F(DSchedContinueSupTest, DSchedContinueSupTest_005_1, TestSize.Level1)
205 {
206 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_005_1 begin" << std::endl;
207 ASSERT_NE(conti_, nullptr);
208 ASSERT_EQ(conti_->eventHandler_, nullptr);
209
210 // eventHandler_ is null
211 auto cmd = std::make_shared<DSchedContinueDataCmd>();
212 int32_t ret = conti_->OnContinueDataCmd(cmd);
213 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
214
215 ret = conti_->PostContinueDataTask(cmd);
216 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
217 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_005_1 end ret:" << ret << std::endl;
218 }
219
220 /**
221 * @tc.name: DSchedContinueSupTest_006_1
222 * @tc.desc: OnNotifyComplete, OnContinueEndCmd and PostNotifyCompleteTask
223 * @tc.type: FUNC
224 */
225 HWTEST_F(DSchedContinueSupTest, DSchedContinueSupTest_006_1, TestSize.Level1)
226 {
227 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_006_1 begin" << std::endl;
228 ASSERT_NE(conti_, nullptr);
229 ASSERT_EQ(conti_->eventHandler_, nullptr);
230
231 int32_t missionId = 1;
232 bool isSuccess = true;
233
234 // OnNotifyComplete
235 int32_t ret = conti_->OnNotifyComplete(missionId, isSuccess);
236 EXPECT_EQ(ret, ERR_OK);
237
238 missionId = 0;
239 ret = conti_->OnNotifyComplete(missionId, isSuccess);
240 EXPECT_EQ(ret, ERR_OK);
241
242 isSuccess = false;
243 ret = conti_->OnNotifyComplete(missionId, isSuccess);
244 EXPECT_EQ(ret, ERR_OK);
245
246 // eventHandler_ is null
247 auto cmd = std::make_shared<DSchedContinueEndCmd>();
248 ret = conti_->OnContinueEndCmd(cmd);
249 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
250
251 ret = conti_->PostNotifyCompleteTask(ERR_OK);
252 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
253 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_006_1 end ret:" << ret << std::endl;
254 }
255
256 /**
257 * @tc.name: DSchedContinueSupTest_007_1
258 * @tc.desc: OnContinueEnd and PostContinueEndTask
259 * @tc.type: FUNC
260 */
261 HWTEST_F(DSchedContinueSupTest, DSchedContinueSupTest_007_1, TestSize.Level1)
262 {
263 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_007_1 begin" << std::endl;
264 ASSERT_NE(conti_, nullptr);
265 ASSERT_EQ(conti_->eventHandler_, nullptr);
266 int32_t result = ERR_OK;
267
268 // eventHandler_ is null
269 int32_t ret = conti_->OnContinueEnd(result);
270 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
271
272 ret = conti_->PostContinueEndTask(result);
273 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
274 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_007_1 end ret:" << ret << std::endl;
275 }
276
277 /**
278 * @tc.name: DSchedContinueSupTest_0011_1
279 * @tc.desc: GetMissionIdByBundleName
280 * @tc.type: FUNC
281 */
282 HWTEST_F(DSchedContinueSupTest, DSchedContinueSupTest_0011_1, TestSize.Level1)
283 {
284 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_0011_1 begin" << std::endl;
285 std::string deviceId = "123";
286 int32_t subType = CONTINUE_PULL;
287 int32_t direction = CONTINUE_SINK;
288 int32_t missionId = 1;
289 sptr<IRemoteObject> callback = nullptr;
290 auto info = DSchedContinueInfo(deviceId, deviceId, missionId);
291 auto conti = std::make_shared<DSchedContinue>(subType, direction, callback, info);
292 conti->Init();
293 usleep(WAITTIME);
294
295 int32_t ret = conti->GetMissionIdByBundleName();
296 EXPECT_EQ(ret, ERR_OK);
297 DTEST_LOG << "DSchedContinueSupTest DSchedContinueSupTest_0011_1 end ret:" << ret << std::endl;
298 usleep(WAITTIME);
299 }
300
301 /**
302 * @tc.name: ExecuteQuickStartFailed_037
303 * @tc.desc: ExecuteQuickStartFailed
304 * @tc.type: FUNC
305 */
306 HWTEST_F(DSchedContinueSupTest, OnRemoteDied_038, TestSize.Level1)
307 {
308 DTEST_LOG << "DSchedContinueSupTest OnRemoteDied_038 begin" << std::endl;
309 sptr<StateCallbackIpcDiedListener> diedListener = new StateCallbackIpcDiedListener();
310 EXPECT_NO_FATAL_FAILURE(diedListener->OnRemoteDied(nullptr));
311 DTEST_LOG << "DSchedContinueSupTest OnRemoteDied_038 end ret:" << std::endl;
312 usleep(WAITTIME);
313 }
314 }
315 }
316