summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/dom/render-blocking/support/test-render-blocking.js
blob: 71d0d680966127b8d51ceddf35c6330900e80427 (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
// Observes the `load` event of an EventTarget, or the finishing of a resource
// given its url. Requires `/preload/resources/preload_helper.js` for the latter
// usage.
class LoadObserver {
  constructor(target) {
    this.finishTime = null;
    this.load = new Promise((resolve, reject) => {
      if (target.addEventListener) {
        target.addEventListener('load', ev => {
          this.finishTime = ev.timeStamp;
          resolve(ev);
        });
        target.addEventListener('error', reject);
      } else if (typeof target === 'string') {
        const observer = new PerformanceObserver(() => {
          if (numberOfResourceTimingEntries(target)) {
            this.finishTime = performance.now();
            resolve();
          }
        });
        observer.observe({type: 'resource', buffered: true});
      } else {
        reject('Unsupported target for LoadObserver');
      }
    });
  }

  get finished() {
    return this.finishTime !== null;
  }
}

// Observes the insertion of a script/parser-blocking element into DOM via
// MutationObserver, so that we can access the element before it's loaded.
function nodeInserted(parentNode, predicate) {
  return new Promise(resolve => {
    function callback(mutationList) {
      for (let mutation of mutationList) {
        for (let node of mutation.addedNodes) {
          if (predicate(node))
            resolve(node);
        }
      }
    }
    new MutationObserver(callback).observe(parentNode, {childList: true});
  });
}

function createAutofocusTarget() {
  const autofocusTarget = document.createElement('textarea');
  autofocusTarget.setAttribute('autofocus', '');
  // We may not have a body element at this point if we are testing a
  // script-blocking stylesheet. Hence, the new element is added to
  // documentElement.
  document.documentElement.appendChild(autofocusTarget);
  return autofocusTarget;
}

function createScrollTarget() {
  const scrollTarget = document.createElement('div');
  scrollTarget.style.overflow = 'scroll';
  scrollTarget.style.height = '100px';
  const scrollContent = document.createElement('div');
  scrollContent.style.height = '200px';
  scrollTarget.appendChild(scrollContent);
  document.documentElement.appendChild(scrollTarget);
  return scrollTarget;
}

function createAnimationTarget() {
  const style = document.createElement('style');
  style.textContent = `
      @keyframes anim {
        from { height: 100px; }
        to { height: 200px; }
      }
  `;
  const animationTarget = document.createElement('div');
  animationTarget.style.backgroundColor = 'green';
  animationTarget.style.height = '50px';
  animationTarget.style.animation = 'anim 100ms';
  document.documentElement.appendChild(style);
  document.documentElement.appendChild(animationTarget);
  return animationTarget;
}

// Error margin for comparing timestamps of paint and load events, in case they
// are reported by different threads.
const epsilon = 50;

function test_render_blocking(optionalElementOrUrl, finalTest, finalTestTitle) {
  // Ideally, we should observe the 'load' event on the specific render-blocking
  // elements. However, this is not possible for script-blocking stylesheets, so
  // we have to observe the 'load' event on 'window' instead.
  if (!(optionalElementOrUrl instanceof HTMLElement) &&
      typeof optionalElementOrUrl !== 'string') {
    finalTestTitle = finalTest;
    finalTest = optionalElementOrUrl;
    optionalElementOrUrl = undefined;
  }
  const loadObserver = new LoadObserver(optionalElementOrUrl || window);

  promise_test(async test => {
    assert_implements(window.PerformancePaintTiming);

    await test.step_wait(() => performance.getEntriesByType('paint').length);

    assert_true(loadObserver.finished);
    for (let entry of performance.getEntriesByType('paint')) {
      assert_greater_than(entry.startTime, loadObserver.finishTime - epsilon,
                          `${entry.name} should occur after loading render-blocking resources`);
    }
  }, 'Rendering is blocked before render-blocking resources are loaded');

  promise_test(test => {
    return loadObserver.load.then(() => finalTest(test));
  }, finalTestTitle);
}