diff options
Diffstat (limited to 'chrome/browser/cocoa/styled_text_field_cell_unittest.mm')
-rw-r--r-- | chrome/browser/cocoa/styled_text_field_cell_unittest.mm | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/styled_text_field_cell_unittest.mm b/chrome/browser/cocoa/styled_text_field_cell_unittest.mm new file mode 100644 index 0000000..5928d43 --- /dev/null +++ b/chrome/browser/cocoa/styled_text_field_cell_unittest.mm @@ -0,0 +1,97 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import <Cocoa/Cocoa.h> + +#include "base/scoped_nsobject.h" +#import "chrome/browser/cocoa/styled_text_field_cell.h" +#import "chrome/browser/cocoa/styled_text_field_test_helper.h" +#import "chrome/browser/cocoa/cocoa_test_helper.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" + +namespace { + +// Width of the field so that we don't have to ask |field_| for it all +// the time. +const CGFloat kWidth(300.0); + +class StyledTextFieldCellTest : public CocoaTest { + public: + StyledTextFieldCellTest() { + // Make sure this is wide enough to play games with the cell + // decorations. + const NSRect frame = NSMakeRect(0, 0, kWidth, 30); + + scoped_nsobject<StyledTextFieldTestCell> cell( + [[StyledTextFieldTestCell alloc] initTextCell:@"Testing"]); + cell_ = cell.get(); + [cell_ setEditable:YES]; + [cell_ setBordered:YES]; + + scoped_nsobject<NSTextField> view( + [[NSTextField alloc] initWithFrame:frame]); + view_ = view.get(); + [view_ setCell:cell_]; + + [[test_window() contentView] addSubview:view_]; + } + + //CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... + NSTextField* view_; + StyledTextFieldTestCell* cell_; +}; + +// Basic view tests (AddRemove, Display). +TEST_VIEW(StyledTextFieldCellTest, view_); + +// Test drawing, mostly to ensure nothing leaks or crashes. +TEST_F(StyledTextFieldCellTest, FocusedDisplay) { + [view_ display]; + + // Test focused drawing. + [test_window() makePretendKeyWindowAndSetFirstResponder:view_]; + [view_ display]; + [test_window() clearPretendKeyWindowAndFirstResponder]; + + // Test display of various cell configurations. + [cell_ setLeftMargin:5]; + [view_ display]; + + [cell_ setRightMargin:15]; + [view_ display]; +} + +// The editor frame should be slightly inset from the text frame. +TEST_F(StyledTextFieldCellTest, DrawingRectForBounds) { + const NSRect bounds = [view_ bounds]; + NSRect textFrame = [cell_ textFrameForFrame:bounds]; + NSRect drawingRect = [cell_ drawingRectForBounds:bounds]; + + EXPECT_FALSE(NSIsEmptyRect(drawingRect)); + EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1))); + + // Save the starting frame for after clear. + const NSRect originalDrawingRect = drawingRect; + + [cell_ setLeftMargin:10]; + textFrame = [cell_ textFrameForFrame:bounds]; + drawingRect = [cell_ drawingRectForBounds:bounds]; + EXPECT_FALSE(NSIsEmptyRect(drawingRect)); + EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1))); + + [cell_ setRightMargin:20]; + textFrame = [cell_ textFrameForFrame:bounds]; + drawingRect = [cell_ drawingRectForBounds:bounds]; + EXPECT_FALSE(NSIsEmptyRect(drawingRect)); + EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); + + [cell_ setLeftMargin:0]; + textFrame = [cell_ textFrameForFrame:bounds]; + drawingRect = [cell_ drawingRectForBounds:bounds]; + EXPECT_FALSE(NSIsEmptyRect(drawingRect)); + EXPECT_TRUE(NSContainsRect(NSInsetRect(textFrame, 1, 1), drawingRect)); +} + +} // namespace |