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

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

Issue 2632043002: Create ContentShellActivityTestRule and BaseJUnitRunner (Closed)
Patch Set: Change to adapter pattern 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.annotation.TargetApi;
10 import android.app.Activity;
11 import android.app.Instrumentation;
12 import android.content.ComponentName;
13 import android.content.Context;
14 import android.content.Intent;
15 import android.net.Uri;
16 import android.os.Build;
17 import android.os.PowerManager;
18 import android.text.TextUtils;
19 import android.view.ViewGroup;
20
21 import org.junit.Assert;
22
23 import org.chromium.base.ThreadUtils;
24 import org.chromium.base.test.util.CallbackHelper;
25 import org.chromium.base.test.util.UrlUtils;
26 import org.chromium.content.browser.ContentView;
27 import org.chromium.content.browser.ContentViewCore;
28 import org.chromium.content.browser.test.util.Criteria;
29 import org.chromium.content.browser.test.util.CriteriaHelper;
30 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
31 import org.chromium.content_public.browser.LoadUrlParams;
32 import org.chromium.content_public.browser.NavigationController;
33 import org.chromium.content_public.browser.WebContents;
34 import org.chromium.content_shell.Shell;
35
36 import java.util.concurrent.Callable;
37 import java.util.concurrent.ExecutionException;
38 import java.util.concurrent.TimeUnit;
39
40 /**
41 * Utility class for ContentShellTestBase and ContentShellActivityTestRule durin g
42 * instrumentation test JUnit3 to JUnit4 migration
43 *
44 * Please do not use this class' methods in places other than {@link ContentShel lTestBase}
45 * and {@link ContentShellActivityTestRule}
46 */
47 public final class ContentShellTestDelegate {
boliu 2017/03/02 21:12:02 Ok, let's bikeshed the name a bit.. Delegate is t
the real yoland 2017/03/02 21:26:56 I see, changed to ContentShellTestCommon, since Co
48 /** The maximum time the waitForActiveShellToBeDoneLoading method will wait. */
49 private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeo ut(10000);
50 static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15);
51
52 private final TestDelegateCallback<ContentShellActivity> mCallback;
53
54 ContentShellTestDelegate(TestDelegateCallback<ContentShellActivity> callback ) {
55 mCallback = callback;
56 }
57
58 @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
59 @SuppressWarnings("deprecation")
60 void assertScreenIsOn() {
61 PowerManager pm = (PowerManager) mCallback.getInstrumentationForDelegate ()
62 .getContext()
63 .getSystemService(Context.POWER_SERVICE);
64 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
65 Assert.assertTrue("Many tests will fail if the screen is not on.", p m.isInteractive());
66 } else {
67 Assert.assertTrue("Many tests will fail if the screen is not on.", p m.isScreenOn());
68 }
69 }
70
71 ContentShellActivity launchContentShellWithUrl(String url) {
72 Intent intent = new Intent(Intent.ACTION_MAIN);
73 intent.addCategory(Intent.CATEGORY_LAUNCHER);
74 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
75 if (url != null) intent.setData(Uri.parse(url));
76 intent.setComponent(
77 new ComponentName(mCallback.getInstrumentationForDelegate().getT argetContext(),
78 ContentShellActivity.class));
79 return mCallback.launchActivityWithIntentForDelegate(intent);
80 }
81
82 // TODO(yolandyan): This should use the url exactly without the getIsolatedT estFileUrl call.
83 void launchContentShellWithUrlSync(String url) {
84 String isolatedTestFileUrl = UrlUtils.getIsolatedTestFileUrl(url);
85 launchContentShellWithUrl(isolatedTestFileUrl);
86 Assert.assertNotNull(mCallback.getActivityForDelegate());
87 waitForActiveShellToBeDoneLoading();
88 Assert.assertEquals(UrlUtils.getIsolatedTestFileUrl(isolatedTestFileUrl) ,
89 getContentViewCore().getWebContents().getUrl());
90 }
91
92 void waitForActiveShellToBeDoneLoading() {
93 // Wait for the Content Shell to be initialized.
94 CriteriaHelper.pollUiThread(new Criteria() {
95 @Override
96 public boolean isSatisfied() {
97 Shell shell = mCallback.getActivityForDelegate().getActiveShell( );
98 // There are two cases here that need to be accounted for.
99 // The first is that we've just created a Shell and it isn't
100 // loading because it has no URL set yet. The second is that
101 // we've set a URL and it actually is loading.
102 if (shell == null) {
103 updateFailureReason("Shell is null.");
104 return false;
105 }
106 if (shell.isLoading()) {
107 updateFailureReason("Shell is still loading.");
108 return false;
109 }
110 if (TextUtils.isEmpty(shell.getContentViewCore().getWebContents( ).getUrl())) {
111 updateFailureReason("Shell's URL is empty or null.");
112 return false;
113 }
114 return true;
115 }
116 }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, CriteriaHelper.DEFAULT_POLLING _INTERVAL);
117 }
118
119 ContentViewCore getContentViewCore() {
120 return mCallback.getActivityForDelegate().getActiveShell().getContentVie wCore();
121 }
122
123 WebContents getWebContents() {
124 return mCallback.getActivityForDelegate().getActiveShell().getWebContent s();
125 }
126
127 void loadUrl(final NavigationController navigationController,
128 TestCallbackHelperContainer callbackHelperContainer, final LoadUrlPa rams params)
129 throws Throwable {
130 handleBlockingCallbackAction(
131 callbackHelperContainer.getOnPageFinishedHelper(), new Runnable( ) {
132 @Override
133 public void run() {
134 navigationController.loadUrl(params);
135 }
136 });
137 }
138
139 Shell loadNewShell(final String url) throws ExecutionException {
140 Shell shell = ThreadUtils.runOnUiThreadBlocking(new Callable<Shell>() {
141 @Override
142 public Shell call() {
143 mCallback.getActivityForDelegate().getShellManager().launchShell (url);
144 return mCallback.getActivityForDelegate().getActiveShell();
145 }
146 });
147 Assert.assertNotNull("Unable to create shell.", shell);
148 Assert.assertEquals("Active shell unexpected.", shell,
149 mCallback.getActivityForDelegate().getActiveShell());
150 waitForActiveShellToBeDoneLoading();
151 return shell;
152 }
153
154 void handleBlockingCallbackAction(CallbackHelper callbackHelper, Runnable ui ThreadAction)
155 throws Throwable {
156 int currentCallCount = callbackHelper.getCallCount();
157 mCallback.runOnUiThreadForDelegate(uiThreadAction);
158 callbackHelper.waitForCallback(
159 currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit .SECONDS);
160 }
161
162 void assertWaitForPageScaleFactorMatch(float expectedScale) {
163 CriteriaHelper.pollInstrumentationThread(
164 Criteria.equals(expectedScale, new Callable<Float>() {
165 @Override
166 public Float call() {
167 return getContentViewCore().getScale();
168 }
169 }));
170 }
171
172 void replaceContainerView() throws Throwable {
173 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
174 @Override
175 public void run() {
176 ContentView cv = ContentView.createContentView(
177 mCallback.getActivityForDelegate(), getContentViewCore() );
178 ((ViewGroup) getContentViewCore().getContainerView().getParent() ).addView(cv);
179 getContentViewCore().setContainerView(cv);
180 getContentViewCore().setContainerViewInternals(cv);
181 cv.requestFocus();
182 }
183 });
184 }
185
186 /**
187 * Interface used by TestRule and TestBase class to implement methods for Te stDelegate class
188 * to use.
189 */
190 public static interface TestDelegateCallback<T extends Activity> {
191 Instrumentation getInstrumentationForDelegate();
192 T launchActivityWithIntentForDelegate(Intent t);
193 T getActivityForDelegate();
194 void runOnUiThreadForDelegate(Runnable runnable) throws Throwable;
195 }
196 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698