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

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

Issue 2805070: [Mac] First part of Omnibox decoration refactor. Enable ev bubble. (Closed) Base URL: git://codf21.jail/chromium.git
Patch Set: comment clarification Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 #include "app/resource_bundle.h" 7 #include "app/resource_bundle.h"
8 #import "base/cocoa_protocols_mac.h" 8 #import "base/cocoa_protocols_mac.h"
9 #include "base/scoped_nsobject.h" 9 #include "base/scoped_nsobject.h"
10 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field.h" 10 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field.h"
11 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h" 11 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_cell.h"
12 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_editor.h" 12 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_editor.h"
13 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_unittest_help er.h" 13 #import "chrome/browser/cocoa/location_bar/autocomplete_text_field_unittest_help er.h"
14 #import "chrome/browser/cocoa/location_bar/location_bar_decoration.h"
14 #import "chrome/browser/cocoa/cocoa_test_helper.h" 15 #import "chrome/browser/cocoa/cocoa_test_helper.h"
15 #include "grit/theme_resources.h" 16 #include "grit/theme_resources.h"
16 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h" 19 #include "testing/platform_test.h"
19 20
20 using ::testing::InSequence; 21 using ::testing::InSequence;
21 using ::testing::Return; 22 using ::testing::Return;
22 using ::testing::StrictMock; 23 using ::testing::StrictMock;
24 using ::testing::_;
23 25
24 namespace { 26 namespace {
25 class MockLocationIconView : public LocationBarViewMac::LocationIconView { 27
28 class MockDecoration : public LocationBarDecoration {
26 public: 29 public:
27 MockLocationIconView() 30 virtual CGFloat GetWidthForSpace(CGFloat width) { return 20.0; }
28 : LocationBarViewMac::LocationIconView(NULL), 31 virtual bool IsVisible() const { return visible_; }
29 is_draggable_(false), 32 void SetVisible(bool visible) { visible_ = visible; }
30 mouse_was_pressed_(false) {}
31 33
32 // |LocationBarViewMac::LocationIconView| dragging support needs 34 virtual void DrawInFrame(NSRect frame, NSView* control_view) { ; }
33 // more setup than this test provides. 35 MOCK_METHOD1(OnMousePressed, bool(NSRect frame));
34 bool IsDraggable() {
35 return is_draggable_;
36 }
37 virtual NSPasteboard* GetDragPasteboard() {
38 return [NSPasteboard pasteboardWithUniqueName];
39 }
40 void SetDraggable(bool is_draggable) {
41 is_draggable_ = is_draggable;
42 }
43 36
44 // We can't use gmock's MOCK_METHOD macro, because it doesn't like the 37 bool visible_;
45 // NSRect argument to OnMousePressed.
46 virtual void OnMousePressed(NSRect bounds) {
47 mouse_was_pressed_ = true;
48 }
49 bool MouseWasPressed() { return mouse_was_pressed_; }
50
51 private:
52 bool is_draggable_;
53 bool mouse_was_pressed_;
54 }; 38 };
55 39
56 class MockPageActionImageView : public LocationBarViewMac::PageActionImageView { 40 class MockPageActionImageView : public LocationBarViewMac::PageActionImageView {
57 public: 41 public:
58 MockPageActionImageView() {} 42 MockPageActionImageView() {}
59 virtual ~MockPageActionImageView() {} 43 virtual ~MockPageActionImageView() {}
60 44
61 // We can't use gmock's MOCK_METHOD macro, because it doesn't like the 45 // We can't use gmock's MOCK_METHOD macro, because it doesn't like the
62 // NSRect argument to OnMousePressed. 46 // NSRect argument to OnMousePressed.
63 virtual void OnMousePressed(NSRect bounds) { 47 virtual void OnMousePressed(NSRect bounds) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 AutocompleteTextFieldTest() { 114 AutocompleteTextFieldTest() {
131 // Make sure this is wide enough to play games with the cell 115 // Make sure this is wide enough to play games with the cell
132 // decorations. 116 // decorations.
133 NSRect frame = NSMakeRect(0, 0, kWidth, 30); 117 NSRect frame = NSMakeRect(0, 0, kWidth, 30);
134 scoped_nsobject<AutocompleteTextField> field( 118 scoped_nsobject<AutocompleteTextField> field(
135 [[AutocompleteTextField alloc] initWithFrame:frame]); 119 [[AutocompleteTextField alloc] initWithFrame:frame]);
136 field_ = field.get(); 120 field_ = field.get();
137 [field_ setStringValue:@"Test test"]; 121 [field_ setStringValue:@"Test test"];
138 [[test_window() contentView] addSubview:field_]; 122 [[test_window() contentView] addSubview:field_];
139 123
124 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
125 [cell clearDecorations];
126 mock_left_decoration_.SetVisible(false);
127 [cell addLeftDecoration:&mock_left_decoration_];
128
140 window_delegate_.reset( 129 window_delegate_.reset(
141 [[AutocompleteTextFieldWindowTestDelegate alloc] init]); 130 [[AutocompleteTextFieldWindowTestDelegate alloc] init]);
142 [test_window() setDelegate:window_delegate_.get()]; 131 [test_window() setDelegate:window_delegate_.get()];
143 } 132 }
144 133
145 NSEvent* KeyDownEventWithFlags(NSUInteger flags) { 134 NSEvent* KeyDownEventWithFlags(NSUInteger flags) {
146 return [NSEvent keyEventWithType:NSKeyDown 135 return [NSEvent keyEventWithType:NSKeyDown
147 location:NSZeroPoint 136 location:NSZeroPoint
148 modifierFlags:flags 137 modifierFlags:flags
149 timestamp:0.0 138 timestamp:0.0
(...skipping 12 matching lines...) Expand all
162 if ([[field_ subviews] count] > 0) { 151 if ([[field_ subviews] count] > 0) {
163 return [[[field_ subviews] objectAtIndex:0] frame]; 152 return [[[field_ subviews] objectAtIndex:0] frame];
164 } else { 153 } else {
165 // Return something which won't work so the caller can soldier 154 // Return something which won't work so the caller can soldier
166 // on. 155 // on.
167 return NSZeroRect; 156 return NSZeroRect;
168 } 157 }
169 } 158 }
170 159
171 AutocompleteTextField* field_; 160 AutocompleteTextField* field_;
161 MockDecoration mock_left_decoration_;
172 scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_; 162 scoped_nsobject<AutocompleteTextFieldWindowTestDelegate> window_delegate_;
173 }; 163 };
174 164
175 TEST_VIEW(AutocompleteTextFieldTest, field_); 165 TEST_VIEW(AutocompleteTextFieldTest, field_);
176 166
177 // Base class for testing AutocompleteTextFieldObserver messages. 167 // Base class for testing AutocompleteTextFieldObserver messages.
178 class AutocompleteTextFieldObserverTest : public AutocompleteTextFieldTest { 168 class AutocompleteTextFieldObserverTest : public AutocompleteTextFieldTest {
179 public: 169 public:
180 virtual void SetUp() { 170 virtual void SetUp() {
181 AutocompleteTextFieldTest::SetUp(); 171 AutocompleteTextFieldTest::SetUp();
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; 258 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
269 259
270 [cell setSearchHintString:@"Type to search" availableWidth:kWidth]; 260 [cell setSearchHintString:@"Type to search" availableWidth:kWidth];
271 [field_ display]; 261 [field_ display];
272 262
273 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; 263 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
274 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix" 264 [cell setKeywordHintPrefix:@"prefix" image:image suffix:@"suffix"
275 availableWidth:kWidth]; 265 availableWidth:kWidth];
276 [field_ display]; 266 [field_ display];
277 267
278 [cell setKeywordString:@"Search Engine:" 268 [cell clearHint];
279 partialString:@"Search Eng:"
280 availableWidth:kWidth];
281 [field_ display];
282
283 [cell clearKeywordAndHint];
284 [field_ display]; 269 [field_ display];
285 } 270 }
286 271
287 TEST_F(AutocompleteTextFieldObserverTest, FlagsChanged) { 272 TEST_F(AutocompleteTextFieldObserverTest, FlagsChanged) {
288 InSequence dummy; // Call mock in exactly the order specified. 273 InSequence dummy; // Call mock in exactly the order specified.
289 274
290 // Test without Control key down, but some other modifier down. 275 // Test without Control key down, but some other modifier down.
291 EXPECT_CALL(field_observer_, OnControlKeyChanged(false)); 276 EXPECT_CALL(field_observer_, OnControlKeyChanged(false));
292 [field_ flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)]; 277 [field_ flagsChanged:KeyDownEventWithFlags(NSShiftKeyMask)];
293 278
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 // Setting a hint should result in a strictly smaller editor frame. 322 // Setting a hint should result in a strictly smaller editor frame.
338 EXPECT_FALSE([cell hintString]); 323 EXPECT_FALSE([cell hintString]);
339 [cell setSearchHintString:@"search hint" availableWidth:kWidth]; 324 [cell setSearchHintString:@"search hint" availableWidth:kWidth];
340 EXPECT_TRUE([cell hintString]); 325 EXPECT_TRUE([cell hintString]);
341 [field_ resetFieldEditorFrameIfNeeded]; 326 [field_ resetFieldEditorFrameIfNeeded];
342 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); 327 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame()));
343 EXPECT_TRUE(NSContainsRect(baseEditorFrame, EditorFrame())); 328 EXPECT_TRUE(NSContainsRect(baseEditorFrame, EditorFrame()));
344 329
345 // Clearing hint string and using -resetFieldEditorFrameIfNeeded 330 // Clearing hint string and using -resetFieldEditorFrameIfNeeded
346 // should result in the same frame as the standard focus machinery. 331 // should result in the same frame as the standard focus machinery.
347 [cell clearKeywordAndHint]; 332 [cell clearHint];
348 EXPECT_FALSE([cell hintString]); 333 EXPECT_FALSE([cell hintString]);
349 [field_ resetFieldEditorFrameIfNeeded]; 334 [field_ resetFieldEditorFrameIfNeeded];
350 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); 335 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame()));
351 } 336 }
352 337
353 // Test that the field editor gets the same bounds when focus is 338 // Test that the field editor gets the same bounds when focus is
354 // delivered by the standard focusing machinery, or by 339 // delivered by the standard focusing machinery, or by
355 // -resetFieldEditorFrameIfNeeded. 340 // -resetFieldEditorFrameIfNeeded.
356 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorSearchHint) { 341 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorSearchHint) {
357 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; 342 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
358 343
359 NSString* const kHintString = @"Type to search"; 344 NSString* const kHintString = @"Type to search";
360 345
361 // Capture the editor frame resulting from the standard focus 346 // Capture the editor frame resulting from the standard focus
362 // machinery. 347 // machinery.
363 [cell setSearchHintString:kHintString availableWidth:kWidth]; 348 [cell setSearchHintString:kHintString availableWidth:kWidth];
364 EXPECT_TRUE([cell hintString]); 349 EXPECT_TRUE([cell hintString]);
365 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 350 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
366 const NSRect baseEditorFrame(EditorFrame()); 351 const NSRect baseEditorFrame(EditorFrame());
367 352
368 // Clearing the hint should result in a strictly larger editor 353 // Clearing the hint should result in a strictly larger editor
369 // frame. 354 // frame.
370 [cell clearKeywordAndHint]; 355 [cell clearHint];
371 EXPECT_FALSE([cell hintString]); 356 EXPECT_FALSE([cell hintString]);
372 [field_ resetFieldEditorFrameIfNeeded]; 357 [field_ resetFieldEditorFrameIfNeeded];
373 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); 358 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame()));
374 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); 359 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame));
375 360
376 // Setting the same hint string and using 361 // Setting the same hint string and using
377 // -resetFieldEditorFrameIfNeeded should result in the same frame as 362 // -resetFieldEditorFrameIfNeeded should result in the same frame as
378 // the standard focus machinery. 363 // the standard focus machinery.
379 [cell setSearchHintString:kHintString availableWidth:kWidth]; 364 [cell setSearchHintString:kHintString availableWidth:kWidth];
380 EXPECT_TRUE([cell hintString]); 365 EXPECT_TRUE([cell hintString]);
381 [field_ resetFieldEditorFrameIfNeeded]; 366 [field_ resetFieldEditorFrameIfNeeded];
382 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); 367 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame()));
383 } 368 }
384 369
385 // Test that the field editor gets the same bounds when focus is 370 // Test that the field editor gets the same bounds when focus is
386 // delivered by the standard focusing machinery, or by 371 // delivered by the standard focusing machinery, or by
387 // -resetFieldEditorFrameIfNeeded. 372 // -resetFieldEditorFrameIfNeeded.
388 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorKeywordHint) { 373 TEST_F(AutocompleteTextFieldTest, ResetFieldEditorKeywordHint) {
389 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; 374 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
390 375
391 NSString* const kFullString = @"Search Engine:"; 376 // Make sure decoration isn't already visible, then make it visible.
392 NSString* const kPartialString = @"Search Eng:"; 377 EXPECT_TRUE(NSIsEmptyRect([cell frameForDecoration:&mock_left_decoration_
378 inFrame:[field_ bounds]]));
379 mock_left_decoration_.SetVisible(true);
380 EXPECT_FALSE(NSIsEmptyRect([cell frameForDecoration:&mock_left_decoration_
381 inFrame:[field_ bounds]]));
393 382
394 // Capture the editor frame resulting from the standard focus 383 // Capture the editor frame resulting from the standard focus
395 // machinery. 384 // machinery.
396 [cell setKeywordString:kFullString
397 partialString:kPartialString
398 availableWidth:kWidth];
399 EXPECT_TRUE([cell keywordString]);
400 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 385 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
401 const NSRect baseEditorFrame(EditorFrame()); 386 const NSRect baseEditorFrame(EditorFrame());
402 387
403 // Clearing the hint should result in a strictly larger editor 388 // When the decoration is not visible the frame should be strictly larger.
404 // frame. 389 mock_left_decoration_.SetVisible(false);
405 [cell clearKeywordAndHint]; 390 EXPECT_TRUE(NSIsEmptyRect([cell frameForDecoration:&mock_left_decoration_
406 EXPECT_FALSE([cell keywordString]); 391 inFrame:[field_ bounds]]));
407 [field_ resetFieldEditorFrameIfNeeded]; 392 [field_ resetFieldEditorFrameIfNeeded];
408 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame())); 393 EXPECT_FALSE(NSEqualRects(baseEditorFrame, EditorFrame()));
409 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame)); 394 EXPECT_TRUE(NSContainsRect(EditorFrame(), baseEditorFrame));
410 395
411 // Setting the same hint string and using 396 // When the decoration is visible, -resetFieldEditorFrameIfNeeded
412 // -resetFieldEditorFrameIfNeeded should result in the same frame as 397 // should result in the same frame as the standard focus machinery.
413 // the standard focus machinery. 398 mock_left_decoration_.SetVisible(true);
414 [cell setKeywordString:kFullString 399 EXPECT_FALSE(NSIsEmptyRect([cell frameForDecoration:&mock_left_decoration_
415 partialString:kPartialString 400 inFrame:[field_ bounds]]));
416 availableWidth:kWidth];
417 EXPECT_TRUE([cell keywordString]);
418 [field_ resetFieldEditorFrameIfNeeded]; 401 [field_ resetFieldEditorFrameIfNeeded];
419 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame())); 402 EXPECT_TRUE(NSEqualRects(baseEditorFrame, EditorFrame()));
420 } 403 }
421 404
422 // Test that resetting the field editor bounds does not cause untoward 405 // Test that resetting the field editor bounds does not cause untoward
423 // messages to the field's observer. 406 // messages to the field's observer.
424 TEST_F(AutocompleteTextFieldObserverTest, ResetFieldEditorContinuesEditing) { 407 TEST_F(AutocompleteTextFieldObserverTest, ResetFieldEditorContinuesEditing) {
425 EXPECT_CALL(field_observer_, OnSetFocus(false)); 408 EXPECT_CALL(field_observer_, OnSetFocus(false));
426 // Becoming first responder doesn't begin editing. 409 // Becoming first responder doesn't begin editing.
427 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 410 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
(...skipping 28 matching lines...) Expand all
456 const NSPoint point(NSMakePoint(300.0 - 20.0, 5.0)); 439 const NSPoint point(NSMakePoint(300.0 - 20.0, 5.0));
457 NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); 440 NSEvent* downEvent(Event(field_, point, NSLeftMouseDown));
458 NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); 441 NSEvent* upEvent(Event(field_, point, NSLeftMouseUp));
459 [NSApp postEvent:upEvent atStart:YES]; 442 [NSApp postEvent:upEvent atStart:YES];
460 [field_ mouseDown:downEvent]; 443 [field_ mouseDown:downEvent];
461 const NSRange selectedRange([[field_ currentEditor] selectedRange]); 444 const NSRange selectedRange([[field_ currentEditor] selectedRange]);
462 EXPECT_EQ(selectedRange.location, [[field_ stringValue] length]); 445 EXPECT_EQ(selectedRange.location, [[field_ stringValue] length]);
463 EXPECT_EQ(selectedRange.length, 0U); 446 EXPECT_EQ(selectedRange.length, 0U);
464 } 447 }
465 448
466 // Clicking in the keyword-search should put the caret in the 449 // Clicking in a left-side decoration which doesn't handle the event
467 // leftmost position. 450 // puts the selection in the leftmost position.
468 TEST_F(AutocompleteTextFieldTest, ClickKeywordPutsCaretLeftmost) { 451 TEST_F(AutocompleteTextFieldTest, ClickLeftDecorationPutsCaretLeftmost) {
452 // Decoration does not handle the mouse event, so the cell should
453 // process it.
454 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_))
455 .WillRepeatedly(Return(false));
456
469 // Set the decoration before becoming responder. 457 // Set the decoration before becoming responder.
470 EXPECT_FALSE([field_ currentEditor]); 458 EXPECT_FALSE([field_ currentEditor]);
471 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; 459 mock_left_decoration_.SetVisible(true);
472 [cell setKeywordString:@"Search Engine:"
473 partialString:@"Search:"
474 availableWidth:kWidth];
475 460
476 // Can't rely on the window machinery to make us first responder, 461 // Make first responder should select all.
477 // here.
478 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 462 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
479 EXPECT_TRUE([field_ currentEditor]); 463 EXPECT_TRUE([field_ currentEditor]);
464 const NSRange allRange = NSMakeRange(0, [[field_ stringValue] length]);
465 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange]));
480 466
481 const NSPoint point(NSMakePoint(20.0, 5.0)); 467 // Generate a click on the decoration.
468 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
469 const NSRect iconFrame =
470 [cell frameForDecoration:&mock_left_decoration_ inFrame:[field_ bounds]];
471 const NSPoint point = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame));
482 NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); 472 NSEvent* downEvent(Event(field_, point, NSLeftMouseDown));
483 NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); 473 NSEvent* upEvent(Event(field_, point, NSLeftMouseUp));
484 [NSApp postEvent:upEvent atStart:YES]; 474 [NSApp postEvent:upEvent atStart:YES];
485 [field_ mouseDown:downEvent]; 475 [field_ mouseDown:downEvent];
486 const NSRange selectedRange([[field_ currentEditor] selectedRange]); 476
487 EXPECT_EQ(selectedRange.location, 0U); 477 // Selection should be a left-hand-side caret.
488 EXPECT_EQ(selectedRange.length, 0U); 478 EXPECT_TRUE(NSEqualRanges(NSMakeRange(0, 0),
479 [[field_ currentEditor] selectedRange]));
489 } 480 }
490 481
491 // Clicks not in the text area or the cell's decorations fall through 482 // Clicks not in the text area or the cell's decorations fall through
492 // to the editor. 483 // to the editor.
493 TEST_F(AutocompleteTextFieldTest, ClickBorderSelectsAll) { 484 TEST_F(AutocompleteTextFieldTest, ClickBorderSelectsAll) {
494 // Can't rely on the window machinery to make us first responder, 485 // Can't rely on the window machinery to make us first responder,
495 // here. 486 // here.
496 [test_window() makePretendKeyWindowAndSetFirstResponder:field_]; 487 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
497 EXPECT_TRUE([field_ currentEditor]); 488 EXPECT_TRUE([field_ currentEditor]);
498 489
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 [NSApp postEvent:upEvent3 atStart:YES]; 584 [NSApp postEvent:upEvent3 atStart:YES];
594 [field_ mouseDown:downEvent3]; 585 [field_ mouseDown:downEvent3];
595 EXPECT_TRUE([field_ currentEditor]); 586 EXPECT_TRUE([field_ currentEditor]);
596 587
597 // Selected the first word. 588 // Selected the first word.
598 const NSRange selectedRange([[field_ currentEditor] selectedRange]); 589 const NSRange selectedRange([[field_ currentEditor] selectedRange]);
599 EXPECT_EQ(selectedRange.location, 0U); 590 EXPECT_EQ(selectedRange.location, 0U);
600 EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]); 591 EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]);
601 } 592 }
602 593
603 // Clicking the security icon should call its OnMousePressed. 594 // Clicking a decoration should call decoration's OnMousePressed.
604 TEST_F(AutocompleteTextFieldTest, LocationIconMouseDown) { 595 TEST_F(AutocompleteTextFieldTest, DecorationMouseDown) {
596 // At this point, not focussed.
597 EXPECT_FALSE([field_ currentEditor]);
598
599 mock_left_decoration_.SetVisible(true);
600
605 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; 601 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
606 602 const NSRect iconFrame =
607 MockLocationIconView location_icon_view; 603 [cell frameForDecoration:&mock_left_decoration_ inFrame:[field_ bounds]];
608 [cell setLocationIconView:&location_icon_view]; 604 const NSPoint location = NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame));
609 location_icon_view.SetImage( 605 NSEvent* downEvent = Event(field_, location, NSLeftMouseDown, 1);
610 ResourceBundle::GetSharedInstance().GetNSImageNamed( 606 NSEvent* upEvent = Event(field_, location, NSLeftMouseUp, 1);
611 IDR_OMNIBOX_HTTPS_VALID));
612 location_icon_view.SetVisible(true);
613
614 NSRect iconFrame([cell locationIconFrameForFrame:[field_ bounds]]);
615 NSPoint location(NSMakePoint(NSMidX(iconFrame), NSMidY(iconFrame)));
616 NSEvent* downEvent(Event(field_, location, NSLeftMouseDown, 1));
617 NSEvent* upEvent(Event(field_, location, NSLeftMouseUp, 1));
618 607
619 // Since location icon can be dragged, the mouse-press is sent on 608 // Since location icon can be dragged, the mouse-press is sent on
620 // mouse-up. 609 // mouse-up.
621 [NSApp postEvent:upEvent atStart:YES]; 610 [NSApp postEvent:upEvent atStart:YES];
611
612 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_))
613 .WillOnce(Return(true));
622 [field_ mouseDown:downEvent]; 614 [field_ mouseDown:downEvent];
623 EXPECT_TRUE(location_icon_view.MouseWasPressed()); 615
616 // Focus the field and test that handled clicks don't affect selection.
617 [test_window() makePretendKeyWindowAndSetFirstResponder:field_];
618 EXPECT_TRUE([field_ currentEditor]);
619 const NSRange allRange = NSMakeRange(0, [[field_ stringValue] length]);
620 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange]));
621
622 // Generate another click on the decoration.
623 downEvent = Event(field_, location, NSLeftMouseDown, 1);
624 upEvent = Event(field_, location, NSLeftMouseUp, 1);
625 [NSApp postEvent:upEvent atStart:YES];
626 EXPECT_CALL(mock_left_decoration_, OnMousePressed(_))
627 .WillOnce(Return(true));
628 [field_ mouseDown:downEvent];
629
630 // The selection should not have changed.
631 EXPECT_TRUE(NSEqualRanges(allRange, [[field_ currentEditor] selectedRange]));
624 632
625 // TODO(shess): Test that mouse drags are initiated if the next 633 // TODO(shess): Test that mouse drags are initiated if the next
626 // event is a drag, or if the mouse-up takes too long to arrive. 634 // event is a drag, or if the mouse-up takes too long to arrive.
635 // IDEA: mock decoration to return a pasteboard which a mock
636 // AutocompleteTextField notes in -dragImage:*.
627 } 637 }
628 638
629 // Clicking a Page Action icon should call its OnMousePressed. 639 // Clicking a Page Action icon should call its OnMousePressed.
630 TEST_F(AutocompleteTextFieldTest, PageActionMouseDown) { 640 TEST_F(AutocompleteTextFieldTest, PageActionMouseDown) {
631 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; 641 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
632 642
633 MockPageActionImageView page_action_view; 643 MockPageActionImageView page_action_view;
634 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; 644 NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"];
635 page_action_view.SetImage(image); 645 page_action_view.SetImage(image);
636 646
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 878
869 scoped_nsobject<AutocompleteTextField> pin([field_ retain]); 879 scoped_nsobject<AutocompleteTextField> pin([field_ retain]);
870 [field_ removeFromSuperview]; 880 [field_ removeFromSuperview];
871 [test_window() resignKeyWindow]; 881 [test_window() resignKeyWindow];
872 882
873 [[test_window() contentView] addSubview:field_]; 883 [[test_window() contentView] addSubview:field_];
874 EXPECT_CALL(field_observer_, ClosePopup()); 884 EXPECT_CALL(field_observer_, ClosePopup());
875 [test_window() resignKeyWindow]; 885 [test_window() resignKeyWindow];
876 } 886 }
877 887
878 TEST_F(AutocompleteTextFieldTest, LocationDragPasteboard) {
879 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell];
880
881 MockLocationIconView location_icon_view;
882 location_icon_view.SetImage(
883 ResourceBundle::GetSharedInstance().GetNSImageNamed(
884 IDR_OMNIBOX_HTTPS_VALID));
885 location_icon_view.SetVisible(true);
886 [cell setLocationIconView:&location_icon_view];
887
888 // Not draggable, so no pasteboard.
889 EXPECT_FALSE([field_ locationDragPasteboard]);
890
891 // Gets a pasteboard when draggable.
892 location_icon_view.SetDraggable(true);
893 EXPECT_TRUE([field_ locationDragPasteboard]);
894 }
895
896 } // namespace 888 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698