OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.content_shell_apk; | |
6 | |
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | |
8 | |
9 import android.annotation.TargetApi; | |
10 import android.content.ComponentName; | |
11 import android.content.Context; | |
12 import android.content.Intent; | |
13 import android.net.Uri; | |
14 import android.os.Build; | |
15 import android.os.PowerManager; | |
16 import android.support.test.InstrumentationRegistry; | |
17 import android.support.test.rule.ActivityTestRule; | |
18 import android.text.TextUtils; | |
19 import android.view.ViewGroup; | |
20 | |
21 import org.junit.Assert; | |
22 import org.junit.runner.Description; | |
23 import org.junit.runners.model.Statement; | |
24 | |
25 import org.chromium.base.ThreadUtils; | |
26 import org.chromium.base.test.util.CallbackHelper; | |
27 import org.chromium.base.test.util.UrlUtils; | |
28 import org.chromium.content.browser.ContentView; | |
29 import org.chromium.content.browser.ContentViewCore; | |
30 import org.chromium.content.browser.test.util.Criteria; | |
31 import org.chromium.content.browser.test.util.CriteriaHelper; | |
32 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | |
33 import org.chromium.content_public.browser.LoadUrlParams; | |
34 import org.chromium.content_public.browser.NavigationController; | |
35 import org.chromium.content_public.browser.WebContents; | |
36 import org.chromium.content_shell.Shell; | |
37 | |
38 import java.lang.annotation.ElementType; | |
39 import java.lang.annotation.Retention; | |
40 import java.lang.annotation.RetentionPolicy; | |
41 import java.lang.annotation.Target; | |
42 import java.util.concurrent.Callable; | |
43 import java.util.concurrent.ExecutionException; | |
44 import java.util.concurrent.TimeUnit; | |
45 | |
46 /** | |
47 * ActivityTestRule for ContentShellActivity. | |
48 * | |
49 * Test would use this ActivityTestRule to launch or get ContentShellActivity. | |
jbudorick
2017/02/22 16:09:30
nits: "Test would" -> "Tests can"
"launch or
the real yoland
2017/02/22 19:35:37
Done
| |
50 * For more information on ActivityTestRule, check | |
jbudorick
2017/02/22 16:09:30
nit: I'm not sure this is necessary.
the real yoland
2017/02/22 19:35:37
Done
| |
51 * https://developer.android.com/reference/android/support/test/rule/ActivityTes tRule.html | |
52 */ | |
53 public class ContentShellActivityTestRule extends ActivityTestRule<ContentShellA ctivity> { | |
54 private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeo ut(10000); | |
55 | |
56 protected static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout (15); | |
57 | |
58 public ContentShellActivityTestRule() { | |
59 super(ContentShellActivity.class, false, false); | |
60 } | |
61 | |
62 public ContentShellActivityTestRule(boolean initialTouchMode, boolean launch Activity) { | |
63 super(ContentShellActivity.class, initialTouchMode, launchActivity); | |
64 } | |
65 | |
66 @Override | |
67 protected void beforeActivityLaunched() { | |
68 assertScreenIsOn(); | |
69 } | |
70 | |
71 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) | |
72 @SuppressWarnings("deprecation") | |
73 private void assertScreenIsOn() { | |
74 PowerManager pm = (PowerManager) InstrumentationRegistry.getContext().ge tSystemService( | |
75 Context.POWER_SERVICE); | |
76 | |
77 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { | |
78 Assert.assertTrue("Many tests will fail if the screen is not on.", p m.isInteractive()); | |
79 } else { | |
80 Assert.assertTrue("Many tests will fail if the screen is not on.", p m.isScreenOn()); | |
81 } | |
82 } | |
83 | |
84 /** | |
85 * Starts the ContentShell activity and loads the given URL. | |
86 * The URL can be null, in which case will default to ContentShellActivity.D EFAULT_SHELL_URL. | |
87 */ | |
88 public ContentShellActivity launchContentShellWithUrl(String url) { | |
89 Intent intent = new Intent(Intent.ACTION_MAIN); | |
90 intent.addCategory(Intent.CATEGORY_LAUNCHER); | |
91 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
92 if (url != null) intent.setData(Uri.parse(url)); | |
93 intent.setComponent( | |
94 new ComponentName(InstrumentationRegistry.getInstrumentation().g etTargetContext(), | |
95 ContentShellActivity.class)); | |
96 return launchActivity(intent); | |
97 } | |
98 | |
99 /** | |
100 * Starts the content shell activity with the provided test url. | |
101 * The url is synchronously loaded. | |
102 * @param url Test url to load. | |
103 */ | |
104 public void launchActivityWithTestUrl(String url) { | |
jbudorick
2017/02/22 16:09:29
Either leave the TODO in or address it. I think it
the real yoland
2017/02/22 19:35:37
I see, changed to launchContentShellWithUrlSync(..
jbudorick
2017/02/22 21:27:39
We should either:
1) move the getIsolatedTestFile
the real yoland
2017/02/23 01:11:59
Done 2
| |
105 launchContentShellWithUrl(UrlUtils.getIsolatedTestFileUrl(url)); | |
106 Assert.assertNotNull(getActivity()); | |
107 waitForActiveShellToBeDoneLoading(); | |
108 Assert.assertEquals(UrlUtils.getIsolatedTestFileUrl(url), | |
109 getContentViewCore().getWebContents().getUrl()); | |
110 } | |
111 | |
112 /** | |
113 * Returns the current ContentViewCore or null if there is no ContentView. | |
114 */ | |
115 public ContentViewCore getContentViewCore() { | |
116 return getActivity().getActiveShell().getContentViewCore(); | |
117 } | |
118 | |
119 /** | |
120 * Returns the WebContents of this Shell. | |
121 */ | |
122 public WebContents getWebContents() { | |
123 return getActivity().getActiveShell().getWebContents(); | |
124 } | |
125 | |
126 /** | |
127 * Waits for the Active shell to finish loading. This times out after | |
128 * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be us ed for long | |
129 * loading pages. Instead it should be used more for test initialization. Th e proper way | |
130 * to wait is to use a TestCallbackHelperContainer after the initial load is completed. | |
131 */ | |
132 public void waitForActiveShellToBeDoneLoading() { | |
133 final ContentShellActivity activity = getActivity(); | |
134 | |
135 // Wait for the Content Shell to be initialized. | |
136 CriteriaHelper.pollUiThread(new Criteria() { | |
137 @Override | |
138 public boolean isSatisfied() { | |
139 Shell shell = activity.getActiveShell(); | |
140 // There are two cases here that need to be accounted for. | |
141 // The first is that we've just created a Shell and it isn't | |
142 // loading because it has no URL set yet. The second is that | |
143 // we've set a URL and it actually is loading. | |
144 if (shell == null) { | |
145 updateFailureReason("Shell is null."); | |
146 return false; | |
147 } | |
148 if (shell.isLoading()) { | |
149 updateFailureReason("Shell is still loading."); | |
150 return false; | |
151 } | |
152 if (TextUtils.isEmpty(shell.getContentViewCore().getWebContents( ).getUrl())) { | |
153 updateFailureReason("Shell's URL is empty or null."); | |
154 return false; | |
155 } | |
156 return true; | |
157 } | |
158 }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, CriteriaHelper.DEFAULT_POLLING _INTERVAL); | |
159 } | |
160 | |
161 /** | |
162 * Creates a new {@link Shell} and waits for it to finish loading. | |
163 * @param url The URL to create the new {@link Shell} with. | |
164 * @return A new instance of a {@link Shell}. | |
165 * @throws ExecutionException | |
166 */ | |
167 public Shell loadNewShell(final String url) throws ExecutionException { | |
168 Shell shell = ThreadUtils.runOnUiThreadBlocking(new Callable<Shell>() { | |
169 @Override | |
170 public Shell call() { | |
171 getActivity().getShellManager().launchShell(url); | |
172 return getActivity().getActiveShell(); | |
173 } | |
174 }); | |
175 | |
176 Assert.assertNotNull("Unable to create shell.", shell); | |
177 Assert.assertEquals("Active shell unexpected.", shell, getActivity().get ActiveShell()); | |
178 | |
179 waitForActiveShellToBeDoneLoading(); | |
180 | |
181 return shell; | |
182 } | |
183 | |
184 /** | |
185 * Loads a URL in the specified content view. | |
186 * | |
187 * @param navigationController The navigation controller to load the URL in. | |
188 * @param callbackHelperContainer The callback helper container used to moni tor progress. | |
189 * @param params The URL params to use. | |
190 */ | |
191 public void loadUrl(final NavigationController navigationController, | |
192 TestCallbackHelperContainer callbackHelperContainer, final LoadUrlPa rams params) | |
193 throws Throwable { | |
194 handleBlockingCallbackAction( | |
195 callbackHelperContainer.getOnPageFinishedHelper(), new Runnable( ) { | |
196 @Override | |
197 public void run() { | |
198 navigationController.loadUrl(params); | |
199 } | |
200 }); | |
201 } | |
202 | |
203 /** | |
204 * Handles performing an action on the UI thread that will return when the s pecified callback | |
205 * is incremented. | |
206 * | |
207 * @param callbackHelper The callback helper that will be blocked on. | |
208 * @param action The action to be performed on the UI thread. | |
209 */ | |
210 public void handleBlockingCallbackAction(CallbackHelper callbackHelper, Runn able action) | |
211 throws Throwable { | |
212 int currentCallCount = callbackHelper.getCallCount(); | |
213 runOnUiThread(action); | |
214 callbackHelper.waitForCallback( | |
215 currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit .SECONDS); | |
216 } | |
217 | |
218 // TODO(aelias): This method needs to be removed once http://crbug.com/17951 1 is fixed. | |
219 // Meanwhile, we have to wait if the page has the <meta viewport> tag. | |
220 /** | |
221 * Waits till the ContentViewCore receives the expected page scale factor | |
222 * from the compositor and asserts that this happens. | |
223 */ | |
224 public void assertWaitForPageScaleFactorMatch(float expectedScale) { | |
225 CriteriaHelper.pollInstrumentationThread( | |
226 Criteria.equals(expectedScale, new Callable<Float>() { | |
227 @Override | |
228 public Float call() { | |
229 return getContentViewCore().getScale(); | |
230 } | |
231 })); | |
232 } | |
233 | |
234 /** | |
235 * Replaces the {@link ContentViewCore#mContainerView} with a newly created | |
236 * {@link ContentView}. | |
237 */ | |
238 @SuppressWarnings("javadoc") | |
239 public void replaceContainerView() throws Throwable { | |
240 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
241 @Override | |
242 public void run() { | |
243 ContentView cv = ContentView.createContentView(getActivity(), ge tContentViewCore()); | |
244 ((ViewGroup) getContentViewCore().getContainerView().getParent() ).addView(cv); | |
245 getContentViewCore().setContainerView(cv); | |
246 getContentViewCore().setContainerViewInternals(cv); | |
247 cv.requestFocus(); | |
248 } | |
249 }); | |
250 } | |
251 | |
252 // TODO(yolandyan): this should be done through parameterized test | |
jbudorick
2017/02/22 16:09:30
This appears to be a preland TODO.
| |
253 @Override | |
254 public Statement apply(final Statement base, final Description desc) { | |
255 return super.apply(new Statement() { | |
256 @Override | |
257 public void evaluate() throws Throwable { | |
258 base.evaluate(); | |
259 try { | |
260 if (desc.getAnnotation(RerunWithUpdatedContainerView.class) != null) { | |
261 replaceContainerView(); | |
262 base.evaluate(); | |
263 } | |
264 } catch (Throwable e) { | |
265 throw new Throwable("@RerunWithUpdatedContainerView failed." | |
266 + " See ContentShellTestBase#runTest.", | |
267 e); | |
268 } | |
269 } | |
270 }, desc); | |
271 } | |
272 | |
273 /** | |
274 * Annotation for tests that should be executed a second time after replacin g | |
275 * the ContentViewCore's container view (see {@link #apply()}). | |
276 * | |
277 * <p>Please note that activity launch is only invoked once before both runs , | |
278 * and that any state changes produced by the first run are visible to the s econd run. | |
279 */ | |
280 @Target(ElementType.METHOD) | |
281 @Retention(RetentionPolicy.RUNTIME) | |
282 public @interface RerunWithUpdatedContainerView {} | |
283 } | |
OLD | NEW |