summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webxr/events_input_sources_change.https.html
blob: b2a17d4e17ae03bf7ed51e07095aa36dcce033a3 (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/webxr_util.js"></script>
<script src="resources/webxr_test_constants.js"></script>

<script>
let testName = "Transient input sources fire events in the right order";

let watcherDone = new Event("watcherdone");

let fakeDeviceInitParams = TRACKED_IMMERSIVE_DEVICE;

let testFunction = function(session, fakeDeviceController, t) {
  let eventWatcher = new EventWatcher(
    t, session, ["inputsourceschange", "selectstart", "select", "selectend", "watcherdone"]);
  let eventPromise = eventWatcher.wait_for(
    ["inputsourceschange", "selectstart", "select", "selectend", "inputsourceschange", "watcherdone"]);

  let inputChangeEvents = 0;
  let cached_input_source = null;
  function onInputSourcesChange(event) {
    t.step(() => {
      inputChangeEvents++;
      assert_equals(event.session, session);
      validateSameObject(event);

      // The first change event should be adding our controller.
      if (inputChangeEvents === 1) {
        validateAdded(event.added, 1);
        validateRemoved(event.removed, 0);
        cached_input_source = session.inputSources[0];
        assert_not_equals(cached_input_source, null);
      } else if (inputChangeEvents === 2) {
        // The second event should be removing our controller.
        validateAdded(event.added, 0);
        validateRemoved(event.removed, 1);
        assert_true(event.removed.includes(cached_input_source));
        session.dispatchEvent(watcherDone);
      }
    });
  }

  function validateAdded(added, length) {
    t.step(() => {
      assert_not_equals(added, null);
      assert_equals(added.length, length,
          "Added length matches expectations");

      let currentSources = Array.from(session.inputSources.values());
      added.forEach((source) => {
        assert_true(currentSources.includes(source),
          "Every element in added should be in the input source list");
      });
    });
  }

  function validateRemoved(removed, length) {
    t.step(() => {
      assert_not_equals(removed, null);
        assert_equals(removed.length, length,
            "Removed length matches expectations");

      let currentSources = Array.from(session.inputSources.values());
      removed.forEach((source) => {
        assert_false(currentSources.includes(source),
          "No element in removed should be in the input source list");
      });
    });
  }

  // Verifies that the same object is returned each time attributes are accessed
  // on an XRInputSourcesChangeEvent, as required by the spec.
  function validateSameObject(event) {
    let eventSession = event.session;
    let added = event.added;
    let removed = event.removed;

    t.step(() => {
      assert_equals(eventSession, event.session,
        "XRInputSourcesChangeEvent.session returns the same object.");
      assert_equals(added, event.added,
        "XRInputSourcesChangeEvent.added returns the same object.");
      assert_equals(removed, event.removed,
        "XRInputSourcesChangeEvent.removed returns the same object.");
    });
  }

  session.addEventListener('inputsourceschange', onInputSourcesChange, false);

  // Create our input source and immediately toggle the primary input so that
  // it appears as already needing to send a click event when it appears.
  let input_source = fakeDeviceController.simulateInputSourceConnection({
    handedness: "right",
    targetRayMode: "tracked-pointer",
    pointerOrigin: VALID_POINTER_TRANSFORM,
    profiles: [],
    selectionClicked: true
  });

  // Make our input source disappear after one frame, and wait an additional
  // frame for that disappearance to propogate.
  requestSkipAnimationFrame(session, (time, xrFrame) => {
    input_source.disconnect();
    session.requestAnimationFrame((time, xrFrame) => {});
  });

  return eventPromise;
};

xr_session_promise_test(
  testName, testFunction, fakeDeviceInitParams, 'immersive-vr');
</script>