videojs-contrib-media-sources.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import document from 'global/document';
  2. import window from 'global/window';
  3. import QUnit from 'qunit';
  4. import sinon from 'sinon';
  5. import videojs from 'video.js';
  6. import FlashMediaSource from '../src/flash-media-source';
  7. import HtmlMediaSource from '../src/html-media-source';
  8. // we disable this because browserify needs to include these files
  9. // but the exports are not important
  10. /* eslint-disable no-unused-vars */
  11. import {MediaSource, URL} from '../src/videojs-contrib-media-sources.js';
  12. /* eslint-disable no-unused-vars */
  13. QUnit.test('the environment is sane', function(assert) {
  14. assert.strictEqual(typeof Array.isArray, 'function', 'es5 exists');
  15. assert.strictEqual(typeof sinon, 'object', 'sinon exists');
  16. assert.strictEqual(typeof videojs, 'function', 'videojs exists');
  17. assert.strictEqual(typeof videojs.MediaSource, 'function', 'plugin is a function');
  18. });
  19. QUnit.module('videojs-contrib-media-sources - General', {
  20. beforeEach() {
  21. this.fixture = document.getElementById('qunit-fixture');
  22. this.video = document.createElement('video');
  23. this.fixture.appendChild(this.video);
  24. this.player = videojs(this.video);
  25. // Mock the environment's timers because certain things - particularly
  26. // player readiness - are asynchronous in video.js 5.
  27. this.clock = sinon.useFakeTimers();
  28. this.oldMediaSource = window.MediaSource || window.WebKitMediaSource;
  29. },
  30. afterEach() {
  31. // The clock _must_ be restored before disposing the player; otherwise,
  32. // certain timeout listeners that happen inside video.js may throw errors.
  33. this.clock.restore();
  34. this.player.dispose();
  35. window.MediaSource = window.WebKitMediaSource = this.oldMediaSource;
  36. }
  37. });
  38. QUnit.test('Plugin is registered', function(assert) {
  39. assert.strictEqual(
  40. typeof videojs.MediaSource,
  41. 'function',
  42. 'MediaSource plugin is attached to videojs'
  43. );
  44. assert.strictEqual(
  45. typeof videojs.URL,
  46. 'object',
  47. 'URL plugin is attached to player'
  48. );
  49. });
  50. QUnit.test('implementation selection is overridable', function() {
  51. // mock native MediaSources
  52. window.MediaSource = videojs.extend(videojs.EventTarget, {
  53. addSourceBuffer() {
  54. throw new Error('Testing Mock');
  55. }
  56. });
  57. window.MediaSource.isTypeSupported = function(mime) {
  58. return true;
  59. };
  60. QUnit.ok(
  61. new videojs.MediaSource({ mode: 'flash' }) instanceof FlashMediaSource,
  62. 'forced flash'
  63. );
  64. QUnit.ok(
  65. new videojs.MediaSource({ mode: 'html5' }) instanceof HtmlMediaSource,
  66. 'forced html5'
  67. );
  68. // 'auto' should use native mediasources when they're available
  69. QUnit.ok(
  70. new videojs.MediaSource() instanceof HtmlMediaSource,
  71. 'used html5'
  72. );
  73. window.MediaSource.isTypeSupported = function(mime) {
  74. return false;
  75. };
  76. // 'auto' should use flash when native mediasources are not available
  77. QUnit.ok(
  78. new videojs.MediaSource() instanceof FlashMediaSource,
  79. 'used flash'
  80. );
  81. });