Skip to content
Permalink
Browse files
  • Loading branch information
timkim committed May 17, 2012
2 parents 9515b1d + 34b767d commit 60f000fc2c82e7c33af81518239fa2435572643c
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 82 deletions.
@@ -5,11 +5,10 @@ These specs are designed to run inside the mobile device that implements it - _i
These set of tests is designed to be used with Cordova. You should initialize a fresh Cordova repository for a target platform and then toss these files into the www folder, replacing the
contents.

Make sure you include Cordova-\*.js in the www folder. You also need to edit Cordova.js to reference the Cordova-\*.js file you are testing.
For example, to test with Cordova-0.9.6.1, the Cordova.js file would be:
Make sure you include cordova-\*.js in the www folder. You also need to edit cordova.js to reference the version of cordova-\*.js file you are testing.
For example, to test with cordova-0.9.6.1, edit the VERSION variable in the cordova.js file as follows:

document.write('<script type="text/javascript" charset="utf-8" src="../Cordova-0.9.6.1.js"></script>');
document.write('<script type="text/javascript" charset="utf-8" src="Cordova-0.9.6.1.js"></script>');
var VERSION='0.9.6.1';

This is done so that you don't have to modify every HTML file when you want to test a new version of Cordova.

@@ -230,6 +230,22 @@
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, gotFS, fsFail);
}

/**
* Function to create a file for BB recording
*/
function getRecordSrcBB() {
var fsFail = function(error) {
console.log("error creating file for BB recording");
};
var gotFile = function(file) {
recordSrc = file.fullPath;
};
var gotFS = function(fileSystem) {
fileSystem.root.getFile("BBRecording.amr", {create: true}, gotFile, fsFail);
};
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, gotFS, fsFail);
}

/**
* Function called when page has finished loading.
*/
@@ -239,6 +255,8 @@
if (device.platform.indexOf("iPhone") !=-1 || device.platform.indexOf("iPad") !=-1)
{
getRecordSrc();
} else if (typeof blackberry !== 'undefined') {
getRecordSrcBB();
}
console.log("Device="+device.platform+" "+device.version);
}, false);
@@ -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();
});
});
});
});

0 comments on commit 60f000f

Please sign in to comment.