OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autocomplete/autocomplete.h" | 5 #include "chrome/browser/autocomplete/autocomplete.h" |
6 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 6 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
7 #include "chrome/browser/autocomplete/autocomplete_edit_view.h" | 7 #include "chrome/browser/autocomplete/autocomplete_edit_view.h" |
8 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" | 8 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
9 #include "chrome/browser/browser.h" | 9 #include "chrome/browser/browser.h" |
10 #include "chrome/browser/browser_window.h" | 10 #include "chrome/browser/browser_window.h" |
| 11 #include "chrome/browser/history/history.h" |
11 #include "chrome/browser/location_bar.h" | 12 #include "chrome/browser/location_bar.h" |
| 13 #include "chrome/browser/profile.h" |
| 14 #include "chrome/common/notification_registrar.h" |
| 15 #include "chrome/common/notification_service.h" |
| 16 #include "chrome/common/notification_type.h" |
12 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
13 #include "chrome/test/in_process_browser_test.h" | 18 #include "chrome/test/in_process_browser_test.h" |
14 #include "chrome/test/ui_test_utils.h" | 19 #include "chrome/test/ui_test_utils.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
16 | 21 |
17 namespace { | 22 namespace { |
18 | 23 |
19 std::wstring AutocompleteResultAsString(const AutocompleteResult& result) { | 24 std::wstring AutocompleteResultAsString(const AutocompleteResult& result) { |
20 std::wstring output(StringPrintf(L"{%d} ", result.size())); | 25 std::wstring output(StringPrintf(L"{%d} ", result.size())); |
21 for (size_t i = 0; i < result.size(); ++i) { | 26 for (size_t i = 0; i < result.size(); ++i) { |
22 AutocompleteMatch match = result.match_at(i); | 27 AutocompleteMatch match = result.match_at(i); |
23 std::wstring provider_name(ASCIIToWide(match.provider->name())); | 28 std::wstring provider_name(ASCIIToWide(match.provider->name())); |
24 output.append(StringPrintf(L"[\"%ls\" by \"%ls\"] ", | 29 output.append(StringPrintf(L"[\"%ls\" by \"%ls\"] ", |
25 match.contents.c_str(), | 30 match.contents.c_str(), |
26 provider_name.c_str())); | 31 provider_name.c_str())); |
27 } | 32 } |
28 return output; | 33 return output; |
29 } | 34 } |
30 | 35 |
31 } // namespace | 36 } // namespace |
32 | 37 |
33 class AutocompleteBrowserTest : public InProcessBrowserTest { | 38 class AutocompleteBrowserTest : public InProcessBrowserTest, |
| 39 public NotificationObserver { |
34 protected: | 40 protected: |
35 LocationBar* GetLocationBar() const { | 41 LocationBar* GetLocationBar() const { |
36 return browser()->window()->GetLocationBar(); | 42 return browser()->window()->GetLocationBar(); |
37 } | 43 } |
38 | 44 |
39 AutocompleteController* GetAutocompleteController() const { | 45 AutocompleteController* GetAutocompleteController() const { |
40 return GetLocationBar()->location_entry()->model()->popup_model()-> | 46 return GetLocationBar()->location_entry()->model()->popup_model()-> |
41 autocomplete_controller(); | 47 autocomplete_controller(); |
42 } | 48 } |
| 49 |
| 50 void WaitForHistoryBackendToLoad() { |
| 51 HistoryService* history_service = |
| 52 browser()->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 53 if (!history_service->backend_loaded()) { |
| 54 NotificationRegistrar registrar; |
| 55 registrar.Add(this, NotificationType::HISTORY_LOADED, |
| 56 NotificationService::AllSources()); |
| 57 ui_test_utils::RunMessageLoop(); |
| 58 } |
| 59 } |
| 60 |
| 61 virtual void Observe(NotificationType type, |
| 62 const NotificationSource& source, |
| 63 const NotificationDetails& details) { |
| 64 DCHECK(type == NotificationType::HISTORY_LOADED); |
| 65 MessageLoop::current()->Quit(); |
| 66 } |
| 67 |
43 }; | 68 }; |
44 | 69 |
45 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) { | 70 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) { |
46 LocationBar* location_bar = GetLocationBar(); | 71 LocationBar* location_bar = GetLocationBar(); |
47 | 72 |
48 EXPECT_EQ(std::wstring(), location_bar->GetInputString()); | 73 EXPECT_EQ(std::wstring(), location_bar->GetInputString()); |
49 EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL), | 74 EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL), |
50 location_bar->location_entry()->GetText()); | 75 location_bar->location_entry()->GetText()); |
51 // TODO(phajdan.jr): check state of IsSelectAll when it's consistent across | 76 // TODO(phajdan.jr): check state of IsSelectAll when it's consistent across |
52 // platforms. | 77 // platforms. |
(...skipping 21 matching lines...) Expand all Loading... |
74 location_bar->location_entry()->SetUserText(L"chrome"); | 99 location_bar->location_entry()->SetUserText(L"chrome"); |
75 location_bar->Revert(); | 100 location_bar->Revert(); |
76 | 101 |
77 EXPECT_EQ(std::wstring(), location_bar->GetInputString()); | 102 EXPECT_EQ(std::wstring(), location_bar->GetInputString()); |
78 EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL), | 103 EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL), |
79 location_bar->location_entry()->GetText()); | 104 location_bar->location_entry()->GetText()); |
80 EXPECT_FALSE(location_bar->location_entry()->IsSelectAll()); | 105 EXPECT_FALSE(location_bar->location_entry()->IsSelectAll()); |
81 } | 106 } |
82 | 107 |
83 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Autocomplete) { | 108 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Autocomplete) { |
| 109 // The results depend on the history backend being loaded. Make sure it is |
| 110 // loaded so that the autocomplete results are consistent. |
| 111 WaitForHistoryBackendToLoad(); |
| 112 |
84 LocationBar* location_bar = GetLocationBar(); | 113 LocationBar* location_bar = GetLocationBar(); |
85 AutocompleteController* autocomplete_controller = GetAutocompleteController(); | 114 AutocompleteController* autocomplete_controller = GetAutocompleteController(); |
86 | 115 |
87 { | 116 { |
88 autocomplete_controller->Start(L"chrome", std::wstring(), | 117 autocomplete_controller->Start(L"chrome", std::wstring(), |
89 true, false, true); | 118 true, false, true); |
90 | 119 |
91 EXPECT_TRUE(autocomplete_controller->done()); | 120 EXPECT_TRUE(autocomplete_controller->done()); |
92 EXPECT_EQ(std::wstring(), location_bar->GetInputString()); | 121 EXPECT_EQ(std::wstring(), location_bar->GetInputString()); |
93 EXPECT_EQ(std::wstring(), location_bar->location_entry()->GetText()); | 122 EXPECT_EQ(std::wstring(), location_bar->location_entry()->GetText()); |
94 EXPECT_TRUE(location_bar->location_entry()->IsSelectAll()); | 123 EXPECT_TRUE(location_bar->location_entry()->IsSelectAll()); |
95 const AutocompleteResult& result = autocomplete_controller->result(); | 124 const AutocompleteResult& result = autocomplete_controller->result(); |
96 ASSERT_EQ(1U, result.size()) << AutocompleteResultAsString(result); | 125 ASSERT_EQ(1U, result.size()) << AutocompleteResultAsString(result); |
97 AutocompleteMatch match = result.match_at(0); | 126 AutocompleteMatch match = result.match_at(0); |
98 EXPECT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, match.type); | 127 EXPECT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, match.type); |
99 EXPECT_FALSE(match.deletable); | 128 EXPECT_FALSE(match.deletable); |
100 } | 129 } |
101 | 130 |
102 { | 131 { |
103 location_bar->Revert(); | 132 location_bar->Revert(); |
104 | 133 |
105 EXPECT_EQ(std::wstring(), location_bar->GetInputString()); | 134 EXPECT_EQ(std::wstring(), location_bar->GetInputString()); |
106 EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL), | 135 EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL), |
107 location_bar->location_entry()->GetText()); | 136 location_bar->location_entry()->GetText()); |
108 EXPECT_FALSE(location_bar->location_entry()->IsSelectAll()); | 137 EXPECT_FALSE(location_bar->location_entry()->IsSelectAll()); |
109 const AutocompleteResult& result = autocomplete_controller->result(); | 138 const AutocompleteResult& result = autocomplete_controller->result(); |
110 EXPECT_TRUE(result.empty()) << AutocompleteResultAsString(result); | 139 EXPECT_TRUE(result.empty()) << AutocompleteResultAsString(result); |
111 } | 140 } |
112 } | 141 } |
OLD | NEW |