WebXR depth: add WebXR test API extension for depth API & write WPTs (#27654)

WebXR Test API - extensions for depth:
https://github.com/immersive-web/webxr-test-api/pull/74

Spec:
https://immersive-web.github.io/depth-sensing/

Change-Id: Iad124c43d8abb5c19ff24d4857ec2714b5211163
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2693691
Commit-Queue: Piotr Bialecki <bialpio@chromium.org>
Reviewed-by: Alexander Cooper <alcooper@chromium.org>
Cr-Commit-Position: refs/heads/master@{#855069}

Co-authored-by: Piotr Bialecki <bialpio@chromium.org>
diff --git a/resources/chromium/webxr-test.js b/resources/chromium/webxr-test.js
index fe9dc3f..f7e9cbd 100644
--- a/resources/chromium/webxr-test.js
+++ b/resources/chromium/webxr-test.js
@@ -337,6 +337,7 @@
     'dom-overlay': vrMojom.XRSessionFeature.DOM_OVERLAY,
     'light-estimation': vrMojom.XRSessionFeature.LIGHT_ESTIMATION,
     'anchors': vrMojom.XRSessionFeature.ANCHORS,
+    'depth-sensing': vrMojom.XRSessionFeature.DEPTH,
   };
 
   static sessionModeToMojoMap = {
@@ -387,6 +388,9 @@
     // Anchor creation callback (initially null, can be set by tests).
     this.anchor_creation_callback_ = null;
 
+    this.depthSensingData_ = null;
+    this.depthSensingDataDirty_ = false;
+
     let supportedModes = [];
     if (fakeDeviceInit.supportedModes) {
       supportedModes = fakeDeviceInit.supportedModes.slice();
@@ -433,6 +437,10 @@
       this.world_ = fakeDeviceInit.world;
     }
 
+    if (fakeDeviceInit.depthSensingData) {
+      this.setDepthSensingData(fakeDeviceInit.depthSensingData);
+    }
+
     this.defaultFramebufferScale_ = default_framebuffer_scale;
     this.enviromentBlendMode_ = this._convertBlendModeToEnum(fakeDeviceInit.environmentBlendMode);
     this.interactionMode_ = this._convertInteractionModeToEnum(fakeDeviceInit.interactionMode);
@@ -675,6 +683,32 @@
     }
   }
 
+  setDepthSensingData(depthSensingData) {
+    for(const key of ["depthData", "normDepthBufferFromNormView", "rawValueToMeters", "width", "height"]) {
+      if(!(key in depthSensingData)) {
+        throw new TypeError("Required key not present. Key: " + key);
+      }
+    }
+
+    if(depthSensingData.depthData != null) {
+      // Create new object w/ properties based on the depthSensingData, but
+      // convert the FakeXRRigidTransformInit into a transformation matrix object.
+      this.depthSensingData_ = Object.assign({},
+        depthSensingData, {
+          normDepthBufferFromNormView: composeGFXTransform(depthSensingData.normDepthBufferFromNormView),
+        });
+    } else {
+      throw new TypeError("`depthData` is not set");
+    }
+
+    this.depthSensingDataDirty_ = true;
+  }
+
+  clearDepthSensingData() {
+    this.depthSensingData_ = null;
+    this.depthSensingDataDirty_ = true;
+  }
+
   // Helper methods
   getNonImmersiveDisplayInfo() {
     const displayInfo = this.getImmersiveDisplayInfo();
@@ -857,6 +891,8 @@
 
         this._calculateAnchorInformation(frameData);
 
+        this._calculateDepthInformation(frameData);
+
         this._injectAdditionalFrameData(options, frameData);
 
         resolve({frameData});
@@ -1119,6 +1155,8 @@
           }
         }
 
+        this.enabledFeatures_ = enabled_features;
+
         return Promise.resolve({
           session: {
             submitFrameSink: submit_frame_sink,
@@ -1129,7 +1167,12 @@
             deviceConfig: {
               usesInputEventing: false,
               defaultFramebufferScale: this.defaultFramebufferScale_,
-              supportsViewportScaling: true
+              supportsViewportScaling: true,
+              depthConfiguration:
+                enabled_features.includes(vrMojom.XRSessionFeature.DEPTH) ? {
+                  depthUsage: vrMojom.XRDepthUsage.kCPUOptimized,
+                  depthDataFormat: vrMojom.XRDepthDataFormat.kLuminanceAlpha,
+                } : null,
             },
             enviromentBlendMode: this.enviromentBlendMode_,
             interactionMode: this.interactionMode_
@@ -1142,8 +1185,16 @@
   }
 
   runtimeSupportsSession(options) {
+    let result = this.supportedModes_.includes(options.mode);
+
+    if (options.requiredFeatures.includes(vrMojom.XRSessionFeature.DEPTH)
+    || options.optionalFeatures.includes(vrMojom.XRSessionFeature.DEPTH)) {
+      result &= options.depthOptions.usagePreferences.includes(vrMojom.XRDepthUsage.kCPUOptimized);
+      result &= options.depthOptions.dataFormatPreferences.includes(vrMojom.XRDepthDataFormat.kLuminanceAlpha);
+    }
+
     return Promise.resolve({
-      supportsSession: this.supportedModes_.includes(options.mode)
+      supportsSession: result,
     });
   }
 
@@ -1199,6 +1250,43 @@
     }
   }
 
+  // Private functions - depth sensing implementation:
+
+  // Modifies passed in frameData to add anchor information.
+  _calculateDepthInformation(frameData) {
+    if (!this.supportedModes_.includes(vrMojom.XRSessionMode.kImmersiveAr)) {
+      return;
+    }
+
+    if (!this.enabledFeatures_.includes(vrMojom.XRSessionFeature.DEPTH)) {
+      return;
+    }
+
+    // If we don't have a current depth data, we'll return null
+    // (i.e. no data is not a valid data, so it cannot be "StillValid").
+    if (this.depthSensingData_ == null) {
+      frameData.depthData = null;
+      return;
+    }
+
+    if(!this.depthSensingDataDirty_) {
+      frameData.depthData = { dataStillValid: {}};
+      return;
+    }
+
+    frameData.depthData = {
+      updatedDepthData: {
+        timeDelta: frameData.timeDelta,
+        normTextureFromNormView: this.depthSensingData_.normDepthBufferFromNormView,
+        rawValueToMeters: this.depthSensingData_.rawValueToMeters,
+        size: { width: this.depthSensingData_.width, height: this.depthSensingData_.height },
+        pixelData: { bytes: this.depthSensingData_.depthData }
+      }
+    };
+
+    this.depthSensingDataDirty_ = false;
+  }
+
   // Private functions - hit test implementation:
 
   // Returns a Promise<bool> that signifies whether hit test source creation should succeed.
diff --git a/webxr/depth-sensing/cpu/depth_sensing_cpu_dataUnavailable.https.html b/webxr/depth-sensing/cpu/depth_sensing_cpu_dataUnavailable.https.html
new file mode 100644
index 0000000..e120f0b
--- /dev/null
+++ b/webxr/depth-sensing/cpu/depth_sensing_cpu_dataUnavailable.https.html
@@ -0,0 +1,27 @@
+<!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_asserts.js"></script>
+<script src="../../resources/webxr_test_constants.js"></script>
+<script src="../../resources/webxr_test_constants_fake_depth.js"></script>
+<script src="../dataUnavailableTests.js"></script>
+
+<script>
+
+const fakeDeviceInitParams = {
+  supportedModes: ["immersive-ar"],
+  views: VALID_VIEWS,
+  supportedFeatures: ALL_FEATURES,
+  depthSensingData: DEPTH_SENSING_DATA,
+};
+
+xr_session_promise_test("Ensures depth data is not available when cleared in the controller, `cpu-optimized`",
+  dataUnavailableTestFunctionGenerator(/*isCpuOptimized=*/true),
+  fakeDeviceInitParams,
+  'immersive-ar', {
+    requiredFeatures: ['depth-sensing'],
+    depthSensing: VALID_DEPTH_CONFIG_CPU_USAGE,
+  });
+
+</script>
diff --git a/webxr/depth-sensing/cpu/depth_sensing_cpu_inactiveFrame.https.html b/webxr/depth-sensing/cpu/depth_sensing_cpu_inactiveFrame.https.html
new file mode 100644
index 0000000..92c20ce
--- /dev/null
+++ b/webxr/depth-sensing/cpu/depth_sensing_cpu_inactiveFrame.https.html
@@ -0,0 +1,26 @@
+<!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_asserts.js"></script>
+<script src="../../resources/webxr_test_constants.js"></script>
+<script src="../../resources/webxr_test_constants_fake_depth.js"></script>
+<script src="../inactiveFrameTests.js"></script>
+
+<script>
+
+const fakeDeviceInitParams = {
+  supportedModes: ["immersive-ar"],
+  views: VALID_VIEWS,
+  supportedFeatures: ALL_FEATURES,
+};
+
+xr_session_promise_test("Ensures getDepthInformation() throws when not run in an active frame, `cpu-optimized`",
+  inactiveFrameTestFunctionGenerator(/*isCpuOptimized=*/true),
+  fakeDeviceInitParams,
+  'immersive-ar', {
+    requiredFeatures: ['depth-sensing'],
+    depthSensing: VALID_DEPTH_CONFIG_CPU_USAGE,
+  });
+
+</script>
diff --git a/webxr/depth-sensing/cpu/depth_sensing_cpu_incorrectUsage.https.html b/webxr/depth-sensing/cpu/depth_sensing_cpu_incorrectUsage.https.html
new file mode 100644
index 0000000..44868d8
--- /dev/null
+++ b/webxr/depth-sensing/cpu/depth_sensing_cpu_incorrectUsage.https.html
@@ -0,0 +1,48 @@
+<!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_asserts.js"></script>
+<script src="../../resources/webxr_test_constants.js"></script>
+<script src="../../resources/webxr_test_constants_fake_depth.js"></script>
+<script src="../incorrectUsageTests.js"></script>
+
+<script>
+
+const fakeDeviceInitParams = {
+  supportedModes: ["immersive-ar"],
+  views: VALID_VIEWS,
+  supportedFeatures: ALL_FEATURES,
+};
+
+const incorrectUsagetestFunctionTryGetWebGLOnCpu = function (session, controller, t, sessionObjects) {
+  return session.requestReferenceSpace('viewer').then((viewerSpace) => {
+    let done = false;
+
+    const glBinding = new XRWebGLBinding(session, sessionObjects.gl);
+
+    session.requestAnimationFrame((time, frame) => {
+      const pose = frame.getViewerPose(viewerSpace);
+      for(const view of pose.views) {
+        t.step(() => {
+          assert_throws_dom("InvalidStateError", () => glBinding.getDepthInformation(view),
+                            "XRWebGLBinding.getDepthInformation() should throw when depth sensing is in `cpu-optimized` usage mode");
+        });
+      }
+
+      done = true;
+    });
+
+    return t.step_wait(() => done);
+  });
+};
+
+xr_session_promise_test("Ensures XRWebGLDepthInformation is not obtainable in `cpu-optimized` usage mode",
+  incorrectUsagetestFunctionTryGetWebGLOnCpu,
+  fakeDeviceInitParams,
+  'immersive-ar', {
+    requiredFeatures: ['depth-sensing'],
+    depthSensing: VALID_DEPTH_CONFIG_CPU_USAGE,
+  });
+
+</script>
diff --git a/webxr/depth-sensing/cpu/depth_sensing_cpu_luminance_alpha_dataValid.https.html b/webxr/depth-sensing/cpu/depth_sensing_cpu_luminance_alpha_dataValid.https.html
new file mode 100644
index 0000000..3bda9e2
--- /dev/null
+++ b/webxr/depth-sensing/cpu/depth_sensing_cpu_luminance_alpha_dataValid.https.html
@@ -0,0 +1,106 @@
+<!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_math_utils.js"></script>
+<script src="../../resources/webxr_test_asserts.js"></script>
+<script src="../../resources/webxr_test_constants.js"></script>
+<script src="../../resources/webxr_test_constants_fake_depth.js"></script>
+
+<script>
+
+const fakeDeviceInitParams = {
+  supportedModes: ["immersive-ar"],
+  views: VALID_VIEWS,
+  supportedFeatures: ALL_FEATURES,
+  depthSensingData: DEPTH_SENSING_DATA,
+};
+
+const assert_depth_valid_at = function(depthInformation, row, column, deltaRow, deltaColumn) {
+  // column and row correspond to the depth buffer coordinates,
+  // *not* to normalized view coordinates the getDepthInMeters() expects.
+
+  const expectedValue = getExpectedValueAt(column, row);
+
+  // 1. Normalize:
+  let x = (column + deltaColumn) / depthInformation.width;
+  let y = (row + deltaRow) / depthInformation.height;
+
+  // 2. Apply the transform that changes the origin and axes:
+  x = 1.0 - x;
+  y = 1.0 - y;
+
+  const depthValue = depthInformation.getDepthInMeters(x, y);
+  assert_approx_equals(depthValue, expectedValue, FLOAT_EPSILON,
+                        "Depth value at (" + column + "," + row + "), deltas=(" + deltaColumn + ", " + deltaRow + "), "
+                        + "coordinates (" + x + "," + y + ") must match!");
+}
+
+const assert_depth_valid = function(depthInformation) {
+  for(let row = 0; row < depthInformation.height; row++) {
+    for(let column = 0; column < depthInformation.width; column++) {
+      // middle of the pixel:
+      assert_depth_valid_at(depthInformation, row, column, 0.5, 0.5);
+
+      // corners of the pixel:
+      assert_depth_valid_at(depthInformation, row, column, FLOAT_EPSILON, FLOAT_EPSILON);
+      assert_depth_valid_at(depthInformation, row, column, FLOAT_EPSILON, 1 - FLOAT_EPSILON);
+      assert_depth_valid_at(depthInformation, row, column, 1 - FLOAT_EPSILON, FLOAT_EPSILON);
+      assert_depth_valid_at(depthInformation, row, column, 1 - FLOAT_EPSILON, 1 - FLOAT_EPSILON);
+    }
+  }
+
+  // Verify out-of-bounds accesses throw:
+  assert_throws_js(RangeError,
+                   () => depthInformation.getDepthInMeters(-FLOAT_EPSILON, 0.0),
+                   "getDepthInMeters() should throw when run with invalid indices - negative x");
+  assert_throws_js(RangeError,
+                   () => depthInformation.getDepthInMeters(0.0, -FLOAT_EPSILON),
+                   "getDepthInMeters() should throw when run with invalid indices - negative y");
+  assert_throws_js(RangeError,
+                   () => depthInformation.getDepthInMeters(1+FLOAT_EPSILON, 0.0),
+                   "getDepthInMeters() should throw when run with invalid indices - too big x");
+  assert_throws_js(RangeError,
+                   () => depthInformation.getDepthInMeters(0.0, 1+FLOAT_EPSILON),
+                   "getDepthInMeters() should throw when run with invalid indices - too big y");
+};
+
+const testCpuOptimizedLuminanceAlpha = function(session, fakeDeviceController, t) {
+  return session.requestReferenceSpace('viewer').then((viewerSpace) => {
+    let done = false;
+
+    const rafCallback = function(time, frame) {
+      const pose = frame.getViewerPose(viewerSpace);
+      if(pose) {
+        for(const view of pose.views) {
+          const depthInformation = frame.getDepthInformation(view);
+
+          t.step(() => {
+            assert_not_equals(depthInformation, null, "XRCPUDepthInformation must not be null!");
+            assert_approx_equals(depthInformation.width, DEPTH_SENSING_DATA.width, FLOAT_EPSILON);
+            assert_approx_equals(depthInformation.height, DEPTH_SENSING_DATA.height, FLOAT_EPSILON);
+            assert_approx_equals(depthInformation.rawValueToMeters, DEPTH_SENSING_DATA.rawValueToMeters, FLOAT_EPSILON);
+            assert_transform_approx_equals(depthInformation.normDepthBufferFromNormView, DEPTH_SENSING_DATA.normDepthBufferFromNormView);
+            assert_depth_valid(depthInformation);
+          });
+        }
+      }
+
+      done = true;
+    };
+
+    session.requestAnimationFrame(rafCallback);
+
+    return t.step_wait(() => done);
+  });
+};
+
+xr_session_promise_test("Ensures depth data is returned and values match expectation, cpu-optimized, luminance-alpha.",
+  testCpuOptimizedLuminanceAlpha,
+  fakeDeviceInitParams,
+  'immersive-ar', {
+    'requiredFeatures': ['depth-sensing'],
+    depthSensing: VALID_DEPTH_CONFIG_CPU_USAGE,
+  });
+
+</script>
diff --git a/webxr/depth-sensing/cpu/depth_sensing_cpu_staleView.https.html b/webxr/depth-sensing/cpu/depth_sensing_cpu_staleView.https.html
new file mode 100644
index 0000000..6a411ac
--- /dev/null
+++ b/webxr/depth-sensing/cpu/depth_sensing_cpu_staleView.https.html
@@ -0,0 +1,26 @@
+<!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_asserts.js"></script>
+<script src="../../resources/webxr_test_constants.js"></script>
+<script src="../../resources/webxr_test_constants_fake_depth.js"></script>
+<script src="../staleViewsTests.js"></script>
+
+<script>
+
+const fakeDeviceInitParams = {
+  supportedModes: ["immersive-ar"],
+  views: VALID_VIEWS,
+  supportedFeatures: ALL_FEATURES,
+};
+
+xr_session_promise_test("Ensures getDepthInformation() throws when run with stale XRView, `cpu-optimized`",
+  staleViewsTestFunctionGenerator(/*isCpuOptimized=*/true),
+  fakeDeviceInitParams,
+  'immersive-ar', {
+    requiredFeatures: ['depth-sensing'],
+    depthSensing: VALID_DEPTH_CONFIG_CPU_USAGE,
+  });
+
+</script>
diff --git a/webxr/depth-sensing/dataUnavailableTests.js b/webxr/depth-sensing/dataUnavailableTests.js
new file mode 100644
index 0000000..7460af7
--- /dev/null
+++ b/webxr/depth-sensing/dataUnavailableTests.js
@@ -0,0 +1,58 @@
+'use strict';
+
+const TestStates = Object.freeze({
+  "ShouldSucceedScheduleRAF": 1,
+  "ShouldFailScheduleRAF": 2,
+  "ShouldSucceedTestDone": 3,
+});
+
+const dataUnavailableTestFunctionGenerator = function(isCpuOptimized) {
+  return (session, controller, t, sessionObjects) => {
+    let state = TestStates.ShouldSucceedScheduleRAF;
+
+    return session.requestReferenceSpace('viewer').then((viewerSpace) => {
+      let done = false;
+
+      const glBinding = new XRWebGLBinding(session, sessionObjects.gl);
+
+      const rafCb = function(time, frame) {
+        const pose = frame.getViewerPose(viewerSpace);
+        for(const view of pose.views) {
+          const depthInformation = isCpuOptimized ? frame.getDepthInformation(view)
+                                                  : glBinding.getDepthInformation(view);
+
+          if (state == TestStates.ShouldSucceedScheduleRAF
+          || state == TestStates.ShouldSucceedTestDone) {
+            t.step(() => {
+              assert_not_equals(depthInformation, null);
+            });
+          } else {
+            t.step(() => {
+              assert_equals(depthInformation, null);
+            });
+          }
+        }
+
+        switch(state) {
+          case TestStates.ShouldSucceedScheduleRAF:
+            controller.clearDepthSensingData();
+            state = TestStates.ShouldFailScheduleRAF;
+            session.requestAnimationFrame(rafCb);
+            break;
+          case TestStates.ShouldFailScheduleRAF:
+            controller.setDepthSensingData(DEPTH_SENSING_DATA);
+            state = TestStates.ShouldSucceedTestDone;
+            session.requestAnimationFrame(rafCb);
+            break;
+          case TestStates.ShouldSucceedTestDone:
+            done = true;
+            break;
+        }
+      };
+
+      session.requestAnimationFrame(rafCb);
+
+      return t.step_wait(() => done);
+    });
+  };
+};
\ No newline at end of file
diff --git a/webxr/depth-sensing/depth_sensing_notEnabled.https.html b/webxr/depth-sensing/depth_sensing_notEnabled.https.html
new file mode 100644
index 0000000..23bae35
--- /dev/null
+++ b/webxr/depth-sensing/depth_sensing_notEnabled.https.html
@@ -0,0 +1,61 @@
+<!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>
+
+const testFunctionCpu = function (session, controller, t) {
+  return session.requestReferenceSpace('viewer').then((viewerSpace) => {
+    let done = false;
+
+    session.requestAnimationFrame((time, frame) => {
+      const pose = frame.getViewerPose(viewerSpace);
+      for(const view of pose.views) {
+        assert_throws_dom("NotSupportedError", () => frame.getDepthInformation(view),
+                          "getDepthInformation() should throw when depth sensing is disabled");
+      }
+
+      done = true;
+    });
+
+    return t.step_wait(() => done);
+  });
+};
+
+const testFunctionGpu = function (session, controller, t, sessionObjects) {
+  return session.requestReferenceSpace('viewer').then((viewerSpace) => {
+    let done = false;
+
+    const glBinding = new XRWebGLBinding(session, sessionObjects.gl);
+
+    session.requestAnimationFrame((time, frame) => {
+      const pose = frame.getViewerPose(viewerSpace);
+      for(const view of pose.views) {
+        t.step(() => {
+          assert_throws_dom("NotSupportedError", () => glBinding.getDepthInformation(view),
+                            "getDepthInformation() should throw when depth sensing is disabled");
+        });
+      }
+
+      done = true;
+    });
+
+    return t.step_wait(() => done);
+  });
+};
+
+xr_session_promise_test(
+  "XRFrame.getDepthInformation() rejects if depth sensing is not enabled on a session",
+  testFunctionCpu,
+  IMMERSIVE_AR_DEVICE,
+  'immersive-ar');
+
+xr_session_promise_test(
+  "XRWebGLBinding.getDepthInformation() rejects if depth sensing is not enabled on a session",
+  testFunctionGpu,
+  IMMERSIVE_AR_DEVICE,
+  'immersive-ar');
+
+</script>
diff --git a/webxr/depth-sensing/gpu/depth_sensing_gpu_dataUnavailable.https.html b/webxr/depth-sensing/gpu/depth_sensing_gpu_dataUnavailable.https.html
new file mode 100644
index 0000000..018edf7
--- /dev/null
+++ b/webxr/depth-sensing/gpu/depth_sensing_gpu_dataUnavailable.https.html
@@ -0,0 +1,27 @@
+<!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_asserts.js"></script>
+<script src="../../resources/webxr_test_constants.js"></script>
+<script src="../../resources/webxr_test_constants_fake_depth.js"></script>
+<script src="../dataUnavailableTests.js"></script>
+
+<script>
+
+const fakeDeviceInitParams = {
+  supportedModes: ["immersive-ar"],
+  views: VALID_VIEWS,
+  supportedFeatures: ALL_FEATURES,
+  depthSensingData: DEPTH_SENSING_DATA,
+};
+
+xr_session_promise_test("Ensures depth data is not available when cleared in the controller, `gpu-optimized`",
+  dataUnavailableTestFunctionGenerator(/*isCpuOptimized=*/false),
+  fakeDeviceInitParams,
+  'immersive-ar', {
+    requiredFeatures: ['depth-sensing'],
+    depthSensing: VALID_DEPTH_CONFIG_GPU_USAGE,
+  });
+
+</script>
diff --git a/webxr/depth-sensing/gpu/depth_sensing_gpu_inactiveFrame.https.html b/webxr/depth-sensing/gpu/depth_sensing_gpu_inactiveFrame.https.html
new file mode 100644
index 0000000..6116f7a
--- /dev/null
+++ b/webxr/depth-sensing/gpu/depth_sensing_gpu_inactiveFrame.https.html
@@ -0,0 +1,26 @@
+<!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_asserts.js"></script>
+<script src="../../resources/webxr_test_constants.js"></script>
+<script src="../../resources/webxr_test_constants_fake_depth.js"></script>
+<script src="../inactiveFrameTests.js"></script>
+
+<script>
+
+const fakeDeviceInitParams = {
+  supportedModes: ["immersive-ar"],
+  views: VALID_VIEWS,
+  supportedFeatures: ALL_FEATURES,
+};
+
+xr_session_promise_test("Ensures getDepthInformation() throws when not run in an active frame, `gpu-optimized`",
+  testFunctionGenerator(/*isCpuOptimized=*/false),
+  fakeDeviceInitParams,
+  'immersive-ar', {
+    requiredFeatures: ['depth-sensing'],
+    depthSensing: VALID_DEPTH_CONFIG_GPU_USAGE,
+  });
+
+</script>
diff --git a/webxr/depth-sensing/gpu/depth_sensing_gpu_incorrectUsage.https.html b/webxr/depth-sensing/gpu/depth_sensing_gpu_incorrectUsage.https.html
new file mode 100644
index 0000000..9fc2e6a
--- /dev/null
+++ b/webxr/depth-sensing/gpu/depth_sensing_gpu_incorrectUsage.https.html
@@ -0,0 +1,46 @@
+<!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_asserts.js"></script>
+<script src="../../resources/webxr_test_constants.js"></script>
+<script src="../../resources/webxr_test_constants_fake_depth.js"></script>
+<script src="../incorrectUsageTests.js"></script>
+
+<script>
+
+const fakeDeviceInitParams = {
+  supportedModes: ["immersive-ar"],
+  views: VALID_VIEWS,
+  supportedFeatures: ALL_FEATURES,
+};
+
+const incorrectUsageTestFunctionTryGetCpuOnGpu = function (session, controller, t, sessionObjects) {
+  return session.requestReferenceSpace('viewer').then((viewerSpace) => {
+    let done = false;
+
+    session.requestAnimationFrame((time, frame) => {
+      const pose = frame.getViewerPose(viewerSpace);
+      for(const view of pose.views) {
+        t.step(() => {
+          assert_throws_dom("InvalidStateError", () => frame.getDepthInformation(view),
+                            "XRFrame.getDepthInformation() should throw when depth sensing is in `gpu-optimized` usage mode");
+        });
+      }
+
+      done = true;
+    });
+
+    return t.step_wait(() => done);
+  });
+};
+
+xr_session_promise_test("Ensures XRCPUDepthInformation is not obtainable in `gpu-optimized` usage mode",
+  incorrectUsageTestFunctionTryGetCpuOnGpu,
+  fakeDeviceInitParams,
+  'immersive-ar', {
+    requiredFeatures: ['depth-sensing'],
+    depthSensing: VALID_DEPTH_CONFIG_GPU_USAGE,
+  });
+
+</script>
diff --git a/webxr/depth-sensing/gpu/depth_sensing_gpu_staleView.https.html b/webxr/depth-sensing/gpu/depth_sensing_gpu_staleView.https.html
new file mode 100644
index 0000000..ecd0d47
--- /dev/null
+++ b/webxr/depth-sensing/gpu/depth_sensing_gpu_staleView.https.html
@@ -0,0 +1,26 @@
+<!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_asserts.js"></script>
+<script src="../../resources/webxr_test_constants.js"></script>
+<script src="../../resources/webxr_test_constants_fake_depth.js"></script>
+<script src="../staleViewsTests.js"></script>
+
+<script>
+
+const fakeDeviceInitParams = {
+  supportedModes: ["immersive-ar"],
+  views: VALID_VIEWS,
+  supportedFeatures: ALL_FEATURES,
+};
+
+xr_session_promise_test("Ensures getDepthInformation() throws when not run with stale XRView, `gpu-optimized`",
+  staleViewsTestFunctionGenerator(/*isCpuOptimized=*/false),
+  fakeDeviceInitParams,
+  'immersive-ar', {
+    requiredFeatures: ['depth-sensing'],
+    depthSensing: VALID_DEPTH_CONFIG_GPU_USAGE,
+  });
+
+</script>
diff --git a/webxr/depth-sensing/inactiveFrameTests.js b/webxr/depth-sensing/inactiveFrameTests.js
new file mode 100644
index 0000000..b310f01
--- /dev/null
+++ b/webxr/depth-sensing/inactiveFrameTests.js
@@ -0,0 +1,36 @@
+'use strict';
+
+const inactiveFrameTestFunctionGenerator = function(isCpuOptimized) {
+  return (session, controller, t, sessionObjects) => {
+    return session.requestReferenceSpace('viewer').then((viewerSpace) => {
+      let callbacksKickedOff = false;
+      let callbackCounter = 0;
+
+      const glBinding = new XRWebGLBinding(session, sessionObjects.gl);
+
+      const rafCb = function(time, frame) {
+        const pose = frame.getViewerPose(viewerSpace);
+        for(const view of pose.views) {
+          const callback = () => {
+            t.step(() => {
+              assert_throws_dom("InvalidStateError",
+                                () => isCpuOptimized ? frame.getDepthInformation(view)
+                                                     : glBinding.getDepthInformation(view),
+                                "getDepthInformation() should throw when ran outside RAF");
+            });
+            callbackCounter--;
+          }
+
+          t.step_timeout(callback, 10);
+          callbackCounter++;
+        }
+
+        callbacksKickedOff = true;
+      };
+
+      session.requestAnimationFrame(rafCb);
+
+      return t.step_wait(() => callbacksKickedOff && (callbackCounter == 0));
+    });
+  };
+};
diff --git a/webxr/depth-sensing/staleViewsTests.js b/webxr/depth-sensing/staleViewsTests.js
new file mode 100644
index 0000000..b1f11c9
--- /dev/null
+++ b/webxr/depth-sensing/staleViewsTests.js
@@ -0,0 +1,39 @@
+'use strict';
+
+const staleViewsTestFunctionGenerator = function(isCpuOptimized) {
+  return (session, controller, t, sessionObjects) => {
+    let done = false;
+
+    const staleViews = new Set();
+
+    return session.requestReferenceSpace('viewer').then((viewerSpace) => {
+      const glBinding = new XRWebGLBinding(session, sessionObjects.gl);
+
+      const secondRafCb = function(time, frame) {
+        for(const view of staleViews) {
+          t.step(() => {
+            assert_throws_dom("InvalidStateError",
+                                () => isCpuOptimized ? frame.getDepthInformation(view)
+                                                     : glBinding.getDepthInformation(view),
+                                "getDepthInformation() should throw when run with stale XRView");
+          });
+        }
+
+        done = true;
+      };
+
+      const firstRafCb = function(time, frame) {
+        const pose = frame.getViewerPose(viewerSpace);
+        for(const view of pose.views) {
+          staleViews.add(view);
+        }
+
+        session.requestAnimationFrame(secondRafCb);
+      };
+
+      session.requestAnimationFrame(firstRafCb);
+
+      return t.step_wait(() => done);
+    });
+  };
+};
\ No newline at end of file
diff --git a/webxr/resources/webxr_test_constants.js b/webxr/resources/webxr_test_constants.js
index 40643d0..7dbedd9 100644
--- a/webxr/resources/webxr_test_constants.js
+++ b/webxr/resources/webxr_test_constants.js
@@ -125,6 +125,7 @@
   'dom-overlay',
   'light-estimation',
   'anchors',
+  'depth-sensing',
 ];
 
 const TRACKED_IMMERSIVE_DEVICE = {
diff --git a/webxr/resources/webxr_test_constants_fake_depth.js b/webxr/resources/webxr_test_constants_fake_depth.js
new file mode 100644
index 0000000..36890d3
--- /dev/null
+++ b/webxr/resources/webxr_test_constants_fake_depth.js
@@ -0,0 +1,78 @@
+'use strict';
+
+// This file introduces constants used to mock depth data for depth sensing API.
+
+const convertDepthBufferToArrayBuffer = function (data, desiredFormat) {
+  if(desiredFormat == "luminance-alpha") {
+    const result = new ArrayBuffer(data.length * 2);  // each entry has 2 bytes
+    const view = new Uint16Array(result);
+
+    for(let i = 0; i < data.length; ++i) {
+      view[i] = data[i];
+    }
+
+    return new Uint8Array(result);
+  } else if(desiredFormat == "float32") {
+    const result = new ArrayBuffer(data.length * 4);  // each entry has 4 bytes
+    const view = new Float32Array(result);
+
+    for(let i = 0; i < data.length; ++i) {
+      view[i] = data[i];
+    }
+
+    return new Uint8Array(result);
+  } else {
+    throw new Error("Unrecognized data format!");
+  }
+}
+
+// Let's assume that the depth values are in cm, Xcm = x * 1/100m
+const RAW_VALUE_TO_METERS = 1/100;
+
+const createDepthSensingData = function() {
+  const depthSensingBufferHeight = 5;
+  const depthSensingBufferWidth = 7;
+  const depthSensingBuffer = [
+    1,  1,  1,   1,   1,    1,    1,  // first row
+    1,  2,  3,   4,   5,    6,    7,
+    1,  4,  9,  16,  25,   36,   49,
+    1,  8, 27,  64, 125,  216,  343,
+    1, 16, 81, 256, 625, 1296, 2401,
+  ];  // depthSensingBuffer value at column c, row r is Math.pow(c+1, r).
+
+  // Let's assume that the origin of the depth buffer is in the bottom right
+  // corner, with X's growing to the left and Y's growing upwards.
+  // This corresponds to the origin at 2401 in the above matrix, with X axis
+  // growing from 2401 towards 1296, and Y axis growing from 2401 towards 343.
+  // This corresponds to a rotation around Z axis by 180 degrees, with origin at [1,1].
+  const depthSensingBufferFromViewerTransform = {
+    position: [1, 1, 0],
+    orientation: [0, 0, 1, 0],
+  };
+
+  return {
+    depthData: convertDepthBufferToArrayBuffer(depthSensingBuffer, "luminance-alpha"),
+    width: depthSensingBufferWidth,
+    height: depthSensingBufferHeight,
+    normDepthBufferFromNormView: depthSensingBufferFromViewerTransform,
+    rawValueToMeters: RAW_VALUE_TO_METERS,
+  };
+};
+
+const DEPTH_SENSING_DATA = createDepthSensingData();
+
+// Returns expected depth value at |column|, |row| coordinates, expressed
+// in depth buffer's coordinate system.
+const getExpectedValueAt = function(column, row) {
+  return Math.pow(column+1, row) * RAW_VALUE_TO_METERS;
+};
+
+const VALID_DEPTH_CONFIG_CPU_USAGE = {
+  usagePreference: ['cpu-optimized'],
+  dataFormatPreference: ['luminance-alpha', 'float32'],
+};
+
+const VALID_DEPTH_CONFIG_GPU_USAGE = {
+  usagePreference: ['gpu-optimized'],
+  dataFormatPreference: ['luminance-alpha', 'float32'],
+};
diff --git a/webxr/resources/webxr_util.js b/webxr/resources/webxr_util.js
index 8ca7918..cf9c6ff 100644
--- a/webxr/resources/webxr_util.js
+++ b/webxr/resources/webxr_util.js
@@ -18,7 +18,7 @@
     // Perform any required test setup:
     xr_debug(name, 'setup');
 
-    assert_implements(navigator.xr, 'missing navigator.xr');
+    assert_implements(navigator.xr, 'missing navigator.xr - ensure test is run in a secure context.');
 
     // Only set up once.
     if (!navigator.xr.test) {
@@ -88,7 +88,8 @@
 // Calls the passed in test function with the session, the controller for the
 // device, and the test object.
 function xr_session_promise_test(
-    name, func, fakeDeviceInit, sessionMode, sessionInit, properties, glcontextPropertiesParam, gllayerPropertiesParam) {
+    name, func, fakeDeviceInit, sessionMode, sessionInit, properties,
+    glcontextPropertiesParam, gllayerPropertiesParam) {
   const glcontextProperties = (glcontextPropertiesParam) ? glcontextPropertiesParam : {};
   const gllayerProperties = (gllayerPropertiesParam) ? gllayerPropertiesParam : {};
 
@@ -133,7 +134,11 @@
                         });
                         sessionObjects.glLayer = glLayer;
                         xr_debug(name, 'session.visibilityState=' + session.visibilityState);
-                        resolve(func(session, testDeviceController, t, sessionObjects));
+                        try {
+                          resolve(func(session, testDeviceController, t, sessionObjects));
+                        } catch(err) {
+                          reject("Test function failed with: " + err);
+                        }
                       })
                       .catch((err) => {
                         xr_debug(name, 'error: ' + err);