Updated Branches: refs/heads/master ce5fb8633 -> 34b767dd2
[CB-763] Added better accelerometer autotests Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-mobile-spec/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-mobile-spec/commit/34b767dd Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-mobile-spec/tree/34b767dd Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-mobile-spec/diff/34b767dd Branch: refs/heads/master Commit: 34b767dd2d11cde46d2e9b5e351c0093ab2afbdc Parents: ce5fb86 Author: Fil Maj <maj....@gmail.com> Authored: Mon May 14 16:07:47 2012 -0700 Committer: Fil Maj <maj....@gmail.com> Committed: Mon May 14 16:07:47 2012 -0700 ---------------------------------------------------------------------- autotest/tests/accelerometer.tests.js | 233 +++++++++++++++++++++------- 1 files changed, 180 insertions(+), 53 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-mobile-spec/blob/34b767dd/autotest/tests/accelerometer.tests.js ---------------------------------------------------------------------- diff --git a/autotest/tests/accelerometer.tests.js b/autotest/tests/accelerometer.tests.js index c9a2745..e988454 100644 --- a/autotest/tests/accelerometer.tests.js +++ b/autotest/tests/accelerometer.tests.js @@ -1,58 +1,185 @@ describe('Accelerometer (navigator.accelerometer)', function () { - it("should exist", function () { + it("should exist", function () { expect(navigator.accelerometer).toBeDefined(); - }); - - it("should contain a getCurrentAcceleration function", function() { - expect(typeof navigator.accelerometer.getCurrentAcceleration).toBeDefined(); - expect(typeof navigator.accelerometer.getCurrentAcceleration == 'function').toBe(true); - }); - - it("getCurrentAcceleration success callback should be called with an Acceleration object", function() { - - var win = jasmine.createSpy().andCallFake(function(a) { - expect(a).toBeDefined(); - expect(a.x).toBeDefined(); - expect(typeof a.x == 'number').toBe(true); - expect(a.y).toBeDefined(); - expect(typeof a.y == 'number').toBe(true); - expect(a.z).toBeDefined(); - expect(typeof a.z == 'number').toBe(true); - }), - fail = jasmine.createSpy(); - - runs(function () { - navigator.accelerometer.getCurrentAcceleration(win, fail); - }); - - waitsFor(function () { return win.wasCalled; }, "win never called", Tests.TEST_TIMEOUT); - - runs(function () { - expect(fail).not.toHaveBeenCalled(); - }); - }); - - it("should contain a watchAcceleration function", function() { - expect(navigator.accelerometer.watchAcceleration).toBeDefined(); - expect(typeof navigator.accelerometer.watchAcceleration == 'function').toBe(true); - }); - - it("should contain a clearWatch function", function() { - expect(navigator.accelerometer.clearWatch).toBeDefined(); - expect(typeof navigator.accelerometer.clearWatch == 'function').toBe(true); - }); - - describe('Acceleration model', function () { - it("should be able to define a new Acceleration object with x, y, z and timestamp properties.", function () { - var x = 1; - var y = 2; - var z = 3; - var a = new Acceleration(x, y, z); - expect(a).toBeDefined(); - expect(a.x).toBe(x); - expect(a.y).toBe(y); - expect(a.z).toBe(z); - expect(a.timestamp).toBeDefined(); + }); + + describe("getCurrentAcceleration", function() { + it("should exist", function() { + expect(typeof navigator.accelerometer.getCurrentAcceleration).toBeDefined(); + expect(typeof navigator.accelerometer.getCurrentAcceleration == 'function').toBe(true); + }); + + it("success callback should be called with an Acceleration object", function() { + var win = jasmine.createSpy().andCallFake(function(a) { + expect(a).toBeDefined(); + expect(a.x).toBeDefined(); + expect(typeof a.x == 'number').toBe(true); + expect(a.y).toBeDefined(); + expect(typeof a.y == 'number').toBe(true); + expect(a.z).toBeDefined(); + expect(typeof a.z == 'number').toBe(true); + expect(a.timestamp).toBeDefined(); + expect(typeof a.timestamp).toBe('number'); + }), + fail = jasmine.createSpy(); + + runs(function () { + navigator.accelerometer.getCurrentAcceleration(win, fail); + }); + + waitsFor(function () { return win.wasCalled; }, "win never called", Tests.TEST_TIMEOUT); + + runs(function () { + expect(fail).not.toHaveBeenCalled(); + }); + }); + + it("success callback Acceleration object should have (reasonable) values for x, y and z expressed in m/s^2", function() { + var reasonableThreshold = 15; + var win = jasmine.createSpy().andCallFake(function(a) { + expect(a.x).toBeLessThan(reasonableThreshold); + expect(a.x).toBeGreaterThan(reasonableThreshold * -1); + expect(a.y).toBeLessThan(reasonableThreshold); + expect(a.y).toBeGreaterThan(reasonableThreshold * -1); + expect(a.z).toBeLessThan(reasonableThreshold); + expect(a.z).toBeGreaterThan(reasonableThreshold * -1); + }), + fail = jasmine.createSpy(); + + runs(function () { + navigator.accelerometer.getCurrentAcceleration(win, fail); + }); + + waitsFor(function () { return win.wasCalled; }, "win never called", Tests.TEST_TIMEOUT); + + runs(function () { + expect(fail).not.toHaveBeenCalled(); + }); + }); + + it("success callback Acceleration object should return a recent timestamp", function() { + var veryRecently = (new Date()).getTime(); + var win = jasmine.createSpy().andCallFake(function(a) { + expect(a.timestamp).toBeGreaterThan(veryRecently); + }), + fail = jasmine.createSpy(); + + runs(function () { + navigator.accelerometer.getCurrentAcceleration(win, fail); + }); + + waitsFor(function () { return win.wasCalled; }, "win never called", Tests.TEST_TIMEOUT); + + runs(function () { + expect(fail).not.toHaveBeenCalled(); + }); + }); + }); + + describe("watchAcceleration", function() { + var id; + + afterEach(function() { + navigator.accelerometer.clearWatch(id); + }); + + it("should exist", function() { + expect(navigator.accelerometer.watchAcceleration).toBeDefined(); + expect(typeof navigator.accelerometer.watchAcceleration == 'function').toBe(true); + }); + it("success callback should be called with an Acceleration object", function() { + var win = jasmine.createSpy().andCallFake(function(a) { + expect(a).toBeDefined(); + expect(a.x).toBeDefined(); + expect(typeof a.x == 'number').toBe(true); + expect(a.y).toBeDefined(); + expect(typeof a.y == 'number').toBe(true); + expect(a.z).toBeDefined(); + expect(typeof a.z == 'number').toBe(true); + expect(a.timestamp).toBeDefined(); + expect(typeof a.timestamp).toBe('number'); + }), + fail = jasmine.createSpy(); + + runs(function () { + id = navigator.accelerometer.watchAcceleration(win, fail, {frequency:500}); + }); + + waitsFor(function () { return win.wasCalled; }, "win never called", Tests.TEST_TIMEOUT); + + runs(function () { + expect(fail).not.toHaveBeenCalled(); + }); + }); + + it("success callback Acceleration object should have (reasonable) values for x, y and z expressed in m/s^2", function() { + var reasonableThreshold = 15; + var win = jasmine.createSpy().andCallFake(function(a) { + expect(a.x).toBeLessThan(reasonableThreshold); + expect(a.x).toBeGreaterThan(reasonableThreshold * -1); + expect(a.y).toBeLessThan(reasonableThreshold); + expect(a.y).toBeGreaterThan(reasonableThreshold * -1); + expect(a.z).toBeLessThan(reasonableThreshold); + expect(a.z).toBeGreaterThan(reasonableThreshold * -1); + }), + fail = jasmine.createSpy(); + + runs(function () { + id = navigator.accelerometer.watchAcceleration(win, fail, {frequency:500}); + }); + + waitsFor(function () { return win.wasCalled; }, "win never called", Tests.TEST_TIMEOUT); + + runs(function () { + expect(fail).not.toHaveBeenCalled(); + }); + }); + + it("success callback Acceleration object should return a recent timestamp", function() { + var veryRecently = (new Date()).getTime(); + var win = jasmine.createSpy().andCallFake(function(a) { + expect(a.timestamp).toBeGreaterThan(veryRecently); + }), + fail = jasmine.createSpy(); + + runs(function () { + id = navigator.accelerometer.watchAcceleration(win, fail, {frequency:500}); + }); + + waitsFor(function () { return win.wasCalled; }, "win never called", Tests.TEST_TIMEOUT); + + runs(function () { + expect(fail).not.toHaveBeenCalled(); + }); + }); + }); + + describe("clearWatch", function() { + it("should exist", function() { + expect(navigator.accelerometer.clearWatch).toBeDefined(); + expect(typeof navigator.accelerometer.clearWatch == 'function').toBe(true); + }); + + it("should clear an existing watch", function() { + var id, + win = jasmine.createSpy(); + + runs(function() { + id = navigator.accelerometer.watchAcceleration(win, function() {}, {frequency:100}); + }); + + waitsFor(function () { return win.wasCalled; }, "win never called", Tests.TEST_TIMEOUT); + + runs(function() { + win.reset(); + navigator.accelerometer.clearWatch(id); + }); + + waits(201); + + runs(function() { + expect(win).not.toHaveBeenCalled(); + }); }); }); });