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

<!--
Tests that AR hit test returns results when a plane is present.
-->
<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>var shouldAutoCreateNonImmersiveSession = false;</script>
    <script src="../resources/webxr_boilerplate.js"></script>
    <script>
      const TestState = Object.freeze({
        "Initial":  0,
        "HitTestSourceRequested": 1,
        "HitTestSourceAvailable": 2,  // hitTestSource variable is guaranteed to be non-null in this state
        "SessionEndRequested": 3,
        "Done": 4,
      });

      let testState = TestState.Initial;

      let hitTestSource = null;

      function stepStartHitTesting(isTransientTest) {
        const sessionInfo = sessionInfos[sessionTypes.AR];
        const referenceSpace = sessionInfo.currentRefSpace;
        const currentSession = sessionInfo.currentSession;

        if(isTransientTest) {
          console.log('Requesting transient input hit test source');
          currentSession.requestHitTestSourceForTransientInput({
            profile: "generic-touchscreen",
            offsetRay: new XRRay()
          }).then((hts) => {
            console.log('Got transient input hit test source');
            hitTestSource = hts;
            testState = TestState.HitTestSourceAvailable;
          });
        } else {
          console.log('Requesting hit test source');
          currentSession.requestHitTestSource({
            space: referenceSpace,
            offsetRay: new XRRay()
          }).then((hts) => {
            console.log('Got hit test source');
            hitTestSource = hts;
            testState = TestState.HitTestSourceAvailable;
          });
        }

        testState = TestState.HitTestSourceRequested;

        onARFrameCallback = (session, frame) => {
          console.log('Got AR frame, test state: ' + testState);
          switch(testState) {
            case TestState.HitTestSourceAvailable: {
              console.log('Ending session');
              session.end().then(() => {
                console.log('Session ended');
                hitTestSource.cancel();
                testState = TestState.Done;
                done();
              });

              testState = TestState.SessionEndRequested;

              return;
            }
            default:
              return;
          }
        };
      }
    </script>
  </body>
</html>