diff --git a/examples/showcase/tracked-controls/index.html b/examples/showcase/tracked-controls/index.html index 255f97d174c..1500ae805a1 100644 --- a/examples/showcase/tracked-controls/index.html +++ b/examples/showcase/tracked-controls/index.html @@ -22,8 +22,8 @@ material="color: #F2E646;"> - - + + @@ -57,13 +57,5 @@ - diff --git a/src/components/gearvr-controls.js b/src/components/gearvr-controls.js index 5a4f10a7025..b9db615bb0f 100644 --- a/src/components/gearvr-controls.js +++ b/src/components/gearvr-controls.js @@ -98,9 +98,12 @@ module.exports.Component = registerComponent('gearvr-controls', { this.checkIfControllerPresent(); // Note that due to gamepadconnected event propagation issues, we don't rely on events. window.addEventListener('gamepaddisconnected', this.checkIfControllerPresent, false); + this.addControllersUpdateListener(); }, pause: function () { + this.removeEventListeners(); + this.removeControllersUpdateListener(); // Note that due to gamepadconnected event propagation issues, we don't rely on events. window.removeEventListener('gamepaddisconnected', this.checkIfControllerPresent, false); }, diff --git a/src/components/oculus-touch-controls.js b/src/components/oculus-touch-controls.js index fe4f95f254a..31d208e06e5 100644 --- a/src/components/oculus-touch-controls.js +++ b/src/components/oculus-touch-controls.js @@ -109,7 +109,6 @@ module.exports.Component = registerComponent('oculus-touch-controls', { }, pause: function () { - this.removeControllersUpdateListener(); this.removeEventListeners(); this.removeControllersUpdateListener(); // Note that due to gamepadconnected event propagation issues, we don't rely on events. diff --git a/src/utils/tracked-controls.js b/src/utils/tracked-controls.js index f23e59192e3..3223ca56066 100644 --- a/src/utils/tracked-controls.js +++ b/src/utils/tracked-controls.js @@ -75,20 +75,20 @@ function isControllerPresent (component, idPrefix, queryObject) { gamepads = trackedControlsSystem.controllers; } - if (gamepads) { - for (var i = 0; i < gamepads.length; ++i) { - gamepad = gamepads[i]; - isPrefixMatch = (!idPrefix || idPrefix === '' || gamepad.id.indexOf(idPrefix) === 0); - isPresent = isPrefixMatch; - if (isPresent && queryObject.hand) { - isPresent = (gamepad.hand || DEFAULT_HANDEDNESS) === queryObject.hand; - } - if (isPresent && queryObject.index) { - isPresent = index === queryObject.index; // need to use count of gamepads with idPrefix - } - if (isPresent) { break; } - if (isPrefixMatch) { index++; } // update count of gamepads with idPrefix + if (!gamepads) { return isPresent; } + + for (var i = 0; i < gamepads.length; ++i) { + gamepad = gamepads[i]; + isPrefixMatch = (!idPrefix || idPrefix === '' || gamepad.id.indexOf(idPrefix) === 0); + isPresent = isPrefixMatch; + if (isPresent && queryObject.hand) { + isPresent = (gamepad.hand || DEFAULT_HANDEDNESS) === queryObject.hand; + } + if (isPresent && queryObject.index) { + isPresent = index === queryObject.index; // need to use count of gamepads with idPrefix } + if (isPresent) { break; } + if (isPrefixMatch) { index++; } // update count of gamepads with idPrefix } return isPresent; diff --git a/tests/components/oculus-touch-controls.test.js b/tests/components/oculus-touch-controls.test.js index 9146da013d2..7d8f3401879 100644 --- a/tests/components/oculus-touch-controls.test.js +++ b/tests/components/oculus-touch-controls.test.js @@ -1,7 +1,7 @@ /* global assert, process, setup, suite, test, CustomEvent, Event */ var entityFactory = require('../helpers').entityFactory; -suite.only('oculus-touch-controls', function () { +suite('oculus-touch-controls', function () { var el; var component; @@ -10,6 +10,7 @@ suite.only('oculus-touch-controls', function () { el.setAttribute('oculus-touch-controls', ''); el.addEventListener('loaded', function () { component = el.components['oculus-touch-controls']; + component.controllersWhenPresent = [{id: 'Oculus Touch', index: 0, hand: 'left', pose: {}}]; done(); }); }); @@ -20,9 +21,6 @@ suite.only('oculus-touch-controls', function () { var injectTrackedControlsSpy = this.sinon.spy(component, 'injectTrackedControls'); var removeEventListenersSpy = this.sinon.spy(component, 'removeEventListeners'); - // Mock isControllerPresent to return false. - this.sinon.stub(component, 'isControllerPresent', () => false); - // Mock has not been checked previously. delete component.controllerPresent; @@ -39,9 +37,6 @@ suite.only('oculus-touch-controls', function () { var injectTrackedControlsSpy = this.sinon.spy(component, 'injectTrackedControls'); var removeEventListenersSpy = this.sinon.spy(component, 'removeEventListeners'); - // Mock isControllerPresent to return false. - this.sinon.stub(component, 'isControllerPresent', () => false); - // Mock that it's been checked previously. component.controllerPresent = false; @@ -59,7 +54,7 @@ suite.only('oculus-touch-controls', function () { var removeEventListenersSpy = this.sinon.spy(component, 'removeEventListeners'); // Mock isControllerPresent to return true. - this.sinon.stub(component, 'isControllerPresent', () => true); + el.sceneEl.systems['tracked-controls'].controllers = component.controllersWhenPresent; // Mock that it's never been checked previously. delete component.controllerPresent; @@ -78,7 +73,7 @@ suite.only('oculus-touch-controls', function () { var removeEventListenersSpy = this.sinon.spy(component, 'removeEventListeners'); // Mock isControllerPresent to return true. - this.sinon.stub(component, 'isControllerPresent', () => true); + el.sceneEl.systems['tracked-controls'].controllers = component.controllersWhenPresent; // Mock that it's was currently present. component.controllerPresent = true; @@ -96,7 +91,6 @@ suite.only('oculus-touch-controls', function () { var injectTrackedControlsSpy = this.sinon.spy(component, 'injectTrackedControls'); // Mock isControllerPresent to return true. - this.sinon.stub(component, 'isControllerPresent', () => false); // Mock that it's was currently present. component.controllerPresent = true; @@ -112,8 +106,7 @@ suite.only('oculus-touch-controls', function () { suite('axismove', function () { test('can emit thumbstick moved', function (done) { var evt; - // Mock isControllerPresent to return true. - this.sinon.stub(component, 'isControllerPresent', () => true); + el.sceneEl.systems['tracked-controls'].controllers = component.controllersWhenPresent; // Do the check. component.checkIfControllerPresent(); // Install event handler listening for thumbstickmoved. @@ -130,8 +123,7 @@ suite.only('oculus-touch-controls', function () { test('does not emit thumbstickmoved if axismove has no changes', function (done) { var evt; - // Mock isControllerPresent to return true. - this.sinon.stub(component, 'isControllerPresent', () => true); + el.sceneEl.systems['tracked-controls'].controllers = component.controllersWhenPresent; // Do the check. component.checkIfControllerPresent(); // Install event handler listening for thumbstickmoved. @@ -148,8 +140,6 @@ suite.only('oculus-touch-controls', function () { suite('buttonchanged', function () { test('can emit triggerchanged', function (done) { var evt; - // Mock isControllerPresent to return true. - this.sinon.stub(component, 'isControllerPresent', () => true); // Do the check. component.checkIfControllerPresent(); // Install event handler listening for triggerchanged. @@ -178,7 +168,6 @@ suite.only('oculus-touch-controls', function () { component.pause(); component.play(); // Mock isControllerPresent to return false. - this.sinon.stub(component, 'isControllerPresent', () => false); // Reset everGotGamepadEvent so we don't think we've looked before. delete component.everGotGamepadEvent; // Fire emulated gamepaddisconnected event. diff --git a/tests/components/vive-controls.test.js b/tests/components/vive-controls.test.js index dcf02d6bedc..8e383b7355d 100644 --- a/tests/components/vive-controls.test.js +++ b/tests/components/vive-controls.test.js @@ -5,31 +5,30 @@ var controllerComponentName = 'vive-controls'; suite(controllerComponentName, function () { setup(function (done) { var el = this.el = entityFactory(); - el.setAttribute(controllerComponentName, ''); + el.setAttribute(controllerComponentName, 'hand: right'); // to ensure index = 0 el.addEventListener('loaded', function () { var controllerComponent = el.components[controllerComponentName]; - controllerComponent.isControllerPresent = function () { return controllerComponent.isControllerPresentMockValue; }; + controllerComponent.controllersWhenPresent = [{id: 'OpenVR Gamepad', index: 0, hand: 'right', pose: {}}]; done(); }); }); suite('checkIfControllerPresent', function () { - test('first-time, if no controllers, remove event listeners and remember not present', function () { + test('first-time, if no controllers, remember not present', function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); - var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); - // mock isControllerPresent to return false - controllerComponent.isControllerPresentMockValue = false; + + el.sceneEl.systems['tracked-controls'].controllers = []; + // reset so we don't think we've looked before - delete controllerComponent.controllerPresent; + controllerComponent.controllerPresent = false; // do the check controllerComponent.checkIfControllerPresent(); // check assertions assert.notOk(injectTrackedControlsSpy.called); assert.notOk(addEventListenersSpy.called); - assert.ok(removeEventListenersSpy.called); assert.ok(controllerComponent.controllerPresent === false); // not undefined }); @@ -39,8 +38,9 @@ suite(controllerComponentName, function () { var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); - // mock isControllerPresent to return false - controllerComponent.isControllerPresentMockValue = false; + + el.sceneEl.systems['tracked-controls'].controllers = []; + // pretend we've looked before controllerComponent.controllerPresent = false; // do the check @@ -58,10 +58,11 @@ suite(controllerComponentName, function () { var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); - // mock isControllerPresent to return true - controllerComponent.isControllerPresentMockValue = true; + + el.sceneEl.systems['tracked-controls'].controllers = controllerComponent.controllersWhenPresent; + // reset so we don't think we've looked before - delete controllerComponent.controllerPresent; + controllerComponent.controllerPresent = false; // do the check controllerComponent.checkIfControllerPresent(); // check assertions @@ -77,8 +78,9 @@ suite(controllerComponentName, function () { var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); - // mock isControllerPresent to return true - controllerComponent.isControllerPresentMockValue = true; + + el.sceneEl.systems['tracked-controls'].controllers = controllerComponent.controllersWhenPresent; + // pretend we've looked before controllerComponent.controllerPresent = true; // do the check @@ -96,8 +98,9 @@ suite(controllerComponentName, function () { var addEventListenersSpy = this.sinon.spy(controllerComponent, 'addEventListeners'); var injectTrackedControlsSpy = this.sinon.spy(controllerComponent, 'injectTrackedControls'); var removeEventListenersSpy = this.sinon.spy(controllerComponent, 'removeEventListeners'); - // mock isControllerPresent to return true - controllerComponent.isControllerPresentMockValue = false; + + el.sceneEl.systems['tracked-controls'].controllers = []; + // pretend we've looked before controllerComponent.controllerPresent = true; // do the check @@ -116,8 +119,8 @@ suite(controllerComponentName, function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; var evt; - // mock isControllerPresent to return true - controllerComponent.isControllerPresentMockValue = true; + + el.sceneEl.systems['tracked-controls'].controllers = controllerComponent.controllersWhenPresent; // do the check controllerComponent.checkIfControllerPresent(); // install event handler listening for thumbstickmoved @@ -136,8 +139,8 @@ suite(controllerComponentName, function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; var evt; - // mock isControllerPresent to return true - controllerComponent.isControllerPresentMockValue = true; + + el.sceneEl.systems['tracked-controls'].controllers = controllerComponent.controllersWhenPresent; // do the check controllerComponent.checkIfControllerPresent(); // install event handler listening for thumbstickmoved @@ -159,8 +162,8 @@ suite(controllerComponentName, function () { var el = this.el; var controllerComponent = el.components[controllerComponentName]; var evt; - // mock isControllerPresent to return true - controllerComponent.isControllerPresentMockValue = true; + + el.sceneEl.systems['tracked-controls'].controllers = controllerComponent.controllersWhenPresent; // do the check controllerComponent.checkIfControllerPresent(); // install event handler listening for triggerchanged @@ -187,8 +190,8 @@ suite(controllerComponentName, function () { controllerComponent.checkIfControllerPresent = controllerComponent.checkIfControllerPresent.bind(controllerComponent); controllerComponent.pause(); controllerComponent.play(); - // mock isControllerPresent to return false - controllerComponent.isControllerPresentMockValue = false; + + el.sceneEl.systems['tracked-controls'].controllers = []; // reset everGotGamepadEvent so we don't think we've looked before delete controllerComponent.everGotGamepadEvent; // fire emulated gamepaddisconnected event