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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <inttypes.h>
#include <stddef.h>
#include "base/command_line.h"
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/trace_event/memory_dump_manager.h"
#include "build/build_config.h"
#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
#include "chrome/browser/autocomplete/in_memory_url_index_factory.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/search_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/history/core/browser/history_service.h"
#include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_provider.h"
#include "components/omnibox/browser/omnibox_controller.h"
#include "components/omnibox/browser/omnibox_edit_model.h"
#include "components/omnibox/browser/omnibox_view.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/omnibox_event.pb.h"
namespace {
std::u16string AutocompleteResultAsString(const AutocompleteResult& result) {
std::string output(base::StringPrintf("{%" PRIuS "} ", result.size()));
for (size_t i = 0; i < result.size(); ++i) {
AutocompleteMatch match = result.match_at(i);
output.append(base::StringPrintf("[\"%s\" by \"%s\"] ",
base::UTF16ToUTF8(match.contents).c_str(),
match.provider->GetName()));
}
return base::UTF8ToUTF16(output);
}
} // namespace
class AutocompleteBrowserTest : public extensions::ExtensionBrowserTest {
protected:
void WaitForTemplateURLServiceToLoad() {
search_test_utils::WaitForTemplateURLServiceToLoad(
TemplateURLServiceFactory::GetForProfile(browser()->profile()));
}
LocationBar* GetLocationBar() const {
return browser()->window()->GetLocationBar();
}
AutocompleteController* GetAutocompleteController() const {
return GetLocationBar()
->GetOmniboxView()
->controller()
->autocomplete_controller();
}
void FocusSearchCheckPreconditions() const {
LocationBar* location_bar = GetLocationBar();
OmniboxView* omnibox_view = location_bar->GetOmniboxView();
OmniboxEditModel* omnibox_model = omnibox_view->model();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(url::kAboutBlankURL16, omnibox_view->GetText());
EXPECT_EQ(std::u16string(), omnibox_model->keyword());
EXPECT_FALSE(omnibox_model->is_keyword_hint());
EXPECT_FALSE(omnibox_model->is_keyword_selected());
}
};
IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) {
WaitForTemplateURLServiceToLoad();
LocationBar* location_bar = GetLocationBar();
OmniboxView* omnibox_view = location_bar->GetOmniboxView();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(url::kAboutBlankURL16, omnibox_view->GetText());
// TODO(phajdan.jr): check state of IsSelectAll when it's consistent across
// platforms.
location_bar->FocusLocation(true);
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(url::kAboutBlankURL16, omnibox_view->GetText());
EXPECT_TRUE(omnibox_view->IsSelectAll());
omnibox_view->SetUserText(u"chrome");
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(u"chrome", omnibox_view->GetText());
EXPECT_FALSE(omnibox_view->IsSelectAll());
omnibox_view->RevertAll();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(url::kAboutBlankURL16, omnibox_view->GetText());
EXPECT_FALSE(omnibox_view->IsSelectAll());
omnibox_view->SetUserText(u"chrome");
location_bar->Revert();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(url::kAboutBlankURL16, omnibox_view->GetText());
EXPECT_FALSE(omnibox_view->IsSelectAll());
}
// Autocomplete test is flaky on ChromeOS.
// http://crbug.com/52928
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_Autocomplete DISABLED_Autocomplete
#else
#define MAYBE_Autocomplete Autocomplete
#endif
IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, MAYBE_Autocomplete) {
WaitForTemplateURLServiceToLoad();
// 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(HistoryServiceFactory::GetForProfile(
browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS));
LocationBar* location_bar = GetLocationBar();
OmniboxView* omnibox_view = location_bar->GetOmniboxView();
AutocompleteController* autocomplete_controller = GetAutocompleteController();
{
omnibox_view->model()->SetInputInProgress(true);
AutocompleteInput input(
u"chrome", metrics::OmniboxEventProto::NTP,
ChromeAutocompleteSchemeClassifier(browser()->profile()));
input.set_prevent_inline_autocomplete(true);
input.set_omit_asynchronous_matches(true);
autocomplete_controller->Start(input);
EXPECT_TRUE(autocomplete_controller->done());
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_TRUE(omnibox_view->GetText().empty());
EXPECT_FALSE(omnibox_view->IsSelectAll());
const AutocompleteResult& result = autocomplete_controller->result();
ASSERT_GE(result.size(), 1U) << AutocompleteResultAsString(result);
AutocompleteMatch match = result.match_at(0);
EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, match.type);
EXPECT_FALSE(match.deletable);
}
{
location_bar->Revert();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(url::kAboutBlankURL16, omnibox_view->GetText());
EXPECT_FALSE(omnibox_view->IsSelectAll());
const AutocompleteResult& result = autocomplete_controller->result();
EXPECT_TRUE(result.empty()) << AutocompleteResultAsString(result);
}
}
IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, TabAwayRevertSelect) {
WaitForTemplateURLServiceToLoad();
// http://code.google.com/p/chromium/issues/detail?id=38385
// Make sure that tabbing away from an empty omnibox causes a revert
// and select all.
LocationBar* location_bar = GetLocationBar();
OmniboxView* omnibox_view = location_bar->GetOmniboxView();
EXPECT_EQ(url::kAboutBlankURL16, omnibox_view->GetText());
omnibox_view->SetUserText(std::u16string());
content::CreateAndLoadWebContentsObserver observer;
chrome::AddSelectedTabWithURL(browser(),
GURL(url::kAboutBlankURL),
ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
observer.Wait();
EXPECT_EQ(url::kAboutBlankURL16, omnibox_view->GetText());
chrome::CloseTab(browser());
EXPECT_EQ(url::kAboutBlankURL16, omnibox_view->GetText());
EXPECT_TRUE(omnibox_view->IsSelectAll());
}
IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, FocusSearch) {
WaitForTemplateURLServiceToLoad();
LocationBar* location_bar = GetLocationBar();
OmniboxView* omnibox_view = location_bar->GetOmniboxView();
OmniboxEditModel* omnibox_model = omnibox_view->model();
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(browser()->profile());
std::u16string default_search_keyword =
template_url_service->GetDefaultSearchProvider()->keyword();
std::u16string query_text = u"foo";
size_t selection_start, selection_end;
// Focus search when omnibox is blank.
{
FocusSearchCheckPreconditions();
location_bar->FocusSearch();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(std::u16string(), omnibox_view->GetText());
EXPECT_EQ(default_search_keyword, omnibox_model->keyword());
EXPECT_FALSE(omnibox_model->is_keyword_hint());
EXPECT_TRUE(omnibox_model->is_keyword_selected());
omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
EXPECT_EQ(0U, selection_start);
EXPECT_EQ(0U, selection_end);
omnibox_view->RevertAll();
}
// Focus search when omnibox is _not_ already in keyword mode.
{
FocusSearchCheckPreconditions();
omnibox_view->SetUserText(query_text);
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(query_text, omnibox_view->GetText());
EXPECT_EQ(std::u16string(), omnibox_model->keyword());
EXPECT_FALSE(omnibox_model->is_keyword_hint());
EXPECT_FALSE(omnibox_model->is_keyword_selected());
location_bar->FocusSearch();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(query_text, omnibox_view->GetText());
EXPECT_EQ(default_search_keyword, omnibox_model->keyword());
EXPECT_FALSE(omnibox_model->is_keyword_hint());
EXPECT_TRUE(omnibox_model->is_keyword_selected());
omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
EXPECT_EQ(0U, std::min(selection_start, selection_end));
EXPECT_EQ(query_text.length(), std::max(selection_start, selection_end));
omnibox_view->RevertAll();
}
// Focus search when omnibox _is_ already in keyword mode, but no query
// has been typed.
{
FocusSearchCheckPreconditions();
location_bar->FocusSearch();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(std::u16string(), omnibox_view->GetText());
EXPECT_EQ(default_search_keyword, omnibox_model->keyword());
EXPECT_FALSE(omnibox_model->is_keyword_hint());
EXPECT_TRUE(omnibox_model->is_keyword_selected());
omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
EXPECT_EQ(0U, selection_start);
EXPECT_EQ(0U, selection_end);
location_bar->FocusSearch();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(std::u16string(), omnibox_view->GetText());
EXPECT_EQ(default_search_keyword, omnibox_model->keyword());
EXPECT_FALSE(omnibox_model->is_keyword_hint());
EXPECT_TRUE(omnibox_model->is_keyword_selected());
omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
EXPECT_EQ(0U, selection_start);
EXPECT_EQ(0U, selection_end);
omnibox_view->RevertAll();
}
// Focus search when omnibox _is_ already in keyword mode, and some query
// has been typed.
{
FocusSearchCheckPreconditions();
omnibox_view->SetUserText(query_text);
location_bar->FocusSearch();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(query_text, omnibox_view->GetText());
EXPECT_EQ(default_search_keyword, omnibox_model->keyword());
EXPECT_FALSE(omnibox_model->is_keyword_hint());
EXPECT_TRUE(omnibox_model->is_keyword_selected());
omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
EXPECT_EQ(0U, std::min(selection_start, selection_end));
EXPECT_EQ(query_text.length(), std::max(selection_start, selection_end));
location_bar->FocusSearch();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(query_text, omnibox_view->GetText());
EXPECT_EQ(default_search_keyword, omnibox_model->keyword());
EXPECT_FALSE(omnibox_model->is_keyword_hint());
EXPECT_TRUE(omnibox_model->is_keyword_selected());
omnibox_view->GetSelectionBounds(&selection_start, &selection_end);
EXPECT_EQ(0U, std::min(selection_start, selection_end));
EXPECT_EQ(query_text.length(), std::max(selection_start, selection_end));
omnibox_view->RevertAll();
}
// If the user gets into keyword mode using a keyboard shortcut, and presses
// backspace, they should be left with their original query without their dsp
// keyword.
{
FocusSearchCheckPreconditions();
omnibox_view->SetUserText(query_text);
// The user presses Ctrl-K.
location_bar->FocusSearch();
// The user presses backspace.
omnibox_model->ClearKeyword();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(query_text, omnibox_view->GetText());
EXPECT_EQ(std::u16string(), omnibox_model->keyword());
EXPECT_FALSE(omnibox_model->is_keyword_hint());
EXPECT_FALSE(omnibox_model->is_keyword_selected());
omnibox_view->RevertAll();
}
// Calling FocusSearch() when the permanent URL is showing should result in an
// empty query string.
{
FocusSearchCheckPreconditions();
omnibox_model->ResetDisplayTexts();
EXPECT_EQ(url::kAboutBlankURL16, omnibox_view->GetText());
location_bar->FocusSearch();
EXPECT_FALSE(location_bar->navigation_params().destination_url.is_valid());
EXPECT_EQ(std::u16string(), omnibox_view->GetText());
EXPECT_EQ(default_search_keyword, omnibox_model->keyword());
EXPECT_FALSE(omnibox_model->is_keyword_hint());
EXPECT_TRUE(omnibox_model->is_keyword_selected());
omnibox_view->RevertAll();
}
}
IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, MemoryTracing) {
auto* in_memory_url_index = InMemoryURLIndexFactory::GetForProfile(profile());
auto* autocomplete_controller = GetAutocompleteController();
const std::vector<std::string> expected_names{
base::StringPrintf("omnibox/in_memory_url_index/0x%" PRIXPTR,
reinterpret_cast<uintptr_t>(in_memory_url_index)),
base::StringPrintf("omnibox/autocomplete_controller/0x%" PRIXPTR,
reinterpret_cast<uintptr_t>(autocomplete_controller))};
auto OnMemoryDumpDone =
[](const std::vector<std::string>& expected_names, base::OnceClosure quit,
bool success, uint64_t dump_guid,
std::unique_ptr<base::trace_event::ProcessMemoryDump> pmd) {
ASSERT_TRUE(success);
const auto& allocator_dumps = pmd->allocator_dumps();
for (const auto& expected_dump_name : expected_names)
EXPECT_TRUE(allocator_dumps.count(expected_dump_name));
std::move(quit).Run();
};
base::RunLoop run_loop;
base::trace_event::MemoryDumpRequestArgs args{
1 /* dump_guid*/, base::trace_event::MemoryDumpType::kExplicitlyTriggered,
base::trace_event::MemoryDumpLevelOfDetail::kBackground};
base::trace_event::MemoryDumpManager::GetInstance()->CreateProcessDump(
args,
base::BindOnce(OnMemoryDumpDone, expected_names, run_loop.QuitClosure()));
run_loop.Run();
}
|