| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 pressure:0.0]; | 61 pressure:0.0]; |
| 62 } | 62 } |
| 63 NSEvent* Event(NSView* view, const NSPoint point, const NSEventType type) { | 63 NSEvent* Event(NSView* view, const NSPoint point, const NSEventType type) { |
| 64 return Event(view, point, type, 1); | 64 return Event(view, point, type, 1); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Width of the field so that we don't have to ask |field_| for it all | 67 // Width of the field so that we don't have to ask |field_| for it all |
| 68 // the time. | 68 // the time. |
| 69 static const CGFloat kWidth(300.0); | 69 static const CGFloat kWidth(300.0); |
| 70 | 70 |
| 71 class AutocompleteTextFieldTest : public PlatformTest { | 71 class AutocompleteTextFieldTest : public CocoaTest { |
| 72 public: | 72 public: |
| 73 AutocompleteTextFieldTest() { | 73 AutocompleteTextFieldTest() { |
| 74 // Make sure this is wide enough to play games with the cell | 74 // Make sure this is wide enough to play games with the cell |
| 75 // decorations. | 75 // decorations. |
| 76 NSRect frame = NSMakeRect(0, 0, kWidth, 30); | 76 NSRect frame = NSMakeRect(0, 0, kWidth, 30); |
| 77 field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]); | 77 scoped_nsobject<AutocompleteTextField> field( |
| 78 [[AutocompleteTextField alloc] initWithFrame:frame]); |
| 79 field_ = field.get(); |
| 78 [field_ setStringValue:@"Test test"]; | 80 [field_ setStringValue:@"Test test"]; |
| 79 [field_ setObserver:&field_observer_]; | 81 [field_ setObserver:&field_observer_]; |
| 80 [cocoa_helper_.contentView() addSubview:field_.get()]; | 82 [[test_window() contentView] addSubview:field_]; |
| 81 | 83 |
| 82 window_delegate_.reset( | 84 window_delegate_.reset( |
| 83 [[AutocompleteTextFieldWindowTestDelegate alloc] init]); | 85 [[AutocompleteTextFieldWindowTestDelegate alloc] init]); |
| 84 [cocoa_helper_.window() setDelegate:window_delegate_.get()]; | 86 [test_window() setDelegate:window_delegate_.get()]; |
| 85 } | |
| 86 | |
| 87 // The removeFromSuperview call is needed to prevent crashes in | |
| 88 // later tests. | |
| 89 // TODO(shess): -removeromSuperview should not be necessary. Fix | |
| 90 // it. Also in autocomplete_text_field_editor_unittest.mm. | |
| 91 ~AutocompleteTextFieldTest() { | |
| 92 [cocoa_helper_.window() setDelegate:nil]; | |
| 93 [field_ removeFromSuperview]; | |
| 94 } | 87 } |
| 95 | 88 |
| 96 NSEvent* KeyDownEventWithFlags(NSUInteger flags) { | 89 NSEvent* KeyDownEventWithFlags(NSUInteger flags) { |
| 97 return [NSEvent keyEventWithType:NSKeyDown | 90 return [NSEvent keyEventWithType:NSKeyDown |
| 98 location:NSZeroPoint | 91 location:NSZeroPoint |
| 99 modifierFlags:flags | 92 modifierFlags:flags |
| 100 timestamp:0.0 | 93 timestamp:0.0 |
| 101 windowNumber:[cocoa_helper_.window() windowNumber] | 94 windowNumber:[test_window() windowNumber] |
| 102 context:nil | 95 context:nil |
| 103 characters:@"a" | 96 characters:@"a" |
| 104 charactersIgnoringModifiers:@"a" | 97 charactersIgnoringModifiers:@"a" |
| 105 isARepeat:NO | 98 isARepeat:NO |
| 106 keyCode:'a']; | 99 keyCode:'a']; |
| 107 } | 100 } |
| 108 | 101 |
| 109 // Helper to return the field-editor frame being used w/in |field_|. | 102 // Helper to return the field-editor frame being used w/in |field_|. |
| 110 NSRect EditorFrame() { | 103 NSRect EditorFrame() { |
| 111 EXPECT_TRUE([field_.get() currentEditor]); | 104 EXPECT_TRUE([field_ currentEditor]); |
| 112 EXPECT_EQ([[field_.get() subviews] count], 1U); | 105 EXPECT_EQ([[field_ subviews] count], 1U); |
| 113 if ([[field_.get() subviews] count] > 0) { | 106 if ([[field_ subviews] count] > 0) { |
| 114 return [[[field_.get() subviews] objectAtIndex:0] frame]; | 107 return [[[field_ subviews] objectAtIndex:0] frame]; |
| 115 } else { | 108 } else { |
| 116 // Return something which won't work so the caller can soldier | 109 // Return something which won't work so the caller can soldier |
| 117 // on. | 110 // on. |
| 118 return NSZeroRect; | 111 return NSZeroRect; |
| 119 } | 112 } |
| 120 } | 113 } |
| 121 | 114 |
| 122 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 115 AutocompleteTextField* field_; |
| 123 scoped_nsobject<AutocompleteTextField> field_; | |
| 124 MockAutocompleteTextFieldObserver field_observer_; | 116 MockAutocompleteTextFieldObserver field_observer_; |
| 125 scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_; | 117 scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_; |
| 126 }; | 118 }; |
| 127 | 119 |
| 120 TEST_VIEW(AutocompleteTextFieldTest, field_); |
| 121 |
| 128 // Test that we have the right cell class. | 122 // Test that we have the right cell class. |
| 129 TEST_F(AutocompleteTextFieldTest, CellClass) { | 123 TEST_F(AutocompleteTextFieldTest, CellClass) { |
| 130 EXPECT_TRUE([[field_ cell] isKindOfClass:[AutocompleteTextFieldCell class]]); | 124 EXPECT_TRUE([[field_ cell] isKindOfClass:[AutocompleteTextFieldCell class]]); |
| 131 } | 125 } |
| 132 | 126 |
| 133 // Test adding/removing from the view hierarchy, mostly to ensure nothing | |
| 134 // leaks or crashes. | |
| 135 TEST_F(AutocompleteTextFieldTest, AddRemove) { | |
| 136 EXPECT_EQ(cocoa_helper_.contentView(), [field_ superview]); | |
| 137 [field_.get() removeFromSuperview]; | |
| 138 EXPECT_FALSE([field_ superview]); | |
| 139 } | |
| 140 | |
| 141 // Test that we get the same cell from -cell and | 127 // Test that we get the same cell from -cell and |
| 142 // -autocompleteTextFieldCell. | 128 // -autocompleteTextFieldCell. |
| 143 TEST_F(AutocompleteTextFieldTest, Cell) { | 129 TEST_F(AutocompleteTextFieldTest, Cell) { |
| 144 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 130 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 145 EXPECT_EQ(cell, [field_ cell]); | 131 EXPECT_EQ(cell, [field_ cell]); |
| 146 EXPECT_TRUE(cell != nil); | 132 EXPECT_TRUE(cell != nil); |
| 147 } | 133 } |
| 148 | 134 |
| 149 // Test that becoming first responder sets things up correctly. | 135 // Test that becoming first responder sets things up correctly. |
| 150 TEST_F(AutocompleteTextFieldTest, FirstResponder) { | 136 TEST_F(AutocompleteTextFieldTest, FirstResponder) { |
| 151 EXPECT_EQ(nil, [field_ currentEditor]); | 137 EXPECT_EQ(nil, [field_ currentEditor]); |
| 152 EXPECT_EQ([[field_ subviews] count], 0U); | 138 EXPECT_EQ([[field_ subviews] count], 0U); |
| 153 cocoa_helper_.makeFirstResponder(field_); | 139 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 154 EXPECT_FALSE(nil == [field_ currentEditor]); | 140 EXPECT_FALSE(nil == [field_ currentEditor]); |
| 155 EXPECT_EQ([[field_ subviews] count], 1U); | 141 EXPECT_EQ([[field_ subviews] count], 1U); |
| 156 EXPECT_TRUE([[field_ currentEditor] isDescendantOf:field_.get()]); | 142 EXPECT_TRUE([[field_ currentEditor] isDescendantOf:field_]); |
| 157 | 143 |
| 158 // Check that the window delegate is providing the right editor. | 144 // Check that the window delegate is providing the right editor. |
| 159 Class c = [AutocompleteTextFieldEditor class]; | 145 Class c = [AutocompleteTextFieldEditor class]; |
| 160 EXPECT_TRUE([[field_ currentEditor] isKindOfClass:c]); | 146 EXPECT_TRUE([[field_ currentEditor] isKindOfClass:c]); |
| 161 } | 147 } |
| 162 | 148 |
| 163 TEST_F(AutocompleteTextFieldTest, AvailableDecorationWidth) { | 149 TEST_F(AutocompleteTextFieldTest, AvailableDecorationWidth) { |
| 164 // A fudge factor to account for how much space the border takes up. | 150 // A fudge factor to account for how much space the border takes up. |
| 165 // The test shouldn't be too dependent on the field's internals, but | 151 // The test shouldn't be too dependent on the field's internals, but |
| 166 // it also shouldn't let deranged cases fall through the cracks | 152 // it also shouldn't let deranged cases fall through the cracks |
| (...skipping 25 matching lines...) Expand all Loading... |
| 192 [field_ setStringValue:string]; | 178 [field_ setStringValue:string]; |
| 193 availableWidth = [field_ availableDecorationWidth]; | 179 availableWidth = [field_ availableDecorationWidth]; |
| 194 EXPECT_LT(availableWidth, 0.0); | 180 EXPECT_LT(availableWidth, 0.0); |
| 195 } | 181 } |
| 196 | 182 |
| 197 // Test drawing, mostly to ensure nothing leaks or crashes. | 183 // Test drawing, mostly to ensure nothing leaks or crashes. |
| 198 TEST_F(AutocompleteTextFieldTest, Display) { | 184 TEST_F(AutocompleteTextFieldTest, Display) { |
| 199 [field_ display]; | 185 [field_ display]; |
| 200 | 186 |
| 201 // Test focussed drawing. | 187 // Test focussed drawing. |
| 202 cocoa_helper_.makeFirstResponder(field_); | 188 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 203 [field_ display]; | 189 [field_ display]; |
| 204 cocoa_helper_.clearFirstResponder(); | 190 [test_window() clearPretendKeyWindowAndFirstResponder]; |
| 205 | 191 |
| 206 // Test display of various cell configurations. | 192 // Test display of various cell configurations. |
| 207 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 193 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 208 | 194 |
| 209 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; | 195 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; |
| 210 [field_ display]; | 196 [field_ display]; |
| 211 | 197 |
| 212 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; | 198 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; |
| 213 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix" | 199 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix" |
| 214 availableWidth:kWidth]; | 200 availableWidth:kWidth]; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 232 | 218 |
| 233 // Test with Control key down. | 219 // Test with Control key down. |
| 234 EXPECT_CALL(field_observer_, OnControlKeyChanged(true)); | 220 EXPECT_CALL(field_observer_, OnControlKeyChanged(true)); |
| 235 [field_ flagsChanged:KeyDownEventWithFlags(NSControlKeyMask)]; | 221 [field_ flagsChanged:KeyDownEventWithFlags(NSControlKeyMask)]; |
| 236 } | 222 } |
| 237 | 223 |
| 238 // This test is here rather than in the editor's tests because the | 224 // This test is here rather than in the editor's tests because the |
| 239 // field catches -flagsChanged: because it's on the responder chain, | 225 // field catches -flagsChanged: because it's on the responder chain, |
| 240 // the field editor doesn't implement it. | 226 // the field editor doesn't implement it. |
| 241 TEST_F(AutocompleteTextFieldTest, FieldEditorFlagsChanged) { | 227 TEST_F(AutocompleteTextFieldTest, FieldEditorFlagsChanged) { |
| 242 cocoa_helper_.makeFirstResponder(field_); | 228 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 243 NSResponder* firstResponder = [[field_ window] firstResponder]; | 229 NSResponder* firstResponder = [[field_ window] firstResponder]; |
| 244 EXPECT_EQ(firstResponder, [field_ currentEditor]); | 230 EXPECT_EQ(firstResponder, [field_ currentEditor]); |
| 245 | 231 |
| 246 InSequence dummy; // Call mock in exactly the order specified. | 232 InSequence dummy; // Call mock in exactly the order specified. |
| 247 | 233 |
| 248 // Test without Control key down, but some other modifier down. | 234 // Test without Control key down, but some other modifier down. |
| 249 EXPECT_CALL(field_observer_, OnControlKeyChanged(false)); | 235 EXPECT_CALL(field_observer_, OnControlKeyChanged(false)); |
| 250 [firstResponder flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)]; | 236 [firstResponder flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)]; |
| 251 | 237 |
| 252 // Test with Control key down. | 238 // Test with Control key down. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 263 } | 249 } |
| 264 | 250 |
| 265 // Test that the field editor gets the same bounds when focus is | 251 // Test that the field editor gets the same bounds when focus is |
| 266 // delivered by the standard focusing machinery, or by | 252 // delivered by the standard focusing machinery, or by |
| 267 // -resetFieldEditorFrameIfNeeded. | 253 // -resetFieldEditorFrameIfNeeded. |
| 268 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBase) { | 254 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBase) { |
| 269 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 255 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 270 | 256 |
| 271 // Capture the editor frame resulting from the standard focus | 257 // Capture the editor frame resulting from the standard focus |
| 272 // machinery. | 258 // machinery. |
| 273 cocoa_helper_.makeFirstResponder(field_); | 259 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 274 const NSRect baseEditorFrame(EditorFrame()); | 260 const NSRect baseEditorFrame(EditorFrame()); |
| 275 | 261 |
| 276 // Setting a hint should result in a strictly smaller editor frame. | 262 // Setting a hint should result in a strictly smaller editor frame. |
| 277 EXPECT_FALSE([cell hintString]); | 263 EXPECT_FALSE([cell hintString]); |
| 278 [cell setSearchHintString:@"search hint" availableWidth:kWidth]; | 264 [cell setSearchHintString:@"search hint" availableWidth:kWidth]; |
| 279 EXPECT_TRUE([cell hintString]); | 265 EXPECT_TRUE([cell hintString]); |
| 280 [field_ resetFieldEditorFrameIfNeeded]; | 266 [field_ resetFieldEditorFrameIfNeeded]; |
| 281 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); | 267 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); |
| 282 EXPECT_TRUE(NSContainsRect(baseEditorFrame, EditorFrame())); | 268 EXPECT_TRUE(NSContainsRect(baseEditorFrame, EditorFrame())); |
| 283 | 269 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 294 // -resetFieldEditorFrameIfNeeded. | 280 // -resetFieldEditorFrameIfNeeded. |
| 295 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorSearchHint) { | 281 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorSearchHint) { |
| 296 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 282 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 297 | 283 |
| 298 const NSString* kHintString(@"Type to search"); | 284 const NSString* kHintString(@"Type to search"); |
| 299 | 285 |
| 300 // Capture the editor frame resulting from the standard focus | 286 // Capture the editor frame resulting from the standard focus |
| 301 // machinery. | 287 // machinery. |
| 302 [cell setSearchHintString:kHintString availableWidth:kWidth]; | 288 [cell setSearchHintString:kHintString availableWidth:kWidth]; |
| 303 EXPECT_TRUE([cell hintString]); | 289 EXPECT_TRUE([cell hintString]); |
| 304 cocoa_helper_.makeFirstResponder(field_); | 290 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 305 const NSRect baseEditorFrame(EditorFrame()); | 291 const NSRect baseEditorFrame(EditorFrame()); |
| 306 | 292 |
| 307 // Clearing the hint should result in a strictly larger editor | 293 // Clearing the hint should result in a strictly larger editor |
| 308 // frame. | 294 // frame. |
| 309 [cell clearKeywordAndHint]; | 295 [cell clearKeywordAndHint]; |
| 310 EXPECT_FALSE([cell hintString]); | 296 EXPECT_FALSE([cell hintString]); |
| 311 [field_ resetFieldEditorFrameIfNeeded]; | 297 [field_ resetFieldEditorFrameIfNeeded]; |
| 312 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); | 298 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); |
| 313 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); | 299 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); |
| 314 | 300 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 329 | 315 |
| 330 const NSString* kFullString(@"Search Engine:"); | 316 const NSString* kFullString(@"Search Engine:"); |
| 331 const NSString* kPartialString(@"Search Eng:"); | 317 const NSString* kPartialString(@"Search Eng:"); |
| 332 | 318 |
| 333 // Capture the editor frame resulting from the standard focus | 319 // Capture the editor frame resulting from the standard focus |
| 334 // machinery. | 320 // machinery. |
| 335 [cell setKeywordString:kFullString | 321 [cell setKeywordString:kFullString |
| 336 partialString:kPartialString | 322 partialString:kPartialString |
| 337 availableWidth:kWidth]; | 323 availableWidth:kWidth]; |
| 338 EXPECT_TRUE([cell keywordString]); | 324 EXPECT_TRUE([cell keywordString]); |
| 339 cocoa_helper_.makeFirstResponder(field_); | 325 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 340 const NSRect baseEditorFrame(EditorFrame()); | 326 const NSRect baseEditorFrame(EditorFrame()); |
| 341 | 327 |
| 342 // Clearing the hint should result in a strictly larger editor | 328 // Clearing the hint should result in a strictly larger editor |
| 343 // frame. | 329 // frame. |
| 344 [cell clearKeywordAndHint]; | 330 [cell clearKeywordAndHint]; |
| 345 EXPECT_FALSE([cell keywordString]); | 331 EXPECT_FALSE([cell keywordString]); |
| 346 [field_ resetFieldEditorFrameIfNeeded]; | 332 [field_ resetFieldEditorFrameIfNeeded]; |
| 347 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); | 333 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); |
| 348 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); | 334 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); |
| 349 | 335 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 364 // First, test that -makeFirstResponder: sends | 350 // First, test that -makeFirstResponder: sends |
| 365 // -controlTextDidBeginEditing: and -control:textShouldEndEditing at | 351 // -controlTextDidBeginEditing: and -control:textShouldEndEditing at |
| 366 // the expected times. | 352 // the expected times. |
| 367 { | 353 { |
| 368 id mockDelegate = | 354 id mockDelegate = |
| 369 [OCMockObject mockForProtocol:@protocol(MockTextEditingDelegate)]; | 355 [OCMockObject mockForProtocol:@protocol(MockTextEditingDelegate)]; |
| 370 | 356 |
| 371 [field_ setDelegate:mockDelegate]; | 357 [field_ setDelegate:mockDelegate]; |
| 372 | 358 |
| 373 // Becoming first responder doesn't begin editing. | 359 // Becoming first responder doesn't begin editing. |
| 374 cocoa_helper_.makeFirstResponder(field_); | 360 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 375 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); | 361 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); |
| 376 EXPECT_TRUE(nil != editor); | 362 EXPECT_TRUE(nil != editor); |
| 377 [mockDelegate verify]; | 363 [mockDelegate verify]; |
| 378 | 364 |
| 379 // This should begin editing. | 365 // This should begin editing. |
| 380 [[mockDelegate expect] controlTextDidBeginEditing:OCMOCK_ANY]; | 366 [[mockDelegate expect] controlTextDidBeginEditing:OCMOCK_ANY]; |
| 381 [editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""]; | 367 [editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""]; |
| 382 [mockDelegate verify]; | 368 [mockDelegate verify]; |
| 383 | 369 |
| 384 // Changing first responder ends editing. | 370 // Changing first responder ends editing. |
| 385 BOOL yes = YES; | 371 BOOL yes = YES; |
| 386 [[[mockDelegate expect] andReturnValue:OCMOCK_VALUE(yes)] | 372 [[[mockDelegate expect] andReturnValue:OCMOCK_VALUE(yes)] |
| 387 control:OCMOCK_ANY textShouldEndEditing:OCMOCK_ANY]; | 373 control:OCMOCK_ANY textShouldEndEditing:OCMOCK_ANY]; |
| 388 cocoa_helper_.makeFirstResponder(field_); | 374 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 389 [mockDelegate verify]; | 375 [mockDelegate verify]; |
| 390 | 376 |
| 391 [field_ setDelegate:nil]; | 377 [field_ setDelegate:nil]; |
| 392 } | 378 } |
| 393 | 379 |
| 394 // Test that -resetFieldEditorFrameIfNeeded manages to rearrange the | 380 // Test that -resetFieldEditorFrameIfNeeded manages to rearrange the |
| 395 // editor without ending editing. | 381 // editor without ending editing. |
| 396 { | 382 { |
| 397 id mockDelegate = | 383 id mockDelegate = |
| 398 [OCMockObject mockForProtocol:@protocol(MockTextEditingDelegate)]; | 384 [OCMockObject mockForProtocol:@protocol(MockTextEditingDelegate)]; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 418 // Clicking in the search hint should put the caret in the rightmost | 404 // Clicking in the search hint should put the caret in the rightmost |
| 419 // position. | 405 // position. |
| 420 TEST_F(AutocompleteTextFieldTest, ClickSearchHintPutsCaretRightmost) { | 406 TEST_F(AutocompleteTextFieldTest, ClickSearchHintPutsCaretRightmost) { |
| 421 // Set the decoration before becoming responder. | 407 // Set the decoration before becoming responder. |
| 422 EXPECT_FALSE([field_ currentEditor]); | 408 EXPECT_FALSE([field_ currentEditor]); |
| 423 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 409 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 424 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; | 410 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; |
| 425 | 411 |
| 426 // Can't rely on the window machinery to make us first responder, | 412 // Can't rely on the window machinery to make us first responder, |
| 427 // here. | 413 // here. |
| 428 cocoa_helper_.makeFirstResponder(field_); | 414 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 429 EXPECT_TRUE([field_ currentEditor]); | 415 EXPECT_TRUE([field_ currentEditor]); |
| 430 | 416 |
| 431 const NSPoint point(NSMakePoint(300.0 - 20.0, 5.0)); | 417 const NSPoint point(NSMakePoint(300.0 - 20.0, 5.0)); |
| 432 NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); | 418 NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); |
| 433 NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); | 419 NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); |
| 434 [NSApp postEvent:upEvent atStart:YES]; | 420 [NSApp postEvent:upEvent atStart:YES]; |
| 435 [field_ mouseDown:downEvent]; | 421 [field_ mouseDown:downEvent]; |
| 436 const NSRange selectedRange([[field_ currentEditor] selectedRange]); | 422 const NSRange selectedRange([[field_ currentEditor] selectedRange]); |
| 437 EXPECT_EQ(selectedRange.location, [[field_ stringValue] length]); | 423 EXPECT_EQ(selectedRange.location, [[field_ stringValue] length]); |
| 438 EXPECT_EQ(selectedRange.length, 0U); | 424 EXPECT_EQ(selectedRange.length, 0U); |
| 439 } | 425 } |
| 440 | 426 |
| 441 // Clicking in the keyword-search should put the caret in the | 427 // Clicking in the keyword-search should put the caret in the |
| 442 // leftmost position. | 428 // leftmost position. |
| 443 TEST_F(AutocompleteTextFieldTest, ClickKeywordPutsCaretLeftmost) { | 429 TEST_F(AutocompleteTextFieldTest, ClickKeywordPutsCaretLeftmost) { |
| 444 // Set the decoration before becoming responder. | 430 // Set the decoration before becoming responder. |
| 445 EXPECT_FALSE([field_ currentEditor]); | 431 EXPECT_FALSE([field_ currentEditor]); |
| 446 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 432 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 447 [cell setKeywordString:@"Search Engine:" | 433 [cell setKeywordString:@"Search Engine:" |
| 448 partialString:@"Search:" | 434 partialString:@"Search:" |
| 449 availableWidth:kWidth]; | 435 availableWidth:kWidth]; |
| 450 | 436 |
| 451 // Can't rely on the window machinery to make us first responder, | 437 // Can't rely on the window machinery to make us first responder, |
| 452 // here. | 438 // here. |
| 453 cocoa_helper_.makeFirstResponder(field_); | 439 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 454 EXPECT_TRUE([field_ currentEditor]); | 440 EXPECT_TRUE([field_ currentEditor]); |
| 455 | 441 |
| 456 const NSPoint point(NSMakePoint(20.0, 5.0)); | 442 const NSPoint point(NSMakePoint(20.0, 5.0)); |
| 457 NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); | 443 NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); |
| 458 NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); | 444 NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); |
| 459 [NSApp postEvent:upEvent atStart:YES]; | 445 [NSApp postEvent:upEvent atStart:YES]; |
| 460 [field_ mouseDown:downEvent]; | 446 [field_ mouseDown:downEvent]; |
| 461 const NSRange selectedRange([[field_ currentEditor] selectedRange]); | 447 const NSRange selectedRange([[field_ currentEditor] selectedRange]); |
| 462 EXPECT_EQ(selectedRange.location, 0U); | 448 EXPECT_EQ(selectedRange.location, 0U); |
| 463 EXPECT_EQ(selectedRange.length, 0U); | 449 EXPECT_EQ(selectedRange.length, 0U); |
| 464 } | 450 } |
| 465 | 451 |
| 466 // Clicks not in the text area or the cell's decorations fall through | 452 // Clicks not in the text area or the cell's decorations fall through |
| 467 // to the editor. | 453 // to the editor. |
| 468 TEST_F(AutocompleteTextFieldTest, ClickBorderSelectsAll) { | 454 TEST_F(AutocompleteTextFieldTest, ClickBorderSelectsAll) { |
| 469 // Can't rely on the window machinery to make us first responder, | 455 // Can't rely on the window machinery to make us first responder, |
| 470 // here. | 456 // here. |
| 471 cocoa_helper_.makeFirstResponder(field_); | 457 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 472 EXPECT_TRUE([field_ currentEditor]); | 458 EXPECT_TRUE([field_ currentEditor]); |
| 473 | 459 |
| 474 const NSPoint point(NSMakePoint(20.0, 1.0)); | 460 const NSPoint point(NSMakePoint(20.0, 1.0)); |
| 475 NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); | 461 NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); |
| 476 NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); | 462 NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); |
| 477 [NSApp postEvent:upEvent atStart:YES]; | 463 [NSApp postEvent:upEvent atStart:YES]; |
| 478 [field_ mouseDown:downEvent]; | 464 [field_ mouseDown:downEvent]; |
| 479 | 465 |
| 480 // Clicking in the narrow border area around a Cocoa NSTextField | 466 // Clicking in the narrow border area around a Cocoa NSTextField |
| 481 // does a select-all. Regardless of whether this is a good call, it | 467 // does a select-all. Regardless of whether this is a good call, it |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 attributes:attributes]); | 600 attributes:attributes]); |
| 615 | 601 |
| 616 // Check that what we get back looks like what we put in. | 602 // Check that what we get back looks like what we put in. |
| 617 EXPECT_FALSE([[field_ stringValue] isEqualToString:kString]); | 603 EXPECT_FALSE([[field_ stringValue] isEqualToString:kString]); |
| 618 [field_ setAttributedStringValue:attributedString]; | 604 [field_ setAttributedStringValue:attributedString]; |
| 619 EXPECT_TRUE([[field_ attributedStringValue] | 605 EXPECT_TRUE([[field_ attributedStringValue] |
| 620 isEqualToAttributedString:attributedString]); | 606 isEqualToAttributedString:attributedString]); |
| 621 EXPECT_TRUE([[field_ stringValue] isEqualToString:kString]); | 607 EXPECT_TRUE([[field_ stringValue] isEqualToString:kString]); |
| 622 | 608 |
| 623 // Try that again with focus. | 609 // Try that again with focus. |
| 624 cocoa_helper_.makeFirstResponder(field_); | 610 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 611 |
| 625 EXPECT_TRUE([field_ currentEditor]); | 612 EXPECT_TRUE([field_ currentEditor]); |
| 626 | 613 |
| 627 // Check that what we get back looks like what we put in. | 614 // Check that what we get back looks like what we put in. |
| 628 [field_ setStringValue:@""]; | 615 [field_ setStringValue:@""]; |
| 629 EXPECT_FALSE([[field_ stringValue] isEqualToString:kString]); | 616 EXPECT_FALSE([[field_ stringValue] isEqualToString:kString]); |
| 630 [field_ setAttributedStringValue:attributedString]; | 617 [field_ setAttributedStringValue:attributedString]; |
| 631 EXPECT_TRUE([[field_ attributedStringValue] | 618 EXPECT_TRUE([[field_ attributedStringValue] |
| 632 isEqualToAttributedString:attributedString]); | 619 isEqualToAttributedString:attributedString]); |
| 633 EXPECT_TRUE([[field_ stringValue] isEqualToString:kString]); | 620 EXPECT_TRUE([[field_ stringValue] isEqualToString:kString]); |
| 634 } | 621 } |
| 635 | 622 |
| 636 // -setAttributedStringValue: shouldn't reset the undo state if things | 623 // -setAttributedStringValue: shouldn't reset the undo state if things |
| 637 // are being editted. | 624 // are being editted. |
| 638 TEST_F(AutocompleteTextFieldTest, SetAttributedStringUndo) { | 625 TEST_F(AutocompleteTextFieldTest, SetAttributedStringUndo) { |
| 639 NSColor* redColor = [NSColor redColor]; | 626 NSColor* redColor = [NSColor redColor]; |
| 640 NSDictionary* attributes = | 627 NSDictionary* attributes = |
| 641 [NSDictionary dictionaryWithObject:redColor | 628 [NSDictionary dictionaryWithObject:redColor |
| 642 forKey:NSForegroundColorAttributeName]; | 629 forKey:NSForegroundColorAttributeName]; |
| 643 static const NSString* kString = @"This is a test"; | 630 static const NSString* kString = @"This is a test"; |
| 644 scoped_nsobject<NSAttributedString> attributedString( | 631 scoped_nsobject<NSAttributedString> attributedString( |
| 645 [[NSAttributedString alloc] initWithString:kString | 632 [[NSAttributedString alloc] initWithString:kString |
| 646 attributes:attributes]); | 633 attributes:attributes]); |
| 647 | 634 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 648 cocoa_helper_.makeFirstResponder(field_); | |
| 649 EXPECT_TRUE([field_ currentEditor]); | 635 EXPECT_TRUE([field_ currentEditor]); |
| 650 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); | 636 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); |
| 651 NSUndoManager* undoManager = [editor undoManager]; | 637 NSUndoManager* undoManager = [editor undoManager]; |
| 652 EXPECT_TRUE(undoManager); | 638 EXPECT_TRUE(undoManager); |
| 653 | 639 |
| 654 // Nothing to undo, yet. | 640 // Nothing to undo, yet. |
| 655 EXPECT_FALSE([undoManager canUndo]); | 641 EXPECT_FALSE([undoManager canUndo]); |
| 656 | 642 |
| 657 // Starting an editing action creates an undoable item. | 643 // Starting an editing action creates an undoable item. |
| 658 [editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""]; | 644 [editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""]; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 671 EXPECT_TRUE([undoManager canUndo]); | 657 EXPECT_TRUE([undoManager canUndo]); |
| 672 [field_ setAttributedStringValue:attributedString]; | 658 [field_ setAttributedStringValue:attributedString]; |
| 673 EXPECT_TRUE([undoManager canUndo]); | 659 EXPECT_TRUE([undoManager canUndo]); |
| 674 | 660 |
| 675 // Verify that calling -clearUndoChain clears the undo chain. | 661 // Verify that calling -clearUndoChain clears the undo chain. |
| 676 [field_ clearUndoChain]; | 662 [field_ clearUndoChain]; |
| 677 EXPECT_FALSE([undoManager canUndo]); | 663 EXPECT_FALSE([undoManager canUndo]); |
| 678 } | 664 } |
| 679 | 665 |
| 680 TEST_F(AutocompleteTextFieldTest, EditorGetsCorrectUndoManager) { | 666 TEST_F(AutocompleteTextFieldTest, EditorGetsCorrectUndoManager) { |
| 681 cocoa_helper_.makeFirstResponder(field_); | 667 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; |
| 682 | 668 |
| 683 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); | 669 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); |
| 684 EXPECT_TRUE(editor); | 670 EXPECT_TRUE(editor); |
| 685 EXPECT_EQ([field_ undoManagerForTextView:editor], [editor undoManager]); | 671 EXPECT_EQ([field_ undoManagerForTextView:editor], [editor undoManager]); |
| 686 } | 672 } |
| 687 } // namespace | 673 } // namespace |
| OLD | NEW |