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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
// 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>
#import "base/cocoa_protocols_mac.h"
#include "base/scoped_nsobject.h"
#import "chrome/browser/cocoa/autocomplete_text_field.h"
#import "chrome/browser/cocoa/autocomplete_text_field_cell.h"
#import "chrome/browser/cocoa/autocomplete_text_field_editor.h"
#import "chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
@interface AutocompleteTextFieldTestDelegate : NSObject {
BOOL receivedControlTextDidBeginEditing_;
BOOL receivedControlTextShouldEndEditing_;
}
- init;
- (BOOL)receivedControlTextDidBeginEditing;
- (BOOL)receivedControlTextShouldEndEditing;
@end
namespace {
class AutocompleteTextFieldTest : public PlatformTest {
public:
AutocompleteTextFieldTest() {
// Make sure this is wide enough to play games with the cell
// decorations.
NSRect frame = NSMakeRect(0, 0, 300, 30);
field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]);
[field_ setStringValue:@"Testing"];
[field_ setObserver:&field_observer_];
[cocoa_helper_.contentView() addSubview:field_.get()];
window_delegate_.reset(
[[AutocompleteTextFieldWindowTestDelegate alloc] init]);
[cocoa_helper_.window() setDelegate:window_delegate_.get()];
}
// The removeFromSuperview call is needed to prevent crashes in
// later tests.
// TODO(shess): -removeromSuperview should not be necessary. Fix
// it. Also in autocomplete_text_field_editor_unittest.mm.
~AutocompleteTextFieldTest() {
[cocoa_helper_.window() setDelegate:nil];
[field_ removeFromSuperview];
}
NSEvent* KeyDownEventWithFlags(NSUInteger flags) {
return [NSEvent keyEventWithType:NSKeyDown
location:NSZeroPoint
modifierFlags:flags
timestamp:0.0
windowNumber:[cocoa_helper_.window() windowNumber]
context:nil
characters:@"a"
charactersIgnoringModifiers:@"a"
isARepeat:NO
keyCode:'a'];
}
CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc...
scoped_nsobject<AutocompleteTextField> field_;
AutocompleteTextFieldObserverMock field_observer_;
scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_;
};
// Test that we have the right cell class.
TEST_F(AutocompleteTextFieldTest, CellClass) {
EXPECT_TRUE([[field_ cell] isKindOfClass:[AutocompleteTextFieldCell class]]);
}
// Test adding/removing from the view hierarchy, mostly to ensure nothing
// leaks or crashes.
TEST_F(AutocompleteTextFieldTest, AddRemove) {
EXPECT_EQ(cocoa_helper_.contentView(), [field_ superview]);
[field_.get() removeFromSuperview];
EXPECT_FALSE([field_ superview]);
}
// Test that we get the same cell from -cell and
// -autocompleteTextFieldCell.
TEST_F(AutocompleteTextFieldTest, Cell) {
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
EXPECT_EQ(cell, [field_ cell]);
EXPECT_TRUE(cell != nil);
}
// Test drawing, mostly to ensure nothing leaks or crashes.
TEST_F(AutocompleteTextFieldTest, Display) {
[field_ display];
// Test focussed drawing.
cocoa_helper_.makeFirstResponder(field_);
[field_ display];
cocoa_helper_.clearFirstResponder();
// Test display of various cell configurations.
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
[cell setSearchHintString:@"Type to search"];
[field_ display];
NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
[cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix"];
[field_ display];
[cell setKeywordString:@"Search Engine:"];
[field_ display];
[cell clearKeywordAndHint];
[field_ display];
}
TEST_F(AutocompleteTextFieldTest, FlagsChanged) {
// Test without Control key down, but some other modifier down.
field_observer_.Reset();
EXPECT_FALSE(field_observer_.on_control_key_changed_called_);
[field_ flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)];
EXPECT_TRUE(field_observer_.on_control_key_changed_called_);
EXPECT_FALSE(field_observer_.on_control_key_changed_value_);
// Test with Control key down.
field_observer_.Reset();
EXPECT_FALSE(field_observer_.on_control_key_changed_called_);
[field_ flagsChanged:KeyDownEventWithFlags(NSControlKeyMask)];
EXPECT_TRUE(field_observer_.on_control_key_changed_called_);
EXPECT_TRUE(field_observer_.on_control_key_changed_value_);
}
// This test is here rather than in the editor's tests because the
// field catches -flagsChanged: because it's on the responder chain,
// the field editor doesn't implement it.
TEST_F(AutocompleteTextFieldTest, FieldEditorFlagsChanged) {
cocoa_helper_.makeFirstResponder(field_);
NSResponder* firstResponder = [[field_ window] firstResponder];
EXPECT_EQ(firstResponder, [field_ currentEditor]);
// Test without Control key down, but some other modifier down.
field_observer_.Reset();
EXPECT_FALSE(field_observer_.on_control_key_changed_called_);
[firstResponder flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)];
EXPECT_TRUE(field_observer_.on_control_key_changed_called_);
EXPECT_FALSE(field_observer_.on_control_key_changed_value_);
// Test with Control key down.
field_observer_.Reset();
EXPECT_FALSE(field_observer_.on_control_key_changed_called_);
[firstResponder flagsChanged:KeyDownEventWithFlags(NSControlKeyMask)];
EXPECT_TRUE(field_observer_.on_control_key_changed_called_);
EXPECT_TRUE(field_observer_.on_control_key_changed_value_);
}
// Test that the field editor is reset correctly when search keyword
// or hints change.
TEST_F(AutocompleteTextFieldTest, ResetFieldEditor) {
EXPECT_EQ(nil, [field_ currentEditor]);
EXPECT_EQ([[field_ subviews] count], 0U);
[[field_ window] makeFirstResponder:field_];
EXPECT_FALSE(nil == [field_ currentEditor]);
EXPECT_EQ([[field_ subviews] count], 1U);
// Check that the window delegate is working right.
{
Class c = [AutocompleteTextFieldEditor class];
EXPECT_TRUE([[field_ currentEditor] isKindOfClass:c]);
}
// The field editor may not be an immediate subview of |field_|, it
// may be a subview of a clipping view (for purposes of scrolling).
// So just look at the immediate subview.
EXPECT_EQ([[field_ subviews] count], 1U);
const NSRect baseEditorFrame([[[field_ subviews] objectAtIndex:0] frame]);
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
EXPECT_FALSE([cell fieldEditorNeedsReset]);
// Asking the cell to add a search hint should leave the field
// editor alone until -resetFieldEditorFrameIfNeeded is called.
// Then the field editor should be moved to a smaller region with
// the same left-hand side.
[cell setSearchHintString:@"Type to search"];
EXPECT_TRUE([cell fieldEditorNeedsReset]);
NSRect r = [[[field_ subviews] objectAtIndex:0] frame];
EXPECT_TRUE(NSEqualRects(r, baseEditorFrame));
[field_ resetFieldEditorFrameIfNeeded];
r = [[[field_ subviews] objectAtIndex:0] frame];
EXPECT_FALSE([cell fieldEditorNeedsReset]);
EXPECT_FALSE(NSEqualRects(r, baseEditorFrame));
EXPECT_TRUE(NSContainsRect(baseEditorFrame, r));
EXPECT_EQ(NSMinX(r), NSMinX(baseEditorFrame));
EXPECT_LT(NSWidth(r), NSWidth(baseEditorFrame));
// Save the search-hint editor frame for later.
const NSRect searchHintEditorFrame(r);
// Asking the cell to change to keyword mode should leave the field
// editor alone until -resetFieldEditorFrameIfNeeded is called.
// Then the field editor should be moved to a smaller region with
// the same right-hand side.
[cell setKeywordString:@"Search Engine:"];
EXPECT_TRUE([cell fieldEditorNeedsReset]);
r = [[[field_ subviews] objectAtIndex:0] frame];
EXPECT_TRUE(NSEqualRects(r, searchHintEditorFrame));
[field_ resetFieldEditorFrameIfNeeded];
r = [[[field_ subviews] objectAtIndex:0] frame];
EXPECT_FALSE([cell fieldEditorNeedsReset]);
EXPECT_FALSE(NSEqualRects(r, baseEditorFrame));
EXPECT_FALSE(NSEqualRects(r, searchHintEditorFrame));
EXPECT_TRUE(NSContainsRect(baseEditorFrame, r));
EXPECT_EQ(NSMaxX(r), NSMaxX(baseEditorFrame));
EXPECT_LT(NSWidth(r), NSWidth(baseEditorFrame));
// Asking the cell to clear everything should leave the field editor
// alone until -resetFieldEditorFrameIfNeeded is called. Then the
// field editor should be back to baseEditorFrame.
[cell clearKeywordAndHint];
EXPECT_TRUE([cell fieldEditorNeedsReset]);
[field_ resetFieldEditorFrameIfNeeded];
r = [[[field_ subviews] objectAtIndex:0] frame];
EXPECT_FALSE([cell fieldEditorNeedsReset]);
EXPECT_TRUE(NSEqualRects(r, baseEditorFrame));
}
// Test that the field editor is reset correctly when search keyword
// or hints change.
TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBlocksEndEditing) {
[[field_ window] makeFirstResponder:field_];
// First, test that -makeFirstResponder: sends
// -controlTextDidBeginEditing: and -control:textShouldEndEditing at
// the expected times.
{
scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate(
[[AutocompleteTextFieldTestDelegate alloc] init]);
EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]);
EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
[field_ setDelegate:delegate];
[[field_ window] makeFirstResponder:field_];
NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]);
EXPECT_TRUE(nil != editor);
EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]);
EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
// This should start the begin/end editing state.
[editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""];
EXPECT_TRUE([delegate receivedControlTextDidBeginEditing]);
EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
// This should send the end-editing message.
[[field_ window] makeFirstResponder:field_];
EXPECT_TRUE([delegate receivedControlTextShouldEndEditing]);
[field_ setDelegate:nil];
}
// Then test that -resetFieldEditorFrameIfNeeded manages without
// sending that message.
{
scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate(
[[AutocompleteTextFieldTestDelegate alloc] init]);
[field_ setDelegate:delegate];
EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]);
EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
EXPECT_FALSE([cell fieldEditorNeedsReset]);
[cell setSearchHintString:@"Type to search"];
EXPECT_TRUE([cell fieldEditorNeedsReset]);
EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
[field_ resetFieldEditorFrameIfNeeded];
EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
EXPECT_TRUE([delegate receivedControlTextDidBeginEditing]);
[field_ setDelegate:nil];
}
}
} // namespace
@implementation AutocompleteTextFieldTestDelegate
- init {
self = [super init];
if (self) {
receivedControlTextDidBeginEditing_ = NO;
receivedControlTextShouldEndEditing_ = NO;
}
return self;
}
- (BOOL)receivedControlTextDidBeginEditing {
return receivedControlTextDidBeginEditing_;
}
- (BOOL)receivedControlTextShouldEndEditing {
return receivedControlTextShouldEndEditing_;
}
- (void)controlTextDidBeginEditing:(NSNotification*)aNotification {
receivedControlTextDidBeginEditing_ = YES;
}
- (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor {
receivedControlTextShouldEndEditing_ = YES;
return YES;
}
@end
|