create-url.test.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.module('createObjectURL', {
  14. beforeEach() {
  15. this.fixture = document.getElementById('qunit-fixture');
  16. this.video = document.createElement('video');
  17. this.fixture.appendChild(this.video);
  18. this.player = videojs(this.video);
  19. // Mock the environment's timers because certain things - particularly
  20. // player readiness - are asynchronous in video.js 5.
  21. this.clock = sinon.useFakeTimers();
  22. this.oldMediaSource = window.MediaSource || window.WebKitMediaSource;
  23. // force MediaSource support
  24. if (!window.MediaSource) {
  25. window.MediaSource = function() {
  26. let result = new window.Blob();
  27. result.addEventListener = function() {};
  28. result.addSourceBuffer = function() {};
  29. return result;
  30. };
  31. }
  32. },
  33. afterEach() {
  34. // The clock _must_ be restored before disposing the player; otherwise,
  35. // certain timeout listeners that happen inside video.js may throw errors.
  36. this.clock.restore();
  37. this.player.dispose();
  38. window.MediaSource = window.WebKitMediaSource = this.oldMediaSource;
  39. }
  40. });
  41. QUnit.test('delegates to the native implementation', function() {
  42. QUnit.ok(!(/blob:vjs-media-source\//).test(
  43. videojs.URL.createObjectURL(
  44. new window.Blob())
  45. ),
  46. 'created a native blob URL'
  47. );
  48. });
  49. QUnit.test('uses the native MediaSource when available', function() {
  50. QUnit.ok(!(/blob:vjs-media-source\//).test(
  51. videojs.URL.createObjectURL(
  52. new HtmlMediaSource())
  53. ),
  54. 'created a native blob URL'
  55. );
  56. });
  57. QUnit.test('emulates a URL for the shim', function() {
  58. QUnit.ok((/blob:vjs-media-source\//).test(
  59. videojs.URL.createObjectURL(
  60. new FlashMediaSource())
  61. ),
  62. 'created an emulated blob URL'
  63. );
  64. });
  65. QUnit.test('stores the associated blob URL on the media source', function() {
  66. let blob = new window.Blob();
  67. let url = videojs.URL.createObjectURL(blob);
  68. QUnit.equal(blob.url_, url, 'captured the generated URL');
  69. });