Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: chrome/browser/ui/views/omnibox/omnibox_view_views_unittest.cc

Issue 2641003002: Show scheme in black and content in gray for data: protocol urls (Closed)
Patch Set: Only call UpdateSchemeStyle when a scheme is present. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 nullptr,
36 gfx::FontList()), 41 gfx::FontList()),
37 update_popup_call_count_(0) { 42 update_popup_call_count_(0),
38 } 43 profile_(profile),
44 base_text_is_emphasized_(false) {}
39 45
40 void CheckUpdatePopupCallInfo(size_t call_count, const base::string16& text, 46 void CheckUpdatePopupCallInfo(size_t call_count, const base::string16& text,
41 const gfx::Range& selection_range) { 47 const gfx::Range& selection_range) {
42 EXPECT_EQ(call_count, update_popup_call_count_); 48 EXPECT_EQ(call_count, update_popup_call_count_);
43 EXPECT_EQ(text, update_popup_text_); 49 EXPECT_EQ(text, update_popup_text_);
44 EXPECT_EQ(selection_range, update_popup_selection_range_); 50 EXPECT_EQ(selection_range, update_popup_selection_range_);
45 } 51 }
46 52
53 void EmphasizeURLComponents() override {
54 UpdateTextStyle(text(), ChromeAutocompleteSchemeClassifier(profile_));
55 }
56
57 gfx::Range scheme_range() const { return scheme_range_; }
58 gfx::Range emphasis_range() const { return emphasis_range_; }
59 bool base_text_is_emphasized() const { return base_text_is_emphasized_; }
60
47 private: 61 private:
48 // OmniboxView: 62 // OmniboxView:
49 void UpdatePopup() override { 63 void UpdatePopup() override {
50 ++update_popup_call_count_; 64 ++update_popup_call_count_;
51 update_popup_text_ = text(); 65 update_popup_text_ = text();
52 update_popup_selection_range_ = GetSelectedRange(); 66 update_popup_selection_range_ = GetSelectedRange();
53 } 67 }
54 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
83 // Simplistic test override returns whether a given string looks like a URL
84 // without having to mock AutocompleteClassifier objects and their
85 // dependencies.
86 bool CurrentTextIsURL() override {
87 bool looks_like_url = (text().find(':') != std::string::npos ||
88 text().find('/') != std::string::npos);
89 return looks_like_url;
90 }
91
55 size_t update_popup_call_count_; 92 size_t update_popup_call_count_;
56 base::string16 update_popup_text_; 93 base::string16 update_popup_text_;
57 gfx::Range update_popup_selection_range_; 94 gfx::Range update_popup_selection_range_;
95 Profile* profile_;
96
97 // Range of the last scheme logged by |UpdateSchemeStyle()|.
Peter Kasting 2017/03/03 02:31:08 Nit: No || around function names (3 places)
elawrence 2017/03/03 16:07:42 Done.
98 gfx::Range scheme_range_;
99
100 // Range of the last text emphasized by |SetEmphasis()|.
101 gfx::Range emphasis_range_;
102
103 // |SetEmphasis()| logs whether the base color of the text is emphasized.
104 bool base_text_is_emphasized_;
58 105
59 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxViewViews); 106 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxViewViews);
60 }; 107 };
61 108
62 class TestingOmniboxEditController : public ChromeOmniboxEditController { 109 class TestingOmniboxEditController : public ChromeOmniboxEditController {
63 public: 110 public:
64 explicit TestingOmniboxEditController(CommandUpdater* command_updater) 111 explicit TestingOmniboxEditController(CommandUpdater* command_updater)
65 : ChromeOmniboxEditController(command_updater) {} 112 : ChromeOmniboxEditController(command_updater) {}
66 113
67 protected: 114 protected:
(...skipping 22 matching lines...) Expand all
90 } 137 }
91 138
92 views::Textfield* omnibox_textfield() { 139 views::Textfield* omnibox_textfield() {
93 return omnibox_view(); 140 return omnibox_view();
94 } 141 }
95 142
96 ui::TextEditCommand scheduled_text_edit_command() const { 143 ui::TextEditCommand scheduled_text_edit_command() const {
97 return test_api_->scheduled_text_edit_command(); 144 return test_api_->scheduled_text_edit_command();
98 } 145 }
99 146
147 void SetAndEmphasizeText(const std::string& newText) {
Peter Kasting 2017/03/03 09:17:14 Nit: newText -> new_text
elawrence 2017/03/03 16:07:42 Done.
148 omnibox_textfield()->SetText(base::ASCIIToUTF16(newText));
149 omnibox_view()->EmphasizeURLComponents();
150 }
151
100 private: 152 private:
101 // testing::Test: 153 // testing::Test:
102 void SetUp() override { 154 void SetUp() override {
103 #if defined(OS_CHROMEOS) 155 #if defined(OS_CHROMEOS)
104 chromeos::input_method::InitializeForTesting( 156 chromeos::input_method::InitializeForTesting(
105 new chromeos::input_method::MockInputMethodManagerImpl); 157 new chromeos::input_method::MockInputMethodManagerImpl);
106 #endif 158 #endif
107 omnibox_view_.reset(new TestingOmniboxViewViews( 159 omnibox_view_.reset(new TestingOmniboxViewViews(
108 &omnibox_edit_controller_, &profile_, &command_updater_)); 160 &omnibox_edit_controller_, &profile_, &command_updater_));
109 test_api_.reset(new views::TextfieldTestApi(omnibox_view_.get())); 161 test_api_.reset(new views::TextfieldTestApi(omnibox_view_.get()));
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 TEST_F(OmniboxViewViewsTest, ScheduledTextEditCommand) { 208 TEST_F(OmniboxViewViewsTest, ScheduledTextEditCommand) {
157 omnibox_textfield()->SetTextEditCommandForNextKeyEvent( 209 omnibox_textfield()->SetTextEditCommandForNextKeyEvent(
158 ui::TextEditCommand::MOVE_UP); 210 ui::TextEditCommand::MOVE_UP);
159 EXPECT_EQ(ui::TextEditCommand::MOVE_UP, scheduled_text_edit_command()); 211 EXPECT_EQ(ui::TextEditCommand::MOVE_UP, scheduled_text_edit_command());
160 212
161 ui::KeyEvent up_pressed(ui::ET_KEY_PRESSED, ui::VKEY_UP, 0); 213 ui::KeyEvent up_pressed(ui::ET_KEY_PRESSED, ui::VKEY_UP, 0);
162 omnibox_textfield()->OnKeyEvent(&up_pressed); 214 omnibox_textfield()->OnKeyEvent(&up_pressed);
163 EXPECT_EQ(ui::TextEditCommand::INVALID_COMMAND, 215 EXPECT_EQ(ui::TextEditCommand::INVALID_COMMAND,
164 scheduled_text_edit_command()); 216 scheduled_text_edit_command());
165 } 217 }
218
219 // Ensure that the scheme is emphasized for data: URLs.
220 TEST_F(OmniboxViewViewsTest, TestEmphasisForDATA) {
221 SetAndEmphasizeText("data:text/html,Hello%20World");
222 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->scheme_range());
223 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized());
224 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->emphasis_range());
225 }
226
227 // Ensure that the origin is emphasized for http: URLs.
228 TEST_F(OmniboxViewViewsTest, TestEmphasisForHTTP) {
229 SetAndEmphasizeText("http://www.example.com/path/file.htm");
230 EXPECT_EQ(gfx::Range(0, 4), omnibox_view()->scheme_range());
231 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized());
232 EXPECT_EQ(gfx::Range(7, 22), omnibox_view()->emphasis_range());
233 }
234
235 // Ensure that the origin is emphasized for https: URLs.
236 TEST_F(OmniboxViewViewsTest, TestEmphasisForHTTPS) {
237 SetAndEmphasizeText("https://www.example.com/path/file.htm");
238 EXPECT_EQ(gfx::Range(0, 5), omnibox_view()->scheme_range());
239 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized());
240 EXPECT_EQ(gfx::Range(8, 23), omnibox_view()->emphasis_range());
241 }
242
243 // Ensure that nothing is emphasized for chrome-extension: URLs.
244 TEST_F(OmniboxViewViewsTest, TestEmphasisForChromeExtensionScheme) {
245 SetAndEmphasizeText("chrome-extension://ldfbacdbackkjhclmhnjabngnppnkagl");
246 EXPECT_EQ(gfx::Range(0, 16), omnibox_view()->scheme_range());
247 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized());
248 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range());
249 }
250
251 // Ensure that everything is emphasized for unknown scheme hierarchical URLs.
252 TEST_F(OmniboxViewViewsTest, TestEmphasisForUnknownHierarchicalScheme) {
253 SetAndEmphasizeText("nosuchscheme://opaque/string");
254 EXPECT_EQ(gfx::Range(0, 12), omnibox_view()->scheme_range());
255 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized());
256 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range());
257 }
258
259 // Ensure that everything is emphasized for unknown scheme URLs.
260 TEST_F(OmniboxViewViewsTest, TestEmphasisForUnknownScheme) {
261 SetAndEmphasizeText("nosuchscheme:opaquestring");
262 EXPECT_EQ(gfx::Range(0, 12), omnibox_view()->scheme_range());
263 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized());
264 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range());
265 }
266
267 // Ensure that the origin is emphasized for URL-like text.
268 TEST_F(OmniboxViewViewsTest, TestEmphasisForPartialURLs) {
269 SetAndEmphasizeText("example/path/file");
270 EXPECT_EQ(gfx::Range(), omnibox_view()->scheme_range());
271 EXPECT_FALSE(omnibox_view()->base_text_is_emphasized());
272 EXPECT_EQ(gfx::Range(0, 7), omnibox_view()->emphasis_range());
273 }
274
275 // Ensure that everything is emphasized for plain text.
276 TEST_F(OmniboxViewViewsTest, TestEmphasisForNonURLs) {
277 SetAndEmphasizeText("This is plain text");
278
279 EXPECT_EQ(gfx::Range(), omnibox_view()->scheme_range());
280 EXPECT_TRUE(omnibox_view()->base_text_is_emphasized());
281 EXPECT_EQ(gfx::Range(), omnibox_view()->emphasis_range());
282 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698