cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [2/2] js commit: [android] Return the exec result payload if it finished sync.
Date Thu, 25 Oct 2012 19:01:17 GMT
[android] Return the exec result payload if it finished sync.

Fixes https://issues.apache.org/jira/browse/CB-1745


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/576c320a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/576c320a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/576c320a

Branch: refs/heads/master
Commit: 576c320a0021220723e86deb9635745a68cf1c73
Parents: c8f9ab6
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Thu Oct 25 14:11:04 2012 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Thu Oct 25 14:13:52 2012 -0400

----------------------------------------------------------------------
 lib/android/exec.js                             |   21 ++++++-
 lib/android/plugin/android/nativeapiprovider.js |    4 ++
 test/android/test.exec.js                       |   50 ++++++++++++++---
 3 files changed, 63 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/576c320a/lib/android/exec.js
----------------------------------------------------------------------
diff --git a/lib/android/exec.js b/lib/android/exec.js
index bd5fc7b..a938e71 100644
--- a/lib/android/exec.js
+++ b/lib/android/exec.js
@@ -70,11 +70,18 @@ function androidExec(success, fail, service, action, args) {
     }
 
     var callbackId = service + cordova.callbackId++,
-        argsJson = JSON.stringify(args);
-    if (success || fail) {
-        cordova.callbacks[callbackId] = {success:success, fail:fail};
+        argsJson = JSON.stringify(args),
+        returnValue;
+
+    // TODO: Returning the payload of a synchronous call was deprecated in 2.2.0.
+    // Remove it after 6 months.
+    function captureReturnValue(value) {
+        returnValue = value;
+        success(value);
     }
 
+    cordova.callbacks[callbackId] = {success:captureReturnValue, fail:fail};
+
     if (jsToNativeBridgeMode == jsToNativeModes.LOCATION_CHANGE) {
         window.location = 'http://cdv_exec/' + service + '#' + action + '#' + callbackId
+ '#' + argsJson;
     } else {
@@ -86,6 +93,14 @@ function androidExec(success, fail, service, action, args) {
         }
         androidExec.processMessages(messages);
     }
+    if (cordova.callbacks[callbackId]) {
+        if (success || fail) {
+            cordova.callbacks[callbackId].success = success;
+        } else {
+            delete cordova.callbacks[callbackId];
+        }
+    }
+    return returnValue;
 }
 
 function pollOnce() {

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/576c320a/lib/android/plugin/android/nativeapiprovider.js
----------------------------------------------------------------------
diff --git a/lib/android/plugin/android/nativeapiprovider.js b/lib/android/plugin/android/nativeapiprovider.js
index 35cc253..b8b4b59 100644
--- a/lib/android/plugin/android/nativeapiprovider.js
+++ b/lib/android/plugin/android/nativeapiprovider.js
@@ -9,5 +9,9 @@ module.exports = {
     get: function() { return currentApi; },
     setPreferPrompt: function(value) {
         currentApi = value ? require('cordova/plugin/android/promptbasednativeapi') : nativeApi;
+    },
+    // Used only by tests.
+    set: function(value) {
+        currentApi = value;
     }
 };

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/576c320a/test/android/test.exec.js
----------------------------------------------------------------------
diff --git a/test/android/test.exec.js b/test/android/test.exec.js
index 5ce1f38..c03700c 100644
--- a/test/android/test.exec.js
+++ b/test/android/test.exec.js
@@ -22,18 +22,26 @@
 describe('exec.processMessages', function () {
     var cordova = require('cordova'),
         exec = require('cordova/androidexec'),
-        callbackSpy = jasmine.createSpy('callbackFromNative'),
-        origPrompt = typeof prompt == 'undefined' ? null : prompt,
-        origCallbackFromNative = cordova.callbackFromNative;
+        nativeApiProvider = require('cordova/plugin/android/nativeapiprovider'),
+        origNativeApi = nativeApiProvider.get();
+
+    var nativeApi = {
+        exec: jasmine.createSpy('nativeApi.exec'),
+        retrieveJsMessages: jasmine.createSpy('nativeApi.retrieveJsMessages'),
+    };
+
 
     beforeEach(function() {
-        callbackSpy.reset();
-        cordova.callbackFromNative = callbackSpy;
+        nativeApi.exec.reset();
+        nativeApi.retrieveJsMessages.reset();
+        // Avoid a log message warning about the lack of _nativeApi.
+        exec.setJsToNativeBridgeMode(exec.jsToNativeModes.PROMPT);
+        nativeApiProvider.set(nativeApi);
     });
 
     afterEach(function() {
-        cordova.callbackFromNative = origCallbackFromNative;
-        prompt = origPrompt;
+        nativeApiProvider.set(origNativeApi);
+        cordova.callbacks = {};
     });
 
     function createCallbackMessage(success, keepCallback, status, callbackId, encodedPayload)
{
@@ -47,7 +55,31 @@ describe('exec.processMessages', function () {
         return ret;
     }
 
+    describe('exec', function() {
+        it('should return payload value when plugin is synchronous', function() {
+            var winSpy = jasmine.createSpy('win');
+            nativeApi.exec.andCallFake(function(service, action, callbackId, argsJson) {
+                return createCallbackMessage(true, true, 1, callbackId, 't');
+            });
+
+            var result = exec(winSpy, null, 'Service', 'action', []);
+            expect(winSpy).toHaveBeenCalledWith(true);
+            expect(result).toBe(true);
+        });
+    });
+
     describe('processMessages', function() {
+        var origCallbackFromNative = cordova.callbackFromNative,
+            callbackSpy = jasmine.createSpy('callbackFromNative');
+
+        beforeEach(function() {
+            callbackSpy.reset();
+            cordova.callbackFromNative = callbackSpy;
+        });
+
+        afterEach(function() {
+            cordova.callbackFromNative = origCallbackFromNative;
+        });
         it('should handle payloads of false', function() {
             var messages = createCallbackMessage(true, true, 1, 'id', 'f');
             exec.processMessages(messages);
@@ -99,14 +131,14 @@ describe('exec.processMessages', function () {
         it('should poll for more messages when hitting an *', function() {
             var message1 = createCallbackMessage(false, false, 3, 'id', 'sfoo');
             var message2 = createCallbackMessage(true, true, 1, 'id', 'f');
-            prompt = jasmine.createSpy('prompt').andCallFake(function() {
+            nativeApi.retrieveJsMessages.andCallFake(function() {
                 callbackSpy.reset();
                 return message2;
             });
             var messages = message1 + '*';
             exec.processMessages(messages);
             expect(callbackSpy).toHaveBeenCalledWith('id', false, 3, 'foo', false);
-            waitsFor(function() { return prompt.wasCalled }, 500);
+            waitsFor(function() { return nativeApi.retrieveJsMessages.wasCalled }, 500);
             runs(function() {
                 expect(callbackSpy).toHaveBeenCalledWith('id', true, 1, false, true);
             });


Mime
View raw message