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

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

Issue 2576563002: [Mac] Reverse the omnibox in RTL (Closed)
Patch Set: Review comments Created 4 years 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <ApplicationServices/ApplicationServices.h> 5 #import <ApplicationServices/ApplicationServices.h>
6 #import <Cocoa/Cocoa.h> 6 #import <Cocoa/Cocoa.h>
7 7
8 #include "base/mac/foundation_util.h" 8 #include "base/mac/foundation_util.h"
9 #include "base/mac/scoped_nsobject.h" 9 #include "base/mac/scoped_nsobject.h"
10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" 10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 NSRect frame = NSMakeRect(0, 0, kWidth, 30); 78 NSRect frame = NSMakeRect(0, 0, kWidth, 30);
79 base::scoped_nsobject<AutocompleteTextField> field( 79 base::scoped_nsobject<AutocompleteTextField> field(
80 [[AutocompleteTextField alloc] initWithFrame:frame]); 80 [[AutocompleteTextField alloc] initWithFrame:frame]);
81 field_ = field.get(); 81 field_ = field.get();
82 [field_ setStringValue:@"Test test"]; 82 [field_ setStringValue:@"Test test"];
83 [[test_window() contentView] addSubview:field_]; 83 [[test_window() contentView] addSubview:field_];
84 84
85 AutocompleteTextFieldCell* cell = [field_ cell]; 85 AutocompleteTextFieldCell* cell = [field_ cell];
86 [cell clearDecorations]; 86 [cell clearDecorations];
87 87
88 mock_left_decoration_.SetVisible(false); 88 mock_leading_decoration_.SetVisible(false);
89 [cell addLeftDecoration:&mock_left_decoration_]; 89 [cell addLeadingDecoration:&mock_leading_decoration_];
90 90
91 mock_right_decoration_.SetVisible(false); 91 mock_trailing_decoration_.SetVisible(false);
92 [cell addRightDecoration:&mock_right_decoration_]; 92 [cell addTrailingDecoration:&mock_trailing_decoration_];
93 93
94 window_delegate_.reset( 94 window_delegate_.reset(
95 [[AutocompleteTextFieldWindowTestDelegate alloc] init]); 95 [[AutocompleteTextFieldWindowTestDelegate alloc] init]);
96 [test_window() setDelegate:window_delegate_.get()]; 96 [test_window() setDelegate:window_delegate_.get()];
97 } 97 }
98 98
99 NSEvent* KeyDownEventWithFlags(NSUInteger flags) { 99 NSEvent* KeyDownEventWithFlags(NSUInteger flags) {
100 return [NSEvent keyEventWithType:NSKeyDown 100 return [NSEvent keyEventWithType:NSKeyDown
101 location:NSZeroPoint 101 location:NSZeroPoint
102 modifierFlags:flags 102 modifierFlags:flags
(...skipping 18 matching lines...) Expand all
121 return NSZeroRect; 121 return NSZeroRect;
122 } 122 }
123 } 123 }
124 124
125 AutocompleteTextFieldEditor* FieldEditor() { 125 AutocompleteTextFieldEditor* FieldEditor() {
126 return base::mac::ObjCCastStrict<AutocompleteTextFieldEditor>( 126 return base::mac::ObjCCastStrict<AutocompleteTextFieldEditor>(
127 [field_ currentEditor]); 127 [field_ currentEditor]);
128 } 128 }
129 129
130 AutocompleteTextField* field_; 130 AutocompleteTextField* field_;
131 MockDecoration mock_left_decoration_; 131 MockDecoration mock_leading_decoration_;
132 MockDecoration mock_right_decoration_; 132 MockDecoration mock_trailing_decoration_;
133 base::scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> 133 base::scoped_nsobject<AutocompleteTextFieldWindowTestDelegate>
134 window_delegate_; 134 window_delegate_;
135 }; 135 };
136 136
137 TEST_VIEW(AutocompleteTextFieldTest, field_); 137 TEST_VIEW(AutocompleteTextFieldTest, field_);
138 138
139 // Base class for testing AutocompleteTextFieldObserver messages. 139 // Base class for testing AutocompleteTextFieldObserver messages.
140 class AutocompleteTextFieldObserverTest : public AutocompleteTextFieldTest { 140 class AutocompleteTextFieldObserverTest : public AutocompleteTextFieldTest {
141 public: 141 public:
142 virtual void SetUp() { 142 virtual void SetUp() {
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 // Test that the field editor gets the same bounds when focus is 289 // Test that the field editor gets the same bounds when focus is
290 // delivered by the standard focusing machinery, or by 290 // delivered by the standard focusing machinery, or by
291 // -resetFieldEditorFrameIfNeeded. 291 // -resetFieldEditorFrameIfNeeded.
292 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBase) { 292 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBase) {
293 // Capture the editor frame resulting from the standard focus 293 // Capture the editor frame resulting from the standard focus
294 // machinery. 294 // machinery.
295 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 295 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
296 const NSRect baseEditorFrame = EditorFrame(); 296 const NSRect baseEditorFrame = EditorFrame();
297 297
298 // A decoration should result in a strictly smaller editor frame. 298 // A decoration should result in a strictly smaller editor frame.
299 mock_left_decoration_.SetVisible(true); 299 mock_leading_decoration_.SetVisible(true);
300 [field_ resetFieldEditorFrameIfNeeded]; 300 [field_ resetFieldEditorFrameIfNeeded];
301 EXPECT_NSNE(baseEditorFrame, EditorFrame()); 301 EXPECT_NSNE(baseEditorFrame, EditorFrame());
302 EXPECT_TRUE(NSContainsRect(baseEditorFrame, EditorFrame())); 302 EXPECT_TRUE(NSContainsRect(baseEditorFrame, EditorFrame()));
303 303
304 // Removing the decoration and using -resetFieldEditorFrameIfNeeded 304 // Removing the decoration and using -resetFieldEditorFrameIfNeeded
305 // should result in the same frame as the standard focus machinery. 305 // should result in the same frame as the standard focus machinery.
306 mock_left_decoration_.SetVisible(false); 306 mock_leading_decoration_.SetVisible(false);
307 [field_ resetFieldEditorFrameIfNeeded]; 307 [field_ resetFieldEditorFrameIfNeeded];
308 EXPECT_NSEQ(baseEditorFrame, EditorFrame()); 308 EXPECT_NSEQ(baseEditorFrame, EditorFrame());
309 } 309 }
310 310
311 // Test that the field editor gets the same bounds when focus is 311 // Test that the field editor gets the same bounds when focus is
312 // delivered by the standard focusing machinery, or by 312 // delivered by the standard focusing machinery, or by
313 // -resetFieldEditorFrameIfNeeded, this time with a decoration 313 // -resetFieldEditorFrameIfNeeded, this time with a decoration
314 // pre-loaded. 314 // pre-loaded.
315 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorWithDecoration) { 315 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorWithDecoration) {
316 AutocompleteTextFieldCell* cell = [field_ cell]; 316 AutocompleteTextFieldCell* cell = [field_ cell];
317 317
318 // Make sure decoration isn't already visible, then make it visible. 318 // Make sure decoration isn't already visible, then make it visible.
319 EXPECT_TRUE(NSIsEmptyRect([cell frameForDecoration:&mock_left_decoration_ 319 EXPECT_TRUE(NSIsEmptyRect([cell frameForDecoration:&mock_leading_decoration_
320 inFrame:[field_ bounds]])); 320 inFrame:[field_ bounds]]));
321 mock_left_decoration_.SetVisible(true); 321 mock_leading_decoration_.SetVisible(true);
322 EXPECT_FALSE(NSIsEmptyRect([cell frameForDecoration:&mock_left_decoration_ 322 EXPECT_FALSE(NSIsEmptyRect([cell frameForDecoration:&mock_leading_decoration_
323 inFrame:[field_ bounds]])); 323 inFrame:[field_ bounds]]));
324 324
325 // Capture the editor frame resulting from the standard focus 325 // Capture the editor frame resulting from the standard focus
326 // machinery. 326 // machinery.
327 327
328 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 328 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
329 const NSRect baseEditorFrame = EditorFrame(); 329 const NSRect baseEditorFrame = EditorFrame();
330 330
331 // When the decoration is not visible the frame should be strictly larger. 331 // When the decoration is not visible the frame should be strictly larger.
332 mock_left_decoration_.SetVisible(false); 332 mock_leading_decoration_.SetVisible(false);
333 EXPECT_TRUE(NSIsEmptyRect([cell frameForDecoration:&mock_left_decoration_ 333 EXPECT_TRUE(NSIsEmptyRect([cell frameForDecoration:&mock_leading_decoration_
334 inFrame:[field_ bounds]])); 334 inFrame:[field_ bounds]]));
335 [field_ resetFieldEditorFrameIfNeeded]; 335 [field_ resetFieldEditorFrameIfNeeded];
336 EXPECT_NSNE(baseEditorFrame, EditorFrame()); 336 EXPECT_NSNE(baseEditorFrame, EditorFrame());
337 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); 337 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame));
338 338
339 // When the decoration is visible, -resetFieldEditorFrameIfNeeded 339 // When the decoration is visible, -resetFieldEditorFrameIfNeeded
340 // should result in the same frame as the standard focus machinery. 340 // should result in the same frame as the standard focus machinery.
341 mock_left_decoration_.SetVisible(true); 341 mock_leading_decoration_.SetVisible(true);
342 EXPECT_FALSE(NSIsEmptyRect([cell frameForDecoration:&mock_left_decoration_ 342 EXPECT_FALSE(NSIsEmptyRect([cell frameForDecoration:&mock_leading_decoration_
343 inFrame:[field_ bounds]])); 343 inFrame:[field_ bounds]]));
344 344
345 [field_ resetFieldEditorFrameIfNeeded]; 345 [field_ resetFieldEditorFrameIfNeeded];
346 EXPECT_NSEQ(baseEditorFrame, EditorFrame()); 346 EXPECT_NSEQ(baseEditorFrame, EditorFrame());
347 } 347 }
348 348
349 // Test that resetting the field editor bounds does not cause untoward 349 // Test that resetting the field editor bounds does not cause untoward
350 // messages to the field's observer. 350 // messages to the field's observer.
351 TEST_F(AutocompleteTextFieldObserverTest, ResetFieldEditorContinuesEditing) { 351 TEST_F(AutocompleteTextFieldObserverTest, ResetFieldEditorContinuesEditing) {
352 // Many of these methods try to change the selection. 352 // Many of these methods try to change the selection.
353 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(A<NSRange>())) 353 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(A<NSRange>()))
354 .WillRepeatedly(ReturnArg<0>()); 354 .WillRepeatedly(ReturnArg<0>());
355 355
356 EXPECT_CALL(field_observer_, OnSetFocus(false)); 356 EXPECT_CALL(field_observer_, OnSetFocus(false));
357 // Becoming first responder doesn't begin editing. 357 // Becoming first responder doesn't begin editing.
358 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 358 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
359 const NSRect baseEditorFrame = EditorFrame(); 359 const NSRect baseEditorFrame = EditorFrame();
360 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); 360 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]);
361 EXPECT_TRUE(nil != editor); 361 EXPECT_TRUE(nil != editor);
362 362
363 // This should begin editing and indicate a change. 363 // This should begin editing and indicate a change.
364 EXPECT_CALL(field_observer_, OnDidBeginEditing()); 364 EXPECT_CALL(field_observer_, OnDidBeginEditing());
365 EXPECT_CALL(field_observer_, OnBeforeChange()); 365 EXPECT_CALL(field_observer_, OnBeforeChange());
366 EXPECT_CALL(field_observer_, OnDidChange()); 366 EXPECT_CALL(field_observer_, OnDidChange());
367 [editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""]; 367 [editor shouldChangeTextInRange:NSMakeRange(0, 0) replacementString:@""];
368 [editor didChangeText]; 368 [editor didChangeText];
369 369
370 // No messages to |field_observer_| when the frame actually changes. 370 // No messages to |field_observer_| when the frame actually changes.
371 mock_left_decoration_.SetVisible(true); 371 mock_leading_decoration_.SetVisible(true);
372 [field_ resetFieldEditorFrameIfNeeded]; 372 [field_ resetFieldEditorFrameIfNeeded];
373 EXPECT_NSNE(baseEditorFrame, EditorFrame()); 373 EXPECT_NSNE(baseEditorFrame, EditorFrame());
374 } 374 }
375 375
376 // Clicking in a right-hand decoration which does not handle the mouse 376 // Clicking in a right-hand decoration which does not handle the mouse
377 // puts the caret rightmost. 377 // puts the caret rightmost.
378 TEST_F(AutocompleteTextFieldTest, ClickRightDecorationPutsCaretRightmost) { 378 TEST_F(AutocompleteTextFieldTest, ClickRightDecorationPutsCaretRightmost) {
379 // Decoration does not handle the mouse event, so the cell should 379 // Decoration does not handle the mouse event, so the cell should
380 // process it. Called at least once. 380 // process it. Called at least once.
381 EXPECT_CALL(mock_right_decoration_, AcceptsMousePress()) 381 EXPECT_CALL(mock_trailing_decoration_, AcceptsMousePress())
382 .WillOnce(Return(false)) 382 .WillOnce(Return(false))
383 .WillRepeatedly(Return(false)); 383 .WillRepeatedly(Return(false));
384 384
385 // Set the decoration before becoming responder. 385 // Set the decoration before becoming responder.
386 EXPECT_FALSE([field_ currentEditor]); 386 EXPECT_FALSE([field_ currentEditor]);
387 mock_right_decoration_.SetVisible(true); 387 mock_trailing_decoration_.SetVisible(true);
388 388
389 // Make first responder should select all. 389 // Make first responder should select all.
390 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 390 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
391 EXPECT_TRUE([field_ currentEditor]); 391 EXPECT_TRUE([field_ currentEditor]);
392 const NSRange allRange = NSMakeRange(0, [[field_ stringValue] length]); 392 const NSRange allRange = NSMakeRange(0, [[field_ stringValue] length]);
393 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); 393 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange]));
394 394
395 // Generate a click on the decoration. 395 // Generate a click on the decoration.
396 AutocompleteTextFieldCell* cell = [field_ cell]; 396 AutocompleteTextFieldCell* cell = [field_ cell];
397 const NSRect bounds = [field_ bounds]; 397 const NSRect bounds = [field_ bounds];
398 const NSRect iconFrame = 398 const NSRect iconFrame =
399 [cell frameForDecoration:&mock_right_decoration_ inFrame:bounds]; 399 [cell frameForDecoration:&mock_trailing_decoration_ inFrame:bounds];
400 const NSPoint point = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); 400 const NSPoint point = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame));
401 NSEvent* downEvent = Event(field_, point, NSLeftMouseDown); 401 NSEvent* downEvent = Event(field_, point, NSLeftMouseDown);
402 NSEvent* upEvent = Event(field_, point, NSLeftMouseUp); 402 NSEvent* upEvent = Event(field_, point, NSLeftMouseUp);
403 [NSApp postEvent:upEvent atStart:YES]; 403 [NSApp postEvent:upEvent atStart:YES];
404 [field_ mouseDown:downEvent]; 404 [field_ mouseDown:downEvent];
405 405
406 // Selection should be a right-hand-side caret. 406 // Selection should be a right-hand-side caret.
407 EXPECT_TRUE(NSEqualRanges(NSMakeRange([[field_ stringValue] length], 0), 407 EXPECT_TRUE(NSEqualRanges(NSMakeRange([[field_ stringValue] length], 0),
408 [[field_ currentEditor] selectedRange])); 408 [[field_ currentEditor] selectedRange]));
409 } 409 }
410 410
411 // Clicking in a left-side decoration which doesn't handle the event 411 // Clicking in a left-side decoration which doesn't handle the event
412 // puts the selection in the leftmost position. 412 // puts the selection in the leftmost position.
413 TEST_F(AutocompleteTextFieldTest, ClickLeftDecorationPutsCaretLeftmost) { 413 TEST_F(AutocompleteTextFieldTest, ClickLeftDecorationPutsCaretLeftmost) {
414 // Decoration does not handle the mouse event, so the cell should 414 // Decoration does not handle the mouse event, so the cell should
415 // process it. Called at least once. 415 // process it. Called at least once.
416 EXPECT_CALL(mock_left_decoration_, AcceptsMousePress()) 416 EXPECT_CALL(mock_leading_decoration_, AcceptsMousePress())
417 .WillOnce(Return(false)) 417 .WillOnce(Return(false))
418 .WillRepeatedly(Return(false)); 418 .WillRepeatedly(Return(false));
419 419
420 // Set the decoration before becoming responder. 420 // Set the decoration before becoming responder.
421 EXPECT_FALSE([field_ currentEditor]); 421 EXPECT_FALSE([field_ currentEditor]);
422 mock_left_decoration_.SetVisible(true); 422 mock_leading_decoration_.SetVisible(true);
423 423
424 // Make first responder should select all. 424 // Make first responder should select all.
425 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 425 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
426 EXPECT_TRUE([field_ currentEditor]); 426 EXPECT_TRUE([field_ currentEditor]);
427 const NSRange allRange = NSMakeRange(0, [[field_ stringValue] length]); 427 const NSRange allRange = NSMakeRange(0, [[field_ stringValue] length]);
428 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); 428 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange]));
429 429
430 // Generate a click on the decoration. 430 // Generate a click on the decoration.
431 AutocompleteTextFieldCell* cell = [field_ cell]; 431 AutocompleteTextFieldCell* cell = [field_ cell];
432 const NSRect bounds = [field_ bounds]; 432 const NSRect bounds = [field_ bounds];
433 const NSRect iconFrame = 433 const NSRect iconFrame =
434 [cell frameForDecoration:&mock_left_decoration_ inFrame:bounds]; 434 [cell frameForDecoration:&mock_leading_decoration_ inFrame:bounds];
435 const NSPoint point = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); 435 const NSPoint point = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame));
436 NSEvent* downEvent = Event(field_, point, NSLeftMouseDown); 436 NSEvent* downEvent = Event(field_, point, NSLeftMouseDown);
437 NSEvent* upEvent = Event(field_, point, NSLeftMouseUp); 437 NSEvent* upEvent = Event(field_, point, NSLeftMouseUp);
438 [NSApp postEvent:upEvent atStart:YES]; 438 [NSApp postEvent:upEvent atStart:YES];
439 [field_ mouseDown:downEvent]; 439 [field_ mouseDown:downEvent];
440 440
441 // Selection should be a left-hand-side caret. 441 // Selection should be a left-hand-side caret.
442 EXPECT_TRUE(NSEqualRanges(NSMakeRange(0, 0), 442 EXPECT_TRUE(NSEqualRanges(NSMakeRange(0, 0),
443 [[field_ currentEditor] selectedRange])); 443 [[field_ currentEditor] selectedRange]));
444 } 444 }
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 const NSRange selectedRange([[field_ currentEditor] selectedRange]); 553 const NSRange selectedRange([[field_ currentEditor] selectedRange]);
554 EXPECT_EQ(selectedRange.location, 0U); 554 EXPECT_EQ(selectedRange.location, 0U);
555 EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]); 555 EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]);
556 } 556 }
557 557
558 // Clicking a decoration should call decoration's OnMousePressed. 558 // Clicking a decoration should call decoration's OnMousePressed.
559 TEST_F(AutocompleteTextFieldTest, LeftDecorationMouseDown) { 559 TEST_F(AutocompleteTextFieldTest, LeftDecorationMouseDown) {
560 // At this point, not focussed. 560 // At this point, not focussed.
561 EXPECT_FALSE([field_ currentEditor]); 561 EXPECT_FALSE([field_ currentEditor]);
562 562
563 mock_left_decoration_.SetVisible(true); 563 mock_leading_decoration_.SetVisible(true);
564 EXPECT_CALL(mock_left_decoration_, AcceptsMousePress()) 564 EXPECT_CALL(mock_leading_decoration_, AcceptsMousePress())
565 .WillRepeatedly(Return(true)); 565 .WillRepeatedly(Return(true));
566 566
567 AutocompleteTextFieldCell* cell = [field_ cell]; 567 AutocompleteTextFieldCell* cell = [field_ cell];
568 [cell updateMouseTrackingAndToolTipsInRect:[field_ frame] ofView:field_]; 568 [cell updateMouseTrackingAndToolTipsInRect:[field_ frame] ofView:field_];
569 569
570 const NSRect iconFrame = 570 const NSRect iconFrame = [cell frameForDecoration:&mock_leading_decoration_
571 [cell frameForDecoration:&mock_left_decoration_ inFrame:[field_ bounds]]; 571 inFrame:[field_ bounds]];
572 const NSPoint location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); 572 const NSPoint location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame));
573 NSEvent* downEvent = Event(field_, location, NSLeftMouseDown, 1); 573 NSEvent* downEvent = Event(field_, location, NSLeftMouseDown, 1);
574 NSEvent* upEvent = Event(field_, location, NSLeftMouseUp, 1); 574 NSEvent* upEvent = Event(field_, location, NSLeftMouseUp, 1);
575 575
576 // Since decorations can be dragged, the mouse-press is sent on 576 // Since decorations can be dragged, the mouse-press is sent on
577 // mouse-up. 577 // mouse-up.
578 [NSApp postEvent:upEvent atStart:YES]; 578 [NSApp postEvent:upEvent atStart:YES];
579 579
580 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_, _)) 580 EXPECT_CALL(mock_leading_decoration_, OnMousePressed(_, _))
581 .WillOnce(Return(true)); 581 .WillOnce(Return(true));
582 [field_ mouseDown:downEvent]; 582 [field_ mouseDown:downEvent];
583 583
584 // Focus the field and test that handled clicks don't affect selection. 584 // Focus the field and test that handled clicks don't affect selection.
585 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 585 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
586 EXPECT_TRUE([field_ currentEditor]); 586 EXPECT_TRUE([field_ currentEditor]);
587 const NSRange allRange = NSMakeRange(0, [[field_ stringValue] length]); 587 const NSRange allRange = NSMakeRange(0, [[field_ stringValue] length]);
588 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); 588 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange]));
589 589
590 // Generate another click on the decoration. 590 // Generate another click on the decoration.
591 downEvent = Event(field_, location, NSLeftMouseDown, 1); 591 downEvent = Event(field_, location, NSLeftMouseDown, 1);
592 upEvent = Event(field_, location, NSLeftMouseUp, 1); 592 upEvent = Event(field_, location, NSLeftMouseUp, 1);
593 [NSApp postEvent:upEvent atStart:YES]; 593 [NSApp postEvent:upEvent atStart:YES];
594 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_, _)) 594 EXPECT_CALL(mock_leading_decoration_, OnMousePressed(_, _))
595 .WillOnce(Return(true)); 595 .WillOnce(Return(true));
596 [field_ mouseDown:downEvent]; 596 [field_ mouseDown:downEvent];
597 597
598 // The selection should not have changed. 598 // The selection should not have changed.
599 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange])); 599 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange]));
600 600
601 // TODO(shess): Test that mouse drags are initiated if the next 601 // TODO(shess): Test that mouse drags are initiated if the next
602 // event is a drag, or if the mouse-up takes too long to arrive. 602 // event is a drag, or if the mouse-up takes too long to arrive.
603 // IDEA: mock decoration to return a pasteboard which a mock 603 // IDEA: mock decoration to return a pasteboard which a mock
604 // AutocompleteTextField notes in -dragImage:*. 604 // AutocompleteTextField notes in -dragImage:*.
605 } 605 }
606 606
607 // Clicking a decoration should call decoration's OnMousePressed. 607 // Clicking a decoration should call decoration's OnMousePressed.
608 TEST_F(AutocompleteTextFieldTest, RightDecorationMouseDown) { 608 TEST_F(AutocompleteTextFieldTest, RightDecorationMouseDown) {
609 // At this point, not focussed. 609 // At this point, not focussed.
610 EXPECT_FALSE([field_ currentEditor]); 610 EXPECT_FALSE([field_ currentEditor]);
611 611
612 mock_right_decoration_.SetVisible(true); 612 mock_trailing_decoration_.SetVisible(true);
613 EXPECT_CALL(mock_right_decoration_, AcceptsMousePress()) 613 EXPECT_CALL(mock_trailing_decoration_, AcceptsMousePress())
614 .WillRepeatedly(Return(true)); 614 .WillRepeatedly(Return(true));
615 615
616 AutocompleteTextFieldCell* cell = [field_ cell]; 616 AutocompleteTextFieldCell* cell = [field_ cell];
617 [cell updateMouseTrackingAndToolTipsInRect:[field_ frame] ofView:field_]; 617 [cell updateMouseTrackingAndToolTipsInRect:[field_ frame] ofView:field_];
618 618
619 const NSRect bounds = [field_ bounds]; 619 const NSRect bounds = [field_ bounds];
620 const NSRect iconFrame = 620 const NSRect iconFrame =
621 [cell frameForDecoration:&mock_right_decoration_ inFrame:bounds]; 621 [cell frameForDecoration:&mock_trailing_decoration_ inFrame:bounds];
622 const NSPoint location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)); 622 const NSPoint location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame));
623 NSEvent* downEvent = Event(field_, location, NSLeftMouseDown, 1); 623 NSEvent* downEvent = Event(field_, location, NSLeftMouseDown, 1);
624 NSEvent* upEvent = Event(field_, location, NSLeftMouseUp, 1); 624 NSEvent* upEvent = Event(field_, location, NSLeftMouseUp, 1);
625 625
626 // Since decorations can be dragged, the mouse-press is sent on 626 // Since decorations can be dragged, the mouse-press is sent on
627 // mouse-up. 627 // mouse-up.
628 [NSApp postEvent:upEvent atStart:YES]; 628 [NSApp postEvent:upEvent atStart:YES];
629 629
630 EXPECT_CALL(mock_right_decoration_, OnMousePressed(_, _)) 630 EXPECT_CALL(mock_trailing_decoration_, OnMousePressed(_, _))
631 .WillOnce(Return(true)); 631 .WillOnce(Return(true));
632 [field_ mouseDown:downEvent]; 632 [field_ mouseDown:downEvent];
633 } 633 }
634 634
635 // Test that page action menus are properly returned. 635 // Test that page action menus are properly returned.
636 // TODO(shess): Really, this should test that things are forwarded to 636 // TODO(shess): Really, this should test that things are forwarded to
637 // the cell, and the cell tests should test that the right things are 637 // the cell, and the cell tests should test that the right things are
638 // selected. It's easier to mock the event here, though. This code's 638 // selected. It's easier to mock the event here, though. This code's
639 // event-mockers might be worth promoting to |cocoa_test_event_utils.h| or 639 // event-mockers might be worth promoting to |cocoa_test_event_utils.h| or
640 // |cocoa_test_helper.h|. 640 // |cocoa_test_helper.h|.
641 TEST_F(AutocompleteTextFieldTest, DecorationMenu) { 641 TEST_F(AutocompleteTextFieldTest, DecorationMenu) {
642 AutocompleteTextFieldCell* cell = [field_ cell]; 642 AutocompleteTextFieldCell* cell = [field_ cell];
643 const NSRect bounds([field_ bounds]); 643 const NSRect bounds([field_ bounds]);
644 644
645 const CGFloat edge = NSHeight(bounds) - 4.0; 645 const CGFloat edge = NSHeight(bounds) - 4.0;
646 const NSSize size = NSMakeSize(edge, edge); 646 const NSSize size = NSMakeSize(edge, edge);
647 base::scoped_nsobject<NSImage> image([[NSImage alloc] initWithSize:size]); 647 base::scoped_nsobject<NSImage> image([[NSImage alloc] initWithSize:size]);
648 648
649 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Menu"]); 649 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Menu"]);
650 650
651 mock_left_decoration_.SetVisible(true); 651 mock_leading_decoration_.SetVisible(true);
652 mock_right_decoration_.SetVisible(true); 652 mock_trailing_decoration_.SetVisible(true);
653 653
654 // The item with a menu returns it. 654 // The item with a menu returns it.
655 NSRect actionFrame = [cell frameForDecoration:&mock_right_decoration_ 655 NSRect actionFrame =
656 inFrame:bounds]; 656 [cell frameForDecoration:&mock_trailing_decoration_ inFrame:bounds];
657 NSPoint location = NSMakePoint(NSMidX(actionFrame), NSMidY(actionFrame)); 657 NSPoint location = NSMakePoint(NSMidX(actionFrame), NSMidY(actionFrame));
658 NSEvent* event = Event(field_, location, NSRightMouseDown, 1); 658 NSEvent* event = Event(field_, location, NSRightMouseDown, 1);
659 659
660 // Check that the decoration is called, and the field returns the 660 // Check that the decoration is called, and the field returns the
661 // menu. 661 // menu.
662 EXPECT_CALL(mock_right_decoration_, GetMenu()) 662 EXPECT_CALL(mock_trailing_decoration_, GetMenu())
663 .WillOnce(Return(menu.get())); 663 .WillOnce(Return(menu.get()));
664 NSMenu *decorationMenu = [field_ decorationMenuForEvent:event]; 664 NSMenu *decorationMenu = [field_ decorationMenuForEvent:event];
665 EXPECT_EQ(decorationMenu, menu); 665 EXPECT_EQ(decorationMenu, menu);
666 666
667 // The item without a menu returns nil. 667 // The item without a menu returns nil.
668 EXPECT_CALL(mock_left_decoration_, GetMenu()) 668 EXPECT_CALL(mock_leading_decoration_, GetMenu())
669 .WillOnce(Return(static_cast<NSMenu*>(nil))); 669 .WillOnce(Return(static_cast<NSMenu*>(nil)));
670 actionFrame = [cell frameForDecoration:&mock_left_decoration_ 670 actionFrame =
671 inFrame:bounds]; 671 [cell frameForDecoration:&mock_leading_decoration_ inFrame:bounds];
672 location = NSMakePoint(NSMidX(actionFrame), NSMidY(actionFrame)); 672 location = NSMakePoint(NSMidX(actionFrame), NSMidY(actionFrame));
673 event = Event(field_, location, NSRightMouseDown, 1); 673 event = Event(field_, location, NSRightMouseDown, 1);
674 EXPECT_FALSE([field_ decorationMenuForEvent:event]); 674 EXPECT_FALSE([field_ decorationMenuForEvent:event]);
675 675
676 // Something not in an action returns nil. 676 // Something not in an action returns nil.
677 location = NSMakePoint(NSMidX(bounds), NSMidY(bounds)); 677 location = NSMakePoint(NSMidX(bounds), NSMidY(bounds));
678 event = Event(field_, location, NSRightMouseDown, 1); 678 event = Event(field_, location, NSRightMouseDown, 1);
679 EXPECT_FALSE([field_ decorationMenuForEvent:event]); 679 EXPECT_FALSE([field_ decorationMenuForEvent:event]);
680 } 680 }
681 681
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 [[field_ cell] setHideFocusState:NO 789 [[field_ cell] setHideFocusState:NO
790 ofView:field_]; 790 ofView:field_];
791 EXPECT_TRUE([[field_ cell] showsFirstResponder]); 791 EXPECT_TRUE([[field_ cell] showsFirstResponder]);
792 EXPECT_TRUE([FieldEditor() shouldDrawInsertionPoint]); 792 EXPECT_TRUE([FieldEditor() shouldDrawInsertionPoint]);
793 } 793 }
794 794
795 // Verify that the tracking areas are added properly. 795 // Verify that the tracking areas are added properly.
796 TEST_F(AutocompleteTextFieldTest, UpdateTrackingAreas) { 796 TEST_F(AutocompleteTextFieldTest, UpdateTrackingAreas) {
797 AutocompleteTextFieldCell* cell = [field_ cell]; 797 AutocompleteTextFieldCell* cell = [field_ cell];
798 798
799 mock_left_decoration_.SetVisible(true); 799 mock_leading_decoration_.SetVisible(true);
800 mock_right_decoration_.SetVisible(true); 800 mock_trailing_decoration_.SetVisible(true);
801 801
802 EXPECT_CALL(mock_left_decoration_, AcceptsMousePress()) 802 EXPECT_CALL(mock_leading_decoration_, AcceptsMousePress())
803 .WillOnce(Return(true)) 803 .WillOnce(Return(true))
804 .WillRepeatedly(Return(true)); 804 .WillRepeatedly(Return(true));
805 EXPECT_CALL(mock_right_decoration_, AcceptsMousePress()) 805 EXPECT_CALL(mock_trailing_decoration_, AcceptsMousePress())
806 .WillOnce(Return(false)) 806 .WillOnce(Return(false))
807 .WillRepeatedly(Return(false)); 807 .WillRepeatedly(Return(false));
808 [cell updateMouseTrackingAndToolTipsInRect:[field_ bounds] ofView:field_]; 808 [cell updateMouseTrackingAndToolTipsInRect:[field_ bounds] ofView:field_];
809 809
810 EXPECT_EQ([cell mouseTrackingDecorations].size(), 1.0); 810 EXPECT_EQ([cell mouseTrackingDecorations].size(), 1.0);
811 811
812 [cell clearTrackingArea]; 812 [cell clearTrackingArea];
813 EXPECT_TRUE([cell mouseTrackingDecorations].empty()); 813 EXPECT_TRUE([cell mouseTrackingDecorations].empty());
814 814
815 EXPECT_CALL(mock_right_decoration_, AcceptsMousePress()) 815 EXPECT_CALL(mock_trailing_decoration_, AcceptsMousePress())
816 .WillOnce(Return(true)) 816 .WillOnce(Return(true))
817 .WillRepeatedly(Return(true)); 817 .WillRepeatedly(Return(true));
818 818
819 [cell updateMouseTrackingAndToolTipsInRect:[field_ bounds] ofView:field_]; 819 [cell updateMouseTrackingAndToolTipsInRect:[field_ bounds] ofView:field_];
820 EXPECT_EQ([cell mouseTrackingDecorations].size(), 2.0); 820 EXPECT_EQ([cell mouseTrackingDecorations].size(), 2.0);
821 } 821 }
822 822
823 // Verify that clicking a decoration that accepts mouse clicks does not focus 823 // Verify that clicking a decoration that accepts mouse clicks does not focus
824 // the Omnibox. 824 // the Omnibox.
825 TEST_F(AutocompleteTextFieldObserverTest, 825 TEST_F(AutocompleteTextFieldObserverTest,
826 ClickingDecorationDoesNotFocusOmnibox) { 826 ClickingDecorationDoesNotFocusOmnibox) {
827 AutocompleteTextFieldCell* cell = [field_ cell]; 827 AutocompleteTextFieldCell* cell = [field_ cell];
828 828
829 // Set up a non-interactive decoration. 829 // Set up a non-interactive decoration.
830 MockDecoration noninteractive_decoration; 830 MockDecoration noninteractive_decoration;
831 noninteractive_decoration.SetVisible(true); 831 noninteractive_decoration.SetVisible(true);
832 EXPECT_CALL(noninteractive_decoration, AcceptsMousePress()) 832 EXPECT_CALL(noninteractive_decoration, AcceptsMousePress())
833 .WillRepeatedly(testing::Return(false)); 833 .WillRepeatedly(testing::Return(false));
834 [cell addLeftDecoration:&noninteractive_decoration]; 834 [cell addLeadingDecoration:&noninteractive_decoration];
835 835
836 // Set up an interactive decoration. 836 // Set up an interactive decoration.
837 MockDecoration interactive_decoration; 837 MockDecoration interactive_decoration;
838 EXPECT_CALL(interactive_decoration, AcceptsMousePress()) 838 EXPECT_CALL(interactive_decoration, AcceptsMousePress())
839 .WillRepeatedly(testing::Return(true)); 839 .WillRepeatedly(testing::Return(true));
840 interactive_decoration.SetVisible(true); 840 interactive_decoration.SetVisible(true);
841 [cell addLeftDecoration:&interactive_decoration]; 841 [cell addLeadingDecoration:&interactive_decoration];
842 [cell updateMouseTrackingAndToolTipsInRect:[field_ frame] ofView:field_]; 842 [cell updateMouseTrackingAndToolTipsInRect:[field_ frame] ofView:field_];
843 EXPECT_CALL(interactive_decoration, OnMousePressed(_, _)) 843 EXPECT_CALL(interactive_decoration, OnMousePressed(_, _))
844 .WillRepeatedly(testing::Return(true)); 844 .WillRepeatedly(testing::Return(true));
845 845
846 // Ignore incidental calls. The exact frequency of these calls doesn't matter 846 // Ignore incidental calls. The exact frequency of these calls doesn't matter
847 // as they are auxiliary. 847 // as they are auxiliary.
848 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(_)) 848 EXPECT_CALL(field_observer_, SelectionRangeForProposedRange(_))
849 .WillRepeatedly(testing::Return(NSMakeRange(0, 0))); 849 .WillRepeatedly(testing::Return(NSMakeRange(0, 0)));
850 EXPECT_CALL(field_observer_, OnMouseDown(_)).Times(testing::AnyNumber()); 850 EXPECT_CALL(field_observer_, OnMouseDown(_)).Times(testing::AnyNumber());
851 EXPECT_CALL(field_observer_, OnSetFocus(false)).Times(testing::AnyNumber()); 851 EXPECT_CALL(field_observer_, OnSetFocus(false)).Times(testing::AnyNumber());
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 base::scoped_nsobject<AutocompleteTextField> pin([field_ retain]); 924 base::scoped_nsobject<AutocompleteTextField> pin([field_ retain]);
925 [field_ removeFromSuperview]; 925 [field_ removeFromSuperview];
926 [test_window() resignKeyWindow]; 926 [test_window() resignKeyWindow];
927 927
928 [[test_window() contentView] addSubview:field_]; 928 [[test_window() contentView] addSubview:field_];
929 EXPECT_CALL(field_observer_, ClosePopup()); 929 EXPECT_CALL(field_observer_, ClosePopup());
930 [test_window() resignKeyWindow]; 930 [test_window() resignKeyWindow];
931 } 931 }
932 932
933 } // namespace 933 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698