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 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "ui/events/keycodes/dom/dom_code.h" | 21 #include "ui/events/keycodes/dom/dom_code.h" |
| 22 #include "ui/views/controls/textfield/textfield_test_api.h" | 22 #include "ui/views/controls/textfield/textfield_test_api.h" |
| 23 | 23 |
| 24 #if defined(OS_CHROMEOS) | 24 #if defined(OS_CHROMEOS) |
| 25 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" | 25 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" |
| 26 #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" |
| 27 #endif | 27 #endif |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // TestingOmniboxViewViews ---------------------------------------------------- | |
| 32 | |
| 31 class TestingOmniboxViewViews : public OmniboxViewViews { | 33 class TestingOmniboxViewViews : public OmniboxViewViews { |
| 32 public: | 34 public: |
| 33 TestingOmniboxViewViews(OmniboxEditController* controller, | 35 TestingOmniboxViewViews(OmniboxEditController* controller, |
| 34 Profile* profile, | 36 Profile* profile, |
| 35 CommandUpdater* command_updater) | 37 CommandUpdater* command_updater); |
| 36 : OmniboxViewViews(controller, | |
| 37 profile, | |
| 38 command_updater, | |
| 39 false, | |
| 40 nullptr, | |
| 41 gfx::FontList()), | |
| 42 update_popup_call_count_(0), | |
| 43 profile_(profile), | |
| 44 base_text_is_emphasized_(false) {} | |
| 45 | 38 |
| 46 void CheckUpdatePopupCallInfo(size_t call_count, const base::string16& text, | 39 void CheckUpdatePopupCallInfo(size_t call_count, |
|
Mark P
2017/03/06 05:23:26
pref inline
Peter Kasting
2017/03/06 21:10:44
I'd like to avoid inlining methods where the body
| |
| 47 const gfx::Range& selection_range) { | 40 const base::string16& text, |
| 48 EXPECT_EQ(call_count, update_popup_call_count_); | 41 const gfx::Range& selection_range); |
| 49 EXPECT_EQ(text, update_popup_text_); | |
| 50 EXPECT_EQ(selection_range, update_popup_selection_range_); | |
| 51 } | |
| 52 | |
| 53 void EmphasizeURLComponents() override { | |
| 54 UpdateTextStyle(text(), ChromeAutocompleteSchemeClassifier(profile_)); | |
| 55 } | |
| 56 | 42 |
| 57 gfx::Range scheme_range() const { return scheme_range_; } | 43 gfx::Range scheme_range() const { return scheme_range_; } |
| 58 gfx::Range emphasis_range() const { return emphasis_range_; } | 44 gfx::Range emphasis_range() const { return emphasis_range_; } |
| 59 bool base_text_is_emphasized() const { return base_text_is_emphasized_; } | 45 bool base_text_is_emphasized() const { return base_text_is_emphasized_; } |
| 60 | 46 |
| 47 // OmniboxViewViews: | |
| 48 void EmphasizeURLComponents() override; | |
|
Mark P
2017/03/06 05:23:26
pref inline
Peter Kasting
2017/03/06 21:10:44
I think this one makes sense, and the only reason
Mark P
2017/03/07 21:10:18
Can you give it a better name or comment then?
I w
Peter Kasting
2017/03/08 10:57:31
Since this is a virtual override, the names are se
| |
| 49 | |
| 61 private: | 50 private: |
| 62 // OmniboxView: | 51 // OmniboxViewViews: |
| 63 void UpdatePopup() override { | 52 void UpdatePopup() override; |
|
Mark P
2017/03/06 05:23:26
pref inline
Mark P
2017/03/07 21:10:18
The behavior is not what I'd expect an UpdatePopup
Peter Kasting
2017/03/08 10:57:31
Done.
| |
| 64 ++update_popup_call_count_; | 53 void SetEmphasis(bool emphasize, const gfx::Range& range) override; |
| 65 update_popup_text_ = text(); | 54 void UpdateSchemeStyle(const gfx::Range& range) override; |
| 66 update_popup_selection_range_ = GetSelectedRange(); | |
| 67 } | |
| 68 | |
| 69 void SetEmphasis(bool emphasize, const gfx::Range& range) override { | |
| 70 if (range == gfx::Range::InvalidRange()) { | |
| 71 base_text_is_emphasized_ = emphasize; | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 EXPECT_TRUE(emphasize); | |
| 76 emphasis_range_ = range; | |
| 77 } | |
| 78 | |
| 79 void UpdateSchemeStyle(const gfx::Range& range) override { | |
| 80 scheme_range_ = range; | |
| 81 } | |
| 82 | 55 |
| 83 // Simplistic test override returns whether a given string looks like a URL | 56 // Simplistic test override returns whether a given string looks like a URL |
| 84 // without having to mock AutocompleteClassifier objects and their | 57 // without having to mock AutocompleteClassifier objects and their |
| 85 // dependencies. | 58 // dependencies. |
| 86 bool CurrentTextIsURL() override { | 59 bool CurrentTextIsURL() override { |
| 87 bool looks_like_url = (text().find(':') != std::string::npos || | 60 bool looks_like_url = (text().find(':') != std::string::npos || |
| 88 text().find('/') != std::string::npos); | 61 text().find('/') != std::string::npos); |
| 89 return looks_like_url; | 62 return looks_like_url; |
| 90 } | 63 } |
| 91 | 64 |
| 92 size_t update_popup_call_count_; | 65 size_t update_popup_call_count_; |
| 93 base::string16 update_popup_text_; | 66 base::string16 update_popup_text_; |
| 94 gfx::Range update_popup_selection_range_; | 67 gfx::Range update_popup_selection_range_; |
| 95 Profile* profile_; | 68 Profile* profile_; |
|
Mark P
2017/03/06 05:23:27
nit: prefer blank line above here, to separate tes
Peter Kasting
2017/03/06 21:10:44
Ignoring this because I'm about to rip this variab
Mark P
2017/03/07 21:10:18
Acknowledged.
| |
| 96 | 69 |
| 97 // Range of the last scheme logged by UpdateSchemeStyle(). | 70 // Range of the last scheme logged by UpdateSchemeStyle(). |
| 98 gfx::Range scheme_range_; | 71 gfx::Range scheme_range_; |
| 99 | 72 |
| 100 // Range of the last text emphasized by SetEmphasis(). | 73 // Range of the last text emphasized by SetEmphasis(). |
| 101 gfx::Range emphasis_range_; | 74 gfx::Range emphasis_range_; |
| 102 | 75 |
| 103 // SetEmphasis() logs whether the base color of the text is emphasized. | 76 // SetEmphasis() logs whether the base color of the text is emphasized. |
| 104 bool base_text_is_emphasized_; | 77 bool base_text_is_emphasized_; |
| 105 | 78 |
| 106 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxViewViews); | 79 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxViewViews); |
| 107 }; | 80 }; |
| 108 | 81 |
| 82 TestingOmniboxViewViews::TestingOmniboxViewViews( | |
| 83 OmniboxEditController* controller, | |
| 84 Profile* profile, | |
| 85 CommandUpdater* command_updater) | |
| 86 : OmniboxViewViews(controller, | |
| 87 profile, | |
| 88 command_updater, | |
| 89 false, | |
| 90 nullptr, | |
| 91 gfx::FontList()), | |
| 92 update_popup_call_count_(0), | |
| 93 profile_(profile), | |
| 94 base_text_is_emphasized_(false) {} | |
| 95 | |
| 96 void TestingOmniboxViewViews::CheckUpdatePopupCallInfo( | |
| 97 size_t call_count, | |
| 98 const base::string16& text, | |
| 99 const gfx::Range& selection_range) { | |
| 100 EXPECT_EQ(call_count, update_popup_call_count_); | |
| 101 EXPECT_EQ(text, update_popup_text_); | |
| 102 EXPECT_EQ(selection_range, update_popup_selection_range_); | |
| 103 } | |
| 104 | |
| 105 void TestingOmniboxViewViews::EmphasizeURLComponents() { | |
| 106 UpdateTextStyle(text(), ChromeAutocompleteSchemeClassifier(profile_)); | |
| 107 } | |
| 108 | |
| 109 void TestingOmniboxViewViews::UpdatePopup() { | |
| 110 ++update_popup_call_count_; | |
| 111 update_popup_text_ = text(); | |
| 112 update_popup_selection_range_ = GetSelectedRange(); | |
| 113 } | |
| 114 | |
| 115 void TestingOmniboxViewViews::SetEmphasis(bool emphasize, | |
| 116 const gfx::Range& range) { | |
| 117 if (range == gfx::Range::InvalidRange()) { | |
| 118 base_text_is_emphasized_ = emphasize; | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 EXPECT_TRUE(emphasize); | |
| 123 emphasis_range_ = range; | |
| 124 } | |
| 125 | |
| 126 void TestingOmniboxViewViews::UpdateSchemeStyle(const gfx::Range& range) { | |
| 127 scheme_range_ = range; | |
| 128 } | |
| 129 | |
| 130 // TestingOmniboxEditController ----------------------------------------------- | |
| 131 | |
| 109 class TestingOmniboxEditController : public ChromeOmniboxEditController { | 132 class TestingOmniboxEditController : public ChromeOmniboxEditController { |
| 110 public: | 133 public: |
| 111 explicit TestingOmniboxEditController(CommandUpdater* command_updater) | 134 explicit TestingOmniboxEditController(CommandUpdater* command_updater) |
| 112 : ChromeOmniboxEditController(command_updater) {} | 135 : ChromeOmniboxEditController(command_updater) {} |
| 113 | 136 |
| 114 protected: | 137 private: |
| 115 // ChromeOmniboxEditController: | 138 // ChromeOmniboxEditController: |
| 116 void UpdateWithoutTabRestore() override {} | 139 void UpdateWithoutTabRestore() override {} |
| 117 void OnChanged() override {} | 140 void OnChanged() override {} |
| 118 ToolbarModel* GetToolbarModel() override { return nullptr; } | 141 ToolbarModel* GetToolbarModel() override { return nullptr; } |
| 119 const ToolbarModel* GetToolbarModel() const override { return nullptr; } | 142 const ToolbarModel* GetToolbarModel() const override { return nullptr; } |
| 120 content::WebContents* GetWebContents() override { return nullptr; } | 143 content::WebContents* GetWebContents() override { return nullptr; } |
| 121 | 144 |
| 122 private: | |
| 123 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController); | 145 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController); |
| 124 }; | 146 }; |
| 125 | 147 |
| 126 } // namespace | 148 } // namespace |
| 127 | 149 |
| 150 // OmniboxViewViewsTest ------------------------------------------------------- | |
| 151 | |
| 128 class OmniboxViewViewsTest : public testing::Test { | 152 class OmniboxViewViewsTest : public testing::Test { |
| 129 public: | 153 public: |
| 130 OmniboxViewViewsTest() | 154 OmniboxViewViewsTest(); |
| 131 : command_updater_(NULL), | |
| 132 omnibox_edit_controller_(&command_updater_) { | |
| 133 } | |
| 134 | 155 |
| 135 TestingOmniboxViewViews* omnibox_view() { | 156 TestingOmniboxViewViews* omnibox_view() { return omnibox_view_.get(); } |
| 136 return omnibox_view_.get(); | 157 views::Textfield* omnibox_textfield() { return omnibox_view(); } |
| 137 } | |
| 138 | |
| 139 views::Textfield* omnibox_textfield() { | |
| 140 return omnibox_view(); | |
| 141 } | |
| 142 | |
| 143 ui::TextEditCommand scheduled_text_edit_command() const { | 158 ui::TextEditCommand scheduled_text_edit_command() const { |
| 144 return test_api_->scheduled_text_edit_command(); | 159 return test_api_->scheduled_text_edit_command(); |
| 145 } | 160 } |
| 146 | 161 |
| 147 void SetAndEmphasizeText(const std::string& new_text) { | 162 // Sets |new_text| as the text in the omnibox. |
|
Mark P
2017/03/06 05:23:26
nit: ... and emphasizes it appropriately.
Peter Kasting
2017/03/06 21:10:44
Done.
| |
| 148 omnibox_textfield()->SetText(base::ASCIIToUTF16(new_text)); | 163 void SetAndEmphasizeText(const std::string& new_text); |
| 149 omnibox_view()->EmphasizeURLComponents(); | |
| 150 } | |
| 151 | 164 |
| 152 private: | 165 private: |
| 153 // testing::Test: | 166 // testing::Test: |
| 154 void SetUp() override { | 167 void SetUp() override; |
| 155 #if defined(OS_CHROMEOS) | 168 void TearDown() override; |
| 156 chromeos::input_method::InitializeForTesting( | |
| 157 new chromeos::input_method::MockInputMethodManagerImpl); | |
| 158 #endif | |
| 159 omnibox_view_.reset(new TestingOmniboxViewViews( | |
| 160 &omnibox_edit_controller_, &profile_, &command_updater_)); | |
| 161 test_api_.reset(new views::TextfieldTestApi(omnibox_view_.get())); | |
| 162 omnibox_view_->Init(); | |
| 163 } | |
| 164 | |
| 165 void TearDown() override { | |
| 166 omnibox_view_.reset(); | |
| 167 #if defined(OS_CHROMEOS) | |
| 168 chromeos::input_method::Shutdown(); | |
| 169 #endif | |
| 170 } | |
| 171 | 169 |
| 172 content::TestBrowserThreadBundle thread_bundle_; | 170 content::TestBrowserThreadBundle thread_bundle_; |
| 173 TestingProfile profile_; | 171 TestingProfile profile_; |
| 174 CommandUpdater command_updater_; | 172 CommandUpdater command_updater_; |
| 175 TestingOmniboxEditController omnibox_edit_controller_; | 173 TestingOmniboxEditController omnibox_edit_controller_; |
| 176 std::unique_ptr<TestingOmniboxViewViews> omnibox_view_; | 174 std::unique_ptr<TestingOmniboxViewViews> omnibox_view_; |
| 177 std::unique_ptr<views::TextfieldTestApi> test_api_; | 175 std::unique_ptr<views::TextfieldTestApi> test_api_; |
| 178 | 176 |
| 179 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); | 177 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); |
| 180 }; | 178 }; |
| 181 | 179 |
| 180 OmniboxViewViewsTest::OmniboxViewViewsTest() | |
| 181 : command_updater_(nullptr), omnibox_edit_controller_(&command_updater_) {} | |
| 182 | |
| 183 void OmniboxViewViewsTest::SetAndEmphasizeText(const std::string& new_text) { | |
| 184 omnibox_view()->SetText(base::ASCIIToUTF16(new_text)); | |
| 185 omnibox_view()->EmphasizeURLComponents(); | |
| 186 } | |
| 187 | |
| 188 void OmniboxViewViewsTest::SetUp() { | |
| 189 #if defined(OS_CHROMEOS) | |
| 190 chromeos::input_method::InitializeForTesting( | |
| 191 new chromeos::input_method::MockInputMethodManagerImpl); | |
| 192 #endif | |
| 193 omnibox_view_ = base::MakeUnique<TestingOmniboxViewViews>( | |
| 194 &omnibox_edit_controller_, &profile_, &command_updater_); | |
| 195 test_api_ = base::MakeUnique<views::TextfieldTestApi>(omnibox_view_.get()); | |
| 196 omnibox_view_->Init(); | |
| 197 } | |
| 198 | |
| 199 void OmniboxViewViewsTest::TearDown() { | |
| 200 omnibox_view_.reset(); | |
| 201 #if defined(OS_CHROMEOS) | |
| 202 chromeos::input_method::Shutdown(); | |
| 203 #endif | |
| 204 } | |
| 205 | |
| 206 // Actual tests --------------------------------------------------------------- | |
| 207 | |
| 182 // Checks that a single change of the text in the omnibox invokes | 208 // Checks that a single change of the text in the omnibox invokes |
| 183 // only one call to OmniboxViewViews::UpdatePopup(). | 209 // only one call to OmniboxViewViews::UpdatePopup(). |
| 184 TEST_F(OmniboxViewViewsTest, UpdatePopupCall) { | 210 TEST_F(OmniboxViewViewsTest, UpdatePopupCall) { |
| 185 ui::KeyEvent char_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::DomCode::US_A, 0, | 211 ui::KeyEvent char_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::DomCode::US_A, 0, |
| 186 ui::DomKey::FromCharacter('a'), | 212 ui::DomKey::FromCharacter('a'), |
| 187 ui::EventTimeForNow()); | 213 ui::EventTimeForNow()); |
| 188 omnibox_textfield()->InsertChar(char_event); | 214 omnibox_textfield()->InsertChar(char_event); |
| 189 omnibox_view()->CheckUpdatePopupCallInfo( | 215 omnibox_view()->CheckUpdatePopupCallInfo( |
| 190 1, base::ASCIIToUTF16("a"), gfx::Range(1)); | 216 1, base::ASCIIToUTF16("a"), gfx::Range(1)); |
| 191 | 217 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 } | 299 } |
| 274 | 300 |
| 275 // Ensure that everything is emphasized for plain text. | 301 // Ensure that everything is emphasized for plain text. |
| 276 TEST_F(OmniboxViewViewsTest, TestEmphasisForNonURLs) { | 302 TEST_F(OmniboxViewViewsTest, TestEmphasisForNonURLs) { |
| 277 SetAndEmphasizeText("This is plain text"); | 303 SetAndEmphasizeText("This is plain text"); |
| 278 | 304 |
| 279 EXPECT_EQ(gfx::Range(), omnibox_view()->scheme_range()); | 305 EXPECT_EQ(gfx::Range(), omnibox_view()->scheme_range()); |
| 280 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized()); | 306 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized()); |
| 281 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range()); | 307 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range()); |
| 282 } | 308 } |
| OLD | NEW |