CB-11482 Fix unreliable tests on Android This closes #46
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/commit/fe7b2139 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/tree/fe7b2139 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/diff/fe7b2139 Branch: refs/heads/1.2.x Commit: fe7b2139966740af7852ca403dbdb9c724059f72 Parents: 293dcb9 Author: Vladimir Kotikov <[email protected]> Authored: Thu Jul 7 11:08:42 2016 +0300 Committer: Steve Gill <[email protected]> Committed: Thu Aug 25 15:54:25 2016 -0700 ---------------------------------------------------------------------- src/android/AccelListener.java | 10 ++++++---- tests/tests.js | 24 ++++++++++++++---------- 2 files changed, 20 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/fe7b2139/src/android/AccelListener.java ---------------------------------------------------------------------- diff --git a/src/android/AccelListener.java b/src/android/AccelListener.java index 3932c0f..61856fb 100755 --- a/src/android/AccelListener.java +++ b/src/android/AccelListener.java @@ -148,9 +148,6 @@ public class AccelListener extends CordovaPlugin implements SensorEventListener this.setStatus(AccelListener.STARTING); - // CB-11531: Reset accuracy to the default level - this.accuracy = SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM; - // Get accelerometer from sensor manager List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); @@ -159,6 +156,9 @@ public class AccelListener extends CordovaPlugin implements SensorEventListener this.mSensor = list.get(0); if (this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_UI)) { this.setStatus(AccelListener.STARTING); + // CB-11531: Mark accuracy as 'reliable' - this is complementary to + // setting it to 'unreliable' 'stop' method + this.accuracy = SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM; } else { this.setStatus(AccelListener.ERROR_FAILED_TO_START); this.fail(AccelListener.ERROR_FAILED_TO_START, "Device sensor returned an error."); @@ -204,8 +204,10 @@ public class AccelListener extends CordovaPlugin implements SensorEventListener * Called two seconds after starting the listener. */ private void timeout() { - if (this.status == AccelListener.STARTING) { + if (this.status == AccelListener.STARTING && + this.accuracy >= SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM) { // call win with latest cached position + // but first check if cached position is reliable this.timestamp = System.currentTimeMillis(); this.win(); } http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/fe7b2139/tests/tests.js ---------------------------------------------------------------------- diff --git a/tests/tests.js b/tests/tests.js index b782629..5deec41 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -123,8 +123,13 @@ exports.defineAutoTests = function () { describe("watchAcceleration", function() { var id; - afterEach(function() { - navigator.accelerometer.clearWatch(id); + afterEach(function(done) { + if (id) { + navigator.accelerometer.clearWatch(id); + } + // clearWatch implementation is async but doesn't accept a cllback + // so let's give it some time before starting next spec + setTimeout(done, 100); }); it("accelerometer.spec.6 should exist", function() { @@ -178,15 +183,11 @@ exports.defineAutoTests = function () { pending(); } var veryRecently = (new Date()).getTime(); - var ACCEPTABLE_PERCENT_RANGE = 95; // Need to check that dates returned are not vastly greater than a recent time stamp. // In case the timestamps returned are ridiculously high var reasonableTimeLimit = veryRecently + 5000; // 5 seconds from now var win = function(a) { - // Checking if the returned timestamp is atleast 95% of the veryRecently timestamp - // If it is greater than very recently (for eg: 125%) it is fine and we do not want - // a positive delta comparison in this assert - expect((a.timestamp * 100) / veryRecently).toBeGreaterThan(ACCEPTABLE_PERCENT_RANGE); + expect(a.timestamp).toBeGreaterThan(veryRecently); expect(a.timestamp).toBeLessThan(reasonableTimeLimit); done(); }; @@ -291,6 +292,7 @@ exports.defineManualTests = function (contentEl, createActionButton) { document.getElementById('x').innerHTML = roundNumber(a.x); document.getElementById('y').innerHTML = roundNumber(a.y); document.getElementById('z').innerHTML = roundNumber(a.z); + document.getElementById('t').innerHTML = a.timestamp; }; // Fail callback @@ -322,6 +324,7 @@ exports.defineManualTests = function (contentEl, createActionButton) { document.getElementById('x').innerHTML = roundNumber(a.x); document.getElementById('y').innerHTML = roundNumber(a.y); document.getElementById('z').innerHTML = roundNumber(a.z); + document.getElementById('t').innerHTML = a.timestamp; console.log("getAccel success callback"); }; @@ -348,9 +351,10 @@ exports.defineManualTests = function (contentEl, createActionButton) { contentEl.innerHTML = '<div id="info">' + 'Status: <span id="accel_status">Stopped</span>' + '<table width="100%">' + - '<tr><td width="20%">X:</td><td id="x"> </td></tr>' + - '<tr><td width="20%">Y:</td><td id="y"> </td></tr>' + - '<tr><td width="20%">Z:</td><td id="z"> </td></tr>' + + '<tr><td width="30%">X:</td><td id="x"> </td></tr>' + + '<tr><td width="30%">Y:</td><td id="y"> </td></tr>' + + '<tr><td width="30%">Z:</td><td id="z"> </td></tr>' + + '<tr><td width="30%">Timestamp:</td><td id="t"> </td></tr>' + '</table></div>' + accelerometer_tests; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
