video.test.js 5.7 KB
Newer Older
1
describe('component-native-video', () => {
2 3 4 5 6 7 8
  if(process.env.uniTestPlatformInfo.startsWith('web')){
    // TODO: web 端暂不支持测试
    it('web', async () => {
      expect(1).toBe(1)
    })
    return
  }
9 10
  let page;
  beforeAll(async () => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
11
    page = await program.reLaunch('/pages/component/video/video');
12 13 14 15 16
    if(process.env.uniTestPlatformInfo.startsWith('web')){
      await page.setData({
        muted: true
      });
    }
17
    await page.waitFor(1000);
18 19 20
  });

  it('test API', async () => {
21
    expect(await page.data('isError')).toBe(false);
22 23
    // play
    await page.callMethod('play');
24 25
    await page.waitFor(100);
    expect(await page.data('isPlaying')).toBe(true);
26 27
    // pause
    await page.callMethod('pause');
28 29
    await page.waitFor(100);
    expect(await page.data('isPause')).toBe(true);
30 31
  });

32
  it('test local source', async () => {
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    await page.setData({
      autoTest: true,
      isError: false
    });
    await page.callMethod('downloadSource');
    await page.waitFor(5000);
    expect(await page.data('isError')).toBe(false);
    await page.setData({
      localSrc: '/static/test-video/2minute-demo.m3u8'
    });
    await page.waitFor(100);
    expect(await page.data('isError')).toBe(false);
    await page.setData({
      autoTest: false
    });
  });

  it('test event play pause', async () => {
    await page.setData({
      autoTest: true
    });
    await page.callMethod('play');
55
    await page.waitFor(100);
lizhongyi_'s avatar
lizhongyi_ 已提交
56 57 58 59 60 61 62 63 64 65 66
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      expect(await page.data('eventPlay')).toEqual({
        type: 'play'
      });
    }else {
      expect(await page.data('eventPlay')).toEqual({
        tagName: 'VIDEO',
        type: 'play'
      });
    }

67
    await page.callMethod('pause');
68
    await page.waitFor(100);
lizhongyi_'s avatar
lizhongyi_ 已提交
69 70 71 72 73 74 75 76 77 78 79
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      expect(await page.data('eventPause')).toEqual({
        type: 'pause'
      });
    }else {
       expect(await page.data('eventPause')).toEqual({
         tagName: 'VIDEO',
         type: 'pause'
       });
    }

80 81 82 83
    await page.callMethod('play');
  });

  it('test event waiting progress timeupdate', async () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
84 85 86
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      return
    }
87 88 89 90
    await page.setData({
      pos: 10
    });
    await page.callMethod('seek');
91
    await page.waitFor(100);
92 93 94 95
    expect(await page.data('eventWaiting')).toEqual({
      tagName: 'VIDEO',
      type: 'waiting'
    });
96
    await page.waitFor(200);
97 98 99
    expect(await page.data('eventProgress')).toEqual({
      tagName: 'VIDEO',
      type: 'progress',
100
      isBufferedValid: true
101
    });
102 103 104 105 106 107 108 109 110 111 112
    const infos = process.env.uniTestPlatformInfo.split(' ');
    const version = parseInt(infos[infos.length - 1]);
    if (process.env.uniTestPlatformInfo.startsWith('android') && version > 5) {
      await page.waitFor(200);
      expect(await page.data('eventTimeupdate')).toEqual({
        tagName: 'VIDEO',
        type: 'timeupdate',
        currentTime: 10,
        duration: 121
      });
    }
113 114
  });

115
  it('test event fullscreenchange controlstoggle fullscreenclick', async () => {
116
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
117 118
      return;
    }
119
    await page.callMethod('requestFullScreen');
120
    await page.waitFor(500);
121 122 123 124 125 126 127
    expect(await page.data('eventFullscreenchange')).toEqual({
      tagName: 'VIDEO',
      type: 'fullscreenchange',
      fullScreen: true,
      direction: 'horizontal'
    });
    if (process.env.uniTestPlatformInfo.startsWith('android')) {
128 129
      await page.waitFor(5000);
      await program.adbCommand('input tap 10 10');
130
      await page.waitFor(100);
131 132 133 134 135 136 137 138 139
      const infos = process.env.uniTestPlatformInfo.split(' ');
      const version = parseInt(infos[infos.length - 1]);
      if (version > 5) { // android5.1模拟器全屏时会弹出系统提示框,无法响应adb tap命令
        expect(await page.data('eventControlstoggle')).toEqual({
          tagName: 'VIDEO',
          type: 'controlstoggle',
          show: true
        });
      }
140 141 142 143 144
      const res = await program.adbCommand('wm size');
      const width = res.data.split(' ').at(-1).split('x')[0];
      const height = res.data.split(' ').at(-1).split('x')[1];
      const res2 = await program.adbCommand('wm density');
      const scale = res2.data.split(' ').at(-1) / 160;
145 146 147
      expect(await page.data('eventFullscreenclick')).toEqual({
        tagName: 'VIDEO',
        type: 'fullscreenclick',
148 149
        screenX: parseInt(10 / scale),
        screenY: parseInt(10 / scale),
150 151 152 153 154 155 156 157
        screenWidth: parseInt(height / scale),
        screenHeight: parseInt(width / scale)
      });
    }
    await page.callMethod('exitFullScreen');
  });

  it('test event ended', async () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
158 159 160
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      return
    }
161 162 163 164
    await page.setData({
      pos: 120
    });
    await page.callMethod('seek');
165
    await page.waitFor(2500);
166 167 168 169 170 171 172
    expect(await page.data('eventEnded')).toEqual({
      tagName: 'VIDEO',
      type: 'ended'
    });
  });

  it('test event error', async () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
173 174 175
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      return
    }
176 177 178 179
    const oldSrc = await page.data('src');
    await page.setData({
      src: 'invalid url'
    });
180
    await page.waitFor(300);
181 182 183 184 185 186 187 188 189
    expect(await page.data('eventError')).toEqual({
      tagName: 'VIDEO',
      type: 'error',
      errCode: 300001
    });
    await page.setData({
      autoTest: false,
      src: oldSrc
    });
190 191
  });

192
  it('test format', async () => {
193
    page = await program.navigateTo('/pages/component/video/video-format');
194 195 196 197
    await page.waitFor(1000);
    expect(await page.data('isError')).toBe(false);
  });
});