@Test public void onBrowserSwitchResult_whenNotPopupBridgeRequest_doesNotCallOnComplete() { BrowserSwitchResult result = mock(BrowserSwitchResult.class); when(result.getStatus()).thenReturn(BrowserSwitchResult.STATUS_OK); mPopupBridge.onBrowserSwitchResult(1, result, null); assertNull(mWebView.mError); assertNull(mWebView.mPayload); }
@Override public void onBrowserSwitchResult(int requestCode, BrowserSwitchResult result, @Nullable Uri returnUri) { String error = null; String payload = null; if (result.getStatus() == BrowserSwitchResult.STATUS_CANCELED) { runJavaScriptInWebView("" + "if (typeof window.popupBridge.onCancel === 'function') {" + " window.popupBridge.onCancel();" + "} else {" + " window.popupBridge.onComplete(null, null);" + "}"); return; } else if (result.getStatus() == BrowserSwitchResult.STATUS_OK) { if (returnUri == null || !returnUri.getScheme().equals(getReturnUrlScheme()) || !returnUri.getHost().equals(POPUP_BRIDGE_URL_HOST)) { return; } JSONObject json = new JSONObject(); JSONObject queryItems = new JSONObject(); Set<String> queryParams = returnUri.getQueryParameterNames(); if (queryParams != null && !queryParams.isEmpty()) { for (String queryParam : queryParams) { try { queryItems.put(queryParam, returnUri.getQueryParameter(queryParam)); } catch (JSONException e) { error = "new Error('Failed to parse query items from return URL. " + e.getLocalizedMessage() + "')"; } } } try { json.put("path", returnUri.getPath()); json.put("queryItems", queryItems); json.put("hash", returnUri.getFragment()); } catch (JSONException ignored) {} payload = json.toString(); } else if (result.getStatus() == BrowserSwitchResult.STATUS_ERROR) { error = "new Error('" + result.getErrorMessage() + "')"; } runJavaScriptInWebView(String.format("window.popupBridge.onComplete(%s, %s);", error, payload)); }
PopupBridge extends BrowserSwitchFragment { @Override public void onBrowserSwitchResult(int requestCode, BrowserSwitchResult result, @Nullable Uri returnUri) { String error = null; String payload = null; if (result.getStatus() == BrowserSwitchResult.STATUS_CANCELED) { runJavaScriptInWebView("" + "if (typeof window.popupBridge.onCancel === 'function') {" + " window.popupBridge.onCancel();" + "} else {" + " window.popupBridge.onComplete(null, null);" + "}"); return; } else if (result.getStatus() == BrowserSwitchResult.STATUS_OK) { if (returnUri == null || !returnUri.getScheme().equals(getReturnUrlScheme()) || !returnUri.getHost().equals(POPUP_BRIDGE_URL_HOST)) { return; } JSONObject json = new JSONObject(); JSONObject queryItems = new JSONObject(); Set<String> queryParams = returnUri.getQueryParameterNames(); if (queryParams != null && !queryParams.isEmpty()) { for (String queryParam : queryParams) { try { queryItems.put(queryParam, returnUri.getQueryParameter(queryParam)); } catch (JSONException e) { error = "new Error('Failed to parse query items from return URL. " + e.getLocalizedMessage() + "')"; } } } try { json.put("path", returnUri.getPath()); json.put("queryItems", queryItems); json.put("hash", returnUri.getFragment()); } catch (JSONException ignored) {} payload = json.toString(); } else if (result.getStatus() == BrowserSwitchResult.STATUS_ERROR) { error = "new Error('" + result.getErrorMessage() + "')"; } runJavaScriptInWebView(String.format("window.popupBridge.onComplete(%s, %s);", error, payload)); } }
PopupBridge extends BrowserSwitchFragment { @Override public void onBrowserSwitchResult(int requestCode, BrowserSwitchResult result, @Nullable Uri returnUri) { String error = null; String payload = null; if (result.getStatus() == BrowserSwitchResult.STATUS_CANCELED) { runJavaScriptInWebView("" + "if (typeof window.popupBridge.onCancel === 'function') {" + " window.popupBridge.onCancel();" + "} else {" + " window.popupBridge.onComplete(null, null);" + "}"); return; } else if (result.getStatus() == BrowserSwitchResult.STATUS_OK) { if (returnUri == null || !returnUri.getScheme().equals(getReturnUrlScheme()) || !returnUri.getHost().equals(POPUP_BRIDGE_URL_HOST)) { return; } JSONObject json = new JSONObject(); JSONObject queryItems = new JSONObject(); Set<String> queryParams = returnUri.getQueryParameterNames(); if (queryParams != null && !queryParams.isEmpty()) { for (String queryParam : queryParams) { try { queryItems.put(queryParam, returnUri.getQueryParameter(queryParam)); } catch (JSONException e) { error = "new Error('Failed to parse query items from return URL. " + e.getLocalizedMessage() + "')"; } } } try { json.put("path", returnUri.getPath()); json.put("queryItems", queryItems); json.put("hash", returnUri.getFragment()); } catch (JSONException ignored) {} payload = json.toString(); } else if (result.getStatus() == BrowserSwitchResult.STATUS_ERROR) { error = "new Error('" + result.getErrorMessage() + "')"; } runJavaScriptInWebView(String.format("window.popupBridge.onComplete(%s, %s);", error, payload)); } PopupBridge(); }
PopupBridge extends BrowserSwitchFragment { @Override public void onBrowserSwitchResult(int requestCode, BrowserSwitchResult result, @Nullable Uri returnUri) { String error = null; String payload = null; if (result.getStatus() == BrowserSwitchResult.STATUS_CANCELED) { runJavaScriptInWebView("" + "if (typeof window.popupBridge.onCancel === 'function') {" + " window.popupBridge.onCancel();" + "} else {" + " window.popupBridge.onComplete(null, null);" + "}"); return; } else if (result.getStatus() == BrowserSwitchResult.STATUS_OK) { if (returnUri == null || !returnUri.getScheme().equals(getReturnUrlScheme()) || !returnUri.getHost().equals(POPUP_BRIDGE_URL_HOST)) { return; } JSONObject json = new JSONObject(); JSONObject queryItems = new JSONObject(); Set<String> queryParams = returnUri.getQueryParameterNames(); if (queryParams != null && !queryParams.isEmpty()) { for (String queryParam : queryParams) { try { queryItems.put(queryParam, returnUri.getQueryParameter(queryParam)); } catch (JSONException e) { error = "new Error('Failed to parse query items from return URL. " + e.getLocalizedMessage() + "')"; } } } try { json.put("path", returnUri.getPath()); json.put("queryItems", queryItems); json.put("hash", returnUri.getFragment()); } catch (JSONException ignored) {} payload = json.toString(); } else if (result.getStatus() == BrowserSwitchResult.STATUS_ERROR) { error = "new Error('" + result.getErrorMessage() + "')"; } runJavaScriptInWebView(String.format("window.popupBridge.onComplete(%s, %s);", error, payload)); } PopupBridge(); @SuppressLint("SetJavaScriptEnabled") static PopupBridge newInstance(AppCompatActivity activity, WebView webView); @SuppressLint("SetJavaScriptEnabled") static PopupBridge newInstance(FragmentActivity activity, WebView webView); @Override void onAttach(@NonNull Context context); @Override void onCreate(Bundle savedInstanceState); @Override void onBrowserSwitchResult(int requestCode, BrowserSwitchResult result, @Nullable Uri returnUri); @Override String getReturnUrlScheme(); @JavascriptInterface String getReturnUrlPrefix(); @JavascriptInterface void open(String url); @JavascriptInterface void sendMessage(String messageName); @JavascriptInterface void sendMessage(String messageName, String data); void setNavigationListener(PopupBridgeNavigationListener listener); void setMessageListener(PopupBridgeMessageListener listener); }
PopupBridge extends BrowserSwitchFragment { @Override public void onBrowserSwitchResult(int requestCode, BrowserSwitchResult result, @Nullable Uri returnUri) { String error = null; String payload = null; if (result.getStatus() == BrowserSwitchResult.STATUS_CANCELED) { runJavaScriptInWebView("" + "if (typeof window.popupBridge.onCancel === 'function') {" + " window.popupBridge.onCancel();" + "} else {" + " window.popupBridge.onComplete(null, null);" + "}"); return; } else if (result.getStatus() == BrowserSwitchResult.STATUS_OK) { if (returnUri == null || !returnUri.getScheme().equals(getReturnUrlScheme()) || !returnUri.getHost().equals(POPUP_BRIDGE_URL_HOST)) { return; } JSONObject json = new JSONObject(); JSONObject queryItems = new JSONObject(); Set<String> queryParams = returnUri.getQueryParameterNames(); if (queryParams != null && !queryParams.isEmpty()) { for (String queryParam : queryParams) { try { queryItems.put(queryParam, returnUri.getQueryParameter(queryParam)); } catch (JSONException e) { error = "new Error('Failed to parse query items from return URL. " + e.getLocalizedMessage() + "')"; } } } try { json.put("path", returnUri.getPath()); json.put("queryItems", queryItems); json.put("hash", returnUri.getFragment()); } catch (JSONException ignored) {} payload = json.toString(); } else if (result.getStatus() == BrowserSwitchResult.STATUS_ERROR) { error = "new Error('" + result.getErrorMessage() + "')"; } runJavaScriptInWebView(String.format("window.popupBridge.onComplete(%s, %s);", error, payload)); } PopupBridge(); @SuppressLint("SetJavaScriptEnabled") static PopupBridge newInstance(AppCompatActivity activity, WebView webView); @SuppressLint("SetJavaScriptEnabled") static PopupBridge newInstance(FragmentActivity activity, WebView webView); @Override void onAttach(@NonNull Context context); @Override void onCreate(Bundle savedInstanceState); @Override void onBrowserSwitchResult(int requestCode, BrowserSwitchResult result, @Nullable Uri returnUri); @Override String getReturnUrlScheme(); @JavascriptInterface String getReturnUrlPrefix(); @JavascriptInterface void open(String url); @JavascriptInterface void sendMessage(String messageName); @JavascriptInterface void sendMessage(String messageName, String data); void setNavigationListener(PopupBridgeNavigationListener listener); void setMessageListener(PopupBridgeMessageListener listener); static final String POPUP_BRIDGE_NAME; static final String POPUP_BRIDGE_URL_HOST; }