diff options
author | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-01 21:47:03 +0000 |
---|---|---|
committer | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-01 21:47:03 +0000 |
commit | 1e8e6ee9effd80a32c540c0211beefd2e6de88b9 (patch) | |
tree | 740302df17ce71ac7a5632078bf405fb685f9142 /chrome/browser/cocoa | |
parent | b4d16d99907a10025f1516a72c20b07142046a5e (diff) | |
download | chromium_src-1e8e6ee9effd80a32c540c0211beefd2e6de88b9.zip chromium_src-1e8e6ee9effd80a32c540c0211beefd2e6de88b9.tar.gz chromium_src-1e8e6ee9effd80a32c540c0211beefd2e6de88b9.tar.bz2 |
[Mac] Unit test -[AutocompleteTextField mouseDown:].
Breaking this off of the change to single-click because this is
definitely a keeper, that one might be contencious.
http://crbug.com/22980
TEST=Unit test only.
Review URL: http://codereview.chromium.org/242103
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27774 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa')
-rw-r--r-- | chrome/browser/cocoa/autocomplete_text_field_unittest.mm | 97 |
1 files changed, 96 insertions, 1 deletions
diff --git a/chrome/browser/cocoa/autocomplete_text_field_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_unittest.mm index ab50073..a821ec7 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_unittest.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_unittest.mm @@ -27,6 +27,33 @@ using ::testing::InSequence; namespace { +// Mock up an incrementing event number. +NSUInteger eventNumber = 0; + +// Create an event of the indicated |type| at |point| within |view|. +// TODO(shess): Would be nice to have a MockApplication which provided +// nifty accessors to create these things and inject them. It could +// even provide functions for "Click and drag mouse from point A to +// point B". +NSEvent* Event(NSView* view, const NSPoint point, const NSEventType type, + const NSUInteger clickCount) { + NSWindow* window([view window]); + const NSPoint locationInWindow([view convertPoint:point toView:nil]); + const NSPoint location([window convertBaseToScreen:locationInWindow]); + return [NSEvent mouseEventWithType:type + location:location + modifierFlags:0 + timestamp:0 + windowNumber:[window windowNumber] + context:nil + eventNumber:eventNumber++ + clickCount:clickCount + pressure:0.0]; +} +NSEvent* Event(NSView* view, const NSPoint point, const NSEventType type) { + return Event(view, point, type, 1); +} + class AutocompleteTextFieldTest : public PlatformTest { public: AutocompleteTextFieldTest() { @@ -34,7 +61,7 @@ class AutocompleteTextFieldTest : public PlatformTest { // decorations. NSRect frame = NSMakeRect(0, 0, 300, 30); field_.reset([[AutocompleteTextField alloc] initWithFrame:frame]); - [field_ setStringValue:@"Testing"]; + [field_ setStringValue:@"Test test"]; [field_ setObserver:&field_observer_]; [cocoa_helper_.contentView() addSubview:field_.get()]; @@ -332,6 +359,74 @@ TEST_F(AutocompleteTextFieldTest, ResetFieldEditorBlocksEndEditing) { } } +// Clicking in the search hint should put the caret in the rightmost +// position. +TEST_F(AutocompleteTextFieldTest, ClickSearchHintPutsCaretRightmost) { + // Set the decoration before becoming responder. + EXPECT_FALSE([field_ currentEditor]); + AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; + [cell setSearchHintString:@"Type to search"]; + + // Can't rely on the window machinery to make us first responder, + // here. + cocoa_helper_.makeFirstResponder(field_); + EXPECT_TRUE([field_ currentEditor]); + + const NSPoint point(NSMakePoint(300.0 - 20.0, 5.0)); + NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); + NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); + [NSApp postEvent:upEvent atStart:YES]; + [field_ mouseDown:downEvent]; + const NSRange selectedRange([[field_ currentEditor] selectedRange]); + EXPECT_EQ(selectedRange.location, [[field_ stringValue] length]); + EXPECT_EQ(selectedRange.length, 0U); +} + +// Clicking in the keyword-search should put the caret in the +// leftmost position. +TEST_F(AutocompleteTextFieldTest, ClickKeywordPutsCaretLeftmost) { + // Set the decoration before becoming responder. + EXPECT_FALSE([field_ currentEditor]); + AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; + [cell setKeywordString:@"Search Engine:"]; + + // Can't rely on the window machinery to make us first responder, + // here. + cocoa_helper_.makeFirstResponder(field_); + EXPECT_TRUE([field_ currentEditor]); + + const NSPoint point(NSMakePoint(20.0, 5.0)); + NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); + NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); + [NSApp postEvent:upEvent atStart:YES]; + [field_ mouseDown:downEvent]; + const NSRange selectedRange([[field_ currentEditor] selectedRange]); + EXPECT_EQ(selectedRange.location, 0U); + EXPECT_EQ(selectedRange.length, 0U); +} + +// Clicks not in the text area or the cell's decorations fall through +// to the editor. +TEST_F(AutocompleteTextFieldTest, ClickBorderSelectsAll) { + // Can't rely on the window machinery to make us first responder, + // here. + cocoa_helper_.makeFirstResponder(field_); + EXPECT_TRUE([field_ currentEditor]); + + const NSPoint point(NSMakePoint(20.0, 1.0)); + NSEvent* downEvent(Event(field_, point, NSLeftMouseDown)); + NSEvent* upEvent(Event(field_, point, NSLeftMouseUp)); + [NSApp postEvent:upEvent atStart:YES]; + [field_ mouseDown:downEvent]; + + // Clicking in the narrow border area around a Cocoa NSTextField + // does a select-all. Regardless of whether this is a good call, it + // works as a test that things get passed down to the editor. + const NSRange selectedRange([[field_ currentEditor] selectedRange]); + EXPECT_EQ(selectedRange.location, 0U); + EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]); +} + } // namespace @implementation AutocompleteTextFieldTestDelegate |