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

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/searchwidget/SearchActivityTest.java

Issue 2855543005: 🔍 Add basic SearchActivityTests (Closed)
Patch Set: rebase Created 3 years, 7 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 | « chrome/android/java_sources.gni ('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.chrome.browser.searchwidget;
6
7 import android.annotation.SuppressLint;
8 import android.app.Activity;
9 import android.app.Instrumentation;
10 import android.app.Instrumentation.ActivityMonitor;
11 import android.content.Intent;
12 import android.support.test.InstrumentationRegistry;
13 import android.support.test.filters.SmallTest;
14 import android.text.TextUtils;
15 import android.view.KeyEvent;
16
17 import org.junit.After;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23
24 import org.chromium.base.ThreadUtils;
25 import org.chromium.base.annotations.SuppressFBWarnings;
26 import org.chromium.base.test.util.CallbackHelper;
27 import org.chromium.base.test.util.CommandLineFlags;
28 import org.chromium.chrome.R;
29 import org.chromium.chrome.browser.ChromeSwitches;
30 import org.chromium.chrome.browser.ChromeTabbedActivity;
31 import org.chromium.chrome.browser.omnibox.UrlBar;
32 import org.chromium.chrome.browser.searchwidget.SearchActivity.SearchActivityObs erver;
33 import org.chromium.chrome.browser.tab.Tab;
34 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
35 import org.chromium.chrome.test.MultiActivityTestRule;
36 import org.chromium.chrome.test.util.OmniboxTestUtils;
37 import org.chromium.content.browser.test.util.Criteria;
38 import org.chromium.content.browser.test.util.CriteriaHelper;
39 import org.chromium.content.browser.test.util.KeyUtils;
40
41 /**
42 * Tests the {@link SearchActivity}.
43 *
44 * TODO(dfalcantara): Add tests for:
45 * + Checking that user can type in the box before native has loaded and still
46 * have suggestions appear.
47 *
48 * + Performing a search query.
49 * + Performing a search query while the SearchActivity i s alive and the
50 * default search engine is changed outside the SearchA ctivity.
51 *
52 * + Handling the promo dialog.
53 */
54 @RunWith(ChromeJUnit4ClassRunner.class)
55 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
56 public class SearchActivityTest {
57 private static class TestObserver implements SearchActivityObserver {
58 public final CallbackHelper onSetContentViewCallback = new CallbackHelpe r();
59 public final CallbackHelper onFinishNativeInitializationCallback = new C allbackHelper();
60 public final CallbackHelper onFinishDeferredInitializationCallback = new CallbackHelper();
61
62 @Override
63 public void onSetContentView() {
64 onSetContentViewCallback.notifyCalled();
65 }
66
67 @Override
68 public void onFinishNativeInitialization() {
69 onFinishNativeInitializationCallback.notifyCalled();
70 }
71
72 @Override
73 public void onFinishDeferredInitialization() {
74 onFinishDeferredInitializationCallback.notifyCalled();
75 }
76 }
77
78 @Rule
79 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
80 public MultiActivityTestRule mTestRule = new MultiActivityTestRule();
81
82 private TestObserver mTestObserver;
83
84 @Before
85 public void setUp() {
86 mTestObserver = new TestObserver();
87 SearchActivity.setObserverForTests(mTestObserver);
88 }
89
90 @After
91 public void tearDown() {
92 SearchActivity.setObserverForTests(null);
93 }
94
95 @Test
96 @SmallTest
97 public void testOmniboxSuggestionContainerAppears() throws Exception {
98 Activity searchActivity = fullyStartSearchActivity();
99
100 // Type in anything. It should force the suggestions to appear.
101 setUrlBarText(searchActivity, "anything.");
102 final SearchActivityLocationBarLayout locationBar =
103 (SearchActivityLocationBarLayout) searchActivity.findViewById(
104 R.id.search_location_bar);
105 OmniboxTestUtils.waitForOmniboxSuggestions(locationBar);
106 }
107
108 @Test
109 @SmallTest
110 public void testStartsBrowserAfterUrlSubmitted() throws Exception {
111 final Instrumentation instrumentation = InstrumentationRegistry.getInstr umentation();
112 Activity searchActivity = fullyStartSearchActivity();
113
114 // Monitor for ChromeTabbedActivity.
115 ActivityMonitor browserMonitor =
116 new ActivityMonitor(ChromeTabbedActivity.class.getName(), null, false);
117 instrumentation.addMonitor(browserMonitor);
118
119 // Type in a URL that should get kicked to ChromeTabbedActivity.
120 setUrlBarText(searchActivity, "about:blank");
121 final UrlBar urlBar = (UrlBar) searchActivity.findViewById(R.id.url_bar) ;
122 KeyUtils.singleKeyEventView(instrumentation, urlBar, KeyEvent.KEYCODE_EN TER);
123
124 // Wait for ChromeTabbedActivity to start.
125 final Activity browserActivity = instrumentation.waitForMonitorWithTimeo ut(
126 browserMonitor, CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL);
127 Assert.assertNotNull("Activity didn't start", browserActivity);
128 Assert.assertTrue(
129 "Wrong activity started", browserActivity instanceof ChromeTabbe dActivity);
130
131 CriteriaHelper.pollUiThread(new Criteria() {
132 @Override
133 public boolean isSatisfied() {
134 ChromeTabbedActivity chromeActivity = (ChromeTabbedActivity) bro wserActivity;
135 Tab tab = chromeActivity.getActivityTab();
136 if (tab == null) return false;
137
138 return TextUtils.equals("about:blank", tab.getUrl());
139 }
140 });
141 }
142
143 private Activity fullyStartSearchActivity() throws Exception {
144 final Instrumentation instrumentation = InstrumentationRegistry.getInstr umentation();
145
146 ActivityMonitor searchMonitor =
147 new ActivityMonitor(SearchActivity.class.getName(), null, false) ;
148 instrumentation.addMonitor(searchMonitor);
149
150 // The SearchActivity shouldn't have started yet.
151 Assert.assertEquals(0, mTestObserver.onSetContentViewCallback.getCallCou nt());
152 Assert.assertEquals(0, mTestObserver.onFinishNativeInitializationCallbac k.getCallCount());
153 Assert.assertEquals(0, mTestObserver.onFinishDeferredInitializationCallb ack.getCallCount());
154
155 // Fire the Intent to start up the SearchActivity.
156 Intent intent = new Intent();
157 SearchWidgetProvider.startSearchActivity(intent, false);
158 Activity searchActivity = instrumentation.waitForMonitorWithTimeout(
159 searchMonitor, CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL);
160 Assert.assertNotNull("Activity didn't start", searchActivity);
161 Assert.assertTrue("Wrong activity started", searchActivity instanceof Se archActivity);
162
163 // Wait for the Activity fully load.
164 mTestObserver.onSetContentViewCallback.waitForCallback(0);
165 mTestObserver.onFinishNativeInitializationCallback.waitForCallback(0);
166 mTestObserver.onFinishDeferredInitializationCallback.waitForCallback(0);
167
168 instrumentation.removeMonitor(searchMonitor);
169 return searchActivity;
170 }
171
172 @SuppressLint("SetTextI18n")
173 private void setUrlBarText(final Activity activity, final String url) {
174 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
175 @Override
176 public void run() {
177 UrlBar urlBar = (UrlBar) activity.findViewById(R.id.url_bar);
178 urlBar.setText(url);
179 }
180 });
181 }
182 }
OLDNEW
« no previous file with comments | « chrome/android/java_sources.gni ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698