forked from sandflow/imscJS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
unit-tests.js
170 lines (144 loc) · 6.14 KB
/
unit-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (c) 2016, Pierre-Anthony Lemieux <pal@sandflow.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
var errorHandler = {
info: function (msg) {
throw msg;
},
warn: function (msg) {
throw msg;
},
error: function (msg) {
throw msg;
},
fatal: function (msg) {
throw msg;
}
};
function getIMSC1Document(url, metadataHandler) {
return new asyncLoadFile(url).then(function (contents) {
return imsc.fromXML(contents, errorHandler, metadataHandler);
});
}
QUnit.test(
"Parse Time Expressions",
function (assert) {
return getIMSC1Document("unit-tests/timeExpressions.ttml").then(
function (doc) {
assert.close(doc.body.contents[0].contents[0].begin, 1.2, 1e-10);
assert.close(doc.body.contents[0].contents[1].begin, 72, 1e-10);
assert.close(doc.body.contents[0].contents[2].begin, 4320, 1e-10);
assert.close(doc.body.contents[0].contents[3].begin, 24 / 24000 * 1001, 1e-10);
assert.close(doc.body.contents[0].contents[4].begin, 2, 1e-10);
assert.close(doc.body.contents[0].contents[5].begin, 3723, 1e-10);
assert.close(doc.body.contents[0].contents[6].begin, 3723.235, 1e-10);
assert.close(doc.body.contents[0].contents[7].begin, 3723.235, 1e-10);
assert.close(doc.body.contents[0].contents[8].begin, 3600 + 2 * 60 + 3 + 20 / 24000 * 1001, 1e-10);
assert.close(doc.body.contents[0].contents[9].begin, 360000.1, 1e-10);
assert.close(doc.body.contents[0].contents[10].begin, 360000 + 100 / 24000 * 1001, 1e-10);
}
);
}
);
QUnit.test(
"Parse Length Expressions",
function (assert) {
return getIMSC1Document("unit-tests/lengthExpressions.ttml").then(
function (doc) {
assert.deepEqual(
doc.body.contents[0].contents[0].styleAttrs["http://www.w3.org/ns/ttml#styling fontSize"],
{"unit": "%", "value": 10.5}
);
assert.deepEqual(
doc.body.contents[0].contents[1].styleAttrs["http://www.w3.org/ns/ttml#styling fontSize"],
{"unit": "em", "value": 0.105}
);
assert.deepEqual(
doc.body.contents[0].contents[2].styleAttrs["http://www.w3.org/ns/ttml#styling fontSize"],
{"unit": "px", "value": 10.5}
);
assert.deepEqual(
doc.body.contents[0].contents[3].styleAttrs["http://www.w3.org/ns/ttml#styling fontSize"],
{"unit": "c", "value": 0.105}
);
}
);
}
);
QUnit.test(
"Metadata Callbacks",
function (assert) {
var count = 0;
var cur_tag = 0;
var accumul_txt = "";
mh = {
onOpenTag: function (ns, name, attrs) {
switch (count) {
case 0:
assert.equal(name, "title");
assert.equal(ns, "http://www.w3.org/ns/ttml#metadata");
cur_tag = 1;
break;
case 3:
assert.equal(name, "conformsToStandard");
assert.equal(ns, "urn:ebu:metadata");
cur_tag = 2;
break;
case 4:
assert.equal(name, "image");
assert.equal(ns, "http://www.smpte-ra.org/schemas/2052-1/2010/smpte-tt");
assert.equal(attrs["http://www.w3.org/XML/1998/namespace id"].value, "img_1");
assert.equal(attrs[" imagetype"].value, "PNG");
cur_tag = 3;
break;
}
count++;
},
onCloseTag: function () {
switch (cur_tag) {
case 4:
var trimmed_text = accumul_txt.trim();
assert.ok(trimmed_text.startswith("iVBORw0KGgoAAAANS"));
assert.equal(trimmed_text.length, 4146);
}
cur_tag = 0;
accumul_txt = "";
},
onText: function (contents) {
switch (cur_tag) {
case 1:
assert.equal(contents, "Metadata Handler Test");
break;
case 2:
assert.equal(contents, "http://www.w3.org/ns/ttml/profile/imsc1/text");
break;
case 4:
accumul_txt = accumul_txt + contents;
}
}
};
return getIMSC1Document("unit-tests/metadataHandler.ttml", mh);
}
);