summaryrefslogtreecommitdiffstats
path: root/dom/media/webvtt/test/xpcshell/test_parser.js
blob: 5ae70f976219c6a07b359db5e81138886d94f657 (plain)
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
"use strict";

const { WebVTT } = ChromeUtils.importESModule(
  "resource://gre/modules/vtt.sys.mjs"
);

let fakeWindow = {
  /* eslint-disable object-shorthand */
  VTTCue: function () {},
  VTTRegion: function () {},
  /* eslint-enable object-shorthand */
};

// We have a better parser check in WPT. Here I want to check that incomplete
// lines are correctly parsable.
let tests = [
  // Signature
  { input: ["WEBVTT"], cue: 0, region: 0 },
  { input: ["", "WE", "BVT", "T"], cue: 0, region: 0 },
  { input: ["WEBVTT - This file has no cues."], cue: 0, region: 0 },
  { input: ["WEBVTT", " - ", "This file has no cues."], cue: 0, region: 0 },

  // Body with IDs
  {
    input: [
      "WEB",
      "VTT - This file has cues.\n",
      "\n",
      "14\n",
      "00:01:14",
      ".815 --> 00:0",
      "1:18.114\n",
      "- What?\n",
      "- Where are we now?\n",
      "\n",
      "15\n",
      "00:01:18.171 --> 00:01:20.991\n",
      "- T",
      "his is big bat country.\n",
      "\n",
      "16\n",
      "00:01:21.058 --> 00:01:23.868\n",
      "- [ Bat",
      "s Screeching ]\n",
      "- They won't get in your hair. They're after the bug",
      "s.\n",
    ],
    cue: 3,
    region: 0,
  },

  // Body without IDs
  {
    input: [
      "WEBVTT - This file has c",
      "ues.\n",
      "\n",
      "00:01:14.815 --> 00:01:18.114\n",
      "- What?\n",
      "- Where are we now?\n",
      "\n",
      "00:01:18.171 --> 00:01:2",
      "0.991\n",
      "- ",
      "This is big bat country.\n",
      "\n",
      "00:01:21.058 --> 00:01:23.868\n",
      "- [ Bats S",
      "creeching ]\n",
      "- They won't get in your hair. They're after the bugs.\n",
    ],
    cue: 3,
    region: 0,
  },

  // Note
  {
    input: ["WEBVTT - This file has no cues.\n", "\n", "NOTE what"],
    cue: 0,
    region: 0,
  },

  // Regions - This vtt is taken from a WPT
  {
    input: [
      "WE",
      "BVTT\n",
      "\n",
      "REGION\n",
      "id:0\n",
      "\n",
      "REGION\n",
      "id:1\n",
      "region",
      "an",
      "chor:0%,0%\n",
      "\n",
      "R",
      "EGION\n",
      "id:2\n",
      "regionanchor:18446744073709552000%,18446744",
      "073709552000%\n",
      "\n",
      "REGION\n",
      "id:3\n",
      "regionanchor: 100%,100%\n",
      "regio",
      "nanchor :100%,100%\n",
      "regionanchor:100% ,100%\n",
      "regionanchor:100%, 100%\n",
      "regionanchor:100 %,100%\n",
      "regionanchor:10",
      "0%,100 %\n",
      "\n",
      "00:00:00.000 --> 00:00:01.000",
      " region:0\n",
      "text\n",
      "\n",
      "00:00:00.000 --> 00:00:01.000 region:1\n",
      "text\n",
      "\n",
      "00:00:00.000 --> 00:00:01.000 region:3\n",
      "text\n",
    ],
    cue: 3,
    region: 4,
  },
];

function run_test() {
  tests.forEach(test => {
    let parser = new WebVTT.Parser(fakeWindow, null);
    ok(!!parser, "Ok... this is a good starting point");

    let cue = 0;
    parser.oncue = () => {
      ++cue;
    };

    let region = 0;
    parser.onregion = () => {
      ++region;
    };

    parser.onparsingerror = () => {
      ok(false, "No error accepted");
    };

    test.input.forEach(input => {
      parser.parse(new TextEncoder().encode(input));
    });

    parser.flush();

    equal(cue, test.cue, "Cue value matches");
    equal(region, test.region, "Region value matches");
  });
}