| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "base/memory/scoped_nsobject.h" | 7 #include "base/memory/scoped_nsobject.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" | 10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" |
| 11 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" | 11 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" |
| 12 #import "chrome/browser/ui/cocoa/location_bar/ev_bubble_decoration.h" | 12 #import "chrome/browser/ui/cocoa/location_bar/ev_bubble_decoration.h" |
| 13 #import "chrome/browser/ui/cocoa/location_bar/keyword_hint_decoration.h" | 13 #import "chrome/browser/ui/cocoa/location_bar/keyword_hint_decoration.h" |
| 14 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h" | 14 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h" |
| 15 #import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h" | 15 #import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h" |
| 16 #import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h" | 16 #import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h" |
| 17 #import "chrome/browser/ui/cocoa/location_bar/separator_decoration.h" |
| 17 #import "chrome/browser/ui/cocoa/location_bar/star_decoration.h" | 18 #import "chrome/browser/ui/cocoa/location_bar/star_decoration.h" |
| 18 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
| 22 #include "third_party/ocmock/gtest_support.h" | 23 #include "third_party/ocmock/gtest_support.h" |
| 23 #import "third_party/ocmock/OCMock/OCMock.h" | 24 #import "third_party/ocmock/OCMock/OCMock.h" |
| 24 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
| 25 | 26 |
| 26 using ::testing::Return; | 27 using ::testing::Return; |
| 27 using ::testing::StrictMock; | 28 using ::testing::StrictMock; |
| 28 using ::testing::_; | 29 using ::testing::_; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // Width of the field so that we don't have to ask |field_| for it all | 33 // Width of the field so that we don't have to ask |field_| for it all |
| 33 // the time. | 34 // the time. |
| 34 const CGFloat kWidth(300.0); | 35 const CGFloat kWidth(300.0); |
| 35 | 36 |
| 36 // A narrow width for tests which test things that don't fit. | 37 // A narrow width for tests which test things that don't fit. |
| 37 const CGFloat kNarrowWidth(5.0); | 38 const CGFloat kNarrowWidth(5.0); |
| 38 | 39 |
| 39 class MockDecoration : public LocationBarDecoration { | 40 class MockDecoration : public LocationBarDecoration { |
| 40 public: | 41 public: |
| 41 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; } | 42 MockDecoration() : LocationBarDecoration(), |
| 43 should_auto_collapse_(false) { |
| 44 } |
| 45 |
| 46 virtual CGFloat GetWidthForSpace(CGFloat width, CGFloat text_width) { |
| 47 const CGFloat kMyWidth = 20.0; |
| 48 if (should_auto_collapse_ && (width - kMyWidth) < text_width) |
| 49 return kOmittedWidth; |
| 50 return kMyWidth; |
| 51 } |
| 52 |
| 53 void set_should_auto_collapse(bool value) { should_auto_collapse_ = value; } |
| 42 | 54 |
| 43 MOCK_METHOD2(DrawInFrame, void(NSRect frame, NSView* control_view)); | 55 MOCK_METHOD2(DrawInFrame, void(NSRect frame, NSView* control_view)); |
| 44 MOCK_METHOD0(GetToolTip, NSString*()); | 56 MOCK_METHOD0(GetToolTip, NSString*()); |
| 57 |
| 58 private: |
| 59 bool should_auto_collapse_; |
| 45 }; | 60 }; |
| 46 | 61 |
| 47 class AutocompleteTextFieldCellTest : public CocoaTest { | 62 class AutocompleteTextFieldCellTest : public CocoaTest { |
| 48 public: | 63 public: |
| 49 AutocompleteTextFieldCellTest() { | 64 AutocompleteTextFieldCellTest() { |
| 50 // Make sure this is wide enough to play games with the cell | 65 // Make sure this is wide enough to play games with the cell |
| 51 // decorations. | 66 // decorations. |
| 52 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); | 67 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); |
| 53 | 68 |
| 54 scoped_nsobject<NSTextField> view( | 69 scoped_nsobject<NSTextField> view( |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 113 |
| 99 // Load available decorations and try drawing. To make sure that | 114 // Load available decorations and try drawing. To make sure that |
| 100 // they are actually drawn, check that |GetWidthForSpace()| doesn't | 115 // they are actually drawn, check that |GetWidthForSpace()| doesn't |
| 101 // indicate that they should be omitted. | 116 // indicate that they should be omitted. |
| 102 const CGFloat kVeryWide = 1000.0; | 117 const CGFloat kVeryWide = 1000.0; |
| 103 | 118 |
| 104 SelectedKeywordDecoration selected_keyword_decoration([view_ font]); | 119 SelectedKeywordDecoration selected_keyword_decoration([view_ font]); |
| 105 selected_keyword_decoration.SetVisible(true); | 120 selected_keyword_decoration.SetVisible(true); |
| 106 selected_keyword_decoration.SetKeyword(ASCIIToUTF16("Google"), false); | 121 selected_keyword_decoration.SetKeyword(ASCIIToUTF16("Google"), false); |
| 107 [cell addLeftDecoration:&selected_keyword_decoration]; | 122 [cell addLeftDecoration:&selected_keyword_decoration]; |
| 108 EXPECT_NE(selected_keyword_decoration.GetWidthForSpace(kVeryWide), | 123 EXPECT_NE(selected_keyword_decoration.GetWidthForSpace(kVeryWide, 0), |
| 109 LocationBarDecoration::kOmittedWidth); | 124 LocationBarDecoration::kOmittedWidth); |
| 110 | 125 |
| 111 // TODO(shess): This really wants a |LocationBarViewMac|, but only a | 126 // TODO(shess): This really wants a |LocationBarViewMac|, but only a |
| 112 // few methods reference it, so this works well enough. But | 127 // few methods reference it, so this works well enough. But |
| 113 // something better would be nice. | 128 // something better would be nice. |
| 114 LocationIconDecoration location_icon_decoration(NULL); | 129 LocationIconDecoration location_icon_decoration(NULL); |
| 115 location_icon_decoration.SetVisible(true); | 130 location_icon_decoration.SetVisible(true); |
| 116 location_icon_decoration.SetImage([NSImage imageNamed:@"NSApplicationIcon"]); | 131 location_icon_decoration.SetImage([NSImage imageNamed:@"NSApplicationIcon"]); |
| 117 [cell addLeftDecoration:&location_icon_decoration]; | 132 [cell addLeftDecoration:&location_icon_decoration]; |
| 118 EXPECT_NE(location_icon_decoration.GetWidthForSpace(kVeryWide), | 133 EXPECT_NE(location_icon_decoration.GetWidthForSpace(kVeryWide, 0), |
| 119 LocationBarDecoration::kOmittedWidth); | 134 LocationBarDecoration::kOmittedWidth); |
| 120 | 135 |
| 121 EVBubbleDecoration ev_bubble_decoration(&location_icon_decoration, | 136 EVBubbleDecoration ev_bubble_decoration(&location_icon_decoration, |
| 122 [view_ font]); | 137 [view_ font]); |
| 123 ev_bubble_decoration.SetVisible(true); | 138 ev_bubble_decoration.SetVisible(true); |
| 124 ev_bubble_decoration.SetImage([NSImage imageNamed:@"NSApplicationIcon"]); | 139 ev_bubble_decoration.SetImage([NSImage imageNamed:@"NSApplicationIcon"]); |
| 125 ev_bubble_decoration.SetLabel(@"Application"); | 140 ev_bubble_decoration.SetLabel(@"Application"); |
| 126 [cell addLeftDecoration:&ev_bubble_decoration]; | 141 [cell addLeftDecoration:&ev_bubble_decoration]; |
| 127 EXPECT_NE(ev_bubble_decoration.GetWidthForSpace(kVeryWide), | 142 EXPECT_NE(ev_bubble_decoration.GetWidthForSpace(kVeryWide, 0), |
| 128 LocationBarDecoration::kOmittedWidth); | 143 LocationBarDecoration::kOmittedWidth); |
| 129 | 144 |
| 130 StarDecoration star_decoration(NULL); | 145 StarDecoration star_decoration(NULL); |
| 131 star_decoration.SetVisible(true); | 146 star_decoration.SetVisible(true); |
| 132 [cell addRightDecoration:&star_decoration]; | 147 [cell addRightDecoration:&star_decoration]; |
| 133 EXPECT_NE(star_decoration.GetWidthForSpace(kVeryWide), | 148 EXPECT_NE(star_decoration.GetWidthForSpace(kVeryWide, 0), |
| 134 LocationBarDecoration::kOmittedWidth); | 149 LocationBarDecoration::kOmittedWidth); |
| 135 | 150 |
| 136 KeywordHintDecoration keyword_hint_decoration([view_ font]); | 151 KeywordHintDecoration keyword_hint_decoration([view_ font]); |
| 137 keyword_hint_decoration.SetVisible(true); | 152 keyword_hint_decoration.SetVisible(true); |
| 138 keyword_hint_decoration.SetKeyword(ASCIIToUTF16("google"), false); | 153 keyword_hint_decoration.SetKeyword(ASCIIToUTF16("google"), false); |
| 139 [cell addRightDecoration:&keyword_hint_decoration]; | 154 [cell addRightDecoration:&keyword_hint_decoration]; |
| 140 EXPECT_NE(keyword_hint_decoration.GetWidthForSpace(kVeryWide), | 155 EXPECT_NE(keyword_hint_decoration.GetWidthForSpace(kVeryWide, 0), |
| 141 LocationBarDecoration::kOmittedWidth); | 156 LocationBarDecoration::kOmittedWidth); |
| 142 | 157 |
| 143 // Make sure we're actually calling |DrawInFrame()|. | 158 // Make sure we're actually calling |DrawInFrame()|. |
| 144 StrictMock<MockDecoration> mock_decoration; | 159 StrictMock<MockDecoration> mock_decoration; |
| 145 mock_decoration.SetVisible(true); | 160 mock_decoration.SetVisible(true); |
| 146 [cell addLeftDecoration:&mock_decoration]; | 161 [cell addLeftDecoration:&mock_decoration]; |
| 147 EXPECT_CALL(mock_decoration, DrawInFrame(_, _)); | 162 EXPECT_CALL(mock_decoration, DrawInFrame(_, _)); |
| 148 EXPECT_NE(mock_decoration.GetWidthForSpace(kVeryWide), | 163 EXPECT_NE(mock_decoration.GetWidthForSpace(kVeryWide, 0), |
| 149 LocationBarDecoration::kOmittedWidth); | 164 LocationBarDecoration::kOmittedWidth); |
| 150 | 165 |
| 151 [view_ display]; | 166 [view_ display]; |
| 152 | 167 |
| 153 [cell clearDecorations]; | 168 [cell clearDecorations]; |
| 154 } | 169 } |
| 155 | 170 |
| 156 TEST_F(AutocompleteTextFieldCellTest, TextFrame) { | 171 TEST_F(AutocompleteTextFieldCellTest, TextFrame) { |
| 157 AutocompleteTextFieldCell* cell = | 172 AutocompleteTextFieldCell* cell = |
| 158 static_cast<AutocompleteTextFieldCell*>([view_ cell]); | 173 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 | 307 |
| 293 // |controlView| gets the tooltip for the left decoration. | 308 // |controlView| gets the tooltip for the left decoration. |
| 294 id controlView = [OCMockObject mockForClass:[AutocompleteTextField class]]; | 309 id controlView = [OCMockObject mockForClass:[AutocompleteTextField class]]; |
| 295 [[controlView expect] addToolTip:tooltip forRect:leftDecorationRect]; | 310 [[controlView expect] addToolTip:tooltip forRect:leftDecorationRect]; |
| 296 | 311 |
| 297 [cell updateToolTipsInRect:bounds ofView:controlView]; | 312 [cell updateToolTipsInRect:bounds ofView:controlView]; |
| 298 | 313 |
| 299 EXPECT_OCMOCK_VERIFY(controlView); | 314 EXPECT_OCMOCK_VERIFY(controlView); |
| 300 } | 315 } |
| 301 | 316 |
| 317 TEST_F(AutocompleteTextFieldCellTest, HideUnneededSeparators) { |
| 318 AutocompleteTextFieldCell* cell = |
| 319 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 320 const NSRect bounds = [view_ bounds]; |
| 321 |
| 322 SeparatorDecoration separator; |
| 323 [cell clearDecorations]; |
| 324 [cell addRightDecoration:&mock_right_decoration0_]; |
| 325 [cell addRightDecoration:&separator]; |
| 326 [cell addRightDecoration:&mock_right_decoration1_]; |
| 327 separator.SetVisible(true); |
| 328 |
| 329 // Verify that a separator between two decorations is visible. |
| 330 mock_right_decoration0_.SetVisible(true); |
| 331 mock_right_decoration1_.SetVisible(true); |
| 332 NSRect rect = [cell frameForDecoration:&separator inFrame:bounds]; |
| 333 EXPECT_LT(0, NSWidth(rect)); |
| 334 |
| 335 // Verify that a separator with no visible decorations on the right is hidden. |
| 336 mock_right_decoration0_.SetVisible(false); |
| 337 mock_right_decoration1_.SetVisible(true); |
| 338 rect = [cell frameForDecoration:&separator inFrame:bounds]; |
| 339 EXPECT_EQ(0, NSWidth(rect)); |
| 340 |
| 341 // Verify that a separator with no visible decorations on the left is hidden. |
| 342 mock_right_decoration0_.SetVisible(true); |
| 343 mock_right_decoration1_.SetVisible(false); |
| 344 rect = [cell frameForDecoration:&separator inFrame:bounds]; |
| 345 EXPECT_EQ(0, NSWidth(rect)); |
| 346 |
| 347 // Verify that a separator with no visible decorations on the either side is |
| 348 // hidden. |
| 349 mock_right_decoration0_.SetVisible(false); |
| 350 mock_right_decoration1_.SetVisible(false); |
| 351 rect = [cell frameForDecoration:&separator inFrame:bounds]; |
| 352 EXPECT_EQ(0, NSWidth(rect)); |
| 353 } |
| 354 |
| 355 TEST_F(AutocompleteTextFieldCellTest, AutoCollapse) { |
| 356 AutocompleteTextFieldCell* cell = |
| 357 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 358 const NSRect bounds = [view_ bounds]; |
| 359 // Force the string to overlap decorations. |
| 360 [cell setStringValue:@"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"]; |
| 361 |
| 362 // Verify that the decoration is visible without auto collapse. |
| 363 mock_right_decoration0_.SetVisible(true); |
| 364 mock_right_decoration0_.set_should_auto_collapse(false); |
| 365 NSRect rect = [cell frameForDecoration:&mock_right_decoration0_ |
| 366 inFrame:bounds]; |
| 367 EXPECT_LT(0, NSWidth(rect)); |
| 368 |
| 369 // Verify that the decoration is hidden with auto collapse. |
| 370 mock_right_decoration0_.set_should_auto_collapse(true); |
| 371 rect = [cell frameForDecoration:&mock_right_decoration0_ |
| 372 inFrame:bounds]; |
| 373 EXPECT_EQ(0, NSWidth(rect)); |
| 374 |
| 375 // Verify that the decoration is visible with auto collapse and short string. |
| 376 [cell setStringValue:@"WWW"]; |
| 377 rect = [cell frameForDecoration:&mock_right_decoration0_ |
| 378 inFrame:bounds]; |
| 379 EXPECT_LT(0, NSWidth(rect)); |
| 380 } |
| 381 |
| 302 } // namespace | 382 } // namespace |
| OLD | NEW |