chromium/chrome/test/data/xr/e2e_test_files/html/test_webxr_input_sources_change_event.html

<!doctype html>
<!--
A collection of helper functions and listeners to confirm the state of input
sources for the same object tests.
-->
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="../resources/webxr_e2e.css">
  </head>
  <body>
    <canvas id="webgl-canvas"></canvas>
    <script src="../../../../../../third_party/blink/web_tests/resources/testharness.js"></script>
    <script src="../resources/webxr_e2e.js"></script>
    <script src="../resources/webxr_boilerplate.js"></script>
    <script>
      let inputChangeEvents = 0;
      let lastAdded = null;
      let lastRemoved = null;
      function onInputSourcesChange(event) {
        lastAdded = event.added;
        lastRemoved = event.removed;
        inputChangeEvents++;
      }

      onSessionStartedCallback = function(session) {
        if (session.mode.startsWith("immersive")) {
          session.addEventListener('inputsourceschange', onInputSourcesChange, false);
        }
      }

      function getCurrentInputSources() {
        let currentSession = sessionInfos[sessionTypes.IMMERSIVE].currentSession;
        return Array.from(currentSession.inputSources.values());
      }

      let cached_input_source = null;
      function updateCachedInputSource(id) {
        let input_sources = getCurrentInputSources();
        assert_less_than(id, input_sources.length);
        cached_input_source = input_sources[id];
      }

      function validateAdded(length) {
        assert_not_equals(lastAdded, null);
        assert_equals(lastAdded.length, length,
            "Added length matches expectations");

        let currentSources = getCurrentInputSources();
        lastAdded.forEach((source) => {
          assert_true(currentSources.includes(source),
            "Every element in added should be in the input source list");
        });
      }

      function validateRemoved(length) {
        assert_not_equals(lastRemoved, null);
        assert_equals(lastRemoved.length, length,
            "Removed length matches expectations");

        let currentSources = getCurrentInputSources();
        lastRemoved.forEach((source) => {
          assert_false(currentSources.includes(source),
            "No element in removed should be in the input source list");
        });
      }

      function validateCachedAddedPresence(presence) {
        assert_not_equals(lastAdded, null);
        assert_not_equals(cached_input_source, null);
        assert_equals(lastAdded.includes(cached_input_source), presence,
          "Presence of cached input in lastAdded matches expectation");
      }

      function validateCachedRemovedPresence(presence) {
        assert_not_equals(lastRemoved, null);
        assert_not_equals(cached_input_source, null);
        assert_equals(lastRemoved.includes(cached_input_source), presence,
          "Presence of cached input in lastRemoved matches expectation");
      }


    </script>
  </body>
</html>