blob: db44c524449dfaaccd170f81619ede3cdd872f4a [file] [log] [blame]
<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<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="../xr/resources/xr-internal-device-mocking.js"></script>
<script src="../xr/resources/xr-test-utils.js"></script>
<canvas id="webgl-canvas"></canvas>
<script>
let testName = "Input sources are re-created when handedness or target ray mode changes";
let watcherDone = new Event("watcherdone");
let fakeDeviceInitParams = { supportsImmersive:true };
let requestSessionModes = ['immersive-vr'];
let testFunction = function(session, t, fakeDeviceController) {
let eventWatcher = new EventWatcher(t, session, ["watcherdone"]);
let eventPromise = eventWatcher.wait_for(["watcherdone"]);
// Need to have a valid pose or input events don't process.
fakeDeviceController.setXRPresentationFrameData(VALID_POSE_MATRIX, [{
eye:"left",
projectionMatrix: VALID_PROJECTION_MATRIX,
viewMatrix: VALID_VIEW_MATRIX
}, {
eye:"right",
projectionMatrix: VALID_PROJECTION_MATRIX,
viewMatrix: VALID_VIEW_MATRIX
}]);
let inputChangeEvents = 0;
let cached_input_source = null;
function onInputSourcesChange(event) {
t.step(() => {
inputChangeEvents++;
assert_equals(event.session, session);
if (inputChangeEvents == 1) {
// The first change event should be adding our controller.
validateAdded(event.added, 1);
validateRemoved(event.removed, 0);
cached_input_source = getInputSources()[0];
assert_not_equals(cached_input_source, null);
assert_equals(cached_input_source.handedness, "none");
assert_equals(cached_input_source.targetRayMode, "gaze");
} else if (inputChangeEvents == 2) {
// The second event should be replacing the controller with one that has
// the updated target ray mode.
validateInputSourceChange(event, "none", "tracked-pointer");
cached_input_source = getInputSources()[0];
} else if (inputChangeEvents == 3) {
// The third event should be replacing the controller with one that has
// the updated handedness.
validateInputSourceChange(event, "left", "tracked-pointer");
session.dispatchEvent(watcherDone);
}
});
}
function validateInputSourceChange(event, expected_hand, expected_mode) {
validateAdded(event.added, 1);
validateRemoved(event.removed, 1);
assert_true(event.removed.includes(cached_input_source));
assert_false(event.added.includes(cached_input_source));
let source = event.added[0];
assert_equals(source.handedness, expected_hand);
assert_equals(source.targetRayMode, expected_mode);
}
function validateAdded(added, length) {
t.step(() => {
assert_not_equals(added, null);
assert_equals(added.length, length,
"Added length matches expectations");
let currentSources = getInputSources();
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 = getInputSources();
removed.forEach((source) => {
assert_false(currentSources.includes(source),
"No element in removed should be in the input source list");
});
});
}
function getInputSources() {
return Array.from(session.inputSources.values());
}
session.addEventListener('inputsourceschange', onInputSourcesChange, false);
// Session must have a baseLayer or frame requests will be ignored.
session.updateRenderState({ baseLayer: new XRWebGLLayer(session, gl) });
// 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 = new MockXRInputSource();
fakeDeviceController.addInputSource(input_source);
// Make our input source change after one frame, and wait an additional
// frame for that change to propogate.
session.requestAnimationFrame((time, xrFrame) => {
input_source.targetRayMode = "tracked-pointer";
session.requestAnimationFrame((time, xrFrame) => {
input_source.handedness = "left";
session.requestAnimationFrame((time, xrFrame) => {});
});
});
return eventPromise;
};
xr_session_promise_test(
testFunction, fakeDeviceInitParams, requestSessionModes, testName);
</script>