video.test.js 7.0 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
  let page;
10
  let start = 0;
11
  beforeAll(async () => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
12
    page = await program.reLaunch('/pages/component/video/video');
13 14 15 16 17
    if(process.env.uniTestPlatformInfo.startsWith('web')){
      await page.setData({
        muted: true
      });
    }
18
    await page.waitFor(1000);
19 20 21
  });

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

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

52
  it('test event play pause controlstoggle', async () => {
53
    await page.callMethod('play');
54 55 56 57
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventPlay')) || (Date.now() - start > 500);
    });
lizhongyi_'s avatar
lizhongyi_ 已提交
58
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
59 60 61
      // expect(await page.data('eventPlay')).toEqual({
      //   type: 'play'
      // });
lizhongyi_'s avatar
lizhongyi_ 已提交
62 63 64 65 66 67
    }else {
      expect(await page.data('eventPlay')).toEqual({
        tagName: 'VIDEO',
        type: 'play'
      });
    }
68
    await page.callMethod('pause');
69 70 71 72
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventPause')) || (Date.now() - start > 500);
    });
lizhongyi_'s avatar
lizhongyi_ 已提交
73
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
74 75 76
      // expect(await page.data('eventPause')).toEqual({
      //   type: 'pause'
      // });
lizhongyi_'s avatar
lizhongyi_ 已提交
77 78 79 80 81 82
    }else {
       expect(await page.data('eventPause')).toEqual({
         tagName: 'VIDEO',
         type: 'pause'
       });
    }
83
    await page.callMethod('play');
84 85 86 87
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventControlstoggle')) || (Date.now() - start > 500);
    });
88 89 90 91 92 93 94 95 96 97 98 99 100 101
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      // expect(await page.data('eventControlstoggle')).toEqual({
      //   tagName: 'VIDEO',
      //   type: 'controlstoggle',
      //   show: true
      // });
    }else {
      expect(await page.data('eventControlstoggle')).toEqual({
        tagName: 'VIDEO',
        type: 'controlstoggle',
        show: true
      });
    }

102 103
  });

104
  it('test event waiting progress', async () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
105 106 107
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      return
    }
108 109 110 111
    await page.setData({
      pos: 10
    });
    await page.callMethod('seek');
112 113 114 115
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventWaiting')) && (await page.data('eventProgress')) || (Date.now() - start > 1000);
    });
116 117 118 119 120 121 122
    expect(await page.data('eventWaiting')).toEqual({
      tagName: 'VIDEO',
      type: 'waiting'
    });
    expect(await page.data('eventProgress')).toEqual({
      tagName: 'VIDEO',
      type: 'progress',
123
      isBufferedValid: true
124 125 126
    });
  });

127
  it('test event fullscreenchange fullscreenclick', async () => {
128
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
129 130
      return;
    }
131
    await page.callMethod('requestFullScreen');
132 133 134 135
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventFullscreenchange')) || (Date.now() - start > 1000);
    });
136 137 138 139 140 141
    expect(await page.data('eventFullscreenchange')).toEqual({
      tagName: 'VIDEO',
      type: 'fullscreenchange',
      fullScreen: true,
      direction: 'horizontal'
    });
142 143 144
    const infos = process.env.uniTestPlatformInfo.split(' ');
    const version = parseInt(infos[infos.length - 1]);
    if (process.env.uniTestPlatformInfo.startsWith('android') && version > 5) { // android5.1模拟器全屏时会弹出系统提示框,无法响应adb tap命令
145 146
      await page.waitFor(5000);
      await program.adbCommand('input tap 10 10');
147 148
      start = Date.now();
      await page.waitFor(async () => {
149
        return (await page.data('eventFullscreenclick')) || (Date.now() - start > 500);
150
      });
151 152 153 154 155
      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;
156 157 158
      expect(await page.data('eventFullscreenclick')).toEqual({
        tagName: 'VIDEO',
        type: 'fullscreenclick',
159 160
        screenX: parseInt(10 / scale),
        screenY: parseInt(10 / scale),
161 162 163 164 165 166 167
        screenWidth: parseInt(height / scale),
        screenHeight: parseInt(width / scale)
      });
    }
    await page.callMethod('exitFullScreen');
  });

168
  it('test event ended timeupdate', async () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
169 170 171
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      return
    }
172 173 174 175
    await page.setData({
      pos: 120
    });
    await page.callMethod('seek');
176 177
    start = Date.now();
    await page.waitFor(async () => {
178
      return (await page.data('eventEnded')) || (Date.now() - start > 5000);
179
    });
180 181 182 183
    expect(await page.data('eventEnded')).toEqual({
      tagName: 'VIDEO',
      type: 'ended'
    });
184 185 186 187 188 189 190 191 192 193 194 195 196 197
    const infos = process.env.uniTestPlatformInfo.split(' ');
    const version = parseInt(infos[infos.length - 1]);
    if (process.env.uniTestPlatformInfo.startsWith('android') && version > 5) {
      start = Date.now();
      await page.waitFor(async () => {
        return (await page.data('eventTimeupdate')) || (Date.now() - start > 500);
      });
      expect(await page.data('eventTimeupdate')).toEqual({
        tagName: 'VIDEO',
        type: 'timeupdate',
        currentTime: 121,
        duration: 121
      });
    }
198 199 200
  });

  it('test event error', async () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
201 202 203
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      return
    }
204 205 206 207
    const oldSrc = await page.data('src');
    await page.setData({
      src: 'invalid url'
    });
208 209 210 211
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventError')) || (Date.now() - start > 1000);
    });
212 213 214 215 216 217 218 219 220
    expect(await page.data('eventError')).toEqual({
      tagName: 'VIDEO',
      type: 'error',
      errCode: 300001
    });
    await page.setData({
      autoTest: false,
      src: oldSrc
    });
221 222
  });

223
  it('test format', async () => {
224
    page = await program.navigateTo('/pages/component/video/video-format');
225
    await page.waitFor(1000);
226
    expect((await page.data('isError')).value).toBe(false);
227 228
  });
});