1 // Copyright (c) 2012 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/find_bar/find_bar_text_field.h"
10 #import "chrome/browser/ui/cocoa/find_bar/find_bar_text_field_cell.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 static const CGFloat kWidth(300.0);
20 class FindBarTextFieldTest : public CocoaTest {
22 FindBarTextFieldTest() {
23 // Make sure this is wide enough to play games with the cell
25 NSRect frame = NSMakeRect(0, 0, kWidth, 30);
26 base::scoped_nsobject<FindBarTextField> field(
27 [[FindBarTextField alloc] initWithFrame:frame]);
30 [field_ setStringValue:@"Test test"];
31 [[test_window() contentView] addSubview:field_];
34 FindBarTextField* field_;
37 // Basic view tests (AddRemove, Display).
38 TEST_VIEW(FindBarTextFieldTest, field_);
40 // Test that we have the right cell class.
41 TEST_F(FindBarTextFieldTest, CellClass) {
42 EXPECT_TRUE([[field_ cell] isKindOfClass:[FindBarTextFieldCell class]]);
45 // Test that we get the same cell from -cell and
46 // -findBarTextFieldCell.
47 TEST_F(FindBarTextFieldTest, Cell) {
48 FindBarTextFieldCell* cell = [field_ findBarTextFieldCell];
49 EXPECT_EQ(cell, [field_ cell]);
50 EXPECT_TRUE(cell != nil);
53 // Test that becoming first responder sets things up correctly.
54 TEST_F(FindBarTextFieldTest, FirstResponder) {
55 EXPECT_EQ(nil, [field_ currentEditor]);
56 EXPECT_EQ([[field_ subviews] count], 0U);
57 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
58 EXPECT_FALSE(nil == [field_ currentEditor]);
59 EXPECT_EQ([[field_ subviews] count], 1U);
60 EXPECT_TRUE([[field_ currentEditor] isDescendantOf:field_]);
63 // Test drawing, mostly to ensure nothing leaks or crashes.
64 TEST_F(FindBarTextFieldTest, Display) {
67 // Test focussed drawing.
68 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
70 [test_window() clearPretendKeyWindowAndFirstResponder];
72 // Test display of various cell configurations.
73 FindBarTextFieldCell* cell = [field_ findBarTextFieldCell];
74 [cell setActiveMatch:4 of:5];