dialog-page.test.js 16.4 KB
Newer Older
1
jest.setTimeout(50000)
2 3

const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
4
const isWeb = platformInfo.startsWith('web')
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
5
const isAndroid = platformInfo.startsWith('android')
6
const isIos = platformInfo.startsWith('ios')
7
const isMP = platformInfo.startsWith('mp')
8 9 10 11
const FIRST_PAGE_PATH = '/pages/API/dialog-page/dialog-page'
const NEXT_PAGE_PATH = '/pages/API/dialog-page/next-page'

describe('dialog page', () => {
12
  if (process.env.UNI_AUTOMATOR_APP_WEBVIEW == 'true') {
13 14 15 16 17
    it('skip app-webview', () => {
      expect(1).toBe(1)
    })
    return
  }
18
  if (isMP) {
19 20 21 22
    it('skip mp', () => {
      expect(1).toBe(1)
    })
    return
23
  }
24

25 26 27 28 29 30 31 32 33 34
  let page;
  let initLifeCycleNum;
  let lifecycleNum;
  beforeAll(async () => {
    page = await program.reLaunch(FIRST_PAGE_PATH)
    await page.waitFor('view');
    initLifeCycleNum = await page.callMethod('getLifeCycleNum');
    await page.callMethod('setLifeCycleNum', 0)
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    expect(lifecycleNum).toBe(0)
35 36
  });

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  it('open dialog1', async () => {
    await page.callMethod('openDialog1');
    // 无法通过获取 dom 元素来判断是否打开了 dialogPage
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    // 不应触发父页面的生命周期,应该触发:
54
    // 1. openDialogPage success & complete callback
55
    // 2. dialog page 生命周期
56
    expect(lifecycleNum).toBe(7)
57
    await page.callMethod('setLifeCycleNum', 0)
58
  });
59 60 61
  it('check dialogPage methods', async () => {
    expect(await page.callMethod('dialogPageCheckGetDialogPages')).toBe(true)
    let dialogPageStyle = await page.callMethod('dialogPageGetPageStyle')
62
    expect(dialogPageStyle.backgroundColorContent).not.toBe('red')
63 64 65 66 67 68 69 70
    await page.callMethod('dialogPageSetPageStyle')
    dialogPageStyle = await page.callMethod('dialogPageGetPageStyle')
    expect(dialogPageStyle.backgroundColorContent).toBe('red')
    expect(await page.callMethod('dialogPageCheckGetElementById')).toBe(true)
    expect(await page.callMethod('dialogCheckGetAndroidView')).toBe(isAndroid)
    expect(await page.callMethod('dialogCheckGetIOSView')).toBe(false)
    expect(await page.callMethod('dialogCheckGetHTMLElement')).toBe(isWeb)
  })
71 72 73 74 75 76 77 78 79 80 81 82 83 84

  it('closeDialogPage', async () => {
    await page.callMethod('closeDialog');
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
    // closeDialogPage success & complete callback 应被触发
    // dialogPage onUnload 应被触发
    lifecycleNum = await page.callMethod('getLifeCycleNum')
85 86
    expect(lifecycleNum).toBe(-3)
    await page.callMethod('setLifeCycleNum', 0)
87 88 89 90 91
  })

  it('openDialog with wrong path', async () => {
    await page.callMethod('openDialog1WrongPath')
    lifecycleNum = await page.callMethod('getLifeCycleNum')
92 93
    expect(lifecycleNum).toBe(-3)
    await page.callMethod('setLifeCycleNum', 0)
94 95 96 97
  })

  it('navigateTo nextPage & open Dialog', async () => {
    await page.callMethod('goNextPageOpenDialog1')
98
    await page.waitFor(2500)
99
    if (isWeb) {
100
      await page.waitFor(3000)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    }
    page = await program.currentPage()
    expect(page.path).toBe(NEXT_PAGE_PATH.substring(1))
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
117 118
    expect(lifecycleNum).toBe(-4)
    await page.callMethod('setLifeCycleNum', 0)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  })

  it('dialog1 navigateBack', async () => {
    await program.navigateBack()
    page = await program.currentPage()
    // dialogPage onBackPress 返回 true, 应可以拦截 navigateBack
    expect(page.path).toBe(NEXT_PAGE_PATH.substring(1))
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    // onBackPress 生命周期应该被触发
136 137
    expect(lifecycleNum).toBe(1)
    await page.callMethod('setLifeCycleNum', 0)
138 139 140 141 142 143 144 145 146 147
  })

  it('open dialog2', async () => {
    await page.callMethod('openDialog2')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    // 应触发前一个 dialogPage 的 onHide
148 149
    expect(lifecycleNum).toBe(4)
    await page.callMethod('setLifeCycleNum', 0)
150 151 152 153
  })

  it('closeDialogPage', async () => {
    await page.callMethod('closeDialog')
154
    await page.waitFor(1000)
155 156 157
    lifecycleNum = await page.callMethod('getLifeCycleNum')
    // 应触发 success & complete 回调
    // 应触发 dialogPage 的 unload,下层的 dialogPage 会先 show 再 unload
158
    expect(lifecycleNum).toBe(-7)
159 160 161 162 163 164 165 166 167

    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
168
    await page.callMethod('setLifeCycleNum', 0)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  })

  it('open multiple dialog page', async () => {
    await page.callMethod('openDialog1')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image1 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image1).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
186
    expect(lifecycleNum).toBe(4)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

    await page.callMethod('openDialog2')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image2 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image2).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
202 203
    expect(lifecycleNum).toBe(8)
    await page.callMethod('setLifeCycleNum', 0)
204 205
  })

206
  it('openDialogPage to home page', async () => {
207
    // 本测试例中是在 FIRST_PAGE_PATH 中打开
208 209 210 211 212 213
    await page.callMethod('openDialogPage1ToHomePage')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    lifecycleNum = await page.callMethod('getLifeCycleNum')
214 215
    expect(lifecycleNum).toBe(4)
    await page.callMethod('setLifeCycleNum', 0)
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  })

  it('dialog2 navigateBack', async () => {
    await program.navigateBack()
    page = await program.currentPage()
    expect(page.path).toBe(FIRST_PAGE_PATH.substring(1))
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
231
    // dialogPage2 onBackPress +1 dialogPage1 show +1 dialogPage unload -5*2 firstPage show +10
232
    expect(lifecycleNum).toBe(2)
233
    await page.callMethod('setLifeCycleNum', 0)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  })

  it('close specified dialogPage', async () => {
    await page.callMethod('openDialog2')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image1 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image1).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
251
    expect(lifecycleNum).toBe(4)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

    await page.callMethod('openDialog1')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    const image2 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image2).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
267
    expect(lifecycleNum).toBe(10)
268 269 270 271 272 273 274 275 276 277 278

    await page.callMethod('closeSpecifiedDialog', 0)
    const image3 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image3).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
279
    expect(lifecycleNum).toBe(7)
280 281 282 283 284 285 286 287 288 289 290

    await page.callMethod('closeSpecifiedDialog', 1)
    const image4 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image4).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
291
    expect(lifecycleNum).toBe(5)
292 293 294 295 296 297 298 299 300 301 302

    await page.callMethod('closeSpecifiedDialog', 0)
    const image5 = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    });
    expect(image5).toSaveImageSnapshot();
    lifecycleNum = await page.callMethod('getLifeCycleNum')
303
    expect(lifecycleNum).toBe(2)
304
    await page.callMethod('setLifeCycleNum', 0)
305
  })
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  it('check triggerParentHide', async () => {
    // no triggerParentHide should not trigger parent hide
    await page.callMethod('openDialog4')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    // dialog4 show +1
    expect(await page.callMethod('getLifeCycleNum')).toBe(1)
    await page.callMethod('closeDialog')
    await page.waitFor(200)
    // dialog4 unload -5 closeDialog callback +2
    expect(await page.callMethod('getLifeCycleNum')).toBe(-2)


    // triggerParentHide should trigger parent hide
    await page.callMethod('openDialogWithTriggerParentHide')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    // openDialog callback +2 dialog4 show +1 parent hide -10
    expect(await page.callMethod('getLifeCycleNum')).toBe(-9)
    await page.callMethod('closeDialog')
    await page.waitFor(200)
    // dialog4 unload -5 parent show +10 closeDialog callback +2
    expect(await page.callMethod('getLifeCycleNum')).toBe(-2)


    // triggerParentHide should trigger parent hide
    await page.callMethod('openDialogWithTriggerParentHide')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    // openDialog callback +2 dialog4 show +1 parent hide -10
    expect(await page.callMethod('getLifeCycleNum')).toBe(-9)

    // second triggerParentHide should not trigger parent hide
    await page.callMethod('openDialogWithTriggerParentHide')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    // openDialog callback +2 dialog4 show +1
    expect(await page.callMethod('getLifeCycleNum')).toBe(-6)

    await page.callMethod('closeSpecifiedDialog', 1)
    await page.waitFor(200)
    // close not last triggerParentHide should not trigger parent show
    // close callback +2 dialog4 unload -5 dialog4 show +1
    expect(await page.callMethod('getLifeCycleNum')).toBe(-8)

    await page.callMethod('closeSpecifiedDialog', 0)
    await page.waitFor(200)
    // close last triggerParentHide should trigger parent show
    // close callback +2 dialog4 unload -5 parent show +10
    expect(await page.callMethod('getLifeCycleNum')).toBe(-1)


    // no triggerParentHide should not trigger parent hide
    await page.callMethod('openDialog4')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    // dialog4 show +1
    expect(await page.callMethod('getLifeCycleNum')).toBe(0)
    // triggerParentHide should trigger parent hide
    await page.callMethod('openDialogWithTriggerParentHide')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    // openDialog callback +2 dialog4 show +1 parent hide -10
    expect(await page.callMethod('getLifeCycleNum')).toBe(-7)

    // second triggerParentHide should not trigger parent hide
    await page.callMethod('openDialogWithTriggerParentHide')
    await page.waitFor(1000)
    if (isWeb) {
      await page.waitFor(2000)
    }
    // openDialog callback +2 dialog4 show +1
    expect(await page.callMethod('getLifeCycleNum')).toBe(-4)
    // close middle triggerParentHide dialogPage
    await page.callMethod('closeSpecifiedDialog', 1)
    await page.waitFor(200)
    // close callback +2 dialog4 unload -5
    expect(await page.callMethod('getLifeCycleNum')).toBe(-7)
    // close last triggerParentHide dialogPage shoud trigger parent show
    await page.callMethod('closeSpecifiedDialog', 1)
    await page.waitFor(200)
    // close callback +2 dialog4 unload -5 dialog4 show +1 parent show +10
    expect(await page.callMethod('getLifeCycleNum')).toBe(1)
    await page.callMethod('closeDialog')
    await page.waitFor(200)
    // close callback +2 dialog4 unload -5
    expect(await page.callMethod('getLifeCycleNum')).toBe(-2)
  })


  if (isAndroid || isIos) {
    it('after closeDialogPage reset statusBar color', async () => {
      const adbScreenShotArea = {
        x: 900,
        y: 50,
        width: 100,
        height: 70
      };

      if (process.env.uniTestPlatformInfo.startsWith('android 6')) {
        adbScreenShotArea.x = 535
        adbScreenShotArea.width = 90
        adbScreenShotArea.height = 50
      } else if (process.env.uniTestPlatformInfo.startsWith('android 12')) {
        adbScreenShotArea.x = 1160
        adbScreenShotArea.width = 70
        adbScreenShotArea.height = 80
      }

428 429 430 431 432 433
      const imageForParentInit = await program.screenshot({
        deviceShot: true,
        area: adbScreenShotArea,
      });
      expect(imageForParentInit).toSaveImageSnapshot();

434 435
      await page.callMethod('openDialog4')
      await page.waitFor(1000)
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
      const imageForDialog4_1 = await program.screenshot({
        deviceShot: true,
        area: adbScreenShotArea,
      });
      expect(imageForDialog4_1).toSaveImageSnapshot();

      await page.callMethod('openDialog3')
      await page.waitFor(1000)
      const imageForDialog3 = await program.screenshot({
        deviceShot: true,
        area: adbScreenShotArea,
      });
      expect(imageForDialog3).toSaveImageSnapshot();

      await page.callMethod('closeSpecifiedDialog', 1)
      await page.waitFor(1000)

      const imageForDialog4_2 = await program.screenshot({
454 455 456
        deviceShot: true,
        area: adbScreenShotArea,
      });
457
      expect(imageForDialog4_2).toSaveImageSnapshot();
458

459
      await page.callMethod('closeSpecifiedDialog', 0)
460 461
      await page.waitFor(1000)

462
      const imageForParentEnd = await program.screenshot({
463 464 465
        deviceShot: true,
        area: adbScreenShotArea,
      });
466
      expect(imageForParentEnd).toSaveImageSnapshot();
467 468
    })
  }
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
  it('input-hold-keyboard in dialog', async () => {
    await page.callMethod('jest_OpenDialog1')
    await page.waitFor(2000);
    await page.callMethod('jest_getTapPoint')
    const point_x = await page.data('jest_click_x');
    const point_y = await page.data('jest_click_y');
    if (isAndroid) {
      await program.adbCommand("input tap" + " " + point_x + " " + point_y)
      console.log("input tap" + " " + point_x + " " + point_y);
    } else {
      await program.tap({
        x: point_x,
        y: point_y
      })
    }

    await page.waitFor(1000);
    const image = await program.screenshot({
      deviceShot: true,
      area: {
        x: 0,
        y: 200,
      }
    })
    expect(image).toSaveImageSnapshot()
    await page.waitFor(2000);
    await page.callMethod('jest_CloseDialog1')
  })

  it('dialogPage hideStatusBar hideBottomNavigationIndicator', async () => {
    if (isAndroid) {
      await page.callMethod('openDialog2ForTest');
      await page.waitFor(1000);
      await page.callMethod('setPageStyleForTest', {
        hideStatusBar: true,
        hideBottomNavigationIndicator: true
      });
      await page.waitFor(2000);
      const image = await program.screenshot({
        deviceShot: true
      });
      expect(image).toSaveImageSnapshot();
      await page.waitFor(2000);
      await page.callMethod('closeDialog2ForTest');
    }
  });
515 516 517 518 519

  afterAll(async () => {
    await page.callMethod('setLifeCycleNum', initLifeCycleNum)
  });
});