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 3// found in the LICENSE file. 4 5#import <Cocoa/Cocoa.h> 6 7#include "base/mac/scoped_nsobject.h" 8#import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 9#import "chrome/browser/ui/cocoa/styled_text_field_cell.h" 10#import "chrome/browser/ui/cocoa/styled_text_field_test_helper.h" 11#include "testing/gtest/include/gtest/gtest.h" 12#include "testing/platform_test.h" 13 14namespace { 15 16// Width of the field so that we don't have to ask |field_| for it all 17// the time. 18const CGFloat kWidth(300.0); 19 20class StyledTextFieldCellTest : public CocoaTest { 21 public: 22 StyledTextFieldCellTest() { 23 // Make sure this is wide enough to play games with the cell 24 // decorations. 25 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); 26 27 base::scoped_nsobject<StyledTextFieldTestCell> cell( 28 [[StyledTextFieldTestCell alloc] initTextCell:@"Testing"]); 29 cell_ = cell.get(); 30 [cell_ setEditable:YES]; 31 [cell_ setBordered:YES]; 32 33 base::scoped_nsobject<NSTextField> view( 34 [[NSTextField alloc] initWithFrame:frame]); 35 view_ = view.get(); 36 [view_ setCell:cell_]; 37 38 [[test_window() contentView] addSubview:view_]; 39 } 40 41 NSTextField* view_; 42 StyledTextFieldTestCell* cell_; 43}; 44 45// Basic view tests (AddRemove, Display). 46TEST_VIEW(StyledTextFieldCellTest, view_); 47 48// Test drawing, mostly to ensure nothing leaks or crashes. 49TEST_F(StyledTextFieldCellTest, FocusedDisplay) { 50 [view_ display]; 51 52 // Test focused drawing. 53 [test_window() makePretendKeyWindowAndSetFirstResponder:view_]; 54 [view_ display]; 55 [test_window() clearPretendKeyWindowAndFirstResponder]; 56 57 // Test display of various cell configurations. 58 [cell_ setLeftMargin:5]; 59 [view_ display]; 60 61 [cell_ setRightMargin:15]; 62 [view_ display]; 63} 64 65// The editor frame should be slightly inset from the text frame. 66TEST_F(StyledTextFieldCellTest, DrawingRectForBounds) { 67 const NSRect bounds = [view_ bounds]; 68 NSRect textFrame = [cell_ textFrameForFrame:bounds]; 69 NSRect drawingRect = [cell_ drawingRectForBounds:bounds]; 70 71 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); 72 EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1))); 73 74 [cell_ setLeftMargin:10]; 75 textFrame = [cell_ textFrameForFrame:bounds]; 76 drawingRect = [cell_ drawingRectForBounds:bounds]; 77 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); 78 EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1))); 79 80 [cell_ setRightMargin:20]; 81 textFrame = [cell_ textFrameForFrame:bounds]; 82 drawingRect = [cell_ drawingRectForBounds:bounds]; 83 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); 84 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); 85 86 [cell_ setLeftMargin:0]; 87 textFrame = [cell_ textFrameForFrame:bounds]; 88 drawingRect = [cell_ drawingRectForBounds:bounds]; 89 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); 90 EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); 91} 92 93} // namespace 94