OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
8 | 8 |
9 import android.app.Activity; | |
10 import android.net.Uri; | 9 import android.net.Uri; |
| 10 import android.support.test.InstrumentationRegistry; |
11 | 11 |
12 import org.chromium.base.test.util.CallbackHelper; | 12 import org.chromium.base.test.util.CallbackHelper; |
13 import org.chromium.base.test.util.UrlUtils; | 13 import org.chromium.base.test.util.UrlUtils; |
14 import org.chromium.content.browser.test.util.DOMUtils; | 14 import org.chromium.content.browser.test.util.DOMUtils; |
15 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | 15 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
16 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
FinishedHelper; | 16 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
FinishedHelper; |
17 import org.chromium.content_shell.ShellViewAndroidDelegate.ContentIntentHandler; | 17 import org.chromium.content_shell.ShellViewAndroidDelegate.ContentIntentHandler; |
18 import org.chromium.content_shell_apk.ContentShellTestBase; | 18 import org.chromium.content_shell_apk.ContentShellActivityTestRule; |
19 | 19 |
20 import java.util.concurrent.TimeUnit; | 20 import java.util.concurrent.TimeUnit; |
21 | 21 |
22 /** | 22 /** |
23 * Base class for content detection test suites. | 23 * ActivityTestRule for content detection test suites. |
24 */ | 24 */ |
25 public class ContentDetectionTestBase extends ContentShellTestBase { | 25 public class ContentDetectionActivityTestRule extends ContentShellActivityTestRu
le { |
26 | |
27 private static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(10); | 26 private static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(10); |
28 | 27 |
29 private TestCallbackHelperContainer mCallbackHelper; | 28 private TestCallbackHelperContainer mCallbackHelper; |
30 private TestContentIntentHandler mContentIntentHandler; | 29 private TestContentIntentHandler mContentIntentHandler; |
31 | 30 |
32 /** | 31 /** |
33 * CallbackHelper for OnStartContentIntent. | 32 * CallbackHelper for OnStartContentIntent. |
34 */ | 33 */ |
35 private static class OnStartContentIntentHelper extends CallbackHelper { | 34 private static class OnStartContentIntentHelper extends CallbackHelper { |
36 private String mIntentUrl; | 35 private String mIntentUrl; |
(...skipping 23 matching lines...) Expand all Loading... |
60 @Override | 59 @Override |
61 public void onIntentUrlReceived(String intentUrl) { | 60 public void onIntentUrlReceived(String intentUrl) { |
62 mOnStartContentIntentHelper.notifyCalled(intentUrl); | 61 mOnStartContentIntentHelper.notifyCalled(intentUrl); |
63 } | 62 } |
64 } | 63 } |
65 | 64 |
66 /** | 65 /** |
67 * Returns the TestCallbackHelperContainer associated with this ContentView, | 66 * Returns the TestCallbackHelperContainer associated with this ContentView, |
68 * or creates it lazily. | 67 * or creates it lazily. |
69 */ | 68 */ |
70 protected TestCallbackHelperContainer getTestCallbackHelperContainer() { | 69 public TestCallbackHelperContainer getTestCallbackHelperContainer() { |
71 if (mCallbackHelper == null) { | 70 if (mCallbackHelper == null) { |
72 mCallbackHelper = new TestCallbackHelperContainer(getContentViewCore
()); | 71 mCallbackHelper = new TestCallbackHelperContainer(getContentViewCore
()); |
73 } | 72 } |
74 return mCallbackHelper; | 73 return mCallbackHelper; |
75 } | 74 } |
76 | 75 |
77 @Override | 76 @Override |
78 protected void setUp() throws Exception { | 77 protected void beforeActivityLaunched() { |
79 super.setUp(); | 78 super.beforeActivityLaunched(); |
80 mContentIntentHandler = new TestContentIntentHandler(); | 79 mContentIntentHandler = new TestContentIntentHandler(); |
81 } | 80 } |
82 | 81 |
83 @Override | 82 @Override |
84 protected void setActivity(Activity activity) { | 83 protected void afterActivityLaunched() { |
85 super.setActivity(activity); | |
86 getActivity() | 84 getActivity() |
87 .getShellManager() | 85 .getShellManager() |
88 .getActiveShell() | 86 .getActiveShell() |
89 .getViewAndroidDelegate() | 87 .getViewAndroidDelegate() |
90 .setContentIntentHandler(mContentIntentHandler); | 88 .setContentIntentHandler(mContentIntentHandler); |
91 } | 89 } |
92 | 90 |
93 /** | 91 /** |
94 * Encodes the provided content string into an escaped url as intents do. | 92 * Encodes the provided content string into an escaped url as intents do. |
95 * @param content Content to escape into a url. | 93 * @param content Content to escape into a url. |
96 * @return Escaped url. | 94 * @return Escaped url. |
97 */ | 95 */ |
98 protected static String urlForContent(String content) { | 96 public String urlForContent(String content) { |
99 return Uri.encode(content).replaceAll("%20", "+"); | 97 return Uri.encode(content).replaceAll("%20", "+"); |
100 } | 98 } |
101 | 99 |
102 /** | 100 /** |
103 * Checks if the provided test url is the current url in the content view. | 101 * Checks if the provided test url is the current url in the content view. |
104 * @param testUrl Test url to check. | 102 * @param testUrl Test url to check. |
105 * @return true if the test url is the current one, false otherwise. | 103 * @return true if the test url is the current one, false otherwise. |
106 */ | 104 */ |
107 protected boolean isCurrentTestUrl(String testUrl) { | 105 public boolean isCurrentTestUrl(String testUrl) { |
108 return UrlUtils.getIsolatedTestFileUrl(testUrl).equals(getContentViewCor
e() | 106 return UrlUtils.getIsolatedTestFileUrl(testUrl).equals( |
109 .getWebContents().getUrl()); | 107 getContentViewCore().getWebContents().getUrl()); |
110 } | 108 } |
111 | 109 |
112 /** | 110 /** |
113 * Scrolls to the node with the provided id, taps on it and waits for an int
ent to come. | 111 * Scrolls to the node with the provided id, taps on it and waits for an int
ent to come. |
114 * @param id Id of the node to scroll and tap. | 112 * @param id Id of the node to scroll and tap. |
115 * @return The content url of the received intent or null if none. | 113 * @return The content url of the received intent or null if none. |
116 */ | 114 */ |
117 protected String scrollAndTapExpectingIntent(String id) throws Throwable { | 115 public String scrollAndTapExpectingIntent(String id) throws Throwable { |
118 OnStartContentIntentHelper onStartContentIntentHelper = | 116 OnStartContentIntentHelper onStartContentIntentHelper = |
119 mContentIntentHandler.getOnStartContentIntentHelper(); | 117 mContentIntentHandler.getOnStartContentIntentHelper(); |
120 int currentCallCount = onStartContentIntentHelper.getCallCount(); | 118 int currentCallCount = onStartContentIntentHelper.getCallCount(); |
121 | 119 |
122 DOMUtils.clickNode(getContentViewCore(), id); | 120 DOMUtils.clickNode(getContentViewCore(), id); |
123 | 121 |
124 onStartContentIntentHelper.waitForCallback(currentCallCount, 1, WAIT_TIM
EOUT_SECONDS, | 122 onStartContentIntentHelper.waitForCallback( |
125 TimeUnit.SECONDS); | 123 currentCallCount, 1, WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
126 getInstrumentation().waitForIdleSync(); | 124 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
127 return onStartContentIntentHelper.getIntentUrl(); | 125 return onStartContentIntentHelper.getIntentUrl(); |
128 } | 126 } |
129 | 127 |
130 /** | 128 /** |
131 * Scrolls to the node with the provided id, taps on it and waits for a new
page load to finish. | 129 * Scrolls to the node with the provided id, taps on it and waits for a new
page load to finish. |
132 * Useful when tapping on links that take to other pages. | 130 * Useful when tapping on links that take to other pages. |
133 * @param id Id of the node to scroll and tap. | 131 * @param id Id of the node to scroll and tap. |
134 * @return The content url of the received intent or null if none. | 132 * @return The content url of the received intent or null if none. |
135 */ | 133 */ |
136 protected void scrollAndTapNavigatingOut(String id) throws Throwable { | 134 public void scrollAndTapNavigatingOut(String id) throws Throwable { |
137 TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHel
perContainer(); | 135 TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHel
perContainer(); |
138 OnPageFinishedHelper onPageFinishedHelper = | 136 OnPageFinishedHelper onPageFinishedHelper = |
139 callbackHelperContainer.getOnPageFinishedHelper(); | 137 callbackHelperContainer.getOnPageFinishedHelper(); |
140 int currentCallCount = onPageFinishedHelper.getCallCount(); | 138 int currentCallCount = onPageFinishedHelper.getCallCount(); |
141 | 139 |
142 DOMUtils.clickNode(getContentViewCore(), id); | 140 DOMUtils.clickNode(getContentViewCore(), id); |
143 | 141 |
144 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, | 142 onPageFinishedHelper.waitForCallback( |
145 TimeUnit.SECONDS); | 143 currentCallCount, 1, WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
146 getInstrumentation().waitForIdleSync(); | 144 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
147 } | 145 } |
148 } | 146 } |
OLD | NEW |