You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ripple.apache.org by gt...@apache.org on 2013/03/11 16:11:04 UTC

[39/50] [abbrv] git commit: Add start / stop methods to the Cordova compass

Add start / stop methods to the Cordova compass

This fixes #721


Project: http://git-wip-us.apache.org/repos/asf/incubator-ripple/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ripple/commit/1e49131c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ripple/tree/1e49131c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ripple/diff/1e49131c

Branch: refs/heads/master
Commit: 1e49131cbbf4a9664075b94555157876c2587123
Parents: e1a47eb
Author: Gord Tanner <gt...@gmail.com>
Authored: Thu Feb 14 15:53:32 2013 -0500
Committer: Gord Tanner <gt...@gmail.com>
Committed: Thu Feb 14 15:53:32 2013 -0500

----------------------------------------------------------------------
 .../platform/cordova/2.0.0/bridge/compass.js       |   38 +++++++--
 test/unit/client/cordova/compass.js                |   64 ++++++++++++++-
 2 files changed, 94 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/1e49131c/lib/client/platform/cordova/2.0.0/bridge/compass.js
----------------------------------------------------------------------
diff --git a/lib/client/platform/cordova/2.0.0/bridge/compass.js b/lib/client/platform/cordova/2.0.0/bridge/compass.js
index 61b5f27..0b6ddc8 100644
--- a/lib/client/platform/cordova/2.0.0/bridge/compass.js
+++ b/lib/client/platform/cordova/2.0.0/bridge/compass.js
@@ -13,21 +13,45 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-var geo = ripple('geo');
+var geo = ripple('geo'),
+    bridge = ripple('emulatorBridge'),
+    CompassHeading = bridge.window().CompassHeading,
+    _success,
+    _error,
+    _interval;
 
 module.exports = {
     getHeading: function (success) {
         // TODO: build facility to trigger onError() from emulator
         // see pivotal item: https://www.pivotaltracker.com/story/show/7040343
-        success({
-            magneticHeading: geo.getPositionInfo().heading,
-            trueHeading: geo.getPositionInfo().heading,
-            headingAccuracy: 100,
-            timestamp: new Date().getSeconds()
-        });
+
+        var heading = new CompassHeading();
+        heading.trueHeading = geo.getPositionInfo().heading;
+        heading.magneticHeading = geo.getPositionInfo().heading;
+        heading.headingAccuracy = 100;
+        success(heading);
     },
 
     stopHeading: function () {
         //do nothing
+    },
+
+    start: function (success, error) {
+        _success = success;
+        _error = error;
+
+        _interval = window.setInterval(function () {
+            var heading = new CompassHeading();
+            heading.trueHeading = geo.getPositionInfo().heading;
+            heading.magneticHeading = geo.getPositionInfo().heading;
+            heading.headingAccuracy = 100;
+            success(heading);
+        }, 50);
+    },
+
+    stop: function () {
+        _success = null;
+        _error = null;
+        window.clearInterval(_interval);
     }
 };

http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/1e49131c/test/unit/client/cordova/compass.js
----------------------------------------------------------------------
diff --git a/test/unit/client/cordova/compass.js b/test/unit/client/cordova/compass.js
index 6432aca..f82cd2c 100644
--- a/test/unit/client/cordova/compass.js
+++ b/test/unit/client/cordova/compass.js
@@ -15,13 +15,34 @@
  */
 describe("cordova compass bridge", function () {
     var geo = ripple('geo'),
-        target = ripple('platform/cordova/2.0.0/bridge/compass'),
+        target,
+        bridge = ripple('emulatorBridge'),
         heading = {direction: "southish"};
 
     beforeEach(function () {
         spyOn(geo, "getPositionInfo").andReturn({
             heading: heading
         });
+
+        spyOn(bridge, "window").andReturn({
+            CompassHeading: function (m, t, a) {
+                return {
+                    magneticHeading: m,
+                    trueHeading: t,
+                    headingAccuracy: a,
+                    timestamp: 1
+                };
+            }
+        });
+
+        spyOn(window, "setInterval").andReturn(1);
+        spyOn(window, "clearInterval");
+
+        target = ripple('platform/cordova/2.0.0/bridge/compass');
+    });
+
+    afterEach(function () {
+        target.stop();
     });
 
     describe("when calling getHeading", function () {
@@ -39,4 +60,45 @@ describe("cordova compass bridge", function () {
             });
         });
     });
+
+    describe("when starting", function () {
+        it("starts an interval", function () {
+            var s = jasmine.createSpy("success"),
+                f = jasmine.createSpy("fail");
+
+            target.start(s, f);
+            expect(window.setInterval).toHaveBeenCalledWith(jasmine.any(Function), 50);
+        });
+
+        it("the interval function calls the success callback with the AccelerometerInfoChangedEvent", function () {
+            var s = jasmine.createSpy("success"),
+                f = jasmine.createSpy("fail");
+
+            target.start(s, f);
+
+            window.setInterval.mostRecentCall.args[0]();
+
+            expect(geo.getPositionInfo).toHaveBeenCalled();
+            expect(s).toHaveBeenCalledWith({
+                magneticHeading: heading,
+                trueHeading: heading,
+                headingAccuracy: 100,
+                timestamp: jasmine.any(Number)
+            });
+
+            expect(f).not.toHaveBeenCalled();
+        });
+    });
+
+    describe("when stopping", function () {
+        it("it clears the interval", function () {
+            var s = jasmine.createSpy("success"),
+                f = jasmine.createSpy("fail");
+
+            target.start(s, f);
+            target.stop();
+
+            expect(window.clearInterval).toHaveBeenCalledWith(1);
+        });
+    });
 });