summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/screen-orientation/orientation-reading.html
blob: 0bebb6723a384fc112358418d783e72a762940e2 (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
<!DOCTYPE html>
<meta charset="utf-8" />
<meta viewport="width=device-width, initial-scale=1" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script type="module">

import { makeCleanup, getOppositeOrientation } from "./resources/orientation-utils.js";

test(() => {
  assert_true("type" in screen.orientation);
  assert_true("angle" in screen.orientation);
}, "Test screen.orientation properties");

test(() => {
  const type = screen.orientation.type;
  const angle = screen.orientation.angle;

  if (screen.width > screen.height) {
    assert_true(type == "landscape-primary" || type == "landscape-secondary");
  } else if (screen.width < screen.height) {
    assert_true(type == "portrait-primary" || type == "portrait-secondary");
  }

  assert_true(angle == 0 || angle == 90 || angle == 180 || angle == 270);
}, "Test screen.orientation default values.");

promise_test(async t => {
  t.add_cleanup(makeCleanup());
  await test_driver.bless("request full screen");
  await document.documentElement.requestFullscreen();
  try {
    await screen.orientation.lock("portrait-primary");
  } catch (err) {
    // implementation might not support locking to portrait-primary
    return;
  }
  const orientations =
    screen.orientation.angle === 0
      ? {
          secondaryOrientation1: "portrait-secondary",
          primaryOrientation2: "landscape-primary",
          secondaryOrientation2: "landscape-secondary",
        }
      : {
          secondaryOrientation1: "landscape-secondary",
          primaryOrientation2: "portrait-primary",
          secondaryOrientation2: "portrait-secondary",
        };
  try {
    await screen.orientation.lock(orientations.secondaryOrientation1);
  } catch (err) {
    // implementation might not support locking to this orientation
    return;
  }
  assert_equals(
    screen.orientation.angle,
    180,
    "Secondary orientation 1 angle must be 180"
  );
  try {
    await screen.orientation.lock(orientations.primaryOrientation2);
  } catch (err) {
    // implementation might not support locking to this orientation
    return;
  }
  assert_true(
    screen.orientation.angle == 90 || screen.orientation.angle == 270,
    "Primary orientation 2 angle must be either 90 or 270"
  );
  const primaryOrientation2Angle = screen.orientation.angle;
  const secondaryOrientation2Angle = primaryOrientation2Angle === 90 ? 270 : 90;
  try {
    await screen.orientation.lock(orientations.secondaryOrientation2);
  } catch (err) {
    // implementation might not support locking to this orientation
    return;
  }
  assert_equals(
    screen.orientation.angle,
    secondaryOrientation2Angle,
    "Secondary orientation 2 angle must be the opposite angle to primary orientation 2"
  );
  screen.orientation.unlock();
}, "Test the orientations and associated angles");

test(() => {
  const type = screen.orientation.type;
  const angle = screen.orientation.angle;

  try {
    screen.orientation.type = "foo";
  } catch (err) {
    // implementation might throw an exception due to readonly
  }
  try {
    screen.orientation.angle = 42;
  } catch (err) {
    // implementation might throw an exception due to readonly
  }

  assert_equals(screen.orientation.type, type);
  assert_equals(screen.orientation.angle, angle);
}, "Test that screen.orientation properties are not writable");

test(() => {
  assert_equals(screen.orientation, screen.orientation);
}, "Test that screen.orientation is always the same object");

promise_test(async t => {
  t.add_cleanup(makeCleanup());
  await test_driver.bless("request full screen");
  await document.documentElement.requestFullscreen();
  const initialType = screen.orientation.type;
  const initialAngle = screen.orientation.angle;
  const orientationWatcher = new EventWatcher(t, screen.orientation, "change");
  const newOrientationType = getOppositeOrientation();

  // change event is fired before resolving promise by lock.
  const event = await Promise.race([
    orientationWatcher.wait_for("change"),
    screen.orientation.lock(newOrientationType)
  ]);
  assert_true(event instanceof Event, "expected event");
  assert_not_equals(screen.orientation.type, initialType, "type should have changed");
  assert_not_equals(screen.orientation.angle, initialAngle, "angle should have changed");
}, "Test that screen.orientation values change if the orientation changes");
</script>