OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/ui/views/omnibox/omnibox_view_views.h" | 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
13 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 14 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
14 #include "chrome/browser/command_updater.h" | 15 #include "chrome/browser/command_updater.h" |
| 16 #include "chrome/browser/search_engines/template_url_service_factory_test_util.h
" |
15 #include "chrome/browser/ui/omnibox/chrome_omnibox_client.h" | 17 #include "chrome/browser/ui/omnibox/chrome_omnibox_client.h" |
16 #include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h" | 18 #include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h" |
17 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
18 #include "components/omnibox/browser/omnibox_edit_model.h" | 20 #include "components/omnibox/browser/omnibox_edit_model.h" |
| 21 #include "components/toolbar/test_toolbar_model.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "ui/base/ime/text_edit_commands.h" | 24 #include "ui/base/ime/text_edit_commands.h" |
22 #include "ui/events/event_utils.h" | 25 #include "ui/events/event_utils.h" |
23 #include "ui/events/keycodes/dom/dom_code.h" | 26 #include "ui/events/keycodes/dom/dom_code.h" |
24 #include "ui/views/controls/textfield/textfield_test_api.h" | 27 #include "ui/views/controls/textfield/textfield_test_api.h" |
25 | 28 |
26 #if defined(OS_CHROMEOS) | 29 #if defined(OS_CHROMEOS) |
27 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" | 30 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" |
28 #include "chrome/browser/chromeos/input_method/mock_input_method_manager_impl.h" | 31 #include "chrome/browser/chromeos/input_method/mock_input_method_manager_impl.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 68 |
66 private: | 69 private: |
67 // OmniboxViewViews: | 70 // OmniboxViewViews: |
68 // There is no popup and it doesn't actually matter whether we change the | 71 // There is no popup and it doesn't actually matter whether we change the |
69 // visual style of the text, so these methods are all overridden merely to | 72 // visual style of the text, so these methods are all overridden merely to |
70 // capture relevant state at the time of the call, to be checked by test code. | 73 // capture relevant state at the time of the call, to be checked by test code. |
71 void UpdatePopup() override; | 74 void UpdatePopup() override; |
72 void SetEmphasis(bool emphasize, const Range& range) override; | 75 void SetEmphasis(bool emphasize, const Range& range) override; |
73 void UpdateSchemeStyle(const Range& range) override; | 76 void UpdateSchemeStyle(const Range& range) override; |
74 | 77 |
75 // Simplistic test override returns whether a given string looks like a URL | |
76 // without having to mock AutocompleteClassifier objects and their | |
77 // dependencies. | |
78 bool CurrentTextIsURL() override { | |
79 bool looks_like_url = (text().find(':') != std::string::npos || | |
80 text().find('/') != std::string::npos); | |
81 return looks_like_url; | |
82 } | |
83 | |
84 size_t update_popup_call_count_ = 0; | 78 size_t update_popup_call_count_ = 0; |
85 base::string16 update_popup_text_; | 79 base::string16 update_popup_text_; |
86 Range update_popup_selection_range_; | 80 Range update_popup_selection_range_; |
87 | 81 |
88 // Range of the last scheme logged by UpdateSchemeStyle(). | 82 // Range of the last scheme logged by UpdateSchemeStyle(). |
89 Range scheme_range_; | 83 Range scheme_range_; |
90 | 84 |
91 // Range of the last text emphasized by SetEmphasis(). | 85 // Range of the last text emphasized by SetEmphasis(). |
92 Range emphasis_range_; | 86 Range emphasis_range_; |
93 | 87 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 } | 137 } |
144 | 138 |
145 void TestingOmniboxView::UpdateSchemeStyle(const Range& range) { | 139 void TestingOmniboxView::UpdateSchemeStyle(const Range& range) { |
146 scheme_range_ = range; | 140 scheme_range_ = range; |
147 } | 141 } |
148 | 142 |
149 // TestingOmniboxEditController ----------------------------------------------- | 143 // TestingOmniboxEditController ----------------------------------------------- |
150 | 144 |
151 class TestingOmniboxEditController : public ChromeOmniboxEditController { | 145 class TestingOmniboxEditController : public ChromeOmniboxEditController { |
152 public: | 146 public: |
153 explicit TestingOmniboxEditController(CommandUpdater* command_updater) | 147 TestingOmniboxEditController(CommandUpdater* command_updater, |
154 : ChromeOmniboxEditController(command_updater) {} | 148 ToolbarModel* toolbar_model) |
| 149 : ChromeOmniboxEditController(command_updater), |
| 150 toolbar_model_(toolbar_model) {} |
155 | 151 |
156 private: | 152 private: |
157 // ChromeOmniboxEditController: | 153 // ChromeOmniboxEditController: |
158 void UpdateWithoutTabRestore() override {} | 154 void UpdateWithoutTabRestore() override {} |
159 void OnChanged() override {} | 155 void OnChanged() override {} |
160 ToolbarModel* GetToolbarModel() override { return nullptr; } | 156 ToolbarModel* GetToolbarModel() override { return toolbar_model_; } |
161 const ToolbarModel* GetToolbarModel() const override { return nullptr; } | 157 const ToolbarModel* GetToolbarModel() const override { |
| 158 return toolbar_model_; |
| 159 } |
162 content::WebContents* GetWebContents() override { return nullptr; } | 160 content::WebContents* GetWebContents() override { return nullptr; } |
163 | 161 |
| 162 ToolbarModel* toolbar_model_; |
| 163 |
164 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController); | 164 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController); |
165 }; | 165 }; |
166 | 166 |
167 } // namespace | 167 } // namespace |
168 | 168 |
169 // OmniboxViewViewsTest ------------------------------------------------------- | 169 // OmniboxViewViewsTest ------------------------------------------------------- |
170 | 170 |
171 class OmniboxViewViewsTest : public testing::Test { | 171 class OmniboxViewViewsTest : public testing::Test { |
172 public: | 172 public: |
173 OmniboxViewViewsTest(); | 173 OmniboxViewViewsTest(); |
174 | 174 |
175 TestingOmniboxView* omnibox_view() { return omnibox_view_.get(); } | 175 TestingOmniboxView* omnibox_view() { return omnibox_view_.get(); } |
176 views::Textfield* omnibox_textfield() { return omnibox_view(); } | 176 views::Textfield* omnibox_textfield() { return omnibox_view(); } |
177 ui::TextEditCommand scheduled_text_edit_command() const { | 177 ui::TextEditCommand scheduled_text_edit_command() const { |
178 return test_api_->scheduled_text_edit_command(); | 178 return test_api_->scheduled_text_edit_command(); |
179 } | 179 } |
180 | 180 |
181 // Sets |new_text| as the omnibox text, and emphasizes it appropriately. | 181 // Sets |new_text| as the omnibox text, and emphasizes it appropriately. If |
182 void SetAndEmphasizeText(const std::string& new_text); | 182 // |accept_input| is true, pretends that the user has accepted this input |
| 183 // (i.e. it's been navigated to). |
| 184 void SetAndEmphasizeText(const std::string& new_text, bool accept_input); |
183 | 185 |
184 private: | 186 private: |
185 // testing::Test: | 187 // testing::Test: |
186 void SetUp() override; | 188 void SetUp() override; |
187 void TearDown() override; | 189 void TearDown() override; |
188 | 190 |
189 content::TestBrowserThreadBundle thread_bundle_; | 191 content::TestBrowserThreadBundle thread_bundle_; |
190 TestingProfile profile_; | 192 TestingProfile profile_; |
| 193 TemplateURLServiceFactoryTestUtil util_; |
191 CommandUpdater command_updater_; | 194 CommandUpdater command_updater_; |
| 195 TestToolbarModel toolbar_model_; |
192 TestingOmniboxEditController omnibox_edit_controller_; | 196 TestingOmniboxEditController omnibox_edit_controller_; |
193 std::unique_ptr<TestingOmniboxView> omnibox_view_; | 197 std::unique_ptr<TestingOmniboxView> omnibox_view_; |
194 std::unique_ptr<views::TextfieldTestApi> test_api_; | 198 std::unique_ptr<views::TextfieldTestApi> test_api_; |
195 | 199 |
196 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); | 200 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); |
197 }; | 201 }; |
198 | 202 |
199 OmniboxViewViewsTest::OmniboxViewViewsTest() | 203 OmniboxViewViewsTest::OmniboxViewViewsTest() |
200 : command_updater_(nullptr), omnibox_edit_controller_(&command_updater_) {} | 204 : util_(&profile_), |
| 205 command_updater_(nullptr), |
| 206 omnibox_edit_controller_(&command_updater_, &toolbar_model_) {} |
201 | 207 |
202 void OmniboxViewViewsTest::SetAndEmphasizeText(const std::string& new_text) { | 208 void OmniboxViewViewsTest::SetAndEmphasizeText(const std::string& new_text, |
| 209 bool accept_input) { |
203 omnibox_view()->ResetEmphasisTestState(); | 210 omnibox_view()->ResetEmphasisTestState(); |
204 omnibox_view()->SetText(base::ASCIIToUTF16(new_text)); | 211 omnibox_view()->SetUserText(base::ASCIIToUTF16(new_text)); |
| 212 if (accept_input) { |
| 213 // We don't need to actually navigate in this case (and doing so in a test |
| 214 // would be difficult); it's sufficient to mark input as "no longer in |
| 215 // progress", and the edit model will assume the current text is a URL. |
| 216 omnibox_view()->model()->SetInputInProgress(false); |
| 217 } |
205 omnibox_view()->EmphasizeURLComponents(); | 218 omnibox_view()->EmphasizeURLComponents(); |
206 } | 219 } |
207 | 220 |
208 void OmniboxViewViewsTest::SetUp() { | 221 void OmniboxViewViewsTest::SetUp() { |
209 #if defined(OS_CHROMEOS) | 222 #if defined(OS_CHROMEOS) |
210 chromeos::input_method::InitializeForTesting( | 223 chromeos::input_method::InitializeForTesting( |
211 new chromeos::input_method::MockInputMethodManagerImpl); | 224 new chromeos::input_method::MockInputMethodManagerImpl); |
212 #endif | 225 #endif |
| 226 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( |
| 227 &profile_, &AutocompleteClassifierFactory::BuildInstanceFor); |
213 omnibox_view_ = base::MakeUnique<TestingOmniboxView>( | 228 omnibox_view_ = base::MakeUnique<TestingOmniboxView>( |
214 &omnibox_edit_controller_, | 229 &omnibox_edit_controller_, |
215 base::MakeUnique<ChromeOmniboxClient>(&omnibox_edit_controller_, | 230 base::MakeUnique<ChromeOmniboxClient>(&omnibox_edit_controller_, |
216 &profile_), | 231 &profile_), |
217 &command_updater_); | 232 &command_updater_); |
218 test_api_ = base::MakeUnique<views::TextfieldTestApi>(omnibox_view_.get()); | 233 test_api_ = base::MakeUnique<views::TextfieldTestApi>(omnibox_view_.get()); |
219 omnibox_view_->Init(); | 234 omnibox_view_->Init(); |
220 } | 235 } |
221 | 236 |
222 void OmniboxViewViewsTest::TearDown() { | 237 void OmniboxViewViewsTest::TearDown() { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 Range(0, 12)}, | 298 Range(0, 12)}, |
284 {"nosuchscheme:opaquestring", true, Range::InvalidRange(), Range(0, 12)}, | 299 {"nosuchscheme:opaquestring", true, Range::InvalidRange(), Range(0, 12)}, |
285 {"host.com/path/file", false, Range(0, 8), Range::InvalidRange()}, | 300 {"host.com/path/file", false, Range(0, 8), Range::InvalidRange()}, |
286 {"This is plain text", true, Range::InvalidRange(), | 301 {"This is plain text", true, Range::InvalidRange(), |
287 Range::InvalidRange()}, | 302 Range::InvalidRange()}, |
288 }; | 303 }; |
289 | 304 |
290 for (const auto& test_case : test_cases) { | 305 for (const auto& test_case : test_cases) { |
291 SCOPED_TRACE(test_case.input); | 306 SCOPED_TRACE(test_case.input); |
292 | 307 |
293 SetAndEmphasizeText(test_case.input); | 308 SetAndEmphasizeText(test_case.input, false); |
294 EXPECT_EQ(TestingOmniboxView::to_base_text_emphasis( | 309 EXPECT_EQ(TestingOmniboxView::to_base_text_emphasis( |
295 test_case.expected_base_text_emphasized), | 310 test_case.expected_base_text_emphasized), |
296 omnibox_view()->base_text_emphasis()); | 311 omnibox_view()->base_text_emphasis()); |
297 EXPECT_EQ(test_case.expected_emphasis_range, | 312 EXPECT_EQ(test_case.expected_emphasis_range, |
298 omnibox_view()->emphasis_range()); | 313 omnibox_view()->emphasis_range()); |
299 EXPECT_EQ(test_case.expected_scheme_range, omnibox_view()->scheme_range()); | 314 EXPECT_FALSE(omnibox_view()->scheme_range().IsValid()); |
| 315 |
| 316 if (test_case.expected_scheme_range.IsValid()) { |
| 317 SetAndEmphasizeText(test_case.input, true); |
| 318 EXPECT_EQ(TestingOmniboxView::to_base_text_emphasis( |
| 319 test_case.expected_base_text_emphasized), |
| 320 omnibox_view()->base_text_emphasis()); |
| 321 EXPECT_EQ(test_case.expected_emphasis_range, |
| 322 omnibox_view()->emphasis_range()); |
| 323 EXPECT_EQ(test_case.expected_scheme_range, |
| 324 omnibox_view()->scheme_range()); |
| 325 } |
300 } | 326 } |
301 } | 327 } |
OLD | NEW |