Home | History | Annotate | Download | only in ui
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/ui/browser_command_controller.h"
      6 
      7 #include "chrome/app/chrome_command_ids.h"
      8 #include "chrome/browser/browser_process.h"
      9 #include "chrome/browser/command_updater.h"
     10 #include "chrome/browser/profiles/profile_destroyer.h"
     11 #include "chrome/browser/profiles/profile_manager.h"
     12 #include "chrome/browser/profiles/profiles_state.h"
     13 #include "chrome/browser/ui/browser.h"
     14 #include "chrome/browser/ui/browser_commands.h"
     15 #include "chrome/browser/ui/browser_window_state.h"
     16 #include "chrome/common/pref_names.h"
     17 #include "chrome/test/base/browser_with_test_window_test.h"
     18 #include "chrome/test/base/test_browser_window.h"
     19 #include "chrome/test/base/testing_browser_process.h"
     20 #include "chrome/test/base/testing_profile_manager.h"
     21 #include "content/public/browser/native_web_keyboard_event.h"
     22 #include "ui/base/keycodes/keyboard_codes.h"
     23 
     24 typedef BrowserWithTestWindowTest BrowserCommandControllerTest;
     25 
     26 TEST_F(BrowserCommandControllerTest, IsReservedCommandOrKey) {
     27 #if defined(OS_CHROMEOS)
     28   // F1-3 keys are reserved Chrome accelerators on Chrome OS.
     29   EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
     30       IDC_BACK, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
     31                                                 ui::VKEY_F1, 0, 0)));
     32   EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
     33       IDC_FORWARD, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
     34                                                    ui::VKEY_F2, 0, 0)));
     35   EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
     36       IDC_RELOAD, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
     37                                                   ui::VKEY_F3, 0, 0)));
     38 
     39   // When there are modifier keys pressed, don't reserve.
     40   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     41       IDC_RELOAD_IGNORING_CACHE, content::NativeWebKeyboardEvent(
     42           ui::ET_KEY_PRESSED, false, ui::VKEY_F3, ui::EF_SHIFT_DOWN, 0)));
     43   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     44       IDC_RELOAD_IGNORING_CACHE, content::NativeWebKeyboardEvent(
     45           ui::ET_KEY_PRESSED, false, ui::VKEY_F3, ui::EF_CONTROL_DOWN, 0)));
     46   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     47       IDC_FULLSCREEN, content::NativeWebKeyboardEvent(
     48           ui::ET_KEY_PRESSED, false, ui::VKEY_F4, ui::EF_SHIFT_DOWN, 0)));
     49 
     50   // F4-10 keys are not reserved since they are Ash accelerators.
     51   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     52       -1, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
     53                                           ui::VKEY_F4, 0, 0)));
     54   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     55       -1, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
     56                                           ui::VKEY_F5, 0, 0)));
     57   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     58       -1, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
     59                                           ui::VKEY_F6, 0, 0)));
     60   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     61       -1, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
     62                                           ui::VKEY_F7, 0, 0)));
     63   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     64       -1, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
     65                                           ui::VKEY_F8, 0, 0)));
     66   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     67       -1, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
     68                                           ui::VKEY_F9, 0, 0)));
     69   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     70       -1, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
     71                                           ui::VKEY_F10, 0, 0)));
     72 
     73   // Shift+Control+Alt+F3 is also an Ash accelerator. Don't reserve it.
     74   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     75       -1, content::NativeWebKeyboardEvent(
     76           ui::ET_KEY_PRESSED, false,
     77           ui::VKEY_F3,
     78           ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN, 0)));
     79 #endif  // OS_CHROMEOS
     80 
     81 #if defined(USE_AURA)
     82   // Ctrl+n, Ctrl+w are reserved while Ctrl+f is not.
     83 
     84   // The content::NativeWebKeyboardEvent constructor is available only when
     85   // USE_AURA is #defined.
     86   EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
     87       IDC_NEW_WINDOW, content::NativeWebKeyboardEvent(
     88           ui::ET_KEY_PRESSED, false, ui::VKEY_N, ui::EF_CONTROL_DOWN, 0)));
     89   EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
     90       IDC_CLOSE_TAB, content::NativeWebKeyboardEvent(
     91           ui::ET_KEY_PRESSED, false, ui::VKEY_W, ui::EF_CONTROL_DOWN, 0)));
     92   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
     93       IDC_FIND, content::NativeWebKeyboardEvent(
     94           ui::ET_KEY_PRESSED, false, ui::VKEY_F, ui::EF_CONTROL_DOWN, 0)));
     95 #endif  // USE_AURA
     96 }
     97 
     98 TEST_F(BrowserCommandControllerTest, IsReservedCommandOrKeyIsApp) {
     99   browser()->app_name_ = "app";
    100   ASSERT_TRUE(browser()->is_app());
    101 
    102   // When is_app(), no keys are reserved.
    103 #if defined(OS_CHROMEOS)
    104   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
    105       IDC_BACK, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
    106                                                 ui::VKEY_F1, 0, 0)));
    107   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
    108       IDC_FORWARD, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
    109                                                    ui::VKEY_F2, 0, 0)));
    110   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
    111       IDC_RELOAD, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
    112                                                   ui::VKEY_F3, 0, 0)));
    113   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
    114       -1, content::NativeWebKeyboardEvent(ui::ET_KEY_PRESSED, false,
    115                                           ui::VKEY_F4, 0, 0)));
    116 #endif  // OS_CHROMEOS
    117 
    118 #if defined(USE_AURA)
    119   // The content::NativeWebKeyboardEvent constructor is available only when
    120   // USE_AURA is #defined.
    121   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
    122       IDC_NEW_WINDOW, content::NativeWebKeyboardEvent(
    123           ui::ET_KEY_PRESSED, false, ui::VKEY_N, ui::EF_CONTROL_DOWN, 0)));
    124   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
    125       IDC_CLOSE_TAB, content::NativeWebKeyboardEvent(
    126           ui::ET_KEY_PRESSED, false, ui::VKEY_W, ui::EF_CONTROL_DOWN, 0)));
    127   EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
    128       IDC_FIND, content::NativeWebKeyboardEvent(
    129           ui::ET_KEY_PRESSED, false, ui::VKEY_F, ui::EF_CONTROL_DOWN, 0)));
    130 #endif  // USE_AURA
    131 }
    132 
    133 TEST_F(BrowserCommandControllerTest, AppFullScreen) {
    134   // Enable for tabbed browser.
    135   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FULLSCREEN));
    136 
    137   // Enabled for app windows.
    138   browser()->app_name_ = "app";
    139   ASSERT_TRUE(browser()->is_app());
    140   browser()->command_controller()->FullscreenStateChanged();
    141   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FULLSCREEN));
    142 }
    143 
    144 TEST_F(BrowserCommandControllerTest, AvatarMenuDisabledWhenOnlyOneProfile) {
    145   if (!profiles::IsMultipleProfilesEnabled())
    146     return;
    147 
    148   TestingProfileManager testing_profile_manager(
    149       TestingBrowserProcess::GetGlobal());
    150   ASSERT_TRUE(testing_profile_manager.SetUp());
    151   ProfileManager* profile_manager = testing_profile_manager.profile_manager();
    152 
    153   chrome::BrowserCommandController command_controller(browser(),
    154                                                       profile_manager);
    155   const CommandUpdater* command_updater = command_controller.command_updater();
    156 
    157   testing_profile_manager.CreateTestingProfile("p1");
    158   ASSERT_EQ(1U, profile_manager->GetNumberOfProfiles());
    159   EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_SHOW_AVATAR_MENU));
    160 
    161   testing_profile_manager.CreateTestingProfile("p2");
    162   ASSERT_EQ(2U, profile_manager->GetNumberOfProfiles());
    163   EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SHOW_AVATAR_MENU));
    164 
    165   testing_profile_manager.DeleteTestingProfile("p1");
    166   ASSERT_EQ(1U, profile_manager->GetNumberOfProfiles());
    167   EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_SHOW_AVATAR_MENU));
    168 
    169   testing_profile_manager.DeleteTestingProfile("p2");
    170 }
    171 
    172 //////////////////////////////////////////////////////////////////////////////
    173 
    174 // A test browser window that can toggle fullscreen state.
    175 class FullscreenTestBrowserWindow : public TestBrowserWindow {
    176  public:
    177   FullscreenTestBrowserWindow() : fullscreen_(false) {}
    178   virtual ~FullscreenTestBrowserWindow() {}
    179 
    180   // TestBrowserWindow overrides:
    181   virtual bool ShouldHideUIForFullscreen() const OVERRIDE {
    182     return fullscreen_;
    183   }
    184   virtual bool IsFullscreen() const OVERRIDE {
    185     return fullscreen_;
    186   }
    187   virtual void EnterFullscreen(
    188       const GURL& url, FullscreenExitBubbleType type) OVERRIDE {
    189     fullscreen_ = true;
    190   }
    191   virtual void ExitFullscreen() OVERRIDE {
    192     fullscreen_ = false;
    193   }
    194 
    195  private:
    196   bool fullscreen_;
    197 
    198   DISALLOW_COPY_AND_ASSIGN(FullscreenTestBrowserWindow);
    199 };
    200 
    201 // Test that uses FullscreenTestBrowserWindow for its window.
    202 class BrowserCommandControllerFullscreenTest
    203     : public BrowserWithTestWindowTest {
    204  public:
    205   BrowserCommandControllerFullscreenTest() {}
    206   virtual ~BrowserCommandControllerFullscreenTest() {}
    207 
    208   // BrowserWithTestWindowTest overrides:
    209   virtual BrowserWindow* CreateBrowserWindow() OVERRIDE {
    210     return new FullscreenTestBrowserWindow;
    211   }
    212 
    213  private:
    214   DISALLOW_COPY_AND_ASSIGN(BrowserCommandControllerFullscreenTest);
    215 };
    216 
    217 TEST_F(BrowserCommandControllerFullscreenTest,
    218        UpdateCommandsForFullscreenMode) {
    219   // Defaults for a tabbed browser.
    220   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPEN_CURRENT_URL));
    221   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_SHOW_AS_TAB));
    222   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_TOOLBAR));
    223   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_LOCATION));
    224   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_SEARCH));
    225   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_MENU_BAR));
    226   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_NEXT_PANE));
    227   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_PREVIOUS_PANE));
    228   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_BOOKMARKS));
    229   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_DEVELOPER_MENU));
    230   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FEEDBACK));
    231   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
    232   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_IMPORT_SETTINGS));
    233   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_EDIT_SEARCH_ENGINES));
    234   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_VIEW_PASSWORDS));
    235   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ABOUT));
    236   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_SHOW_APP_MENU));
    237   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FULLSCREEN));
    238 
    239   // Simulate going fullscreen.
    240   chrome::ToggleFullscreenMode(browser());
    241   ASSERT_TRUE(browser()->window()->IsFullscreen());
    242   browser()->command_controller()->FullscreenStateChanged();
    243 
    244   // Most commands are disabled in fullscreen.
    245   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_OPEN_CURRENT_URL));
    246   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_SHOW_AS_TAB));
    247   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_TOOLBAR));
    248   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_LOCATION));
    249   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_SEARCH));
    250   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_MENU_BAR));
    251   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_NEXT_PANE));
    252   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_PREVIOUS_PANE));
    253   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_BOOKMARKS));
    254   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_DEVELOPER_MENU));
    255   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_FEEDBACK));
    256   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
    257   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_IMPORT_SETTINGS));
    258   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_EDIT_SEARCH_ENGINES));
    259   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_VIEW_PASSWORDS));
    260   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_ABOUT));
    261   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_SHOW_APP_MENU));
    262   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FULLSCREEN));
    263 
    264   // Exit fullscreen.
    265   chrome::ToggleFullscreenMode(browser());
    266   ASSERT_FALSE(browser()->window()->IsFullscreen());
    267   browser()->command_controller()->FullscreenStateChanged();
    268   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPEN_CURRENT_URL));
    269   EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_SHOW_AS_TAB));
    270   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_TOOLBAR));
    271   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_LOCATION));
    272   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_SEARCH));
    273   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_MENU_BAR));
    274   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_NEXT_PANE));
    275   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_PREVIOUS_PANE));
    276   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_BOOKMARKS));
    277   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_DEVELOPER_MENU));
    278   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FEEDBACK));
    279   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
    280   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_IMPORT_SETTINGS));
    281   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_EDIT_SEARCH_ENGINES));
    282   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_VIEW_PASSWORDS));
    283   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ABOUT));
    284   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_SHOW_APP_MENU));
    285   EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FULLSCREEN));
    286 }
    287 
    288 TEST_F(BrowserCommandControllerTest,
    289     IncognitoModeOnSigninAllowedPrefChange) {
    290   TestingProfileManager testing_profile_manager(
    291       TestingBrowserProcess::GetGlobal());
    292   ASSERT_TRUE(testing_profile_manager.SetUp());
    293 
    294   // Set up a profile with an off the record profile.
    295   TestingProfile::Builder builder;
    296   TestingProfile* profile2 = builder.Build().release();
    297   profile2->set_incognito(true);
    298   TestingProfile::Builder builder2;
    299   TestingProfile* profile1 = builder2.Build().release();
    300   profile2->SetOriginalProfile(profile1);
    301   EXPECT_EQ(profile2->GetOriginalProfile(), profile1);
    302   profile1->SetOffTheRecordProfile(profile2);
    303 
    304   // Create a new browser based on the off the record profile.
    305   Browser::CreateParams profile_params(profile2, chrome::GetActiveDesktop());
    306   scoped_ptr<Browser> browser2(
    307       chrome::CreateBrowserWithTestWindowForParams(&profile_params));
    308 
    309   ProfileManager* profile_manager = testing_profile_manager.profile_manager();
    310   chrome::BrowserCommandController* command_controller =
    311       new chrome::BrowserCommandController(browser2.get(), profile_manager);
    312   const CommandUpdater* command_updater = command_controller->command_updater();
    313 
    314   // Check that the SYNC_SETUP command is updated on preference change.
    315   EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SHOW_SYNC_SETUP));
    316   profile1->GetPrefs()->SetBoolean(prefs::kSigninAllowed, false);
    317   EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_SHOW_SYNC_SETUP));
    318   delete command_controller;
    319   browser2.reset();
    320   ProfileDestroyer::DestroyProfileWhenAppropriate(profile1);
    321 }
    322 
    323 TEST_F(BrowserCommandControllerTest,
    324     OnSigninAllowedPrefChange) {
    325   TestingProfileManager testing_profile_manager(
    326       TestingBrowserProcess::GetGlobal());
    327   ASSERT_TRUE(testing_profile_manager.SetUp());
    328   ProfileManager* profile_manager = testing_profile_manager.profile_manager();
    329   chrome::BrowserCommandController command_controller(browser(),
    330                                                       profile_manager);
    331   const CommandUpdater* command_updater = command_controller.command_updater();
    332 
    333   // Check that the SYNC_SETUP command is updated on preference change.
    334   EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SHOW_SYNC_SETUP));
    335   profile()->GetPrefs()->SetBoolean(prefs::kSigninAllowed, false);
    336   EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_SHOW_SYNC_SETUP));
    337 }
    338