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