Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: chrome/browser/cocoa/autocomplete_text_field_unittest.mm

Issue 211030: [Mac] Expose AutocompleteEditViewMac to Cocoa code. (Closed)
Patch Set: Rohit's comments Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #import "base/cocoa_protocols_mac.h" 7 #import "base/cocoa_protocols_mac.h"
8 #include "base/scoped_nsobject.h" 8 #include "base/scoped_nsobject.h"
9 #import "chrome/browser/cocoa/autocomplete_text_field.h" 9 #import "chrome/browser/cocoa/autocomplete_text_field.h"
10 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" 10 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h"
11 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h" 11 #import "chrome/browser/cocoa/autocomplete_text_field_editor.h"
12 #import "chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h"
12 #import "chrome/browser/cocoa/cocoa_test_helper.h" 13 #import "chrome/browser/cocoa/cocoa_test_helper.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h" 15 #include "testing/platform_test.h"
15 16
16 @interface AutocompleteTextFieldTestDelegate : NSObject { 17 @interface AutocompleteTextFieldTestDelegate : NSObject {
17 BOOL textShouldPaste_;
18 BOOL receivedTextShouldPaste_;
19 BOOL receivedFlagsChanged_;
20 BOOL receivedControlTextDidBeginEditing_; 18 BOOL receivedControlTextDidBeginEditing_;
21 BOOL receivedControlTextShouldEndEditing_; 19 BOOL receivedControlTextShouldEndEditing_;
22 } 20 }
23 - initWithTextShouldPaste:(BOOL)flag; 21 - init;
24 - (BOOL)receivedTextShouldPaste;
25 - (BOOL)receivedFlagsChanged;
26 - (BOOL)receivedControlTextDidBeginEditing; 22 - (BOOL)receivedControlTextDidBeginEditing;
27 - (BOOL)receivedControlTextShouldEndEditing; 23 - (BOOL)receivedControlTextShouldEndEditing;
28 @end 24 @end
29 25
30 @interface AutocompleteTextFieldWindowTestDelegate :
31 NSObject<NSWindowDelegate> {
32 scoped_nsobject<AutocompleteTextFieldEditor> editor_;
33 }
34 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject;
35 @end
36
37 namespace { 26 namespace {
38 27
39 class AutocompleteTextFieldTest : public PlatformTest { 28 class AutocompleteTextFieldTest : public PlatformTest {
40 public: 29 public:
41 AutocompleteTextFieldTest() { 30 AutocompleteTextFieldTest() {
42 // Make sure this is wide enough to play games with the cell 31 // Make sure this is wide enough to play games with the cell
43 // decorations. 32 // decorations.
44 NSRect frame = NSMakeRect(0, 0, 300, 30); 33 NSRect frame = NSMakeRect(0, 0, 300, 30);
45 field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]); 34 field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]);
46 [field_ setStringValue:@"Testing"]; 35 [field_ setStringValue:@"Testing"];
36 [field_ setObserver:&field_observer_];
47 [cocoa_helper_.contentView() addSubview:field_.get()]; 37 [cocoa_helper_.contentView() addSubview:field_.get()];
48 38
49 window_delegate_.reset( 39 window_delegate_.reset(
50 [[AutocompleteTextFieldWindowTestDelegate alloc] init]); 40 [[AutocompleteTextFieldWindowTestDelegate alloc] init]);
51 [cocoa_helper_.window() setDelegate:window_delegate_.get()]; 41 [cocoa_helper_.window() setDelegate:window_delegate_.get()];
52 } 42 }
53 43
54 // The removeFromSuperview call is needed to prevent crashes in later tests. 44 // The removeFromSuperview call is needed to prevent crashes in
45 // later tests.
46 // TODO(shess): -removeromSuperview should not be necessary. Fix
47 // it. Also in autocomplete_text_field_editor_unittest.mm.
55 ~AutocompleteTextFieldTest() { 48 ~AutocompleteTextFieldTest() {
56 [cocoa_helper_.window() setDelegate:nil]; 49 [cocoa_helper_.window() setDelegate:nil];
57 [field_ removeFromSuperview]; 50 [field_ removeFromSuperview];
58 } 51 }
59 52
53 NSEvent* KeyDownEventWithFlags(NSUInteger flags) {
54 return [NSEvent keyEventWithType:NSKeyDown
55 location:NSZeroPoint
56 modifierFlags:flags
57 timestamp:0.0
58 windowNumber:[cocoa_helper_.window() windowNumber]
59 context:nil
60 characters:@"a"
61 charactersIgnoringModifiers:@"a"
62 isARepeat:NO
63 keyCode:'a'];
64 }
65
60 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... 66 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc...
61 scoped_nsobject<AutocompleteTextField> field_; 67 scoped_nsobject<AutocompleteTextField> field_;
68 AutocompleteTextFieldObserverMock field_observer_;
62 scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_; 69 scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_;
63 }; 70 };
64 71
65 // Test that we have the right cell class. 72 // Test that we have the right cell class.
66 TEST_F(AutocompleteTextFieldTest, CellClass) { 73 TEST_F(AutocompleteTextFieldTest, CellClass) {
67 EXPECT_TRUE([[field_ cell] isKindOfClass:[AutocompleteTextFieldCell class]]); 74 EXPECT_TRUE([[field_ cell] isKindOfClass:[AutocompleteTextFieldCell class]]);
68 } 75 }
69 76
70 // Test adding/removing from the view hierarchy, mostly to ensure nothing 77 // Test adding/removing from the view hierarchy, mostly to ensure nothing
71 // leaks or crashes. 78 // leaks or crashes.
(...skipping 30 matching lines...) Expand all
102 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix"]; 109 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix"];
103 [field_ display]; 110 [field_ display];
104 111
105 [cell setKeywordString:@"Search Engine:"]; 112 [cell setKeywordString:@"Search Engine:"];
106 [field_ display]; 113 [field_ display];
107 114
108 [cell clearKeywordAndHint]; 115 [cell clearKeywordAndHint];
109 [field_ display]; 116 [field_ display];
110 } 117 }
111 118
112 // Test that -textShouldPaste: properly queries the delegate. 119 TEST_F(AutocompleteTextFieldTest, FlagsChanged) {
113 TEST_F(AutocompleteTextFieldTest, TextShouldPaste) { 120 // Test without Control key down, but some other modifier down.
114 EXPECT_TRUE(![field_ delegate]); 121 field_observer_.Reset();
115 EXPECT_TRUE([field_ textShouldPaste:nil]); 122 EXPECT_FALSE(field_observer_.on_control_key_changed_called_);
123 [field_ flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)];
124 EXPECT_TRUE(field_observer_.on_control_key_changed_called_);
125 EXPECT_FALSE(field_observer_.on_control_key_changed_value_);
116 126
117 scoped_nsobject<AutocompleteTextFieldTestDelegate> shouldPaste( 127 // Test with Control key down.
118 [[AutocompleteTextFieldTestDelegate alloc] initWithTextShouldPaste:YES]); 128 field_observer_.Reset();
119 [field_ setDelegate:shouldPaste]; 129 EXPECT_FALSE(field_observer_.on_control_key_changed_called_);
120 EXPECT_FALSE([shouldPaste receivedTextShouldPaste]); 130 [field_ flagsChanged:KeyDownEventWithFlags(NSControlKeyMask)];
121 EXPECT_TRUE([field_ textShouldPaste:nil]); 131 EXPECT_TRUE(field_observer_.on_control_key_changed_called_);
122 EXPECT_TRUE([shouldPaste receivedTextShouldPaste]); 132 EXPECT_TRUE(field_observer_.on_control_key_changed_value_);
123
124 scoped_nsobject<AutocompleteTextFieldTestDelegate> shouldNotPaste(
125 [[AutocompleteTextFieldTestDelegate alloc] initWithTextShouldPaste:NO]);
126 [field_ setDelegate:shouldNotPaste];
127 EXPECT_FALSE([shouldNotPaste receivedTextShouldPaste]);
128 EXPECT_FALSE([field_ textShouldPaste:nil]);
129 EXPECT_TRUE([shouldNotPaste receivedTextShouldPaste]);
130 [field_ setDelegate:nil];
131 } 133 }
132 134
133 // Test that -control:flagsChanged: properly reaches the delegate. 135 // This test is here rather than in the editor's tests because the
134 TEST_F(AutocompleteTextFieldTest, FlagsChanged) { 136 // field catches -flagsChanged: because it's on the responder chain,
135 EXPECT_TRUE(![field_ delegate]); 137 // the field editor doesn't implement it.
138 TEST_F(AutocompleteTextFieldTest, FieldEditorFlagsChanged) {
139 cocoa_helper_.makeFirstResponder(field_);
140 NSResponder* firstResponder = [[field_ window] firstResponder];
141 EXPECT_EQ(firstResponder, [field_ currentEditor]);
136 142
137 // This shouldn't crash, at least. 143 // Test without Control key down, but some other modifier down.
138 [field_ flagsChanged:nil]; 144 field_observer_.Reset();
145 EXPECT_FALSE(field_observer_.on_control_key_changed_called_);
146 [firstResponder flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)];
147 EXPECT_TRUE(field_observer_.on_control_key_changed_called_);
148 EXPECT_FALSE(field_observer_.on_control_key_changed_value_);
139 149
140 scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate( 150 // Test with Control key down.
141 [[AutocompleteTextFieldTestDelegate alloc] initWithTextShouldPaste:NO]); 151 field_observer_.Reset();
142 [field_ setDelegate:delegate]; 152 EXPECT_FALSE(field_observer_.on_control_key_changed_called_);
143 EXPECT_FALSE([delegate receivedFlagsChanged]); 153 [firstResponder flagsChanged:KeyDownEventWithFlags(NSControlKeyMask)];
144 [field_ flagsChanged:nil]; 154 EXPECT_TRUE(field_observer_.on_control_key_changed_called_);
145 EXPECT_TRUE([delegate receivedFlagsChanged]); 155 EXPECT_TRUE(field_observer_.on_control_key_changed_value_);
146 [field_ setDelegate:nil];
147 }
148
149 // Test that -control:flagsChanged: properly reaches the delegate when
150 // the -flagsChanged: message goes to the editor. In that case it is
151 // forwarded via the responder chain.
152 TEST_F(AutocompleteTextFieldTest, FieldEditorFlagsChanged) {
153 EXPECT_TRUE(![field_ delegate]);
154
155 scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate(
156 [[AutocompleteTextFieldTestDelegate alloc] initWithTextShouldPaste:NO]);
157
158 // Setup a field editor for |field_|.
159 scoped_nsobject<AutocompleteTextFieldEditor> editor(
160 [[AutocompleteTextFieldEditor alloc] init]);
161 [field_ setDelegate:delegate];
162
163 [editor setFieldEditor:YES];
164 [[field_ cell] setUpFieldEditorAttributes:editor];
165 [[field_ cell] editWithFrame:[field_ bounds]
166 inView:field_
167 editor:editor
168 delegate:[field_ delegate]
169 event:nil];
170 EXPECT_FALSE([delegate receivedFlagsChanged]);
171 [editor flagsChanged:nil];
172 EXPECT_TRUE([delegate receivedFlagsChanged]);
173 [field_ setDelegate:nil];
174 } 156 }
175 157
176 // Test that the field editor is reset correctly when search keyword 158 // Test that the field editor is reset correctly when search keyword
177 // or hints change. 159 // or hints change.
178 TEST_F(AutocompleteTextFieldTest, ResetFieldEditor) { 160 TEST_F(AutocompleteTextFieldTest, ResetFieldEditor) {
179 EXPECT_EQ(nil, [field_ currentEditor]); 161 EXPECT_EQ(nil, [field_ currentEditor]);
180 EXPECT_EQ([[field_ subviews] count], 0U); 162 EXPECT_EQ([[field_ subviews] count], 0U);
181 [[field_ window] makeFirstResponder:field_]; 163 [[field_ window] makeFirstResponder:field_];
182 EXPECT_FALSE(nil == [field_ currentEditor]); 164 EXPECT_FALSE(nil == [field_ currentEditor]);
183 EXPECT_EQ([[field_ subviews] count], 1U); 165 EXPECT_EQ([[field_ subviews] count], 1U);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 // Test that the field editor is reset correctly when search keyword 229 // Test that the field editor is reset correctly when search keyword
248 // or hints change. 230 // or hints change.
249 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBlocksEndEditing) { 231 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBlocksEndEditing) {
250 [[field_ window] makeFirstResponder:field_]; 232 [[field_ window] makeFirstResponder:field_];
251 233
252 // First, test that -makeFirstResponder: sends 234 // First, test that -makeFirstResponder: sends
253 // -controlTextDidBeginEditing: and -control:textShouldEndEditing at 235 // -controlTextDidBeginEditing: and -control:textShouldEndEditing at
254 // the expected times. 236 // the expected times.
255 { 237 {
256 scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate( 238 scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate(
257 [[AutocompleteTextFieldTestDelegate alloc] initWithTextShouldPaste:NO]); 239 [[AutocompleteTextFieldTestDelegate alloc] init]);
258 EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]); 240 EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]);
259 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]); 241 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
260 242
261 [field_ setDelegate:delegate]; 243 [field_ setDelegate:delegate];
262 [[field_ window] makeFirstResponder:field_]; 244 [[field_ window] makeFirstResponder:field_];
263 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); 245 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]);
264 EXPECT_TRUE(nil != editor); 246 EXPECT_TRUE(nil != editor);
265 EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]); 247 EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]);
266 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]); 248 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
267 249
268 // This should start the begin/end editing state. 250 // This should start the begin/end editing state.
269 [editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""]; 251 [editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""];
270 EXPECT_TRUE([delegate receivedControlTextDidBeginEditing]); 252 EXPECT_TRUE([delegate receivedControlTextDidBeginEditing]);
271 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]); 253 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
272 254
273 // This should send the end-editing message. 255 // This should send the end-editing message.
274 [[field_ window] makeFirstResponder:field_]; 256 [[field_ window] makeFirstResponder:field_];
275 EXPECT_TRUE([delegate receivedControlTextShouldEndEditing]); 257 EXPECT_TRUE([delegate receivedControlTextShouldEndEditing]);
276 [field_ setDelegate:nil]; 258 [field_ setDelegate:nil];
277 } 259 }
278 260
279 // Then test that -resetFieldEditorFrameIfNeeded manages without 261 // Then test that -resetFieldEditorFrameIfNeeded manages without
280 // sending that message. 262 // sending that message.
281 { 263 {
282 scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate( 264 scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate(
283 [[AutocompleteTextFieldTestDelegate alloc] initWithTextShouldPaste:NO]); 265 [[AutocompleteTextFieldTestDelegate alloc] init]);
284 [field_ setDelegate:delegate]; 266 [field_ setDelegate:delegate];
285 EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]); 267 EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]);
286 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]); 268 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
287 269
288 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; 270 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
289 EXPECT_FALSE([cell fieldEditorNeedsReset]); 271 EXPECT_FALSE([cell fieldEditorNeedsReset]);
290 [cell setSearchHintString:@"Type to search"]; 272 [cell setSearchHintString:@"Type to search"];
291 EXPECT_TRUE([cell fieldEditorNeedsReset]); 273 EXPECT_TRUE([cell fieldEditorNeedsReset]);
292 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]); 274 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
293 [field_ resetFieldEditorFrameIfNeeded]; 275 [field_ resetFieldEditorFrameIfNeeded];
294 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]); 276 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]);
295 EXPECT_TRUE([delegate receivedControlTextDidBeginEditing]); 277 EXPECT_TRUE([delegate receivedControlTextDidBeginEditing]);
296 [field_ setDelegate:nil]; 278 [field_ setDelegate:nil];
297 } 279 }
298 } 280 }
299 281
300 } // namespace 282 } // namespace
301 283
302 @implementation AutocompleteTextFieldTestDelegate 284 @implementation AutocompleteTextFieldTestDelegate
303 285
304 - initWithTextShouldPaste:(BOOL)flag { 286 - init {
305 self = [super init]; 287 self = [super init];
306 if (self) { 288 if (self) {
307 textShouldPaste_ = flag;
308 receivedTextShouldPaste_ = NO;
309 receivedFlagsChanged_ = NO;
310 receivedControlTextDidBeginEditing_ = NO; 289 receivedControlTextDidBeginEditing_ = NO;
311 receivedControlTextShouldEndEditing_ = NO; 290 receivedControlTextShouldEndEditing_ = NO;
312 } 291 }
313 return self; 292 return self;
314 } 293 }
315 294
316 - (BOOL)receivedTextShouldPaste {
317 return receivedTextShouldPaste_;
318 }
319
320 - (BOOL)receivedFlagsChanged {
321 return receivedFlagsChanged_;
322 }
323
324 - (BOOL)receivedControlTextDidBeginEditing { 295 - (BOOL)receivedControlTextDidBeginEditing {
325 return receivedControlTextDidBeginEditing_; 296 return receivedControlTextDidBeginEditing_;
326 } 297 }
327 298
328 - (BOOL)receivedControlTextShouldEndEditing { 299 - (BOOL)receivedControlTextShouldEndEditing {
329 return receivedControlTextShouldEndEditing_; 300 return receivedControlTextShouldEndEditing_;
330 } 301 }
331 302
332 - (BOOL)control:(NSControl*)control textShouldPaste:(NSText*)fieldEditor {
333 receivedTextShouldPaste_ = YES;
334 return textShouldPaste_;
335 }
336
337 - (void)control:(id)control flagsChanged:(NSEvent*)theEvent {
338 receivedFlagsChanged_ = YES;
339 }
340
341 - (void)controlTextDidBeginEditing:(NSNotification*)aNotification { 303 - (void)controlTextDidBeginEditing:(NSNotification*)aNotification {
342 receivedControlTextDidBeginEditing_ = YES; 304 receivedControlTextDidBeginEditing_ = YES;
343 } 305 }
344 306
345 - (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor { 307 - (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor {
346 receivedControlTextShouldEndEditing_ = YES; 308 receivedControlTextShouldEndEditing_ = YES;
347 return YES; 309 return YES;
348 } 310 }
349 311
350 @end 312 @end
351
352 @implementation AutocompleteTextFieldWindowTestDelegate
353
354 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject {
355 EXPECT_TRUE([anObject isKindOfClass:[AutocompleteTextField class]]);
356
357 if (editor_ == nil) {
358 editor_.reset([[AutocompleteTextFieldEditor alloc] init]);
359 }
360 EXPECT_TRUE(editor_ != nil);
361
362 // This needs to be called every time, otherwise notifications
363 // aren't sent correctly.
364 [editor_ setFieldEditor:YES];
365 return editor_;
366 }
367
368 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698