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