OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.android_webview.test; | 5 package org.chromium.android_webview.test; |
6 | 6 |
7 import android.os.Build; | 7 import android.os.Build; |
8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.MediumTest; |
9 | 9 |
10 import org.chromium.android_webview.AwContents; | 10 import org.chromium.android_webview.AwContents; |
11 import org.chromium.android_webview.test.util.AwTestTouchUtils; | 11 import org.chromium.android_webview.test.util.AwTestTouchUtils; |
12 import org.chromium.android_webview.test.util.CommonResources; | 12 import org.chromium.android_webview.test.util.CommonResources; |
13 import org.chromium.base.test.util.Feature; | 13 import org.chromium.base.test.util.Feature; |
14 import org.chromium.base.test.util.MinAndroidSdkLevel; | 14 import org.chromium.base.test.util.MinAndroidSdkLevel; |
| 15 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
15 import org.chromium.net.test.util.TestWebServer; | 16 import org.chromium.net.test.util.TestWebServer; |
16 | 17 |
17 import java.util.concurrent.Callable; | 18 import java.util.concurrent.Callable; |
18 import java.util.concurrent.TimeUnit; | 19 import java.util.concurrent.TimeUnit; |
19 | 20 |
20 /** | 21 /** |
21 * Tests for pop up window flow. | 22 * Tests for pop up window flow. |
22 */ | 23 */ |
23 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT) | 24 @MinAndroidSdkLevel(Build.VERSION_CODES.KITKAT) |
24 public class PopupWindowTest extends AwTestBase { | 25 public class PopupWindowTest extends AwTestBase { |
25 private TestAwContentsClient mParentContentsClient; | 26 private TestAwContentsClient mParentContentsClient; |
26 private AwTestContainerView mParentContainerView; | 27 private AwTestContainerView mParentContainerView; |
27 private AwContents mParentContents; | 28 private AwContents mParentContents; |
28 private TestAwContentsClient mPopupContentsClient; | 29 private TestAwContentsClient mPopupContentsClient; |
29 private AwTestContainerView mPopupContainerView; | 30 private AwTestContainerView mPopupContainerView; |
30 private AwContents mPopupContents; | 31 private AwContents mPopupContents; |
31 private TestWebServer mWebServer; | 32 private TestWebServer mWebServer; |
32 | 33 |
33 private static final String POPUP_TITLE = "Popup Window"; | 34 private static final String POPUP_TITLE = "Popup Window"; |
34 | 35 |
35 @Override | 36 @Override |
36 public void setUp() throws Exception { | 37 public void setUp() throws Exception { |
37 super.setUp(); | 38 super.setUp(); |
38 mParentContentsClient = new TestAwContentsClient(); | 39 mParentContentsClient = new TestAwContentsClient(); |
39 mParentContainerView = createAwTestContainerViewOnMainSync(mParentConten
tsClient); | 40 mParentContainerView = createAwTestContainerViewOnMainSync(mParentConten
tsClient); |
40 mParentContents = mParentContainerView.getAwContents(); | 41 mParentContents = mParentContainerView.getAwContents(); |
| 42 mPopupContentsClient = new TestAwContentsClient(); |
| 43 mPopupContainerView = createAwTestContainerViewOnMainSync(mPopupContents
Client); |
| 44 mPopupContents = mPopupContainerView.getAwContents(); |
41 mWebServer = TestWebServer.start(); | 45 mWebServer = TestWebServer.start(); |
42 } | 46 } |
43 | 47 |
44 @Override | 48 @Override |
45 public void tearDown() throws Exception { | 49 public void tearDown() throws Exception { |
46 if (mWebServer != null) { | 50 if (mWebServer != null) { |
47 mWebServer.shutdown(); | 51 mWebServer.shutdown(); |
48 } | 52 } |
49 super.tearDown(); | 53 super.tearDown(); |
50 } | 54 } |
51 | 55 |
52 private void triggerPopup() throws Throwable { | 56 // It is expected that the parent page contains a link that opens a popup wi
ndow, |
| 57 // and the test server is already pre-loaded with both parent and popup page
s. |
| 58 private void triggerPopup(String parentUrl) throws Throwable { |
53 enableJavaScriptOnUiThread(mParentContents); | 59 enableJavaScriptOnUiThread(mParentContents); |
54 getInstrumentation().runOnMainSync(new Runnable() { | 60 getInstrumentation().runOnMainSync(new Runnable() { |
55 @Override | 61 @Override |
56 public void run() { | 62 public void run() { |
57 mParentContents.getSettings().setSupportMultipleWindows(true); | 63 mParentContents.getSettings().setSupportMultipleWindows(true); |
58 mParentContents.getSettings().setJavaScriptCanOpenWindowsAutomat
ically(true); | 64 mParentContents.getSettings().setJavaScriptCanOpenWindowsAutomat
ically(true); |
59 } | 65 } |
60 }); | 66 }); |
61 | 67 |
62 final String popupPath = "/popup.html"; | |
63 | |
64 final String parentPageHtml = CommonResources.makeHtmlPageFrom("", | |
65 "<script>" | |
66 + "function tryOpenWindow() {" | |
67 + " var newWindow = window.open('" + popupPath + "');" | |
68 + "}</script>" | |
69 + "<a class=\"full_view\" onclick=\"tryOpenWindow();\">C
lick me!</a>"); | |
70 final String popupPageHtml = CommonResources.makeHtmlPageFrom( | |
71 "<title>" + POPUP_TITLE + "</title>", | |
72 "This is a popup window"); | |
73 | |
74 final String parentUrl = mWebServer.setResponse("/popupParent.html", par
entPageHtml, null); | |
75 mWebServer.setResponse(popupPath, popupPageHtml, null); | |
76 | |
77 mParentContentsClient.getOnCreateWindowHelper().setReturnValue(true); | 68 mParentContentsClient.getOnCreateWindowHelper().setReturnValue(true); |
78 loadUrlSync(mParentContents, | 69 loadUrlSync(mParentContents, |
79 mParentContentsClient.getOnPageFinishedHelper(), | 70 mParentContentsClient.getOnPageFinishedHelper(), |
80 parentUrl); | 71 parentUrl); |
81 | 72 |
82 TestAwContentsClient.OnCreateWindowHelper onCreateWindowHelper = | 73 TestAwContentsClient.OnCreateWindowHelper onCreateWindowHelper = |
83 mParentContentsClient.getOnCreateWindowHelper(); | 74 mParentContentsClient.getOnCreateWindowHelper(); |
84 int currentCallCount = onCreateWindowHelper.getCallCount(); | 75 int currentCallCount = onCreateWindowHelper.getCallCount(); |
85 AwTestTouchUtils.simulateTouchCenterOfView(mParentContainerView); | 76 AwTestTouchUtils.simulateTouchCenterOfView(mParentContainerView); |
86 onCreateWindowHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_M
S, | 77 onCreateWindowHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_M
S, |
87 TimeUnit.MILLISECONDS); | 78 TimeUnit.MILLISECONDS); |
88 } | 79 } |
89 | 80 |
90 private void connectPendingPopup() throws Exception { | 81 private void connectPendingPopup() throws Exception { |
91 mPopupContentsClient = new TestAwContentsClient(); | |
92 mPopupContainerView = createAwTestContainerViewOnMainSync(mPopupContents
Client); | |
93 mPopupContents = mPopupContainerView.getAwContents(); | |
94 | |
95 getInstrumentation().runOnMainSync(new Runnable() { | 82 getInstrumentation().runOnMainSync(new Runnable() { |
96 @Override | 83 @Override |
97 public void run() { | 84 public void run() { |
98 mParentContents.supplyContentsForPopup(mPopupContents); | 85 mParentContents.supplyContentsForPopup(mPopupContents); |
99 } | 86 } |
100 }); | 87 }); |
101 } | 88 } |
102 | 89 |
103 @SmallTest | 90 @MediumTest |
104 @Feature({"AndroidWebView"}) | 91 @Feature({"AndroidWebView"}) |
105 public void testPopupWindow() throws Throwable { | 92 public void testPopupWindow() throws Throwable { |
106 triggerPopup(); | 93 final String popupPath = "/popup.html"; |
| 94 final String parentPageHtml = CommonResources.makeHtmlPageFrom("", |
| 95 "<script>" |
| 96 + "function tryOpenWindow() {" |
| 97 + " var newWindow = window.open('" + popupPath + "');" |
| 98 + "}</script>" |
| 99 + "<a class=\"full_view\" onclick=\"tryOpenWindow();\">Click me!
</a>"); |
| 100 final String popupPageHtml = CommonResources.makeHtmlPageFrom( |
| 101 "<title>" + POPUP_TITLE + "</title>", |
| 102 "This is a popup window"); |
| 103 final String parentUrl = mWebServer.setResponse("/popupParent.html", par
entPageHtml, null); |
| 104 mWebServer.setResponse(popupPath, popupPageHtml, null); |
| 105 triggerPopup(parentUrl); |
107 connectPendingPopup(); | 106 connectPendingPopup(); |
108 poll(new Callable<Boolean>() { | 107 poll(new Callable<Boolean>() { |
109 @Override | 108 @Override |
110 public Boolean call() throws Exception { | 109 public Boolean call() throws Exception { |
111 return POPUP_TITLE.equals(getTitleOnUiThread(mPopupContents)); | 110 return POPUP_TITLE.equals(getTitleOnUiThread(mPopupContents)); |
112 } | 111 } |
113 }); | 112 }); |
114 } | 113 } |
| 114 |
| 115 @MediumTest |
| 116 @Feature({"AndroidWebView"}) |
| 117 public void testOnPageFinishedCalledOnDomModificationAfterNavigation() throw
s Throwable { |
| 118 final String popupPath = "/popup.html"; |
| 119 final String popupUrl = mWebServer.setResponseWithNoContentStatus(popupP
ath); |
| 120 final String parentHtml = CommonResources.makeHtmlPageFrom("", |
| 121 "<script>" |
| 122 + "function tryOpenWindow() {" |
| 123 + " window.popupWindow = window.open('" + popupPath + "');" |
| 124 + "}" |
| 125 + "function modifyDomOfPopup() {" |
| 126 + " window.popupWindow.document.body.innerHTML = 'Hello from th
e parent!';" |
| 127 + "}</script>" |
| 128 + "<a class='full_view' onclick='tryOpenWindow();'>Click me!</a>
"); |
| 129 final String parentUrl = mWebServer.setResponse("/parent.html", parentHt
ml, null); |
| 130 triggerPopup(parentUrl); |
| 131 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
| 132 mPopupContentsClient.getOnPageFinishedHelper(); |
| 133 int onPageFinishedCallCount = onPageFinishedHelper.getCallCount(); |
| 134 connectPendingPopup(); |
| 135 onPageFinishedHelper.waitForCallback(onPageFinishedCallCount); |
| 136 |
| 137 onPageFinishedCallCount = onPageFinishedHelper.getCallCount(); |
| 138 executeJavaScriptAndWaitForResult(mParentContents, mParentContentsClient
, |
| 139 "modifyDomOfPopup()"); |
| 140 onPageFinishedHelper.waitForCallback(onPageFinishedCallCount); |
| 141 assertEquals("about:blank", onPageFinishedHelper.getUrl()); |
| 142 } |
115 } | 143 } |
OLD | NEW |