cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lorinb...@apache.org
Subject webworks commit: [CB-3371] Fixed Battery plugin start to change contexts rather than throw an error - Also added support for working across multiple webViews
Date Mon, 13 May 2013 20:59:13 GMT
Updated Branches:
  refs/heads/future 1890c10c2 -> b13f11b3b


[CB-3371] Fixed Battery plugin start to change contexts rather than throw an error
- Also added support for working across multiple webViews


Project: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/commit/b13f11b3
Tree: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/tree/b13f11b3
Diff: http://git-wip-us.apache.org/repos/asf/cordova-blackberry/diff/b13f11b3

Branch: refs/heads/future
Commit: b13f11b3bfda3053611de7a83fbd94c75a4fdc63
Parents: 1890c10
Author: Jeffrey Heifetz <jheifetz@blackberry.com>
Authored: Mon Apr 29 14:21:16 2013 -0400
Committer: lorinbeer <lorin@adobe.com>
Committed: Mon May 13 13:46:51 2013 -0700

----------------------------------------------------------------------
 .../plugins/Battery/src/blackberry10/index.js      |   33 ++++++----
 blackberry10/bin/test/plugins/Battery/index.js     |   48 ++++++++++-----
 2 files changed, 54 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/b13f11b3/blackberry10/bin/templates/project/plugins/Battery/src/blackberry10/index.js
----------------------------------------------------------------------
diff --git a/blackberry10/bin/templates/project/plugins/Battery/src/blackberry10/index.js
b/blackberry10/bin/templates/project/plugins/Battery/src/blackberry10/index.js
index fcac7b2..07a943c 100644
--- a/blackberry10/bin/templates/project/plugins/Battery/src/blackberry10/index.js
+++ b/blackberry10/bin/templates/project/plugins/Battery/src/blackberry10/index.js
@@ -17,32 +17,41 @@
 var SYSTEM_EVENTS = ["device.battery.statusChange",
                      "device.battery.chargeLow",
                      "device.battery.chargeCritical"],
-    clientListener;
+    device = window.qnx.webplatform.device,
+    _clientListeners = {};
 
 module.exports = {
     start: function (success, fail, args, env) {
-        var result = new PluginResult(args, env);
-        if (!!clientListener) {
-            result.error("Battery listener already running");
-        } else {
-            clientListener = function (info) {
+        var result = new PluginResult(args, env),
+            listener = function (info) {
                 result.callbackOk(info, true);
             };
+
+        if (_clientListeners[env.webview.id]) {
+            //TODO: Change back to erroring out after reset is implemented
+            //result.error("Battery listener already running");
             SYSTEM_EVENTS.forEach(function (event) {
-                window.qnx.webplatform.device.addEventListener(event, clientListener);
+                device.removeEventListener(event, _clientListeners[env.webview.id]);
             });
-            result.noResult(true);
         }
+
+        _clientListeners[env.webview.id] = listener;
+        SYSTEM_EVENTS.forEach(function (event) {
+            device.addEventListener(event, listener);
+        });
+        result.noResult(true);
     },
     stop: function (success, fail, args, env) {
-        var result = new PluginResult(args, env);
-        if (!clientListener) {
+        var result = new PluginResult(args, env),
+            listener = _clientListeners[env.webview.id];
+
+        if (!listener) {
             result.error("Battery listener has not started");
         } else {
             SYSTEM_EVENTS.forEach(function (event) {
-                window.qnx.webplatform.device.removeEventListener(event, clientListener);
+                device.removeEventListener(event, listener);
             });
-            clientListener = null;
+            delete _clientListeners[env.webview.id];
             result.noResult(false);
         }
     }

http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/b13f11b3/blackberry10/bin/test/plugins/Battery/index.js
----------------------------------------------------------------------
diff --git a/blackberry10/bin/test/plugins/Battery/index.js b/blackberry10/bin/test/plugins/Battery/index.js
index a9c610a..9343890 100644
--- a/blackberry10/bin/test/plugins/Battery/index.js
+++ b/blackberry10/bin/test/plugins/Battery/index.js
@@ -19,55 +19,71 @@ describe("Battery", function () {
     var _apiDir = __dirname + "./../../../templates/project/plugins/Battery/src/blackberry10/",
         index,
         callback,
-        result = {
+        mockPluginResult = {
             ok: jasmine.createSpy(),
             error: jasmine.createSpy(),
             noResult: jasmine.createSpy(),
             callbackOk: jasmine.createSpy()
+        },
+        noop = function () {},
+        args,
+        env = {
+            webview: {
+                id: 42
+            }
         };
 
+
     beforeEach(function () {
-        index = require(_apiDir + "index");
         GLOBAL.window = {
             qnx: {
                 webplatform: {
                     device: {
-                        addEventListener: jasmine.createSpy().andCallFake(function (evt,
cb) {
+                        addEventListener: jasmine.createSpy("webplatform.device.addEventListener").andCallFake(function
(evt, cb) {
                             callback = cb;
                         }),
-                        removeEventListener: jasmine.createSpy()
+                        removeEventListener: jasmine.createSpy("webplatform.device.removeEventListener")
                     }
                 }
             }
         };
         GLOBAL.PluginResult = function () {
-            return result;
+            return mockPluginResult;
         };
+        index = require(_apiDir + "index");
     });
 
     afterEach(function () {
-        index = null;
         delete GLOBAL.window;
         delete GLOBAL.PluginResult;
+        delete require.cache[require.resolve(_apiDir + "index")];
     });
 
     describe("start", function () {
 
         it("calls noResult and keeps callbacks", function () {
-            index.start();
+            index.start(noop, noop, args, env);
+            expect(window.qnx.webplatform.device.removeEventListener).not.toHaveBeenCalled();
             expect(window.qnx.webplatform.device.addEventListener).toHaveBeenCalled();
-            expect(result.noResult).toHaveBeenCalledWith(true);
+            expect(mockPluginResult.noResult).toHaveBeenCalledWith(true);
+            expect(mockPluginResult.error).not.toHaveBeenCalled();
         });
 
         it("callback calls ok and keeps callbacks", function () {
             callback("OK");
-            expect(result.callbackOk).toHaveBeenCalledWith("OK", true);
+            expect(mockPluginResult.callbackOk).toHaveBeenCalledWith("OK", true);
+            expect(mockPluginResult.error).not.toHaveBeenCalled();
         });
 
-        it("calls error if already started", function () {
-            index.start();
-            expect(window.qnx.webplatform.device.addEventListener).not.toHaveBeenCalled();
-            expect(result.error).toHaveBeenCalled();
+        it("does not call error if already started", function () {
+            index.start(noop, noop, args, env);
+            window.qnx.webplatform.device.addEventListener.reset();
+            mockPluginResult.noResult.reset();
+            index.start(noop, noop, args, env);
+            expect(window.qnx.webplatform.device.removeEventListener).toHaveBeenCalled();
+            expect(window.qnx.webplatform.device.addEventListener).toHaveBeenCalled();
+            expect(mockPluginResult.error).not.toHaveBeenCalled();
+            expect(mockPluginResult.noResult).toHaveBeenCalledWith(true);
         });
 
 
@@ -76,9 +92,11 @@ describe("Battery", function () {
     describe("stop", function () {
 
         it("calls noResult and does not keep callbacks", function () {
-            index.stop();
+            index.start(noop, noop, args, env);
+            window.qnx.webplatform.device.removeEventListener.reset();
+            index.stop(noop, noop, args, env);
             expect(window.qnx.webplatform.device.removeEventListener).toHaveBeenCalled();
-            expect(result.noResult).toHaveBeenCalledWith(false);
+            expect(mockPluginResult.noResult).toHaveBeenCalledWith(false);
         });
 
     });


Mime
View raw message