OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 #include "base/string_util.h" | |
6 #include "chrome/browser/instant/instant_overlay.h" | |
7 #include "chrome/browser/instant/instant_test_utils.h" | |
8 #include "chrome/browser/instant/search.h" | |
9 #include "chrome/browser/task_manager/task_manager.h" | |
10 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/browser/ui/browser_commands.h" | |
13 #include "chrome/browser/ui/host_desktop.h" | |
14 #include "chrome/browser/ui/omnibox/omnibox_view.h" | |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
16 #include "chrome/common/chrome_notification_types.h" | |
17 #include "chrome/test/base/in_process_browser_test.h" | |
18 #include "chrome/test/base/interactive_test_utils.h" | |
19 #include "chrome/test/base/ui_test_utils.h" | |
20 #include "content/public/browser/navigation_controller.h" | |
21 #include "content/public/browser/navigation_entry.h" | |
22 #include "content/public/browser/notification_service.h" | |
23 #include "content/public/browser/render_process_host.h" | |
24 #include "content/public/browser/web_contents.h" | |
25 #include "content/public/test/browser_test_utils.h" | |
26 #include "googleurl/src/gurl.h" | |
27 #include "net/base/mock_host_resolver.h" | |
28 | |
29 // Instant extended tests that need to be run manually because they need to | |
30 // talk to the external network. All tests in this file should be marked as | |
31 // "MANUAL_" unless they are disabled. | |
32 class InstantExtendedManualTest : public InProcessBrowserTest, | |
33 public InstantTestBase { | |
34 public: | |
35 InstantExtendedManualTest() { | |
36 host_resolver_proc_ = new net::RuleBasedHostResolverProc(NULL); | |
37 host_resolver_proc_->AllowDirectLookup("*"); | |
38 scoped_host_resolver_proc_.reset( | |
39 new net::ScopedDefaultHostResolverProc(host_resolver_proc_.get())); | |
40 } | |
41 | |
42 virtual ~InstantExtendedManualTest() { | |
43 scoped_host_resolver_proc_.reset(); | |
44 host_resolver_proc_ = NULL; | |
45 } | |
46 | |
47 virtual void SetUpOnMainThread() OVERRIDE { | |
48 const testing::TestInfo* const test_info = | |
49 testing::UnitTest::GetInstance()->current_test_info(); | |
50 ASSERT_TRUE(StartsWithASCII(test_info->name(), "MANUAL_", true) || | |
51 StartsWithASCII(test_info->name(), "DISABLED_", true)); | |
52 } | |
53 | |
54 protected: | |
55 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
56 chrome::search::EnableInstantExtendedAPIForTesting(); | |
57 } | |
58 | |
59 content::WebContents* active_tab() { | |
60 return browser()->tab_strip_model()->GetActiveWebContents(); | |
61 } | |
62 | |
63 bool PressBackspace() { | |
64 return ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_BACK, | |
65 false, false, false, false); | |
66 } | |
67 | |
68 bool PressBackspaceAndWaitForSuggestions() { | |
69 content::WindowedNotificationObserver observer( | |
70 chrome::NOTIFICATION_INSTANT_SET_SUGGESTION, | |
71 content::NotificationService::AllSources()); | |
72 bool result = PressBackspace(); | |
73 observer.Wait(); | |
74 return result; | |
75 } | |
76 | |
77 bool PressBackspaceAndWaitForOverlayToShow() { | |
78 InstantTestModelObserver observer( | |
79 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); | |
80 bool result = PressBackspace(); | |
81 observer.WaitForDesiredOverlayState(); | |
82 return result; | |
83 } | |
84 | |
85 void PressEnterAndWaitForNavigation() { | |
86 content::WindowedNotificationObserver nav_observer( | |
87 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
88 content::NotificationService::AllSources()); | |
89 browser()->window()->GetLocationBar()->AcceptInput(); | |
90 nav_observer.Wait(); | |
91 } | |
92 | |
93 bool PressEnterAndWaitForNavigationWithTitle(content::WebContents* contents, | |
94 const string16& title) { | |
95 content::TitleWatcher title_watcher(contents, title); | |
96 content::WindowedNotificationObserver nav_observer( | |
97 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
98 content::NotificationService::AllSources()); | |
99 browser()->window()->GetLocationBar()->AcceptInput(); | |
100 nav_observer.Wait(); | |
101 return title_watcher.WaitAndGetTitle() == title; | |
102 } | |
103 | |
104 GURL GetActiveTabURL() { | |
105 return active_tab()->GetController().GetActiveEntry()->GetURL(); | |
106 } | |
107 | |
108 string16 GetBlueText() { | |
109 size_t start = 0, end = 0; | |
110 omnibox()->GetSelectionBounds(&start, &end); | |
111 if (start > end) | |
112 std::swap(start, end); | |
113 return omnibox()->GetText().substr(start, end - start); | |
114 } | |
115 | |
116 bool GetSelectionState(bool* selected) { | |
117 return GetBoolFromJS(instant()->GetOverlayContents(), | |
118 "google.ac.gs().api.i()", selected); | |
119 } | |
120 | |
121 private: | |
122 scoped_refptr<net::RuleBasedHostResolverProc> host_resolver_proc_; | |
123 scoped_ptr<net::ScopedDefaultHostResolverProc> scoped_host_resolver_proc_; | |
124 }; | |
125 | |
126 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, | |
127 MANUAL_OmniboxFocusLoadsInstant) { | |
128 set_browser(browser()); | |
129 | |
130 // Explicitly unfocus the omnibox. | |
131 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | |
132 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER); | |
133 | |
134 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); | |
135 EXPECT_FALSE(omnibox()->model()->has_focus()); | |
136 | |
137 // Delete any existing overlay. | |
138 instant()->overlay_.reset(); | |
139 EXPECT_FALSE(instant()->GetOverlayContents()); | |
140 | |
141 // Refocus the omnibox. The InstantController should've preloaded Instant. | |
142 FocusOmniboxAndWaitForInstantExtendedSupport(); | |
143 | |
144 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); | |
145 EXPECT_TRUE(omnibox()->model()->has_focus()); | |
146 | |
147 content::WebContents* overlay_tab = instant()->GetOverlayContents(); | |
148 EXPECT_TRUE(overlay_tab); | |
149 | |
150 // Check that the page supports Instant, but it isn't showing. | |
151 EXPECT_TRUE(instant()->overlay_->supports_instant()); | |
152 EXPECT_FALSE(instant()->IsOverlayingSearchResults()); | |
153 EXPECT_TRUE(instant()->model()->mode().is_default()); | |
154 } | |
155 | |
156 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, | |
157 MANUAL_BackspaceFromQueryToSameQueryAndSearch) { | |
158 set_browser(browser()); | |
159 FocusOmniboxAndWaitForInstantExtendedSupport(); | |
160 | |
161 // Type "face" and expect Google to set gray text for "book" to suggest | |
162 // [facebook], the query. | |
163 SetOmniboxTextAndWaitForOverlayToShow("face"); | |
164 EXPECT_EQ(ASCIIToUTF16("face"), omnibox()->GetText()); | |
165 EXPECT_EQ(ASCIIToUTF16("book"), omnibox()->GetInstantSuggestion()); | |
166 | |
167 // Backspace to the text "fac". | |
168 EXPECT_TRUE(PressBackspaceAndWaitForSuggestions()); | |
169 EXPECT_EQ(ASCIIToUTF16("fac"), omnibox()->GetText()); | |
170 EXPECT_EQ(ASCIIToUTF16("ebook"), omnibox()->GetInstantSuggestion()); | |
171 | |
172 // Press Enter. The page should show search results for [fac]. | |
173 EXPECT_TRUE(PressEnterAndWaitForNavigationWithTitle( | |
174 instant()->GetOverlayContents(), | |
175 ASCIIToUTF16("fac - Google Search"))); | |
176 } | |
177 | |
178 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, | |
179 MANUAL_BackspaceFromQueryToOtherQueryAndSearch) { | |
180 set_browser(browser()); | |
181 FocusOmniboxAndWaitForInstantExtendedSupport(); | |
182 | |
183 // Type "fan" and expect Google to set gray text to "dango" to suggest | |
184 // [fandango], the query. | |
185 SetOmniboxTextAndWaitForOverlayToShow("fan"); | |
186 EXPECT_EQ(ASCIIToUTF16("fan"), omnibox()->GetText()); | |
187 EXPECT_EQ(ASCIIToUTF16("dango"), omnibox()->GetInstantSuggestion()); | |
188 | |
189 // Backspace to the text "fa". Expect Google to set gray text for "cebook" to | |
190 // suggest [facebook], the query. | |
191 EXPECT_TRUE(PressBackspaceAndWaitForSuggestions()); | |
192 EXPECT_EQ(ASCIIToUTF16("fa"), omnibox()->GetText()); | |
193 EXPECT_EQ(ASCIIToUTF16("cebook"), omnibox()->GetInstantSuggestion()); | |
194 | |
195 // Press Enter. Instant should clear gray text and submit the query [fa]. | |
196 EXPECT_TRUE(PressEnterAndWaitForNavigationWithTitle( | |
197 instant()->GetOverlayContents(), | |
198 ASCIIToUTF16("fa - Google Search"))); | |
199 } | |
200 | |
201 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, | |
202 MANUAL_BackspaceFromUrlToNonSelectedUrlAndSearch) { | |
203 set_browser(browser()); | |
204 FocusOmniboxAndWaitForInstantExtendedSupport(); | |
205 | |
206 // Type "facebook.c" and expect Google to set blue text to "om" to suggest | |
207 // http://www.facebook.com/, the URL. | |
208 SetOmniboxTextAndWaitForOverlayToShow("facebook.c"); | |
209 EXPECT_EQ(ASCIIToUTF16("facebook.com"), omnibox()->GetText()); | |
210 EXPECT_EQ(ASCIIToUTF16("om"), GetBlueText()); | |
211 bool selected = false; | |
212 EXPECT_TRUE(GetSelectionState(&selected)); | |
213 EXPECT_TRUE(selected); | |
214 | |
215 // Backspace to the text "facebook.c". Expect no suggestion text and no | |
216 // selected suggestion. | |
217 // Page won't actually show because it's showing "press enter to search". | |
218 EXPECT_TRUE(PressBackspaceAndWaitForSuggestions()); | |
219 EXPECT_EQ(ASCIIToUTF16("facebook.c"), omnibox()->GetText()); | |
220 EXPECT_EQ(ASCIIToUTF16(""), GetBlueText()); | |
221 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion()); | |
222 EXPECT_TRUE(GetSelectionState(&selected)); | |
223 EXPECT_FALSE(selected); | |
224 | |
225 // Press Enter. Instant should submit the query "facebook.c". | |
226 EXPECT_TRUE(PressEnterAndWaitForNavigationWithTitle( | |
227 active_tab(), ASCIIToUTF16("facebook.c - Google Search"))); | |
228 } | |
229 | |
230 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, | |
231 MANUAL_BackspaceFromUrlToUrlAndNavigate) { | |
232 set_browser(browser()); | |
233 FocusOmniboxAndWaitForInstantExtendedSupport(); | |
234 | |
235 // Type "facebook.com/" and expect Google to set blue text to "login.php" to | |
236 // suggest http://www.facebook.com/login.php, the URL. | |
237 SetOmniboxTextAndWaitForOverlayToShow("facebook.com/"); | |
238 EXPECT_EQ(ASCIIToUTF16("facebook.com/login.php"), omnibox()->GetText()); | |
239 EXPECT_EQ(ASCIIToUTF16("login.php"), GetBlueText()); | |
240 bool selected = false; | |
241 EXPECT_TRUE(GetSelectionState(&selected)); | |
242 EXPECT_TRUE(selected); | |
243 | |
244 // Backspace to the text "facebook.com/". There should be no suggestion text, | |
245 // but the first suggestion should be selected. | |
246 EXPECT_TRUE(PressBackspaceAndWaitForSuggestions()); | |
247 EXPECT_EQ(ASCIIToUTF16("facebook.com/"), omnibox()->GetText()); | |
248 EXPECT_EQ(ASCIIToUTF16(""), GetBlueText()); | |
249 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion()); | |
250 selected = false; | |
251 EXPECT_TRUE(GetSelectionState(&selected)); | |
252 EXPECT_TRUE(selected); | |
253 | |
254 // Press Enter. Instant should navigate to facebook.com. | |
255 PressEnterAndWaitForNavigation(); | |
256 EXPECT_TRUE(GetActiveTabURL().DomainIs("facebook.com")); | |
257 } | |
258 | |
259 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, | |
260 MANUAL_BackspaceFromQueryToSelectedUrlAndNavigate) { | |
261 set_browser(browser()); | |
262 FocusOmniboxAndWaitForInstantExtendedSupport(); | |
263 | |
264 // Type "a.cop" and expect Google to set gray text to "land" to suggest the | |
265 // query [a.copland]. | |
266 SetOmniboxTextAndWaitForOverlayToShow("a.cop"); | |
267 EXPECT_EQ(ASCIIToUTF16("a.cop"), omnibox()->GetText()); | |
268 EXPECT_EQ(ASCIIToUTF16("land"), omnibox()->GetInstantSuggestion()); | |
269 | |
270 // Backspace to the text "a.co". Expect no suggestion text, but the | |
271 // first suggestion should be selected URL "a.co". | |
272 EXPECT_TRUE(PressBackspaceAndWaitForSuggestions()); | |
273 EXPECT_EQ(ASCIIToUTF16("a.co"), omnibox()->GetText()); | |
274 EXPECT_EQ(ASCIIToUTF16(""), GetBlueText()); | |
275 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion()); | |
276 bool selected = false; | |
277 EXPECT_TRUE(GetSelectionState(&selected)); | |
278 EXPECT_TRUE(selected); | |
279 | |
280 // Press Enter. Instant should navigate to a.co/. | |
281 PressEnterAndWaitForNavigation(); | |
282 EXPECT_TRUE(GetActiveTabURL().DomainIs("amazon.com")); | |
283 } | |
284 | |
285 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, | |
286 MANUAL_BackspaceFromSelectedUrlToQueryAndSearch) { | |
287 set_browser(browser()); | |
288 FocusOmniboxAndWaitForInstantExtendedSupport(); | |
289 | |
290 // Type "e.co/" and expect the top suggestion to be the URL "e.co/". | |
291 SetOmniboxTextAndWaitForOverlayToShow("e.co/"); | |
292 EXPECT_EQ(ASCIIToUTF16("e.co/"), omnibox()->GetText()); | |
293 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion()); | |
294 EXPECT_EQ(ASCIIToUTF16(""), GetBlueText()); | |
295 bool selected = false; | |
296 EXPECT_TRUE(GetSelectionState(&selected)); | |
297 EXPECT_TRUE(selected); | |
298 | |
299 // Backspace to the text "e.co". Expect Google to suggest the query [e.coli]. | |
300 EXPECT_TRUE(PressBackspaceAndWaitForOverlayToShow()); | |
301 EXPECT_EQ(ASCIIToUTF16("e.co"), omnibox()->GetText()); | |
302 EXPECT_EQ(ASCIIToUTF16("li"), omnibox()->GetInstantSuggestion()); | |
303 EXPECT_EQ(ASCIIToUTF16(""), GetBlueText()); | |
304 selected = true; | |
305 EXPECT_TRUE(GetSelectionState(&selected)); | |
306 EXPECT_FALSE(selected); | |
307 | |
308 // Press Enter. Instant should search for "e.co". | |
309 EXPECT_TRUE(PressEnterAndWaitForNavigationWithTitle( | |
310 instant()->GetOverlayContents(), | |
311 ASCIIToUTF16("e.co - Google Search"))); | |
312 } | |
OLD | NEW |