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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/profiles/profile_picker_glic_flow_controller.h"
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/test/mock_callback.h"
#include "base/test/test_future.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_destroyer.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/profiles/profile_picker_view_test_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/profile_destruction_waiter.h"
#include "chrome/test/base/profile_waiter.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "content/public/test/browser_test.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
class ProfilePickerGlicFlowControllerBrowserTest : public InProcessBrowserTest {
public:
ProfilePickerGlicFlowControllerBrowserTest() = default;
~ProfilePickerGlicFlowControllerBrowserTest() override = default;
testing::NiceMock<MockProfilePickerWebContentsHost>* host() { return &host_; }
private:
testing::NiceMock<MockProfilePickerWebContentsHost> host_;
};
IN_PROC_BROWSER_TEST_F(ProfilePickerGlicFlowControllerBrowserTest,
InitController) {
EXPECT_CALL(*host(), ShowScreenInPickerContents(
GURL("chrome://profile-picker/?glic"), testing::_));
ProfilePickerGlicFlowController controller(
host(), ClearHostClosure(base::DoNothing()), base::DoNothing());
controller.Init();
}
IN_PROC_BROWSER_TEST_F(ProfilePickerGlicFlowControllerBrowserTest,
PickProfileWithProfileNotLoaded) {
// Create a new Profile and destroy it immediately so that it is not loaded.
ProfileManager* profile_manager = g_browser_process->profile_manager();
base::FilePath new_profile_path =
profile_manager->GenerateNextProfileDirectoryPath();
Profile* new_profile =
&profiles::testing::CreateProfileSync(profile_manager, new_profile_path);
ProfileDestructionWaiter profile_destruction_waiter(new_profile);
Browser* new_browser = CreateBrowser(new_profile);
CloseBrowserSynchronously(new_browser);
profile_destruction_waiter.Wait();
base::MockCallback<base::OnceClosure> clear_host_callback;
EXPECT_CALL(clear_host_callback, Run());
base::MockCallback<base::OnceCallback<void(Profile*)>>
picked_profile_callback;
ProfileWaiter profile_waiter;
// Make sure that the loaded Profile is valid and corresponds to the newly
// created one.
EXPECT_CALL(picked_profile_callback, Run(testing::_))
.WillOnce([&new_profile_path, &profile_manager](Profile* profile) {
ASSERT_TRUE(profile);
EXPECT_EQ(profile->GetPath(), new_profile_path);
EXPECT_FALSE(profile_manager->HasKeepAliveForTesting(
profile, ProfileKeepAliveOrigin::kWaitingForFirstBrowserWindow));
EXPECT_TRUE(profile_manager->HasKeepAliveForTesting(
profile, ProfileKeepAliveOrigin::kWaitingForGlicView));
});
ProfilePickerGlicFlowController controller(
host(), ClearHostClosure(clear_host_callback.Get()),
picked_profile_callback.Get());
base::MockCallback<base::OnceCallback<void(bool)>> mock_callback;
EXPECT_CALL(mock_callback, Run(true));
controller.PickProfile(new_profile_path, ProfilePicker::ProfilePickingArgs(),
mock_callback.Get());
Profile* loaded_profile = profile_waiter.WaitForProfileAdded();
signin::WaitForRefreshTokensLoaded(
IdentityManagerFactory::GetForProfile(loaded_profile));
}
// TODO(crbug.com/404425678): Re-enable failing test on Windows.
#if BUILDFLAG(IS_WIN)
#define MAYBE_PickProfileWithCurrentProfile \
DISABLED_PickProfileWithCurrentProfile
#else
#define MAYBE_PickProfileWithCurrentProfile PickProfileWithCurrentProfile
#endif
IN_PROC_BROWSER_TEST_F(ProfilePickerGlicFlowControllerBrowserTest,
MAYBE_PickProfileWithCurrentProfile) {
base::MockCallback<base::OnceClosure> clear_host_callback;
EXPECT_CALL(clear_host_callback, Run());
base::MockCallback<base::OnceCallback<void(Profile*)>>
picked_profile_callback;
// Return the currently active profile right away if it is already loaded.
EXPECT_CALL(picked_profile_callback, Run(browser()->profile()));
ProfilePickerGlicFlowController controller(
host(), ClearHostClosure(clear_host_callback.Get()),
picked_profile_callback.Get());
base::MockCallback<base::OnceCallback<void(bool)>> mock_callback;
EXPECT_CALL(mock_callback, Run(true));
controller.PickProfile(browser()->profile()->GetPath(),
ProfilePicker::ProfilePickingArgs(),
mock_callback.Get());
}
IN_PROC_BROWSER_TEST_F(ProfilePickerGlicFlowControllerBrowserTest,
PickProfileWithNonExistingProfile) {
base::MockCallback<base::OnceClosure> clear_host_callback;
EXPECT_CALL(clear_host_callback, Run());
base::MockCallback<base::OnceCallback<void(Profile*)>>
picked_profile_callback;
// Callback returns a nullptr profile for non existing profiles.
EXPECT_CALL(picked_profile_callback, Run(nullptr));
ProfilePickerGlicFlowController controller(
host(), ClearHostClosure(clear_host_callback.Get()),
picked_profile_callback.Get());
// Next profile directly is guaranteed not to be tied to an existing profile
// as it was not created yet.
base::FilePath non_profile_file_path =
g_browser_process->profile_manager()->GenerateNextProfileDirectoryPath();
base::MockCallback<base::OnceCallback<void(bool)>> mock_callback;
EXPECT_CALL(mock_callback, Run(false));
controller.PickProfile(non_profile_file_path,
ProfilePicker::ProfilePickingArgs(),
mock_callback.Get());
}
IN_PROC_BROWSER_TEST_F(ProfilePickerGlicFlowControllerBrowserTest,
ClearingControllerShouldCallTheInputCallbacks) {
base::MockCallback<base::OnceClosure> clear_host_callback;
EXPECT_CALL(clear_host_callback, Run());
base::MockCallback<base::OnceCallback<void(Profile*)>>
picked_profile_callback;
// Callback returns a nullptr profile if no profile was selected.
EXPECT_CALL(picked_profile_callback, Run(nullptr));
{
ProfilePickerGlicFlowController controller(
host(), ClearHostClosure(clear_host_callback.Get()),
picked_profile_callback.Get());
}
// Controller is destroyed without any profile selection.
}
|