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

Side by Side Diff: content/public/android/javatests/src/org/chromium/content/browser/ContentDetectionTestBase.java

Issue 2739603003: Add TestRules for content shell test and refactor test bases (Closed)
Patch Set: limit API access 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
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.content.browser; 5 package org.chromium.content.browser;
6 6
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; 7 import android.app.Activity;
8 8
9 import android.app.Activity;
10 import android.net.Uri;
11
12 import org.chromium.base.test.util.CallbackHelper;
13 import org.chromium.base.test.util.UrlUtils;
14 import org.chromium.content.browser.test.util.DOMUtils;
15 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; 9 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
16 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage FinishedHelper; 10 import org.chromium.content_shell_apk.ContentShellActivity;
17 import org.chromium.content_shell.ShellViewAndroidDelegate.ContentIntentHandler;
18 import org.chromium.content_shell_apk.ContentShellTestBase; 11 import org.chromium.content_shell_apk.ContentShellTestBase;
19 12 import org.chromium.content_shell_apk.ContentShellTestCommon.TestCommonCallback;
20 import java.util.concurrent.TimeUnit;
21 13
22 /** 14 /**
23 * Base class for content detection test suites. 15 * Base class for content detection test suites.
24 */ 16 */
25 public class ContentDetectionTestBase extends ContentShellTestBase { 17 public class ContentDetectionTestBase
26 18 extends ContentShellTestBase implements TestCommonCallback<ContentShellA ctivity> {
27 private static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(10); 19 private final ContentDetectionTestCommon mTestCommon = new ContentDetectionT estCommon(this);
28
29 private TestCallbackHelperContainer mCallbackHelper;
30 private TestContentIntentHandler mContentIntentHandler;
31
32 /**
33 * CallbackHelper for OnStartContentIntent.
34 */
35 private static class OnStartContentIntentHelper extends CallbackHelper {
36 private String mIntentUrl;
37 public void notifyCalled(String intentUrl) {
38 mIntentUrl = intentUrl;
39 notifyCalled();
40 }
41 public String getIntentUrl() {
42 assert getCallCount() > 0;
43 return mIntentUrl;
44 }
45 }
46
47 /**
48 * ContentIntentHandler impl to test content detection.
49 */
50 private static class TestContentIntentHandler implements ContentIntentHandle r {
51 private OnStartContentIntentHelper mOnStartContentIntentHelper;
52
53 public OnStartContentIntentHelper getOnStartContentIntentHelper() {
54 if (mOnStartContentIntentHelper == null) {
55 mOnStartContentIntentHelper = new OnStartContentIntentHelper();
56 }
57 return mOnStartContentIntentHelper;
58 }
59
60 @Override
61 public void onIntentUrlReceived(String intentUrl) {
62 mOnStartContentIntentHelper.notifyCalled(intentUrl);
63 }
64 }
65 20
66 /** 21 /**
67 * Returns the TestCallbackHelperContainer associated with this ContentView, 22 * Returns the TestCallbackHelperContainer associated with this ContentView,
68 * or creates it lazily. 23 * or creates it lazily.
69 */ 24 */
70 protected TestCallbackHelperContainer getTestCallbackHelperContainer() { 25 protected TestCallbackHelperContainer getTestCallbackHelperContainer() {
71 if (mCallbackHelper == null) { 26 return mTestCommon.getTestCallbackHelperContainer();
72 mCallbackHelper = new TestCallbackHelperContainer(getContentViewCore ());
73 }
74 return mCallbackHelper;
75 } 27 }
76 28
77 @Override 29 @Override
78 protected void setUp() throws Exception { 30 protected void setUp() throws Exception {
79 super.setUp(); 31 super.setUp();
80 mContentIntentHandler = new TestContentIntentHandler(); 32 mTestCommon.createTestContentIntentHandler();
81 } 33 }
82 34
35 @SuppressWarnings("deprecation")
83 @Override 36 @Override
84 protected void setActivity(Activity activity) { 37 protected void setActivity(Activity activity) {
85 super.setActivity(activity); 38 super.setActivity(activity);
86 getActivity() 39 mTestCommon.setContentHandler();
87 .getShellManager()
88 .getActiveShell()
89 .getViewAndroidDelegate()
90 .setContentIntentHandler(mContentIntentHandler);
91 } 40 }
92 41
93 /** 42 /**
94 * Encodes the provided content string into an escaped url as intents do.
95 * @param content Content to escape into a url.
96 * @return Escaped url.
97 */
98 protected static String urlForContent(String content) {
99 return Uri.encode(content).replaceAll("%20", "+");
100 }
101
102 /**
103 * Checks if the provided test url is the current url in the content view. 43 * Checks if the provided test url is the current url in the content view.
104 * @param testUrl Test url to check. 44 * @param testUrl Test url to check.
105 * @return true if the test url is the current one, false otherwise. 45 * @return true if the test url is the current one, false otherwise.
106 */ 46 */
107 protected boolean isCurrentTestUrl(String testUrl) { 47 protected boolean isCurrentTestUrl(String testUrl) {
108 return UrlUtils.getIsolatedTestFileUrl(testUrl).equals(getContentViewCor e() 48 return mTestCommon.isCurrentTestUrl(testUrl);
109 .getWebContents().getUrl());
110 } 49 }
111 50
112 /** 51 /**
52 * Encodes the provided content string into an escaped url as intents do.
53 * @param content Content to escape into a url.
54 * @return Escaped url.
55 */
56 public static String urlForContent(String content) {
57 return ContentDetectionTestCommon.urlForContent(content);
58 }
59
60 /**
113 * Scrolls to the node with the provided id, taps on it and waits for an int ent to come. 61 * Scrolls to the node with the provided id, taps on it and waits for an int ent to come.
114 * @param id Id of the node to scroll and tap. 62 * @param id Id of the node to scroll and tap.
115 * @return The content url of the received intent or null if none. 63 * @return The content url of the received intent or null if none.
116 */ 64 */
117 protected String scrollAndTapExpectingIntent(String id) throws Throwable { 65 protected String scrollAndTapExpectingIntent(String id) throws Throwable {
118 OnStartContentIntentHelper onStartContentIntentHelper = 66 return mTestCommon.scrollAndTapExpectingIntent(id);
119 mContentIntentHandler.getOnStartContentIntentHelper();
120 int currentCallCount = onStartContentIntentHelper.getCallCount();
121
122 DOMUtils.clickNode(getContentViewCore(), id);
123
124 onStartContentIntentHelper.waitForCallback(currentCallCount, 1, WAIT_TIM EOUT_SECONDS,
125 TimeUnit.SECONDS);
126 getInstrumentation().waitForIdleSync();
127 return onStartContentIntentHelper.getIntentUrl();
128 } 67 }
129 68
130 /** 69 /**
131 * Scrolls to the node with the provided id, taps on it and waits for a new page load to finish. 70 * Scrolls to the node with the provided id, taps on it and waits for a new page load to finish.
132 * Useful when tapping on links that take to other pages. 71 * Useful when tapping on links that take to other pages.
133 * @param id Id of the node to scroll and tap. 72 * @param id Id of the node to scroll and tap.
134 * @return The content url of the received intent or null if none.
135 */ 73 */
136 protected void scrollAndTapNavigatingOut(String id) throws Throwable { 74 protected void scrollAndTapNavigatingOut(String id) throws Throwable {
137 TestCallbackHelperContainer callbackHelperContainer = getTestCallbackHel perContainer(); 75 mTestCommon.scrollAndTapNavigatingOut(id);
138 OnPageFinishedHelper onPageFinishedHelper =
139 callbackHelperContainer.getOnPageFinishedHelper();
140 int currentCallCount = onPageFinishedHelper.getCallCount();
141
142 DOMUtils.clickNode(getContentViewCore(), id);
143
144 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S ECONDS,
145 TimeUnit.SECONDS);
146 getInstrumentation().waitForIdleSync();
147 } 76 }
148 } 77 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698