summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/svg/struct/scripted/use-load-error-events.tentative.html
blob: 2c52072e16906575679ab654b7e901a08b5541c7 (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
<!doctype html>
<title>'load' and 'error' events for SVG &lt;use></title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/rendering-utils.js"></script>
<svg>
  <defs>
    <rect id="local" width="100" height="100" fill="blue"/>
  </defs>
</svg>
<script>
  function newUseElement() {
    return document.createElementNS('http://www.w3.org/2000/svg', 'use');
  }
  function makeWatcher(root, eventType, url) {
    return new Promise(resolve => {
      const watcher = newUseElement();
      watcher.addEventListener(eventType, resolve);
      watcher.setAttribute('href', url);
      root.appendChild(watcher);
    });
  }
  function expectEvents(t, element, event) {
    return new EventWatcher(t, element, ['load', 'error']).wait_for(event);
  }
  const DATA_URL_PAYLOAD =
      '<svg xmlns="http://www.w3.org/2000/svg"><rect id="green" fill="lime" width="100" height="50"/></svg>';
  const cookie = Date.now();
  let counter = 0;
  function makeCookie(index) {
    return `${cookie}-${index}`;
  }
  function getUrl(type) {
    const cookie = makeCookie(counter++);
    switch (type) {
    case 'existing':
      return `/images/colors.svg?${cookie}#green`;
    case 'existing-data':
      return `data:image/svg+xml,${DATA_URL_PAYLOAD}#green`;
    case 'non-existing':
      return `/images/this-file-should-not-exist.svg?${cookie}#green`;
    case 'broken':
      return `/images/fail.gif?${cookie}#green`;
    }
  }

  promise_test(t => {
    const svg = document.querySelector('svg');
    const use = newUseElement();
    expectEvents(t, use, []);
    svg.appendChild(use).setAttribute('href', '#local');
    return waitForAtLeastOneFrame();
  }, document.title + ', local reference, existing');

  promise_test(t => {
    const svg = document.querySelector('svg');
    const use = newUseElement();
    expectEvents(t, use, []);
    svg.appendChild(use).setAttribute('href', '#local_not_there');
    return waitForAtLeastOneFrame();
  }, document.title + ', local reference, non-existing');

  promise_test(t => {
    const svg = document.querySelector('svg');
    const use = newUseElement();
    const watcher = expectEvents(t, use, ['load']);
    const url = getUrl('existing');
    svg.appendChild(use).setAttribute('href', url);
    return watcher;
  }, document.title + ', external reference, existing');

  promise_test(t => {
    const svg = document.querySelector('svg');
    const use = newUseElement();
    const watcher = expectEvents(t, use, ['error']);
    const url = getUrl('existing-data');
    svg.appendChild(use).setAttribute('href', url);
    return watcher;
  }, document.title + ', external data: URL reference, existing');

  promise_test(t => {
    const svg = document.querySelector('svg');
    const use = newUseElement();
    const watcher = expectEvents(t, use, ['error']);
    const url = getUrl('non-existing');
    svg.appendChild(use).setAttribute('href', url);
    return watcher;
  }, document.title + ', external reference, non-existing');

  promise_test(t => {
    const svg = document.querySelector('svg');
    const use = newUseElement();
    const watcher = expectEvents(t, use, ['error']);
    const url = getUrl('broken');
    svg.appendChild(use).setAttribute('href', url);
    return watcher;
  }, document.title + ', external reference, existing, parse error');

  promise_test(t => {
    const svg = document.querySelector('svg');
    const use = newUseElement();
    expectEvents(t, use, []);
    const url = getUrl('existing');
    svg.appendChild(use).setAttribute('href', url);
    t.step_timeout(() => use.setAttribute('href', '#local'));
    return makeWatcher(svg, 'load', url);
  }, document.title + ', external reference, existing, changed to local reference while loading');

  promise_test(t => {
    const svg = document.querySelector('svg');
    const use = newUseElement();
    expectEvents(t, use, []);
    const url = getUrl('existing-data');
    svg.appendChild(use).setAttribute('href', url);
    t.step_timeout(() => use.setAttribute('href', '#local'));
    return makeWatcher(svg, 'error', url);
  }, document.title + ', external data: URL reference, existing, changed to local reference while loading');

  promise_test(t => {
    const svg = document.querySelector('svg');
    const use = newUseElement();
    expectEvents(t, use, []);
    const url = getUrl('non-existing');
    svg.appendChild(use).setAttribute('href', url);
    t.step_timeout(() => use.setAttribute('href', '#local'));
    return makeWatcher(svg, 'error', url);
  }, document.title + ', external reference, non-existing, changed to local reference while loading');
</script>