diff options
author | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-23 21:31:39 +0000 |
---|---|---|
committer | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-23 21:31:39 +0000 |
commit | 599ea9fd7f5f029661916c191ff89b7358719f81 (patch) | |
tree | c72cab547f6dee71007cad75402a774e8bce735f /chrome | |
parent | 4e78ee1663d14ef7a087cefbd644652e53b9bdcb (diff) | |
download | chromium_src-599ea9fd7f5f029661916c191ff89b7358719f81.zip chromium_src-599ea9fd7f5f029661916c191ff89b7358719f81.tar.gz chromium_src-599ea9fd7f5f029661916c191ff89b7358719f81.tar.bz2 |
[Mac] Convert Omnibox paste-and-go to use AutocompleteTextFieldObserver.
Obvious result of Rohit's addition of paste-and-go and my refactor to
add AutocompleteTextFieldObserver. Bonus prize of unit tests for
cocoa/ part of Rohit's change.
Review URL: http://codereview.chromium.org/207047
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26993 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
9 files changed, 88 insertions, 75 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h index 5913c42..56d7cee 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.h +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.h @@ -93,6 +93,9 @@ class AutocompleteEditViewMac : public AutocompleteEditView, // Implement the AutocompleteTextFieldObserver interface. virtual void OnControlKeyChanged(bool pressed); virtual void OnPaste(); + virtual bool CanPasteAndGo(); + virtual int GetPasteActionStringId(); + virtual void OnPasteAndGo(); // Helper functions for use from AutocompleteEditHelper Objective-C // class. @@ -115,19 +118,6 @@ class AutocompleteEditViewMac : public AutocompleteEditView, // visual state (such as closing the popup). void OnDidResignKey(); - // Returns true if the current clipboard text supports paste and go (or paste - // and search). - bool CanPasteAndGo(); - - // Returns the appropriate "Paste and Go" or "Paste and Search" context menu - // string, depending on what is currently in the clipboard. Must not be - // called unless CanPasteAndGo() returns true. - int GetPasteActionStringId(); - - // Called when the user initiates a "paste and go" or "paste and search" into - // |field_|. - void OnPasteAndGo(); - // Checks if a keyword search is possible and forwards to |model_| // if so. Returns true if the tab should be eaten. bool OnTabPressed(); diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm index aa245e6..055dc66 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm @@ -862,18 +862,6 @@ std::wstring AutocompleteEditViewMac::GetClipboardText(Clipboard* clipboard) { // it's set to the start of the text. } -- (NSString*)control:(NSControl*)control - textPasteActionString:(NSText*)fieldEditor { - if (!edit_view_->CanPasteAndGo()) - return nil; - - return l10n_util::GetNSStringWithFixup(edit_view_->GetPasteActionStringId()); -} - -- (void)control:(NSControl*)control textDidPasteAndGo:(NSText*)fieldEditor { - edit_view_->OnPasteAndGo(); -} - // Signal that we've lost focus when the window resigns key. - (void)windowDidResignKey:(NSNotification*)notification { edit_view_->OnDidResignKey(); diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm index 6e468af..d3b5c5b 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm @@ -326,6 +326,7 @@ void AutocompletePopupViewMac::CreatePopupIfNeeded() { void AutocompletePopupViewMac::UpdatePopupAppearance() { const AutocompleteResult& result = model_->result(); if (result.empty()) { + NSLog(@"Empty results in UpdatePopupAppearance"); [[popup_ parentWindow] removeChildWindow:popup_]; [popup_ orderOut:nil]; diff --git a/chrome/browser/cocoa/autocomplete_text_field.h b/chrome/browser/cocoa/autocomplete_text_field.h index 2f87c43..9c8b1c5 100644 --- a/chrome/browser/cocoa/autocomplete_text_field.h +++ b/chrome/browser/cocoa/autocomplete_text_field.h @@ -35,14 +35,21 @@ class AutocompleteTextFieldObserver { // Called when the user pastes into the field. virtual void OnPaste() = 0; -}; -@protocol AutocompleteTextFieldDelegateMethods -// Returns nil if paste actions are not supported. -- (NSString*)control:(NSControl*)control - textPasteActionString:(NSText*)fieldEditor; -- (void)control:(NSControl*)control textDidPasteAndGo:(NSText*)fieldEditor; -@end + // Returns true if the current clipboard text supports paste and go + // (or paste and search). + virtual bool CanPasteAndGo() = 0; + + // Returns the appropriate "Paste and Go" or "Paste and Search" + // context menu string, depending on what is currently in the + // clipboard. Must not be called unless CanPasteAndGo() returns + // true. + virtual int GetPasteActionStringId() = 0; + + // Called when the user initiates a "paste and go" or "paste and + // search" into |field_|. + virtual void OnPasteAndGo() = 0; +}; @interface AutocompleteTextField : NSTextField { @private diff --git a/chrome/browser/cocoa/autocomplete_text_field.mm b/chrome/browser/cocoa/autocomplete_text_field.mm index efc114a..f48d508 100644 --- a/chrome/browser/cocoa/autocomplete_text_field.mm +++ b/chrome/browser/cocoa/autocomplete_text_field.mm @@ -19,21 +19,6 @@ DCHECK([[self cell] isKindOfClass:[AutocompleteTextFieldCell class]]); } -- (NSString*)textPasteActionString:(NSText*)fieldEditor { - id delegate = [self delegate]; - if ([delegate respondsToSelector:@selector(control:textPasteActionString:)]) { - return [delegate control:self textPasteActionString:fieldEditor]; - } - return nil; -} - -- (void)textDidPasteAndGo:(NSText*)fieldEditor { - id delegate = [self delegate]; - if ([delegate respondsToSelector:@selector(control:textDidPasteAndGo:)]) { - [delegate control:self textDidPasteAndGo:fieldEditor]; - } -} - - (void)flagsChanged:(NSEvent*)theEvent { bool controlFlag = ([theEvent modifierFlags]&NSControlKeyMask) != 0; observer_->OnControlKeyChanged(controlFlag); diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor.h b/chrome/browser/cocoa/autocomplete_text_field_editor.h index 0821e08..e7e586f 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_editor.h +++ b/chrome/browser/cocoa/autocomplete_text_field_editor.h @@ -13,15 +13,6 @@ class AutocompleteTextFieldObserver; // is styled but should not appear that way when copied to the // pasteboard). -// TODO(shess): Move delegate stuff to AutocompleteTextFieldObserver. - -@protocol AutocompleteTextFieldEditorDelegateMethods - -// Returns nil if paste actions are not supported. -- (NSString*)textPasteActionString:(NSText*)fieldEditor; -- (void)textDidPasteAndGo:(NSText*)fieldEditor; -@end - // Field editor used for the autocomplete field. @interface AutocompleteTextFieldEditor : NSTextView { } @@ -37,4 +28,5 @@ class AutocompleteTextFieldObserver; @interface AutocompleteTextFieldEditor(PrivateTestMethods) - (AutocompleteTextFieldObserver*)observer; +- (void)pasteAndGo:sender; @end diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor.mm b/chrome/browser/cocoa/autocomplete_text_field_editor.mm index 6516e4b..9cefff9 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_editor.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_editor.mm @@ -56,9 +56,11 @@ } - (void)pasteAndGo:sender { - id delegate = [self delegate]; - if ([delegate respondsToSelector:@selector(textDidPasteAndGo:)]) - [delegate textDidPasteAndGo:self]; + AutocompleteTextFieldObserver* observer = [self observer]; + DCHECK(observer); + if (observer) { + observer->OnPasteAndGo(); + } } // We have rich text, but it shouldn't be modified by the user, so @@ -76,26 +78,29 @@ - (NSMenu*)menuForEvent:(NSEvent*)event { NSMenu* menu = [[[NSMenu alloc] initWithTitle:@"TITLE"] autorelease]; - [menu insertItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_CUT) - action:@selector(cut:) - keyEquivalent:@"" atIndex:0]; - [menu insertItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_COPY) - action:@selector(copy:) - keyEquivalent:@"" atIndex:1]; - [menu insertItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_PASTE) - action:@selector(paste:) - keyEquivalent:@"" atIndex:2]; + [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_CUT) + action:@selector(cut:) + keyEquivalent:@""]; + [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_COPY) + action:@selector(copy:) + keyEquivalent:@""]; + [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(IDS_PASTE) + action:@selector(paste:) + keyEquivalent:@""]; // Paste and go/search. - id delegate = [self delegate]; + AutocompleteTextFieldObserver* observer = [self observer]; + DCHECK(observer); + if (observer && observer->CanPasteAndGo()) { + const int string_id = observer->GetPasteActionStringId(); + NSString* label = l10n_util::GetNSStringWithFixup(string_id); - if ([delegate respondsToSelector:@selector(textPasteActionString:)]) { - NSString* label = [delegate textPasteActionString:self]; - // TODO(rohitrao): If the clipboard is empty, should we show a greyed-out - // "Paste and Go" or nothing at all? + // TODO(rohitrao): If the clipboard is empty, should we show a + // greyed-out "Paste and Go" or nothing at all? if (label) { - [menu insertItemWithTitle:label action:@selector(pasteAndGo:) - keyEquivalent:@"" atIndex:3]; + [menu addItemWithTitle:label + action:@selector(pasteAndGo:) + keyEquivalent:@""]; } } diff --git a/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm b/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm index bdb4962..c4ec346 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm +++ b/chrome/browser/cocoa/autocomplete_text_field_editor_unittest.mm @@ -9,9 +9,12 @@ #include "base/string_util.h" #import "chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h" #import "chrome/browser/cocoa/cocoa_test_helper.h" +#include "grit/generated_resources.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" +using ::testing::Return; + namespace { int NumTypesOnPasteboard(NSPasteboard* pb) { @@ -156,4 +159,43 @@ TEST_F(AutocompleteTextFieldEditorTest, Paste) { [editor_.get() paste:nil]; } +// Test that -pasteAndGo: is correctly delegated to the observer. +TEST_F(AutocompleteTextFieldEditorTest, PasteAndGo) { + EXPECT_CALL(field_observer_, OnPasteAndGo()); + [editor_.get() pasteAndGo:nil]; +} + +// Test that the menu is constructed correctly when CanPasteAndGo(). +TEST_F(AutocompleteTextFieldEditorTest, CanPasteAndGoMenu) { + EXPECT_CALL(field_observer_, CanPasteAndGo()) + .WillOnce(Return(true)); + EXPECT_CALL(field_observer_, GetPasteActionStringId()) + .WillOnce(Return(IDS_PASTE_AND_GO)); + + NSMenu* menu = [editor_.get() menuForEvent:nil]; + NSArray* items = [menu itemArray]; + ASSERT_EQ([items count], 4U); + // TODO(shess): Check the titles, too? + NSUInteger i = 0; // Use an index to make future changes easier. + EXPECT_EQ([[items objectAtIndex:i++] action], @selector(cut:)); + EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copy:)); + EXPECT_EQ([[items objectAtIndex:i++] action], @selector(paste:)); + EXPECT_EQ([[items objectAtIndex:i++] action], @selector(pasteAndGo:)); +} + +// Test that the menu is constructed correctly when !CanPasteAndGo(). +TEST_F(AutocompleteTextFieldEditorTest, CannotPasteAndGoMenu) { + EXPECT_CALL(field_observer_, CanPasteAndGo()) + .WillOnce(Return(false)); + + NSMenu* menu = [editor_.get() menuForEvent:nil]; + NSArray* items = [menu itemArray]; + ASSERT_EQ([items count], 3U); + // TODO(shess): Check the titles, too? + NSUInteger i = 0; // Use an index to make future changes easier. + EXPECT_EQ([[items objectAtIndex:i++] action], @selector(cut:)); + EXPECT_EQ([[items objectAtIndex:i++] action], @selector(copy:)); + EXPECT_EQ([[items objectAtIndex:i++] action], @selector(paste:)); +} + } // namespace diff --git a/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h b/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h index 378cc00..0993510 100644 --- a/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h +++ b/chrome/browser/cocoa/autocomplete_text_field_unittest_helper.h @@ -32,6 +32,9 @@ class MockAutocompleteTextFieldObserver : public AutocompleteTextFieldObserver { public: MOCK_METHOD1(OnControlKeyChanged, void(bool pressed)); MOCK_METHOD0(OnPaste, void()); + MOCK_METHOD0(CanPasteAndGo, bool()); + MOCK_METHOD0(GetPasteActionStringId, int()); + MOCK_METHOD0(OnPasteAndGo, void()); }; } // namespace |