Commit 5690fe9f authored by Jinsuk Kim's avatar Jinsuk Kim Committed by Commit Bot

Fix a bug failing Android webview CTS test

JavascriptInjector instance cached in |AwContents| was left
untouched upon destruction. This is fine for normal webview
but popup window whose WebContents is swapped out on loading
kept using the cached one, therefore caused a problem
restoring the injected Javascript interface objects into
the new WebContents. This CL fixes that by nulling it out,
and adds an instrumentation that does what the failing CTS
test does.

Bug: 800941
Change-Id: I8fb50998fd461316d6fd4dc34f6a02f95856d24d
Reviewed-on: https://chromium-review.googlesource.com/886122Reviewed-by: default avatarBo <boliu@chromium.org>
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532296}
parent 5bf2b08a
......@@ -1130,6 +1130,7 @@ public class AwContents implements SmartClipProvider {
mWebContents = null;
mWebContentsInternals = null;
mNavigationController = null;
mJavascriptInjector = null;
}
assert mNativeAwContents == 0 && mCleanupReference == null && mContentViewCore == null;
......
......@@ -526,7 +526,16 @@ public class AwActivityTestRule extends ActivityTestRule<AwTestRunnerActivity> {
/**
* Supplies the popup window with AwContents then waits for the popup window to finish loading.
*/
public PopupInfo connectPendingPopup(final AwContents parentAwContents) throws Exception {
public PopupInfo connectPendingPopup(AwContents parentAwContents) throws Exception {
PopupInfo popupInfo = createPopupContents(parentAwContents);
loadPopupContents(parentAwContents, popupInfo);
return popupInfo;
}
/**
* Creates a popup window with AwContents.
*/
public PopupInfo createPopupContents(final AwContents parentAwContents) throws Exception {
TestAwContentsClient popupContentsClient;
AwTestContainerView popupContainerView;
final AwContents popupContents;
......@@ -534,7 +543,17 @@ public class AwActivityTestRule extends ActivityTestRule<AwTestRunnerActivity> {
popupContainerView = createAwTestContainerViewOnMainSync(popupContentsClient);
popupContents = popupContainerView.getAwContents();
enableJavaScriptOnUiThread(popupContents);
return new PopupInfo(popupContentsClient, popupContainerView, popupContents);
}
/**
* Waits for the popup window to finish loading.
*/
public void loadPopupContents(final AwContents parentAwContents, PopupInfo info)
throws Exception {
TestAwContentsClient popupContentsClient = info.popupContentsClient;
AwTestContainerView popupContainerView = info.popupContainerView;
final AwContents popupContents = info.popupContents;
ThreadUtils.runOnUiThreadBlocking(
() -> parentAwContents.supplyContentsForPopup(popupContents));
......@@ -548,8 +567,6 @@ public class AwActivityTestRule extends ActivityTestRule<AwTestRunnerActivity> {
finishCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
onReceivedTitleHelper.waitForCallback(
titleCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
return new PopupInfo(popupContentsClient, popupContainerView, popupContents);
}
private boolean testMethodHasAnnotation(Class<? extends Annotation> clazz) {
......
......@@ -6,6 +6,7 @@ package org.chromium.android_webview.test;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.webkit.JavascriptInterface;
import org.junit.After;
import org.junit.Assert;
......@@ -79,6 +80,48 @@ public class PopupWindowTest {
Assert.assertEquals(POPUP_TITLE, mActivityTestRule.getTitleOnUiThread(popupContents));
}
@Test
@SmallTest
@Feature({"AndroidWebView"})
public void testJavascriptInterfaceForPopupWindow() throws Throwable {
// android.webkit.cts.WebViewTest#testJavascriptInterfaceForClientPopup
final String popupPath = "/popup.html";
final String parentPageHtml = CommonResources.makeHtmlPageFrom("",
"<script>"
+ "function tryOpenWindow() {"
+ " var newWindow = window.open('" + popupPath + "');"
+ "}</script>");
final String popupPageHtml = CommonResources.makeHtmlPageFrom(
"<title>" + POPUP_TITLE + "</title>", "This is a popup window");
mActivityTestRule.triggerPopup(mParentContents, mParentContentsClient, mWebServer,
parentPageHtml, popupPageHtml, popupPath, "tryOpenWindow()");
PopupInfo popupInfo = mActivityTestRule.createPopupContents(mParentContents);
TestAwContentsClient popupContentsClient = popupInfo.popupContentsClient;
final AwContents popupContents = popupInfo.popupContents;
class DummyJavaScriptInterface {
@JavascriptInterface
public int test() {
return 42;
}
}
final DummyJavaScriptInterface obj = new DummyJavaScriptInterface();
InstrumentationRegistry.getInstrumentation().runOnMainSync(
() -> popupContents.addJavascriptInterface(obj, "dummy"));
mActivityTestRule.loadPopupContents(mParentContents, popupInfo);
AwActivityTestRule.pollInstrumentationThread(() -> {
String ans = mActivityTestRule.executeJavaScriptAndWaitForResult(
popupContents, popupContentsClient, "dummy.test()");
return ans.equals("42");
});
}
@Test
@SmallTest
@Feature({"AndroidWebView"})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment