1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #import "chrome/browser/ui/cocoa/autofill/down_arrow_popup_menu_cell.h" 6 7 #import <Cocoa/Cocoa.h> 8 9 #include "base/mac/scoped_nsobject.h" 10 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/platform_test.h" 13 #import "ui/gfx/test/ui_cocoa_test_helper.h" 14 15 namespace { 16 17 class DownArrowPopupMenuCellTest : public ui::CocoaTest { 18 public: 19 DownArrowPopupMenuCellTest() { 20 NSRect frame = NSMakeRect(0, 0, 50, 30); 21 view_.reset([[NSButton alloc] initWithFrame:frame]); 22 base::scoped_nsobject<DownArrowPopupMenuCell> cell( 23 [[DownArrowPopupMenuCell alloc] initTextCell:@"Testing"]); 24 [view_ setCell:cell.get()]; 25 [[test_window() contentView] addSubview:view_]; } 26 27 protected: 28 base::scoped_nsobject<NSButton> view_; 29 30 private: 31 DISALLOW_COPY_AND_ASSIGN(DownArrowPopupMenuCellTest); 32 }; 33 34 TEST_VIEW(DownArrowPopupMenuCellTest, view_) 35 36 } // namespace 37 38 // Make internal messages visible for testing 39 @interface DownArrowPopupMenuCell (Testing) 40 41 - (NSSize)cellSize; 42 - (NSSize)imageSize; 43 44 @end 45 46 // Test size computations, make sure they deliver correct results. 47 TEST_F(DownArrowPopupMenuCellTest, Defaults) { 48 DownArrowPopupMenuCell* cell = 49 static_cast<DownArrowPopupMenuCell*>([view_ cell]); 50 ASSERT_TRUE([cell isKindOfClass:[DownArrowPopupMenuCell class]]); 51 52 NSRect rect = NSMakeRect(0, 0, 11, 17); 53 base::scoped_nsobject<NSImage> image( 54 [[NSImage alloc] initWithSize:rect.size]); 55 [cell setImage:image forButtonState:image_button_cell::kDefaultState]; 56 [view_ setTitle:@"Testing"]; 57 58 EXPECT_EQ(NSWidth(rect), [cell imageSize].width); 59 EXPECT_EQ(NSHeight(rect), [cell imageSize].height); 60 61 NSAttributedString* title = [cell attributedTitle]; 62 NSSize titleSize = [title size]; 63 EXPECT_LE(titleSize.width + [image size].width + autofill::kButtonGap, 64 [cell cellSize].width); 65 EXPECT_LE(std::max(titleSize.height, [image size].height), 66 [cell cellSize].height); 67 } 68