1/* 2 * Copyright (c) 2022 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 */ 15import featureAbility from '@ohos.ability.featureAbility' 16import commonEvent from '@ohos.commonEvent' 17 18import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' 19 20describe('ActsStServiceAbilityTest', function () { 21 let bundleName = "com.amsst.stserviceabilityserver"; 22 let abilityName = "com.amsst.stserviceabilityserver.ServiceAbility"; 23 24 25 var subscriber0600; 26 var CommonEventSubscribeInfo0600 = { 27 events: ["ACTS_ASerivceAbilityServer_onConnect_PageConnectService_0600", 28 "ACTS_ASerivceAbilityServer_onDisConnect", 29 ], 30 }; 31 32 var subscriber1400; 33 var CommonEventSubscribeInfo1400 = { 34 events: ["ACTS_ASerivceAbilityServerSecond_onConnect_ServiceConnectService_1400", 35 "ACTS_ASerivceAbilityServerSecond_onDisConnect", 36 ], 37 }; 38 39 function unsubscribe(caller, subscriber) { 40 commonEvent.unsubscribe(subscriber, (err, data) => { 41 console.debug("=ACTS_Aunsubscribe (err,data)=======>" 42 + (caller) 43 + (" , json err【") + JSON.stringify(err) + (" 】") 44 + ("json data【") + JSON.stringify(data) + (" 】") 45 + " ,err=" + err + " ,data=" + data); 46 }); 47 } 48 let gSetTimeout = 1000 49 beforeAll(async (done) => { 50 console.debug('= ACTS_AbeforeAll 1541 ====<begin'); 51 console.debug('= ACTS_AbeforeAll ====<end'); 52 done(); 53 }) 54 beforeEach(async (done) => { 55 setTimeout(function () { 56 done(); 57 }, gSetTimeout); 58 }) 59 afterEach(async (done) => { 60 setTimeout(function () { 61 done(); 62 }, gSetTimeout); 63 }) 64 afterAll((done) => { 65 console.debug('= ACTS_AafterAll ====<begin'); 66 setTimeout(function () { 67 console.debug('= ACTS_AafterAll ====<end'); 68 featureAbility.terminateSelf(); 69 done(); 70 }, gSetTimeout); 71 }) 72 73 /* 74 * @tc.number ACTS_AAbilityStartSetting_0100 75 * @tc.name The configured URI is started and the page is not configured 76 * @tc.desc Function test 77 * @tc.level 0 78 */ 79 it("ACTS_AAbilityStartSetting_0100",0, async function(done){ 80 console.info("ACTS_AlogMessage 1 ACTS_AAbilityStartSetting_0100-------------------"); 81 try{ 82 let Want = { 83 bundleName: "com.example.abilityStartSettingApp", 84 abilityName: "com.example.abilityStartSettingApp.MainAbility", 85 } 86 87 let abilityStartSetting ={ 88 [featureAbility.AbilityStartSetting.BOUNDS_KEY] : [100,200,300,400], 89 [featureAbility.AbilityStartSetting.WINDOW_MODE_KEY] : 90 featureAbility.AbilityWindowConfiguration.WINDOW_MODE_FULLSCREEN, 91 [featureAbility.AbilityStartSetting.DISPLAY_ID_KEY] : 1, 92 } 93 94 var StartAbilityParameter = { 95 want:Want, 96 abilityStartSetting:abilityStartSetting 97 } 98 99 featureAbility.startAbility(StartAbilityParameter,(err,data)=>{ 100 console.log('ACTS_AAbilityStartSetting_0100 asyncCallback errCode : ' + JSON.stringify(err) 101 + " data: " + JSON.stringify(data)); 102 expect(1).assertEqual(err.code); 103 done(); 104 }); 105 }catch(error){ 106 console.log("ACTS_AAbilityStartSetting_0100 : error = " + error); 107 done(); 108 } 109 }) 110 111 /* 112 * @tc.number ACTS_AAbilityStartSettingNew_0200 113 * @tc.name The configured URI is started and the page is not configured 114 * @tc.desc Function test 115 * @tc.level 0 116 */ 117 it("ACTS_AAbilityStartSettingNew_0200",0, async function(done){ 118 console.info("ACTS_logMessage 1 ACTS_AAbilityStartSettingNew_0200-------------------"); 119 try{ 120 let Want = { 121 bundleName: "com.example.abilityStartSettingApp2", 122 abilityName: "com.example.abilityStartSettingApp.MainAbility", 123 } 124 125 let abilityStartSetting ={ 126 [featureAbility.AbilityStartSetting.BOUNDS_KEY] : [100,200,300,400], 127 [featureAbility.AbilityStartSetting.WINDOW_MODE_KEY] : 128 featureAbility.AbilityWindowConfiguration.WINDOW_MODE_UNDEFINED, 129 [featureAbility.AbilityStartSetting.DISPLAY_ID_KEY] : 1, 130 } 131 132 var StartAbilityParameter = { 133 want:Want, 134 abilityStartSetting:abilityStartSetting 135 } 136 137 featureAbility.startAbility(StartAbilityParameter,(err,data)=>{ 138 console.log('ACTS_AAbilityStartSettingNew_0200 asyncCallback errCode : ' + JSON.stringify(err) 139 + " data: " + JSON.stringify(data)); 140 expect(1).assertEqual(err.code); 141 done(); 142 }); 143 }catch(error){ 144 console.log("ACTS_AAbilityStartSettingNew_0200 : error = " + error); 145 done(); 146 } 147}) 148 149/* 150 * @tc.number ACTS_AAbilityStartSettingNew_0300 151 * @tc.name The configured URI is started and the page is not configured 152 * @tc.desc Function test 153 * @tc.level 0 154 */ 155it("ACTS_AAbilityStartSettingNew_0300",0, async function(done){ 156 console.info("ACTS_logMessage 1 ACTS_AAbilityStartSettingNew_0300-------------------"); 157 try{ 158 let Want = { 159 bundleName: "com.example.abilityStartSettingApp", 160 abilityName: "com.example.abilityStartSettingApp.MainAbility2", 161 } 162 163 let abilityStartSetting ={ 164 [featureAbility.AbilityStartSetting.BOUNDS_KEY] : [100,200,300,400], 165 [featureAbility.AbilityStartSetting.WINDOW_MODE_KEY] : 166 featureAbility.AbilityWindowConfiguration.WINDOW_MODE_SPLIT_PRIMARY, 167 [featureAbility.AbilityStartSetting.DISPLAY_ID_KEY] : 1, 168 } 169 170 var StartAbilityParameter = { 171 want:Want, 172 abilityStartSetting:abilityStartSetting 173 } 174 175 featureAbility.startAbility(StartAbilityParameter,(err,data)=>{ 176 console.log('ACTS_AAbilityStartSettingNew_0300 asyncCallback errCode : ' + JSON.stringify(err) 177 + " data: " + JSON.stringify(data)); 178 expect(1).assertEqual(err.code); 179 done(); 180 }); 181 }catch(error){ 182 console.log("ACTS_AAbilityStartSettingNew_0300 : error = " + error); 183 done(); 184 } 185}) 186 187/* 188 * @tc.number ACTS_AAbilityStartSettingNew_0400 189 * @tc.name The configured URI is started and the page is not configured 190 * @tc.desc Function test 191 * @tc.level 0 192 */ 193it("ACTS_AAbilityStartSettingNew_0400",0, async function(done){ 194 console.info("ACTS_logMessage 1 ACTS_AAbilityStartSettingNew_0400-------------------"); 195 try{ 196 let Want = { 197 bundlame: "com.example.abilityStartSettingApp", 198 abilityName: "com.example.abilityStartSettingApp.MainAbility", 199 } 200 201 let abilityStartSetting ={ 202 [featureAbility.AbilityStartSetting.BOUNDS_KEY] : [100,200,300,400], 203 [featureAbility.AbilityStartSetting.WINDOW_MODE_KEY] : 204 featureAbility.AbilityWindowConfiguration.WINDOW_MODE_SPLIT_SECONDARY, 205 [featureAbility.AbilityStartSetting.DISPLAY_ID_KEY] : 1, 206 } 207 208 var StartAbilityParameter = { 209 want:Want, 210 abilityStartSetting:abilityStartSetting 211 } 212 213 featureAbility.startAbility(StartAbilityParameter,(err,data)=>{ 214 console.log('ACTS_AAbilityStartSettingNew_0400 asyncCallback errCode : ' + JSON.stringify(err) 215 + " data: " + JSON.stringify(data)); 216 expect(1).assertEqual(err.code); 217 done(); 218 }); 219 }catch(error){ 220 console.log("ACTS_AAbilityStartSettingNew_0400 : error = " + error); 221 done(); 222 } 223}) 224 225/* 226 * @tc.number ACTS_AAbilityStartSettingNew_0500 227 * @tc.name The configured URI is started and the page is not configured 228 * @tc.desc Function test 229 * @tc.level 0 230 */ 231it("ACTS_AAbilityStartSettingNew_0500",0, async function(done){ 232 console.info("ACTS_--------ACTS_AAbilityStartSettingNew_0500------"); 233 try{ 234 let Want = { 235 bundleName: "com.example.abilityStartSettingApp", 236 abilityName: "com.example.abilityStartSettingApp.MainAbility", 237 } 238 239 let abilityStartSetting ={ 240 [featureAbility.AbilityStartSetting.BOUNDS_KEY] : [100,200,300,400], 241 [featureAbility.AbilityStartSetting.WINDOW_MODE_KEY] : 242 featureAbility.AbilityWindowConfiguration.WINDOW_MODE_FLOATING, 243 [featureAbility.AbilityStartSetting.DISPLAY_ID_KEY] : 1, 244 } 245 246 var StartAbilityParameter = { 247 want:Want, 248 abilityStartSetting:abilityStartSetting 249 } 250 251 featureAbility.startAbility(StartAbilityParameter,(err,data)=>{ 252 console.log('ACTS_AAbilityStartSettingNew_0500 asyncCallback errCode : ' + JSON.stringify(err) 253 + " data: " + JSON.stringify(data)); 254 expect(1).assertEqual(err.code); 255 done(); 256 }); 257 }catch(error){ 258 console.log("ACTS_AAbilityStartSettingNew_0500 : error = " + error); 259 done(); 260 } 261}) 262 263 /* 264 * @tc.number: ACTS_AJsServiceAbility_0600 265 * @tc.name: featureAbility.ConnectAbility : Connects an ability to a Service ability. 266 * @tc.desc: Check the return value of the interface (by AsyncCallback) 267 */ 268 it('ACTS_AJsServiceAbility_0600', 0, async function (done) { 269 console.log('ACTS_AJsServiceAbility_0600====<begin'); 270 try { 271 var mConnIdJsAsyncCallback; 272 commonEvent.createSubscriber(CommonEventSubscribeInfo0600).then(async (data) => { 273 console.debug("=ACTS_AJsServiceAbility_0600 createSubscriber .then(data)=======>" 274 + ("json data【") + JSON.stringify(data) + (" 】") 275 + " ,data=" + data); 276 subscriber0600 = data; 277 await commonEvent.subscribe(subscriber0600, async (err, data) => { 278 console.debug("=ACTS_AJsServiceAbility_0600 subscribe (err,data)=======>" 279 + ("json err【") + JSON.stringify(err) + (" 】") 280 + ("json data【") + JSON.stringify(data) + (" 】") 281 + " ,err=" + err + " ,data=" + data); 282 if (data.event != "ACTS_ASerivceAbilityServer_onDisConnect") { 283 expect("ACTS_ASerivceAbilityServer_onConnect_PageConnectService_0600").assertEqual( 284 data.event); 285 featureAbility.disconnectAbility(mConnIdJsAsyncCallback, (err) => { 286 console.debug("=ACTS_AJsServiceAbility_0600 disconnectAbility err====>" 287 + ("json err=") + JSON.stringify(err)); 288 }) 289 } else { 290 clearTimeout(currentAlertTimeout); 291 expect("ACTS_ASerivceAbilityServer_onDisConnect").assertEqual( 292 data.event); 293 unsubscribe("ACTS_AJsServiceAbility_0600_unsubscribe", subscriber0600); 294 console.log('ACTS_AJsServiceAbility_0600====<end') 295 done(); 296 } 297 }); 298 }) 299 function onConnectCallback(element, remote) { 300 console.debug('ACTS_AJsServiceAbility_0600_onConnectCallback ====> mConnIdJsAsyncCallback=' 301 + JSON.stringify(mConnIdJsAsyncCallback) + " , " + mConnIdJsAsyncCallback); 302 console.debug('ACTS_AJsServiceAbility_0600_onConnectCallback ====> element=' 303 + JSON.stringify(element) + " , " + element); 304 console.debug('ACTS_AJsServiceAbility_0600_onConnectCallback ====> remote=' 305 + JSON.stringify(remote) + " , " + remote); 306 console.debug('ACTS_AJsServiceAbility_0600_onConnectCallback ====> remote is proxy:' 307 + (remote instanceof rpc.RemoteProxy)); 308 } 309 310 function onDisconnectCallback(element) { 311 console.debug('ACTS_AJsServiceAbility_0600_onDisconnectCallback ====> element=' 312 + JSON.stringify(element) + " , " + element); 313 } 314 315 function onFailedCallback(code) { 316 console.debug('ACTS_AJsServiceAbility_0600_onFailedCallback ====> code=' 317 + JSON.stringify(code) + " , " + code) 318 expect(code==featureAbility.ErrorCode.ABILITY_NOT_FOUND 319 || (code!=featureAbility.ErrorCode.NO_ERROR 320 || code!=featureAbility.ErrorCode.INVALID_PARAMETER 321 || code!=featureAbility.ErrorCode.PERMISSION_DENY 322 )).assertTrue(); 323 } 324 325 mConnIdJsAsyncCallback = featureAbility.connectAbility( 326 { 327 bundleName: bundleName, 328 abilityName: abilityName, 329 action: "PageConnectService_0600", 330 }, 331 { 332 onConnect: onConnectCallback, 333 onDisconnect: onDisconnectCallback, 334 onFailed: onFailedCallback, 335 }, 336 ) 337 currentAlertTimeout = setTimeout(mySetTimeout, gSetTimeout); 338 function mySetTimeout() { 339 console.log('ACTS_AJsServiceAbility_0600====<end mySetTimeout') 340 done(); 341 } 342 } catch (err) { 343 clearTimeout(currentAlertTimeout); 344 console.log('ACTS_AJsServiceAbility_0600====<end err=' + err) 345 done(); 346 } 347 }) 348 349 /* 350 * @tc.number: ACTS_AJsServiceAbility_1400 351 * @tc.name: particleability.ConnectAbility : Connects an ability to a Service ability. 352 * @tc.desc: Check the return value of the interface (by AsyncCallback) 353 */ 354 it('ACTS_AJsServiceAbility_1400', 0, async function (done) { 355 console.log('ACTS_AJsServiceAbility_1400====<begin'); 356 try { 357 var mConnIdJsAsyncCallback; 358 var currentAlertTimeout; 359 commonEvent.createSubscriber(CommonEventSubscribeInfo1400).then(async (data) => { 360 console.debug("=ACTS_AJsServiceAbility_1400 createSubscriber .then(data)=======>" 361 + ("json data【") + JSON.stringify(data) + (" 】") 362 + " ,data=" + data); 363 subscriber1400 = data; 364 await commonEvent.subscribe(subscriber1400, async (err, data) => { 365 console.debug("=ACTS_AJsServiceAbility_1400 subscribe (err,data)=======>" 366 + ("json err【") + JSON.stringify(err) + (" 】") 367 + ("json data【") + JSON.stringify(data) + (" 】") 368 + " ,err=" + err + " ,data=" + data); 369 if (data.event != "ACTS_ASerivceAbilityServerSecond_onDisConnect") { 370 expect("ACTS_ASerivceAbilityServerSecond_onConnect_ServiceConnectService_1400" 371 ).assertEqual(data.event); 372 featureAbility.disconnectAbility(mConnIdJsAsyncCallback, (err) => { 373 console.debug("=ACTS_AJsServiceAbility_1400 disconnectAbility err====>" 374 + ("json err=") + JSON.stringify(err)); 375 }) 376 } else { 377 clearTimeout(currentAlertTimeout); 378 expect("ACTS_ASerivceAbilityServerSecond_onDisConnect").assertEqual( 379 data.event); 380 unsubscribe("ACTS_AJsServiceAbility_1400_unsubscribe", subscriber1400); 381 console.log('ACTS_AJsServiceAbility_1400====<end') 382 done(); 383 } 384 }); 385 }) 386 function onConnectCallback(element, remote) { 387 console.debug('ACTS_AJsServiceAbility_1400_onConnectCallback ====> mConnIdJsAsyncCallback=' 388 + JSON.stringify(mConnIdJsAsyncCallback) + " , " + mConnIdJsAsyncCallback); 389 console.debug('ACTS_AJsServiceAbility_1400_onConnectCallback ====> element=' 390 + JSON.stringify(element) + " , " + element); 391 console.debug('ACTS_AJsServiceAbility_1400_onConnectCallback ====> remote=' 392 + JSON.stringify(remote) + " , " + remote); 393 console.debug('ACTS_AJsServiceAbility_1400_onConnectCallback ====> remote is proxy:' 394 + (remote instanceof rpc.RemoteProxy)); 395 } 396 397 function onDisconnectCallback(element) { 398 console.debug('ACTS_AJsServiceAbility_1400_onDisconnectCallback ====> element=' 399 + JSON.stringify(element) + " , " + element); 400 } 401 402 function onFailedCallback(code) { 403 console.debug('ACTS_AJsServiceAbility_1400_onFailedCallback ====> code=' 404 + JSON.stringify(code) + " , " + code) 405 } 406 407 mConnIdJsAsyncCallback = featureAbility.connectAbility( 408 { 409 bundleName: bundleName, 410 abilityName: abilityName, 411 action: "ServiceConnectService_1400", 412 }, 413 { 414 onConnect: onConnectCallback, 415 onDisconnect: onDisconnectCallback, 416 onFailed: onFailedCallback, 417 }, 418 ) 419 420 currentAlertTimeout = setTimeout(mySetTimeout, gSetTimeout); 421 function mySetTimeout() { 422 console.log('ACTS_AJsServiceAbility_1400====<end mySetTimeout') 423 done(); 424 } 425 } catch (err) { 426 clearTimeout(currentAlertTimeout); 427 console.log('ACTS_AJsServiceAbility_1400====<end err' + err) 428 done(); 429 } 430 }) 431})