Commit 534b1a0f authored by Alex Cooper's avatar Alex Cooper Committed by Commit Bot

Update webxr_test_asserts and align xr layout_tests usage of asserts

Updates webxr_test_asserts with chromium's more verbose asserts for
easier debugging of tests, and removes the asserts from xr-test-utils
so that all asserts can be referenced from webxr_test_asserts.

Bug: 985156
Change-Id: Ie8d2ca70dd18a7e1759fe2b4d83b02076fcf91c0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1715916
Commit-Queue: Alexander Cooper <alcooper@chromium.org>
Auto-Submit: Alexander Cooper <alcooper@chromium.org>
Reviewed-by: default avatarKlaus Weidner <klausw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#680180}
parent 5695e8e2
......@@ -6,19 +6,107 @@
// |epsilon| - float specifying precision
// |prefix| - string used as a prefix for logging
let assert_point_approx_equals = function(p1, p2, epsilon = FLOAT_EPSILON, prefix = "") {
assert_approx_equals(p1.x, p2.x, epsilon, prefix + "xs must match");
assert_approx_equals(p1.y, p2.y, epsilon, prefix + "ys must match");
assert_approx_equals(p1.z, p2.z, epsilon, prefix + "zs must match");
assert_approx_equals(p1.w, p2.w, epsilon, prefix + "ws must match");
if (p1 == null && p2 == null) {
return;
}
assert_not_equals(p1, null, prefix + "p1 must be non-null");
assert_not_equals(p2, null, prefix + "p2 must be non-null");
let mismatched_component = null;
for (const v of ['x', 'y', 'z', 'w']) {
if (Math.abs(p1[v] - p2[v]) > epsilon) {
mismatched_component = v;
break;
}
}
if (mismatched_component !== null) {
let error_message = prefix + ' Point comparison failed.\n';
error_message += ` p1: {x: ${p1.x}, y: ${p1.y}, z: ${p1.z}, w: ${p1.w}}\n`;
error_message += ` p2: {x: ${p2.x}, y: ${p2.y}, z: ${p2.z}, w: ${p2.w}}\n`;
error_message += ` Difference in component ${mismatched_component} exceeded the given epsilon.\n`;
assert_approx_equals(p2[mismatched_component], p1[mismatched_component], epsilon, error_message);
}
};
// |m1|, |m2| - arrays of floating point numbers
// |epsilon| - float specifying precision
// |prefix| - string used as a prefix for logging
let assert_matrix_approx_equals = function(m1, m2, epsilon = FLOAT_EPSILON, prefix = "") {
assert_equals(m1.length, m2.length, prefix + "Matrix lengths should match");
for(var i = 0; i < m1.length; ++i) {
assert_approx_equals(m1[i], m2[i], epsilon, prefix + "Component number " + i + " should match");
if (m1 == null && m2 == null) {
return;
}
assert_not_equals(m1, null, prefix + "m1 must be non-null");
assert_not_equals(m2, null, prefix + "m2 must be non-null");
assert_equals(m1.length, 16, prefix + "m1 must have length of 16");
assert_equals(m2.length, 16, prefix + "m2 must have length of 16");
let mismatched_element = -1;
for (let i = 0; i < 16; ++i) {
if (Math.abs(m1[i] - m2[i]) > epsilon) {
mismatched_element = i;
break;
}
}
if (mismatched_element > -1) {
let error_message = prefix + 'Matrix comparison failed.\n';
error_message += ' Difference in element ' + mismatched_element +
' exceeded the given epsilon.\n';
error_message += ' Matrix 1: [' + m1.join(',') + ']\n';
error_message += ' Matrix 2: [' + m2.join(',') + ']\n';
assert_approx_equals(
m1[mismatched_element], m2[mismatched_element], epsilon,
error_message);
}
}
// |m1|, |m2| - arrays of floating point numbers
// |epsilon| - float specifying precision
// |prefix| - string used as a prefix for logging
let assert_matrix_significantly_not_equals = function(m1, m2, epsilon = FLOAT_EPSILON, prefix = "") {
if (m1 == null && m2 == null) {
return;
}
assert_not_equals(m1, null, prefix + "m1 must be non-null");
assert_not_equals(m2, null, prefix + "m2 must be non-null");
assert_equals(m1.length, 16, prefix + "m1 must have length of 16");
assert_equals(m2.length, 16, prefix + "m2 must have length of 16");
let mismatch = false;
for (let i = 0; i < 16; ++i) {
if (Math.abs(m1[i] - m2[i]) > epsilon) {
mismatch = true;
break;
}
}
if (!mismatch) {
let m1_str = '[';
let m2_str = '[';
for (let i = 0; i < 16; ++i) {
m1_str += m1[i] + (i < 15 ? ', ' : '');
m2_str += m2[i] + (i < 15 ? ', ' : '');
}
m1_str += ']';
m2_str += ']';
let error_message = prefix + 'Matrix comparison failed.\n';
error_message +=
' No element exceeded the given epsilon ' + epsilon + '.\n';
error_message += ' Matrix A: ' + m1_str + '\n';
error_message += ' Matrix B: ' + m2_str + '\n';
assert_unreached(error_message);
}
}
......
......@@ -7,6 +7,7 @@
<script src="file:///gen/device/vr/public/mojom/vr_service.mojom.js"></script>
<script src="../external/wpt/resources/chromium/webxr-test.js"></script>
<script src="../external/wpt/webxr/resources/webxr_test_constants.js"></script>
<script src="../external/wpt/webxr/resources/webxr_test_asserts.js"></script>
<script src="../xr/resources/xr-internal-device-mocking.js"></script>
<script src="../xr/resources/xr-test-utils.js"></script>
<canvas />
......@@ -42,7 +43,7 @@ let testFunction = function(session, fakeDeviceController, t) {
// Test that hit results are what we expected.
assert_equals(hitResults.length, 1);
assert_equals(hitResults[0].hitMatrix.length, 16);
assert_matrices_approx_equal(
assert_matrix_approx_equals(
hitResults[0].hitMatrix,
expectedHitMatrix,
FLOAT_EPSILON,
......
......@@ -75,136 +75,3 @@ function xr_session_promise_test(
}));
}, name, properties);
}
function perspectiveFromFieldOfView(fov, near, far) {
let upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
let downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
let leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
let rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
let xScale = 2.0 / (leftTan + rightTan);
let yScale = 2.0 / (upTan + downTan);
let nf = 1.0 / (near - far);
let out = new Float32Array(16);
out[0] = xScale;
out[1] = 0.0;
out[2] = 0.0;
out[3] = 0.0;
out[4] = 0.0;
out[5] = yScale;
out[6] = 0.0;
out[7] = 0.0;
out[8] = -((leftTan - rightTan) * xScale * 0.5);
out[9] = ((upTan - downTan) * yScale * 0.5);
out[10] = (near + far) * nf;
out[11] = -1.0;
out[12] = 0.0;
out[13] = 0.0;
out[14] = (2.0 * far * near) * nf;
out[15] = 0.0;
return out;
}
function assert_points_approx_equal(actual, expected, epsilon = FLOAT_EPSILON, message = '') {
if (expected == null && actual == null) {
return;
}
assert_not_equals(expected, null);
assert_not_equals(actual, null);
let mismatched_component = null;
for (const v of ['x', 'y', 'z', 'w']) {
if (Math.abs(expected[v] - actual[v]) > epsilon) {
mismatched_component = v;
break;
}
}
if (mismatched_component !== null) {
let error_message = message ? message + '\n' : 'Point comparison failed.\n';
error_message += ` Expected: {x: ${expected.x}, y: ${expected.y}, z: ${expected.z}, w: ${expected.w}}\n`;
error_message += ` Actual: {x: ${actual.x}, y: ${actual.y}, z: ${actual.z}, w: ${actual.w}}\n`;
error_message += ` Difference in component ${mismatched_component} exceeded the given epsilon.\n`;
assert_approx_equals(actual[mismatched_component], expected[mismatched_component], epsilon, error_message);
}
}
function assert_matrices_approx_equal(
matA, matB, epsilon = FLOAT_EPSILON, message = '') {
if (matA == null && matB == null) {
return;
}
assert_not_equals(matA, null);
assert_not_equals(matB, null);
assert_equals(matA.length, 16);
assert_equals(matB.length, 16);
let mismatched_element = -1;
for (let i = 0; i < 16; ++i) {
if (Math.abs(matA[i] - matB[i]) > epsilon) {
mismatched_element = i;
break;
}
}
if (mismatched_element > -1) {
let error_message =
message ? message + '\n' : 'Matrix comparison failed.\n';
error_message += ' Difference in element ' + mismatched_element +
' exceeded the given epsilon.\n';
error_message += ' Matrix A: [' + matA.join(',') + ']\n';
error_message += ' Matrix B: [' + matB.join(',') + ']\n';
assert_approx_equals(
matA[mismatched_element], matB[mismatched_element], epsilon,
error_message);
}
}
function assert_matrices_significantly_not_equal(
matA, matB, epsilon = FLOAT_EPSILON, message = '') {
if (matA == null && matB == null) {
return;
}
assert_not_equals(matA, null);
assert_not_equals(matB, null);
assert_equals(matA.length, 16);
assert_equals(matB.length, 16);
let mismatch = false;
for (let i = 0; i < 16; ++i) {
if (Math.abs(matA[i] - matB[i]) > epsilon) {
mismatch = true;
break;
}
}
if (!mismatch) {
let matA_str = '[';
let matB_str = '[';
for (let i = 0; i < 16; ++i) {
matA_str += matA[i] + (i < 15 ? ', ' : '');
matB_str += matB[i] + (i < 15 ? ', ' : '');
}
matA_str += ']';
matB_str += ']';
let error_message =
message ? message + '\n' : 'Matrix comparison failed.\n';
error_message +=
' No element exceeded the given epsilon ' + epsilon + '.\n';
error_message += ' Matrix A: ' + matA_str + '\n';
error_message += ' Matrix B: ' + matB_str + '\n';
assert_unreached(error_message);
}
}
......@@ -7,6 +7,7 @@
<script src="file:///gen/device/vr/public/mojom/vr_service.mojom.js"></script>
<script src="../external/wpt/resources/chromium/webxr-test.js"></script>
<script src="../external/wpt/webxr/resources/webxr_test_constants.js"></script>
<script src="../external/wpt/webxr/resources/webxr_test_asserts.js"></script>
<script src="../xr/resources/xr-internal-device-mocking.js"></script>
<script src="../xr/resources/xr-test-utils.js"></script>
<canvas />
......@@ -41,7 +42,7 @@ let testFunction = function(session, fakeDeviceController, t) {
assert_not_equals(pose, null);
let poseMatrix = pose.transform.matrix;
assert_matrices_approx_equal(poseMatrix, VALID_FLOOR_ORIGIN_MATRIX);
assert_matrix_approx_equals(poseMatrix, VALID_FLOOR_ORIGIN_MATRIX);
// If an explicit array of bounds points was not provided then the
// bounds geometry should represent the four corners of the rectangle
......
......@@ -7,6 +7,7 @@
<script src="file:///gen/device/vr/public/mojom/vr_service.mojom.js"></script>
<script src="../external/wpt/resources/chromium/webxr-test.js"></script>
<script src="../external/wpt/webxr/resources/webxr_test_constants.js"></script>
<script src="../external/wpt/webxr/resources/webxr_test_asserts.js"></script>
<script src="../xr/resources/xr-test-utils.js"></script>
<canvas />
......@@ -53,7 +54,7 @@ let testFunction = function(session, fakeDeviceController, t) {
assert_false(pose2 instanceof XRViewerPose);
assert_not_equals(pose2.transform, null);
assert_not_equals(pose2.transform.matrix, null);
assert_matrices_approx_equal(pose.transform.matrix, pose2.transform.matrix);
assert_matrix_approx_equals(pose.transform.matrix, pose2.transform.matrix);
}
// Ensure that two views are provided.
......@@ -72,8 +73,8 @@ let testFunction = function(session, fakeDeviceController, t) {
assert_not_equals(leftView.projectionMatrix, null);
assert_not_equals(rightView.projectionMatrix, null);
assert_matrices_approx_equal(leftView.projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrices_approx_equal(rightView.projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrix_approx_equals(leftView.projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrix_approx_equals(rightView.projectionMatrix, VALID_PROJECTION_MATRIX);
// Finished test.
resolve();
......
......@@ -7,6 +7,7 @@
<script src="file:///gen/device/vr/public/mojom/vr_service.mojom.js"></script>
<script src="../external/wpt/resources/chromium/webxr-test.js"></script>
<script src="../external/wpt/webxr/resources/webxr_test_constants.js"></script>
<script src="../external/wpt/webxr/resources/webxr_test_asserts.js"></script>
<script src="../xr/resources/xr-test-utils.js"></script>
<canvas />
......@@ -47,8 +48,8 @@ let testFunction = function(session, fakeDeviceController, t) {
if (counter == 0) {
session.requestAnimationFrame(onFrame);
assert_matrices_approx_equal(pose.views[0].projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrices_approx_equal(pose.views[1].projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrix_approx_equals(pose.views[0].projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrix_approx_equals(pose.views[1].projectionMatrix, VALID_PROJECTION_MATRIX);
// Update the near and far depths for the session.
session.updateRenderState({
......@@ -57,8 +58,8 @@ let testFunction = function(session, fakeDeviceController, t) {
// The projection matrices the views report should not take into
// account the new session depth values this frame.
assert_matrices_approx_equal(pose.views[0].projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrices_approx_equal(pose.views[1].projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrix_approx_equals(pose.views[0].projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrix_approx_equals(pose.views[1].projectionMatrix, VALID_PROJECTION_MATRIX);
} else {
// New depth values should be retained between frames.
assert_equals(session.renderState.depthNear, 1.0);
......@@ -66,8 +67,8 @@ let testFunction = function(session, fakeDeviceController, t) {
// Projection matricies should now reflect the new depth values, i.e.
// have changed.
assert_matrices_significantly_not_equal(pose.views[0].projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrices_significantly_not_equal(pose.views[1].projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrix_significantly_not_equals(pose.views[0].projectionMatrix, VALID_PROJECTION_MATRIX);
assert_matrix_significantly_not_equals(pose.views[1].projectionMatrix, VALID_PROJECTION_MATRIX);
resolve();
}
counter++;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment