OLD | NEW |
---|---|
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" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 modifierFlags:flags | 58 modifierFlags:flags |
59 timestamp:0.0 | 59 timestamp:0.0 |
60 windowNumber:[cocoa_helper_.window() windowNumber] | 60 windowNumber:[cocoa_helper_.window() windowNumber] |
61 context:nil | 61 context:nil |
62 characters:@"a" | 62 characters:@"a" |
63 charactersIgnoringModifiers:@"a" | 63 charactersIgnoringModifiers:@"a" |
64 isARepeat:NO | 64 isARepeat:NO |
65 keyCode:'a']; | 65 keyCode:'a']; |
66 } | 66 } |
67 | 67 |
68 // Helper to return the field-editor frame being used w/in |field_|. | |
69 NSRect EditorFrame() { | |
70 EXPECT_TRUE([field_.get() currentEditor]); | |
rohitrao (ping after 24h)
2009/09/23 22:05:35
I want this to be an ASSERT_TRUE, but I have a fee
Scott Hess - ex-Googler
2009/09/23 23:06:26
Yes, ASSERT_TRUE won't work (it's more relevant fo
| |
71 EXPECT_EQ([[field_.get() subviews] count], 1U); | |
72 if ([[field_.get() subviews] count] > 0) { | |
73 return [[[field_.get() subviews] objectAtIndex:0] frame]; | |
74 } else { | |
75 // Return something which won't work so the caller can soldier | |
76 // on. | |
77 return NSZeroRect; | |
78 } | |
79 } | |
80 | |
68 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 81 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
69 scoped_nsobject<AutocompleteTextField> field_; | 82 scoped_nsobject<AutocompleteTextField> field_; |
70 MockAutocompleteTextFieldObserver field_observer_; | 83 MockAutocompleteTextFieldObserver field_observer_; |
71 scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_; | 84 scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_; |
72 }; | 85 }; |
73 | 86 |
74 // Test that we have the right cell class. | 87 // Test that we have the right cell class. |
75 TEST_F(AutocompleteTextFieldTest, CellClass) { | 88 TEST_F(AutocompleteTextFieldTest, CellClass) { |
76 EXPECT_TRUE([[field_ cell] isKindOfClass:[AutocompleteTextFieldCell class]]); | 89 EXPECT_TRUE([[field_ cell] isKindOfClass:[AutocompleteTextFieldCell class]]); |
77 } | 90 } |
78 | 91 |
79 // Test adding/removing from the view hierarchy, mostly to ensure nothing | 92 // Test adding/removing from the view hierarchy, mostly to ensure nothing |
80 // leaks or crashes. | 93 // leaks or crashes. |
81 TEST_F(AutocompleteTextFieldTest, AddRemove) { | 94 TEST_F(AutocompleteTextFieldTest, AddRemove) { |
82 EXPECT_EQ(cocoa_helper_.contentView(), [field_ superview]); | 95 EXPECT_EQ(cocoa_helper_.contentView(), [field_ superview]); |
83 [field_.get() removeFromSuperview]; | 96 [field_.get() removeFromSuperview]; |
84 EXPECT_FALSE([field_ superview]); | 97 EXPECT_FALSE([field_ superview]); |
85 } | 98 } |
86 | 99 |
87 // Test that we get the same cell from -cell and | 100 // Test that we get the same cell from -cell and |
88 // -autocompleteTextFieldCell. | 101 // -autocompleteTextFieldCell. |
89 TEST_F(AutocompleteTextFieldTest, Cell) { | 102 TEST_F(AutocompleteTextFieldTest, Cell) { |
90 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 103 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
91 EXPECT_EQ(cell, [field_ cell]); | 104 EXPECT_EQ(cell, [field_ cell]); |
92 EXPECT_TRUE(cell != nil); | 105 EXPECT_TRUE(cell != nil); |
93 } | 106 } |
94 | 107 |
108 // Test that becoming first responder sets things up correctly. | |
109 TEST_F(AutocompleteTextFieldTest, FirstResponder) { | |
110 EXPECT_EQ(nil, [field_ currentEditor]); | |
111 EXPECT_EQ([[field_ subviews] count], 0U); | |
112 cocoa_helper_.makeFirstResponder(field_); | |
113 EXPECT_FALSE(nil == [field_ currentEditor]); | |
114 EXPECT_EQ([[field_ subviews] count], 1U); | |
115 EXPECT_TRUE([[field_ currentEditor] isDescendantOf:field_.get()]); | |
116 | |
117 // Check that the window delegate is providing the right editor. | |
118 Class c = [AutocompleteTextFieldEditor class]; | |
119 EXPECT_TRUE([[field_ currentEditor] isKindOfClass:c]); | |
120 } | |
121 | |
95 // Test drawing, mostly to ensure nothing leaks or crashes. | 122 // Test drawing, mostly to ensure nothing leaks or crashes. |
96 TEST_F(AutocompleteTextFieldTest, Display) { | 123 TEST_F(AutocompleteTextFieldTest, Display) { |
97 [field_ display]; | 124 [field_ display]; |
98 | 125 |
99 // Test focussed drawing. | 126 // Test focussed drawing. |
100 cocoa_helper_.makeFirstResponder(field_); | 127 cocoa_helper_.makeFirstResponder(field_); |
101 [field_ display]; | 128 [field_ display]; |
102 cocoa_helper_.clearFirstResponder(); | 129 cocoa_helper_.clearFirstResponder(); |
103 | 130 |
104 // Test display of various cell configurations. | 131 // Test display of various cell configurations. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 | 169 |
143 // Test without Control key down, but some other modifier down. | 170 // Test without Control key down, but some other modifier down. |
144 EXPECT_CALL(field_observer_, OnControlKeyChanged(false)); | 171 EXPECT_CALL(field_observer_, OnControlKeyChanged(false)); |
145 [firstResponder flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)]; | 172 [firstResponder flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)]; |
146 | 173 |
147 // Test with Control key down. | 174 // Test with Control key down. |
148 EXPECT_CALL(field_observer_, OnControlKeyChanged(true)); | 175 EXPECT_CALL(field_observer_, OnControlKeyChanged(true)); |
149 [firstResponder flagsChanged:KeyDownEventWithFlags(NSControlKeyMask)]; | 176 [firstResponder flagsChanged:KeyDownEventWithFlags(NSControlKeyMask)]; |
150 } | 177 } |
151 | 178 |
152 // Test that the field editor is reset correctly when search keyword | 179 // Test that the field editor gets the same bounds when focus is |
153 // or hints change. | 180 // delivered by the standard focusing machinery, or by |
154 TEST_F(AutocompleteTextFieldTest, ResetFieldEditor) { | 181 // -resetFieldEditorFrameIfNeeded. |
155 EXPECT_EQ(nil, [field_ currentEditor]); | 182 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBase) { |
156 EXPECT_EQ([[field_ subviews] count], 0U); | |
157 [[field_ window] makeFirstResponder:field_]; | |
158 EXPECT_FALSE(nil == [field_ currentEditor]); | |
159 EXPECT_EQ([[field_ subviews] count], 1U); | |
160 | |
161 // Check that the window delegate is working right. | |
162 { | |
163 Class c = [AutocompleteTextFieldEditor class]; | |
164 EXPECT_TRUE([[field_ currentEditor] isKindOfClass:c]); | |
165 } | |
166 | |
167 // The field editor may not be an immediate subview of |field_|, it | |
168 // may be a subview of a clipping view (for purposes of scrolling). | |
169 // So just look at the immediate subview. | |
170 EXPECT_EQ([[field_ subviews] count], 1U); | |
171 const NSRect baseEditorFrame([[[field_ subviews] objectAtIndex:0] frame]); | |
172 | |
173 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 183 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
174 EXPECT_FALSE([cell fieldEditorNeedsReset]); | 184 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
175 | 185 |
176 // Asking the cell to add a search hint should leave the field | 186 // Capture the editor frame resulting from the standard focus |
177 // editor alone until -resetFieldEditorFrameIfNeeded is called. | 187 // machinery. |
178 // Then the field editor should be moved to a smaller region with | 188 cocoa_helper_.makeFirstResponder(field_); |
179 // the same left-hand side. | 189 const NSRect baseEditorFrame(EditorFrame()); |
180 [cell setSearchHintString:@"Type to search"]; | 190 |
191 // Setting a hint should result in a strictly smaller editor frame. | |
192 [cell setSearchHintString:@"search hint"]; | |
181 EXPECT_TRUE([cell fieldEditorNeedsReset]); | 193 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
182 NSRect r = [[[field_ subviews] objectAtIndex:0] frame]; | |
183 EXPECT_TRUE(NSEqualRects(r, baseEditorFrame)); | |
184 [field_ resetFieldEditorFrameIfNeeded]; | 194 [field_ resetFieldEditorFrameIfNeeded]; |
185 r = [[[field_ subviews] objectAtIndex:0] frame]; | |
186 EXPECT_FALSE([cell fieldEditorNeedsReset]); | 195 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
187 EXPECT_FALSE(NSEqualRects(r, baseEditorFrame)); | 196 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); |
188 EXPECT_TRUE(NSContainsRect(baseEditorFrame, r)); | 197 EXPECT_TRUE(NSContainsRect(baseEditorFrame, EditorFrame())); |
189 EXPECT_EQ(NSMinX(r), NSMinX(baseEditorFrame)); | |
190 EXPECT_LT(NSWidth(r), NSWidth(baseEditorFrame)); | |
191 | 198 |
192 // Save the search-hint editor frame for later. | 199 // Clearing hint string and using -resetFieldEditorFrameIfNeeded |
193 const NSRect searchHintEditorFrame(r); | 200 // should result in the same frame as the standard focus machinery. |
194 | |
195 // Asking the cell to change to keyword mode should leave the field | |
196 // editor alone until -resetFieldEditorFrameIfNeeded is called. | |
197 // Then the field editor should be moved to a smaller region with | |
198 // the same right-hand side. | |
199 [cell setKeywordString:@"Search Engine:"]; | |
200 EXPECT_TRUE([cell fieldEditorNeedsReset]); | |
201 r = [[[field_ subviews] objectAtIndex:0] frame]; | |
202 EXPECT_TRUE(NSEqualRects(r, searchHintEditorFrame)); | |
203 [field_ resetFieldEditorFrameIfNeeded]; | |
204 r = [[[field_ subviews] objectAtIndex:0] frame]; | |
205 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
206 EXPECT_FALSE(NSEqualRects(r, baseEditorFrame)); | |
207 EXPECT_FALSE(NSEqualRects(r, searchHintEditorFrame)); | |
208 EXPECT_TRUE(NSContainsRect(baseEditorFrame, r)); | |
209 EXPECT_EQ(NSMaxX(r), NSMaxX(baseEditorFrame)); | |
210 EXPECT_LT(NSWidth(r), NSWidth(baseEditorFrame)); | |
211 | |
212 // Asking the cell to clear everything should leave the field editor | |
213 // alone until -resetFieldEditorFrameIfNeeded is called. Then the | |
214 // field editor should be back to baseEditorFrame. | |
215 [cell clearKeywordAndHint]; | 201 [cell clearKeywordAndHint]; |
216 EXPECT_TRUE([cell fieldEditorNeedsReset]); | 202 EXPECT_TRUE([cell fieldEditorNeedsReset]); |
217 [field_ resetFieldEditorFrameIfNeeded]; | 203 [field_ resetFieldEditorFrameIfNeeded]; |
218 r = [[[field_ subviews] objectAtIndex:0] frame]; | |
219 EXPECT_FALSE([cell fieldEditorNeedsReset]); | 204 EXPECT_FALSE([cell fieldEditorNeedsReset]); |
220 EXPECT_TRUE(NSEqualRects(r, baseEditorFrame)); | 205 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); |
221 } | 206 } |
222 | 207 |
223 // Test that the field editor is reset correctly when search keyword | 208 // Test that the field editor gets the same bounds when focus is |
224 // or hints change. | 209 // delivered by the standard focusing machinery, or by |
210 // -resetFieldEditorFrameIfNeeded. | |
211 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorSearchHint) { | |
212 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | |
213 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
214 | |
215 const NSString* kHintString(@"Type to search"); | |
216 | |
217 // Capture the editor frame resulting from the standard focus | |
218 // machinery. | |
219 [cell setSearchHintString:kHintString]; | |
220 EXPECT_TRUE([cell fieldEditorNeedsReset]); | |
221 [cell setFieldEditorNeedsReset:NO]; | |
222 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
223 cocoa_helper_.makeFirstResponder(field_); | |
224 const NSRect baseEditorFrame(EditorFrame()); | |
225 | |
226 // Clearing the hint should result in a strictly larger editor | |
227 // frame. | |
228 [cell clearKeywordAndHint]; | |
229 EXPECT_TRUE([cell fieldEditorNeedsReset]); | |
230 [field_ resetFieldEditorFrameIfNeeded]; | |
231 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
232 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); | |
233 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); | |
234 | |
235 // Setting the same hint string and using | |
236 // -resetFieldEditorFrameIfNeeded should result in the same frame as | |
237 // the standard focus machinery. | |
238 [cell setSearchHintString:kHintString]; | |
239 EXPECT_TRUE([cell fieldEditorNeedsReset]); | |
240 [field_ resetFieldEditorFrameIfNeeded]; | |
241 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
242 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); | |
243 } | |
244 | |
245 // Test that the field editor gets the same bounds when focus is | |
246 // delivered by the standard focusing machinery, or by | |
247 // -resetFieldEditorFrameIfNeeded. | |
248 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorKeywordHint) { | |
249 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | |
250 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
251 | |
252 const NSString* kHintString(@"Search Engine:"); | |
253 | |
254 // Capture the editor frame resulting from the standard focus | |
255 // machinery. | |
256 [cell setKeywordString:kHintString]; | |
257 EXPECT_TRUE([cell fieldEditorNeedsReset]); | |
258 [cell setFieldEditorNeedsReset:NO]; | |
259 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
260 cocoa_helper_.makeFirstResponder(field_); | |
261 const NSRect baseEditorFrame(EditorFrame()); | |
262 | |
263 // Clearing the hint should result in a strictly larger editor | |
264 // frame. | |
265 [cell clearKeywordAndHint]; | |
266 EXPECT_TRUE([cell fieldEditorNeedsReset]); | |
267 [field_ resetFieldEditorFrameIfNeeded]; | |
268 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
269 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); | |
270 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); | |
271 | |
272 // Setting the same hint string and using | |
273 // -resetFieldEditorFrameIfNeeded should result in the same frame as | |
274 // the standard focus machinery. | |
275 [cell setKeywordString:kHintString]; | |
276 EXPECT_TRUE([cell fieldEditorNeedsReset]); | |
277 [field_ resetFieldEditorFrameIfNeeded]; | |
278 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
279 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); | |
280 } | |
281 | |
282 // Test that resetting the field editor bounds does not cause untoward | |
283 // messages to the field's delegate. | |
225 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBlocksEndEditing) { | 284 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBlocksEndEditing) { |
226 [[field_ window] makeFirstResponder:field_]; | 285 cocoa_helper_.makeFirstResponder(field_); |
227 | 286 |
228 // First, test that -makeFirstResponder: sends | 287 // First, test that -makeFirstResponder: sends |
229 // -controlTextDidBeginEditing: and -control:textShouldEndEditing at | 288 // -controlTextDidBeginEditing: and -control:textShouldEndEditing at |
230 // the expected times. | 289 // the expected times. |
231 { | 290 { |
232 scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate( | 291 scoped_nsobject<AutocompleteTextFieldTestDelegate> delegate( |
233 [[AutocompleteTextFieldTestDelegate alloc] init]); | 292 [[AutocompleteTextFieldTestDelegate alloc] init]); |
234 EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]); | 293 EXPECT_FALSE([delegate receivedControlTextDidBeginEditing]); |
235 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]); | 294 EXPECT_FALSE([delegate receivedControlTextShouldEndEditing]); |
236 | 295 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 - (void)controlTextDidBeginEditing:(NSNotification*)aNotification { | 356 - (void)controlTextDidBeginEditing:(NSNotification*)aNotification { |
298 receivedControlTextDidBeginEditing_ = YES; | 357 receivedControlTextDidBeginEditing_ = YES; |
299 } | 358 } |
300 | 359 |
301 - (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor { | 360 - (BOOL)control:(NSControl*)control textShouldEndEditing:(NSText*)fieldEditor { |
302 receivedControlTextShouldEndEditing_ = YES; | 361 receivedControlTextShouldEndEditing_ = YES; |
303 return YES; | 362 return YES; |
304 } | 363 } |
305 | 364 |
306 @end | 365 @end |
OLD | NEW |