Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandojsg committed Apr 22, 2017
1 parent dcd3fd7 commit 390efb5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 66 deletions.
12 changes: 2 additions & 10 deletions examples/showcase/tracked-controls/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
material="color: #F2E646;"></a-mixin>
</a-assets>
<!-- Hands -->
<a-entity id="left" oculus-touch-controls="hand: left" vive-controls="hand: left" aabb-collider="objects: .cube;" grab></a-entity>
<a-entity id="right" oculus-touch-controls="hand: right" vive-controls="hand: right" aabb-collider="objects: .cube;" grab></a-entity>
<a-entity hand-controls="left" aabb-collider="objects: .cube;" grab></a-entity>
<a-entity hand-controls="right" aabb-collider="objects: .cube;" grab></a-entity>

<!-- A-Frame cubes -->
<a-entity position="0 0 -0.5">
Expand Down Expand Up @@ -57,13 +57,5 @@
<a-entity light="type: ambient; color: #f4f4f4; intensity: 0.4;" position="-8 10 -18"></a-entity>
</a-entity>
</a-scene>
<script>
document.getElementById('left').addEventListener('controllerconnected', function(evt){
console.log('left', evt);
})
document.getElementById('right').addEventListener('controllerconnected', function(evt){
console.log('right',evt);
});
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions src/components/gearvr-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
1 change: 0 additions & 1 deletion src/components/oculus-touch-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 13 additions & 13 deletions src/utils/tracked-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 6 additions & 17 deletions tests/components/oculus-touch-controls.test.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
});
});
Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
53 changes: 28 additions & 25 deletions tests/components/vive-controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 390efb5

Please sign in to comment.