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

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

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

Powered by Google App Engine
This is Rietveld 408576698