Skip to content

Commit

Permalink
Test WebView without a WebViewClient
Browse files Browse the repository at this point in the history
This is the supposed behavior when a WebView is created with no
WebViewClient:
case 1. loadUrl will load the url directly in the webview without
creating an intent and query the OS to handle the intent. If the loaded
url is redirected by the server, then a browser intent with the
redirected url will be sent.
case 2. clicking on the links should create an intent instead of loading
the link in WebView

BUG=499331

Review URL: https://codereview.chromium.org/1181623005

Cr-Commit-Position: refs/heads/master@{#334526}
  • Loading branch information
hush authored and Commit bot committed Jun 16, 2015
1 parent 6f36a70 commit e46d5d7
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.MinAndroidSdkLevel;
import org.chromium.content.browser.test.util.CallbackHelper;
import org.chromium.content.browser.test.util.DOMUtils;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageStartedHelper;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnReceivedErrorHelper;
import org.chromium.content_public.browser.LoadUrlParams;
Expand Down Expand Up @@ -870,4 +871,97 @@ public Boolean call() {
}
});
}

@SmallTest
@Feature({"AndroidWebView"})
public void testNullContentsClientWithServerRedirect() throws Throwable {
try {
// The test will fire real intents through the test activity.
// Need to temporarily suppress startActivity otherwise there will be a
// handler selection window and the test can't dismiss that.
getActivity().setIgnoreStartActivity(true);
final String testUrl = mWebServer.setResponse("/" + CommonResources.ABOUT_FILENAME,
CommonResources.ABOUT_HTML, CommonResources.getTextHtmlHeaders(true));
AwTestContainerView awTestContainerView =
createAwTestContainerViewOnMainSync(new NullContentsClient() {
@Override
public boolean hasWebViewClient() {
return false;
}
});
final AwContents awContents = awTestContainerView.getAwContents();
loadUrlAsync(awContents, testUrl);
pollOnUiThread(new Callable<Boolean>() {
@Override
public Boolean call() {
return awContents.getTitle().equals(CommonResources.ABOUT_TITLE);
}
});

assertNull(getActivity().getLastSentIntent());

// Now the server will redirect path1 to path2. Path2 will load ABOUT_HTML.
// AwContents should create an intent for the server initiated redirection.
final String path1 = "/from.html";
final String path2 = "/to.html";
final String fromUrl = mWebServer.setRedirect(path1, path2);
final String toUrl = mWebServer.setResponse(
path2, CommonResources.ABOUT_HTML, CommonResources.getTextHtmlHeaders(true));
loadUrlAsync(awContents, fromUrl);

pollOnUiThread(new Callable<Boolean>() {
@Override
public Boolean call() {
return getActivity().getLastSentIntent() != null;
}
});
assertEquals(toUrl, getActivity().getLastSentIntent().getData().toString());
} finally {
getActivity().setIgnoreStartActivity(false);
}
}

@SmallTest
@Feature({"AndroidWebView"})
public void testNullContentsClientOpenLink() throws Throwable {
try {
// The test will fire real intents through the test activity.
// Need to temporarily suppress startActivity otherwise there will be a
// handler selection window and the test can't dismiss that.
getActivity().setIgnoreStartActivity(true);
final String testUrl = mWebServer.setResponse("/" + CommonResources.ABOUT_FILENAME,
CommonResources.ABOUT_HTML, CommonResources.getTextHtmlHeaders(true));
AwTestContainerView awTestContainerView =
createAwTestContainerViewOnMainSync(new NullContentsClient() {
@Override
public boolean hasWebViewClient() {
return false;
}
});
final AwContents awContents = awTestContainerView.getAwContents();
// Clicking on a link should create an intent.
final String htmlWithLink = "<html><title>Click Title</title><body><a id='link' href='"
+ testUrl + "'>Click this!</a></body></html>";
final String urlWithLink = mWebServer.setResponse(
"/html_with_link.html", htmlWithLink, CommonResources.getTextHtmlHeaders(true));
loadUrlAsync(awContents, urlWithLink);
pollOnUiThread(new Callable<Boolean>() {
@Override
public Boolean call() {
return awContents.getTitle().equals("Click Title");
}
});
awContents.getSettings().setJavaScriptEnabled(true);
DOMUtils.clickNode(this, awContents.getContentViewCore(), "link");
pollOnUiThread(new Callable<Boolean>() {
@Override
public Boolean call() {
return getActivity().getLastSentIntent() != null;
}
});
assertEquals(testUrl, getActivity().getLastSentIntent().getData().toString());
} finally {
getActivity().setIgnoreStartActivity(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.chromium.android_webview.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
Expand All @@ -16,6 +17,8 @@
public class AwTestRunnerActivity extends Activity {

private LinearLayout mLinearLayout;
private Intent mLastSentIntent;
private boolean mIgnoreStartActivity;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -49,4 +52,18 @@ public void addView(View view) {
public void removeAllViews() {
mLinearLayout.removeAllViews();
}

@Override
public void startActivity(Intent i) {
mLastSentIntent = i;
if (!mIgnoreStartActivity) super.startActivity(i);
}

public Intent getLastSentIntent() {
return mLastSentIntent;
}

public void setIgnoreStartActivity(boolean ignore) {
mIgnoreStartActivity = ignore;
}
}

0 comments on commit e46d5d7

Please sign in to comment.