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.
5 #import <Cocoa/Cocoa.h>
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"
16 // Width of the field so that we don't have to ask |field_| for it all
18 const CGFloat kWidth(300.0);
20 class StyledTextFieldCellTest : public CocoaTest {
22 StyledTextFieldCellTest() {
23 // Make sure this is wide enough to play games with the cell
25 const NSRect frame = NSMakeRect(0, 0, kWidth, 30);
27 base::scoped_nsobject<StyledTextFieldTestCell> cell(
28 [[StyledTextFieldTestCell alloc] initTextCell:@"Testing"]);
30 [cell_ setEditable:YES];
31 [cell_ setBordered:YES];
33 base::scoped_nsobject<NSTextField> view(
34 [[NSTextField alloc] initWithFrame:frame]);
36 [view_ setCell:cell_];
38 [[test_window() contentView] addSubview:view_];
42 StyledTextFieldTestCell* cell_;
45 // Basic view tests (AddRemove, Display).
46 TEST_VIEW(StyledTextFieldCellTest, view_);
48 // Test drawing, mostly to ensure nothing leaks or crashes.
49 TEST_F(StyledTextFieldCellTest, FocusedDisplay) {
52 // Test focused drawing.
53 [test_window() makePretendKeyWindowAndSetFirstResponder:view_];
55 [test_window() clearPretendKeyWindowAndFirstResponder];
57 // Test display of various cell configurations.
58 [cell_ setLeftMargin:5];
61 [cell_ setRightMargin:15];
65 // The editor frame should be slightly inset from the text frame.
66 TEST_F(StyledTextFieldCellTest, DrawingRectForBounds) {
67 const NSRect bounds = [view_ bounds];
68 NSRect textFrame = [cell_ textFrameForFrame:bounds];
69 NSRect drawingRect = [cell_ drawingRectForBounds:bounds];
71 EXPECT_FALSE(NSIsEmptyRect(drawingRect));
72 EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1)));
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)));
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));
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));