test_parser.js (3371B)
1 "use strict"; 2 3 const { WebVTT } = ChromeUtils.importESModule( 4 "resource://gre/modules/vtt.sys.mjs" 5 ); 6 7 let fakeWindow = { 8 /* eslint-disable object-shorthand */ 9 VTTCue: function () {}, 10 VTTRegion: function () {}, 11 /* eslint-enable object-shorthand */ 12 }; 13 14 // We have a better parser check in WPT. Here I want to check that incomplete 15 // lines are correctly parsable. 16 let tests = [ 17 // Signature 18 { input: ["WEBVTT"], cue: 0, region: 0 }, 19 { input: ["", "WE", "BVT", "T"], cue: 0, region: 0 }, 20 { input: ["WEBVTT - This file has no cues."], cue: 0, region: 0 }, 21 { input: ["WEBVTT", " - ", "This file has no cues."], cue: 0, region: 0 }, 22 23 // Body with IDs 24 { 25 input: [ 26 "WEB", 27 "VTT - This file has cues.\n", 28 "\n", 29 "14\n", 30 "00:01:14", 31 ".815 --> 00:0", 32 "1:18.114\n", 33 "- What?\n", 34 "- Where are we now?\n", 35 "\n", 36 "15\n", 37 "00:01:18.171 --> 00:01:20.991\n", 38 "- T", 39 "his is big bat country.\n", 40 "\n", 41 "16\n", 42 "00:01:21.058 --> 00:01:23.868\n", 43 "- [ Bat", 44 "s Screeching ]\n", 45 "- They won't get in your hair. They're after the bug", 46 "s.\n", 47 ], 48 cue: 3, 49 region: 0, 50 }, 51 52 // Body without IDs 53 { 54 input: [ 55 "WEBVTT - This file has c", 56 "ues.\n", 57 "\n", 58 "00:01:14.815 --> 00:01:18.114\n", 59 "- What?\n", 60 "- Where are we now?\n", 61 "\n", 62 "00:01:18.171 --> 00:01:2", 63 "0.991\n", 64 "- ", 65 "This is big bat country.\n", 66 "\n", 67 "00:01:21.058 --> 00:01:23.868\n", 68 "- [ Bats S", 69 "creeching ]\n", 70 "- They won't get in your hair. They're after the bugs.\n", 71 ], 72 cue: 3, 73 region: 0, 74 }, 75 76 // Note 77 { 78 input: ["WEBVTT - This file has no cues.\n", "\n", "NOTE what"], 79 cue: 0, 80 region: 0, 81 }, 82 83 // Regions - This vtt is taken from a WPT 84 { 85 input: [ 86 "WE", 87 "BVTT\n", 88 "\n", 89 "REGION\n", 90 "id:0\n", 91 "\n", 92 "REGION\n", 93 "id:1\n", 94 "region", 95 "an", 96 "chor:0%,0%\n", 97 "\n", 98 "R", 99 "EGION\n", 100 "id:2\n", 101 "regionanchor:18446744073709552000%,18446744", 102 "073709552000%\n", 103 "\n", 104 "REGION\n", 105 "id:3\n", 106 "regionanchor: 100%,100%\n", 107 "regio", 108 "nanchor :100%,100%\n", 109 "regionanchor:100% ,100%\n", 110 "regionanchor:100%, 100%\n", 111 "regionanchor:100 %,100%\n", 112 "regionanchor:10", 113 "0%,100 %\n", 114 "\n", 115 "00:00:00.000 --> 00:00:01.000", 116 " region:0\n", 117 "text\n", 118 "\n", 119 "00:00:00.000 --> 00:00:01.000 region:1\n", 120 "text\n", 121 "\n", 122 "00:00:00.000 --> 00:00:01.000 region:3\n", 123 "text\n", 124 ], 125 cue: 3, 126 region: 4, 127 }, 128 ]; 129 130 function run_test() { 131 tests.forEach(test => { 132 let parser = new WebVTT.Parser(fakeWindow, null); 133 ok(!!parser, "Ok... this is a good starting point"); 134 135 let cue = 0; 136 parser.oncue = () => { 137 ++cue; 138 }; 139 140 let region = 0; 141 parser.onregion = () => { 142 ++region; 143 }; 144 145 parser.onparsingerror = () => { 146 ok(false, "No error accepted"); 147 }; 148 149 test.input.forEach(input => { 150 parser.parse(new TextEncoder().encode(input)); 151 }); 152 153 parser.flush(); 154 155 equal(cue, test.cue, "Cue value matches"); 156 equal(region, test.region, "Region value matches"); 157 }); 158 }