summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resize-observer/fragments.html
blob: bba94db88e265413a491f945c9e379e876ff9e18 (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>ResizeObserver with multiple fragments</title>
<link rel="author" title="Oriol Brufau" href="mailto:obrufau@igalia.com">
<link rel="help" href="https://drafts.csswg.org/resize-observer-1/">
<link rel="help" href="https://github.com/w3c/csswg-drafts/issues/3673">
<meta name="assert" content="Tests ResizeObserver supports multiple fragments." />

<style>
#wrapper {
  column-width: 100px;
  width: max-content;
  height: 100px;
  margin: 10px;
}
#target {
  outline: solid;
  background: orange;
}
.w50 {
  width: 50px;
}
.w75 {
  width: 75px;
}
.h100 {
  height: 100px;
}
.h150 {
  height: 150px;
}
.h175 {
  height: 175px;
}
</style>

<div id="log"></div>

<div id="wrapper">
  <div id="target"></div>
</div>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
const target = document.getElementById("target");

const nextSizes = (() => {
  let callback = null;
  new ResizeObserver((entries) => {
    if (callback) {
      callback(entries[0].contentBoxSize);
      callback = null;
    }
  }).observe(target);
  return () => {
    if (callback) {
      throw "Already awaiting another notification";
    }
    return new Promise((resolve, reject) => {
      callback = resolve;
      requestAnimationFrame(() => {
        requestAnimationFrame(() => {
          reject("Missing ResizeObserver notification");
          callback = null;
        });
      });
    });
  };
})();

function checkSizes(className, expectedSizes, msg) {
  promise_test(async () => {
    await new Promise(requestAnimationFrame);
    target.className = className;
    let sizes;
    try {
      sizes = await nextSizes();
    } catch (error) {
      assert_unreached(error);
    }
    assert_equals(sizes.length, expectedSizes.length, "number of fragments");
    for (let i = 0; i < sizes.length; ++i) {
      assert_equals(sizes[i].inlineSize, expectedSizes[i][0], `fragment #${i+1} inline size`);
      assert_equals(sizes[i].blockSize, expectedSizes[i][1], `fragment #${i+1} block size`);
    }
  }, msg);
}

checkSizes(
  "w50 h100",
  [[50, 100]],
  "Single fragment"
);
checkSizes(
  "w50 h150",
  [[50, 100], [50, 50]],
  "Adding 2nd fragment"
);
checkSizes(
  "w50 h175",
  [[50, 100], [50, 75]],
  "Resizing 2nd fragment"
);
checkSizes(
  "w75 h175",
  [[75, 100], [75, 75]],
  "Resizing all fragments"
);
checkSizes(
  "w75 h100",
  [[75, 100]],
  "Removing 2nd fragment"
);
</script>