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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
|
// 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 "chrome/browser/app_controller_mac.h"
#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"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/delete_profile_helper.h"
#include "chrome/browser/profiles/profile_attributes_init_params.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_metrics.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.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"
#include "ui/base/l10n/l10n_util_mac.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;
_cmdw_menu_item =
[[NSMenuItem alloc] initWithTitle:@""
action:@selector(commandDispatch:)
keyEquivalent:@"w"];
_cmdw_menu_item.keyEquivalentModifierMask = NSEventModifierFlagCommand;
_cmdw_menu_item.tag = IDC_CLOSE_TAB;
[_app_controller setCmdWMenuItemForTesting:_cmdw_menu_item];
_shift_cmdw_menu_item =
[[NSMenuItem alloc] initWithTitle:@""
action:@selector(performClose:)
keyEquivalent:@"W"];
_shift_cmdw_menu_item.keyEquivalentModifierMask =
NSEventModifierFlagCommand;
_shift_cmdw_menu_item.tag = IDC_CLOSE_WINDOW;
[_app_controller setShiftCmdWMenuItemForTesting:_shift_cmdw_menu_item];
}
void CheckMenuItemsMatchBrowserWindow() {
ASSERT_EQ([NSApp targetForAction:@selector(performClose:)],
*TargetForAction());
[_app_controller updateMenuItemKeyEquivalents];
EXPECT_FALSE(_shift_cmdw_menu_item.hidden);
EXPECT_EQ(_shift_cmdw_menu_item.tag, IDC_CLOSE_WINDOW);
EXPECT_EQ(_shift_cmdw_menu_item.action, @selector(performClose:));
EXPECT_TRUE([_shift_cmdw_menu_item.title
isEqualToString:l10n_util::GetNSStringWithFixup(IDS_CLOSE_WINDOW_MAC)]);
EXPECT_FALSE(_cmdw_menu_item.hidden);
EXPECT_EQ(_cmdw_menu_item.tag, IDC_CLOSE_TAB);
EXPECT_EQ(_cmdw_menu_item.action, @selector(commandDispatch:));
EXPECT_TRUE([_cmdw_menu_item.title
isEqualToString:l10n_util::GetNSStringWithFixup(IDS_CLOSE_TAB_MAC)]);
}
void CheckMenuItemsMatchNonBrowserWindow() {
ASSERT_EQ([NSApp targetForAction:@selector(performClose:)],
*TargetForAction());
[_app_controller updateMenuItemKeyEquivalents];
EXPECT_TRUE(_shift_cmdw_menu_item.hidden);
EXPECT_FALSE(_cmdw_menu_item.hidden);
EXPECT_EQ(_cmdw_menu_item.tag, IDC_CLOSE_WINDOW);
EXPECT_EQ(_cmdw_menu_item.action, @selector(performClose:));
EXPECT_TRUE([_cmdw_menu_item.title
isEqualToString:l10n_util::GetNSStringWithFixup(IDS_CLOSE_WINDOW_MAC)]);
}
// Check that we don't perform any shortcut switching when there's a custom
// shortcut assigned to File->Close Window.
void VerifyMenuItemsForCustomCloseWindowShortcut() {
ASSERT_EQ([NSApp targetForAction:@selector(performClose:)],
*TargetForAction());
NSMenuItem* close_tab_menu_item = _cmdw_menu_item;
NSMenuItem* close_window_menu_item = _shift_cmdw_menu_item;
// Assign a custom shortcut to Close Window.
close_window_menu_item.keyEquivalent = @"w";
close_window_menu_item.keyEquivalentModifierMask =
NSEventModifierFlagCommand | NSEventModifierFlagControl;
[_app_controller updateMenuItemKeyEquivalents];
// Both menu items should be undisturbed from their original states.
EXPECT_FALSE(close_tab_menu_item.hidden);
EXPECT_EQ(close_tab_menu_item.tag, IDC_CLOSE_TAB);
EXPECT_EQ(close_tab_menu_item.action, @selector(commandDispatch:));
EXPECT_TRUE([close_tab_menu_item.keyEquivalent isEqualToString:@"w"]);
EXPECT_EQ(close_tab_menu_item.keyEquivalentModifierMask,
NSEventModifierFlagCommand);
EXPECT_FALSE(close_window_menu_item.hidden);
EXPECT_EQ(close_window_menu_item.tag, IDC_CLOSE_WINDOW);
EXPECT_EQ(close_window_menu_item.action, @selector(performClose:));
EXPECT_TRUE([close_window_menu_item.keyEquivalent isEqualToString:@"w"]);
EXPECT_EQ(close_window_menu_item.keyEquivalentModifierMask,
NSEventModifierFlagCommand | NSEventModifierFlagControl);
}
// Check that we don't perform any shortcut switching when there's a custom
// shortcut assigned to File->Close Tab.
void VerifyMenuItemsForCustomCloseTabShortcut() {
ASSERT_EQ([NSApp targetForAction:@selector(performClose:)],
*TargetForAction());
NSMenuItem* close_tab_menu_item = _cmdw_menu_item;
NSMenuItem* close_window_menu_item = _shift_cmdw_menu_item;
// Assign a custom shortcut to Close Tab.
close_tab_menu_item.keyEquivalent = @"w";
close_tab_menu_item.keyEquivalentModifierMask =
NSEventModifierFlagCommand | NSEventModifierFlagControl;
[_app_controller updateMenuItemKeyEquivalents];
// Both menu items should be undisturbed from their original states.
EXPECT_FALSE(close_tab_menu_item.hidden);
EXPECT_EQ(close_tab_menu_item.tag, IDC_CLOSE_TAB);
EXPECT_EQ(close_tab_menu_item.action, @selector(commandDispatch:));
EXPECT_TRUE([close_tab_menu_item.keyEquivalent isEqualToString:@"w"]);
EXPECT_EQ(close_tab_menu_item.keyEquivalentModifierMask,
NSEventModifierFlagCommand | NSEventModifierFlagControl);
EXPECT_FALSE(close_window_menu_item.hidden);
EXPECT_EQ(close_window_menu_item.tag, IDC_CLOSE_WINDOW);
EXPECT_EQ(close_window_menu_item.action, @selector(performClose:));
EXPECT_TRUE([close_window_menu_item.keyEquivalent isEqualToString:@"W"]);
EXPECT_EQ(close_window_menu_item.keyEquivalentModifierMask,
NSEventModifierFlagCommand);
}
void TearDown() override {
PlatformTest::TearDown();
[_app_controller setCmdWMenuItemForTesting:nil];
[_app_controller setShiftCmdWMenuItemForTesting: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 _cmdw_menu_item;
NSMenuItem* __strong _shift_cmdw_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 a descendant of a
// browser window.
TEST_F(AppControllerKeyEquivalentTest,
UpdateMenuItemsForBrowserWindowDescendant) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kImmersiveFullscreen);
// 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];
// Set up descendants.
NSWindow* child_window = [[NSWindow alloc] init];
[browser_window addChildWindow:child_window ordered:NSWindowAbove];
NSWindow* child_child_window = [[NSWindow alloc] init];
[child_window addChildWindow:child_child_window ordered:NSWindowAbove];
*TargetForAction() = child_child_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();
}
// Tests key equivalents for Close Window and Close Tab when we shift from one
// browser window to no browser windows, and then back to one browser window.
TEST_F(AppControllerKeyEquivalentTest, MenuItemsUpdateWithWindowChanges) {
// 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();
// "Close" it.
NSObject* non_window_object = [[NSObject alloc] init];
*TargetForAction() = non_window_object;
CheckMenuItemsMatchNonBrowserWindow();
// "New" window.
*TargetForAction() = browser_window;
CheckMenuItemsMatchBrowserWindow();
}
TEST_F(AppControllerKeyEquivalentTest,
DontChangeShortcutsWhenCustomCloseWindowShortcutAssigned) {
// 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;
VerifyMenuItemsForCustomCloseWindowShortcut();
}
TEST_F(AppControllerKeyEquivalentTest,
DontChangeShortcutsWhenCustomCloseTabShortcutAssigned) {
// 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;
VerifyMenuItemsForCustomCloseTabShortcut();
}
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);
// Add a profile in the cache (simulate another profile on disk).
ProfileManager* profile_manager = g_browser_process->profile_manager();
ProfileAttributesStorage* profile_storage =
&profile_manager->GetProfileAttributesStorage();
const base::FilePath profile_path =
profile_manager->GenerateNextProfileDirectoryPath();
ProfileAttributesInitParams params;
params.profile_path = profile_path;
params.profile_name = u"New Profile 2";
profile_storage->AddProfile(std::move(params));
base::RunLoop run_loop;
app_controller_mac::RunInProfileSafely(
profile_path, 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->GetPath(), profile_path);
run_loop.Quit();
}),
app_controller_mac::kIgnoreOnFailure);
run_loop.Run();
}
// Tests that RunInProfileSafely() returns nullptr if a profle doesn't exist.
TEST_F(AppControllerSafeProfileTest, SpecificProfileDoesNotExist) {
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("Non-existent Profile"),
base::BindLambdaForTesting([&](Profile* profile) {
EXPECT_EQ(profile, nullptr);
run_loop.Quit();
}),
app_controller_mac::kIgnoreOnFailure);
run_loop.Run();
}
|