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

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

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

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

103 104
  });

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

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

166
  it('test event ended timeupdate', async () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
167 168 169
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      return
    }
170
    await page.callMethod('seek', 120);
171 172
    start = Date.now();
    await page.waitFor(async () => {
173
      return (await page.data('eventEnded')) || (Date.now() - start > 30000);
174
    });
175 176 177 178
    expect(await page.data('eventEnded')).toEqual({
      tagName: 'VIDEO',
      type: 'ended'
    });
179 180 181 182 183 184 185 186 187 188 189 190 191 192
    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
      });
    }
193 194 195
  });

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

218 219 220 221 222 223 224 225 226 227
  it('test sub component', async () => {
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) return
    await page.setData({
      subCompEnable: true,
      subCompShow: true
    });
    await page.waitFor(100);
    expect(await page.callMethod('hasSubComponent')).toBe(true);
  });

228
  it('test format', async () => {
229
    page = await program.navigateTo('/pages/component/video/video-format');
230
    await page.waitFor(1000);
231
    expect((await page.data('isError')).value).toBe(false);
232 233
  });
});