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

  it('test API', async () => {
28
    expect(await page.data('isError')).toBe(false);
29 30
    // play
    await page.callMethod('play');
31 32
    await page.waitFor(100);
    expect(await page.data('isPlaying')).toBe(true);
33 34
    // pause
    await page.callMethod('pause');
35 36
    await page.waitFor(100);
    expect(await page.data('isPause')).toBe(true);
37 38
  });

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

59 60 61 62 63 64 65 66 67 68 69 70
    it('test assets path', async () => {
      if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) return;
      const oldSrc = await page.data('src');
      await page.setData({
        isError: false,
        src: 'file:///android_asset/uni-autoTest/demo10s.mp4'
      });
      await page.waitFor(500);
      expect(await page.data('isError')).toBe(false);
      await page.setData({
        src: oldSrc
      });
71
    });
72 73
  }
  it('test event play pause controlstoggle', async () => {
74
    await page.setData({
75
      autoTest: true,
76 77
    });
    await page.callMethod('play');
78 79 80 81
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventPlay')) || (Date.now() - start > 500);
    });
lizhongyi_'s avatar
lizhongyi_ 已提交
82
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
83 84 85
      // expect(await page.data('eventPlay')).toEqual({
      //   type: 'play'
      // });
lizhongyi_'s avatar
lizhongyi_ 已提交
86 87
    }else {
      expect(await page.data('eventPlay')).toEqual({
88
        tagName: isMP ? undefined : 'VIDEO',
lizhongyi_'s avatar
lizhongyi_ 已提交
89 90 91
        type: 'play'
      });
    }
92
    await page.callMethod('pause');
93 94
    start = Date.now();
    await page.waitFor(async () => {
95
      return (await page.data('eventPause')) || (Date.now() - start > 1000);
96
    });
lizhongyi_'s avatar
lizhongyi_ 已提交
97
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
98 99 100
      // expect(await page.data('eventPause')).toEqual({
      //   type: 'pause'
      // });
101
    } else {
lizhongyi_'s avatar
lizhongyi_ 已提交
102
       expect(await page.data('eventPause')).toEqual({
103
         tagName: isMP ? undefined : 'VIDEO',
lizhongyi_'s avatar
lizhongyi_ 已提交
104 105 106
         type: 'pause'
       });
    }
107
    if(!isMP && !isWeb) {
雪洛's avatar
雪洛 已提交
108 109 110 111
      /**
       * app端video组件controlstoggle事件会在controls显示和隐藏触发(播放、暂停等操作都会触发)。
       * 微信小程序和web播放暂停或者一些其他的操作也会影响controls的显隐,但是不会触发controlstoggle, 只有controls属性变化的时候才会触发
       */
112 113 114 115
      await page.callMethod('play');
      start = Date.now();
      await page.waitFor(async () => {
        return (await page.data('eventControlstoggle')) || (Date.now() - start > 1000);
116
      });
117 118 119 120 121 122 123 124 125 126 127 128 129
      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: isMP ? undefined : 'VIDEO',
          type: 'controlstoggle',
          show: true
        });
      }
130 131
    }

132 133
  });

134
  it('test event waiting progress', async () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
135 136 137
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      return
    }
138
    await page.callMethod('seek', 10);
139 140 141 142
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventWaiting')) && (await page.data('eventProgress')) || (Date.now() - start > 1000);
    });
143
    expect(await page.data('eventWaiting')).toEqual({
144
      tagName: isMP ? undefined : 'VIDEO',
145 146 147
      type: 'waiting'
    });
    expect(await page.data('eventProgress')).toEqual({
148
      tagName: isMP ? undefined : 'VIDEO',
149
      type: 'progress',
150
      isBufferedValid: true
151 152 153
    });
  });

154 155 156
  if (isAndroid) {
    it('test event fullscreenchange fullscreenclick', async () => {
      await page.callMethod('requestFullScreen');
157 158
      start = Date.now();
      await page.waitFor(async () => {
159
        return (await page.data('eventFullscreenchange')) || (Date.now() - start > 1000);
160
      });
161 162 163 164 165
      expect(await page.data('eventFullscreenchange')).toEqual({
        tagName: isMP ? undefined : 'VIDEO',
        type: 'fullscreenchange',
        fullScreen: true,
        direction: 'horizontal'
166
      });
167 168 169 170 171 172 173 174 175 176 177 178 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) { // android5.1模拟器全屏时会弹出系统提示框,无法响应adb tap命令
        await page.waitFor(5000);
        await program.adbCommand('input tap 10 10');
        start = Date.now();
        await page.waitFor(async () => {
          return (await page.data('eventFullscreenclick')) || (Date.now() - start > 1000);
        });
        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;
        expect(await page.data('eventFullscreenclick')).toEqual({
          tagName: isMP ? undefined : 'VIDEO',
          type: 'fullscreenclick',
          screenX: parseInt(10 / scale),
          screenY: parseInt(10 / scale),
          screenWidth: parseInt(height / scale),
          screenHeight: parseInt(width / scale)
        });
      }
      await page.callMethod('exitFullScreen');
    });
  }
193

194
  it('test event ended timeupdate', async () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
195 196 197
    if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
      return
    }
198
    await page.callMethod('seek', 120);
199 200
    start = Date.now();
    await page.waitFor(async () => {
201
      return (await page.data('eventEnded')) || (Date.now() - start > 30000);
202
    });
203
    expect(await page.data('eventEnded')).toEqual({
204
      tagName: isMP ? undefined : 'VIDEO',
205 206
      type: 'ended'
    });
207 208 209 210 211 212 213 214
    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({
215
        tagName: isMP ? undefined : 'VIDEO',
216 217 218 219 220
        type: 'timeupdate',
        currentTime: 121,
        duration: 121
      });
    }
221 222 223
  });

  it('test event error', async () => {
224
    if (isIOS || isMP) {
lizhongyi_'s avatar
lizhongyi_ 已提交
225 226
      return
    }
227 228 229 230
    const oldSrc = await page.data('src');
    await page.setData({
      src: 'invalid url'
    });
231 232 233 234
    start = Date.now();
    await page.waitFor(async () => {
      return (await page.data('eventError')) || (Date.now() - start > 1000);
    });
235 236 237 238 239 240 241 242 243
    expect(await page.data('eventError')).toEqual({
      tagName: 'VIDEO',
      type: 'error',
      errCode: 300001
    });
    await page.setData({
      autoTest: false,
      src: oldSrc
    });
244 245
  });

246
  it('test sub component', async () => {
247 248 249
    if (isIOS || isMP) {
      return
    }
250 251 252 253 254 255 256 257
    await page.setData({
      subCompEnable: true,
      subCompShow: true
    });
    await page.waitFor(100);
    expect(await page.callMethod('hasSubComponent')).toBe(true);
  });

258
  it('test format', async () => {
259
    page = await program.navigateTo('/pages/component/video/video-format');
260
    await page.waitFor(1000);
261
    expect((await page.data('isError')).value).toBe(false);
262 263
  });
});