Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellActivityTestRule.java

Issue 2632043002: Create ContentShellActivityTestRule and BaseJUnitRunner (Closed)
Patch Set: Refactor to TestBase and TestRule to use same delegate class (Only ContentShellTestBase) Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.support.test.InstrumentationRegistry;
10 import android.support.test.rule.ActivityTestRule;
11
12 import org.junit.Assert;
13
14 import org.chromium.base.test.util.CallbackHelper;
15 import org.chromium.base.test.util.UrlUtils;
16 import org.chromium.content.browser.ContentView;
17 import org.chromium.content.browser.ContentViewCore;
18 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
19 import org.chromium.content_public.browser.LoadUrlParams;
20 import org.chromium.content_public.browser.NavigationController;
21 import org.chromium.content_public.browser.WebContents;
22 import org.chromium.content_shell.Shell;
23
24 import java.lang.annotation.ElementType;
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 import java.lang.annotation.Target;
28 import java.util.concurrent.ExecutionException;
29
30 /**
31 * ActivityTestRule for ContentShellActivity.
32 *
33 * Test can use this ActivityTestRule to launch or get ContentShellActivity.
34 */
35 public class ContentShellActivityTestRule extends ActivityTestRule<ContentShellA ctivity> {
36 private final boolean mLaunchActivity;
37 private final ContentShellTestBaseDelegate mDelegate;
38
39 protected static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout (15);
40
41 public ContentShellActivityTestRule() {
42 this(false, false);
43 }
44
45 public ContentShellActivityTestRule(boolean initialTouchMode, boolean launch Activity) {
46 super(ContentShellActivity.class, initialTouchMode, launchActivity);
47 mLaunchActivity = launchActivity;
48 mDelegate = new ContentShellTestBaseDelegate();
49 }
50
51 @Override
52 protected void beforeActivityLaunched() {
53 mDelegate.assertScreenIsOn(InstrumentationRegistry.getInstrumentation()) ;
54 }
55
56 /**
57 * Starts the ContentShell activity and loads the given URL.
58 * The URL can be null, in which case will default to ContentShellActivity.D EFAULT_SHELL_URL.
59 */
60 public ContentShellActivity launchContentShellWithUrl(String url) {
61 Assert.assertFalse(mLaunchActivity);
62 return launchActivity(
63 mDelegate.getActivityLaunchIntent(InstrumentationRegistry.getTar getContext(), url));
64 }
65
66 // TODO(yolandyan): This should use the url exactly without the getIsolatedT estFileUrl call.
67 /**
68 * Starts the content shell activity with the provided test url.
69 * The url is synchronously loaded.
70 * @param url Test url to load.
71 */
72 public void launchContentShellWithUrlSync(String url) {
boliu 2017/03/01 23:01:23 can this move to delegate too? I guess you would l
the real yoland 2017/03/02 20:16:48 Changed to adapter pattern
73 launchContentShellWithUrl(UrlUtils.getIsolatedTestFileUrl(url));
74 Assert.assertNotNull(getActivity());
75 waitForActiveShellToBeDoneLoading();
76 Assert.assertEquals(UrlUtils.getIsolatedTestFileUrl(url),
77 getContentViewCore().getWebContents().getUrl());
78 }
79
80 /**
81 * Returns the current ContentViewCore or null if there is no ContentView.
82 */
83 public ContentViewCore getContentViewCore() {
84 return mDelegate.getContentViewCore(getActivity());
85 }
86
87 /**
88 * Returns the WebContents of this Shell.
89 */
90 public WebContents getWebContents() {
91 return mDelegate.getWebContents(getActivity());
92 }
93
94 /**
95 * Waits for the Active shell to finish loading. This times out after
96 * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be us ed for long
97 * loading pages. Instead it should be used more for test initialization. Th e proper way
98 * to wait is to use a TestCallbackHelperContainer after the initial load is completed.
99 */
100 public void waitForActiveShellToBeDoneLoading() {
101 mDelegate.waitForActiveShellToBeDoneLoading(getActivity());
102 }
103
104 /**
105 * Creates a new {@link Shell} and waits for it to finish loading.
106 * @param url The URL to create the new {@link Shell} with.
107 * @return A new instance of a {@link Shell}.
108 * @throws ExecutionException
109 */
110 public Shell loadNewShell(final String url) throws ExecutionException {
111 return mDelegate.loadNewShell(getActivity(), url);
112 }
113
114 /**
115 * Loads a URL in the specified content view.
116 *
117 * @param navigationController The navigation controller to load the URL in.
118 * @param callbackHelperContainer The callback helper container used to moni tor progress.
119 * @param params The URL params to use.
120 */
121 public void loadUrl(final NavigationController navigationController,
boliu 2017/03/01 23:01:23 this and handleBlockingCallbackAction can just mov
the real yoland 2017/03/02 20:16:48 Done
122 TestCallbackHelperContainer callbackHelperContainer, final LoadUrlPa rams params)
123 throws Throwable {
124 handleBlockingCallbackAction(
125 callbackHelperContainer.getOnPageFinishedHelper(), new Runnable( ) {
126 @Override
127 public void run() {
128 navigationController.loadUrl(params);
129 }
130 });
131 }
132
133 /**
134 * Handles performing an action on the UI thread that will return when the s pecified callback
135 * is incremented.
136 *
137 * @param callbackHelper The callback helper that will be blocked on.
138 * @param action The action to be performed on the UI thread.
139 */
140 public void handleBlockingCallbackAction(CallbackHelper callbackHelper, fina l Runnable action)
141 throws Throwable {
142 Runnable uiThreadRunnable = new Runnable() {
143 @Override
144 public void run() {
145 try {
146 runOnUiThread(action);
147 } catch (Throwable e) {
148 throw new RuntimeException("Unable to run on Ui thread");
149 }
150 }
151 };
152 mDelegate.handleBlockingCallbackAction(callbackHelper, uiThreadRunnable) ;
153 }
154
155 // TODO(aelias): This method needs to be removed once http://crbug.com/17951 1 is fixed.
156 // Meanwhile, we have to wait if the page has the <meta viewport> tag.
157 /**
158 * Waits till the ContentViewCore receives the expected page scale factor
159 * from the compositor and asserts that this happens.
160 */
161 public void assertWaitForPageScaleFactorMatch(float expectedScale) {
162 mDelegate.assertWaitForPageScaleFactorMatch(getContentViewCore(), expect edScale);
163 }
164
165 /**
166 * Replaces the {@link ContentViewCore#mContainerView} with a newly created
167 * {@link ContentView}.
168 */
169 @SuppressWarnings("javadoc")
170 public void replaceContainerView() throws Throwable {
171 mDelegate.replaceContainerView(getActivity());
172 }
173
174 /**
175 * Annotation for tests that should be executed a second time after replacin g
176 * the ContentViewCore's container view.
177 * <p>Please note that activity launch is only invoked once before both runs ,
178 * and that any state changes produced by the first run are visible to the s econd run.
179 */
180 @Target(ElementType.METHOD)
181 @Retention(RetentionPolicy.RUNTIME)
182 public @interface RerunWithUpdatedContainerView {}
183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698