diff options
author | dmazzoni@chromium.org <dmazzoni@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-24 14:27:08 +0000 |
---|---|---|
committer | dmazzoni@chromium.org <dmazzoni@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-24 14:27:08 +0000 |
commit | 434c0eb886cf13bcaa77652c9bfb104dc04f5e9e (patch) | |
tree | 55c1f1997a4dffe086a683f711ad894bea1db4f4 /chrome | |
parent | 9ab8dda6a57fc68c21e14d7d249cf711accfaa62 (diff) | |
download | chromium_src-434c0eb886cf13bcaa77652c9bfb104dc04f5e9e.zip chromium_src-434c0eb886cf13bcaa77652c9bfb104dc04f5e9e.tar.gz chromium_src-434c0eb886cf13bcaa77652c9bfb104dc04f5e9e.tar.bz2 |
Keyboard accessibility for the page and app menus.
Works on Windows, and on Linux with toolkit_views.
The goal is to make Chrome behave more like a standard Windows
application, for users who rely on the keyboard and expect standard
keyboard accelerators to work.
Pressing F10, or pressing and releasing Alt, will set focus to the
Page menu, as if it was the first item in a menu bar.
Pressing enter, space, up arrow, or down arrow will open the focused menu.
Once a menu is opened, pressing left and right arrows will switch between
the two menus. Pressing escape will return focus to the title of the
previously open menu.
A new UI test attempts to select something from the menus using only the
keyboard. It works on Linux (with toolkit_views) and on Windows.
BUG=none
TEST=New keyboard accessibility ui test.
Review URL: http://codereview.chromium.org/660323
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42465 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
29 files changed, 717 insertions, 37 deletions
diff --git a/chrome/app/chrome_dll.rc b/chrome/app/chrome_dll.rc index 7ed9edf..14cee6d 100644 --- a/chrome/app/chrome_dll.rc +++ b/chrome/app/chrome_dll.rc @@ -48,6 +48,8 @@ BEGIN VK_F6, IDC_FOCUS_LOCATION, VIRTKEY "D", IDC_FOCUS_LOCATION, VIRTKEY, ALT "L", IDC_FOCUS_LOCATION, VIRTKEY, CONTROL + VK_F10, IDC_FOCUS_MENU_BAR, VIRTKEY + VK_MENU, IDC_FOCUS_MENU_BAR, VIRTKEY "K", IDC_FOCUS_SEARCH, VIRTKEY, CONTROL "E", IDC_FOCUS_SEARCH, VIRTKEY, CONTROL "T", IDC_FOCUS_TOOLBAR, VIRTKEY, SHIFT, ALT diff --git a/chrome/app/chrome_dll_resource.h b/chrome/app/chrome_dll_resource.h index 708eaa2..ed5bc03 100644 --- a/chrome/app/chrome_dll_resource.h +++ b/chrome/app/chrome_dll_resource.h @@ -189,6 +189,7 @@ #define IDC_FOCUS_TOOLBAR 39000 #define IDC_FOCUS_LOCATION 39001 #define IDC_FOCUS_SEARCH 39002 +#define IDC_FOCUS_MENU_BAR 39003 // Show various bits of UI #define IDC_OPEN_FILE 40000 diff --git a/chrome/browser/automation/automation_provider.cc b/chrome/browser/automation/automation_provider.cc index a2b7d8d..ddde680 100644 --- a/chrome/browser/automation/automation_provider.cc +++ b/chrome/browser/automation/automation_provider.cc @@ -139,7 +139,8 @@ class ClickTask : public Task { AutomationProvider::AutomationProvider(Profile* profile) : redirect_query_(0), profile_(profile), - reply_message_(NULL) { + reply_message_(NULL), + popup_menu_waiter_(NULL) { browser_tracker_.reset(new AutomationBrowserTracker(this)); tab_tracker_.reset(new AutomationTabTracker(this)); window_tracker_.reset(new AutomationWindowTracker(this)); @@ -497,6 +498,14 @@ void AutomationProvider::OnMessageReceived(const IPC::Message& message) { IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass, LoginWithUserAndPass) #endif +#if defined(TOOLKIT_VIEWS) + IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForFocusedViewIDToChange, + WaitForFocusedViewIDToChange) + IPC_MESSAGE_HANDLER(AutomationMsg_StartTrackingPopupMenus, + StartTrackingPopupMenus) + IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForPopupMenuToOpen, + WaitForPopupMenuToOpen) +#endif IPC_END_MESSAGE_MAP() } @@ -2528,3 +2537,23 @@ void AutomationProvider::SetContentSetting( *success = true; } } + +#if !defined(TOOLKIT_VIEWS) +void AutomationProvider::GetFocusedViewID(int handle, int* view_id) { + NOTIMPLEMENTED(); +}; + +void AutomationProvider::WaitForFocusedViewIDToChange( + int handle, int previous_view_id, IPC::Message* reply_message) { + NOTIMPLEMENTED(); +} + +void AutomationProvider::StartTrackingPopupMenus( + int browser_handle, bool* success) { + NOTIMPLEMENTED(); +} + +void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) { + NOTIMPLEMENTED(); +} +#endif // !defined(TOOLKIT_VIEWS) diff --git a/chrome/browser/automation/automation_provider.h b/chrome/browser/automation/automation_provider.h index e5b36bf..2328606 100644 --- a/chrome/browser/automation/automation_provider.h +++ b/chrome/browser/automation/automation_provider.h @@ -35,6 +35,7 @@ #endif // defined(OS_WIN) struct AutomationMsg_Find_Params; +class PopupMenuWaiter; namespace IPC { struct Reposition_Params; @@ -133,6 +134,7 @@ class AutomationProvider : public base::RefCounted<AutomationProvider>, protected: friend class base::RefCounted<AutomationProvider>; + friend class PopupMenuWaiter; virtual ~AutomationProvider(); private: @@ -579,6 +581,22 @@ class AutomationProvider : public base::RefCounted<AutomationProvider>, // Returns NULL on failure. RenderViewHost* GetViewForTab(int tab_handle); + // Block until the focused view ID changes to something other than + // previous_view_id. + void WaitForFocusedViewIDToChange(int handle, + int previous_view_id, + IPC::Message* reply_message); + + // Start tracking popup menus. Must be called before executing the + // command that might open the popup menu; then call WaitForPopupMenuToOpen. + void StartTrackingPopupMenus(int browser_handle, bool* success); + + // Wait until a popup menu has opened. + void WaitForPopupMenuToOpen(IPC::Message* reply_message); + + // Method called by the popup menu tracker when a popup menu is opened. + void NotifyPopupMenuOpened(); + typedef ObserverList<NotificationObserver> NotificationObserverList; typedef std::map<NavigationController*, LoginHandler*> LoginHandlerMap; typedef std::map<int, ExtensionPortContainer*> PortContainerMap; @@ -614,6 +632,13 @@ class AutomationProvider : public base::RefCounted<AutomationProvider>, IPC::Message* reply_message_; + // Keep track of whether a popup menu has been opened since the last time + // that StartTrackingPopupMenus has been called. + bool popup_menu_opened_; + + // A temporary object that receives a notification when a popup menu opens. + PopupMenuWaiter* popup_menu_waiter_; + DISALLOW_COPY_AND_ASSIGN(AutomationProvider); }; diff --git a/chrome/browser/automation/automation_provider_gtk.cc b/chrome/browser/automation/automation_provider_gtk.cc index 601e81a..62b378f 100644 --- a/chrome/browser/automation/automation_provider_gtk.cc +++ b/chrome/browser/automation/automation_provider_gtk.cc @@ -79,10 +79,6 @@ void AutomationProvider::IsWindowMaximized(int handle, bool* is_maximized, NOTIMPLEMENTED(); } -void AutomationProvider::GetFocusedViewID(int handle, int* view_id) { - NOTIMPLEMENTED(); -} - void AutomationProvider::PrintAsync(int tab_handle) { NOTIMPLEMENTED(); } @@ -227,4 +223,3 @@ void AutomationProvider::GetWindowTitle(int handle, string16* text) { const gchar* title = gtk_window_get_title(window); text->assign(UTF8ToUTF16(title)); } - diff --git a/chrome/browser/automation/automation_provider_mac.mm b/chrome/browser/automation/automation_provider_mac.mm index f58b7a8..2d3f3c5 100644 --- a/chrome/browser/automation/automation_provider_mac.mm +++ b/chrome/browser/automation/automation_provider_mac.mm @@ -66,10 +66,6 @@ void AutomationProvider::IsWindowMaximized(int handle, bool* is_maximized, NOTIMPLEMENTED(); } -void AutomationProvider::GetFocusedViewID(int handle, int* view_id) { - NOTIMPLEMENTED(); -} - void AutomationProvider::PrintAsync(int tab_handle) { NOTIMPLEMENTED(); } @@ -158,4 +154,3 @@ void AutomationProvider::GetWindowTitle(int handle, string16* text) { text->assign(base::SysNSStringToUTF16(title)); } - diff --git a/chrome/browser/automation/automation_provider_views.cc b/chrome/browser/automation/automation_provider_views.cc index f161105..f7a461c 100644 --- a/chrome/browser/automation/automation_provider_views.cc +++ b/chrome/browser/automation/automation_provider_views.cc @@ -4,7 +4,12 @@ #include "chrome/browser/automation/automation_provider.h" +#include "chrome/browser/views/frame/browser_view.h" +#include "chrome/browser/views/toolbar_view.h" +#include "chrome/test/automation/automation_messages.h" #include "gfx/point.h" +#include "views/controls/menu/menu_wrapper.h" +#include "views/focus/focus_manager.h" #include "views/view.h" #include "views/widget/root_view.h" #include "views/widget/widget.h" @@ -34,3 +39,150 @@ void AutomationProvider::WindowGetViewBounds(int handle, int view_id, } } +void AutomationProvider::GetFocusedViewID(int handle, int* view_id) { + *view_id = -1; + if (window_tracker_->ContainsHandle(handle)) { + gfx::NativeWindow window = window_tracker_->GetResource(handle); + views::FocusManager* focus_manager = + views::FocusManager::GetFocusManagerForNativeWindow(window); + DCHECK(focus_manager); + views::View* focused_view = focus_manager->GetFocusedView(); + if (focused_view) + *view_id = focused_view->GetID(); + } +} + +// Helper class that waits until the focus has changed to a view other +// than the one with the provided view id. +class ViewFocusChangeWaiter : public views::FocusChangeListener { + public: + ViewFocusChangeWaiter(views::FocusManager* focus_manager, + int previous_view_id, + AutomationProvider* automation, + IPC::Message* reply_message) + : focus_manager_(focus_manager), + previous_view_id_(previous_view_id), + automation_(automation), + reply_message_(reply_message), + ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { + focus_manager_->AddFocusChangeListener(this); + // Call the focus change notification once in case the focus has + // already changed. + FocusWillChange(NULL, focus_manager_->GetFocusedView()); + } + + ~ViewFocusChangeWaiter() { + focus_manager_->RemoveFocusChangeListener(this); + } + + // Inherited from FocusChangeListener + virtual void FocusWillChange(views::View* focused_before, + views::View* focused_now) { + // This listener is called before focus actually changes. Post a task + // that will get run after focus changes. + MessageLoop::current()->PostTask( + FROM_HERE, + method_factory_.NewRunnableMethod( + &ViewFocusChangeWaiter::FocusChanged, + focused_before, + focused_now)); + } + + private: + void FocusChanged(views::View* focused_before, + views::View* focused_now) { + if (focused_now && focused_now->GetID() != previous_view_id_) { + AutomationMsg_WaitForFocusedViewIDToChange::WriteReplyParams( + reply_message_, true, focused_now->GetID()); + + automation_->Send(reply_message_); + delete this; + } + } + + views::FocusManager* focus_manager_; + int previous_view_id_; + AutomationProvider* automation_; + IPC::Message* reply_message_; + ScopedRunnableMethodFactory<ViewFocusChangeWaiter> method_factory_; + + DISALLOW_COPY_AND_ASSIGN(ViewFocusChangeWaiter); +}; + +void AutomationProvider::WaitForFocusedViewIDToChange( + int handle, int previous_view_id, IPC::Message* reply_message) { + if (!window_tracker_->ContainsHandle(handle)) + return; + gfx::NativeWindow window = window_tracker_->GetResource(handle); + views::FocusManager* focus_manager = + views::FocusManager::GetFocusManagerForNativeWindow(window); + + // The waiter will respond to the IPC and delete itself when done. + new ViewFocusChangeWaiter(focus_manager, + previous_view_id, + this, + reply_message); +} + +class PopupMenuWaiter : public views::MenuListener { + public: + PopupMenuWaiter(ToolbarView* toolbar_view, + AutomationProvider* automation) + : toolbar_view_(toolbar_view), + automation_(automation), + reply_message_(NULL) { + toolbar_view_->AddMenuListener(this); + } + + // Implementation of views::MenuListener + virtual void OnMenuOpened() { + toolbar_view_->RemoveMenuListener(this); + automation_->popup_menu_opened_ = true; + automation_->popup_menu_waiter_ = NULL; + if (reply_message_) { + AutomationMsg_WaitForPopupMenuToOpen::WriteReplyParams( + reply_message_, true); + automation_->Send(reply_message_); + } + delete this; + } + + void set_reply_message(IPC::Message* reply_message) { + reply_message_ = reply_message; + } + + private: + ToolbarView* toolbar_view_; + AutomationProvider* automation_; + IPC::Message* reply_message_; + + DISALLOW_COPY_AND_ASSIGN(PopupMenuWaiter); +}; + +void AutomationProvider::StartTrackingPopupMenus( + int browser_handle, bool* success) { + if (browser_tracker_->ContainsHandle(browser_handle)) { + Browser* browser = browser_tracker_->GetResource(browser_handle); + BrowserView* browser_view = reinterpret_cast<BrowserView*>( + browser->window()); + ToolbarView* toolbar_view = browser_view->GetToolbarView(); + popup_menu_opened_ = false; + popup_menu_waiter_ = new PopupMenuWaiter(toolbar_view, this); + *success = true; + } +} + +void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) { + // See if the menu already opened and return true if so. + if (popup_menu_opened_) { + AutomationMsg_WaitForPopupMenuToOpen::WriteReplyParams( + reply_message, true); + Send(reply_message); + return; + } + + // Otherwise, register this reply message with the waiter, + // which will handle responding to this IPC when the popup + // menu opens. + popup_menu_waiter_->set_reply_message(reply_message); +} diff --git a/chrome/browser/automation/automation_provider_win.cc b/chrome/browser/automation/automation_provider_win.cc index 9539498..2139420 100644 --- a/chrome/browser/automation/automation_provider_win.cc +++ b/chrome/browser/automation/automation_provider_win.cc @@ -231,19 +231,6 @@ void AutomationProvider::WindowSimulateDrag(int handle, } } -void AutomationProvider::GetFocusedViewID(int handle, int* view_id) { - *view_id = -1; - if (window_tracker_->ContainsHandle(handle)) { - HWND hwnd = window_tracker_->GetResource(handle); - views::FocusManager* focus_manager = - views::FocusManager::GetFocusManagerForNativeView(hwnd); - DCHECK(focus_manager); - views::View* focused_view = focus_manager->GetFocusedView(); - if (focused_view) - *view_id = focused_view->GetID(); - } -} - void AutomationProvider::GetWindowBounds(int handle, gfx::Rect* bounds, bool* success) { *success = false; diff --git a/chrome/browser/automation/ui_controls_win.cc b/chrome/browser/automation/ui_controls_win.cc index 3ff1a5c..aba7248 100644 --- a/chrome/browser/automation/ui_controls_win.cc +++ b/chrome/browser/automation/ui_controls_win.cc @@ -166,6 +166,21 @@ bool SendKeyPressImpl(base::KeyboardCode key, scoped_refptr<InputDispatcher> dispatcher( task ? new InputDispatcher(task, WM_KEYUP) : NULL); + // If a pop-up menu is open, it won't receive events sent using SendInput. + // Check for a pop-up menu using its window class (#32768) and if one + // exists, send the key event directly there. + HWND popup_menu = ::FindWindow(L"#32768", 0); + if (popup_menu != NULL) { + WPARAM w_param = win_util::KeyboardCodeToWin(key); + LPARAM l_param = 0; + ::SendMessage(popup_menu, WM_KEYDOWN, w_param, l_param); + ::SendMessage(popup_menu, WM_KEYUP, w_param, l_param); + + if (dispatcher.get()) + dispatcher->AddRef(); + return true; + } + INPUT input[8] = { 0 }; // 8, assuming all the modifiers are activated int i = 0; diff --git a/chrome/browser/browser.cc b/chrome/browser/browser.cc index 4e37d4c..04524e3 100644 --- a/chrome/browser/browser.cc +++ b/chrome/browser/browser.cc @@ -874,6 +874,9 @@ void Browser::UpdateCommandsForFullscreenMode(bool is_fullscreen) { command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, show_main_ui); command_updater_.UpdateCommandEnabled(IDC_FOCUS_LOCATION, show_main_ui); command_updater_.UpdateCommandEnabled(IDC_FOCUS_SEARCH, show_main_ui); + command_updater_.UpdateCommandEnabled( + IDC_FOCUS_MENU_BAR, + show_main_ui && !is_fullscreen && (type() & TYPE_POPUP) == 0); // Show various bits of UI command_updater_.UpdateCommandEnabled(IDC_DEVELOPER_MENU, show_main_ui); @@ -1322,6 +1325,12 @@ void Browser::FocusToolbar() { window_->FocusToolbar(); } +void Browser::FocusPageAndAppMenus() { + UserMetrics::RecordAction( + UserMetricsAction("FocusPageAndAppMenus"), profile_); + window_->FocusPageAndAppMenus(); +} + void Browser::FocusLocationBar() { UserMetrics::RecordAction(UserMetricsAction("FocusLocation"), profile_); window_->SetFocusToLocationBar(); @@ -1752,6 +1761,7 @@ void Browser::ExecuteCommandWithDisposition( case IDC_FOCUS_TOOLBAR: FocusToolbar(); break; case IDC_FOCUS_LOCATION: FocusLocationBar(); break; case IDC_FOCUS_SEARCH: FocusSearch(); break; + case IDC_FOCUS_MENU_BAR: FocusPageAndAppMenus(); break; // Show various bits of UI case IDC_OPEN_FILE: OpenFile(); break; diff --git a/chrome/browser/browser.h b/chrome/browser/browser.h index 51470f7..fad8295 100644 --- a/chrome/browser/browser.h +++ b/chrome/browser/browser.h @@ -460,6 +460,7 @@ class Browser : public TabStripModelDelegate, void FocusToolbar(); void FocusLocationBar(); void FocusSearch(); + void FocusPageAndAppMenus(); // Show various bits of UI void OpenFile(); diff --git a/chrome/browser/browser_window.h b/chrome/browser/browser_window.h index a4ff572..c2e3911 100644 --- a/chrome/browser/browser_window.h +++ b/chrome/browser/browser_window.h @@ -150,6 +150,11 @@ class BrowserWindow { // Focuses the toolbar (for accessibility). virtual void FocusToolbar() = 0; + // Focuses the page and app menus like they were a menu bar. + // + // Not used on the Mac, which has a "normal" menu bar. + virtual void FocusPageAndAppMenus() = 0; + // Returns whether the bookmark bar is visible or not. virtual bool IsBookmarkBarVisible() const = 0; diff --git a/chrome/browser/cocoa/browser_window_cocoa.h b/chrome/browser/cocoa/browser_window_cocoa.h index b0ace80..4de884a 100644 --- a/chrome/browser/cocoa/browser_window_cocoa.h +++ b/chrome/browser/cocoa/browser_window_cocoa.h @@ -57,6 +57,7 @@ class BrowserWindowCocoa : public BrowserWindow, virtual void UpdateToolbar(TabContents* contents, bool should_restore_state); virtual void FocusToolbar(); + virtual void FocusPageAndAppMenus(); virtual bool IsBookmarkBarVisible() const; virtual bool IsBookmarkBarAnimating() const; virtual bool IsToolbarVisible() const; diff --git a/chrome/browser/cocoa/browser_window_cocoa.mm b/chrome/browser/cocoa/browser_window_cocoa.mm index 01d592e..83706d7 100644 --- a/chrome/browser/cocoa/browser_window_cocoa.mm +++ b/chrome/browser/cocoa/browser_window_cocoa.mm @@ -237,6 +237,10 @@ void BrowserWindowCocoa::FocusToolbar() { NOTIMPLEMENTED(); } +void BrowserWindowCocoa::FocusPageAndAppMenus() { + // Chrome uses the standard Mac OS X menu bar, so this isn't needed. +} + bool BrowserWindowCocoa::IsBookmarkBarVisible() const { return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); } diff --git a/chrome/browser/gtk/browser_window_gtk.cc b/chrome/browser/gtk/browser_window_gtk.cc index 39e6348..4357b78 100644 --- a/chrome/browser/gtk/browser_window_gtk.cc +++ b/chrome/browser/gtk/browser_window_gtk.cc @@ -783,6 +783,10 @@ void BrowserWindowGtk::FocusToolbar() { NOTIMPLEMENTED(); } +void BrowserWindowGtk::FocusPageAndAppMenus() { + NOTIMPLEMENTED(); +} + bool BrowserWindowGtk::IsBookmarkBarVisible() const { return browser_->SupportsWindowFeature(Browser::FEATURE_BOOKMARKBAR) && bookmark_bar_.get() && diff --git a/chrome/browser/gtk/browser_window_gtk.h b/chrome/browser/gtk/browser_window_gtk.h index f24e094..67613b6 100644 --- a/chrome/browser/gtk/browser_window_gtk.h +++ b/chrome/browser/gtk/browser_window_gtk.h @@ -75,6 +75,7 @@ class BrowserWindowGtk : public BrowserWindow, virtual void UpdateToolbar(TabContents* contents, bool should_restore_state); virtual void FocusToolbar(); + virtual void FocusPageAndAppMenus(); virtual bool IsBookmarkBarVisible() const; virtual bool IsBookmarkBarAnimating() const; virtual bool IsToolbarVisible() const; diff --git a/chrome/browser/views/accelerator_table_gtk.cc b/chrome/browser/views/accelerator_table_gtk.cc index 28a630b..f3a7a54 100644 --- a/chrome/browser/views/accelerator_table_gtk.cc +++ b/chrome/browser/views/accelerator_table_gtk.cc @@ -20,6 +20,8 @@ const AcceleratorMapping kAcceleratorMap[] = { { base::VKEY_L, false, true, false, IDC_FOCUS_LOCATION }, { base::VKEY_D, false, false, true, IDC_FOCUS_LOCATION }, { base::VKEY_F6, false, false, false, IDC_FOCUS_LOCATION }, + { base::VKEY_F10, false, false, false, IDC_FOCUS_MENU_BAR }, + { base::VKEY_MENU, false, false, false, IDC_FOCUS_MENU_BAR }, // Tab/window controls. { base::VKEY_T, false, true, false, IDC_NEW_TAB }, diff --git a/chrome/browser/views/frame/browser_view.cc b/chrome/browser/views/frame/browser_view.cc index 302e13f..3c44731 100644 --- a/chrome/browser/views/frame/browser_view.cc +++ b/chrome/browser/views/frame/browser_view.cc @@ -638,10 +638,10 @@ void BrowserView::TraverseNextAccessibleToolbar(bool forward) { // TODO(mohamed) This needs to be smart, that applies to all toolbars. // Currently it just traverses between bookmarks and toolbar. if (!forward && toolbar_->IsVisible() && toolbar_->IsEnabled()) { - toolbar_->InitiateTraversal(last_focused_view_storage_id_); + toolbar_->RequestFocus(); } else if (forward && bookmark_bar_view_->IsVisible() && bookmark_bar_view_->IsEnabled()) { - bookmark_bar_view_->InitiateTraversal(last_focused_view_storage_id_); + bookmark_bar_view_->RequestFocus(); } } @@ -863,17 +863,31 @@ void BrowserView::UpdateToolbar(TabContents* contents, } void BrowserView::FocusToolbar() { - // Remove existing views in the storage, traversal should be restarted. + // Start the traversal within the main toolbar, passing it the storage id + // of the view where focus should be returned if the user exits the toolbar. + SaveFocusedView(); + toolbar_->InitiateTraversal(last_focused_view_storage_id_); +} + +void BrowserView::FocusPageAndAppMenus() { + // Chrome doesn't have a traditional menu bar, but it has menu buttons in + // the main toolbar that play the same role. If the user presses a key + // that would typically focus the menu bar, tell the toolbar to focus + // the first menu button. Pass it the storage id of the view where + // focus should be returned if the user presses escape. + // + // Not used on the Mac, which has a normal menu bar. + SaveFocusedView(); + toolbar_->EnterMenuBarEmulationMode(last_focused_view_storage_id_, NULL); +} + +void BrowserView::SaveFocusedView() { views::ViewStorage* view_storage = views::ViewStorage::GetSharedInstance(); if (view_storage->RetrieveView(last_focused_view_storage_id_)) view_storage->RemoveView(last_focused_view_storage_id_); - - // Store the last focused view into the storage, to handle existing traversal. - view_storage->StoreView(last_focused_view_storage_id_, - GetRootView()->GetFocusedView()); - - // Start the traversal within the main toolbar. - toolbar_->InitiateTraversal(last_focused_view_storage_id_); + views::View* focused_view = GetRootView()->GetFocusedView(); + if (focused_view) + view_storage->StoreView(last_focused_view_storage_id_, focused_view); } void BrowserView::DestroyBrowser() { diff --git a/chrome/browser/views/frame/browser_view.h b/chrome/browser/views/frame/browser_view.h index bf753e9..026fa53 100644 --- a/chrome/browser/views/frame/browser_view.h +++ b/chrome/browser/views/frame/browser_view.h @@ -270,6 +270,7 @@ class BrowserView : public BrowserBubbleHost, virtual void UpdateStopGoState(bool is_loading, bool force); virtual void UpdateToolbar(TabContents* contents, bool should_restore_state); virtual void FocusToolbar(); + virtual void FocusPageAndAppMenus(); virtual void DestroyBrowser(); virtual bool IsBookmarkBarVisible() const; virtual bool IsBookmarkBarAnimating() const; @@ -472,6 +473,9 @@ class BrowserView : public BrowserBubbleHost, // Initialize the hung plugin detector. void InitHangMonitor(); + // Save the current focused view to view storage + void SaveFocusedView(); + // Initialize class statics. static void InitClass(); diff --git a/chrome/browser/views/toolbar_view.cc b/chrome/browser/views/toolbar_view.cc index a640442..47ca0e0 100644 --- a/chrome/browser/views/toolbar_view.cc +++ b/chrome/browser/views/toolbar_view.cc @@ -34,6 +34,7 @@ #include "chrome/browser/views/bookmark_menu_button.h" #include "chrome/browser/views/browser_actions_container.h" #include "chrome/browser/views/event_utils.h" +#include "chrome/browser/views/frame/browser_view.h" #include "chrome/browser/views/go_button.h" #include "chrome/browser/views/location_bar_view.h" #include "chrome/browser/views/toolbar_star_toggle.h" @@ -52,6 +53,8 @@ #include "views/controls/label.h" #include "views/controls/menu/menu_2.h" #include "views/drag_utils.h" +#include "views/focus/view_storage.h" +#include "views/widget/tooltip_manager.h" #include "views/window/non_client_view.h" #include "views/window/window.h" @@ -90,7 +93,9 @@ ToolbarView::ToolbarView(Browser* browser) bookmark_menu_(NULL), profile_(NULL), browser_(browser), - profiles_menu_contents_(NULL) { + profiles_menu_contents_(NULL), + last_focused_view_storage_id_(-1), + ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { SetID(VIEW_ID_TOOLBAR); browser_->command_updater()->AddCommandObserver(IDC_BACK, this); browser_->command_updater()->AddCommandObserver(IDC_FORWARD, this); @@ -108,6 +113,14 @@ ToolbarView::ToolbarView(Browser* browser) } } +ToolbarView::~ToolbarView() { + if (page_menu_->HasFocus() || app_menu_->HasFocus()) { + views::FocusManager* focus_manager = GetFocusManager(); + focus_manager->UnregisterAccelerators(this); + focus_manager->RemoveFocusChangeListener(this); + } +} + void ToolbarView::Init(Profile* profile) { back_menu_model_.reset(new BackForwardMenuModel( browser_, BackForwardMenuModel::BACKWARD_MENU)); @@ -148,6 +161,82 @@ void ToolbarView::SetAppMenuModel(AppMenuModel* model) { app_menu_menu_.reset(new views::Menu2(app_menu_model_.get())); } +void ToolbarView::EnterMenuBarEmulationMode(int last_focused_view_storage_id, + views::MenuButton* menu_to_focus) { + last_focused_view_storage_id_ = last_focused_view_storage_id; + if (!menu_to_focus) + menu_to_focus = page_menu_; + + // If we're already in the menu bar emulation mode, just set the focus. + if (page_menu_->IsFocusable() && app_menu_->IsFocusable()) { + menu_to_focus->RequestFocus(); + return; + } + + // Make the menus focusable and set focus to the initial menu. + page_menu_->SetFocusable(true); + app_menu_->SetFocusable(true); + menu_to_focus->RequestFocus(); + + // Listen so we know when focus has moved to something other than one + // of these menus. + views::FocusManager* focus_manager = GetFocusManager(); + focus_manager->AddFocusChangeListener(this); + + // Add accelerators so that the usual keys used to interact with a + // menu bar work as expected. + views::Accelerator return_key(base::VKEY_RETURN, false, false, false); + focus_manager->RegisterAccelerator(return_key, this); + views::Accelerator space(base::VKEY_SPACE, false, false, false); + focus_manager->RegisterAccelerator(space, this); + views::Accelerator escape(base::VKEY_ESCAPE, false, false, false); + focus_manager->RegisterAccelerator(escape, this); + views::Accelerator down(base::VKEY_DOWN, false, false, false); + focus_manager->RegisterAccelerator(down, this); + views::Accelerator up(base::VKEY_UP, false, false, false); + focus_manager->RegisterAccelerator(up, this); + views::Accelerator left(base::VKEY_LEFT, false, false, false); + focus_manager->RegisterAccelerator(left, this); + views::Accelerator right(base::VKEY_RIGHT, false, false, false); + focus_manager->RegisterAccelerator(right, this); +} + +void ToolbarView::AddMenuListener(views::MenuListener* listener) { + menu_listeners_.push_back(listener); +} + +void ToolbarView::RemoveMenuListener(views::MenuListener* listener) { + for (std::vector<views::MenuListener*>::iterator iter = + menu_listeners_.begin(); + iter != menu_listeners_.end(); + ++iter) { + if (*iter == listener) { + menu_listeners_.erase(iter); + return; + } + } +} + +//////////////////////////////////////////////////////////////////////////////// +// ToolbarView, FocusChangeListener overrides: + +void ToolbarView::FocusWillChange(views::View* focused_before, + views::View* focused_now) { + // If the focus is switching to something outside the menu bar, + // take it out of the focus traversal. + if (focused_now != NULL && + focused_now != page_menu_ && + focused_now != app_menu_) { + // Post ExitMenuBarEmulationMode to the queue rather than running it + // right away, because otherwise we'll remove ourselves from the + // list of listeners while FocusManager is in the middle of iterating + // over that list. + MessageLoop::current()->PostTask( + FROM_HERE, method_factory_.NewRunnableMethod( + &ToolbarView::ExitMenuBarEmulationMode)); + } +} + //////////////////////////////////////////////////////////////////////////////// // ToolbarView, AccessibleToolbarView overrides: @@ -324,6 +413,43 @@ void ToolbarView::ExecuteCommand(int command_id) { //////////////////////////////////////////////////////////////////////////////// // ToolbarView, views::View overrides: +bool ToolbarView::AcceleratorPressed( + const views::Accelerator& accelerator) { + // The only accelerators we handle here are if the menus are focused. + views::View* focused_view = GetFocusManager()->GetFocusedView(); + if (focused_view != page_menu_ && focused_view != app_menu_) { + ExitMenuBarEmulationMode(); + return false; + } + + // Safe to cast, given the check above. + views::MenuButton* menu = static_cast<views::MenuButton*>(focused_view); + switch (accelerator.GetKeyCode()) { + case base::VKEY_ESCAPE: + RestoreLastFocusedView(); + return true; + case base::VKEY_LEFT: + case base::VKEY_RIGHT: + if (menu == app_menu_) + page_menu_->RequestFocus(); + else + app_menu_->RequestFocus(); + return true; + case base::VKEY_UP: + case base::VKEY_DOWN: + case base::VKEY_RETURN: + case base::VKEY_SPACE: + // Hide the tooltip before activating a menu button. + if (GetWidget()->GetTooltipManager()) + GetWidget()->GetTooltipManager()->HideKeyboardTooltip(); + + ActivateMenuButton(menu); + return true; + default: + return false; + } +} + gfx::Size ToolbarView::GetPreferredSize() { if (IsDisplayModeNormal()) { int min_width = kControlIndent + back_->GetPreferredSize().width() + @@ -708,11 +834,99 @@ void ToolbarView::LoadRightSideControlsImages() { void ToolbarView::RunPageMenu(const gfx::Point& pt) { page_menu_model_.reset(new PageMenuModel(this, browser_)); page_menu_menu_.reset(new views::Menu2(page_menu_model_.get())); + for (unsigned int i = 0; i < menu_listeners_.size(); i++) { + page_menu_menu_->AddMenuListener(menu_listeners_[i]); + } page_menu_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); + for (unsigned int i = 0; i < menu_listeners_.size(); i++) { + page_menu_menu_->RemoveMenuListener(menu_listeners_[i]); + } + SwitchToOtherMenuIfNeeded(page_menu_menu_.get(), app_menu_); } void ToolbarView::RunAppMenu(const gfx::Point& pt) { if (app_menu_model_->BuildProfileSubMenu()) app_menu_menu_->Rebuild(); + for (unsigned int i = 0; i < menu_listeners_.size(); i++) { + app_menu_menu_->AddMenuListener(menu_listeners_[i]); + } app_menu_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); + for (unsigned int i = 0; i < menu_listeners_.size(); i++) { + app_menu_menu_->RemoveMenuListener(menu_listeners_[i]); + } + SwitchToOtherMenuIfNeeded(app_menu_menu_.get(), page_menu_); +} + +void ToolbarView::SwitchToOtherMenuIfNeeded( + views::Menu2* previous_menu, views::MenuButton* next_menu_button) { + // If the user tried to move to the right or left, switch from the + // app menu to the page menu. Switching to the next menu is delayed + // until the next event loop so that the call stack that initiated + // activating the first menu can return. (If we didn't do this, the + // call stack would grow each time the user switches menus, and + // the actions taken after the user finally exits a menu would cause + // flicker.) + views::MenuWrapper::MenuAction action = previous_menu->GetMenuAction(); + if (action == views::MenuWrapper::MENU_ACTION_NEXT || + action == views::MenuWrapper::MENU_ACTION_PREVIOUS) { + MessageLoop::current()->PostTask( + FROM_HERE, method_factory_.NewRunnableMethod( + &ToolbarView::ActivateMenuButton, + next_menu_button)); + } +} + +void ToolbarView::ActivateMenuButton(views::MenuButton* menu_button) { +#if defined(OS_LINUX) + // Under GTK, opening a pop-up menu causes the main window to lose focus. + // Focus is automatically returned when the menu closes. + // + // Make sure that the menu button being activated has focus, so that + // when the user escapes from the menu without selecting anything, focus + // will be returned here. + if (!menu_button->HasFocus()) { + menu_button->RequestFocus(); + GetFocusManager()->StoreFocusedView(); + } +#endif + +#if defined(OS_WIN) + // On Windows, we have to explicitly clear the focus before opening + // the pop-up menu, then set the focus again when it closes. + GetFocusManager()->ClearFocus(); +#endif + + // Tell the menu button to activate, opening its pop-up menu. + menu_button->Activate(); + +#if defined(OS_WIN) + EnterMenuBarEmulationMode(last_focused_view_storage_id_, menu_button); +#endif +} + +void ToolbarView::ExitMenuBarEmulationMode() { + if (page_menu_->HasFocus() || app_menu_->HasFocus()) + RestoreLastFocusedView(); + + views::FocusManager* focus_manager = GetFocusManager(); + focus_manager->UnregisterAccelerators(this); + focus_manager->RemoveFocusChangeListener(this); + page_menu_->SetFocusable(false); + app_menu_->SetFocusable(false); +} + +void ToolbarView::RestoreLastFocusedView() { + views::ViewStorage* view_storage = views::ViewStorage::GetSharedInstance(); + views::View* last_focused_view = + view_storage->RetrieveView(last_focused_view_storage_id_); + if (last_focused_view) { + last_focused_view->RequestFocus(); + } else { + // Focus the location bar + views::View* view = GetAncestorWithClassName(BrowserView::kViewClassName); + if (view) { + BrowserView* browser_view = static_cast<BrowserView*>(view); + browser_view->SetFocusToLocationBar(); + } + } } diff --git a/chrome/browser/views/toolbar_view.h b/chrome/browser/views/toolbar_view.h index 23f5232..675eb33 100644 --- a/chrome/browser/views/toolbar_view.h +++ b/chrome/browser/views/toolbar_view.h @@ -20,6 +20,7 @@ #include "chrome/browser/views/location_bar_view.h" #include "views/controls/button/menu_button.h" #include "views/controls/menu/menu.h" +#include "views/controls/menu/menu_wrapper.h" #include "views/controls/menu/view_menu_delegate.h" #include "views/view.h" @@ -36,6 +37,7 @@ class Menu2; class ToolbarView : public AccessibleToolbarView, public views::ViewMenuDelegate, public views::DragController, + public views::FocusChangeListener, public menus::SimpleMenuModel::Delegate, public LocationBarView::Delegate, public NotificationObserver, @@ -44,7 +46,7 @@ class ToolbarView : public AccessibleToolbarView, public BubblePositioner { public: explicit ToolbarView(Browser* browser); - virtual ~ToolbarView() { } + virtual ~ToolbarView(); // Create the contents of the Browser Toolbar void Init(Profile* profile); @@ -62,6 +64,25 @@ class ToolbarView : public AccessibleToolbarView, // Sets the app menu model. void SetAppMenuModel(AppMenuModel* model); + // Focuses the page menu and enters a special mode where the page + // and app menus are focusable and allow for keyboard navigation just + // like a normal menu bar. As soon as focus leaves one of the menus, + // the special mode is exited. + // + // Pass it the storage id of the view where focus should be returned + // if the user escapes, and the menu button to focus initially. If + // |menu_to_focus| is NULL, it will focus the page menu by default. + // + // Not used on the Mac, which has a "normal" menu bar. + void EnterMenuBarEmulationMode(int last_focused_view_storage_id, + views::MenuButton* menu_to_focus); + + // Add a listener to receive a callback when the menu opens. + void AddMenuListener(views::MenuListener* listener); + + // Remove a menu listener. + void RemoveMenuListener(views::MenuListener* listener); + // Accessors... Browser* browser() const { return browser_; } BrowserActionsContainer* browser_actions() const { return browser_actions_; } @@ -71,6 +92,10 @@ class ToolbarView : public AccessibleToolbarView, views::MenuButton* page_menu() const { return page_menu_; } views::MenuButton* app_menu() const { return app_menu_; } + // Overridden from views::FocusChangeListener: + virtual void FocusWillChange(views::View* focused_before, + views::View* focused_now); + // Overridden from AccessibleToolbarView: virtual bool IsAccessibleViewTraversable(views::View* view); @@ -106,6 +131,7 @@ class ToolbarView : public AccessibleToolbarView, virtual void ExecuteCommand(int command_id); // Overridden from views::View: + virtual bool AcceleratorPressed(const views::Accelerator& accelerator); virtual gfx::Size GetPreferredSize(); virtual void Layout(); virtual void Paint(gfx::Canvas* canvas); @@ -138,6 +164,13 @@ class ToolbarView : public AccessibleToolbarView, void RunPageMenu(const gfx::Point& pt); void RunAppMenu(const gfx::Point& pt); + // Check if the menu exited with a code indicating the user wants to + // switch to the other menu, and then switch to that other menu. + void SwitchToOtherMenuIfNeeded(views::Menu2* previous_menu, + views::MenuButton* next_menu_button); + + void ActivateMenuButton(views::MenuButton* menu_button); + // Types of display mode this toolbar can have. enum DisplayMode { DISPLAYMODE_NORMAL, // Normal toolbar with buttons, etc. @@ -148,6 +181,14 @@ class ToolbarView : public AccessibleToolbarView, return display_mode_ == DISPLAYMODE_NORMAL; } + // Take the menus out of the focus traversal, unregister accelerators, + // and stop listening to focus change events. + void ExitMenuBarEmulationMode(); + + // Restore the view that was focused before EnterMenuBarEmulationMode + // was called. + void RestoreLastFocusedView(); + scoped_ptr<BackForwardMenuModel> back_menu_model_; scoped_ptr<BackForwardMenuModel> forward_menu_model_; @@ -186,6 +227,16 @@ class ToolbarView : public AccessibleToolbarView, // TODO(beng): build these into MenuButton. scoped_ptr<views::Menu2> page_menu_menu_; scoped_ptr<views::Menu2> app_menu_menu_; + + // Storage id for the last view that was focused before focus + // was given to one of the toolbar views. + int last_focused_view_storage_id_; + + // Vector of listeners to receive callbacks when the menu opens. + std::vector<views::MenuListener*> menu_listeners_; + + // Used to post tasks to switch to the next/previous menu. + ScopedRunnableMethodFactory<ToolbarView> method_factory_; }; #endif // CHROME_BROWSER_VIEWS_TOOLBAR_VIEW_H_ diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 582ce0a..e978edf 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -317,6 +317,7 @@ 'test/ui/dromaeo_benchmark_uitest.cc', 'test/ui/fast_shutdown_uitest.cc', 'test/ui/history_uitest.cc', + 'test/ui/keyboard_access_uitest.cc', 'test/ui/layout_plugin_uitest.cc', 'test/ui/mouseleave_uitest.cc', 'test/ui/npapi_uitest.cc', diff --git a/chrome/test/automation/automation_messages_internal.h b/chrome/test/automation/automation_messages_internal.h index 7447020..0e82155 100644 --- a/chrome/test/automation/automation_messages_internal.h +++ b/chrome/test/automation/automation_messages_internal.h @@ -1288,4 +1288,23 @@ IPC_BEGIN_MESSAGES(Automation) int64 /* id */, bool /* success */) + // Block until the focused view id changes to something other than + // |previous_view_id|. + IPC_SYNC_MESSAGE_ROUTED2_2(AutomationMsg_WaitForFocusedViewIDToChange, + int /* window handle */, + int /* previous_view_id */, + bool /* success */, + int /* new_view_id */) + + // To avoid race conditions, waiting until a popup menu opens is a + // three-step process: + // 1. Call StartTrackingPopupMenus. + // 2. Call an automation method that results in opening the popup menu. + // 3. Call WaitForPopupMenuToOpen and check for success. + IPC_SYNC_MESSAGE_ROUTED1_1(AutomationMsg_StartTrackingPopupMenus, + int /* browser handle */, + bool /* success */) + IPC_SYNC_MESSAGE_ROUTED0_1(AutomationMsg_WaitForPopupMenuToOpen, + bool /* success */) + IPC_END_MESSAGES(Automation) diff --git a/chrome/test/automation/browser_proxy.cc b/chrome/test/automation/browser_proxy.cc index 6db138e..f0825a0 100644 --- a/chrome/test/automation/browser_proxy.cc +++ b/chrome/test/automation/browser_proxy.cc @@ -91,7 +91,7 @@ bool BrowserProxy::GetActiveTabIndex(int* active_tab_index) const { *active_tab_index = active_tab_index_response; return true; } - + return false; } @@ -561,3 +561,24 @@ bool BrowserProxy::ShutdownSessionService() { return did_shutdown; } +bool BrowserProxy::StartTrackingPopupMenus() { + if (!is_valid()) + return false; + + bool result = false; + if (!sender_->Send(new AutomationMsg_StartTrackingPopupMenus + (0, handle_, &result))) + return false; + return result; +} + +bool BrowserProxy::WaitForPopupMenuToOpen() { + if (!is_valid()) + return false; + + bool result = false; + if (!sender_->Send(new AutomationMsg_WaitForPopupMenuToOpen + (0, &result))) + return false; + return result; +} diff --git a/chrome/test/automation/browser_proxy.h b/chrome/test/automation/browser_proxy.h index a21bfe6..509a630 100644 --- a/chrome/test/automation/browser_proxy.h +++ b/chrome/test/automation/browser_proxy.h @@ -217,6 +217,15 @@ class BrowserProxy : public AutomationResourceProxy { // on success. bool ShutdownSessionService() WARN_UNUSED_RESULT; + // To avoid race conditions, waiting until a popup menu opens is a + // three-step process: + // 1. Call StartTrackingPopupMenus. + // 2. Call an automation method that results in opening the popup menu. + // 3. Call WaitForPopupMenuToOpen and check for success. + // Both methods return true on success. + bool StartTrackingPopupMenus() WARN_UNUSED_RESULT; + bool WaitForPopupMenuToOpen() WARN_UNUSED_RESULT; + protected: virtual ~BrowserProxy() {} private: diff --git a/chrome/test/automation/window_proxy.cc b/chrome/test/automation/window_proxy.cc index 295f05b..c098abd 100644 --- a/chrome/test/automation/window_proxy.cc +++ b/chrome/test/automation/window_proxy.cc @@ -123,6 +123,15 @@ bool WindowProxy::GetFocusedViewID(int* view_id) { view_id)); } +bool WindowProxy::WaitForFocusedViewIDToChange( + int old_view_id, int* new_view_id) { + bool result = false; + if (!sender_->Send(new AutomationMsg_WaitForFocusedViewIDToChange + (0, handle_, old_view_id, &result, new_view_id))) + return false; + return result; +} + scoped_refptr<BrowserProxy> WindowProxy::GetBrowser() { return GetBrowserWithTimeout(base::kNoTimeout, NULL); } diff --git a/chrome/test/automation/window_proxy.h b/chrome/test/automation/window_proxy.h index 3c5deca..3f6fa8a 100644 --- a/chrome/test/automation/window_proxy.h +++ b/chrome/test/automation/window_proxy.h @@ -90,6 +90,10 @@ class WindowProxy : public AutomationResourceProxy { // was retrieved. bool GetFocusedViewID(int* view_id); + // Waits until the focused view ID changes to something other than + // |old_view_id|. Returns true if the focused view ID did change. + bool WaitForFocusedViewIDToChange(int old_view_id, int* new_view_id); + // Returns the browser this window corresponds to, or NULL if this window // is not a browser. The caller owns the returned BrowserProxy. scoped_refptr<BrowserProxy> GetBrowser(); diff --git a/chrome/test/test_browser_window.h b/chrome/test/test_browser_window.h index 169afdb..1390dd8 100644 --- a/chrome/test/test_browser_window.h +++ b/chrome/test/test_browser_window.h @@ -48,6 +48,7 @@ class TestBrowserWindow : public BrowserWindow { virtual void UpdateToolbar(TabContents* contents, bool should_restore_state) {} virtual void FocusToolbar() {} + virtual void FocusPageAndAppMenus() {} virtual void ShowPageMenu() {} virtual void ShowAppMenu() {} virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event, diff --git a/chrome/test/ui/keyboard_access_uitest.cc b/chrome/test/ui/keyboard_access_uitest.cc new file mode 100644 index 0000000..f5c339e --- /dev/null +++ b/chrome/test/ui/keyboard_access_uitest.cc @@ -0,0 +1,104 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/keyboard_codes.h" +#include "chrome/test/automation/browser_proxy.h" +#include "chrome/test/automation/tab_proxy.h" +#include "chrome/test/automation/window_proxy.h" +#include "chrome/test/ui/ui_test.h" +#include "googleurl/src/gurl.h" + +// This functionality currently works on Windows and on Linux when +// toolkit_views is defined (i.e. for Chrome OS). It's not needed +// on the Mac, and it's not yet implemented on Linux. +#if defined(TOOLKIT_VIEWS) + +namespace { + +class KeyboardAccessTest : public UITest { + public: + KeyboardAccessTest() { + dom_automation_enabled_ = true; + show_window_ = true; + } + + // Use the keyboard to select "New Tab" from the app menu. + // This test depends on the fact that there are two menus and that + // New Tab is the first item in the app menu. If the menus change, + // this test will need to be changed to reflect that. + // + // If alternate_key_sequence is true, use "Alt" instead of "F10" to + // open the menu bar, and "Down" instead of "Enter" to open a menu. + void TestMenuKeyboardAccess(bool alternate_key_sequence); + + DISALLOW_COPY_AND_ASSIGN(KeyboardAccessTest); +}; + +void KeyboardAccessTest::TestMenuKeyboardAccess(bool alternate_key_sequence) { + scoped_refptr<BrowserProxy> browser = automation()->GetBrowserWindow(0); + ASSERT_TRUE(browser.get()); + scoped_refptr<WindowProxy> window = browser->GetWindow(); + ASSERT_TRUE(window.get()); + + // Navigate to a page in the first tab, which makes sure that focus is + // set to the browser window. + scoped_refptr<TabProxy> tab(GetActiveTab()); + ASSERT_TRUE(tab.get()); + + ASSERT_TRUE(tab->NavigateToURL(GURL("about:"))); + + // The initial tab index should be 0. + int tab_index = -1; + ASSERT_TRUE(browser->GetActiveTabIndex(&tab_index)); + ASSERT_EQ(0, tab_index); + + // Get the focused view ID, then press a key to activate the + // page menu, then wait until the focused view changes. + int original_view_id = -1; + ASSERT_TRUE(window->GetFocusedViewID(&original_view_id)); + + if (alternate_key_sequence) + ASSERT_TRUE(window->SimulateOSKeyPress(base::VKEY_MENU, 0)); + else + ASSERT_TRUE(window->SimulateOSKeyPress(base::VKEY_F10, 0)); + + int new_view_id = -1; + ASSERT_TRUE(window->WaitForFocusedViewIDToChange( + original_view_id, &new_view_id)); + + ASSERT_TRUE(browser->StartTrackingPopupMenus()); + + // Press RIGHT to focus the app menu, then RETURN or DOWN to open it. + ASSERT_TRUE(window->SimulateOSKeyPress(base::VKEY_RIGHT, 0)); + if (alternate_key_sequence) + ASSERT_TRUE(window->SimulateOSKeyPress(base::VKEY_DOWN, 0)); + else + ASSERT_TRUE(window->SimulateOSKeyPress(base::VKEY_RETURN, 0)); + + // Wait until the popup menu actually opens. + ASSERT_TRUE(browser->WaitForPopupMenuToOpen()); + + // Press DOWN to select the first item, then RETURN to select it. + ASSERT_TRUE(window->SimulateOSKeyPress(base::VKEY_DOWN, 0)); + ASSERT_TRUE(window->SimulateOSKeyPress(base::VKEY_RETURN, 0)); + + // Wait for the new tab to appear. + ASSERT_TRUE(browser->WaitForTabCountToBecome(2, sleep_timeout_ms())); + + // Make sure that the new tab index is 1. + ASSERT_TRUE(browser->GetActiveTabIndex(&tab_index)); + ASSERT_EQ(1, tab_index); +} + +TEST_F(KeyboardAccessTest, TestMenuKeyboardAccess) { + TestMenuKeyboardAccess(false); +} + +TEST_F(KeyboardAccessTest, TestAltMenuKeyboardAccess) { + TestMenuKeyboardAccess(true); +} + +} // namespace + +#endif // defined(TOOLKIT_VIEWS) |