summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/orientation-event/orientation/add-listener-from-callback.https.html
blob: 7f664ab40070487bd9c433572deefdb919aa25e5 (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
<!DOCTYPE html>
<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 src="../resources/orientation-event-helpers.js"></script>
<script>
'use strict';

promise_test(async (t) => {
  const helper = new SensorTestHelper(t, 'deviceorientation');
  await helper.grantSensorsPermissions();
  await helper.initializeSensors();

  const orientationData1 = generateOrientationData(1.1, 2.2, 3.3, false);
  const orientationData2 = generateOrientationData(11.1, 22.2, 33.3, false);

  let firstListener = null;
  let secondListener = null;
  let firstEventCount = 0;
  let firstPromise = new Promise(resolve => {
    firstListener = async (event) => {
      assert_true(event instanceof DeviceOrientationEvent, 'event is DeviceOrientationEvent');
      assert_equals(event.type, 'deviceorientation', 'event.type is devicemotion');
      assert_true(event.target instanceof Window, 'event is fired on the window object');
      assertEventEquals(event, getExpectedOrientationEvent(orientationData1));
      window.removeEventListener('deviceorientation', firstListener);
      // Some implementations (e.g. Chromium) work without the call below
      // because they disconnect from the virtual sensor in the
      // removeEventListener() call above before connecting again, and in this
      // case the same orientation data is still considered a significant
      // change. This is an implementation detail though, so we explicitly pass
      // different data here.
      await helper.setData(orientationData2);
      if (++firstEventCount == 1) {
        window.addEventListener('deviceorientation', secondListener);
      }
      resolve(event);
    };
  });

  let secondEventCount = 0;
  let secondPromise = new Promise(resolve => {
    secondListener = (event) => {
      assertEventEquals(event, getExpectedOrientationEvent(orientationData2));
      window.removeEventListener('deviceorientation', secondListener);
      ++secondEventCount;
      resolve(event);
    };
  });

  await helper.setData(orientationData1);
  window.addEventListener('deviceorientation', firstListener);
  await firstPromise;
  await secondPromise;
  assert_equals(firstEventCount, 1, "Too many events fired for the first listener");
  assert_equals(secondEventCount, 1, "Too many events fired for the second listener");
}, 'Tests that adding a new deviceorientation event listener from a callback works as expected.');
</script>