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