summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/interactive-elements/the-dialog-element/multiple-centered-dialogs.html
blob: 70bb3810e2b7f17afb3b5cf83eef845adc71d1cd (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
<!DOCTYPE html>
<link rel=author href="mailto:jarhar@chromium.org">
<link rel=author href="mailto:falken@chromium.org">
<link rel=help href="https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<style>
body {
  height: 10000px;
}

dialog {
  padding: 0;
  height: 50px;
  width: 50px;
}

#console {
  position: fixed;
}
</style>

<dialog id="top-dialog"></dialog>
<dialog id="first-middle-dialog"></dialog>
<dialog id="second-middle-dialog" style="left: 100px"></dialog>
<dialog id="bottom-dialog"></dialog>

<script>
test(() => {
  function documentHeight() {
    // clientHeight is an integer, but we want the correct floating point
    // value.  Start a binary search at clientHeight-1 and clientHeight+1.
    let min = document.documentElement.clientHeight;
    let max = min + 1;
    --min;

    // binary search with media queries to find the correct height
    for (let iter = 0; iter < 10; ++iter) {
      let test = (min + max) / 2;
      if (window.matchMedia(`(min-height: ${test}px)`).matches)
        min = test;
      else
        max = test;
    }
    return min;
  }
  function expectedTop(dialog) {
    let height = documentHeight();
    return (height - dialog.getBoundingClientRect().height) / 2;
  }

  function showAndTest(id) {
    dialog = document.getElementById(id);
    dialog.showModal();
    assert_approx_equals(dialog.getBoundingClientRect().top, expectedTop(dialog), 0.05, id);
  }

  showAndTest('top-dialog');

  window.scroll(0, 100);
  showAndTest('first-middle-dialog');
  showAndTest('second-middle-dialog');

  window.scroll(0, 200);
  showAndTest('bottom-dialog');
}, 'Test that multiple dialogs are centered properly.');
</script>