1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Cocoa/Cocoa.h>
#include "base/apple/scoped_objc_class_swizzler.h"
#include "base/files/file_path.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "chrome/app/chrome_command_ids.h"
#import "chrome/browser/app_controller_mac.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/delete_profile_helper.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_metrics.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/platform_test.h"
namespace {
id __weak* TargetForAction() {
static id __weak targetForAction;
return &targetForAction;
}
} // namespace
@interface FakeBrowserWindow : NSWindow
@end
@implementation FakeBrowserWindow
@end
// A class providing alternative implementations of various methods.
@interface AppControllerKeyEquivalentTestHelper : NSObject
- (id __weak)targetForAction:(SEL)selector;
- (BOOL)windowHasBrowserTabs:(NSWindow*)window;
@end
@implementation AppControllerKeyEquivalentTestHelper
- (id __weak)targetForAction:(SEL)selector {
return *TargetForAction();
}
- (BOOL)windowHasBrowserTabs:(NSWindow*)window {
return [window isKindOfClass:[FakeBrowserWindow class]];
}
@end
class AppControllerTest : public PlatformTest {
protected:
AppControllerTest()
: profile_manager_(TestingBrowserProcess::GetGlobal()),
profile_(nullptr) {}
void SetUp() override {
PlatformTest::SetUp();
ASSERT_TRUE(profile_manager_.SetUp());
profile_ = profile_manager_.CreateTestingProfile("New Profile 1");
}
void TearDown() override {
TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr);
base::RunLoop().RunUntilIdle();
PlatformTest::TearDown();
}
content::BrowserTaskEnvironment task_environment_;
TestingProfileManager profile_manager_;
raw_ptr<TestingProfile, DanglingUntriaged> profile_;
};
class AppControllerKeyEquivalentTest : public PlatformTest {
protected:
AppControllerKeyEquivalentTest() = default;
void SetUp() override {
PlatformTest::SetUp();
nsapp_target_for_action_swizzler_ =
std::make_unique<base::apple::ScopedObjCClassSwizzler>(
[NSApp class], [AppControllerKeyEquivalentTestHelper class],
@selector(targetForAction:));
app_controller_swizzler_ =
std::make_unique<base::apple::ScopedObjCClassSwizzler>(
[AppController class], [AppControllerKeyEquivalentTestHelper class],
@selector(windowHasBrowserTabs:));
app_controller_ = AppController.sharedController;
close_window_menu_item_ = [[NSMenuItem alloc] initWithTitle:@""
action:nullptr
keyEquivalent:@""];
[app_controller_ setCloseWindowMenuItemForTesting:close_window_menu_item_];
close_tab_menu_item_ = [[NSMenuItem alloc] initWithTitle:@""
action:nullptr
keyEquivalent:@""];
[app_controller_ setCloseTabMenuItemForTesting:close_tab_menu_item_];
}
void CheckMenuItemsMatchBrowserWindow() {
ASSERT_EQ([NSApp targetForAction:@selector(performClose:)],
*TargetForAction());
[app_controller_ updateMenuItemKeyEquivalents];
EXPECT_TRUE([[close_window_menu_item_ keyEquivalent] isEqualToString:@"W"]);
EXPECT_EQ([close_window_menu_item_ keyEquivalentModifierMask],
NSEventModifierFlagCommand);
EXPECT_TRUE([[close_tab_menu_item_ keyEquivalent] isEqualToString:@"w"]);
EXPECT_EQ([close_tab_menu_item_ keyEquivalentModifierMask],
NSEventModifierFlagCommand);
}
void CheckMenuItemsMatchNonBrowserWindow() {
ASSERT_EQ([NSApp targetForAction:@selector(performClose:)],
*TargetForAction());
[app_controller_ updateMenuItemKeyEquivalents];
EXPECT_TRUE([[close_window_menu_item_ keyEquivalent] isEqualToString:@"w"]);
EXPECT_EQ([close_window_menu_item_ keyEquivalentModifierMask],
NSEventModifierFlagCommand);
EXPECT_TRUE([[close_tab_menu_item_ keyEquivalent] isEqualToString:@""]);
EXPECT_EQ([close_tab_menu_item_ keyEquivalentModifierMask], 0UL);
}
void TearDown() override {
PlatformTest::TearDown();
[app_controller_ setCloseWindowMenuItemForTesting:nil];
[app_controller_ setCloseTabMenuItemForTesting:nil];
*TargetForAction() = nil;
}
private:
std::unique_ptr<base::apple::ScopedObjCClassSwizzler>
nsapp_target_for_action_swizzler_;
std::unique_ptr<base::apple::ScopedObjCClassSwizzler>
app_controller_swizzler_;
AppController* __strong app_controller_;
NSMenuItem* __strong close_window_menu_item_;
NSMenuItem* __strong close_tab_menu_item_;
};
TEST_F(AppControllerTest, DockMenuProfileNotLoaded) {
AppController* app_controller = AppController.sharedController;
NSMenu* menu = [app_controller applicationDockMenu:NSApp];
// Incognito item is hidden when the profile is not loaded.
EXPECT_EQ(nil, [app_controller lastProfileIfLoaded]);
EXPECT_EQ(-1, [menu indexOfItemWithTag:IDC_NEW_INCOGNITO_WINDOW]);
}
TEST_F(AppControllerTest, DockMenu) {
PrefService* local_state = g_browser_process->local_state();
local_state->SetString(prefs::kProfileLastUsed,
profile_->GetPath().BaseName().MaybeAsASCII());
AppController* app_controller = AppController.sharedController;
NSMenu* menu = [app_controller applicationDockMenu:NSApp];
NSMenuItem* item;
EXPECT_TRUE(menu);
EXPECT_NE(-1, [menu indexOfItemWithTag:IDC_NEW_WINDOW]);
// Incognito item is shown when the profile is loaded.
EXPECT_EQ(profile_, [app_controller lastProfileIfLoaded]);
EXPECT_NE(-1, [menu indexOfItemWithTag:IDC_NEW_INCOGNITO_WINDOW]);
for (item in [menu itemArray]) {
EXPECT_EQ(app_controller, [item target]);
EXPECT_EQ(@selector(commandFromDock:), [item action]);
}
}
TEST_F(AppControllerTest, LastProfileIfLoaded) {
// Create a second profile.
base::FilePath dest_path1 = profile_->GetPath();
base::FilePath dest_path2 =
profile_manager_.CreateTestingProfile("New Profile 2")->GetPath();
ASSERT_EQ(2U, profile_manager_.profile_manager()->GetNumberOfProfiles());
ASSERT_EQ(2U, profile_manager_.profile_manager()->GetLoadedProfiles().size());
PrefService* local_state = g_browser_process->local_state();
local_state->SetString(prefs::kProfileLastUsed,
dest_path1.BaseName().MaybeAsASCII());
AppController* app_controller = AppController.sharedController;
// Delete the active profile.
profile_manager_.profile_manager()
->GetDeleteProfileHelper()
.MaybeScheduleProfileForDeletion(
dest_path1, base::DoNothing(),
ProfileMetrics::DELETE_PROFILE_USER_MANAGER);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(dest_path2, app_controller.lastProfileIfLoaded->GetPath());
}
// Tests key equivalents for Close Window when target is a child window (like a
// bubble).
TEST_F(AppControllerKeyEquivalentTest, UpdateMenuItemsForBubbleWindow) {
// Set up the "bubble" and main window.
const NSRect kContentRect = NSMakeRect(0.0, 0.0, 10.0, 10.0);
NSWindow* child_window =
[[NSWindow alloc] initWithContentRect:kContentRect
styleMask:NSWindowStyleMaskClosable
backing:NSBackingStoreBuffered
defer:YES];
child_window.releasedWhenClosed = NO;
NSWindow* browser_window =
[[FakeBrowserWindow alloc] initWithContentRect:kContentRect
styleMask:NSWindowStyleMaskClosable
backing:NSBackingStoreBuffered
defer:YES];
browser_window.releasedWhenClosed = NO;
[browser_window addChildWindow:child_window ordered:NSWindowAbove];
*TargetForAction() = child_window;
CheckMenuItemsMatchBrowserWindow();
}
// Tests key equivalents for Close Window when target is an NSPopOver.
TEST_F(AppControllerKeyEquivalentTest, UpdateMenuItemsForPopover) {
// Set up the popover and main window.
const NSRect kContentRect = NSMakeRect(0.0, 0.0, 10.0, 10.0);
NSPopover* popover = [[NSPopover alloc] init];
NSWindow* popover_window =
[[NSWindow alloc] initWithContentRect:kContentRect
styleMask:NSWindowStyleMaskClosable
backing:NSBackingStoreBuffered
defer:YES];
popover_window.releasedWhenClosed = NO;
[popover setContentViewController:[[NSViewController alloc] init]];
[[popover contentViewController] setView:[popover_window contentView]];
NSWindow* browser_window =
[[FakeBrowserWindow alloc] initWithContentRect:kContentRect
styleMask:NSWindowStyleMaskClosable
backing:NSBackingStoreBuffered
defer:YES];
browser_window.releasedWhenClosed = NO;
[browser_window addChildWindow:popover_window ordered:NSWindowAbove];
*TargetForAction() = popover;
CheckMenuItemsMatchBrowserWindow();
}
// Tests key equivalents for Close Window when target is a browser window.
TEST_F(AppControllerKeyEquivalentTest, UpdateMenuItemsForBrowserWindow) {
// Set up the browser window.
const NSRect kContentRect = NSMakeRect(0.0, 0.0, 10.0, 10.0);
NSWindow* browser_window =
[[FakeBrowserWindow alloc] initWithContentRect:kContentRect
styleMask:NSWindowStyleMaskClosable
backing:NSBackingStoreBuffered
defer:YES];
*TargetForAction() = browser_window;
CheckMenuItemsMatchBrowserWindow();
}
// Tests key equivalents for Close Window when target is not a browser window.
TEST_F(AppControllerKeyEquivalentTest, UpdateMenuItemsForNonBrowserWindow) {
// Set up the window.
const NSRect kContentRect = NSMakeRect(0.0, 0.0, 10.0, 10.0);
NSWindow* main_window =
[[NSWindow alloc] initWithContentRect:kContentRect
styleMask:NSWindowStyleMaskClosable
backing:NSBackingStoreBuffered
defer:YES];
*TargetForAction() = main_window;
CheckMenuItemsMatchNonBrowserWindow();
}
// Tests key equivalents for Close Window when target is not a window.
TEST_F(AppControllerKeyEquivalentTest, UpdateMenuItemsForNonWindow) {
NSObject* non_window_object = [[NSObject alloc] init];
*TargetForAction() = non_window_object;
CheckMenuItemsMatchNonBrowserWindow();
}
class AppControllerSafeProfileTest : public AppControllerTest {
protected:
AppControllerSafeProfileTest() = default;
~AppControllerSafeProfileTest() override = default;
};
// Tests that RunInLastProfileSafely() works with an already-loaded
// profile.
TEST_F(AppControllerSafeProfileTest, LastProfileLoaded) {
PrefService* local_state = g_browser_process->local_state();
local_state->SetString(prefs::kProfileLastUsed,
profile_->GetPath().BaseName().MaybeAsASCII());
AppController* app_controller = AppController.sharedController;
ASSERT_EQ(profile_, app_controller.lastProfileIfLoaded);
base::RunLoop run_loop;
app_controller_mac::RunInLastProfileSafely(
base::BindLambdaForTesting([&](Profile* profile) {
EXPECT_EQ(profile, profile_.get());
run_loop.Quit();
}),
app_controller_mac::kIgnoreOnFailure);
run_loop.Run();
}
// Tests that RunInLastProfileSafely() re-loads the profile from disk if
// it's not currently in memory.
TEST_F(AppControllerSafeProfileTest, LastProfileNotLoaded) {
PrefService* local_state = g_browser_process->local_state();
local_state->SetString(prefs::kProfileLastUsed, "New Profile 2");
AppController* app_controller = AppController.sharedController;
ASSERT_EQ(nil, app_controller.lastProfileIfLoaded);
base::RunLoop run_loop;
app_controller_mac::RunInLastProfileSafely(
base::BindLambdaForTesting([&](Profile* profile) {
EXPECT_NE(profile, nullptr);
EXPECT_NE(profile, profile_.get());
EXPECT_EQ(profile->GetBaseName().MaybeAsASCII(), "New Profile 2");
run_loop.Quit();
}),
app_controller_mac::kIgnoreOnFailure);
run_loop.Run();
}
// Tests that RunInProfileInSafeProfileHelper::RunInProfile() works with an
// already-loaded profile.
TEST_F(AppControllerSafeProfileTest, SpecificProfileLoaded) {
PrefService* local_state = g_browser_process->local_state();
local_state->SetString(prefs::kProfileLastUsed,
profile_->GetPath().BaseName().MaybeAsASCII());
AppController* app_controller = AppController.sharedController;
ASSERT_EQ(profile_, app_controller.lastProfileIfLoaded);
TestingProfile* profile2 =
profile_manager_.CreateTestingProfile("New Profile 2");
base::RunLoop run_loop;
app_controller_mac::RunInProfileSafely(
profile_manager_.profiles_dir().AppendASCII("New Profile 2"),
base::BindLambdaForTesting([&](Profile* profile) {
// This should run with the specific profile we asked for, rather than
// the last-used profile.
EXPECT_EQ(profile, profile2);
run_loop.Quit();
}),
app_controller_mac::kIgnoreOnFailure);
run_loop.Run();
}
// Tests that RunInProfileSafely() re-loads the profile from
// disk if it's not currently in memory.
TEST_F(AppControllerSafeProfileTest, SpecificProfileNotLoaded) {
PrefService* local_state = g_browser_process->local_state();
local_state->SetString(prefs::kProfileLastUsed,
profile_->GetPath().BaseName().MaybeAsASCII());
AppController* app_controller = AppController.sharedController;
ASSERT_EQ(profile_, app_controller.lastProfileIfLoaded);
base::RunLoop run_loop;
app_controller_mac::RunInProfileSafely(
profile_manager_.profiles_dir().AppendASCII("New Profile 2"),
base::BindLambdaForTesting([&](Profile* profile) {
// This should run with the specific profile we asked for, rather than
// the last-used profile.
EXPECT_NE(profile, nullptr);
EXPECT_NE(profile, profile_.get());
EXPECT_EQ(profile->GetBaseName().MaybeAsASCII(), "New Profile 2");
run_loop.Quit();
}),
app_controller_mac::kIgnoreOnFailure);
run_loop.Run();
}
|