Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" | 5 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | |
| 9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/test/scoped_feature_list.h" | |
| 11 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | 13 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
| 14 #include "chrome/browser/ui/cocoa/l10n_util.h" | |
| 12 #include "chrome/browser/ui/omnibox/chrome_omnibox_client.h" | 15 #include "chrome/browser/ui/omnibox/chrome_omnibox_client.h" |
| 13 #include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h" | 16 #include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h" |
| 14 #include "chrome/browser/ui/toolbar/chrome_toolbar_model_delegate.h" | 17 #include "chrome/browser/ui/toolbar/chrome_toolbar_model_delegate.h" |
| 15 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/omnibox/browser/omnibox_popup_model.h" | 19 #include "components/omnibox/browser/omnibox_popup_model.h" |
| 17 #include "components/omnibox/browser/omnibox_popup_view.h" | 20 #include "components/omnibox/browser/omnibox_popup_view.h" |
| 18 #include "components/toolbar/toolbar_model_impl.h" | 21 #include "components/toolbar/toolbar_model_impl.h" |
| 19 #include "content/public/common/content_constants.h" | 22 #include "content/public/common/content_constants.h" |
| 20 #include "testing/platform_test.h" | 23 #include "testing/platform_test.h" |
| 21 #include "ui/gfx/font.h" | 24 #include "ui/gfx/font.h" |
| 22 #include "ui/gfx/geometry/rect.h" | 25 #include "ui/gfx/geometry/rect.h" |
| 23 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 24 | 27 |
| 25 namespace { | 28 namespace { |
| 26 | 29 |
| 27 class MockOmniboxEditModel : public OmniboxEditModel { | 30 class MockOmniboxEditModel : public OmniboxEditModel { |
| 28 public: | 31 public: |
| 29 MockOmniboxEditModel(OmniboxView* view, | 32 MockOmniboxEditModel(OmniboxView* view, |
| 30 OmniboxEditController* controller, | 33 OmniboxEditController* controller, |
| 31 Profile* profile) | 34 Profile* profile) |
| 32 : OmniboxEditModel( | 35 : OmniboxEditModel( |
| 33 view, | 36 view, |
| 34 controller, | 37 controller, |
| 35 base::WrapUnique(new ChromeOmniboxClient(controller, profile))), | 38 base::WrapUnique(new ChromeOmniboxClient(controller, profile))), |
| 36 up_or_down_count_(0) {} | 39 up_or_down_count_(0) {} |
|
Mark Mentovai
2016/12/12 17:45:24
, currrent_text_is_url_(false)
lgrey
2016/12/12 18:13:33
Done.
| |
| 37 | 40 |
| 38 void OnUpOrDownKeyPressed(int count) override { up_or_down_count_ = count; } | 41 void OnUpOrDownKeyPressed(int count) override { up_or_down_count_ = count; } |
| 39 | 42 |
| 43 bool CurrentTextIsURL() const override { return current_text_is_url_; } | |
| 44 | |
| 40 int up_or_down_count() const { return up_or_down_count_; } | 45 int up_or_down_count() const { return up_or_down_count_; } |
| 41 | 46 |
| 42 void set_up_or_down_count(int count) { | 47 void set_up_or_down_count(int count) { |
| 43 up_or_down_count_ = count; | 48 up_or_down_count_ = count; |
| 44 } | 49 } |
| 45 | 50 |
| 51 void set_current_text_is_url(bool is_url) { current_text_is_url_ = is_url; } | |
| 52 | |
| 46 private: | 53 private: |
| 47 int up_or_down_count_; | 54 int up_or_down_count_; |
| 55 bool current_text_is_url_; | |
| 48 | 56 |
| 49 DISALLOW_COPY_AND_ASSIGN(MockOmniboxEditModel); | 57 DISALLOW_COPY_AND_ASSIGN(MockOmniboxEditModel); |
| 50 }; | 58 }; |
| 51 | 59 |
| 52 class MockOmniboxPopupView : public OmniboxPopupView { | 60 class MockOmniboxPopupView : public OmniboxPopupView { |
| 53 public: | 61 public: |
| 54 MockOmniboxPopupView() : is_open_(false) {} | 62 MockOmniboxPopupView() : is_open_(false) {} |
| 55 ~MockOmniboxPopupView() override {} | 63 ~MockOmniboxPopupView() override {} |
| 56 | 64 |
| 57 // Overridden from OmniboxPopupView: | 65 // Overridden from OmniboxPopupView: |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 | 177 |
| 170 // With popup open verify that pressing up and down arrow works. | 178 // With popup open verify that pressing up and down arrow works. |
| 171 popup_view.set_is_open(true); | 179 popup_view.set_is_open(true); |
| 172 model->set_up_or_down_count(0); | 180 model->set_up_or_down_count(0); |
| 173 view.OnDoCommandBySelector(@selector(moveDown:)); | 181 view.OnDoCommandBySelector(@selector(moveDown:)); |
| 174 EXPECT_EQ(1, model->up_or_down_count()); | 182 EXPECT_EQ(1, model->up_or_down_count()); |
| 175 model->set_up_or_down_count(0); | 183 model->set_up_or_down_count(0); |
| 176 view.OnDoCommandBySelector(@selector(moveUp:)); | 184 view.OnDoCommandBySelector(@selector(moveUp:)); |
| 177 EXPECT_EQ(-1, model->up_or_down_count()); | 185 EXPECT_EQ(-1, model->up_or_down_count()); |
| 178 } | 186 } |
| 187 | |
| 188 TEST_F(OmniboxViewMacTest, WritingDirectionLTR) { | |
| 189 OmniboxViewMac view(NULL, profile(), NULL, NULL); | |
| 190 | |
| 191 // This is deleted by the omnibox view. | |
| 192 MockOmniboxEditModel* model = | |
| 193 new MockOmniboxEditModel(&view, NULL, profile()); | |
| 194 model->set_current_text_is_url(true); | |
| 195 SetModel(&view, model); | |
| 196 base::scoped_nsobject<NSMutableAttributedString> string( | |
| 197 [[NSMutableAttributedString alloc] initWithString:@"Foo"]); | |
| 198 view.ApplyTextStyle(string); | |
| 199 | |
| 200 NSParagraphStyle* paragraphStyle = | |
| 201 [string attribute:NSParagraphStyleAttributeName | |
| 202 atIndex:0 | |
| 203 effectiveRange:NULL]; | |
| 204 DCHECK(paragraphStyle); | |
| 205 EXPECT_EQ(paragraphStyle.alignment, NSLeftTextAlignment); | |
| 206 model->set_current_text_is_url(true); | |
| 207 EXPECT_EQ(paragraphStyle.baseWritingDirection, NSWritingDirectionLeftToRight); | |
| 208 } | |
| 209 | |
| 210 TEST_F(OmniboxViewMacTest, WritingDirectionRTL) { | |
| 211 // TODO(lgrey): Replace setup with scoper when crrev.com/2555033003 lands. | |
| 212 std::string old_locale(base::i18n::GetConfiguredLocale()); | |
| 213 base::i18n::SetICUDefaultLocale("he"); // Hebrew | |
| 214 base::test::ScopedFeatureList scoped_feature_list; | |
| 215 scoped_feature_list.InitAndEnableFeature( | |
| 216 cocoa_l10n_util::kExperimentalMacRTL); | |
| 217 NSString* const appleTextDirectionDefaultsKey = @"AppleTextDirection"; | |
| 218 NSString* const forceRTLWritingDirectionDefaultsKey = | |
| 219 @"NSForceRightToLeftWritingDirection"; | |
| 220 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | |
| 221 BOOL oldTextDirection = [defaults boolForKey:appleTextDirectionDefaultsKey]; | |
| 222 BOOL oldRTLWritingDirection = | |
| 223 [defaults boolForKey:forceRTLWritingDirectionDefaultsKey]; | |
| 224 [defaults setBool:YES forKey:appleTextDirectionDefaultsKey]; | |
| 225 [defaults setBool:YES forKey:forceRTLWritingDirectionDefaultsKey]; | |
| 226 | |
| 227 OmniboxViewMac view(NULL, profile(), NULL, NULL); | |
| 228 | |
| 229 // This is deleted by the omnibox view. | |
| 230 MockOmniboxEditModel* model = | |
| 231 new MockOmniboxEditModel(&view, NULL, profile()); | |
| 232 SetModel(&view, model); | |
| 233 model->set_current_text_is_url(true); | |
| 234 base::scoped_nsobject<NSMutableAttributedString> string( | |
| 235 [[NSMutableAttributedString alloc] initWithString:@"Foo"]); | |
| 236 view.ApplyTextStyle(string); | |
| 237 | |
| 238 NSParagraphStyle* paragraphStyle = | |
| 239 [string attribute:NSParagraphStyleAttributeName | |
| 240 atIndex:0 | |
| 241 effectiveRange:NULL]; | |
| 242 DCHECK(paragraphStyle); | |
| 243 EXPECT_EQ(paragraphStyle.alignment, NSRightTextAlignment); | |
| 244 EXPECT_EQ(paragraphStyle.baseWritingDirection, NSWritingDirectionLeftToRight); | |
| 245 | |
| 246 base::i18n::SetICUDefaultLocale(old_locale); | |
| 247 [defaults setBool:oldTextDirection forKey:appleTextDirectionDefaultsKey]; | |
| 248 [defaults setBool:oldRTLWritingDirection | |
| 249 forKey:forceRTLWritingDirectionDefaultsKey]; | |
| 250 } | |
| OLD | NEW |