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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 | 65 |
63 // OmniboxViewViews: | 66 // OmniboxViewViews: |
64 void EmphasizeURLComponents() override; | 67 void EmphasizeURLComponents() override; |
65 | 68 |
66 private: | 69 private: |
67 // OmniboxViewViews: | 70 // OmniboxViewViews: |
68 void UpdatePopup() override; | 71 void UpdatePopup() override; |
69 void SetEmphasis(bool emphasize, const Range& range) override; | 72 void SetEmphasis(bool emphasize, const Range& range) override; |
70 void UpdateSchemeStyle(const Range& range) override; | 73 void UpdateSchemeStyle(const Range& range) override; |
71 | 74 |
72 // Simplistic test override returns whether a given string looks like a URL | |
73 // without having to mock AutocompleteClassifier objects and their | |
74 // dependencies. | |
75 bool CurrentTextIsURL() override { | |
76 bool looks_like_url = (text().find(':') != std::string::npos || | |
77 text().find('/') != std::string::npos); | |
78 return looks_like_url; | |
79 } | |
80 | |
81 size_t update_popup_call_count_ = 0; | 75 size_t update_popup_call_count_ = 0; |
82 base::string16 update_popup_text_; | 76 base::string16 update_popup_text_; |
83 Range update_popup_selection_range_; | 77 Range update_popup_selection_range_; |
84 | 78 |
85 // Range of the last scheme logged by UpdateSchemeStyle(). | 79 // Range of the last scheme logged by UpdateSchemeStyle(). |
86 Range scheme_range_; | 80 Range scheme_range_; |
87 | 81 |
88 // Range of the last text emphasized by SetEmphasis(). | 82 // Range of the last text emphasized by SetEmphasis(). |
89 Range emphasis_range_; | 83 Range emphasis_range_; |
90 | 84 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 } | 134 } |
141 | 135 |
142 void TestingOmniboxView::UpdateSchemeStyle(const Range& range) { | 136 void TestingOmniboxView::UpdateSchemeStyle(const Range& range) { |
143 scheme_range_ = range; | 137 scheme_range_ = range; |
144 } | 138 } |
145 | 139 |
146 // TestingOmniboxEditController ----------------------------------------------- | 140 // TestingOmniboxEditController ----------------------------------------------- |
147 | 141 |
148 class TestingOmniboxEditController : public ChromeOmniboxEditController { | 142 class TestingOmniboxEditController : public ChromeOmniboxEditController { |
149 public: | 143 public: |
150 explicit TestingOmniboxEditController(CommandUpdater* command_updater) | 144 TestingOmniboxEditController(CommandUpdater* command_updater, |
151 : ChromeOmniboxEditController(command_updater) {} | 145 ToolbarModel* toolbar_model) |
146 : ChromeOmniboxEditController(command_updater), | |
147 toolbar_model_(toolbar_model) {} | |
152 | 148 |
153 private: | 149 private: |
154 // ChromeOmniboxEditController: | 150 // ChromeOmniboxEditController: |
155 void UpdateWithoutTabRestore() override {} | 151 void UpdateWithoutTabRestore() override {} |
156 void OnChanged() override {} | 152 void OnChanged() override {} |
157 ToolbarModel* GetToolbarModel() override { return nullptr; } | 153 ToolbarModel* GetToolbarModel() override { return toolbar_model_; } |
158 const ToolbarModel* GetToolbarModel() const override { return nullptr; } | 154 const ToolbarModel* GetToolbarModel() const override { |
155 return toolbar_model_; | |
156 } | |
159 content::WebContents* GetWebContents() override { return nullptr; } | 157 content::WebContents* GetWebContents() override { return nullptr; } |
160 | 158 |
159 ToolbarModel* toolbar_model_; | |
160 | |
161 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController); | 161 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController); |
162 }; | 162 }; |
163 | 163 |
164 } // namespace | 164 } // namespace |
165 | 165 |
166 // OmniboxViewViewsTest ------------------------------------------------------- | 166 // OmniboxViewViewsTest ------------------------------------------------------- |
167 | 167 |
168 class OmniboxViewViewsTest : public testing::Test { | 168 class OmniboxViewViewsTest : public testing::Test { |
169 public: | 169 public: |
170 OmniboxViewViewsTest(); | 170 OmniboxViewViewsTest(); |
171 | 171 |
172 TestingOmniboxView* omnibox_view() { return omnibox_view_.get(); } | 172 TestingOmniboxView* omnibox_view() { return omnibox_view_.get(); } |
173 views::Textfield* omnibox_textfield() { return omnibox_view(); } | 173 views::Textfield* omnibox_textfield() { return omnibox_view(); } |
174 ui::TextEditCommand scheduled_text_edit_command() const { | 174 ui::TextEditCommand scheduled_text_edit_command() const { |
175 return test_api_->scheduled_text_edit_command(); | 175 return test_api_->scheduled_text_edit_command(); |
176 } | 176 } |
177 | 177 |
178 // Sets |new_text| as the omnibox text, and emphasizes it appropriately. | 178 // Sets |new_text| as the omnibox text, and emphasizes it appropriately. If |
179 void SetAndEmphasizeText(const std::string& new_text); | 179 // |accept_input| is true, pretends that the user has accepted this input |
180 // (i.e. it's been navigated to). | |
181 void SetAndEmphasizeText(const std::string& new_text, bool accept_input); | |
180 | 182 |
181 private: | 183 private: |
182 // testing::Test: | 184 // testing::Test: |
183 void SetUp() override; | 185 void SetUp() override; |
184 void TearDown() override; | 186 void TearDown() override; |
185 | 187 |
186 content::TestBrowserThreadBundle thread_bundle_; | 188 content::TestBrowserThreadBundle thread_bundle_; |
187 TestingProfile profile_; | 189 TestingProfile profile_; |
190 TemplateURLServiceFactoryTestUtil util_; | |
188 CommandUpdater command_updater_; | 191 CommandUpdater command_updater_; |
192 TestToolbarModel toolbar_model_; | |
189 TestingOmniboxEditController omnibox_edit_controller_; | 193 TestingOmniboxEditController omnibox_edit_controller_; |
190 std::unique_ptr<TestingOmniboxView> omnibox_view_; | 194 std::unique_ptr<TestingOmniboxView> omnibox_view_; |
191 std::unique_ptr<views::TextfieldTestApi> test_api_; | 195 std::unique_ptr<views::TextfieldTestApi> test_api_; |
192 | 196 |
193 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); | 197 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); |
194 }; | 198 }; |
195 | 199 |
196 OmniboxViewViewsTest::OmniboxViewViewsTest() | 200 OmniboxViewViewsTest::OmniboxViewViewsTest() |
197 : command_updater_(nullptr), omnibox_edit_controller_(&command_updater_) {} | 201 : util_(&profile_), |
202 command_updater_(nullptr), | |
203 omnibox_edit_controller_(&command_updater_, &toolbar_model_) {} | |
198 | 204 |
199 void OmniboxViewViewsTest::SetAndEmphasizeText(const std::string& new_text) { | 205 void OmniboxViewViewsTest::SetAndEmphasizeText(const std::string& new_text, |
206 bool accept_input) { | |
200 omnibox_view()->ResetEmphasisTestState(); | 207 omnibox_view()->ResetEmphasisTestState(); |
201 omnibox_view()->SetText(base::ASCIIToUTF16(new_text)); | 208 omnibox_view()->SetUserText(base::ASCIIToUTF16(new_text)); |
209 if (accept_input) { | |
210 // We don't need to actually navigate in this case (and doing so in a test | |
211 // would be difficult); it's sufficient to mark input as "no longer in | |
212 // progress", and the edit model will assume the current text is a URL. | |
213 omnibox_view()->model()->SetInputInProgress(false); | |
214 } | |
202 omnibox_view()->EmphasizeURLComponents(); | 215 omnibox_view()->EmphasizeURLComponents(); |
203 } | 216 } |
204 | 217 |
205 void OmniboxViewViewsTest::SetUp() { | 218 void OmniboxViewViewsTest::SetUp() { |
206 #if defined(OS_CHROMEOS) | 219 #if defined(OS_CHROMEOS) |
207 chromeos::input_method::InitializeForTesting( | 220 chromeos::input_method::InitializeForTesting( |
208 new chromeos::input_method::MockInputMethodManagerImpl); | 221 new chromeos::input_method::MockInputMethodManagerImpl); |
209 #endif | 222 #endif |
223 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( | |
224 &profile_, &AutocompleteClassifierFactory::BuildInstanceFor); | |
210 omnibox_view_ = base::MakeUnique<TestingOmniboxView>( | 225 omnibox_view_ = base::MakeUnique<TestingOmniboxView>( |
211 &omnibox_edit_controller_, | 226 &omnibox_edit_controller_, |
212 base::MakeUnique<ChromeOmniboxClient>(&omnibox_edit_controller_, | 227 base::MakeUnique<ChromeOmniboxClient>(&omnibox_edit_controller_, |
213 &profile_), | 228 &profile_), |
214 &command_updater_); | 229 &command_updater_); |
215 test_api_ = base::MakeUnique<views::TextfieldTestApi>(omnibox_view_.get()); | 230 test_api_ = base::MakeUnique<views::TextfieldTestApi>(omnibox_view_.get()); |
216 omnibox_view_->Init(); | 231 omnibox_view_->Init(); |
217 } | 232 } |
218 | 233 |
219 void OmniboxViewViewsTest::TearDown() { | 234 void OmniboxViewViewsTest::TearDown() { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 ui::TextEditCommand::MOVE_UP); | 271 ui::TextEditCommand::MOVE_UP); |
257 EXPECT_EQ(ui::TextEditCommand::MOVE_UP, scheduled_text_edit_command()); | 272 EXPECT_EQ(ui::TextEditCommand::MOVE_UP, scheduled_text_edit_command()); |
258 | 273 |
259 ui::KeyEvent up_pressed(ui::ET_KEY_PRESSED, ui::VKEY_UP, 0); | 274 ui::KeyEvent up_pressed(ui::ET_KEY_PRESSED, ui::VKEY_UP, 0); |
260 omnibox_textfield()->OnKeyEvent(&up_pressed); | 275 omnibox_textfield()->OnKeyEvent(&up_pressed); |
261 EXPECT_EQ(ui::TextEditCommand::INVALID_COMMAND, | 276 EXPECT_EQ(ui::TextEditCommand::INVALID_COMMAND, |
262 scheduled_text_edit_command()); | 277 scheduled_text_edit_command()); |
263 } | 278 } |
264 | 279 |
265 TEST_F(OmniboxViewViewsTest, Emphasis) { | 280 TEST_F(OmniboxViewViewsTest, Emphasis) { |
266 const struct { | 281 constexpr struct { |
Peter Kasting
2017/03/06 21:18:03
Note: This depends on https://codereview.chromium.
| |
267 const char* input; | 282 const char* input; |
268 bool base_text_emphasized; | 283 bool base_text_emphasized; |
269 Range emphasis_range; | 284 Range emphasis_range; |
270 Range scheme_range; | 285 Range scheme_range; |
271 } test_cases[] = { | 286 } test_cases[] = { |
272 {"data:text/html,Hello%20World", false, Range(0, 4), Range(0, 4)}, | 287 {"data:text/html,Hello%20World", false, Range(0, 4), Range(0, 4)}, |
273 {"http://www.example.com/path/file.htm", false, Range(7, 22), | 288 {"http://www.example.com/path/file.htm", false, Range(7, 22), |
274 Range(0, 4)}, | 289 Range(0, 4)}, |
275 {"https://www.example.com/path/file.htm", false, Range(8, 23), | 290 {"https://www.example.com/path/file.htm", false, Range(8, 23), |
276 Range(0, 5)}, | 291 Range(0, 5)}, |
277 {"chrome-extension://ldfbacdbackkjhclmhnjabngnppnkagl", false, | 292 {"chrome-extension://ldfbacdbackkjhclmhnjabngnppnkagl", false, |
278 Range::InvalidRange(), Range(0, 16)}, | 293 Range::InvalidRange(), Range(0, 16)}, |
279 {"nosuchscheme://opaque/string", true, Range::InvalidRange(), | 294 {"nosuchscheme://opaque/string", true, Range::InvalidRange(), |
280 Range(0, 12)}, | 295 Range(0, 12)}, |
281 {"nosuchscheme:opaquestring", true, Range::InvalidRange(), Range(0, 12)}, | 296 {"nosuchscheme:opaquestring", true, Range::InvalidRange(), Range(0, 12)}, |
282 {"host.com/path/file", false, Range(0, 8), Range::InvalidRange()}, | 297 {"host.com/path/file", false, Range(0, 8), Range::InvalidRange()}, |
283 {"This is plain text", true, Range::InvalidRange(), | 298 {"This is plain text", true, Range::InvalidRange(), |
284 Range::InvalidRange()}, | 299 Range::InvalidRange()}, |
285 }; | 300 }; |
286 | 301 |
287 for (const auto& test_case : test_cases) { | 302 for (const auto& test_case : test_cases) { |
288 SCOPED_TRACE(test_case.input); | 303 SCOPED_TRACE(test_case.input); |
289 | 304 |
290 SetAndEmphasizeText(test_case.input); | 305 SetAndEmphasizeText(test_case.input, false); |
291 EXPECT_EQ(TestingOmniboxView::to_emphasis(test_case.base_text_emphasized), | 306 EXPECT_EQ(TestingOmniboxView::to_emphasis(test_case.base_text_emphasized), |
292 omnibox_view()->base_text_emphasis()); | 307 omnibox_view()->base_text_emphasis()); |
293 EXPECT_EQ(test_case.emphasis_range, omnibox_view()->emphasis_range()); | 308 EXPECT_EQ(test_case.emphasis_range, omnibox_view()->emphasis_range()); |
294 EXPECT_EQ(test_case.scheme_range, omnibox_view()->scheme_range()); | 309 EXPECT_FALSE(omnibox_view()->scheme_range().IsValid()); |
310 | |
311 if (test_case.scheme_range.IsValid()) { | |
elawrence
2017/03/07 03:16:03
Maybe consider a comment here to clarify the less-
| |
312 SetAndEmphasizeText(test_case.input, true); | |
313 EXPECT_EQ(TestingOmniboxView::to_emphasis(test_case.base_text_emphasized), | |
314 omnibox_view()->base_text_emphasis()); | |
315 EXPECT_EQ(test_case.emphasis_range, omnibox_view()->emphasis_range()); | |
316 EXPECT_EQ(test_case.scheme_range, omnibox_view()->scheme_range()); | |
317 } | |
295 } | 318 } |
296 } | 319 } |
OLD | NEW |