blob: 1f074c4609e8f602dd4009064cab334669ed9fbf [file] [log] [blame]
[email protected]3642e2d2012-10-29 21:31:141// 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
[email protected]e9476cd2013-12-04 05:55:347#include "base/command_line.h"
Lei Zhang1f46798c2021-05-26 01:35:188#include "base/cxx17_backports.h"
Nico Weber0cc71122019-07-29 17:30:409#include "build/branding_buildflags.h"
avi655876a2015-12-25 07:18:1510#include "build/build_config.h"
Yuta Hijikata40b891d2020-11-27 09:05:0111#include "build/chromeos_buildflags.h"
[email protected]3642e2d2012-10-29 21:31:1412#include "chrome/app/chrome_command_ids.h"
[email protected]557c4e22012-12-18 18:30:5513#include "chrome/browser/browser_process.h"
[email protected]074311a2013-02-28 23:14:0914#include "chrome/browser/command_updater.h"
[email protected]57b25292014-05-01 16:31:0615#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]557c4e22012-12-18 18:30:5516#include "chrome/browser/profiles/profile_manager.h"
[email protected]de71ae992013-07-18 03:30:3817#include "chrome/browser/profiles/profiles_state.h"
[email protected]3642e2d2012-10-29 21:31:1418#include "chrome/browser/ui/browser.h"
[email protected]bee964522012-11-20 01:56:5319#include "chrome/browser/ui/browser_commands.h"
[email protected]3642e2d2012-10-29 21:31:1420#include "chrome/browser/ui/browser_window_state.h"
sriramsr39b6b7872015-03-04 00:03:3221#include "chrome/browser/ui/exclusive_access/exclusive_access_context.h"
22#include "chrome/browser/ui/tabs/tab_strip_model.h"
Rohit Agarwala14404f52021-06-14 11:50:4423#include "chrome/common/chrome_features.h"
[email protected]e9476cd2013-12-04 05:55:3424#include "chrome/common/chrome_switches.h"
[email protected]074311a2013-02-28 23:14:0925#include "chrome/common/pref_names.h"
[email protected]3642e2d2012-10-29 21:31:1426#include "chrome/test/base/browser_with_test_window_test.h"
[email protected]6a414ff2013-02-27 08:22:5427#include "chrome/test/base/test_browser_window.h"
[email protected]557c4e22012-12-18 18:30:5528#include "chrome/test/base/testing_browser_process.h"
29#include "chrome/test/base/testing_profile_manager.h"
Colin Blundell3517170e2019-07-11 08:16:3430#include "components/signin/public/base/signin_pref_names.h"
[email protected]3642e2d2012-10-29 21:31:1431#include "content/public/browser/native_web_keyboard_event.h"
dtapuska821d5772015-10-15 02:51:3032#include "ui/events/keycodes/dom/dom_code.h"
[email protected]7e9acd082013-09-17 23:31:1633#include "ui/events/keycodes/keyboard_codes.h"
[email protected]3642e2d2012-10-29 21:31:1434
[email protected]557c4e22012-12-18 18:30:5535typedef BrowserWithTestWindowTest BrowserCommandControllerTest;
36
37TEST_F(BrowserCommandControllerTest, IsReservedCommandOrKey) {
Yuta Hijikata40b891d2020-11-27 09:05:0138#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]3642e2d2012-10-29 21:31:1439 // F1-3 keys are reserved Chrome accelerators on Chrome OS.
40 EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
[email protected]397abd32013-08-21 05:44:1941 IDC_BACK, content::NativeWebKeyboardEvent(
dtapuska821d5772015-10-15 02:51:3042 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_BROWSER_BACK,
43 ui::DomCode::BROWSER_BACK, 0))));
[email protected]3642e2d2012-10-29 21:31:1444 EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:3045 IDC_FORWARD, content::NativeWebKeyboardEvent(ui::KeyEvent(
46 ui::ET_KEY_PRESSED, ui::VKEY_BROWSER_FORWARD,
47 ui::DomCode::BROWSER_FORWARD, 0))));
[email protected]3642e2d2012-10-29 21:31:1448 EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
[email protected]397abd32013-08-21 05:44:1949 IDC_RELOAD, content::NativeWebKeyboardEvent(
dtapuska821d5772015-10-15 02:51:3050 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_BROWSER_REFRESH,
51 ui::DomCode::BROWSER_REFRESH, 0))));
[email protected]3642e2d2012-10-29 21:31:1452
53 // When there are modifier keys pressed, don't reserve.
54 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
toyoshim7dad4b1182016-04-01 14:28:0555 IDC_RELOAD_BYPASSING_CACHE, content::NativeWebKeyboardEvent(ui::KeyEvent(
56 ui::ET_KEY_PRESSED, ui::VKEY_F3,
57 ui::DomCode::F3, ui::EF_SHIFT_DOWN))));
[email protected]3642e2d2012-10-29 21:31:1458 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
toyoshim7dad4b1182016-04-01 14:28:0559 IDC_RELOAD_BYPASSING_CACHE, content::NativeWebKeyboardEvent(ui::KeyEvent(
60 ui::ET_KEY_PRESSED, ui::VKEY_F3,
61 ui::DomCode::F3, ui::EF_CONTROL_DOWN))));
[email protected]3642e2d2012-10-29 21:31:1462 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
63 IDC_FULLSCREEN, content::NativeWebKeyboardEvent(
dtapuska821d5772015-10-15 02:51:3064 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_F4,
65 ui::DomCode::F4, ui::EF_SHIFT_DOWN))));
[email protected]3642e2d2012-10-29 21:31:1466
67 // F4-10 keys are not reserved since they are Ash accelerators.
68 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:3069 -1, content::NativeWebKeyboardEvent(ui::KeyEvent(
70 ui::ET_KEY_PRESSED, ui::VKEY_F4, ui::DomCode::F4, 0))));
[email protected]3642e2d2012-10-29 21:31:1471 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:3072 -1, content::NativeWebKeyboardEvent(ui::KeyEvent(
73 ui::ET_KEY_PRESSED, ui::VKEY_F5, ui::DomCode::F5, 0))));
[email protected]3642e2d2012-10-29 21:31:1474 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:3075 -1, content::NativeWebKeyboardEvent(ui::KeyEvent(
76 ui::ET_KEY_PRESSED, ui::VKEY_F6, ui::DomCode::F6, 0))));
[email protected]3642e2d2012-10-29 21:31:1477 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:3078 -1, content::NativeWebKeyboardEvent(ui::KeyEvent(
79 ui::ET_KEY_PRESSED, ui::VKEY_F7, ui::DomCode::F7, 0))));
[email protected]3642e2d2012-10-29 21:31:1480 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:3081 -1, content::NativeWebKeyboardEvent(ui::KeyEvent(
82 ui::ET_KEY_PRESSED, ui::VKEY_F8, ui::DomCode::F8, 0))));
[email protected]3642e2d2012-10-29 21:31:1483 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:3084 -1, content::NativeWebKeyboardEvent(ui::KeyEvent(
85 ui::ET_KEY_PRESSED, ui::VKEY_F9, ui::DomCode::F9, 0))));
[email protected]3642e2d2012-10-29 21:31:1486 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:3087 -1, content::NativeWebKeyboardEvent(ui::KeyEvent(
88 ui::ET_KEY_PRESSED, ui::VKEY_F10, ui::DomCode::F10, 0))));
[email protected]3642e2d2012-10-29 21:31:1489
90 // Shift+Control+Alt+F3 is also an Ash accelerator. Don't reserve it.
91 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:3092 -1, content::NativeWebKeyboardEvent(ui::KeyEvent(
93 ui::ET_KEY_PRESSED, ui::VKEY_F3, ui::DomCode::F3,
94 ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN))));
Yuta Hijikata40b891d2020-11-27 09:05:0195#endif // BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]3642e2d2012-10-29 21:31:1496
97#if defined(USE_AURA)
98 // Ctrl+n, Ctrl+w are reserved while Ctrl+f is not.
99
100 // The content::NativeWebKeyboardEvent constructor is available only when
101 // USE_AURA is #defined.
102 EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:30103 IDC_NEW_WINDOW, content::NativeWebKeyboardEvent(ui::KeyEvent(
kpschoedela281bf12016-01-07 15:08:19104 ui::ET_KEY_PRESSED, ui::VKEY_N, ui::DomCode::US_N,
dtapuska821d5772015-10-15 02:51:30105 ui::EF_CONTROL_DOWN))));
[email protected]3642e2d2012-10-29 21:31:14106 EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:30107 IDC_CLOSE_TAB, content::NativeWebKeyboardEvent(ui::KeyEvent(
kpschoedela281bf12016-01-07 15:08:19108 ui::ET_KEY_PRESSED, ui::VKEY_W, ui::DomCode::US_W,
dtapuska821d5772015-10-15 02:51:30109 ui::EF_CONTROL_DOWN))));
[email protected]3642e2d2012-10-29 21:31:14110 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
111 IDC_FIND, content::NativeWebKeyboardEvent(
dtapuska821d5772015-10-15 02:51:30112 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_F,
kpschoedela281bf12016-01-07 15:08:19113 ui::DomCode::US_F, ui::EF_CONTROL_DOWN))));
[email protected]3642e2d2012-10-29 21:31:14114#endif // USE_AURA
115}
116
[email protected]557c4e22012-12-18 18:30:55117TEST_F(BrowserCommandControllerTest, IsReservedCommandOrKeyIsApp) {
Joel Hockey2687ab52019-08-14 23:59:46118 Browser::CreateParams params = Browser::CreateParams::CreateForApp(
119 "app",
120 /*trusted_source=*/true, browser()->window()->GetBounds(), profile(),
121 /*user_gesture=*/true);
122 params.window = browser()->window();
Ramin Halavati1a201c92020-10-29 12:45:16123 set_browser(Browser::Create(params));
[email protected]3642e2d2012-10-29 21:31:14124
Joel Hockey2687ab52019-08-14 23:59:46125 ASSERT_TRUE(browser()->is_type_app());
126
127 // When is_type_app(), no keys are reserved.
Yuta Hijikata40b891d2020-11-27 09:05:01128#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]3642e2d2012-10-29 21:31:14129 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:30130 IDC_BACK, content::NativeWebKeyboardEvent(ui::KeyEvent(
131 ui::ET_KEY_PRESSED, ui::VKEY_F1, ui::DomCode::F1, 0))));
[email protected]3642e2d2012-10-29 21:31:14132 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:30133 IDC_FORWARD, content::NativeWebKeyboardEvent(ui::KeyEvent(
134 ui::ET_KEY_PRESSED, ui::VKEY_F2, ui::DomCode::F2, 0))));
[email protected]3642e2d2012-10-29 21:31:14135 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:30136 IDC_RELOAD, content::NativeWebKeyboardEvent(ui::KeyEvent(
137 ui::ET_KEY_PRESSED, ui::VKEY_F3, ui::DomCode::F3, 0))));
[email protected]3642e2d2012-10-29 21:31:14138 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:30139 -1, content::NativeWebKeyboardEvent(ui::KeyEvent(
140 ui::ET_KEY_PRESSED, ui::VKEY_F4, ui::DomCode::F4, 0))));
Yuta Hijikata40b891d2020-11-27 09:05:01141#endif // BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]3642e2d2012-10-29 21:31:14142
143#if defined(USE_AURA)
144 // The content::NativeWebKeyboardEvent constructor is available only when
145 // USE_AURA is #defined.
146 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:30147 IDC_NEW_WINDOW, content::NativeWebKeyboardEvent(ui::KeyEvent(
kpschoedela281bf12016-01-07 15:08:19148 ui::ET_KEY_PRESSED, ui::VKEY_N, ui::DomCode::US_N,
dtapuska821d5772015-10-15 02:51:30149 ui::EF_CONTROL_DOWN))));
[email protected]3642e2d2012-10-29 21:31:14150 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
dtapuska821d5772015-10-15 02:51:30151 IDC_CLOSE_TAB, content::NativeWebKeyboardEvent(ui::KeyEvent(
kpschoedela281bf12016-01-07 15:08:19152 ui::ET_KEY_PRESSED, ui::VKEY_W, ui::DomCode::US_W,
dtapuska821d5772015-10-15 02:51:30153 ui::EF_CONTROL_DOWN))));
[email protected]3642e2d2012-10-29 21:31:14154 EXPECT_FALSE(browser()->command_controller()->IsReservedCommandOrKey(
155 IDC_FIND, content::NativeWebKeyboardEvent(
dtapuska821d5772015-10-15 02:51:30156 ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_F,
kpschoedela281bf12016-01-07 15:08:19157 ui::DomCode::US_F, ui::EF_CONTROL_DOWN))));
[email protected]3642e2d2012-10-29 21:31:14158#endif // USE_AURA
159}
[email protected]bee964522012-11-20 01:56:53160
Ramin Halavatib2af10a2021-07-05 05:49:42161TEST_F(BrowserWithTestWindowTest, IncognitoCommands) {
[email protected]57b25292014-05-01 16:31:06162 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
163 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_IMPORT_SETTINGS));
estade8c0780f2015-08-21 23:36:41164 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_SHOW_SIGNIN));
[email protected]57b25292014-05-01 16:31:06165
166 TestingProfile* testprofile = browser()->profile()->AsTestingProfile();
167 EXPECT_TRUE(testprofile);
168 testprofile->SetGuestSession(true);
Ivan Sandrk9669d0e2017-12-15 23:50:20169 chrome::BrowserCommandController ::
170 UpdateSharedCommandsForIncognitoAvailability(
Aran Gilmand217b5b2019-04-11 17:45:43171 browser()->command_controller(), testprofile);
[email protected]57b25292014-05-01 16:31:06172 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
173 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_IMPORT_SETTINGS));
estade8c0780f2015-08-21 23:36:41174 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_SHOW_SIGNIN));
[email protected]57b25292014-05-01 16:31:06175
176 testprofile->SetGuestSession(false);
177 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
178 IncognitoModePrefs::FORCED);
Ivan Sandrk9669d0e2017-12-15 23:50:20179 chrome::BrowserCommandController ::
180 UpdateSharedCommandsForIncognitoAvailability(
Aran Gilmand217b5b2019-04-11 17:45:43181 browser()->command_controller(), testprofile);
[email protected]57b25292014-05-01 16:31:06182 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
183 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_IMPORT_SETTINGS));
estade8c0780f2015-08-21 23:36:41184 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_SHOW_SIGNIN));
[email protected]57b25292014-05-01 16:31:06185}
186
[email protected]557c4e22012-12-18 18:30:55187TEST_F(BrowserCommandControllerTest, AppFullScreen) {
[email protected]bee964522012-11-20 01:56:53188 // Enable for tabbed browser.
189 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FULLSCREEN));
190
191 // Enabled for app windows.
Joel Hockey2687ab52019-08-14 23:59:46192 Browser::CreateParams params = Browser::CreateParams::CreateForApp(
193 "app",
194 /*trusted_source=*/true, browser()->window()->GetBounds(), profile(),
195 /*user_gesture=*/true);
196 params.window = browser()->window();
Ramin Halavati1a201c92020-10-29 12:45:16197 set_browser(Browser::Create(params));
Joel Hockey2687ab52019-08-14 23:59:46198 ASSERT_TRUE(browser()->is_type_app());
[email protected]bee964522012-11-20 01:56:53199 browser()->command_controller()->FullscreenStateChanged();
200 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FULLSCREEN));
[email protected]bee964522012-11-20 01:56:53201}
[email protected]557c4e22012-12-18 18:30:55202
anthonyvd66aa95d2015-10-02 15:40:26203TEST_F(BrowserCommandControllerTest, AvatarAcceleratorEnabledOnDesktop) {
[email protected]de71ae992013-07-18 03:30:38204 if (!profiles::IsMultipleProfilesEnabled())
[email protected]557c4e22012-12-18 18:30:55205 return;
206
Evan Stadeef1543f2017-11-17 20:20:13207 TestingProfileManager* testing_profile_manager = profile_manager();
208 ProfileManager* profile_manager = testing_profile_manager->profile_manager();
[email protected]338416c02014-05-13 16:47:06209 chrome::BrowserCommandController command_controller(browser());
Ivan Sandrk9669d0e2017-12-15 23:50:20210 const CommandUpdater* command_updater = &command_controller;
[email protected]557c4e22012-12-18 18:30:55211
[email protected]338416c02014-05-13 16:47:06212 bool enabled = true;
Pavol Marko56df0272018-07-04 17:55:04213 size_t profiles_count = 1U;
Yuta Hijikata40b891d2020-11-27 09:05:01214#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]338416c02014-05-13 16:47:06215 // Chrome OS uses system tray menu to handle multi-profiles.
216 enabled = false;
Pavol Marko56df0272018-07-04 17:55:04217 profiles_count = 2U;
[email protected]338416c02014-05-13 16:47:06218#endif
219
Pavol Marko56df0272018-07-04 17:55:04220 ASSERT_EQ(profiles_count, profile_manager->GetNumberOfProfiles());
[email protected]338416c02014-05-13 16:47:06221 EXPECT_EQ(enabled, command_updater->IsCommandEnabled(IDC_SHOW_AVATAR_MENU));
[email protected]557c4e22012-12-18 18:30:55222
Evan Stadeef1543f2017-11-17 20:20:13223 testing_profile_manager->CreateTestingProfile("p2");
Pavol Marko56df0272018-07-04 17:55:04224 profiles_count++;
225 ASSERT_EQ(profiles_count, profile_manager->GetNumberOfProfiles());
[email protected]338416c02014-05-13 16:47:06226 EXPECT_EQ(enabled, command_updater->IsCommandEnabled(IDC_SHOW_AVATAR_MENU));
[email protected]557c4e22012-12-18 18:30:55227
Evan Stadeef1543f2017-11-17 20:20:13228 testing_profile_manager->DeleteTestingProfile("p2");
Pavol Marko56df0272018-07-04 17:55:04229 profiles_count--;
230 ASSERT_EQ(profiles_count, profile_manager->GetNumberOfProfiles());
[email protected]338416c02014-05-13 16:47:06231 EXPECT_EQ(enabled, command_updater->IsCommandEnabled(IDC_SHOW_AVATAR_MENU));
[email protected]557c4e22012-12-18 18:30:55232}
[email protected]6a414ff2013-02-27 08:22:54233
Ramin Halavatif362b7a2019-04-17 05:24:50234TEST_F(BrowserCommandControllerTest, AvatarMenuAlwaysEnabledInIncognitoMode) {
[email protected]e9476cd2013-12-04 05:55:34235 // Set up a profile with an off the record profile.
[email protected]e9476cd2013-12-04 05:55:34236 TestingProfile::Builder normal_builder;
dcheng9603ab92016-04-08 04:17:32237 std::unique_ptr<TestingProfile> original_profile = normal_builder.Build();
[email protected]e9476cd2013-12-04 05:55:34238
239 // Create a new browser based on the off the record profile.
Ramin Halavati94e457272021-04-13 12:27:51240 Browser::CreateParams profile_params(
241 original_profile->GetPrimaryOTRProfile(/*create_if_needed=*/true), true);
dcheng9603ab92016-04-08 04:17:32242 std::unique_ptr<Browser> otr_browser(
Charles Zhaofa602932020-10-21 01:38:55243 CreateBrowserWithTestWindowForParams(profile_params));
[email protected]e9476cd2013-12-04 05:55:34244
[email protected]338416c02014-05-13 16:47:06245 chrome::BrowserCommandController command_controller(otr_browser.get());
Ivan Sandrk9669d0e2017-12-15 23:50:20246 const CommandUpdater* command_updater = &command_controller;
[email protected]e9476cd2013-12-04 05:55:34247
Ramin Halavatif362b7a2019-04-17 05:24:50248 // The avatar menu should be enabled.
249 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SHOW_AVATAR_MENU));
[email protected]e9476cd2013-12-04 05:55:34250 // The command line is reset at the end of every test by the test suite.
[email protected]e9476cd2013-12-04 05:55:34251}
252
[email protected]6a414ff2013-02-27 08:22:54253//////////////////////////////////////////////////////////////////////////////
sriramsr39b6b7872015-03-04 00:03:32254class BrowserCommandControllerFullscreenTest;
[email protected]6a414ff2013-02-27 08:22:54255
256// A test browser window that can toggle fullscreen state.
sriramsr39b6b7872015-03-04 00:03:32257class FullscreenTestBrowserWindow : public TestBrowserWindow,
258 ExclusiveAccessContext {
[email protected]6a414ff2013-02-27 08:22:54259 public:
sriramsr39b6b7872015-03-04 00:03:32260 FullscreenTestBrowserWindow(
261 BrowserCommandControllerFullscreenTest* test_browser)
zijiehe68cd3dc22017-04-07 18:50:29262 : fullscreen_(false),
263 toolbar_showing_(false),
264 test_browser_(test_browser) {}
sriramsr39b6b7872015-03-04 00:03:32265
dcheng5dd5ff62014-10-21 12:42:38266 ~FullscreenTestBrowserWindow() override {}
[email protected]6a414ff2013-02-27 08:22:54267
268 // TestBrowserWindow overrides:
dcheng5dd5ff62014-10-21 12:42:38269 bool ShouldHideUIForFullscreen() const override { return fullscreen_; }
270 bool IsFullscreen() const override { return fullscreen_; }
271 void EnterFullscreen(const GURL& url,
Mike Wasserman854c9482019-12-20 18:17:19272 ExclusiveAccessBubbleType type,
273 int64_t display_id) override {
[email protected]6a414ff2013-02-27 08:22:54274 fullscreen_ = true;
275 }
dcheng5dd5ff62014-10-21 12:42:38276 void ExitFullscreen() override { fullscreen_ = false; }
zijiehe68cd3dc22017-04-07 18:50:29277 bool IsToolbarShowing() const override { return toolbar_showing_; }
[email protected]6a414ff2013-02-27 08:22:54278
sriramsr39b6b7872015-03-04 00:03:32279 ExclusiveAccessContext* GetExclusiveAccessContext() override { return this; }
280
281 // Exclusive access interface:
282 Profile* GetProfile() override;
283 content::WebContents* GetActiveWebContents() override;
sriramsr39b6b7872015-03-04 00:03:32284 void UpdateExclusiveAccessExitBubbleContent(
285 const GURL& url,
Chong Zhang7a437eb2017-08-04 16:13:47286 ExclusiveAccessBubbleType bubble_type,
Joe Downing64999bc22018-05-01 04:43:00287 ExclusiveAccessBubbleHideCallback bubble_first_hide_callback,
288 bool force_update) override {}
mgiuca6fb14632015-12-08 03:59:47289 void OnExclusiveAccessUserInput() override {}
Ivan Sandrk29a551c2018-10-11 19:08:49290 bool CanUserExitFullscreen() const override { return true; }
sriramsr39b6b7872015-03-04 00:03:32291
zijiehe68cd3dc22017-04-07 18:50:29292 void set_toolbar_showing(bool showing) { toolbar_showing_ = showing; }
293
[email protected]6a414ff2013-02-27 08:22:54294 private:
295 bool fullscreen_;
zijiehe68cd3dc22017-04-07 18:50:29296 bool toolbar_showing_;
sriramsr39b6b7872015-03-04 00:03:32297 BrowserCommandControllerFullscreenTest* test_browser_;
[email protected]6a414ff2013-02-27 08:22:54298
299 DISALLOW_COPY_AND_ASSIGN(FullscreenTestBrowserWindow);
300};
301
302// Test that uses FullscreenTestBrowserWindow for its window.
303class BrowserCommandControllerFullscreenTest
Ramin Halavatib2af10a2021-07-05 05:49:42304 : public BrowserWithTestWindowTest {
[email protected]6a414ff2013-02-27 08:22:54305 public:
Ramin Halavatib2af10a2021-07-05 05:49:42306 BrowserCommandControllerFullscreenTest() = default;
Ramin Halavati3c7580a2020-11-05 06:35:42307 ~BrowserCommandControllerFullscreenTest() override = default;
[email protected]6a414ff2013-02-27 08:22:54308
sriramsr39b6b7872015-03-04 00:03:32309 Browser* GetBrowser() { return BrowserWithTestWindowTest::browser(); }
310
[email protected]6a414ff2013-02-27 08:22:54311 // BrowserWithTestWindowTest overrides:
Alan Screen189db682019-04-18 18:19:23312 std::unique_ptr<BrowserWindow> CreateBrowserWindow() override {
313 return std::make_unique<FullscreenTestBrowserWindow>(this);
[email protected]6a414ff2013-02-27 08:22:54314 }
315
316 private:
317 DISALLOW_COPY_AND_ASSIGN(BrowserCommandControllerFullscreenTest);
318};
319
sriramsr39b6b7872015-03-04 00:03:32320Profile* FullscreenTestBrowserWindow::GetProfile() {
321 return test_browser_->GetBrowser()->profile();
322}
323
324content::WebContents* FullscreenTestBrowserWindow::GetActiveWebContents() {
325 return test_browser_->GetBrowser()->tab_strip_model()->GetActiveWebContents();
326}
327
Ramin Halavatib2af10a2021-07-05 05:49:42328TEST_F(BrowserCommandControllerFullscreenTest,
[email protected]6a414ff2013-02-27 08:22:54329 UpdateCommandsForFullscreenMode) {
zijiehe68cd3dc22017-04-07 18:50:29330 struct {
331 int command_id;
332 // Whether the command is enabled in tab mode.
333 bool enabled_in_tab;
334 // Whether the keyboard shortcut is reserved in tab mode.
335 bool reserved_in_tab;
336 // Whether the command is enabled in fullscreen mode.
337 bool enabled_in_fullscreen;
338 // Whether the keyboard shortcut is reserved in fullscreen mode.
339 bool reserved_in_fullscreen;
340 } commands[] = {
341 // 1. Most commands are disabled in fullscreen.
342 // 2. In fullscreen, only the exit fullscreen commands are reserved. All
343 // other shortcuts should be delivered to the web page. See
344 // http://crbug.com/680809.
345
346 // Command ID | tab mode | fullscreen |
347 // | enabled | reserved | enabled | reserved |
Mihai Sardarescu290bc882018-10-12 10:48:14348 // clang-format off
zijiehe68cd3dc22017-04-07 18:50:29349 { IDC_OPEN_CURRENT_URL, true, false, false, false },
350 { IDC_FOCUS_TOOLBAR, true, false, false, false },
351 { IDC_FOCUS_LOCATION, true, false, false, false },
352 { IDC_FOCUS_SEARCH, true, false, false, false },
353 { IDC_FOCUS_MENU_BAR, true, false, false, false },
354 { IDC_FOCUS_NEXT_PANE, true, false, false, false },
355 { IDC_FOCUS_PREVIOUS_PANE, true, false, false, false },
356 { IDC_FOCUS_BOOKMARKS, true, false, false, false },
357 { IDC_DEVELOPER_MENU, true, false, false, false },
Nico Weber0cc71122019-07-29 17:30:40358#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
zijiehe68cd3dc22017-04-07 18:50:29359 { IDC_FEEDBACK, true, false, false, false },
[email protected]236ad3022013-09-04 03:27:43360#endif
zijiehe68cd3dc22017-04-07 18:50:29361 { IDC_OPTIONS, true, false, false, false },
362 { IDC_IMPORT_SETTINGS, true, false, false, false },
363 { IDC_EDIT_SEARCH_ENGINES, true, false, false, false },
364 { IDC_VIEW_PASSWORDS, true, false, false, false },
365 { IDC_ABOUT, true, false, false, false },
366 { IDC_SHOW_APP_MENU, true, false, false, false },
Tina Wangf09681ac2019-03-27 20:52:10367 { IDC_SEND_TAB_TO_SELF, true, false, false, false },
Tina Wangfa016992019-05-31 00:01:15368 { IDC_SEND_TAB_TO_SELF_SINGLE_TARGET,
369 true, false, false, false },
zijiehe68cd3dc22017-04-07 18:50:29370 { IDC_FULLSCREEN, true, false, true, true },
371 { IDC_CLOSE_TAB, true, true, true, false },
372 { IDC_CLOSE_WINDOW, true, true, true, false },
373 { IDC_NEW_INCOGNITO_WINDOW, true, true, true, false },
374 { IDC_NEW_TAB, true, true, true, false },
375 { IDC_NEW_WINDOW, true, true, true, false },
376 { IDC_SELECT_NEXT_TAB, true, true, true, false },
377 { IDC_SELECT_PREVIOUS_TAB, true, true, true, false },
378 { IDC_EXIT, true, true, true, true },
379 { IDC_SHOW_AS_TAB, false, false, false, false },
Mihai Sardarescu290bc882018-10-12 10:48:14380 { IDC_SHOW_SIGNIN, true, false, true, false },
381 // clang-format on
zijiehe68cd3dc22017-04-07 18:50:29382 };
383 const content::NativeWebKeyboardEvent key_event(
Dave Tapuskab99c7332020-04-22 02:14:18384 blink::WebInputEvent::Type::kUndefined, 0,
Daniel Cheng224569ee2018-04-25 05:45:06385 blink::WebInputEvent::GetStaticTimeStampForTests());
zijiehe68cd3dc22017-04-07 18:50:29386 // Defaults for a tabbed browser.
Avi Drissman22f82872018-12-25 23:09:07387 for (size_t i = 0; i < base::size(commands); i++) {
zijiehe68cd3dc22017-04-07 18:50:29388 SCOPED_TRACE(commands[i].command_id);
389 EXPECT_EQ(chrome::IsCommandEnabled(browser(), commands[i].command_id),
390 commands[i].enabled_in_tab);
391 EXPECT_EQ(browser()->command_controller()->IsReservedCommandOrKey(
392 commands[i].command_id, key_event),
393 commands[i].reserved_in_tab);
394 }
[email protected]6a414ff2013-02-27 08:22:54395
396 // Simulate going fullscreen.
397 chrome::ToggleFullscreenMode(browser());
398 ASSERT_TRUE(browser()->window()->IsFullscreen());
399 browser()->command_controller()->FullscreenStateChanged();
[email protected]0d7717fa2013-03-19 22:06:20400
zijiehe68cd3dc22017-04-07 18:50:29401 // By default, in fullscreen mode, the toolbar should be hidden; and all
402 // platforms behave similarly.
403 EXPECT_FALSE(window()->IsToolbarShowing());
Avi Drissman22f82872018-12-25 23:09:07404 for (size_t i = 0; i < base::size(commands); i++) {
zijiehe68cd3dc22017-04-07 18:50:29405 SCOPED_TRACE(commands[i].command_id);
406 EXPECT_EQ(chrome::IsCommandEnabled(browser(), commands[i].command_id),
407 commands[i].enabled_in_fullscreen);
408 EXPECT_EQ(browser()->command_controller()->IsReservedCommandOrKey(
409 commands[i].command_id, key_event),
410 commands[i].reserved_in_fullscreen);
411 }
zijiehe3c7af992017-02-12 20:59:40412
Avi Drissman73ea8642020-07-29 19:56:15413#if defined(OS_MAC)
zijiehe68cd3dc22017-04-07 18:50:29414 // When the toolbar is showing, commands should be reserved as if the content
415 // were in a tab; IDC_FULLSCREEN should also be reserved.
416 static_cast<FullscreenTestBrowserWindow*>(window())->set_toolbar_showing(
417 true);
zijiehee84b99c2017-03-25 04:46:35418 EXPECT_TRUE(browser()->command_controller()->IsReservedCommandOrKey(
zijiehe68cd3dc22017-04-07 18:50:29419 IDC_FULLSCREEN, key_event));
Avi Drissman22f82872018-12-25 23:09:07420 for (size_t i = 0; i < base::size(commands); i++) {
zijiehe68cd3dc22017-04-07 18:50:29421 if (commands[i].command_id != IDC_FULLSCREEN) {
422 SCOPED_TRACE(commands[i].command_id);
423 EXPECT_EQ(browser()->command_controller()->IsReservedCommandOrKey(
424 commands[i].command_id, key_event),
425 commands[i].reserved_in_tab);
426 }
427 }
428 // Return to default state.
429 static_cast<FullscreenTestBrowserWindow*>(window())->set_toolbar_showing(
430 false);
431#endif
[email protected]6a414ff2013-02-27 08:22:54432
433 // Exit fullscreen.
434 chrome::ToggleFullscreenMode(browser());
435 ASSERT_FALSE(browser()->window()->IsFullscreen());
436 browser()->command_controller()->FullscreenStateChanged();
[email protected]90efc3e2014-04-22 18:39:03437
Avi Drissman22f82872018-12-25 23:09:07438 for (size_t i = 0; i < base::size(commands); i++) {
zijiehe68cd3dc22017-04-07 18:50:29439 SCOPED_TRACE(commands[i].command_id);
440 EXPECT_EQ(chrome::IsCommandEnabled(browser(), commands[i].command_id),
441 commands[i].enabled_in_tab);
442 EXPECT_EQ(browser()->command_controller()->IsReservedCommandOrKey(
443 commands[i].command_id, key_event),
444 commands[i].reserved_in_tab);
445 }
zijiehe13207802017-02-16 08:06:10446
[email protected]90efc3e2014-04-22 18:39:03447 // Guest Profiles disallow some options.
448 TestingProfile* testprofile = browser()->profile()->AsTestingProfile();
449 EXPECT_TRUE(testprofile);
450 testprofile->SetGuestSession(true);
451
452 browser()->command_controller()->FullscreenStateChanged();
[email protected]57b25292014-05-01 16:31:06453 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
454 EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_IMPORT_SETTINGS));
[email protected]6a414ff2013-02-27 08:22:54455}
estade8c0780f2015-08-21 23:36:41456
zijiehedb473d552017-02-24 01:13:41457// Ensure that the logic for enabling IDC_OPTIONS is consistent, regardless of
458// the order of entering fullscreen and forced incognito modes. See
459// http://crbug.com/694331.
Ramin Halavatib2af10a2021-07-05 05:49:42460TEST_F(BrowserWithTestWindowTest, OptionsConsistency) {
zijiehedb473d552017-02-24 01:13:41461 TestingProfile* profile = browser()->profile()->AsTestingProfile();
462 // Setup guest session.
463 profile->SetGuestSession(true);
464 // Setup forced incognito mode.
465 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
466 IncognitoModePrefs::FORCED);
467 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
468 // Enter fullscreen.
469 browser()->command_controller()->FullscreenStateChanged();
470 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
471 // Exit fullscreen
472 browser()->command_controller()->FullscreenStateChanged();
473 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
474 // Reenter incognito mode, this should trigger
475 // UpdateSharedCommandsForIncognitoAvailability() again.
476 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
477 IncognitoModePrefs::DISABLED);
478 IncognitoModePrefs::SetAvailability(browser()->profile()->GetPrefs(),
479 IncognitoModePrefs::FORCED);
480 EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPTIONS));
481}
482
Mihai Sardarescuf87ccc12018-09-26 10:37:45483TEST_F(BrowserCommandControllerTest, IncognitoModeOnSigninAllowedPrefChange) {
estade8c0780f2015-08-21 23:36:41484 // Set up a profile with an off the record profile.
dcheng9603ab92016-04-08 04:17:32485 std::unique_ptr<TestingProfile> profile1 = TestingProfile::Builder().Build();
Ramin Halavati94e457272021-04-13 12:27:51486 Profile* profile2 = profile1->GetPrimaryOTRProfile(/*create_if_needed=*/true);
estade8c0780f2015-08-21 23:36:41487
488 EXPECT_EQ(profile2->GetOriginalProfile(), profile1.get());
489
490 // Create a new browser based on the off the record profile.
Ramin Halavati94e457272021-04-13 12:27:51491 Browser::CreateParams profile_params(
492 profile1->GetPrimaryOTRProfile(/*create_if_needed=*/true), true);
dcheng9603ab92016-04-08 04:17:32493 std::unique_ptr<Browser> browser2(
Charles Zhaofa602932020-10-21 01:38:55494 CreateBrowserWithTestWindowForParams(profile_params));
estade8c0780f2015-08-21 23:36:41495
496 chrome::BrowserCommandController command_controller(browser2.get());
Ivan Sandrk9669d0e2017-12-15 23:50:20497 const CommandUpdater* command_updater = &command_controller;
estade8c0780f2015-08-21 23:36:41498
Mihai Sardarescuf87ccc12018-09-26 10:37:45499 // Check that the SYNC_SETUP command is updated on preference change.
Mihai Sardarescu290bc882018-10-12 10:48:14500 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SHOW_SIGNIN));
Mihai Sardarescuf87ccc12018-09-26 10:37:45501 profile1->GetPrefs()->SetBoolean(prefs::kSigninAllowed, false);
Mihai Sardarescu290bc882018-10-12 10:48:14502 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_SHOW_SIGNIN));
Mihai Sardarescuf87ccc12018-09-26 10:37:45503}
504
505TEST_F(BrowserCommandControllerTest, OnSigninAllowedPrefChange) {
506 chrome::BrowserCommandController command_controller(browser());
507 const CommandUpdater* command_updater = &command_controller;
508
509 // Check that the SYNC_SETUP command is updated on preference change.
Mihai Sardarescu290bc882018-10-12 10:48:14510 EXPECT_TRUE(command_updater->IsCommandEnabled(IDC_SHOW_SIGNIN));
Mihai Sardarescuf87ccc12018-09-26 10:37:45511 profile()->GetPrefs()->SetBoolean(prefs::kSigninAllowed, false);
Mihai Sardarescu290bc882018-10-12 10:48:14512 EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_SHOW_SIGNIN));
estade8c0780f2015-08-21 23:36:41513}
Ramin Halavati3c7580a2020-11-05 06:35:42514
Rohit Agarwala14404f52021-06-14 11:50:44515class IncognitoClearBrowsingDataCommandTest
516 : public BrowserWithTestWindowTest,
517 public testing::WithParamInterface<bool> {
518 public:
519 IncognitoClearBrowsingDataCommandTest() {
520 if (GetParam()) {
521 scoped_feature_list_.InitAndEnableFeature(
522 features::kIncognitoClearBrowsingDataDialogForDesktop);
523 } else {
524 scoped_feature_list_.InitAndDisableFeature(
525 features::kIncognitoClearBrowsingDataDialogForDesktop);
526 }
527 }
528
529 private:
530 base::test::ScopedFeatureList scoped_feature_list_;
531};
532
533INSTANTIATE_TEST_SUITE_P(
534 IncognitoClearBrowsingDataCommandTestWithFeatureFlag,
535 IncognitoClearBrowsingDataCommandTest,
536 /*should_show_cbd_option_in_incognito=*/testing::Bool());
537
538TEST_P(IncognitoClearBrowsingDataCommandTest,
539 testClearBrowsingDataOptionStateInIncognito) {
540 // Set up a profile with an off the record profile.
541 std::unique_ptr<TestingProfile> profile1 = TestingProfile::Builder().Build();
542 Profile* incognito_profile =
543 profile1->GetPrimaryOTRProfile(/*create_if_needed=*/true);
544 EXPECT_EQ(incognito_profile->GetOriginalProfile(), profile1.get());
545
546 // Create a new browser based on the off the record profile.
547 Browser::CreateParams profile_params(incognito_profile, true);
548 std::unique_ptr<Browser> incognito_browser =
549 CreateBrowserWithTestWindowForParams(profile_params);
550
551 chrome::BrowserCommandController command_controller(incognito_browser.get());
552 bool should_show_cbd_option_in_incognito = GetParam();
553 EXPECT_EQ(should_show_cbd_option_in_incognito,
554 command_controller.IsCommandEnabled(IDC_CLEAR_BROWSING_DATA));
555}