summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa
diff options
context:
space:
mode:
authorshess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-02 20:05:10 +0000
committershess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-02 20:05:10 +0000
commitc69b93f4dcfc77af1c6b221e1000ba3ce7883be8 (patch)
tree6be91ad221a9410ce33dd33781c5b6fd3b51d1df /chrome/browser/cocoa
parenta52b1cae064a01de97c59dc51ada747ebe327dde (diff)
downloadchromium_src-c69b93f4dcfc77af1c6b221e1000ba3ce7883be8.zip
chromium_src-c69b93f4dcfc77af1c6b221e1000ba3ce7883be8.tar.gz
chromium_src-c69b93f4dcfc77af1c6b221e1000ba3ce7883be8.tar.bz2
[Mac] Change Omnibox to select-all on mouse-up if the mouse didn't move.
This is to match Windows... http://crbug.com/22980 TEST=When Omnibox has focus, everything should work identically. TEST=Put focus in content. Single-click in Omnibox should select-all on mouse-up. TEST=Put focus in content. Click-drag in Omnibox should select text like normal. TEST=Put focus in content. Double-click should select-all on first mouse-up, but select-word on next click. TEST=Same test for triple-click. Review URL: http://codereview.chromium.org/243063 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27885 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa')
-rw-r--r--chrome/browser/cocoa/autocomplete_text_field.mm18
-rw-r--r--chrome/browser/cocoa/autocomplete_text_field_unittest.mm90
2 files changed, 108 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/autocomplete_text_field.mm b/chrome/browser/cocoa/autocomplete_text_field.mm
index 99d9194..1f2c23f4 100644
--- a/chrome/browser/cocoa/autocomplete_text_field.mm
+++ b/chrome/browser/cocoa/autocomplete_text_field.mm
@@ -111,6 +111,24 @@
if (NSMouseInRect(location, textFrame, [self isFlipped]) ||
!NSMouseInRect(location, fullFrame, [self isFlipped])) {
[super mouseDown:theEvent];
+
+ // After the event has been handled, if the current event is a
+ // mouse up and no selection was created (the mouse didn't move),
+ // select the entire field.
+ // NOTE(shess): This does not interfere with single-clicking to
+ // place caret after a selection is made. An NSTextField only has
+ // a selection when it has a field editor. The field editor is an
+ // NSText subview, which will receive the -mouseDown: in that
+ // case, and this code will never fire.
+ NSText* editor = [self currentEditor];
+ if (editor) {
+ NSEvent* currentEvent = [NSApp currentEvent];
+ if ([currentEvent type] == NSLeftMouseUp &&
+ ![editor selectedRange].length) {
+ [editor selectAll:nil];
+ }
+ }
+
return;
}
diff --git a/chrome/browser/cocoa/autocomplete_text_field_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_unittest.mm
index a821ec7..41cbe21 100644
--- a/chrome/browser/cocoa/autocomplete_text_field_unittest.mm
+++ b/chrome/browser/cocoa/autocomplete_text_field_unittest.mm
@@ -427,6 +427,96 @@ TEST_F(AutocompleteTextFieldTest, ClickBorderSelectsAll) {
EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]);
}
+// Single-click with no drag should setup a field editor and
+// select all.
+TEST_F(AutocompleteTextFieldTest, ClickSelectsAll) {
+ EXPECT_FALSE([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];
+ EXPECT_TRUE([field_ currentEditor]);
+ const NSRange selectedRange([[field_ currentEditor] selectedRange]);
+ EXPECT_EQ(selectedRange.location, 0U);
+ EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]);
+}
+
+// Click-drag selects text, not select all.
+TEST_F(AutocompleteTextFieldTest, ClickDragSelectsText) {
+ EXPECT_FALSE([field_ currentEditor]);
+
+ NSEvent* downEvent(Event(field_, NSMakePoint(20.0, 5.0), NSLeftMouseDown));
+ NSEvent* upEvent(Event(field_, NSMakePoint(0.0, 5.0), NSLeftMouseUp));
+ [NSApp postEvent:upEvent atStart:YES];
+ [field_ mouseDown:downEvent];
+ EXPECT_TRUE([field_ currentEditor]);
+
+ // Expect this to have selected a prefix of the content. Mostly
+ // just don't want the select-all behavior.
+ const NSRange selectedRange([[field_ currentEditor] selectedRange]);
+ EXPECT_EQ(selectedRange.location, 0U);
+ EXPECT_LT(selectedRange.length, [[field_ stringValue] length]);
+}
+
+// TODO(shess): Test that click/pause/click allows cursor placement.
+// In this case the first click goes to the field, but the second
+// click goes to the field editor, so the current testing pattern
+// can't work. What really needs to happen is to push through the
+// NSWindow event machinery so that we can say "two independent clicks
+// at the same location have the right effect". Once that is done, it
+// might make sense to revise the other tests to use the same
+// machinery.
+
+// Double-click selects word, not select all.
+TEST_F(AutocompleteTextFieldTest, DoubleClickSelectsWord) {
+ EXPECT_FALSE([field_ currentEditor]);
+
+ const NSPoint point(NSMakePoint(20.0, 5.0));
+ NSEvent* downEvent(Event(field_, point, NSLeftMouseDown, 1));
+ NSEvent* upEvent(Event(field_, point, NSLeftMouseUp, 1));
+ NSEvent* downEvent2(Event(field_, point, NSLeftMouseDown, 2));
+ NSEvent* upEvent2(Event(field_, point, NSLeftMouseUp, 2));
+ [NSApp postEvent:upEvent atStart:YES];
+ [field_ mouseDown:downEvent];
+ [NSApp postEvent:upEvent2 atStart:YES];
+ [field_ mouseDown:downEvent2];
+ EXPECT_TRUE([field_ currentEditor]);
+
+ // Selected the first word.
+ const NSRange selectedRange([[field_ currentEditor] selectedRange]);
+ const NSRange spaceRange([[field_ stringValue] rangeOfString:@" "]);
+ EXPECT_GT(spaceRange.location, 0U);
+ EXPECT_LT(spaceRange.length, [[field_ stringValue] length]);
+ EXPECT_EQ(selectedRange.location, 0U);
+ EXPECT_EQ(selectedRange.length, spaceRange.location);
+}
+
+TEST_F(AutocompleteTextFieldTest, TripleClickSelectsAll) {
+ EXPECT_FALSE([field_ currentEditor]);
+
+ const NSPoint point(NSMakePoint(20.0, 5.0));
+ NSEvent* downEvent(Event(field_, point, NSLeftMouseDown, 1));
+ NSEvent* upEvent(Event(field_, point, NSLeftMouseUp, 1));
+ NSEvent* downEvent2(Event(field_, point, NSLeftMouseDown, 2));
+ NSEvent* upEvent2(Event(field_, point, NSLeftMouseUp, 2));
+ NSEvent* downEvent3(Event(field_, point, NSLeftMouseDown, 3));
+ NSEvent* upEvent3(Event(field_, point, NSLeftMouseUp, 3));
+ [NSApp postEvent:upEvent atStart:YES];
+ [field_ mouseDown:downEvent];
+ [NSApp postEvent:upEvent2 atStart:YES];
+ [field_ mouseDown:downEvent2];
+ [NSApp postEvent:upEvent3 atStart:YES];
+ [field_ mouseDown:downEvent3];
+ EXPECT_TRUE([field_ currentEditor]);
+
+ // Selected the first word.
+ const NSRange selectedRange([[field_ currentEditor] selectedRange]);
+ EXPECT_EQ(selectedRange.location, 0U);
+ EXPECT_EQ(selectedRange.length, [[field_ stringValue] length]);
+}
+
} // namespace
@implementation AutocompleteTextFieldTestDelegate