| 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: |
| 42 MockDecoration() : LocationBarDecoration(), |
| 43 should_auto_collapse_(false) { |
| 44 } |
| 45 |
| 41 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; } | 46 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; } |
| 47 virtual bool ShouldAutoCollapse() const OVERRIDE { |
| 48 return should_auto_collapse_; |
| 49 } |
| 50 |
| 51 void set_should_auto_collapse(bool value) { should_auto_collapse_ = value; } |
| 42 | 52 |
| 43 MOCK_METHOD2(DrawInFrame, void(NSRect frame, NSView* control_view)); | 53 MOCK_METHOD2(DrawInFrame, void(NSRect frame, NSView* control_view)); |
| 44 MOCK_METHOD0(GetToolTip, NSString*()); | 54 MOCK_METHOD0(GetToolTip, NSString*()); |
| 55 |
| 56 private: |
| 57 bool should_auto_collapse_; |
| 45 }; | 58 }; |
| 46 | 59 |
| 47 class AutocompleteTextFieldCellTest : public CocoaTest { | 60 class AutocompleteTextFieldCellTest : public CocoaTest { |
| 48 public: | 61 public: |
| 49 AutocompleteTextFieldCellTest() { | 62 AutocompleteTextFieldCellTest() { |
| 50 // Make sure this is wide enough to play games with the cell | 63 // Make sure this is wide enough to play games with the cell |
| 51 // decorations. | 64 // decorations. |
| 52 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); | 65 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); |
| 53 | 66 |
| 54 scoped_nsobject<NSTextField> view( | 67 scoped_nsobject<NSTextField> view( |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 | 305 |
| 293 // |controlView| gets the tooltip for the left decoration. | 306 // |controlView| gets the tooltip for the left decoration. |
| 294 id controlView = [OCMockObject mockForClass:[AutocompleteTextField class]]; | 307 id controlView = [OCMockObject mockForClass:[AutocompleteTextField class]]; |
| 295 [[controlView expect] addToolTip:tooltip forRect:leftDecorationRect]; | 308 [[controlView expect] addToolTip:tooltip forRect:leftDecorationRect]; |
| 296 | 309 |
| 297 [cell updateToolTipsInRect:bounds ofView:controlView]; | 310 [cell updateToolTipsInRect:bounds ofView:controlView]; |
| 298 | 311 |
| 299 EXPECT_OCMOCK_VERIFY(controlView); | 312 EXPECT_OCMOCK_VERIFY(controlView); |
| 300 } | 313 } |
| 301 | 314 |
| 315 TEST_F(AutocompleteTextFieldCellTest, HideUnneededSeparators) { |
| 316 AutocompleteTextFieldCell* cell = |
| 317 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 318 const NSRect bounds = [view_ bounds]; |
| 319 |
| 320 SeparatorDecoration separator; |
| 321 [cell clearDecorations]; |
| 322 [cell addRightDecoration:&mock_right_decoration0_]; |
| 323 [cell addRightDecoration:&separator]; |
| 324 [cell addRightDecoration:&mock_right_decoration1_]; |
| 325 separator.SetVisible(true); |
| 326 |
| 327 // Verify that a separator between two decorations is visible. |
| 328 mock_right_decoration0_.SetVisible(true); |
| 329 mock_right_decoration1_.SetVisible(true); |
| 330 NSRect rect = [cell frameForDecoration:&separator inFrame:bounds]; |
| 331 EXPECT_LT(0, NSWidth(rect)); |
| 332 |
| 333 // Verify that a separator with no visible decorations on the right is hidden. |
| 334 mock_right_decoration0_.SetVisible(false); |
| 335 mock_right_decoration1_.SetVisible(true); |
| 336 rect = [cell frameForDecoration:&separator inFrame:bounds]; |
| 337 EXPECT_EQ(0, NSWidth(rect)); |
| 338 |
| 339 // Verify that a separator with no visible decorations on the left is hidden. |
| 340 mock_right_decoration0_.SetVisible(true); |
| 341 mock_right_decoration1_.SetVisible(false); |
| 342 rect = [cell frameForDecoration:&separator inFrame:bounds]; |
| 343 EXPECT_EQ(0, NSWidth(rect)); |
| 344 } |
| 345 |
| 346 TEST_F(AutocompleteTextFieldCellTest, AutoCollapse) { |
| 347 AutocompleteTextFieldCell* cell = |
| 348 static_cast<AutocompleteTextFieldCell*>([view_ cell]); |
| 349 const NSRect bounds = [view_ bounds]; |
| 350 // Force the string to overlap decorations. |
| 351 [cell setStringValue:@"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"]; |
| 352 |
| 353 // Verify that the decoration is visible without auto collapse. |
| 354 mock_right_decoration0_.SetVisible(true); |
| 355 mock_right_decoration0_.set_should_auto_collapse(false); |
| 356 NSRect rect = [cell frameForDecoration:&mock_right_decoration0_ |
| 357 inFrame:bounds]; |
| 358 EXPECT_LT(0, NSWidth(rect)); |
| 359 |
| 360 // Verify that the decoration is hidden with auto collapse. |
| 361 mock_right_decoration0_.set_should_auto_collapse(true); |
| 362 rect = [cell frameForDecoration:&mock_right_decoration0_ |
| 363 inFrame:bounds]; |
| 364 EXPECT_EQ(0, NSWidth(rect)); |
| 365 |
| 366 // Verify that the decoration is visible with auto collapse and short string. |
| 367 [cell setStringValue:@"WWW"]; |
| 368 rect = [cell frameForDecoration:&mock_right_decoration0_ |
| 369 inFrame:bounds]; |
| 370 EXPECT_LT(0, NSWidth(rect)); |
| 371 } |
| 372 |
| 302 } // namespace | 373 } // namespace |
| OLD | NEW |