1 | // Copyright (c) 2012 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 android.content.ComponentName; |
8 | import android.content.Intent; |
9 | import android.net.Uri; |
10 | import android.test.ActivityInstrumentationTestCase2; |
11 | import android.text.TextUtils; |
12 | |
13 | import org.chromium.base.test.util.UrlUtils; |
14 | import org.chromium.content.browser.ContentView; |
15 | import org.chromium.content.browser.ContentViewCore; |
16 | import org.chromium.content.browser.LoadUrlParams; |
17 | import org.chromium.content.browser.test.util.CallbackHelper; |
18 | import org.chromium.content.browser.test.util.Criteria; |
19 | import org.chromium.content.browser.test.util.CriteriaHelper; |
20 | import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
21 | import org.chromium.content_shell.Shell; |
22 | |
23 | import java.util.concurrent.TimeUnit; |
24 | import java.util.concurrent.atomic.AtomicBoolean; |
25 | |
26 | /** |
27 | * Base test class for all ContentShell based tests. |
28 | */ |
29 | public class ContentShellTestBase extends ActivityInstrumentationTestCase2<ContentShellActivity> { |
30 | |
31 | /** The maximum time the waitForActiveShellToBeDoneLoading method will wait. */ |
32 | private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = 10000; |
33 | |
34 | protected static final int WAIT_PAGE_LOADING_TIMEOUT_SECONDS = 15; |
35 | |
36 | public ContentShellTestBase() { |
37 | super(ContentShellActivity.class); |
38 | } |
39 | |
40 | /** |
41 | * Starts the ContentShell activity and loads the given URL. |
42 | * The URL can be null, in which case will default to ContentShellActivity.DEFAULT_SHELL_URL. |
43 | */ |
44 | protected ContentShellActivity launchContentShellWithUrl(String url) { |
45 | return launchContentShellWithUrlAndCommandLineArgs(url, null); |
46 | } |
47 | |
48 | /** |
49 | * Starts the ContentShell activity appending the provided command line arguments |
50 | * and loads the given URL. The URL can be null, in which case will default to |
51 | * ContentShellActivity.DEFAULT_SHELL_URL. |
52 | */ |
53 | protected ContentShellActivity launchContentShellWithUrlAndCommandLineArgs(String url, |
54 | String[] commandLineArgs) { |
55 | Intent intent = new Intent(Intent.ACTION_MAIN); |
56 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
57 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
58 | if (url != null) intent.setData(Uri.parse(url)); |
59 | intent.setComponent(new ComponentName(getInstrumentation().getTargetContext(), |
60 | ContentShellActivity.class)); |
61 | if (commandLineArgs != null) { |
62 | intent.putExtra(ContentShellActivity.COMMAND_LINE_ARGS_KEY, commandLineArgs); |
63 | } |
64 | setActivityIntent(intent); |
65 | return getActivity(); |
66 | } |
67 | |
68 | // TODO(cjhopman): These functions are inconsistent with launchContentShell***. Should be |
69 | // startContentShell*** and should use the url exactly without the getTestFileUrl call. Possibly |
70 | // these two ways of starting the activity (launch* and start*) should be merged into one. |
71 | /** |
72 | * Starts the content shell activity with the provided test url. |
73 | * The url is synchronously loaded. |
74 | * @param url Test url to load. |
75 | */ |
76 | protected void startActivityWithTestUrl(String url) throws Throwable { |
77 | launchContentShellWithUrl(UrlUtils.getTestFileUrl(url)); |
78 | assertNotNull(getActivity()); |
79 | assertTrue(waitForActiveShellToBeDoneLoading()); |
80 | assertEquals(UrlUtils.getTestFileUrl(url), getContentView().getUrl()); |
81 | } |
82 | |
83 | /** |
84 | * Starts the content shell activity with the provided test url and optional command line |
85 | * arguments to append. |
86 | * The url is synchronously loaded. |
87 | * @param url Test url to load. |
88 | * @param commandLineArgs Optional command line args to append when launching the activity. |
89 | */ |
90 | protected void startActivityWithTestUrlAndCommandLineArgs( |
91 | String url, String[] commandLineArgs) throws Throwable { |
92 | launchContentShellWithUrlAndCommandLineArgs( |
93 | UrlUtils.getTestFileUrl(url), commandLineArgs); |
94 | assertNotNull(getActivity()); |
95 | assertTrue(waitForActiveShellToBeDoneLoading()); |
96 | } |
97 | |
98 | /** |
99 | * Returns the current ContentView. |
100 | */ |
101 | protected ContentView getContentView() { |
102 | return getActivity().getActiveShell().getContentView(); |
103 | } |
104 | |
105 | /** |
106 | * Returns the current ContentViewCore or null if there is no ContentView. |
107 | */ |
108 | protected ContentViewCore getContentViewCore() { |
109 | return getContentView() == null ? null : getContentView().getContentViewCore(); |
110 | } |
111 | |
112 | /** |
113 | * Waits for the Active shell to finish loading. This times out after |
114 | * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be used for long |
115 | * loading pages. Instead it should be used more for test initialization. The proper way |
116 | * to wait is to use a TestCallbackHelperContainer after the initial load is completed. |
117 | * @return Whether or not the Shell was actually finished loading. |
118 | * @throws InterruptedException |
119 | */ |
120 | protected boolean waitForActiveShellToBeDoneLoading() throws InterruptedException { |
121 | final ContentShellActivity activity = getActivity(); |
122 | |
123 | // Wait for the Content Shell to be initialized. |
124 | return CriteriaHelper.pollForCriteria(new Criteria() { |
125 | @Override |
126 | public boolean isSatisfied() { |
127 | try { |
128 | final AtomicBoolean isLoaded = new AtomicBoolean(false); |
129 | runTestOnUiThread(new Runnable() { |
130 | @Override |
131 | public void run() { |
132 | Shell shell = activity.getActiveShell(); |
133 | if (shell != null) { |
134 | // There are two cases here that need to be accounted for. |
135 | // The first is that we've just created a Shell and it isn't |
136 | // loading because it has no URL set yet. The second is that |
137 | // we've set a URL and it actually is loading. |
138 | isLoaded.set(!shell.isLoading() |
139 | && !TextUtils.isEmpty(shell.getContentView().getUrl())); |
140 | } else { |
141 | isLoaded.set(false); |
142 | } |
143 | } |
144 | }); |
145 | |
146 | return isLoaded.get(); |
147 | } catch (Throwable e) { |
148 | return false; |
149 | } |
150 | } |
151 | }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, CriteriaHelper.DEFAULT_POLLING_INTERVAL); |
152 | } |
153 | |
154 | /** |
155 | * Loads a URL in the specified content view. |
156 | * |
157 | * @param contentView The content view to load the URL in. |
158 | * @param callbackHelperContainer The callback helper container used to monitor progress. |
159 | * @param params The URL params to use. |
160 | */ |
161 | protected void loadUrl( |
162 | final ContentView contentView, TestCallbackHelperContainer callbackHelperContainer, |
163 | final LoadUrlParams params) throws Throwable { |
164 | handleBlockingCallbackAction( |
165 | callbackHelperContainer.getOnPageFinishedHelper(), |
166 | new Runnable() { |
167 | @Override |
168 | public void run() { |
169 | contentView.loadUrl(params); |
170 | } |
171 | }); |
172 | } |
173 | |
174 | /** |
175 | * Handles performing an action on the UI thread that will return when the specified callback |
176 | * is incremented. |
177 | * |
178 | * @param callbackHelper The callback helper that will be blocked on. |
179 | * @param action The action to be performed on the UI thread. |
180 | */ |
181 | protected void handleBlockingCallbackAction( |
182 | CallbackHelper callbackHelper, Runnable action) throws Throwable { |
183 | int currentCallCount = callbackHelper.getCallCount(); |
184 | runTestOnUiThread(action); |
185 | callbackHelper.waitForCallback( |
186 | currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
187 | } |
188 | |
189 | // TODO(aelias): This method needs to be removed once http://crbug.com/179511 is fixed. |
190 | // Meanwhile, we have to wait if the page has the <meta viewport> tag. |
191 | /** |
192 | * Waits till the ContentViewCore receives the expected page scale factor |
193 | * from the compositor and asserts that this happens. |
194 | */ |
195 | protected void assertWaitForPageScaleFactorMatch(final float expectedScale) |
196 | throws InterruptedException { |
197 | assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
198 | @Override |
199 | public boolean isSatisfied() { |
200 | return getContentViewCore().getScale() == expectedScale; |
201 | } |
202 | })); |
203 | } |
204 | } |