UNPKG

4.87 kBJavaScriptView Raw
1import QUnit from 'qunit';
2import { createRepresentation, createRepresentations } from '../src/representations.js';
3import { makeMochTech } from './util/util.js';
4
5QUnit.module('Representations API');
6
7QUnit.test('creates a representation with a working enabled function', function(assert) {
8 let enabledChanges = 0;
9 const onEnabledChange = () => enabledChanges++;
10 const level = {
11 index: 0,
12 width: 1280,
13 height: 720,
14 bitrate: 12140000
15 };
16
17 const representation = createRepresentation(onEnabledChange, level);
18
19 assert.strictEqual(representation.id, '0', 'correct id');
20 assert.equal(representation.width, 1280, 'correct width');
21 assert.equal(representation.height, 720, 'correct height');
22 assert.equal(representation.bandwidth, 12140000, 'correct bandwidth');
23 assert.equal(typeof representation.enabled, 'function', 'has enabled function');
24
25 let isEnabled = representation.enabled();
26
27 assert.ok(isEnabled, 'new representations start enabled');
28 assert.equal(enabledChanges, 0, 'getting enabled does not call enabled callback');
29
30 // set enabled to same value: true -> true
31 representation.enabled(true);
32 isEnabled = representation.enabled();
33
34 assert.ok(isEnabled, 'setting enabled to same value has no change');
35 assert.equal(enabledChanges, 0,
36 'setting enabled to the same value does not call enabled callback');
37
38 // set enabled to different value: true -> false
39 representation.enabled(false);
40 isEnabled = representation.enabled();
41
42 assert.notOk(isEnabled, 'setting enabled to different value updates enabled');
43 assert.equal(enabledChanges, 1,
44 'setting enabled to different value calls enabled callback');
45
46 // set enabled to non boolean
47 representation.enabled('true');
48 isEnabled = representation.enabled();
49
50 assert.notOk(isEnabled, 'setting enabled to non boolean does not update enabled');
51 assert.equal(enabledChanges, 1,
52 'setting enabled to non boolean does not call enabled callback');
53});
54
55QUnit.test('createRepresentations creates a list of representation objects',
56function(assert) {
57 const levels = [
58 {
59 index: 0,
60 width: 640,
61 height: 360,
62 bitrate: 865000
63 },
64 {
65 index: 1,
66 width: 1280,
67 height: 720,
68 bitrate: 12140000
69 },
70 {
71 index: 2,
72 width: void 0,
73 height: void 0,
74 bitrate: 65000,
75 audio: true
76 },
77 {
78 index: 3,
79 width: 1920,
80 height: 1080,
81 bitrate: 16120000
82 }
83 ];
84 const tech = makeMochTech({ levels: () => levels }, {});
85
86 const representationsApi = createRepresentations(tech);
87
88 assert.equal(typeof representationsApi, 'function',
89 'createRepresentations returns a function for getting the list of representations');
90
91 const representations = representationsApi();
92
93 assert.equal(representations.length, 3, 'created a list of representations');
94 assert.equal(representations[0].id, '0', 'created representation for video');
95 assert.equal(representations[1].id, '1', 'created representation for video');
96 assert.equal(representations[2].id, '3', 'created representation for video');
97});
98
99QUnit.test('representations sets levels on tech correctly when enabling/disabling',
100function(assert) {
101 const levels = [
102 {
103 index: 0,
104 width: 640,
105 height: 360,
106 bitrate: 865000
107 },
108 {
109 index: 1,
110 width: 1280,
111 height: 720,
112 bitrate: 12140000
113 },
114 {
115 index: 2,
116 width: 1920,
117 height: 1080,
118 bitrate: 16120000
119 }
120 ];
121 let currentLevel = -1;
122 let autoLevelCapping = -1;
123 const tech = makeMochTech({ levels: () => levels }, {
124 level: (val) => currentLevel = val,
125 autoLevelCapping: (val) => autoLevelCapping = val
126 });
127 const representations = createRepresentations(tech)();
128
129 assert.deepEqual(representations.map(rep => rep.enabled()), [true, true, true],
130 'all representations enabled on creation');
131 assert.equal(currentLevel, -1, 'auto level mode');
132 assert.equal(autoLevelCapping, -1, 'no autoLevelCapping');
133
134 representations[2].enabled(false);
135 assert.equal(currentLevel, -1,
136 'auto level mode when more than one representation is enabled');
137 assert.equal(autoLevelCapping, 1, 'autoLevelCapping set to highest enabled bitrate');
138
139 representations[2].enabled(true);
140 assert.equal(currentLevel, -1, 'auto level mode when all enabled');
141 assert.equal(autoLevelCapping, -1, 'no autoLevelCapping when all enabled');
142
143 representations[2].enabled(false);
144 representations[0].enabled(false);
145 assert.equal(currentLevel, 1, 'manual level mode when only one enabled representation');
146 assert.equal(autoLevelCapping, -1, 'no autoLevelCapping in manual level mode');
147
148 representations[1].enabled(false);
149 assert.equal(currentLevel, -1, 'auto level mode when all disabled');
150 assert.equal(autoLevelCapping, -1, 'no autoLevelCapping when all disabled');
151});