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 */ 15 16import configuration from '@system.configuration'; 17import prompt from '@system.prompt'; 18import router from '@system.router'; 19import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'; 20 21describe('basicabilityapi', function () { 22 let testResult; 23 let testResultFail; 24 let test; 25 beforeAll(function () { 26 testResult = true; 27 testResultFail = false; 28 test = "success" 29 }); 30 beforeEach(function () { 31 }); 32 afterEach(function () { 33 }); 34 afterAll(function () { 35 }); 36 37 async function backToIndex(){ 38 let backToIndexPromise = new Promise((resolve, reject) => { 39 setTimeout(() => { 40 router.back({ 41 uri: 'pages/index/index' 42 }); 43 resolve(); 44 }, 500); 45 }); 46 let clearPromise = new Promise((resolve, reject) => { 47 setTimeout(() => { 48 router.clear(); 49 resolve(); 50 }, 500); 51 }); 52 await backToIndexPromise.then(() => { 53 return clearPromise; 54 }); 55 } 56 57 /** 58 * @tc.number SUB_ACE_BASICABILITY_JS_API_0100 59 * @tc.name testClearInterval 60 * @tc.desc Cancel the repetitive timing tasks previously set by setInterval. 61 */ 62 it('testClearInterval', 0, async function(done) { 63 console.info('testClearInterval START'); 64 let res = 0; 65 let intervalID = -1; 66 let promise1 = new Promise((resolve, reject) => { 67 intervalID = setInterval(function () { 68 res++; 69 console.info('testClearInterval res = ' + res); 70 resolve(); 71 }, 100); 72 }); 73 let promise2 = new Promise((resolve, reject) => { 74 setTimeout(function () { 75 console.info('[clearInterval] start'); 76 clearInterval(intervalID); 77 console.info('[clearInterval] end'); 78 resolve(); 79 }, 600); 80 }); 81 Promise.all([promise1, promise2]).then(() => { 82 console.info('testClearInterval finally'); 83 expect(5).assertEqual(res); 84 console.info('testClearInterval END'); 85 done(); 86 }); 87 }); 88 89 /** 90 * @tc.number SUB_ACE_BASICABILITY_JS_API_0200 91 * @tc.name testConsole 92 * @tc.desc Print a text message. 93 */ 94 it('testConsole', 0, function () { 95 console.info('testConsole START'); 96 const versionCode = 1.1; 97 console.info('[console.info] versionCode: ' + versionCode); 98 console.debug('[console.debug] versionCode: ' + versionCode); 99 console.log('[console.log] versionCode: ' + versionCode); 100 console.warn('[console.warn] versionCode: ' + versionCode); 101 console.error('[console.error] versionCode: ' + versionCode); 102 expect(test).assertEqual('success'); 103 console.info('testConsole END'); 104 }); 105 106 /** 107 * @tc.number SUB_ACE_BASICABILITY_JS_API_0300 108 * @tc.name testRouterPush 109 * @tc.desc Go to the specified page of the application. 110 */ 111 it('testRouterPush', 0, async function (done) { 112 console.info('testRouterPush START'); 113 let options = { 114 uri : 'pages/routerPush/index' 115 } 116 let promise1 = new Promise((resolve, reject) => { 117 router.push(options); 118 resolve(); 119 }); 120 let promise2 = new Promise((resolve, reject) => { 121 setTimeout(() => { 122 let pages = router.getState(); 123 console.info("[router.push] getState" + JSON.stringify(pages)); 124 expect("pages/routerPush/").assertEqual(pages.path); 125 console.info("[router.push] getLength:" + router.getLength()); 126 expect("2").assertEqual(router.getLength()); 127 console.info('testRouterPush SUCCESS'); 128 resolve(); 129 }, 500); 130 }); 131 await promise1.then(() => { 132 return promise2; 133 }); 134 await backToIndex(); 135 console.info('testRouterPush END'); 136 done(); 137 }); 138 139 /** 140 * @tc.number SUB_ACE_BASICABILITY_JS_API_0310 141 * @tc.name testRouterPushNotExist 142 * @tc.desc Test push not exist page. 143 */ 144 it('testRouterPushNotExist', 0, async function (done) { 145 console.info('testRouterPushNotExist START'); 146 let promise1 = new Promise((resolve, reject) => { 147 router.push({ 148 uri: 'pages/routerNotExist/index' 149 }); 150 resolve(); 151 }); 152 let promise2 = new Promise((resolve, reject) => { 153 setTimeout(() => { 154 let pages = router.getState(); 155 console.info("testRouterPushNotExist getState" + JSON.stringify(pages)); 156 expect("pages/index/").assertEqual(pages.path); 157 console.info("testRouterPushNotExist getLength:" + router.getLength()); 158 expect("1").assertEqual(router.getLength()); 159 console.info('testRouterPushNotExist success'); 160 resolve(); 161 }, 500); 162 }); 163 await promise1.then(() => { 164 return promise2; 165 }); 166 console.info('testRouterPushNotExist END'); 167 done(); 168 }); 169 170 /** 171 * @tc.number SUB_ACE_BASICABILITY_JS_API_0400 172 * @tc.name testRouterReplace 173 * @tc.desc Replace the current page with a page in the application, and destroy the replaced page. 174 */ 175 it('testRouterReplace', 0, async function (done) { 176 //测试框架不允许index页面直接替换,需要先跳转到其他页面再替换 177 console.info('testRouterReplace START'); 178 let options = { 179 uri : 'pages/routerPush/index' 180 } 181 let promise1 = new Promise((resolve, reject) => { 182 router.push(options); 183 resolve(); 184 }); 185 let repleasePage = { 186 uri : 'pages/routerReplace/index' 187 } 188 let promise2 = new Promise((resolve, reject) => { 189 setTimeout(() => { 190 router.replace(repleasePage); 191 resolve(); 192 }, 500); 193 }); 194 //替换堆栈数量不会变 195 let promise3 = new Promise((resolve, reject) => { 196 setTimeout(() => { 197 let pages = router.getState(); 198 console.info("[router.replace] getState" + JSON.stringify(pages)); 199 expect("pages/routerReplace/").assertEqual(pages.path); 200 console.info("[router.replace] getLength:" + router.getLength()); 201 expect("2").assertEqual(router.getLength()); 202 console.info('testRouterReplace SUCCESS'); 203 resolve(); 204 }, 1000); 205 }); 206 await promise1.then(() => { 207 return promise2; 208 }).then(() => { 209 return promise3; 210 }); 211 await backToIndex(); 212 console.info('testRouterReplace END'); 213 done(); 214 }); 215 216 /** 217 * @tc.number SUB_ACE_BASICABILITY_JS_API_0410 218 * @tc.name testRouterReplaceNotExist 219 * @tc.desc Test replace not exist page. 220 */ 221 it('testRouterReplaceNotExist', 0, async function (done) { 222 console.info('testRouterReplaceNotExist START'); 223 await setTimeout(() => { 224 router.replace({ 225 uri: 'pages/routerNotExist/index' 226 }); 227 }, 500); 228 await setTimeout(() => { 229 let pages = router.getState(); 230 console.info("testRouterReplaceNotExist getState" + JSON.stringify(pages)); 231 expect("pages/index/").assertEqual(pages.path); 232 console.info("testRouterReplaceNotExist getLength:" + router.getLength()); 233 expect("1").assertEqual(router.getLength()); 234 console.info('testRouterReplaceNotExist END'); 235 done(); 236 }, 1000); 237 }); 238 239 /** 240 * @tc.number SUB_ACE_BASICABILITY_JS_API_0500 241 * @tc.name testRouterBack 242 * @tc.desc Return to the previous page or the specified page. 243 */ 244 it('testRouterBack', 0, async function (done) { 245 console.info('testRouterBack START'); 246 let promise1 = new Promise((resolve, reject) => { 247 router.push({ 248 uri: 'pages/routerPush/index' 249 }); 250 resolve(); 251 }); 252 let promise2 = new Promise((resolve, reject) => { 253 setTimeout(() => { 254 router.back({ 255 uri: 'pages/index/index' 256 }); 257 resolve(); 258 }, 500); 259 }); 260 let promise3 = new Promise((resolve, reject) => { 261 setTimeout(() => { 262 let pages = router.getState(); 263 console.info("[router.back] getState" + JSON.stringify(pages)); 264 expect("pages/index/").assertEqual(pages.path); 265 console.info("[router.back] getLength:" + router.getLength()); 266 expect("1").assertEqual(router.getLength()); 267 console.info('testRouterBack SUCCESS'); 268 resolve(); 269 }, 1000); 270 }); 271 await promise1.then(() => { 272 return promise2; 273 }).then(() => { 274 return promise3; 275 }); 276 console.info('testRouterBack END'); 277 done(); 278 }); 279 280 /** 281 * @tc.number SUB_ACE_BASICABILITY_JS_API_0600 282 * @tc.name testRouterClear 283 * @tc.desc Clear all historical pages in the page stack, and only keep the current page as the top page. 284 */ 285 it('testRouterClear', 0, function () { 286 console.info('testRouterClear START'); 287 router.clear(); 288 console.info("[router.clear] router.getLength:" + router.getLength()); 289 expect("1").assertEqual(router.getLength()); 290 console.info('testRouterClear END'); 291 }); 292 293 /** 294 * @tc.number SUB_ACE_BASICABILITY_JS_API_0700 295 * @tc.name testRouterLength 296 * @tc.desc Get the number of pages currently in the page stack. 297 */ 298 it('testRouterLength', 0, async function (done) { 299 console.info('testRouterLength START'); 300 let size = router.getLength(); 301 console.info('[router.getLength] pages stack size = ' + size); 302 expect(size).assertEqual('1'); 303 let options = { 304 uri : 'pages/routerPush/index' 305 } 306 let promise1 = new Promise((resolve, reject) => { 307 router.push(options); 308 resolve(); 309 }); 310 let promise2 = new Promise((resolve, reject) => { 311 setTimeout(() => { 312 console.info("testRouterLength getLength:" + router.getLength()); 313 expect("2").assertEqual(router.getLength()); 314 console.info('testRouterLength SUCCESS'); 315 resolve(); 316 }, 500); 317 }); 318 await promise1.then(() => { 319 return promise2; 320 }); 321 await backToIndex(); 322 console.info('testRouterLength END'); 323 done(); 324 }); 325 326 /** 327 * @tc.number SUB_ACE_BASICABILITY_JS_API_0800 328 * @tc.name testRouterGetState 329 * @tc.desc Get the status information of the current page. 330 */ 331 it('testRouterGetState', 0, async function (done) { 332 console.info('testRouterGetState START'); 333 await setTimeout(() => { 334 let page = router.getState(); 335 console.info('[router.getState] index: ' + page.index); 336 console.info('[router.getState] name: ' + page.name); 337 console.info('[router.getState] path: ' + page.path); 338 expect(page.index).assertEqual(1); 339 expect(page.name).assertEqual('index'); 340 expect(page.path).assertEqual('pages/index/'); 341 console.info('testRouterGetState END'); 342 done(); 343 }, 500); 344 }); 345 346 /** 347 * @tc.number SUB_ACE_BASICABILITY_JS_API_0900 348 * @tc.name testPromptShowToast 349 * @tc.desc Show text pop-up window. 350 */ 351 it('testPromptShowToast', 0, function () { 352 console.info('testPromptShowToast START'); 353 const delay = 5000; 354 prompt.showToast({ 355 message: 'message', 356 duration: delay, 357 }); 358 expect(test).assertEqual('success'); 359 console.info('[prompt.showToast] success'); 360 console.info('testPromptShowToast END'); 361 }); 362 363 /** 364 * @tc.number SUB_ACE_BASICABILITY_JS_API_1000 365 * @tc.name testPromptDialog 366 * @tc.desc Display the dialog box in the page. 367 */ 368 it('testPromptDialog', 0, function () { 369 console.info('testPromptDialog START') 370 prompt.showDialog({ 371 title: 'dialog showDialog test', 372 message: 'message of dialog', 373 buttons: [ 374 { 375 text: 'OK', 376 color: '#0000ff', 377 index: 0 378 } 379 ], 380 success: function (ret) { 381 console.info("[prompt.showDialog] ret.index " + ret.index); 382 expect(testResult).toBeTrue(); 383 }, 384 cancel: function () { 385 console.log('[prompt.showDialog] dialog cancel callback'); 386 expect(testResultFail).toBeTrue(); 387 }, 388 complete: function () { 389 console.log('[prompt.showDialog] complete'); 390 } 391 }); 392 console.info('testPromptDialog END'); 393 }); 394 395 /** 396 * @tc.number SUB_ACE_BASICABILITY_JS_API_1100 397 * @tc.name testConfigurationGetLocale 398 * @tc.desc Get the current language and region of the app. Synchronize with the language and region. 399 */ 400 it('testConfigurationGetLocale', 0, function () { 401 console.info('testConfigurationGetLocale START'); 402 const localeInfo = configuration.getLocale(); 403 console.info("[configuration.getLocale] localeInfo: " + JSON.stringify(localeInfo)); 404 console.info("[configuration.getLocale] language: " + localeInfo.language); 405 console.info("[configuration.getLocale] countryOrRegion: " + localeInfo.countryOrRegion); 406 console.info("[configuration.getLocale] dir: " + localeInfo.dir); 407 expect(localeInfo.language).assertEqual('zh'); 408 expect(localeInfo.dir).assertEqual('ltr'); 409 console.info('testConfigurationGetLocale END'); 410 }); 411 412 /** 413 * @tc.number SUB_ACE_BASICABILITY_JS_API_1200 414 * @tc.name testSetTimeout 415 * @tc.desc Set up a timer that executes a function or a specified piece of code after the timer expires. 416 */ 417 it('testSetTimeout', 0, async function (done) { 418 console.info('testSetTimeout START'); 419 let start_time = new Date().getTime(); 420 const delay = 200; 421 await setTimeout(function (v1, v2) { 422 let end_time = new Date().getTime(); 423 console.info("[settimeout] start_time: " + start_time); 424 console.info("[settimeout] end_time: " + end_time); 425 console.info('[settimeout] delay: ' + (end_time - start_time)); 426 console.info('[settimeout] v1: ' + v1); 427 console.info('[settimeout] v2: ' + v2); 428 expect('test').assertEqual(v1); 429 expect('message').assertEqual(v2); 430 expect(end_time - start_time >= delay).assertTrue(); 431 console.info('testSetTimeout END'); 432 done(); 433 }, delay, 'test', 'message'); 434 }); 435 436 /** 437 * @tc.number SUB_ACE_BASICABILITY_JS_API_1300 438 * @tc.name testClearTimeout 439 * @tc.desc The timer previously established by calling setTimeout() is cancelled. 440 */ 441 it('testClearTimeout', 0, async function (done) { 442 console.info('testClearTimeout START'); 443 let res = 0; 444 let timeoutID = setTimeout(function () { 445 res++; 446 }, 700); 447 await setTimeout(function () { 448 console.info('testClearTimeout delay 0.5s') 449 clearTimeout(timeoutID); 450 console.info("[clearTimeout] success"); 451 }, 500); 452 await setTimeout(function () { 453 expect(0).assertEqual(res); 454 console.info('testClearTimeout END'); 455 done(); 456 }, 1000); 457 }); 458 459 /** 460 * @tc.number SUB_ACE_BASICABILITY_JS_API_1400 461 * @tc.name testSetInterval 462 * @tc.desc Call a function or execute a code segment repeatedly, with a fixed time delay between each call. 463 */ 464 it('testSetInterval', 0, async function (done) { 465 console.info('testSetInterval START'); 466 let res = 0; 467 let intervalID = setInterval(function () { 468 res++; 469 }, 100); 470 await setTimeout(function () { 471 expect(9).assertEqual(res); 472 console.info('testSetInterval SUCCESS'); 473 clearInterval(intervalID); 474 console.info('testSetInterval END'); 475 done(); 476 }, 1000); 477 }); 478}); 479