Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / styled_text_field_cell_unittest.mm
bloba01c2c474a484a888d60c093b1fa5f3d1e1e7951
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"
14 namespace {
16 // Width of the field so that we don't have to ask |field_| for it all
17 // the time.
18 const CGFloat kWidth(300.0);
20 class 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);
27     base::scoped_nsobject<StyledTextFieldTestCell> cell(
28         [[StyledTextFieldTestCell alloc] initTextCell:@"Testing"]);
29     cell_ = cell.get();
30     [cell_ setEditable:YES];
31     [cell_ setBordered:YES];
33     base::scoped_nsobject<NSTextField> view(
34         [[NSTextField alloc] initWithFrame:frame]);
35     view_ = view.get();
36     [view_ setCell:cell_];
38     [[test_window() contentView] addSubview:view_];
39   }
41   NSTextField* 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) {
50   [view_ display];
52   // Test focused drawing.
53   [test_window() makePretendKeyWindowAndSetFirstResponder:view_];
54   [view_ display];
55   [test_window() clearPretendKeyWindowAndFirstResponder];
57   // Test display of various cell configurations.
58   [cell_ setLeftMargin:5];
59   [view_ display];
61   [cell_ setRightMargin:15];
62   [view_ display];
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));
93 }  // namespace