rendition-mixin.test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* eslint-disable max-len */
  2. import QUnit from 'qunit';
  3. import RenditionMixin from '../src/rendition-mixin.js';
  4. import videojs from 'video.js';
  5. const makeMockPlaylist = function(options) {
  6. options = options || {};
  7. let playlist = {
  8. segments: [],
  9. attributes: {}
  10. };
  11. playlist.attributes.BANDWIDTH = options.bandwidth;
  12. if ('width' in options) {
  13. playlist.attributes.RESOLUTION = playlist.attributes.RESOLUTION || {};
  14. playlist.attributes.RESOLUTION.width = options.width;
  15. }
  16. if ('height' in options) {
  17. playlist.attributes.RESOLUTION = playlist.attributes.RESOLUTION || {};
  18. playlist.attributes.RESOLUTION.height = options.height;
  19. }
  20. if ('excludeUntil' in options) {
  21. playlist.excludeUntil = options.excludeUntil;
  22. }
  23. if ('uri' in options) {
  24. playlist.uri = options.uri;
  25. }
  26. if ('disabled' in options) {
  27. playlist.disabled = options.disabled;
  28. }
  29. return playlist;
  30. };
  31. const makeMockHlsHandler = function(playlistOptions) {
  32. let mcp = {
  33. fastQualityChange_: () => {
  34. mcp.fastQualityChange_.calls++;
  35. }
  36. };
  37. mcp.fastQualityChange_.calls = 0;
  38. let hlsHandler = {
  39. masterPlaylistController_: mcp
  40. };
  41. hlsHandler.playlists = new videojs.EventTarget();
  42. hlsHandler.playlists.master = { playlists: [] };
  43. playlistOptions.forEach((playlist, i) => {
  44. hlsHandler.playlists.master.playlists[i] = makeMockPlaylist(playlist);
  45. if (playlist.uri) {
  46. hlsHandler.playlists.master.playlists[playlist.uri] =
  47. hlsHandler.playlists.master.playlists[i];
  48. }
  49. });
  50. return hlsHandler;
  51. };
  52. QUnit.module('Rendition Selector API Mixin');
  53. QUnit.test('adds the representations API to HlsHandler', function(assert) {
  54. let hlsHandler = makeMockHlsHandler([
  55. {}
  56. ]);
  57. RenditionMixin(hlsHandler);
  58. assert.equal(typeof hlsHandler.representations, 'function',
  59. 'added the representations API');
  60. });
  61. QUnit.test('returns proper number of representations', function(assert) {
  62. let hlsHandler = makeMockHlsHandler([
  63. {}, {}, {}
  64. ]);
  65. RenditionMixin(hlsHandler);
  66. let renditions = hlsHandler.representations();
  67. assert.equal(renditions.length, 3, 'number of renditions is 3');
  68. });
  69. QUnit.test('returns representations in playlist order', function(assert) {
  70. let hlsHandler = makeMockHlsHandler([
  71. {
  72. bandwidth: 10
  73. },
  74. {
  75. bandwidth: 20
  76. },
  77. {
  78. bandwidth: 30
  79. }
  80. ]);
  81. RenditionMixin(hlsHandler);
  82. let renditions = hlsHandler.representations();
  83. assert.equal(renditions[0].bandwidth, 10, 'rendition has bandwidth 10');
  84. assert.equal(renditions[1].bandwidth, 20, 'rendition has bandwidth 20');
  85. assert.equal(renditions[2].bandwidth, 30, 'rendition has bandwidth 30');
  86. });
  87. QUnit.test('returns representations with width and height if present', function(assert) {
  88. let hlsHandler = makeMockHlsHandler([
  89. {
  90. bandwidth: 10,
  91. width: 100,
  92. height: 200
  93. },
  94. {
  95. bandwidth: 20,
  96. width: 500,
  97. height: 600
  98. },
  99. {
  100. bandwidth: 30
  101. }
  102. ]);
  103. RenditionMixin(hlsHandler);
  104. let renditions = hlsHandler.representations();
  105. assert.equal(renditions[0].width, 100, 'rendition has a width of 100');
  106. assert.equal(renditions[0].height, 200, 'rendition has a height of 200');
  107. assert.equal(renditions[1].width, 500, 'rendition has a width of 500');
  108. assert.equal(renditions[1].height, 600, 'rendition has a height of 600');
  109. assert.equal(renditions[2].width, undefined, 'rendition has a width of undefined');
  110. assert.equal(renditions[2].height, undefined, 'rendition has a height of undefined');
  111. });
  112. QUnit.test('incompatible playlists are not included in the representations list',
  113. function(assert) {
  114. let hlsHandler = makeMockHlsHandler([
  115. {
  116. bandwidth: 0,
  117. excludeUntil: Infinity,
  118. uri: 'media0.m3u8'
  119. },
  120. {
  121. bandwidth: 0,
  122. excludeUntil: 0,
  123. uri: 'media1.m3u8'
  124. },
  125. {
  126. bandwidth: 0,
  127. excludeUntil: Date.now() + 999999,
  128. uri: 'media2.m3u8'
  129. },
  130. {
  131. bandwidth: 0,
  132. excludeUntil: 1,
  133. uri: 'media3.m3u8'
  134. },
  135. {
  136. bandwidth: 0,
  137. uri: 'media4.m3u8'
  138. }
  139. ]);
  140. RenditionMixin(hlsHandler);
  141. let renditions = hlsHandler.representations();
  142. assert.equal(renditions.length, 4, 'incompatible rendition not added');
  143. assert.equal(renditions[0].id, 'media1.m3u8', 'rendition is enabled');
  144. assert.equal(renditions[1].id, 'media2.m3u8', 'rendition is enabled');
  145. assert.equal(renditions[2].id, 'media3.m3u8', 'rendition is enabled');
  146. assert.equal(renditions[3].id, 'media4.m3u8', 'rendition is enabled');
  147. });
  148. QUnit.test('setting a representation to disabled sets disabled to true',
  149. function(assert) {
  150. let renditiondisabled = 0;
  151. let hlsHandler = makeMockHlsHandler([
  152. {
  153. bandwidth: 0,
  154. excludeUntil: 0,
  155. uri: 'media0.m3u8'
  156. },
  157. {
  158. bandwidth: 0,
  159. excludeUntil: 0,
  160. uri: 'media1.m3u8'
  161. }
  162. ]);
  163. let playlists = hlsHandler.playlists.master.playlists;
  164. hlsHandler.playlists.on('renditiondisabled', function() {
  165. renditiondisabled++;
  166. });
  167. RenditionMixin(hlsHandler);
  168. let renditions = hlsHandler.representations();
  169. assert.equal(renditiondisabled, 0, 'renditiondisabled event has not been triggered');
  170. renditions[0].enabled(false);
  171. assert.equal(renditiondisabled, 1, 'renditiondisabled event has been triggered');
  172. assert.equal(playlists[0].disabled, true, 'rendition has been disabled');
  173. assert.equal(playlists[1].disabled, undefined, 'rendition has not been disabled');
  174. assert.equal(playlists[0].excludeUntil, 0,
  175. 'excludeUntil not touched when disabling a rendition');
  176. assert.equal(playlists[1].excludeUntil, 0,
  177. 'excludeUntil not touched when disabling a rendition');
  178. });
  179. QUnit.test('changing the enabled state of a representation calls fastQualityChange_',
  180. function(assert) {
  181. let renditionEnabledEvents = 0;
  182. let hlsHandler = makeMockHlsHandler([
  183. {
  184. bandwidth: 0,
  185. disabled: true,
  186. uri: 'media0.m3u8'
  187. },
  188. {
  189. bandwidth: 0,
  190. uri: 'media1.m3u8'
  191. }
  192. ]);
  193. let mpc = hlsHandler.masterPlaylistController_;
  194. hlsHandler.playlists.on('renditionenabled', function() {
  195. renditionEnabledEvents++;
  196. });
  197. RenditionMixin(hlsHandler);
  198. let renditions = hlsHandler.representations();
  199. assert.equal(mpc.fastQualityChange_.calls, 0, 'fastQualityChange_ was never called');
  200. assert.equal(renditionEnabledEvents, 0,
  201. 'renditionenabled event has not been triggered');
  202. renditions[0].enabled(true);
  203. assert.equal(mpc.fastQualityChange_.calls, 1, 'fastQualityChange_ was called once');
  204. assert.equal(renditionEnabledEvents, 1,
  205. 'renditionenabled event has been triggered once');
  206. renditions[1].enabled(false);
  207. assert.equal(mpc.fastQualityChange_.calls, 2, 'fastQualityChange_ was called twice');
  208. });