| 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 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 NSRect frame = [field_ frame]; | 251 NSRect frame = [field_ frame]; |
| 252 frame.size.width += 10.0; | 252 frame.size.width += 10.0; |
| 253 [field_ setFrame:frame]; | 253 [field_ setFrame:frame]; |
| 254 } | 254 } |
| 255 | 255 |
| 256 // Test that the field editor gets the same bounds when focus is | 256 // Test that the field editor gets the same bounds when focus is |
| 257 // delivered by the standard focusing machinery, or by | 257 // delivered by the standard focusing machinery, or by |
| 258 // -resetFieldEditorFrameIfNeeded. | 258 // -resetFieldEditorFrameIfNeeded. |
| 259 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBase) { | 259 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBase) { |
| 260 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 260 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 261 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 262 | 261 |
| 263 // Capture the editor frame resulting from the standard focus | 262 // Capture the editor frame resulting from the standard focus |
| 264 // machinery. | 263 // machinery. |
| 265 cocoa_helper_.makeFirstResponder(field_); | 264 cocoa_helper_.makeFirstResponder(field_); |
| 266 const NSRect baseEditorFrame(EditorFrame()); | 265 const NSRect baseEditorFrame(EditorFrame()); |
| 267 | 266 |
| 268 // Setting a hint should result in a strictly smaller editor frame. | 267 // Setting a hint should result in a strictly smaller editor frame. |
| 268 EXPECT_FALSE([cell hintString]); |
| 269 [cell setSearchHintString:@"search hint" availableWidth:kWidth]; | 269 [cell setSearchHintString:@"search hint" availableWidth:kWidth]; |
| 270 EXPECT_TRUE([cell fieldEditorNeedsReset]); | 270 EXPECT_TRUE([cell hintString]); |
| 271 [field_ resetFieldEditorFrameIfNeeded]; | 271 [field_ resetFieldEditorFrameIfNeeded]; |
| 272 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 273 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); | 272 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); |
| 274 EXPECT_TRUE(NSContainsRect(baseEditorFrame, EditorFrame())); | 273 EXPECT_TRUE(NSContainsRect(baseEditorFrame, EditorFrame())); |
| 275 | 274 |
| 276 // Clearing hint string and using -resetFieldEditorFrameIfNeeded | 275 // Clearing hint string and using -resetFieldEditorFrameIfNeeded |
| 277 // should result in the same frame as the standard focus machinery. | 276 // should result in the same frame as the standard focus machinery. |
| 278 [cell clearKeywordAndHint]; | 277 [cell clearKeywordAndHint]; |
| 279 EXPECT_TRUE([cell fieldEditorNeedsReset]); | 278 EXPECT_FALSE([cell hintString]); |
| 280 [field_ resetFieldEditorFrameIfNeeded]; | 279 [field_ resetFieldEditorFrameIfNeeded]; |
| 281 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 282 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); | 280 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); |
| 283 } | 281 } |
| 284 | 282 |
| 285 // Test that the field editor gets the same bounds when focus is | 283 // Test that the field editor gets the same bounds when focus is |
| 286 // delivered by the standard focusing machinery, or by | 284 // delivered by the standard focusing machinery, or by |
| 287 // -resetFieldEditorFrameIfNeeded. | 285 // -resetFieldEditorFrameIfNeeded. |
| 288 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorSearchHint) { | 286 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorSearchHint) { |
| 289 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 287 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 290 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 291 | 288 |
| 292 const NSString* kHintString(@"Type to search"); | 289 const NSString* kHintString(@"Type to search"); |
| 293 | 290 |
| 294 // Capture the editor frame resulting from the standard focus | 291 // Capture the editor frame resulting from the standard focus |
| 295 // machinery. | 292 // machinery. |
| 296 [cell setSearchHintString:kHintString availableWidth:kWidth]; | 293 [cell setSearchHintString:kHintString availableWidth:kWidth]; |
| 297 EXPECT_TRUE([cell fieldEditorNeedsReset]); | 294 EXPECT_TRUE([cell hintString]); |
| 298 [cell setFieldEditorNeedsReset:NO]; | |
| 299 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 300 cocoa_helper_.makeFirstResponder(field_); | 295 cocoa_helper_.makeFirstResponder(field_); |
| 301 const NSRect baseEditorFrame(EditorFrame()); | 296 const NSRect baseEditorFrame(EditorFrame()); |
| 302 | 297 |
| 303 // Clearing the hint should result in a strictly larger editor | 298 // Clearing the hint should result in a strictly larger editor |
| 304 // frame. | 299 // frame. |
| 305 [cell clearKeywordAndHint]; | 300 [cell clearKeywordAndHint]; |
| 306 EXPECT_TRUE([cell fieldEditorNeedsReset]); | 301 EXPECT_FALSE([cell hintString]); |
| 307 [field_ resetFieldEditorFrameIfNeeded]; | 302 [field_ resetFieldEditorFrameIfNeeded]; |
| 308 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 309 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); | 303 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); |
| 310 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); | 304 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); |
| 311 | 305 |
| 312 // Setting the same hint string and using | 306 // Setting the same hint string and using |
| 313 // -resetFieldEditorFrameIfNeeded should result in the same frame as | 307 // -resetFieldEditorFrameIfNeeded should result in the same frame as |
| 314 // the standard focus machinery. | 308 // the standard focus machinery. |
| 315 [cell setSearchHintString:kHintString availableWidth:kWidth]; | 309 [cell setSearchHintString:kHintString availableWidth:kWidth]; |
| 316 EXPECT_TRUE([cell fieldEditorNeedsReset]); | 310 EXPECT_TRUE([cell hintString]); |
| 317 [field_ resetFieldEditorFrameIfNeeded]; | 311 [field_ resetFieldEditorFrameIfNeeded]; |
| 318 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 319 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); | 312 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); |
| 320 } | 313 } |
| 321 | 314 |
| 322 // Test that the field editor gets the same bounds when focus is | 315 // Test that the field editor gets the same bounds when focus is |
| 323 // delivered by the standard focusing machinery, or by | 316 // delivered by the standard focusing machinery, or by |
| 324 // -resetFieldEditorFrameIfNeeded. | 317 // -resetFieldEditorFrameIfNeeded. |
| 325 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorKeywordHint) { | 318 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorKeywordHint) { |
| 326 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 319 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 327 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 328 | 320 |
| 329 const NSString* kFullString(@"Search Engine:"); | 321 const NSString* kFullString(@"Search Engine:"); |
| 330 const NSString* kPartialString(@"Search Eng:"); | 322 const NSString* kPartialString(@"Search Eng:"); |
| 331 | 323 |
| 332 // Capture the editor frame resulting from the standard focus | 324 // Capture the editor frame resulting from the standard focus |
| 333 // machinery. | 325 // machinery. |
| 334 [cell setKeywordString:kFullString | 326 [cell setKeywordString:kFullString |
| 335 partialString:kPartialString | 327 partialString:kPartialString |
| 336 availableWidth:kWidth]; | 328 availableWidth:kWidth]; |
| 337 EXPECT_TRUE([cell fieldEditorNeedsReset]); | 329 EXPECT_TRUE([cell keywordString]); |
| 338 [cell setFieldEditorNeedsReset:NO]; | |
| 339 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 340 cocoa_helper_.makeFirstResponder(field_); | 330 cocoa_helper_.makeFirstResponder(field_); |
| 341 const NSRect baseEditorFrame(EditorFrame()); | 331 const NSRect baseEditorFrame(EditorFrame()); |
| 342 | 332 |
| 343 // Clearing the hint should result in a strictly larger editor | 333 // Clearing the hint should result in a strictly larger editor |
| 344 // frame. | 334 // frame. |
| 345 [cell clearKeywordAndHint]; | 335 [cell clearKeywordAndHint]; |
| 346 EXPECT_TRUE([cell fieldEditorNeedsReset]); | 336 EXPECT_FALSE([cell keywordString]); |
| 347 [field_ resetFieldEditorFrameIfNeeded]; | 337 [field_ resetFieldEditorFrameIfNeeded]; |
| 348 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 349 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); | 338 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); |
| 350 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); | 339 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); |
| 351 | 340 |
| 352 // Setting the same hint string and using | 341 // Setting the same hint string and using |
| 353 // -resetFieldEditorFrameIfNeeded should result in the same frame as | 342 // -resetFieldEditorFrameIfNeeded should result in the same frame as |
| 354 // the standard focus machinery. | 343 // the standard focus machinery. |
| 355 [cell setKeywordString:kFullString | 344 [cell setKeywordString:kFullString |
| 356 partialString:kPartialString | 345 partialString:kPartialString |
| 357 availableWidth:kWidth]; | 346 availableWidth:kWidth]; |
| 358 EXPECT_TRUE([cell fieldEditorNeedsReset]); | 347 EXPECT_TRUE([cell keywordString]); |
| 359 [field_ resetFieldEditorFrameIfNeeded]; | 348 [field_ resetFieldEditorFrameIfNeeded]; |
| 360 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 361 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); | 349 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); |
| 362 } | 350 } |
| 363 | 351 |
| 364 // Test that resetting the field editor bounds does not cause untoward | 352 // Test that resetting the field editor bounds does not cause untoward |
| 365 // messages to the field's delegate. | 353 // messages to the field's delegate. |
| 366 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBlocksEndEditing) { | 354 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBlocksEndEditing) { |
| 367 // First, test that -makeFirstResponder: sends | 355 // First, test that -makeFirstResponder: sends |
| 368 // -controlTextDidBeginEditing: and -control:textShouldEndEditing at | 356 // -controlTextDidBeginEditing: and -control:textShouldEndEditing at |
| 369 // the expected times. | 357 // the expected times. |
| 370 { | 358 { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 [field_ setDelegate:mockDelegate]; | 391 [field_ setDelegate:mockDelegate]; |
| 404 | 392 |
| 405 // Start editing. | 393 // Start editing. |
| 406 [[mockDelegate expect] controlTextDidBeginEditing:OCMOCK_ANY]; | 394 [[mockDelegate expect] controlTextDidBeginEditing:OCMOCK_ANY]; |
| 407 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); | 395 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); |
| 408 [editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""]; | 396 [editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""]; |
| 409 [mockDelegate verify]; | 397 [mockDelegate verify]; |
| 410 | 398 |
| 411 // No more messages to mockDelegate. | 399 // No more messages to mockDelegate. |
| 412 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 400 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 413 EXPECT_FALSE([cell fieldEditorNeedsReset]); | |
| 414 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; | 401 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; |
| 415 EXPECT_TRUE([cell fieldEditorNeedsReset]); | |
| 416 [field_ resetFieldEditorFrameIfNeeded]; | 402 [field_ resetFieldEditorFrameIfNeeded]; |
| 417 [mockDelegate verify]; | 403 [mockDelegate verify]; |
| 418 | 404 |
| 419 [field_ setDelegate:nil]; | 405 [field_ setDelegate:nil]; |
| 420 } | 406 } |
| 421 } | 407 } |
| 422 | 408 |
| 423 // Clicking in the search hint should put the caret in the rightmost | 409 // Clicking in the search hint should put the caret in the rightmost |
| 424 // position. | 410 // position. |
| 425 TEST_F(AutocompleteTextFieldTest, ClickSearchHintPutsCaretRightmost) { | 411 TEST_F(AutocompleteTextFieldTest, ClickSearchHintPutsCaretRightmost) { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]); | 572 [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]); |
| 587 [cell setHintIcon:hintIcon.get() label:nil color:nil]; | 573 [cell setHintIcon:hintIcon.get() label:nil color:nil]; |
| 588 NSRect iconFrame([cell hintImageFrameForFrame:[field_ bounds]]); | 574 NSRect iconFrame([cell hintImageFrameForFrame:[field_ bounds]]); |
| 589 NSPoint location(NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame))); | 575 NSPoint location(NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame))); |
| 590 NSEvent* event(Event(field_, location, NSLeftMouseDown, 1)); | 576 NSEvent* event(Event(field_, location, NSLeftMouseDown, 1)); |
| 591 EXPECT_CALL(field_observer_, OnSecurityIconClicked()); | 577 EXPECT_CALL(field_observer_, OnSecurityIconClicked()); |
| 592 [field_ mouseDown:event]; | 578 [field_ mouseDown:event]; |
| 593 } | 579 } |
| 594 | 580 |
| 595 } // namespace | 581 } // namespace |
| OLD | NEW |