| 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 19 matching lines...) Expand all Loading... |
| 48 | 51 |
| 49 // OmniboxViewViews: | 52 // OmniboxViewViews: |
| 50 void EmphasizeURLComponents() override; | 53 void EmphasizeURLComponents() override; |
| 51 | 54 |
| 52 private: | 55 private: |
| 53 // OmniboxViewViews: | 56 // OmniboxViewViews: |
| 54 void UpdatePopup() override; | 57 void UpdatePopup() override; |
| 55 void SetEmphasis(bool emphasize, const gfx::Range& range) override; | 58 void SetEmphasis(bool emphasize, const gfx::Range& range) override; |
| 56 void UpdateSchemeStyle(const gfx::Range& range) override; | 59 void UpdateSchemeStyle(const gfx::Range& range) override; |
| 57 | 60 |
| 58 // Simplistic test override returns whether a given string looks like a URL | |
| 59 // without having to mock AutocompleteClassifier objects and their | |
| 60 // dependencies. | |
| 61 bool CurrentTextIsURL() override { | |
| 62 bool looks_like_url = (text().find(':') != std::string::npos || | |
| 63 text().find('/') != std::string::npos); | |
| 64 return looks_like_url; | |
| 65 } | |
| 66 | |
| 67 size_t update_popup_call_count_; | 61 size_t update_popup_call_count_; |
| 68 base::string16 update_popup_text_; | 62 base::string16 update_popup_text_; |
| 69 gfx::Range update_popup_selection_range_; | 63 gfx::Range update_popup_selection_range_; |
| 70 | 64 |
| 71 // Range of the last scheme logged by UpdateSchemeStyle(). | 65 // Range of the last scheme logged by UpdateSchemeStyle(). |
| 72 gfx::Range scheme_range_; | 66 gfx::Range scheme_range_; |
| 73 | 67 |
| 74 // Range of the last text emphasized by SetEmphasis(). | 68 // Range of the last text emphasized by SetEmphasis(). |
| 75 gfx::Range emphasis_range_; | 69 gfx::Range emphasis_range_; |
| 76 | 70 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 } | 118 } |
| 125 | 119 |
| 126 void TestingOmniboxViewViews::UpdateSchemeStyle(const gfx::Range& range) { | 120 void TestingOmniboxViewViews::UpdateSchemeStyle(const gfx::Range& range) { |
| 127 scheme_range_ = range; | 121 scheme_range_ = range; |
| 128 } | 122 } |
| 129 | 123 |
| 130 // TestingOmniboxEditController ----------------------------------------------- | 124 // TestingOmniboxEditController ----------------------------------------------- |
| 131 | 125 |
| 132 class TestingOmniboxEditController : public ChromeOmniboxEditController { | 126 class TestingOmniboxEditController : public ChromeOmniboxEditController { |
| 133 public: | 127 public: |
| 134 explicit TestingOmniboxEditController(CommandUpdater* command_updater) | 128 TestingOmniboxEditController(CommandUpdater* command_updater, |
| 135 : ChromeOmniboxEditController(command_updater) {} | 129 ToolbarModel* toolbar_model) |
| 130 : ChromeOmniboxEditController(command_updater), |
| 131 toolbar_model_(toolbar_model) {} |
| 136 | 132 |
| 137 private: | 133 private: |
| 138 // ChromeOmniboxEditController: | 134 // ChromeOmniboxEditController: |
| 139 void UpdateWithoutTabRestore() override {} | 135 void UpdateWithoutTabRestore() override {} |
| 140 void OnChanged() override {} | 136 void OnChanged() override {} |
| 141 ToolbarModel* GetToolbarModel() override { return nullptr; } | 137 ToolbarModel* GetToolbarModel() override { return toolbar_model_; } |
| 142 const ToolbarModel* GetToolbarModel() const override { return nullptr; } | 138 const ToolbarModel* GetToolbarModel() const override { |
| 139 return toolbar_model_; |
| 140 } |
| 143 content::WebContents* GetWebContents() override { return nullptr; } | 141 content::WebContents* GetWebContents() override { return nullptr; } |
| 144 | 142 |
| 143 ToolbarModel* toolbar_model_; |
| 144 |
| 145 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController); | 145 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController); |
| 146 }; | 146 }; |
| 147 | 147 |
| 148 } // namespace | 148 } // namespace |
| 149 | 149 |
| 150 // OmniboxViewViewsTest ------------------------------------------------------- | 150 // OmniboxViewViewsTest ------------------------------------------------------- |
| 151 | 151 |
| 152 class OmniboxViewViewsTest : public testing::Test { | 152 class OmniboxViewViewsTest : public testing::Test { |
| 153 public: | 153 public: |
| 154 OmniboxViewViewsTest(); | 154 OmniboxViewViewsTest(); |
| 155 | 155 |
| 156 TestingOmniboxViewViews* omnibox_view() { return omnibox_view_.get(); } | 156 TestingOmniboxViewViews* omnibox_view() { return omnibox_view_.get(); } |
| 157 views::Textfield* omnibox_textfield() { return omnibox_view(); } | 157 views::Textfield* omnibox_textfield() { return omnibox_view(); } |
| 158 ui::TextEditCommand scheduled_text_edit_command() const { | 158 ui::TextEditCommand scheduled_text_edit_command() const { |
| 159 return test_api_->scheduled_text_edit_command(); | 159 return test_api_->scheduled_text_edit_command(); |
| 160 } | 160 } |
| 161 | 161 |
| 162 // Sets |new_text| as the text in the omnibox. | 162 // Sets |new_text| as the text in the omnibox. If |accept_input| is true, |
| 163 void SetAndEmphasizeText(const std::string& new_text); | 163 // pretends that the user has accepted this input (i.e. it's been navigated |
| 164 // to). |
| 165 void SetAndEmphasizeText(const std::string& new_text, |
| 166 bool accept_input = true); |
| 164 | 167 |
| 165 private: | 168 private: |
| 166 // testing::Test: | 169 // testing::Test: |
| 167 void SetUp() override; | 170 void SetUp() override; |
| 168 void TearDown() override; | 171 void TearDown() override; |
| 169 | 172 |
| 170 content::TestBrowserThreadBundle thread_bundle_; | 173 content::TestBrowserThreadBundle thread_bundle_; |
| 171 TestingProfile profile_; | 174 TestingProfile profile_; |
| 175 TemplateURLServiceFactoryTestUtil util_; |
| 172 CommandUpdater command_updater_; | 176 CommandUpdater command_updater_; |
| 177 TestToolbarModel toolbar_model_; |
| 173 TestingOmniboxEditController omnibox_edit_controller_; | 178 TestingOmniboxEditController omnibox_edit_controller_; |
| 174 std::unique_ptr<TestingOmniboxViewViews> omnibox_view_; | 179 std::unique_ptr<TestingOmniboxViewViews> omnibox_view_; |
| 175 std::unique_ptr<views::TextfieldTestApi> test_api_; | 180 std::unique_ptr<views::TextfieldTestApi> test_api_; |
| 176 | 181 |
| 177 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); | 182 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); |
| 178 }; | 183 }; |
| 179 | 184 |
| 180 OmniboxViewViewsTest::OmniboxViewViewsTest() | 185 OmniboxViewViewsTest::OmniboxViewViewsTest() |
| 181 : command_updater_(nullptr), omnibox_edit_controller_(&command_updater_) {} | 186 : util_(&profile_), |
| 187 command_updater_(nullptr), |
| 188 omnibox_edit_controller_(&command_updater_, &toolbar_model_) {} |
| 182 | 189 |
| 183 void OmniboxViewViewsTest::SetAndEmphasizeText(const std::string& new_text) { | 190 void OmniboxViewViewsTest::SetAndEmphasizeText(const std::string& new_text, |
| 184 omnibox_view()->SetText(base::ASCIIToUTF16(new_text)); | 191 bool accept_input) { |
| 192 omnibox_view()->SetUserText(base::ASCIIToUTF16(new_text)); |
| 193 if (accept_input) { |
| 194 // We don't need to actually navigate in this case (and doing so in a test |
| 195 // would be difficult); it's sufficient to mark input as "no longer in |
| 196 // progress", and the edit model will assume the current text is a URL. |
| 197 omnibox_view()->model()->SetInputInProgress(false); |
| 198 } |
| 185 omnibox_view()->EmphasizeURLComponents(); | 199 omnibox_view()->EmphasizeURLComponents(); |
| 186 } | 200 } |
| 187 | 201 |
| 188 void OmniboxViewViewsTest::SetUp() { | 202 void OmniboxViewViewsTest::SetUp() { |
| 189 #if defined(OS_CHROMEOS) | 203 #if defined(OS_CHROMEOS) |
| 190 chromeos::input_method::InitializeForTesting( | 204 chromeos::input_method::InitializeForTesting( |
| 191 new chromeos::input_method::MockInputMethodManagerImpl); | 205 new chromeos::input_method::MockInputMethodManagerImpl); |
| 192 #endif | 206 #endif |
| 207 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( |
| 208 &profile_, &AutocompleteClassifierFactory::BuildInstanceFor); |
| 193 omnibox_view_ = base::MakeUnique<TestingOmniboxViewViews>( | 209 omnibox_view_ = base::MakeUnique<TestingOmniboxViewViews>( |
| 194 &omnibox_edit_controller_, | 210 &omnibox_edit_controller_, |
| 195 base::MakeUnique<ChromeOmniboxClient>(&omnibox_edit_controller_, | 211 base::MakeUnique<ChromeOmniboxClient>(&omnibox_edit_controller_, |
| 196 &profile_), | 212 &profile_), |
| 197 &command_updater_); | 213 &command_updater_); |
| 198 test_api_ = base::MakeUnique<views::TextfieldTestApi>(omnibox_view_.get()); | 214 test_api_ = base::MakeUnique<views::TextfieldTestApi>(omnibox_view_.get()); |
| 199 omnibox_view_->Init(); | 215 omnibox_view_->Init(); |
| 200 } | 216 } |
| 201 | 217 |
| 202 void OmniboxViewViewsTest::TearDown() { | 218 void OmniboxViewViewsTest::TearDown() { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 ui::TextEditCommand::MOVE_UP); | 255 ui::TextEditCommand::MOVE_UP); |
| 240 EXPECT_EQ(ui::TextEditCommand::MOVE_UP, scheduled_text_edit_command()); | 256 EXPECT_EQ(ui::TextEditCommand::MOVE_UP, scheduled_text_edit_command()); |
| 241 | 257 |
| 242 ui::KeyEvent up_pressed(ui::ET_KEY_PRESSED, ui::VKEY_UP, 0); | 258 ui::KeyEvent up_pressed(ui::ET_KEY_PRESSED, ui::VKEY_UP, 0); |
| 243 omnibox_textfield()->OnKeyEvent(&up_pressed); | 259 omnibox_textfield()->OnKeyEvent(&up_pressed); |
| 244 EXPECT_EQ(ui::TextEditCommand::INVALID_COMMAND, | 260 EXPECT_EQ(ui::TextEditCommand::INVALID_COMMAND, |
| 245 scheduled_text_edit_command()); | 261 scheduled_text_edit_command()); |
| 246 } | 262 } |
| 247 | 263 |
| 248 // Ensure that the scheme is emphasized for data: URLs. | 264 // Ensure that the scheme is emphasized for data: URLs. |
| 249 TEST_F(OmniboxViewViewsTest, TestEmphasisForDATA) { | 265 TEST_F(OmniboxViewViewsTest, TestEmphasisForData) { |
| 250 SetAndEmphasizeText("data:text/html,Hello%20World"); | 266 SetAndEmphasizeText("data:text/html,Hello%20World"); |
| 251 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->scheme_range()); | 267 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->scheme_range()); |
| 252 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized()); | 268 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized()); |
| 253 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->emphasis_range()); | 269 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->emphasis_range()); |
| 254 } | 270 } |
| 255 | 271 |
| 256 // Ensure that the origin is emphasized for http: URLs. | 272 // Ensure that the origin is emphasized for http: URLs. |
| 257 TEST_F(OmniboxViewViewsTest, TestEmphasisForHTTP) { | 273 TEST_F(OmniboxViewViewsTest, TestEmphasisForHTTP) { |
| 258 SetAndEmphasizeText("http://www.example.com/path/file.htm"); | 274 SetAndEmphasizeText("http://www.example.com/path/file.htm"); |
| 259 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->scheme_range()); | 275 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->scheme_range()); |
| 260 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized()); | 276 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized()); |
| 261 EXPECT_EQ(gfx::Range(7, 22), omnibox_view()->emphasis_range()); | 277 EXPECT_EQ(gfx::Range(7, 22), omnibox_view()->emphasis_range()); |
| 262 } | 278 } |
| 263 | 279 |
| 264 // Ensure that the origin is emphasized for https: URLs. | 280 // Ensure that the origin is emphasized for https: URLs. |
| 265 TEST_F(OmniboxViewViewsTest, TestEmphasisForHTTPS) { | 281 TEST_F(OmniboxViewViewsTest, TestEmphasisForHTTPS) { |
| 266 SetAndEmphasizeText("https://www.example.com/path/file.htm"); | 282 SetAndEmphasizeText("https://www.example.com/path/file.htm"); |
| 267 EXPECT_EQ(gfx::Range(0, 5), omnibox_view()->scheme_range()); | 283 EXPECT_EQ(gfx::Range(0, 5), omnibox_view()->scheme_range()); |
| 268 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized()); | 284 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized()); |
| 269 EXPECT_EQ(gfx::Range(8, 23), omnibox_view()->emphasis_range()); | 285 EXPECT_EQ(gfx::Range(8, 23), omnibox_view()->emphasis_range()); |
| 270 } | 286 } |
| 271 | 287 |
| 272 // Ensure that nothing is emphasized for chrome-extension: URLs. | 288 // Ensure that nothing is emphasized for chrome-extension: URLs. |
| 273 TEST_F(OmniboxViewViewsTest, TestEmphasisForChromeExtensionScheme) { | 289 TEST_F(OmniboxViewViewsTest, TestEmphasisForChromeExtension) { |
| 274 SetAndEmphasizeText("chrome-extension://ldfbacdbackkjhclmhnjabngnppnkagl"); | 290 SetAndEmphasizeText("chrome-extension://ldfbacdbackkjhclmhnjabngnppnkagl"); |
| 275 EXPECT_EQ(gfx::Range(0, 16), omnibox_view()->scheme_range()); | 291 EXPECT_EQ(gfx::Range(0, 16), omnibox_view()->scheme_range()); |
| 276 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized()); | 292 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized()); |
| 277 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range()); | 293 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range()); |
| 278 } | 294 } |
| 279 | 295 |
| 280 // Ensure that everything is emphasized for unknown scheme hierarchical URLs. | 296 // Ensure that everything is emphasized for unknown scheme hierarchical URLs. |
| 281 TEST_F(OmniboxViewViewsTest, TestEmphasisForUnknownHierarchicalScheme) { | 297 TEST_F(OmniboxViewViewsTest, TestEmphasisForUnknownHierarchicalScheme) { |
| 282 SetAndEmphasizeText("nosuchscheme://opaque/string"); | 298 SetAndEmphasizeText("nosuchscheme://opaque/string"); |
| 283 EXPECT_EQ(gfx::Range(0, 12), omnibox_view()->scheme_range()); | 299 EXPECT_EQ(gfx::Range(0, 12), omnibox_view()->scheme_range()); |
| 284 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized()); | 300 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized()); |
| 285 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range()); | 301 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range()); |
| 286 } | 302 } |
| 287 | 303 |
| 288 // Ensure that everything is emphasized for unknown scheme URLs. | 304 // Ensure that everything is emphasized for unknown scheme URLs. |
| 289 TEST_F(OmniboxViewViewsTest, TestEmphasisForUnknownScheme) { | 305 TEST_F(OmniboxViewViewsTest, TestEmphasisForUnknownScheme) { |
| 290 SetAndEmphasizeText("nosuchscheme:opaquestring"); | 306 SetAndEmphasizeText("nosuchscheme:opaquestring"); |
| 291 EXPECT_EQ(gfx::Range(0, 12), omnibox_view()->scheme_range()); | 307 EXPECT_EQ(gfx::Range(0, 12), omnibox_view()->scheme_range()); |
| 292 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized()); | 308 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized()); |
| 293 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range()); | 309 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range()); |
| 294 } | 310 } |
| 295 | 311 |
| 296 // Ensure that the origin is emphasized for URL-like text. | 312 // Ensure that the origin is emphasized for URL-like text. |
| 297 TEST_F(OmniboxViewViewsTest, TestEmphasisForPartialURLs) { | 313 TEST_F(OmniboxViewViewsTest, TestEmphasisForPartialURLs) { |
| 298 SetAndEmphasizeText("example/path/file"); | 314 SetAndEmphasizeText("host.com/path/file", false); |
| 299 EXPECT_EQ(gfx::Range(), omnibox_view()->scheme_range()); | 315 EXPECT_EQ(gfx::Range(), omnibox_view()->scheme_range()); |
| 300 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized()); | 316 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized()); |
| 301 EXPECT_EQ(gfx::Range(0, 7), omnibox_view()->emphasis_range()); | 317 EXPECT_EQ(gfx::Range(0, 8), omnibox_view()->emphasis_range()); |
| 302 } | 318 } |
| 303 | 319 |
| 304 // Ensure that everything is emphasized for plain text. | 320 // Ensure that everything is emphasized for plain text. |
| 305 TEST_F(OmniboxViewViewsTest, TestEmphasisForNonURLs) { | 321 TEST_F(OmniboxViewViewsTest, TestEmphasisForNonURLs) { |
| 306 SetAndEmphasizeText("This is plain text"); | 322 SetAndEmphasizeText("This is plain text", false); |
| 307 | 323 |
| 308 EXPECT_EQ(gfx::Range(), omnibox_view()->scheme_range()); | 324 EXPECT_EQ(gfx::Range(), omnibox_view()->scheme_range()); |
| 309 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized()); | 325 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized()); |
| 310 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range()); | 326 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range()); |
| 311 } | 327 } |
| OLD | NEW |