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 #import "base/mac/cocoa_protocols.h" 8 #include "base/memory/scoped_nsobject.h" 9 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field.h" 10 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field_cell.h" 11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/platform_test.h" 14 #import "third_party/ocmock/OCMock/OCMock.h" 15 16 // OCMock wants to mock a concrete class or protocol. This should 17 // provide a correct protocol for newer versions of the SDK, while 18 // providing something mockable for older versions. 19 20 @protocol MockTextEditingDelegate<NSControlTextEditingDelegate> 21 - (void)controlTextDidBeginEditing:(NSNotification*)aNotification; 22 - (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor; 23 @end 24 25 namespace { 26 27 // Width of the field so that we don't have to ask |field_| for it all 28 // the time. 29 static const CGFloat kWidth(300.0); 30 31 class FindBarTextFieldTest : public CocoaTest { 32 public: 33 FindBarTextFieldTest() { 34 // Make sure this is wide enough to play games with the cell 35 // decorations. 36 NSRect frame = NSMakeRect(0, 0, kWidth, 30); 37 scoped_nsobject<FindBarTextField> field( 38 [[FindBarTextField alloc] initWithFrame:frame]); 39 field_ = field.get(); 40 41 [field_ setStringValue:@"Test test"]; 42 [[test_window() contentView] addSubview:field_]; 43 } 44 45 FindBarTextField* field_; 46 }; 47 48 // Basic view tests (AddRemove, Display). 49 TEST_VIEW(FindBarTextFieldTest, field_); 50 51 // Test that we have the right cell class. 52 TEST_F(FindBarTextFieldTest, CellClass) { 53 EXPECT_TRUE([[field_ cell] isKindOfClass:[FindBarTextFieldCell class]]); 54 } 55 56 // Test that we get the same cell from -cell and 57 // -findBarTextFieldCell. 58 TEST_F(FindBarTextFieldTest, Cell) { 59 FindBarTextFieldCell* cell = [field_ findBarTextFieldCell]; 60 EXPECT_EQ(cell, [field_ cell]); 61 EXPECT_TRUE(cell != nil); 62 } 63 64 // Test that becoming first responder sets things up correctly. 65 TEST_F(FindBarTextFieldTest, FirstResponder) { 66 EXPECT_EQ(nil, [field_ currentEditor]); 67 EXPECT_EQ([[field_ subviews] count], 0U); 68 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 69 EXPECT_FALSE(nil == [field_ currentEditor]); 70 EXPECT_EQ([[field_ subviews] count], 1U); 71 EXPECT_TRUE([[field_ currentEditor] isDescendantOf:field_]); 72 } 73 74 // Test drawing, mostly to ensure nothing leaks or crashes. 75 TEST_F(FindBarTextFieldTest, Display) { 76 [field_ display]; 77 78 // Test focussed drawing. 79 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 80 [field_ display]; 81 [test_window() clearPretendKeyWindowAndFirstResponder]; 82 83 // Test display of various cell configurations. 84 FindBarTextFieldCell* cell = [field_ findBarTextFieldCell]; 85 [cell setActiveMatch:4 of:5]; 86 [field_ display]; 87 88 [cell clearResults]; 89 [field_ display]; 90 } 91 92 } // namespace 93