Skip to content

Commit

Permalink
Wait for Window to Load Before Executing Javascript Callback (#42)
Browse files Browse the repository at this point in the history
* Run javascript event notifications after window has loaded.

Signed-off-by: Steven Shropshire <steven.shropshire@getbraintree.com>

* Update CHANGELOG.

Signed-off-by: Steven Shropshire <steven.shropshire@getbraintree.com>

* Modify CHANGELOG.

Signed-off-by: Steven Shropshire <steven.shropshire@getbraintree.com>

* Fix javascript injection unit test.

Signed-off-by: Steven Shropshire <steven.shropshire@getbraintree.com>

* Add TODOs.

Signed-off-by: Steven Shropshire <steven.shropshire@getbraintree.com>

* Remove new line restriction from regex.

Signed-off-by: Steven Shropshire <steven.shropshire@getbraintree.com>

* Remove trailing new lines from javascript source.

Signed-off-by: Steven Shropshire <steven.shropshire@getbraintree.com>

Co-authored-by: Sarah Koop <skoop@paypal.com>
  • Loading branch information
sshropshire and sarahkoop authored Mar 25, 2021
1 parent 1750848 commit 4bff178
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## unreleased

* Update browser-switch to 2.0.0-beta2
* Run `onComplete` and `onCancel` callbacks after WebView window has loaded (fixes #26)

## 4.0.0-beta1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import android.os.Bundle;
import android.webkit.WebView;

import com.braintreepayments.api.PopupBridgeClient;

import androidx.appcompat.app.AppCompatActivity;

import com.braintreepayments.api.PopupBridgeClient;

public class PopupActivity extends AppCompatActivity {

private WebView mWebView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,20 @@ private void onBrowserSwitchResult(BrowserSwitchResult result) {
Uri returnUri = result.getDeepLinkUrl();
if (result.getStatus() == BrowserSwitchStatus.CANCELED) {
runJavaScriptInWebView(""
+ "if (typeof window.popupBridge.onCancel === 'function') {"
+ " window.popupBridge.onCancel();"
+ "function notifyCanceled() {"
+ " if (typeof window.popupBridge.onCancel === 'function') {"
+ " window.popupBridge.onCancel();"
+ " } else {"
+ " window.popupBridge.onComplete(null, null);"
+ " }"
+ "}"
+ ""
+ "if (document.readyState === 'complete') {"
+ " notifyCanceled();"
+ "} else {"
+ " window.popupBridge.onComplete(null, null);"
+ " window.addEventListener('load', function () {"
+ " notifyCanceled();"
+ " });"
+ "}");
return;
} else if (result.getStatus() == BrowserSwitchStatus.SUCCESS) {
Expand Down Expand Up @@ -140,7 +150,20 @@ private void onBrowserSwitchResult(BrowserSwitchResult result) {
payload = json.toString();
}

runJavaScriptInWebView(String.format("window.popupBridge.onComplete(%s, %s);", error, payload));
String successJavascript = String.format(""
+ "function notifyComplete() {"
+ " window.popupBridge.onComplete(%s, %s);"
+ "}"
+ ""
+ "if (document.readyState === 'complete') {"
+ " notifyComplete();"
+ "} else {"
+ " window.addEventListener('load', function () {"
+ " notifyComplete();"
+ " });"
+ "}", error, payload);

runJavaScriptInWebView(successJavascript);
}

@JavascriptInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public MockWebView(Context context) {

@Override
public void evaluateJavascript(String script, ValueCallback<String> resultCallback) {
String expression = "window\\.popupBridge\\.onComplete\\((.*), (.*)\\)";
// match everything except semi-colon to capture each parameter as a whole
// without drifting into other lines
String expression = "window\\.popupBridge\\.onComplete\\(([^;]*), ([^;]*)\\);";
mJavascriptEval = script;
Pattern pattern = Pattern.compile(expression);
Matcher match = pattern.matcher(script);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void constructor_whenWebViewIsNull_throwsException() {
try {
new PopupBridgeClient(fragmentActivity, null, "my-custom-url-scheme");
fail("Should throw");
} catch (IllegalArgumentException e){
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "WebView is null");
}
}
Expand Down Expand Up @@ -190,7 +190,7 @@ public void onBrowserSwitchResult_whenReturnUrlHasNoQueryParams_reportsPayloadWi
}

@Test
public void onBrowserSwitchResult_whenReturnUrlIncludesFragmentIdentifier_reportsPayloadWithFragmentIdentifier()
public void onBrowserSwitchResult_whenReturnUrlIncludesFragmentIdentifier_reportsPayloadWithFragmentIdentifier()
throws JSONException {
BrowserSwitchResult result = mock(BrowserSwitchResult.class);
when(result.getStatus()).thenReturn(BrowserSwitchStatus.SUCCESS);
Expand Down Expand Up @@ -242,8 +242,8 @@ public void onBrowserSwitchResult_whenNoPath_returnsEmptyString() throws JSONExc
BrowserSwitchResult result = mock(BrowserSwitchResult.class);
when(result.getStatus()).thenReturn(BrowserSwitchStatus.SUCCESS);

Uri uri = new Uri.Builder()
.scheme("my-custom-url-scheme")
Uri uri = new Uri.Builder()
.scheme("my-custom-url-scheme")
.authority("popupbridgev1")
.build();
when(result.getDeepLinkUrl()).thenReturn(uri);
Expand Down

0 comments on commit 4bff178

Please sign in to comment.