11cb0ef41Sopenharmony_ci// These are defined by the test:
21cb0ef41Sopenharmony_ci// errors (boolean)
31cb0ef41Sopenharmony_ci// encoder (function)
41cb0ef41Sopenharmony_ci// ranges (array)
51cb0ef41Sopenharmony_ci// separator (string)
61cb0ef41Sopenharmony_ci// expect (function)
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_civar tests = [];
91cb0ef41Sopenharmony_civar cplist = [];
101cb0ef41Sopenharmony_civar numTests = null;
111cb0ef41Sopenharmony_civar numFrames = 2;
121cb0ef41Sopenharmony_civar chunkSize = 400;
131cb0ef41Sopenharmony_civar numChunks = null;
141cb0ef41Sopenharmony_civar frames = null;
151cb0ef41Sopenharmony_civar frames = null;
161cb0ef41Sopenharmony_civar forms = null;
171cb0ef41Sopenharmony_civar encodedSeparator = encodeURIComponent(separator);
181cb0ef41Sopenharmony_civar currentChunkIndex = 0;
191cb0ef41Sopenharmony_civar pageCharset = document.querySelector("meta[charset]").getAttribute("charset");
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cisetup(function() {
221cb0ef41Sopenharmony_ci    // create a simple list of just those code points for which there is an encoding possible
231cb0ef41Sopenharmony_ci    codepoints = [];
241cb0ef41Sopenharmony_ci    for (var range of ranges) {
251cb0ef41Sopenharmony_ci        for (var i = range[0]; i < range[1]; i++) {
261cb0ef41Sopenharmony_ci            result = encoder(String.fromCodePoint(i));
271cb0ef41Sopenharmony_ci            var success = !!result;
281cb0ef41Sopenharmony_ci            if (errors) {
291cb0ef41Sopenharmony_ci              success = !success;
301cb0ef41Sopenharmony_ci            }
311cb0ef41Sopenharmony_ci            if (success) {
321cb0ef41Sopenharmony_ci                var item = {};
331cb0ef41Sopenharmony_ci                codepoints.push(item);
341cb0ef41Sopenharmony_ci                item.cp = i;
351cb0ef41Sopenharmony_ci                item.expected = expect(result, i);
361cb0ef41Sopenharmony_ci                item.desc = range[2];
371cb0ef41Sopenharmony_ci            }
381cb0ef41Sopenharmony_ci        }
391cb0ef41Sopenharmony_ci    }
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    // convert the information into a simple array of objects that can be easily traversed
421cb0ef41Sopenharmony_ci    var currentChunk = [];
431cb0ef41Sopenharmony_ci    var currentTests = [];
441cb0ef41Sopenharmony_ci    cplist = [currentChunk];
451cb0ef41Sopenharmony_ci    tests = [currentTests];
461cb0ef41Sopenharmony_ci    for (i = 0; i < codepoints.length; i++) {
471cb0ef41Sopenharmony_ci        if (currentChunk.length == chunkSize) {
481cb0ef41Sopenharmony_ci            currentChunk = [];
491cb0ef41Sopenharmony_ci            cplist.push(currentChunk);
501cb0ef41Sopenharmony_ci            currentTests = [];
511cb0ef41Sopenharmony_ci            tests.push(currentTests);
521cb0ef41Sopenharmony_ci        }
531cb0ef41Sopenharmony_ci        var item = {};
541cb0ef41Sopenharmony_ci        currentChunk.push(item);
551cb0ef41Sopenharmony_ci        item.cp = codepoints[i].cp;
561cb0ef41Sopenharmony_ci        item.expected = codepoints[i].expected;
571cb0ef41Sopenharmony_ci        item.desc = codepoints[i].desc;
581cb0ef41Sopenharmony_ci        currentTests.push(subsetTest(async_test,
591cb0ef41Sopenharmony_ci                                     (item.desc ? item.desc + " " : "") +
601cb0ef41Sopenharmony_ci                                     "U+" +
611cb0ef41Sopenharmony_ci                                     item.cp.toString(16).toUpperCase() +
621cb0ef41Sopenharmony_ci                                     " " +
631cb0ef41Sopenharmony_ci                                     String.fromCodePoint(item.cp) +
641cb0ef41Sopenharmony_ci                                     " " +
651cb0ef41Sopenharmony_ci                                     item.expected
661cb0ef41Sopenharmony_ci        ));
671cb0ef41Sopenharmony_ci    }
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    numChunks = cplist.length;
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci    for (var i = 0; i < numFrames; i++) {
721cb0ef41Sopenharmony_ci        var frame = document.createElement("iframe");
731cb0ef41Sopenharmony_ci        frame.id = frame.name = "frame-" + i;
741cb0ef41Sopenharmony_ci        document.body.appendChild(frame);
751cb0ef41Sopenharmony_ci        var form = document.createElement("form");
761cb0ef41Sopenharmony_ci        form.id = "form-" + i;
771cb0ef41Sopenharmony_ci        form.method = "GET";
781cb0ef41Sopenharmony_ci        form.action = "/common/blank.html";
791cb0ef41Sopenharmony_ci        form.acceptCharset = pageCharset;
801cb0ef41Sopenharmony_ci        form.target = frame.id;
811cb0ef41Sopenharmony_ci        var input = document.createElement("input");
821cb0ef41Sopenharmony_ci        input.id = input.name = "input-" + i;
831cb0ef41Sopenharmony_ci        form.appendChild(input);
841cb0ef41Sopenharmony_ci        document.body.appendChild(form);
851cb0ef41Sopenharmony_ci    }
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci    addEventListener("load", function() {
881cb0ef41Sopenharmony_ci        frames = Array.prototype.slice.call(
891cb0ef41Sopenharmony_ci            document.getElementsByTagName("iframe")
901cb0ef41Sopenharmony_ci        );
911cb0ef41Sopenharmony_ci        forms = Array.prototype.slice.call(
921cb0ef41Sopenharmony_ci            document.getElementsByTagName("form")
931cb0ef41Sopenharmony_ci        );
941cb0ef41Sopenharmony_ci        inputs = Array.prototype.slice.call(
951cb0ef41Sopenharmony_ci            document.getElementsByTagName("input")
961cb0ef41Sopenharmony_ci        );
971cb0ef41Sopenharmony_ci        for (var i = 0; i < Math.min(numFrames, numChunks); i++) {
981cb0ef41Sopenharmony_ci            runNext(i);
991cb0ef41Sopenharmony_ci        }
1001cb0ef41Sopenharmony_ci    });
1011cb0ef41Sopenharmony_ci});
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_cifunction runNext(id) {
1041cb0ef41Sopenharmony_ci    var i = currentChunkIndex;
1051cb0ef41Sopenharmony_ci    currentChunkIndex += 1;
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci    var iframe = frames[id];
1081cb0ef41Sopenharmony_ci    var form = forms[id];
1091cb0ef41Sopenharmony_ci    var input = inputs[id];
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci    input.value = cplist[i]
1121cb0ef41Sopenharmony_ci        .map(function(x) {
1131cb0ef41Sopenharmony_ci            return String.fromCodePoint(x.cp);
1141cb0ef41Sopenharmony_ci        })
1151cb0ef41Sopenharmony_ci        .join(separator);
1161cb0ef41Sopenharmony_ci    form.submit();
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci    iframe.onload = function() {
1191cb0ef41Sopenharmony_ci        var url = iframe.contentWindow.location;
1201cb0ef41Sopenharmony_ci        var query = url.search;
1211cb0ef41Sopenharmony_ci        var result_string = query.substr(query.indexOf("=") + 1);
1221cb0ef41Sopenharmony_ci        var results = result_string.split(encodedSeparator);
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci        for (var j = 0; j < cplist[i].length; j++) {
1251cb0ef41Sopenharmony_ci            var t = tests[i][j];
1261cb0ef41Sopenharmony_ci            if (t) {
1271cb0ef41Sopenharmony_ci                t.step(function() {
1281cb0ef41Sopenharmony_ci                    assert_equals(
1291cb0ef41Sopenharmony_ci                        normalizeStr(results[j]),
1301cb0ef41Sopenharmony_ci                        normalizeStr(cplist[i][j].expected)
1311cb0ef41Sopenharmony_ci                    );
1321cb0ef41Sopenharmony_ci                });
1331cb0ef41Sopenharmony_ci                t.done();
1341cb0ef41Sopenharmony_ci            }
1351cb0ef41Sopenharmony_ci        }
1361cb0ef41Sopenharmony_ci        if (currentChunkIndex < numChunks) {
1371cb0ef41Sopenharmony_ci            runNext(id);
1381cb0ef41Sopenharmony_ci        }
1391cb0ef41Sopenharmony_ci    };
1401cb0ef41Sopenharmony_ci}
141