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

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

Issue 2632043002: Create ContentShellActivityTestRule and BaseJUnitRunner (Closed)
Patch Set: Change javadoc 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
« no previous file with comments | « content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBase.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 * Implementation of utility methods for ContentShellTestBase and ContentShellAc tivityTestRule to
42 * wrap around during 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 ContentShellTestCommon {
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 TestCommonCallback<ContentShellActivity> mCallback;
53
54 ContentShellTestCommon(TestCommonCallback<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.getInstrumentationForTestComm on()
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.getInstrumentationForTestCommon().ge tTargetContext(),
78 ContentShellActivity.class));
79 return mCallback.launchActivityWithIntentForTestCommon(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.getActivityForTestCommon());
87 waitForActiveShellToBeDoneLoading();
88 Assert.assertEquals(isolatedTestFileUrl, getContentViewCore().getWebCont ents().getUrl());
89 }
90
91 void waitForActiveShellToBeDoneLoading() {
92 // Wait for the Content Shell to be initialized.
93 CriteriaHelper.pollUiThread(new Criteria() {
94 @Override
95 public boolean isSatisfied() {
96 Shell shell = mCallback.getActivityForTestCommon().getActiveShel l();
97 // There are two cases here that need to be accounted for.
98 // The first is that we've just created a Shell and it isn't
99 // loading because it has no URL set yet. The second is that
100 // we've set a URL and it actually is loading.
101 if (shell == null) {
102 updateFailureReason("Shell is null.");
103 return false;
104 }
105 if (shell.isLoading()) {
106 updateFailureReason("Shell is still loading.");
107 return false;
108 }
109 if (TextUtils.isEmpty(shell.getContentViewCore().getWebContents( ).getUrl())) {
110 updateFailureReason("Shell's URL is empty or null.");
111 return false;
112 }
113 return true;
114 }
115 }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, CriteriaHelper.DEFAULT_POLLING _INTERVAL);
116 }
117
118 ContentViewCore getContentViewCore() {
119 return mCallback.getActivityForTestCommon().getActiveShell().getContentV iewCore();
120 }
121
122 WebContents getWebContents() {
123 return mCallback.getActivityForTestCommon().getActiveShell().getWebConte nts();
124 }
125
126 void loadUrl(final NavigationController navigationController,
127 TestCallbackHelperContainer callbackHelperContainer, final LoadUrlPa rams params)
128 throws Throwable {
129 handleBlockingCallbackAction(
130 callbackHelperContainer.getOnPageFinishedHelper(), new Runnable( ) {
131 @Override
132 public void run() {
133 navigationController.loadUrl(params);
134 }
135 });
136 }
137
138 Shell loadNewShell(final String url) throws ExecutionException {
139 Shell shell = ThreadUtils.runOnUiThreadBlocking(new Callable<Shell>() {
140 @Override
141 public Shell call() {
142 mCallback.getActivityForTestCommon().getShellManager().launchShe ll(url);
143 return mCallback.getActivityForTestCommon().getActiveShell();
144 }
145 });
146 Assert.assertNotNull("Unable to create shell.", shell);
147 Assert.assertEquals("Active shell unexpected.", shell,
148 mCallback.getActivityForTestCommon().getActiveShell());
149 waitForActiveShellToBeDoneLoading();
150 return shell;
151 }
152
153 void handleBlockingCallbackAction(CallbackHelper callbackHelper, Runnable ui ThreadAction)
154 throws Throwable {
155 int currentCallCount = callbackHelper.getCallCount();
156 mCallback.runOnUiThreadForTestCommon(uiThreadAction);
157 callbackHelper.waitForCallback(
158 currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit .SECONDS);
159 }
160
161 void assertWaitForPageScaleFactorMatch(float expectedScale) {
162 CriteriaHelper.pollInstrumentationThread(
163 Criteria.equals(expectedScale, new Callable<Float>() {
164 @Override
165 public Float call() {
166 return getContentViewCore().getScale();
167 }
168 }));
169 }
170
171 void replaceContainerView() throws Throwable {
172 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
173 @Override
174 public void run() {
175 ContentView cv = ContentView.createContentView(
176 mCallback.getActivityForTestCommon(), getContentViewCore ());
177 ((ViewGroup) getContentViewCore().getContainerView().getParent() ).addView(cv);
178 getContentViewCore().setContainerView(cv);
179 getContentViewCore().setContainerViewInternals(cv);
180 cv.requestFocus();
181 }
182 });
183 }
184
185 /**
186 * Interface used by TestRule and TestBase class to implement methods for Te stCommonCallback
187 * class to use.
188 */
189 public static interface TestCommonCallback<T extends Activity> {
190 Instrumentation getInstrumentationForTestCommon();
191 T launchActivityWithIntentForTestCommon(Intent t);
192 T getActivityForTestCommon();
193 void runOnUiThreadForTestCommon(Runnable runnable) throws Throwable;
194 }
195 }
OLDNEW
« no previous file with comments | « content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBase.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698