summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/import-maps/data-driven/resources/test-helper.js
blob: d34869b01348ef873fff39ebbe69aad8c42e99b5 (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
setup({allow_uncaught_exception : true});

// Creates a new Document (via <iframe>) and add an inline import map.
function createTestIframe(importMap, importMapBaseURL) {
  return new Promise(resolve => {
    const iframe = document.createElement('iframe');

    window.addEventListener('message', event => {
        // Parsing result is saved here and checked later, rather than
        // rejecting the promise on errors.
        iframe.parseImportMapResult = event.data.type;
        resolve(iframe);
      },
      {once: true});

    const testHTML = createTestHTML(importMap, importMapBaseURL);

    if (new URL(importMapBaseURL).protocol === 'data:') {
      iframe.src = 'data:text/html;base64,' + btoa(testHTML);
    } else {
      iframe.src = '/common/blank.html';
      iframe.addEventListener('load', () => {
        iframe.contentDocument.write(testHTML);
        iframe.contentDocument.close();
      }, {once: true});
    }
    document.body.appendChild(iframe);
  });
}

function createTestHTML(importMap, importMapBaseURL) {
  return `
    <!DOCTYPE html>
    <script src="${location.origin}/import-maps/data-driven/resources/test-helper-iframe.js"></script>

    <base href="${importMapBaseURL}">
    <script type="importmap" onerror="onScriptError(event)">
    ${JSON.stringify(importMap)}
    </script>

    <script type="module">
      if (!window.registrationResult) {
        window.registrationResult = {type: 'Success'};
      }
      window.removeEventListener('error', window.windowErrorHandler);
      parent.postMessage(window.registrationResult, '*');
    </script>
  `;
}

// Returns a promise that is resolved with the resulting URL, or rejected if
// the resolution fails.
function resolve(specifier, baseURL, iframe) {
  return new Promise((resolve, reject) => {
    window.addEventListener('message', event => {
        if (event.data.type === 'ResolutionSuccess') {
          resolve(event.data.result);
        } else if (event.data.type === 'Failure') {
          if (event.data.result === 'TypeError') {
            reject(new TypeError(event.data.message));
          } else {
            reject(new Error(event.data.message));
          }
        } else {
          assert_unreached('Invalid message: ' + event.data.type);
        }
      },
      {once: true});

    iframe.contentWindow.postMessage(
      {action: 'resolve', specifier, baseURL},
      '*'
    );
  });
}

function assert_no_extra_properties(object, expectedProperties, description) {
  for (const actualProperty in object) {
    assert_true(expectedProperties.indexOf(actualProperty) !== -1,
        description + ': unexpected property ' + actualProperty);
  }
}

async function runTests(j) {
  const tests = j.tests;
  delete j.tests;

  if (j.hasOwnProperty('importMap')) {
    assert_own_property(j, 'importMap');
    assert_own_property(j, 'importMapBaseURL');
    j.iframe = await createTestIframe(j.importMap, j.importMapBaseURL);
    delete j.importMap;
    delete j.importMapBaseURL;
  }

  assert_no_extra_properties(
      j,
      ['expectedResults', 'expectedParsedImportMap',
      'baseURL', 'name', 'iframe',
      'importMap', 'importMapBaseURL',
      'link', 'details'],
      j.name);

  if (tests) {
    // Nested node.
    for (const testName in tests) {
      let fullTestName = testName;
      if (j.name) {
        fullTestName = j.name + ': ' + testName;
      }
      tests[testName].name = fullTestName;
      const k = Object.assign({}, j, tests[testName]);
      await runTests(k);
    }
  } else {
    // Leaf node.
    for (const key of ['iframe', 'name', 'expectedResults']) {
      assert_own_property(j, key, j.name);
    }

    assert_equals(
        j.iframe.parseImportMapResult,
        'Success',
        'Import map registration should be successful for resolution tests');
    for (const [specifier, expected] of Object.entries(j.expectedResults)) {
      promise_test(async t => {
        if (expected === null) {
          return promise_rejects_js(t, TypeError, resolve(specifier, j.baseURL, j.iframe));
        } else {
          assert_equals(await resolve(specifier, j.baseURL, j.iframe), expected);
        }
      },
      j.name + ': ' + specifier);
    }
  }
}

export async function runTestsFromJSON(jsonURL) {
  const response = await fetch(jsonURL);
  const json = await response.json();
  await runTests(json);
}