Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for Window to Load Before Executing Javascript Callback #42

Merged
merged 7 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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