global-events.test.js 16.8 KB
Newer Older
1
const PAGE_PATH = '/pages/component/global-events/global-events'
DCloud-WZF's avatar
DCloud-WZF 已提交
2

3
describe('event trigger', () => {
4 5 6 7 8 9
  if (process.env.UNI_TEST_DEVICES_DIRECTION == 'landscape') {
    it('跳过横屏模式', () => {
      expect(1).toBe(1)
    })
    return
  }
10
  const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
11 12
  const isAndroid = platformInfo.startsWith('android')
  const isIos = platformInfo.startsWith('ios')
DCloud-WZF's avatar
DCloud-WZF 已提交
13 14 15 16 17 18 19
  let page
  beforeAll(async () => {
    page = await program.navigateTo(PAGE_PATH)
    await page.waitFor('view')
  })

  it('touch', async () => {
20 21
    if (!process.env.UNI_AUTOMATOR_APP_WEBVIEW) {
      const el = await page.$('#touch-target')
DCloud-WZF's avatar
DCloud-WZF 已提交
22 23 24
      await el.touchstart({
        touches: [{
          identifier: 1,
25 26 27 28 29 30
          pageX: 101,
          pageY: 101,
          clientX: 101,
          clientY: 101,
          screenX: 101,
          screenY: 101
DCloud-WZF's avatar
DCloud-WZF 已提交
31 32 33
        }, ],
        changedTouches: [{
          identifier: 1,
34 35 36 37 38 39
          pageX: 101,
          pageY: 101,
          clientX: 101,
          clientY: 101,
          screenX: 101,
          screenY: 101
DCloud-WZF's avatar
DCloud-WZF 已提交
40 41
        }, ],
      })
DCloud-WZF's avatar
DCloud-WZF 已提交
42 43
      const touchStartTouchTargetIdentifier = '1'
      const touchStartTouchTargetValue = '101'
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      const touchStartTouchIdentifier = await page.$('#touch-start-touch-identifier')
      expect(await touchStartTouchIdentifier.text()).toBe(touchStartTouchTargetIdentifier)
      const touchStartTouchPageX = await page.$('#touch-start-touch-page-x')
      expect(await touchStartTouchPageX.text()).toBe(touchStartTouchTargetValue)
      const touchStartTouchPageY = await page.$('#touch-start-touch-page-y')
      expect(await touchStartTouchPageY.text()).toBe(touchStartTouchTargetValue)
      const touchStartTouchClientX = await page.$('#touch-start-touch-client-x')
      expect(await touchStartTouchClientX.text()).toBe(touchStartTouchTargetValue)
      const touchStartTouchClientY = await page.$('#touch-start-touch-client-y')
      expect(await touchStartTouchClientY.text()).toBe(touchStartTouchTargetValue)
      const touchStartTouchScreenX = await page.$('#touch-start-touch-screen-x')
      expect(await touchStartTouchScreenX.text()).toBe(touchStartTouchTargetValue)
      const touchStartTouchScreenY = await page.$('#touch-start-touch-screen-y')
      expect(await touchStartTouchScreenY.text()).toBe(touchStartTouchTargetValue)

      const touchStartChangedTouchIdentifier = await page.$('#touch-start-changed-touch-identifier')
      expect(await touchStartChangedTouchIdentifier.text()).toBe(touchStartTouchTargetIdentifier)
      const touchStartChangedTouchPageX = await page.$('#touch-start-changed-touch-page-x')
      expect(await touchStartChangedTouchPageX.text()).toBe(touchStartTouchTargetValue)
      const touchStartChangedTouchPageY = await page.$('#touch-start-changed-touch-page-y')
      expect(await touchStartChangedTouchPageY.text()).toBe(touchStartTouchTargetValue)
      const touchStartChangedTouchClientX = await page.$('#touch-start-changed-touch-client-x')
      expect(await touchStartChangedTouchClientX.text()).toBe(touchStartTouchTargetValue)
      const touchStartChangedTouchClientY = await page.$('#touch-start-changed-touch-client-y')
      expect(await touchStartChangedTouchClientY.text()).toBe(touchStartTouchTargetValue)
      const touchStartChangedTouchScreenX = await page.$('#touch-start-changed-touch-screen-x')
      expect(await touchStartChangedTouchScreenX.text()).toBe(touchStartTouchTargetValue)
      const touchStartChangedTouchScreenY = await page.$('#touch-start-changed-touch-screen-y')
      expect(await touchStartChangedTouchScreenY.text()).toBe(touchStartTouchTargetValue)

DCloud-WZF's avatar
DCloud-WZF 已提交
74 75 76
      await el.touchmove({
        touches: [{
          identifier: 1,
77 78 79 80 81 82 83
          pageX: 102,
          pageY: 102,
          clientX: 102,
          clientY: 102,
          screenX: 102,
          screenY: 102
        }],
DCloud-WZF's avatar
DCloud-WZF 已提交
84 85
        changedTouches: [{
          identifier: 1,
86 87 88 89 90 91
          pageX: 102,
          pageY: 102,
          clientX: 102,
          clientY: 102,
          screenX: 102,
          screenY: 102
DCloud-WZF's avatar
DCloud-WZF 已提交
92 93
        }, ],
      })
94

DCloud-WZF's avatar
DCloud-WZF 已提交
95 96
      const touchMoveTouchTargetIdentifier = '1'
      const touchMoveTouchTargetValue = '102'
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      const touchMoveTouchIdentifier = await page.$('#touch-move-touch-identifier')
      expect(await touchMoveTouchIdentifier.text()).toBe(touchMoveTouchTargetIdentifier)
      const touchMoveTouchPageX = await page.$('#touch-move-touch-page-x')
      expect(await touchMoveTouchPageX.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveTouchPageY = await page.$('#touch-move-touch-page-y')
      expect(await touchMoveTouchPageY.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveTouchClientX = await page.$('#touch-move-touch-client-x')
      expect(await touchMoveTouchClientX.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveTouchClientY = await page.$('#touch-move-touch-client-y')
      expect(await touchMoveTouchClientY.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveTouchScreenX = await page.$('#touch-move-touch-screen-x')
      expect(await touchMoveTouchScreenX.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveTouchScreenY = await page.$('#touch-move-touch-screen-y')
      expect(await touchMoveTouchScreenY.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveChangedTouchIdentifier = await page.$('#touch-move-changed-touch-identifier')
      expect(await touchMoveChangedTouchIdentifier.text()).toBe(touchMoveTouchTargetIdentifier)
      const touchMoveChangedTouchPageX = await page.$('#touch-move-changed-touch-page-x')
      expect(await touchMoveChangedTouchPageX.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveChangedTouchPageY = await page.$('#touch-move-changed-touch-page-y')
      expect(await touchMoveChangedTouchPageY.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveChangedTouchClientX = await page.$('#touch-move-changed-touch-client-x')
      expect(await touchMoveChangedTouchClientX.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveChangedTouchClientY = await page.$('#touch-move-changed-touch-client-y')
      expect(await touchMoveChangedTouchClientY.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveChangedTouchScreenX = await page.$('#touch-move-changed-touch-screen-x')
      expect(await touchMoveChangedTouchScreenX.text()).toBe(touchMoveTouchTargetValue)
      const touchMoveChangedTouchScreenY = await page.$('#touch-move-changed-touch-screen-y')
      expect(await touchMoveChangedTouchScreenY.text()).toBe(touchMoveTouchTargetValue)

DCloud-WZF's avatar
DCloud-WZF 已提交
126
      await el.touchend({
127 128 129 130 131 132 133 134 135
        touches: [{
          identifier: 1,
          pageX: 103,
          pageY: 103,
          clientX: 103,
          clientY: 103,
          screenX: 103,
          screenY: 103
        }],
DCloud-WZF's avatar
DCloud-WZF 已提交
136 137
        changedTouches: [{
          identifier: 1,
138 139 140 141 142 143
          pageX: 103,
          pageY: 103,
          clientX: 103,
          clientY: 103,
          screenX: 103,
          screenY: 103
DCloud-WZF's avatar
DCloud-WZF 已提交
144 145
        }, ],
      })
DCloud-WZF's avatar
DCloud-WZF 已提交
146 147
      const touchEndTouchTargetIdentifier = '1'
      const touchEndTouchTargetValue = '103'
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
      const touchEndTouchIdentifier = await page.$('#touch-end-touch-identifier')
      expect(await touchEndTouchIdentifier.text()).toBe(touchEndTouchTargetIdentifier)
      const touchEndTouchPageX = await page.$('#touch-end-touch-page-x')
      expect(await touchEndTouchPageX.text()).toBe(touchEndTouchTargetValue)
      const touchEndTouchPageY = await page.$('#touch-end-touch-page-y')
      expect(await touchEndTouchPageY.text()).toBe(touchEndTouchTargetValue)
      const touchEndTouchClientX = await page.$('#touch-end-touch-client-x')
      expect(await touchEndTouchClientX.text()).toBe(touchEndTouchTargetValue)
      const touchEndTouchClientY = await page.$('#touch-end-touch-client-y')
      expect(await touchEndTouchClientY.text()).toBe(touchEndTouchTargetValue)
      const touchEndTouchScreenX = await page.$('#touch-end-touch-screen-x')
      expect(await touchEndTouchScreenX.text()).toBe(touchEndTouchTargetValue)
      const touchEndTouchScreenY = await page.$('#touch-end-touch-screen-y')
      expect(await touchEndTouchScreenY.text()).toBe(touchEndTouchTargetValue)
      const touchEndChangedTouchIdentifier = await page.$('#touch-end-changed-touch-identifier')
      expect(await touchEndChangedTouchIdentifier.text()).toBe(touchEndTouchTargetIdentifier)
      const touchEndChangedTouchPageX = await page.$('#touch-end-changed-touch-page-x')
      expect(await touchEndChangedTouchPageX.text()).toBe(touchEndTouchTargetValue)
      const touchEndChangedTouchPageY = await page.$('#touch-end-changed-touch-page-y')
      expect(await touchEndChangedTouchPageY.text()).toBe(touchEndTouchTargetValue)
      const touchEndChangedTouchClientX = await page.$('#touch-end-changed-touch-client-x')
      expect(await touchEndChangedTouchClientX.text()).toBe(touchEndTouchTargetValue)
      const touchEndChangedTouchClientY = await page.$('#touch-end-changed-touch-client-y')
      expect(await touchEndChangedTouchClientY.text()).toBe(touchEndTouchTargetValue)
      const touchEndChangedTouchScreenX = await page.$('#touch-end-changed-touch-screen-x')
      expect(await touchEndChangedTouchScreenX.text()).toBe(touchEndTouchTargetValue)
      const touchEndChangedTouchScreenY = await page.$('#touch-end-changed-touch-screen-y')
      expect(await touchEndChangedTouchScreenY.text()).toBe(touchEndTouchTargetValue)
DCloud-WZF's avatar
DCloud-WZF 已提交
176 177 178 179
    }
  })

  it('click', async () => {
180
    const el = await page.$('#longpress-target')
DCloud-WZF's avatar
DCloud-WZF 已提交
181
    await el.tap()
DCloud-WZF's avatar
DCloud-WZF 已提交
182 183
    const targetX = '0'
    const targetY = '0'
184 185 186 187 188 189 190 191
    const tapEventX = await page.$('#tap-event-x')
    expect(await tapEventX.text()).toBe(targetX)
    const tapEventY = await page.$('#tap-event-y')
    expect(await tapEventY.text()).toBe(targetY)
    const clickEventX = await page.$('#click-event-x')
    expect(await clickEventX.text()).toBe(targetX)
    const clickEventY = await page.$('#click-event-y')
    expect(await clickEventY.text()).toBe(targetY)
DCloud-WZF's avatar
DCloud-WZF 已提交
192 193 194
  })

  it('longPress', async () => {
195 196
    if (!process.env.UNI_AUTOMATOR_APP_WEBVIEW) {
      const el = await page.$('#longpress-target')
DCloud-WZF's avatar
DCloud-WZF 已提交
197
      await el.longpress()
DCloud-WZF's avatar
DCloud-WZF 已提交
198 199
      const longPressTouchTargetIdentifier = '1'
      const longPressTouchTargetValue = '0'
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
      const longPressTouchIdentifier = await page.$('#long-press-touch-identifier')
      expect(await longPressTouchIdentifier.text()).toBe(longPressTouchTargetIdentifier)
      const longPressTouchPageX = await page.$('#long-press-touch-page-x')
      expect(await longPressTouchPageX.text()).toBe(longPressTouchTargetValue)
      const longPressTouchPageY = await page.$('#long-press-touch-page-y')
      expect(await longPressTouchPageY.text()).toBe(longPressTouchTargetValue)
      const longPressTouchClientX = await page.$('#long-press-touch-client-x')
      expect(await longPressTouchClientX.text()).toBe(longPressTouchTargetValue)
      const longPressTouchClientY = await page.$('#long-press-touch-client-y')
      expect(await longPressTouchClientY.text()).toBe(longPressTouchTargetValue)
      const longPressTouchScreenX = await page.$('#long-press-touch-screen-x')
      expect(await longPressTouchScreenX.text()).toBe(longPressTouchTargetValue)
      const longPressTouchScreenY = await page.$('#long-press-touch-screen-y')
      expect(await longPressTouchScreenY.text()).toBe(longPressTouchTargetValue)
      const longPressChangedTouchIdentifier = await page.$('#long-press-changed-touch-identifier')
      expect(await longPressChangedTouchIdentifier.text()).toBe(longPressTouchTargetIdentifier)
      const longPressChangedTouchPageX = await page.$('#long-press-changed-touch-page-x')
      expect(await longPressChangedTouchPageX.text()).toBe(longPressTouchTargetValue)
      const longPressChangedTouchPageY = await page.$('#long-press-changed-touch-page-y')
      expect(await longPressChangedTouchPageY.text()).toBe(longPressTouchTargetValue)
      const longPressChangedTouchClientX = await page.$('#long-press-changed-touch-client-x')
      expect(await longPressChangedTouchClientX.text()).toBe(longPressTouchTargetValue)
      const longPressChangedTouchClientY = await page.$('#long-press-changed-touch-client-y')
      expect(await longPressChangedTouchClientY.text()).toBe(longPressTouchTargetValue)
      const longPressChangedTouchScreenX = await page.$('#long-press-changed-touch-screen-x')
      expect(await longPressChangedTouchScreenX.text()).toBe(longPressTouchTargetValue)
      const longPressChangedTouchScreenY = await page.$('#long-press-changed-touch-screen-y')
      expect(await longPressChangedTouchScreenY.text()).toBe(longPressTouchTargetValue)
228 229

      if (isAndroid || isIos) {
230
        if (isIos) {
231 232 233 234 235
          // 规避系统授权弹框
          await program.tap({
            x: 100,
            y: 500,
          })
236
        }
237 238 239 240 241
        await program.tap({
          x: 200,
          y: 400,
          duration: 1000
        })
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
        const longPressTouchIdentifierText = await longPressTouchIdentifier.text()
        expect(longPressTouchIdentifierText).not.toBe(longPressTouchTargetIdentifier)
        expect(longPressTouchIdentifierText).toBeTruthy()
        const longPressTouchPageXText = await longPressTouchPageX.text()
        expect(longPressTouchPageXText).not.toBe(longPressTouchTargetValue)
        expect(longPressTouchPageXText).toBeTruthy()
        const longPressTouchPageYText = await longPressTouchPageY.text()
        expect(longPressTouchPageYText).not.toBe(longPressTouchTargetValue)
        expect(longPressTouchPageYText).toBeTruthy()
        const longPressTouchClientXText = await longPressTouchClientX.text()
        expect(longPressTouchClientXText).not.toBe(longPressTouchTargetValue)
        expect(longPressTouchClientXText).toBeTruthy()
        const longPressTouchClientYText = await longPressTouchClientY.text()
        expect(longPressTouchClientYText).not.toBe(longPressTouchTargetValue)
        expect(longPressTouchClientYText).toBeTruthy()
        const longPressTouchScreenXText = await longPressTouchScreenX.text()
        expect(longPressTouchScreenXText).not.toBe(longPressTouchTargetValue)
        expect(longPressTouchScreenXText).toBeTruthy()
        const longPressTouchScreenYText = await longPressTouchScreenY.text()
        expect(longPressTouchScreenYText).not.toBe(longPressTouchTargetValue)
        expect(longPressTouchScreenYText).toBeTruthy()
        const longPressChangedTouchIdentifierText = await longPressChangedTouchIdentifier.text()
        expect(longPressChangedTouchIdentifierText).not.toBe(longPressTouchTargetIdentifier)
        expect(longPressChangedTouchIdentifierText).toBeTruthy()
        const longPressChangedTouchPageXText = await longPressChangedTouchPageX.text()
        expect(longPressChangedTouchPageXText).not.toBe(longPressTouchTargetValue)
        expect(longPressChangedTouchPageXText).toBeTruthy()
        const longPressChangedTouchPageYText = await longPressChangedTouchPageY.text()
        expect(longPressChangedTouchPageYText).not.toBe(longPressTouchTargetValue)
        expect(longPressChangedTouchPageYText).toBeTruthy()
        const longPressChangedTouchClientXText = await longPressChangedTouchClientX.text()
        expect(longPressChangedTouchClientXText).not.toBe(longPressTouchTargetValue)
        expect(longPressChangedTouchClientXText).toBeTruthy()
        const longPressChangedTouchClientYText = await longPressChangedTouchClientY.text()
        expect(longPressChangedTouchClientYText).not.toBe(longPressTouchTargetValue)
        expect(longPressChangedTouchClientYText).toBeTruthy()
        const longPressChangedTouchScreenXText = await longPressChangedTouchScreenX.text()
        expect(longPressChangedTouchScreenXText).not.toBe(longPressTouchTargetValue)
        expect(longPressChangedTouchScreenXText).toBeTruthy()
        const longPressChangedTouchScreenYText = await longPressChangedTouchScreenY.text()
        expect(longPressChangedTouchScreenYText).not.toBe(longPressTouchTargetValue)
        expect(longPressChangedTouchScreenYText).toBeTruthy()
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
285 286
    }
  })
287 288 289 290 291 292 293 294 295

  it('mock tap event', async () => {

    // ios only 坐标换算准确
    if (isIos) {

      page = await program.reLaunch(PAGE_PATH)
      await page.waitFor('view')

296 297 298 299 300 301 302
      const [
        x,
        y
      ] = await page.callMethod('jest_getRect')

      expect(x > 0).toBe(true)
      expect(y > 0).toBe(true)
303 304 305

      await program.tap({
        x: x,
306
        y: y
307
      })
308
      await page.waitFor(200)
309 310 311 312 313 314 315 316 317 318 319 320

      const clickEventX = await page.$('#click-event-x')
      const StringX = await clickEventX.text()

      expect(Number(StringX)).toBeGreaterThan(0)
      const clickEventY = await page.$('#click-event-y')
      const StringY = await clickEventY.text()
      expect(Number(StringY)).toBeGreaterThan(0)
    } else {
      expect(1).toBe(1)
    }
  })
321
})