Chromium Code Reviews| 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/chrome_autocomplete_scheme_classifier.h" | |
| 13 #include "chrome/browser/command_updater.h" | 14 #include "chrome/browser/command_updater.h" |
| 14 #include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h" | 15 #include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h" |
| 15 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/base/ime/text_edit_commands.h" | 19 #include "ui/base/ime/text_edit_commands.h" |
| 19 #include "ui/events/event_utils.h" | 20 #include "ui/events/event_utils.h" |
| 20 #include "ui/events/keycodes/dom/dom_code.h" | 21 #include "ui/events/keycodes/dom/dom_code.h" |
| 21 #include "ui/views/controls/textfield/textfield_test_api.h" | 22 #include "ui/views/controls/textfield/textfield_test_api.h" |
| 22 | 23 |
| 23 #if defined(OS_CHROMEOS) | 24 #if defined(OS_CHROMEOS) |
| 24 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" | 25 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" |
| 25 #include "chrome/browser/chromeos/input_method/mock_input_method_manager_impl.h" | 26 #include "chrome/browser/chromeos/input_method/mock_input_method_manager_impl.h" |
| 26 #endif | 27 #endif |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 class TestingOmniboxViewViews : public OmniboxViewViews { | 31 class TestingOmniboxViewViews : public OmniboxViewViews { |
| 31 public: | 32 public: |
| 32 TestingOmniboxViewViews(OmniboxEditController* controller, | 33 TestingOmniboxViewViews(OmniboxEditController* controller, |
| 33 Profile* profile, | 34 Profile* profile, |
| 34 CommandUpdater* command_updater) | 35 CommandUpdater* command_updater) |
| 35 : OmniboxViewViews(controller, profile, command_updater, false, NULL, | 36 : OmniboxViewViews(controller, |
| 37 profile, | |
| 38 command_updater, | |
| 39 false, | |
| 40 NULL, | |
|
Peter Kasting
2017/03/01 02:39:11
Nit: While here: nullptr
elawrence
2017/03/01 21:49:01
Done.
| |
| 36 gfx::FontList()), | 41 gfx::FontList()), |
| 37 update_popup_call_count_(0) { | 42 update_popup_call_count_(0), |
| 38 } | 43 profile_(profile) {} |
| 39 | 44 |
| 40 void CheckUpdatePopupCallInfo(size_t call_count, const base::string16& text, | 45 void CheckUpdatePopupCallInfo(size_t call_count, const base::string16& text, |
| 41 const gfx::Range& selection_range) { | 46 const gfx::Range& selection_range) { |
| 42 EXPECT_EQ(call_count, update_popup_call_count_); | 47 EXPECT_EQ(call_count, update_popup_call_count_); |
| 43 EXPECT_EQ(text, update_popup_text_); | 48 EXPECT_EQ(text, update_popup_text_); |
| 44 EXPECT_EQ(selection_range, update_popup_selection_range_); | 49 EXPECT_EQ(selection_range, update_popup_selection_range_); |
| 45 } | 50 } |
| 46 | 51 |
| 52 void EmphasizeURLComponents() override { | |
| 53 ApplyEmphasis(text(), ChromeAutocompleteSchemeClassifier(profile_)); | |
| 54 } | |
| 55 | |
| 56 void SetEmphasis(bool emphasize, gfx::Range range) override { | |
| 57 if (range == gfx::Range::InvalidRange()) { | |
| 58 base_text_is_emphasized_ = emphasize; | |
| 59 return; | |
| 60 } | |
| 61 | |
| 62 EXPECT_TRUE(emphasize); | |
| 63 emphasis_range_ = range; | |
| 64 } | |
| 65 | |
| 66 void UpdateSchemeEmphasis(gfx::Range range) override { | |
| 67 scheme_range_ = range; | |
| 68 } | |
| 69 | |
| 70 gfx::Range GetSchemeRange() { return scheme_range_; } | |
|
Peter Kasting
2017/03/01 02:39:11
Nit: Next three can be const.
I suggest naming th
elawrence
2017/03/01 21:49:01
Done.
| |
| 71 | |
| 72 gfx::Range GetEmphasisRange() { return emphasis_range_; } | |
| 73 | |
| 74 bool IsBaseTextEmphasized() { return base_text_is_emphasized_; } | |
| 75 | |
| 47 private: | 76 private: |
| 48 // OmniboxView: | 77 // OmniboxView: |
| 49 void UpdatePopup() override { | 78 void UpdatePopup() override { |
| 50 ++update_popup_call_count_; | 79 ++update_popup_call_count_; |
| 51 update_popup_text_ = text(); | 80 update_popup_text_ = text(); |
| 52 update_popup_selection_range_ = GetSelectedRange(); | 81 update_popup_selection_range_ = GetSelectedRange(); |
| 53 } | 82 } |
| 54 | 83 |
| 55 size_t update_popup_call_count_; | 84 size_t update_popup_call_count_; |
| 56 base::string16 update_popup_text_; | 85 base::string16 update_popup_text_; |
| 57 gfx::Range update_popup_selection_range_; | 86 gfx::Range update_popup_selection_range_; |
| 87 Profile* profile_; | |
| 88 | |
| 89 // Range of the last scheme logged by |UpdateSchemeEmphasis()|. | |
| 90 gfx::Range scheme_range_; | |
| 91 | |
| 92 // Range of the last text emphasized by |SetEmphasis()|. | |
| 93 gfx::Range emphasis_range_; | |
| 94 | |
| 95 // |SetEmphasis()| logs whether the base color of the text is emphasized. | |
| 96 bool base_text_is_emphasized_; | |
| 58 | 97 |
| 59 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxViewViews); | 98 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxViewViews); |
| 60 }; | 99 }; |
| 61 | 100 |
| 62 class TestingOmniboxEditController : public ChromeOmniboxEditController { | 101 class TestingOmniboxEditController : public ChromeOmniboxEditController { |
| 63 public: | 102 public: |
| 64 explicit TestingOmniboxEditController(CommandUpdater* command_updater) | 103 explicit TestingOmniboxEditController(CommandUpdater* command_updater) |
| 65 : ChromeOmniboxEditController(command_updater) {} | 104 : ChromeOmniboxEditController(command_updater) {} |
| 66 | 105 |
| 67 protected: | 106 protected: |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 TEST_F(OmniboxViewViewsTest, ScheduledTextEditCommand) { | 195 TEST_F(OmniboxViewViewsTest, ScheduledTextEditCommand) { |
| 157 omnibox_textfield()->SetTextEditCommandForNextKeyEvent( | 196 omnibox_textfield()->SetTextEditCommandForNextKeyEvent( |
| 158 ui::TextEditCommand::MOVE_UP); | 197 ui::TextEditCommand::MOVE_UP); |
| 159 EXPECT_EQ(ui::TextEditCommand::MOVE_UP, scheduled_text_edit_command()); | 198 EXPECT_EQ(ui::TextEditCommand::MOVE_UP, scheduled_text_edit_command()); |
| 160 | 199 |
| 161 ui::KeyEvent up_pressed(ui::ET_KEY_PRESSED, ui::VKEY_UP, 0); | 200 ui::KeyEvent up_pressed(ui::ET_KEY_PRESSED, ui::VKEY_UP, 0); |
| 162 omnibox_textfield()->OnKeyEvent(&up_pressed); | 201 omnibox_textfield()->OnKeyEvent(&up_pressed); |
| 163 EXPECT_EQ(ui::TextEditCommand::INVALID_COMMAND, | 202 EXPECT_EQ(ui::TextEditCommand::INVALID_COMMAND, |
| 164 scheduled_text_edit_command()); | 203 scheduled_text_edit_command()); |
| 165 } | 204 } |
| 205 | |
| 206 // Ensure only scheme is emphasized for data: URLs. | |
| 207 TEST_F(OmniboxViewViewsTest, TestEmphasisForDATA) { | |
| 208 omnibox_textfield()->SetText( | |
| 209 base::ASCIIToUTF16("data:text/html,Hello%20World")); | |
| 210 omnibox_view()->EmphasizeURLComponents(); | |
| 211 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->GetSchemeRange()); | |
| 212 EXPECT_FALSE(omnibox_view()->IsBaseTextEmphasized()); | |
| 213 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->GetEmphasisRange()); | |
| 214 } | |
| 215 | |
| 216 // Ensure only origin is emphasized for http: URLs. | |
| 217 TEST_F(OmniboxViewViewsTest, TestEmphasisForHTTP) { | |
| 218 omnibox_textfield()->SetText( | |
| 219 base::ASCIIToUTF16("http://www.example.com/path/file.htm")); | |
| 220 omnibox_view()->EmphasizeURLComponents(); | |
| 221 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->GetSchemeRange()); | |
| 222 EXPECT_FALSE(omnibox_view()->IsBaseTextEmphasized()); | |
| 223 EXPECT_EQ(gfx::Range(7, 22), omnibox_view()->GetEmphasisRange()); | |
| 224 } | |
| 225 | |
| 226 // Ensure only origin is emphasized for https: URLs. | |
| 227 TEST_F(OmniboxViewViewsTest, TestEmphasisForHTTPS) { | |
| 228 omnibox_textfield()->SetText( | |
| 229 base::ASCIIToUTF16("https://www.example.com/path/file.htm")); | |
| 230 omnibox_view()->EmphasizeURLComponents(); | |
| 231 EXPECT_EQ(gfx::Range(0, 5), omnibox_view()->GetSchemeRange()); | |
| 232 EXPECT_FALSE(omnibox_view()->IsBaseTextEmphasized()); | |
| 233 EXPECT_EQ(gfx::Range(8, 23), omnibox_view()->GetEmphasisRange()); | |
| 234 } | |
| 235 | |
| 236 // Ensure that nothing is emphasized for chrome-extension: URLs. | |
| 237 TEST_F(OmniboxViewViewsTest, TestEmphasisForChromeExtensionScheme) { | |
| 238 omnibox_textfield()->SetText(base::ASCIIToUTF16( | |
| 239 "chrome-extension://ldfbacdbackkjhclmhnjabngnppnkagl")); | |
| 240 omnibox_view()->EmphasizeURLComponents(); | |
| 241 EXPECT_EQ(gfx::Range(0, 16), omnibox_view()->GetSchemeRange()); | |
| 242 EXPECT_FALSE(omnibox_view()->IsBaseTextEmphasized()); | |
| 243 EXPECT_EQ(gfx::Range(), omnibox_view()->GetEmphasisRange()); | |
| 244 } | |
| 245 | |
| 246 // Ensure that everything is emphasized for unknown scheme heirarchical URLs. | |
|
Peter Kasting
2017/03/01 02:39:11
Nit: hierarchical
elawrence
2017/03/01 21:49:01
Done.
| |
| 247 TEST_F(OmniboxViewViewsTest, TestEmphasisForUnknownHierarchicalScheme) { | |
| 248 omnibox_textfield()->SetText( | |
| 249 base::ASCIIToUTF16("nosuchscheme://opaque/string")); | |
| 250 omnibox_view()->EmphasizeURLComponents(); | |
| 251 EXPECT_EQ(gfx::Range(0, 12), omnibox_view()->GetSchemeRange()); | |
| 252 EXPECT_TRUE(omnibox_view()->IsBaseTextEmphasized()); | |
| 253 EXPECT_EQ(gfx::Range(), omnibox_view()->GetEmphasisRange()); | |
| 254 } | |
| 255 | |
| 256 // Ensure that everything is emphasized for unknown scheme URLs. | |
| 257 TEST_F(OmniboxViewViewsTest, TestEmphasisForUnknownScheme) { | |
| 258 omnibox_textfield()->SetText(base::ASCIIToUTF16("nosuchscheme:opaquestring")); | |
| 259 omnibox_view()->EmphasizeURLComponents(); | |
| 260 EXPECT_EQ(gfx::Range(0, 12), omnibox_view()->GetSchemeRange()); | |
| 261 EXPECT_TRUE(omnibox_view()->IsBaseTextEmphasized()); | |
| 262 EXPECT_EQ(gfx::Range(), omnibox_view()->GetEmphasisRange()); | |
| 263 } | |
| 264 | |
| 265 // Ensure that Origin is emphasized for URL-like text. | |
|
Peter Kasting
2017/03/01 02:39:10
Nit: Origin -> the origin
elawrence
2017/03/01 21:49:01
Done.
| |
| 266 TEST_F(OmniboxViewViewsTest, TestEmphasisForPartialURLs) { | |
| 267 omnibox_textfield()->SetText(base::ASCIIToUTF16("example/path/file")); | |
| 268 omnibox_view()->EmphasizeURLComponents(); | |
| 269 EXPECT_EQ(gfx::Range::InvalidRange(), omnibox_view()->GetSchemeRange()); | |
| 270 EXPECT_FALSE(omnibox_view()->IsBaseTextEmphasized()); | |
| 271 EXPECT_EQ(gfx::Range(0, 7), omnibox_view()->GetEmphasisRange()); | |
| 272 } | |
| 273 | |
| 274 // Non-URL text gets treated as a URL where the entire string is interpreted | |
| 275 // as a hostname. | |
|
Peter Kasting
2017/03/01 02:39:10
That doesn't sound right. CurrentTextIsURL() ough
elawrence
2017/03/01 21:49:01
The problem is that OmniboxEditModel::CurrentTextI
Peter Kasting
2017/03/01 23:18:47
That's because you're calling SetText() in all of
elawrence
2017/03/01 23:50:53
Yes, SetUserText() code path that leads to the des
elawrence
2017/03/02 19:08:01
Exposing CurrentTextIsURL as a virtual function on
| |
| 276 TEST_F(OmniboxViewViewsTest, TestEmphasisForNonURLs) { | |
| 277 omnibox_textfield()->SetText(base::ASCIIToUTF16("This is plain text")); | |
| 278 omnibox_view()->EmphasizeURLComponents(); | |
| 279 EXPECT_EQ(gfx::Range::InvalidRange(), omnibox_view()->GetSchemeRange()); | |
| 280 EXPECT_FALSE(omnibox_view()->IsBaseTextEmphasized()); | |
| 281 EXPECT_EQ(gfx::Range(0, 18), omnibox_view()->GetEmphasisRange()); | |
| 282 } | |
| OLD | NEW |