Validate the BCP47-ness of the language string passed to TrackBase::setLanguage()
[WebKit-https.git] / LayoutTests / media / media-source / only-bcp47-language-tags-accepted-as-valid.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <title>Test that only BCP47 language tags are accepted as valid but still reflected</title>
5         <!--
6         <script src="media-controls.js"></script>
7         <script src="media-file.js"></script>
8         -->
9         <script src="media-source-loader.js"></script>
10         <script src="../video-test.js"></script>
11         <script>
12             var loader;
13             var source;
14             var sourceBuffer;
15
16             function mediaDataLoadingFailed() {
17                 failTest('Media data loading failed');
18             }
19
20             function mediaDataLoaded() {
21                 source = new MediaSource();
22                 waitForEvent('sourceopen', sourceOpen, false, false, source);
23                 waitForEventAndFail('error');
24                 run('video.src = URL.createObjectURL(source)');
25             }
26
27             function sourceOpen() {
28                 run('source.duration = loader.duration()');
29                 run('sourceBuffer = source.addSourceBuffer(loader.type())');
30                 waitForEventOn(sourceBuffer, 'update', sourceInitialized, false, true);
31                 run('sourceBuffer.appendBuffer(loader.initSegment())');
32             }
33
34             function sourceInitialized() {
35                 waitForEventOn(sourceBuffer, 'update', startBCP74Test, false, true);
36                 consoleWrite('Append a partial media segment.')
37                 run('sourceBuffer.appendBuffer(loader.mediaSegment(0).slice(0, loader.mediaSegment(0).byteLength / 2))');
38             }
39
40             if (window.testRunner)
41                 testRunner.dumpAsText();
42
43             var validLanguageTags = [
44                 // Grandfathered, irregular, https://tools.ietf.org/html/bcp47#section-2.1
45                 "en-GB-oed", "i-ami", "i-bnn", "i-default",
46                 "i-enochian", "i-hak", "i-klingon", "i-lux",
47                 "i-mingo", "i-navajo", "i-pwn", "i-tao",
48                 "i-tay", "i-tsu", "sgn-BE-FR", "sgn-BE-NL",
49                 "sgn-CH-DE",
50                 // Grandfathered, regular, https://tools.ietf.org/html/bcp47#section-2.1
51                 "art-lojban", "cel-gaulish", "no-bok", "no-nyn",
52                 "zh-guoyu", "zh-hakka", "zh-min", "zh-min-nan",
53                 "zh-xiang",
54                 // Other examples from the spec, mainly https://tools.ietf.org/html/bcp47#appendix-A
55                 "de", "fr", "ja", "zh-Hant",
56                 "zh-Han", "sr-Cyrl", "sr-Latn", "zh-cmn-Hans-CN",
57                 "cmn-Hans-CN", "zh-yue-HK", "yue-HK", "zh-Hans-CN",
58                 "sr-Latn-RS", "sl-rozaj", "sl-rozaj-biske", "sl-nedis",
59                 "de-CH-1901", "sl-IT-nedis", "hy-Latn-IT-arevela", "en-US",
60                 "es-419", "de-CH-x-phonebk", "az-Arab-x-AZE-derbend", "x-whatever",
61                 "qaa-Qaaa-QM-x-southern", "de-Qaaa", "sr-Latn-QM", "sr-Qaaa-RS",
62                 "zh-Hant-CN-x-private1-private2", "de-DE", "de-de", "de-Latn-DE",
63                 "de-Latf-DE", "de-DE-x-goethe", "de-Latn-DE-1996", "de-Deva-DE",
64                 "en-US-u-islamcal", "zh-CN-a-myext-x-private", "en-a-myext-b-another",
65                 "zh-Latn-CN-variant1-a-extend1-x-wadegile-private1"
66             ];
67
68             var invalidLanguageTags = [
69                 // Invalid length, less than 2 or over 100
70                 "a", "1", "ab-abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij",
71                 // Invalid first characters
72                 "1a", "-a",
73                 // Invalid second characters
74                 "a-", "a1",
75                 // Invalid third characters
76                 "aa1",
77                 // Invalid fourth characters
78                 "aaaa", "aaa1",
79                 // Invalid characters beyond the fourth
80                 "inv-alid-char space", "inv-alid-char–longDash", "inv-alid-char-Pålska",
81                 "inv-alid-char-*", "inv-alid-char-"
82             ];
83
84             var videoTrack;
85             var audioTrack;
86             var textTrack;
87             function startBCP74Test () {
88                 videoTrack = video.videoTracks[0];
89                 audioTrack = video.audioTracks[0];
90                 // Access text track language through the element's srclang attribute
91                 // since the DOM property is read-only.
92                 textTrack = document.getElementById("textTrack");
93                 for (var i = 0; i < validLanguageTags.length; i++) {
94                     videoTrack.language = validLanguageTags[i];
95                     testExpected("videoTrack.language", validLanguageTags[i]);
96                     textTrack.setAttribute("srclang", validLanguageTags[i]);
97                     testExpected("textTrack.track.language", validLanguageTags[i]);
98                     audioTrack.language = validLanguageTags[i];
99                     testExpected("audioTrack.language", validLanguageTags[i]);
100                 }
101                 var notOverwritten = "not-overwritten";
102                 for (var i = 0; i < invalidLanguageTags.length; i++) {
103                     // These are accepted in the web page layer but will produce console warnings
104                     videoTrack.language = invalidLanguageTags[i];
105                     testExpected("videoTrack.language", invalidLanguageTags[i]);
106                     textTrack.setAttribute("srclang", invalidLanguageTags[i]);
107                     testExpected("textTrack.track.language", invalidLanguageTags[i]);
108                     audioTrack.language = invalidLanguageTags[i];
109                     testExpected("audioTrack.language", invalidLanguageTags[i]);
110                 }
111                 endTest();
112             }
113
114             function runTest()
115             {
116                 findMediaElement();
117
118                 loader = new MediaSourceLoader('content/test-fragmented-manifest.json');
119                 loader.onload = mediaDataLoaded;
120                 loader.onerror = mediaDataLoadingFailed;
121             }
122         </script>
123     </head>
124
125     <body onload="runTest()">
126         <p>Test that only BCP47 language tags are accepted as valid but still reflected.</p>
127         <video controls>
128             <track id="textTrack" kind="captions" src="../track/captions-webvtt/captions-fast.vtt" />
129         </video>
130     </body>
131 </html>