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/memory/scoped_nsobject.h" 8 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field_cell.h" 9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/platform_test.h" 12 13 @interface FindBarTextFieldCell (ExposedForTesting) 14 - (NSAttributedString*)resultsString; 15 @end 16 17 @implementation FindBarTextFieldCell (ExposedForTesting) 18 - (NSAttributedString*)resultsString { 19 return resultsString_.get(); 20 } 21 @end 22 23 namespace { 24 25 // Width of the field so that we don't have to ask |field_| for it all 26 // the time. 27 const CGFloat kWidth(300.0); 28 29 // A narrow width for tests which test things that don't fit. 30 const CGFloat kNarrowWidth(5.0); 31 32 class FindBarTextFieldCellTest : public CocoaTest { 33 public: 34 FindBarTextFieldCellTest() { 35 // Make sure this is wide enough to play games with the cell 36 // decorations. 37 const NSRect frame = NSMakeRect(0, 0, kWidth, 30); 38 39 scoped_nsobject<FindBarTextFieldCell> cell( 40 [[FindBarTextFieldCell alloc] initTextCell:@"Testing"]); 41 cell_ = cell; 42 [cell_ setEditable:YES]; 43 [cell_ setBordered:YES]; 44 45 scoped_nsobject<NSTextField> view( 46 [[NSTextField alloc] initWithFrame:frame]); 47 view_ = view; 48 [view_ setCell:cell_]; 49 50 [[test_window() contentView] addSubview:view_]; 51 } 52 53 NSTextField* view_; 54 FindBarTextFieldCell* cell_; 55 }; 56 57 // Basic view tests (AddRemove, Display). 58 TEST_VIEW(FindBarTextFieldCellTest, view_); 59 60 // Test drawing, mostly to ensure nothing leaks or crashes. 61 TEST_F(FindBarTextFieldCellTest, FocusedDisplay) { 62 [view_ display]; 63 64 // Test focused drawing. 65 [test_window() makePretendKeyWindowAndSetFirstResponder:view_]; 66 [view_ display]; 67 [test_window() clearPretendKeyWindowAndFirstResponder]; 68 69 // Test display of various cell configurations. 70 [cell_ setActiveMatch:4 of:30]; 71 [view_ display]; 72 73 [cell_ setActiveMatch:0 of:0]; 74 [view_ display]; 75 76 [cell_ clearResults]; 77 [view_ display]; 78 } 79 80 // Verify that setting and clearing the find results changes the results string 81 // appropriately. 82 TEST_F(FindBarTextFieldCellTest, SetAndClearFindResults) { 83 [cell_ setActiveMatch:10 of:30]; 84 scoped_nsobject<NSAttributedString> tenString([[cell_ resultsString] copy]); 85 EXPECT_GT([tenString length], 0U); 86 87 [cell_ setActiveMatch:0 of:0]; 88 scoped_nsobject<NSAttributedString> zeroString([[cell_ resultsString] copy]); 89 EXPECT_GT([zeroString length], 0U); 90 EXPECT_FALSE([tenString isEqualToAttributedString:zeroString]); 91 92 [cell_ clearResults]; 93 EXPECT_EQ(0U, [[cell_ resultsString] length]); 94 } 95 96 TEST_F(FindBarTextFieldCellTest, TextFrame) { 97 const NSRect bounds = [view_ bounds]; 98 NSRect textFrame = [cell_ textFrameForFrame:bounds]; 99 NSRect cursorFrame = [cell_ textCursorFrameForFrame:bounds]; 100 101 // At default settings, everything goes to the text area. 102 EXPECT_FALSE(NSIsEmptyRect(textFrame)); 103 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); 104 EXPECT_EQ(NSMinX(bounds), NSMinX(textFrame)); 105 EXPECT_EQ(NSMaxX(bounds), NSMaxX(textFrame)); 106 EXPECT_TRUE(NSEqualRects(cursorFrame, textFrame)); 107 108 // Setting an active match leaves text frame to left. 109 [cell_ setActiveMatch:4 of:5]; 110 textFrame = [cell_ textFrameForFrame:bounds]; 111 cursorFrame = [cell_ textCursorFrameForFrame:bounds]; 112 EXPECT_FALSE(NSIsEmptyRect(textFrame)); 113 EXPECT_TRUE(NSContainsRect(bounds, textFrame)); 114 EXPECT_LT(NSMaxX(textFrame), NSMaxX(bounds)); 115 EXPECT_TRUE(NSEqualRects(cursorFrame, textFrame)); 116 117 } 118 119 // The editor frame should be slightly inset from the text frame. 120 TEST_F(FindBarTextFieldCellTest, DrawingRectForBounds) { 121 const NSRect bounds = [view_ bounds]; 122 NSRect textFrame = [cell_ textFrameForFrame:bounds]; 123 NSRect drawingRect = [cell_ drawingRectForBounds:bounds]; 124 125 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); 126 EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1))); 127 128 [cell_ setActiveMatch:4 of:5]; 129 textFrame = [cell_ textFrameForFrame:bounds]; 130 drawingRect = [cell_ drawingRectForBounds:bounds]; 131 EXPECT_FALSE(NSIsEmptyRect(drawingRect)); 132 EXPECT_TRUE(NSContainsRect(textFrame, NSInsetRect(drawingRect, 1, 1))); 133 } 134 135 } // namespace 136