autocomplete_browsertest.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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 "base/utf_string_conversions.h"
7#include "chrome/browser/autocomplete/autocomplete.h"
8#include "chrome/browser/autocomplete/autocomplete_edit.h"
9#include "chrome/browser/autocomplete/autocomplete_edit_view.h"
10#include "chrome/browser/autocomplete/autocomplete_match.h"
11#include "chrome/browser/autocomplete/autocomplete_popup_model.h"
12#include "chrome/browser/browser_window.h"
13#include "chrome/browser/history/history.h"
14#include "chrome/browser/tab_contents/tab_contents.h"
15#include "chrome/browser/tabs/tab_strip_model.h"
16#include "chrome/browser/ui/browser.h"
17#include "chrome/browser/ui/omnibox/location_bar.h"
18#include "chrome/common/notification_type.h"
19#include "chrome/common/url_constants.h"
20#include "chrome/test/in_process_browser_test.h"
21#include "chrome/test/ui_test_utils.h"
22#include "testing/gtest/include/gtest/gtest.h"
23
24// Autocomplete test is flaky on ChromeOS.
25// http://crbug.com/52928
26#if defined(OS_CHROMEOS)
27#define MAYBE_Autocomplete FLAKY_Autocomplete
28#else
29#define MAYBE_Autocomplete Autocomplete
30#endif
31
32
33namespace {
34
35std::wstring AutocompleteResultAsString(const AutocompleteResult& result) {
36  std::wstring output(StringPrintf(L"{%d} ", result.size()));
37  for (size_t i = 0; i < result.size(); ++i) {
38    AutocompleteMatch match = result.match_at(i);
39    std::wstring provider_name(ASCIIToWide(match.provider->name()));
40    output.append(StringPrintf(L"[\"%ls\" by \"%ls\"] ",
41                               match.contents.c_str(),
42                               provider_name.c_str()));
43  }
44  return output;
45}
46
47}  // namespace
48
49class AutocompleteBrowserTest : public InProcessBrowserTest {
50 protected:
51  LocationBar* GetLocationBar() const {
52    return browser()->window()->GetLocationBar();
53  }
54
55  AutocompleteController* GetAutocompleteController() const {
56    return GetLocationBar()->location_entry()->model()->popup_model()->
57        autocomplete_controller();
58  }
59};
60
61IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) {
62  LocationBar* location_bar = GetLocationBar();
63
64  EXPECT_TRUE(location_bar->GetInputString().empty());
65  EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL),
66            location_bar->location_entry()->GetText());
67  // TODO(phajdan.jr): check state of IsSelectAll when it's consistent across
68  // platforms.
69
70  location_bar->FocusLocation(true);
71
72  EXPECT_TRUE(location_bar->GetInputString().empty());
73  EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL),
74            location_bar->location_entry()->GetText());
75  EXPECT_TRUE(location_bar->location_entry()->IsSelectAll());
76
77  location_bar->location_entry()->SetUserText(L"chrome");
78
79  EXPECT_TRUE(location_bar->GetInputString().empty());
80  EXPECT_EQ(L"chrome", location_bar->location_entry()->GetText());
81  EXPECT_FALSE(location_bar->location_entry()->IsSelectAll());
82
83  location_bar->location_entry()->RevertAll();
84
85  EXPECT_TRUE(location_bar->GetInputString().empty());
86  EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL),
87            location_bar->location_entry()->GetText());
88  EXPECT_FALSE(location_bar->location_entry()->IsSelectAll());
89
90  location_bar->location_entry()->SetUserText(L"chrome");
91  location_bar->Revert();
92
93  EXPECT_TRUE(location_bar->GetInputString().empty());
94  EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL),
95            location_bar->location_entry()->GetText());
96  EXPECT_FALSE(location_bar->location_entry()->IsSelectAll());
97}
98
99IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, MAYBE_Autocomplete) {
100  // The results depend on the history backend being loaded. Make sure it is
101  // loaded so that the autocomplete results are consistent.
102  ui_test_utils::WaitForHistoryToLoad(browser());
103
104  LocationBar* location_bar = GetLocationBar();
105  AutocompleteController* autocomplete_controller = GetAutocompleteController();
106
107  {
108    autocomplete_controller->Start(L"chrome", std::wstring(),
109                                   true, false, true, true);
110
111    EXPECT_TRUE(autocomplete_controller->done());
112    EXPECT_TRUE(location_bar->GetInputString().empty());
113    EXPECT_TRUE(location_bar->location_entry()->GetText().empty());
114    EXPECT_TRUE(location_bar->location_entry()->IsSelectAll());
115    const AutocompleteResult& result = autocomplete_controller->result();
116    ASSERT_EQ(1U, result.size()) << AutocompleteResultAsString(result);
117    AutocompleteMatch match = result.match_at(0);
118    EXPECT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, match.type);
119    EXPECT_FALSE(match.deletable);
120  }
121
122  {
123    location_bar->Revert();
124
125    EXPECT_TRUE(location_bar->GetInputString().empty());
126    EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL),
127              location_bar->location_entry()->GetText());
128    EXPECT_FALSE(location_bar->location_entry()->IsSelectAll());
129    const AutocompleteResult& result = autocomplete_controller->result();
130    EXPECT_TRUE(result.empty()) << AutocompleteResultAsString(result);
131  }
132}
133
134IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, TabAwayRevertSelect) {
135  // http://code.google.com/p/chromium/issues/detail?id=38385
136  // Make sure that tabbing away from an empty omnibar causes a revert
137  // and select all.
138  LocationBar* location_bar = GetLocationBar();
139  EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL),
140            location_bar->location_entry()->GetText());
141  location_bar->location_entry()->SetUserText(L"");
142  browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL),
143                                   PageTransition::START_PAGE);
144  ui_test_utils::WaitForNavigation(
145      &browser()->GetSelectedTabContents()->controller());
146  EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL),
147            location_bar->location_entry()->GetText());
148  browser()->CloseTab();
149  EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL),
150            location_bar->location_entry()->GetText());
151  EXPECT_TRUE(location_bar->location_entry()->IsSelectAll());
152}
153
154IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, FocusSearch) {
155  LocationBar* location_bar = GetLocationBar();
156
157  // Focus search when omnibox is blank
158  {
159    EXPECT_TRUE(location_bar->GetInputString().empty());
160    EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL),
161              location_bar->location_entry()->GetText());
162
163    location_bar->FocusSearch();
164    EXPECT_TRUE(location_bar->GetInputString().empty());
165    EXPECT_EQ(L"?", location_bar->location_entry()->GetText());
166
167    size_t selection_start, selection_end;
168    location_bar->location_entry()->GetSelectionBounds(&selection_start,
169                                                       &selection_end);
170    EXPECT_EQ(1U, selection_start);
171    EXPECT_EQ(1U, selection_end);
172  }
173
174  // Focus search when omnibox is _not_ alread in forced query mode.
175  {
176    location_bar->location_entry()->SetUserText(L"foo");
177    EXPECT_TRUE(location_bar->GetInputString().empty());
178    EXPECT_EQ(L"foo", location_bar->location_entry()->GetText());
179
180    location_bar->FocusSearch();
181    EXPECT_TRUE(location_bar->GetInputString().empty());
182    EXPECT_EQ(L"?", location_bar->location_entry()->GetText());
183
184    size_t selection_start, selection_end;
185    location_bar->location_entry()->GetSelectionBounds(&selection_start,
186                                                       &selection_end);
187    EXPECT_EQ(1U, selection_start);
188    EXPECT_EQ(1U, selection_end);
189  }
190
191  // Focus search when omnibox _is_ already in forced query mode, but no query
192  // has been typed.
193  {
194    location_bar->location_entry()->SetUserText(L"?");
195    EXPECT_TRUE(location_bar->GetInputString().empty());
196    EXPECT_EQ(L"?", location_bar->location_entry()->GetText());
197
198    location_bar->FocusSearch();
199    EXPECT_TRUE(location_bar->GetInputString().empty());
200    EXPECT_EQ(L"?", location_bar->location_entry()->GetText());
201
202    size_t selection_start, selection_end;
203    location_bar->location_entry()->GetSelectionBounds(&selection_start,
204                                                       &selection_end);
205    EXPECT_EQ(1U, selection_start);
206    EXPECT_EQ(1U, selection_end);
207  }
208
209  // Focus search when omnibox _is_ already in forced query mode, and some query
210  // has been typed.
211  {
212    location_bar->location_entry()->SetUserText(L"?foo");
213    EXPECT_TRUE(location_bar->GetInputString().empty());
214    EXPECT_EQ(L"?foo", location_bar->location_entry()->GetText());
215
216    location_bar->FocusSearch();
217    EXPECT_TRUE(location_bar->GetInputString().empty());
218    EXPECT_EQ(L"?foo", location_bar->location_entry()->GetText());
219
220    size_t selection_start, selection_end;
221    location_bar->location_entry()->GetSelectionBounds(&selection_start,
222                                                       &selection_end);
223    EXPECT_EQ(1U, std::min(selection_start, selection_end));
224    EXPECT_EQ(4U, std::max(selection_start, selection_end));
225  }
226
227  // Focus search when omnibox is in forced query mode with leading whitespace.
228  {
229    location_bar->location_entry()->SetUserText(L"   ?foo");
230    EXPECT_TRUE(location_bar->GetInputString().empty());
231    EXPECT_EQ(L"   ?foo", location_bar->location_entry()->GetText());
232
233    location_bar->FocusSearch();
234    EXPECT_TRUE(location_bar->GetInputString().empty());
235    EXPECT_EQ(L"   ?foo", location_bar->location_entry()->GetText());
236
237    size_t selection_start, selection_end;
238    location_bar->location_entry()->GetSelectionBounds(&selection_start,
239                                                       &selection_end);
240    EXPECT_EQ(4U, std::min(selection_start, selection_end));
241    EXPECT_EQ(7U, std::max(selection_start, selection_end));
242  }
243}
244