You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by gt...@apache.org on 2012/11/13 20:13:42 UTC
[5/16] js commit: Updating tests based on code review (WIP)
Updating tests based on code review (WIP)
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/6235f1c9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/6235f1c9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/6235f1c9
Branch: refs/heads/master
Commit: 6235f1c971b625c9f27feff5fd38d9145ba85f5c
Parents: 8051761
Author: Gord Tanner <gt...@gmail.com>
Authored: Mon Nov 12 15:46:21 2012 -0500
Committer: Gord Tanner <gt...@gmail.com>
Committed: Mon Nov 12 15:46:21 2012 -0500
----------------------------------------------------------------------
lib/blackberry/plugin/webworks/accelerometer.js | 2 +-
lib/blackberry/plugin/webworks/media.js | 2 +-
test/blackberry/webworks/test.accelerometer.js | 71 ++--
test/blackberry/webworks/test.logger.js | 22 +-
test/blackberry/webworks/test.media.js | 392 +++++++++---------
5 files changed, 251 insertions(+), 238 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/6235f1c9/lib/blackberry/plugin/webworks/accelerometer.js
----------------------------------------------------------------------
diff --git a/lib/blackberry/plugin/webworks/accelerometer.js b/lib/blackberry/plugin/webworks/accelerometer.js
index 90dc0e7..70142fa 100644
--- a/lib/blackberry/plugin/webworks/accelerometer.js
+++ b/lib/blackberry/plugin/webworks/accelerometer.js
@@ -38,6 +38,6 @@ module.exports = {
},
stop: function (args, win, fail) {
window.removeEventListener("devicemotion", callback);
- return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "WebWorks Is On It" };
+ return { "status" : cordova.callbackStatus.OK, "message" : "removed" };
}
};
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/6235f1c9/lib/blackberry/plugin/webworks/media.js
----------------------------------------------------------------------
diff --git a/lib/blackberry/plugin/webworks/media.js b/lib/blackberry/plugin/webworks/media.js
index 82404c5..c2ec671 100644
--- a/lib/blackberry/plugin/webworks/media.js
+++ b/lib/blackberry/plugin/webworks/media.js
@@ -105,7 +105,7 @@ module.exports = {
return {"status" : 9, "message" : "Media Object id was not sent in arguments"};
}
- var id = args[0],
+ var id = args[0],
audio = audioObjects[id],
result;
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/6235f1c9/test/blackberry/webworks/test.accelerometer.js
----------------------------------------------------------------------
diff --git a/test/blackberry/webworks/test.accelerometer.js b/test/blackberry/webworks/test.accelerometer.js
index b315108..10af041 100644
--- a/test/blackberry/webworks/test.accelerometer.js
+++ b/test/blackberry/webworks/test.accelerometer.js
@@ -20,44 +20,65 @@
*/
describe("accelerometer", function () {
- var accelerometer = require('cordova/plugin/webworks/accelerometer');
+ var accelerometer = require('cordova/plugin/webworks/accelerometer'),
+ cordova = require('cordova');
- describe("start", function() {
- it("should start webworks watching the accelerometer", function() {
- spyOn(window, "removeEventListener");
- spyOn(window, "addEventListener");
+ beforeEach(function(){
+ spyOn(window, "removeEventListener");
+ spyOn(window, "addEventListener");
+ });
- var win = jasmine.createSpy('win'),
- fail = jasmine.createSpy('fail'),
- args = {x:1,y:2,z:3},
- aStart = accelerometer.start(args, win, fail);
+ describe("start", function() {
+ it("returns no result", function () {
+ expect(accelerometer.start()).toEqual({
+ status: cordova.callbackStatus.NO_RESULT,
+ message: "WebWorks Is On It"
+ });
+ });
- expect(window.removeEventListener).toHaveBeenCalledWith("devicemotion", undefined);
+ it("removes the event listener", function () {
+ accelerometer.start();
+ expect(window.removeEventListener).toHaveBeenCalledWith("devicemotion", jasmine.any(Function));
+ });
+ it("adds the event listener back", function () {
+ accelerometer.start();
expect(window.addEventListener).toHaveBeenCalledWith("devicemotion", jasmine.any(Function));
+ });
- window.removeEventListener.reset();
- window.addEventListener.reset();
-
- expect(aStart.status).toBe(0);
- expect(aStart.message).toBe('WebWorks Is On It');
+ it("grabs the motion information from the callback and calls success", function () {
+ var success = jasmine.createSpy("success");
+ accelerometer.start({}, success);
+ window.addEventListener.mostRecentCall.args[1]({
+ accelerationIncludingGravity: {
+ x: 1,
+ y: 2,
+ z: 3,
+ },
+ timestamp: "around tea time"
+ });
+
+ expect(success).toHaveBeenCalledWith({
+ x: 1,
+ y: 2,
+ z: 3,
+ timestamp: "around tea time"
+ });
});
});
describe("stop", function() {
- it("should stop webworks from watching the accelerometer", function() {
- spyOn(window, "removeEventListener");
- var aStop = accelerometer.stop();
+ it("returns OK", function () {
+ expect(accelerometer.stop()).toEqual({
+ status: cordova.callbackStatus.OK,
+ message: "removed"
+ });
+ });
+ it("removes the event listener", function () {
+ accelerometer.stop();
expect(window.removeEventListener).toHaveBeenCalledWith("devicemotion", jasmine.any(Function));
-
- window.removeEventListener.reset();
-
- expect(aStop.status).toBe(0);
- expect(aStop.message).toBe('WebWorks Is On It');
-
});
});
-
});
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/6235f1c9/test/blackberry/webworks/test.logger.js
----------------------------------------------------------------------
diff --git a/test/blackberry/webworks/test.logger.js b/test/blackberry/webworks/test.logger.js
index 38ec7e2..9299211 100644
--- a/test/blackberry/webworks/test.logger.js
+++ b/test/blackberry/webworks/test.logger.js
@@ -22,15 +22,19 @@
describe("logger", function () {
var logger = require('cordova/plugin/webworks/logger');
- describe("log", function() {
- it("should post a console log", function() {
- spyOn(console, "log");
- var l = logger.log("Yodalay-Yodalay-Yodalay-Hee-Hoo!");
-
- expect(console.log).toHaveBeenCalledWith("Yodalay-Yodalay-Yodalay-Hee-Hoo!");
- expect(l.status).toBe(1);
- expect(l.message).toBe('Message logged to console: Yodalay-Yodalay-Yodalay-Hee-Hoo!');
- });
+ beforeEach(function () {
+ spyOn(console, "log");
+ });
+
+ it("should post a console log", function() {
+ logger.log("Yodalay-Yodalay-Yodalay-Hee-Hoo!");
+ expect(console.log).toHaveBeenCalledWith("Yodalay-Yodalay-Yodalay-Hee-Hoo!");
});
+ it("returns ok", function () {
+ expect(logger.log('moo')).toEqual({
+ status: cordova.callbackStatus.OK,
+ message: 'Message logged to console: moo'
+ });
+ });
});
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/6235f1c9/test/blackberry/webworks/test.media.js
----------------------------------------------------------------------
diff --git a/test/blackberry/webworks/test.media.js b/test/blackberry/webworks/test.media.js
index ca35da8..ae2bacb 100644
--- a/test/blackberry/webworks/test.media.js
+++ b/test/blackberry/webworks/test.media.js
@@ -21,254 +21,242 @@
describe("media", function () {
var media = require('cordova/plugin/webworks/media'),
- m;
-
- beforeEach(function(){
- global.Audio = function(src){
- var funcs = {
- play: function(){},
- pause: function(){},
- currentTime: 800,
- duration: 2100
- }
- return funcs;
- }
- });
-
- afterEach(function(){
- delete global.Audio;
- });
-
- describe("create", function() {
- it("should fail to create a media object", function() {
- m = media.create({});
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media Object id was not sent in arguments');
- });
-
- it("should fail to create a media object", function() {
- m = media.create(9);
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media Object id was not sent in arguments');
- });
-
- it("should create a media object", function() {
- m = media.create(["jammin", "tunes/wejammin.mp3"]);
- expect(m.status).toBe(1);
- expect(m.message).toBe('Audio object created');
+ audio = {
+ play: jasmine.createSpy("play"),
+ pause: jasmine.createSpy("pause"),
+ currentTime: 800,
+ duration: 2100
+ };
+
+ beforeEach(function(){
+ audio.play.reset();
+ audio.pause.reset();
+ global.blackberry = {
+ media:{
+ microphone:{
+ record: jasmine.createSpy("blackberry.media.microphone.record")
+ }
+ }
+ };
+
+ global.Audio = jasmine.createSpy("Audio").andCallFake(function () {
+ return audio;
});
});
- describe("startPlayingAudio", function(){
- it("should fail to find the media Object", function() {
- m = media.startPlayingAudio({});
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media Object id was not sent in arguments');
- });
+ afterEach(function(){
+ delete global.Audio;
+ delete global.blackberry;
+ });
- it("should fail to find media Source ", function() {
- m = media.startPlayingAudio(["jammin"]);
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media source argument not found');
+ function handlesNoArgs(func) {
+ it("should return an error message when no args provided", function() {
+ expect(func({})).toEqual({
+ status: 9,
+ message: 'Media Object id was not sent in arguments'
+ });
});
-
- it("should create a media object ", function() {
- m = media.startPlayingAudio(["jammin", "tunes/wejammin.mp3"]);
- expect(m.status).toBe(1);
- expect(m.message).toBe('Audio play started');
+ }
+
+ function handlesNotFound(func) {
+ it("returns error if it can't find audio object", function () {
+ expect(func(["FreeBird"])).toEqual({
+ status: 2,
+ message: 'Audio Object has not been initialized'
+ });
});
- });
+ }
+
+ describe("create", function() {
- describe("stopPlayingAudio", function(){
- it("should fail to find the media Object", function() {
- m = media.stopPlayingAudio(0);
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media Object id was not sent in arguments');
- });
+ handlesNoArgs(media.create);
- it("should stop the audio playback ", function() {
- m = media.stopPlayingAudio(["jammin", "tunes/wejammin.mp3"]);
- expect(m.status).toBe(1);
- expect(m.message).toBe('Audio play stopped');
+ it("should create an audio object for the src", function () {
+ media.create(["jammin", "tunes/wejammin.mp3"]);
+ expect(Audio).toHaveBeenCalledWith("tunes/wejammin.mp3");
});
- it("should find that the Audio Object failed to initialze", function() {
- global.audio = undefined;
- m = media.stopPlayingAudio(["jammin"]);
- expect(m.status).toBe(2);
- expect(m.message).toBe('Audio Object has not been initialized');
- delete global.audio;
+ it("returns success", function() {
+ expect(media.create(["jammin", "tunes/wejammin.mp3"])).toEqual({
+ status: 1,
+ message: 'Audio object created'
+ });
});
});
- describe("seekToAudio", function(){
- it("should fail to find the media Object", function() {
- m = media.seekToAudio(0);
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media Object id was not sent in arguments');
- });
+ describe("startPlayingAudio", function(){
- it("should find that the Audio Object failed to initialze", function() {
- global.audio = undefined;
- m = media.seekToAudio(["jammin"]);
- expect(m.status).toBe(2);
- expect(m.message).toBe('Audio Object has not been initialized');
- delete global.audio;
- });
+ handlesNoArgs(media.startPlayingAudio);
- it("should find that there is no time argument in the function", function() {
- media.create(["jammin", "tunes/wejammin.mp3"]);
- m = media.seekToAudio(["jammin"]);
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media seek time argument not found');
- media.release(["jammin"]);
+ it("errors out when no src", function () {
+ expect(media.startPlayingAudio([1])).toEqual({
+ status: 9,
+ message: "Media source argument not found"
+ });
});
- // TODO: review error generators!!! It should have a listener to see when it changes and make calls based on those changes of that particular variable.
- // it("should result in an error seeking audio", function() {
- // media.create(["jammin", "tunes/wejammin.mp3"]);
- // m = media.seekToAudio(["jammin", 9000]);
-
- // // expect(m.status).toBe(3);
- // expect(m.message).toBe('Seek to audio succeeded');
-
- // media.release(["jammin"]);
- // });
-
- it("should successfully seek to audio", function() {
- media.create(["jammin", "tunes/wejammin.mp3"]);
- m = media.seekToAudio(["jammin", 800]);
- expect(m.status).toBe(1);
- expect(m.message).toBe('Seek to audio succeeded');
- media.release(["jammin"]);
+ it("returns success", function() {
+ expect(media.startPlayingAudio(["jammin", "tunes/wejammin.mp3"])).toEqual({
+ status: 1,
+ message: 'Audio play started'
+ });
});
-
- });
-
- describe("pausePlayingAudio", function(){
- it("should fail to find the media Object", function() {
- m = media.pausePlayingAudio(0);
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media Object id was not sent in arguments');
+ it("creates an audio object for the src", function () {
+ media.startPlayingAudio(["push", "pushit.mp3"]);
+ expect(Audio).toHaveBeenCalledWith("pushit.mp3");
});
- it("should find that the Audio Object failed to initialze", function() {
- global.audio = undefined;
- m = media.pausePlayingAudio(["jammin"]);
- expect(m.status).toBe(2);
- expect(m.message).toBe('Audio Object has not been initialized');
- delete global.audio;
+ it("calls play on the audio object", function () {
+ media.startPlayingAudio(["baby", "babybabybabyohhh.mp3"]);
+ expect(audio.play).toHaveBeenCalled();
});
- it("should successfully pause the audio", function() {
- media.create(["jammin", "tunes/wejammin.mp3"]);
- m = media.pausePlayingAudio(["jammin"]);
- expect(m.status).toBe(1);
- expect(m.message).toBe('Audio paused');
- media.release(["jammin"]);
+ it("calls pause if the audio id already existed", function () {
+ media.startPlayingAudio(["ice", "iceicebaby.mp3"]);
+ media.startPlayingAudio(["ice", "iceicebaby.mp3"]);
+ expect(audio.pause).toHaveBeenCalled();
});
-
- });
- describe("getCurrentPositionAudio", function(){
- it("should fail to find the media Object", function() {
- m = media.getCurrentPositionAudio(0);
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media Object id was not sent in arguments');
+ it("doesn't call pause on new id's", function () {
+ media.startPlayingAudio(["loser", "loser.mp3"]);
+ expect(audio.pause).not.toHaveBeenCalled();
});
+ });
- it("should find that the Audio Object failed to initialze", function() {
- global.audio = undefined;
- m = media.getCurrentPositionAudio(["jammin"]);
- expect(m.status).toBe(2);
- expect(m.message).toBe('Audio Object has not been initialized');
- delete global.audio;
+ describe("stopPlayingAudio", function(){
+ handlesNoArgs(media.stopPlayingAudio);
+
+ it("finds that no Audio Object exists", function () {
+ expect(media.stopPlayingAudio(["Free Bird"])).toEqual({
+ status: 2,
+ message: 'Audio Object has not been initialized'
+ });
});
- it("should successfully pause the audio", function() {
- media.create(["jammin", "tunes/wejammin.mp3"]);
- m = media.getCurrentPositionAudio(["jammin"]);
- expect(m.status).toBe(1);
- expect(m.message).toBe(800);
- media.release(["jammin"]);
+ describe("when it can find the audio object", function () {
+ beforeEach(function () {
+ media.startPlayingAudio(["thriller", "triller.mp3"]);
+ audio.pause.reset(); //since start will call play
+ });
+
+ it("returns success", function () {
+ expect(media.stopPlayingAudio(["thriller"])).toEqual({
+ status: 1,
+ message: "Audio play stopped"
+ });
+ });
+
+ it("calls pause on the found audio object", function () {
+ media.stopPlayingAudio(["thriller"]);
+ expect(audio.pause).toHaveBeenCalled();
+
+ });
});
-
});
- describe("getDuration", function(){
- it("should fail to find the media Object", function() {
- m = media.getDuration(0);
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media Object id was not sent in arguments');
+ describe("seekToAudio", function(){
+ handlesNoArgs(media.seekToAudio);
+ handlesNotFound(media.seekToAudio);
+
+ describe("when it can find an audio object", function () {
+ beforeEach(function () {
+ media.create(["yellowSubmarine", "yellowSubmarine.ogg"]);
+ });
+
+ it("returns a message when no seek time provided", function () {
+ expect(media.seekToAudio(["yellowSubmarine"])).toEqual({
+ status: 9,
+ message: 'Media seek time argument not found'
+ });
+ });
+
+ it("sets the currentTime of the audio object", function () {
+ media.seekToAudio(["yellowSubmarine", 12]);
+ expect(audio.currentTime).toBe(12);
+ });
+
+ describe("when setting the current time fails", function () {
+ beforeEach(function () {
+ spyOn(console, "log");
+ audio.__defineSetter__("currentTime", jasmine.createSpy("audio.currentTime").andThrow("holy balls!"));
+ });
+
+ afterEach(function () {
+ delete audio.currentTime;
+ audio.currentTime = 800;
+ });
+
+ it("logs the error", function () {
+ media.seekToAudio(["yellowSubmarine", 33]);
+ expect(console.log).toHaveBeenCalledWith("Error seeking audio: holy balls!");
+ });
+
+ it("returns the error", function () {
+ expect(media.seekToAudio(["yellowSubmarine", 33])).toEqual({
+ status: 3,
+ message: "Error seeking audio: holy balls!"
+ });
+ });
+ });
});
+ });
- it("should find that the Audio Object failed to initialze", function() {
- global.audio = undefined;
- m = media.getDuration(["jammin"]);
- expect(m.status).toBe(2);
- expect(m.message).toBe('Audio Object has not been initialized');
- delete global.audio;
- });
+ describe("pausePlayingAudio", function(){
+ handlesNoArgs(media.pausePlayingAudio);
+ handlesNotFound(media.pausePlayingAudio);
- it("should successfully pause the audio", function() {
- media.create(["jammin", "tunes/wejammin.mp3"]);
- m = media.getDuration(["jammin"]);
- expect(m.status).toBe(1);
- expect(m.message).toBe(2100);
- media.release(["jammin"]);
+ it("should pause the existing audio", function () {
+ media.create(["WhatIsLove", "babyDontHurtMe.mp3"]);
+ media.pausePlayingAudio(["WhatIsLove"]);
+ expect(audio.pause).toHaveBeenCalled();
});
+ it("should return Audio paused", function () {
+ media.create(["TheBoysAreBackInTown", "thinLizzyBaby.mp3"]);
+ expect(media.pausePlayingAudio(["TheBoysAreBackInTown"])).toEqual({
+ status: 1,
+ message: 'Audio paused'
+ });
+ });
});
- describe("startRecordingAudio", function(){
- it("should fail to find the media Object", function() {
- m = media.startRecordingAudio(0);
- expect(m.status).toBe(9);
- expect(m.message).toBe('Media Object id was not sent in arguments');
+ describe("getCurrentPositionAudio", function(){
+ handlesNoArgs(media.getCurrentPositionAudio);
+ handlesNotFound(media.getCurrentPositionAudio);
+
+ it("should return current audio position", function () {
+ media.create(["InTheEnd", "linkinPark/inTheEnd.mp3"]);
+ expect(media.getCurrentPositionAudio(["InTheEnd"])).toEqual({
+ status: 1,
+ message: 800
+ });
});
+ });
- it("should find that the Audio Object failed to initialze", function() {
- global.audio = undefined;
- m = media.startRecordingAudio(["jammin"]);
- expect(m.status).toBe(2);
- expect(m.message).toBe('Media start recording, insufficient arguments');
- delete global.audio;
- });
+ describe("getDuration", function(){
+ handlesNoArgs(media.getDuration);
+ handlesNotFound(media.getDuration);
+
+ it("should return errors", function () {
+ expect(media.getDuration(["EndlessLove"])).toEqual({
+ status: 2,
+ message:'Audio Object has not been initialized'
+ });
+ });
+ });
+
+ xdescribe("startRecordingAudio", function(){
+ handlesNoArgs(media.startRecordingAudio);
+ handlesNotFound(media.startRecordingAudio);
- it("should successfully pause the audio", function() {
+ xit("should successfully pause the audio", function() {
media.create(["jammin", "tunes/wejammin.mp3"]);
m = media.startRecordingAudio(["jammin"]);
- expect(m.status).toBe(1);
- expect(m.message).toBe(2100);
+ expect(m.status).toBe(0);
+ expect(m.message).toBe('WebWorks Is On It');
media.release(["jammin"]);
});
});
-
- // describe("getDuration", function(){
- // it("should fail to find the media Object", function() {
- // m = media.getDuration(0);
- // expect(m.status).toBe(9);
- // expect(m.message).toBe('Media Object id was not sent in arguments');
- // });
-
- // it("should find that the Audio Object failed to initialze", function() {
- // global.audio = undefined;
- // m = media.getDuration(["jammin"]);
- // expect(m.status).toBe(2);
- // expect(m.message).toBe('Audio Object has not been initialized');
- // delete global.audio;
- // });
-
- // it("should successfully pause the audio", function() {
- // media.create(["jammin", "tunes/wejammin.mp3"]);
- // m = media.getDuration(["jammin"]);
- // expect(m.status).toBe(1);
- // expect(m.message).toBe(2100);
- // media.release(["jammin"]);
- // });
-
- // });
-
});