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
|
// Copyright 2022 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_view_test_utils.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/notreached.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/ui/profiles/profile_picker.h"
#include "chrome/browser/ui/profiles/profile_ui_test_utils.h"
#include "chrome/browser/ui/views/profiles/profile_management_step_controller.h"
#include "chrome/browser/ui/views/profiles/profile_management_types.h"
#include "chrome/browser/ui/views/profiles/profile_picker_view.h"
#include "chrome/browser/ui/webui/signin/managed_user_profile_notice_handler.h"
#include "chrome/browser/ui/webui/signin/managed_user_profile_notice_ui.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/view.h"
#if BUILDFLAG(IS_ANDROID)
#error This file should only be included on desktop.
#endif
namespace {
content::WebContents* GetPickerWebContents() {
if (!ProfilePicker::GetWebViewForTesting()) {
return nullptr;
}
return ProfilePicker::GetWebViewForTesting()->GetWebContents();
}
class TestProfileManagementFlowController
: public ProfileManagementFlowController,
public content::WebContentsObserver {
public:
TestProfileManagementFlowController(
ProfilePickerWebContentsHost* host,
ClearHostClosure clear_host_callback,
Step step,
ProfileManagementStepTestView::StepControllerFactory factory,
base::OnceClosure initial_step_load_finished_closure)
: ProfileManagementFlowController(host,
std::move(clear_host_callback),
/*flow_type_string==*/"TestFlow"),
step_(step),
step_controller_factory_(std::move(factory)),
initial_step_load_finished_closure_(
std::move(initial_step_load_finished_closure)) {}
void Init() override {
RegisterStep(step_, step_controller_factory_.Run(host()));
SwitchToStep(
step_, /*reset_state=*/true,
/*step_switch_finished_callback=*/
StepSwitchFinishedCallback(base::BindOnce(
&TestProfileManagementFlowController::OnInitialStepSwitchFinished,
weak_ptr_factory_.GetWeakPtr())));
}
void OnInitialStepSwitchFinished(bool success) {
if (host()->GetPickerContents()->IsLoading()) {
Observe(host()->GetPickerContents());
} else {
DCHECK(initial_step_load_finished_closure_);
std::move(initial_step_load_finished_closure_).Run();
}
}
void DidStopLoading() override {
Observe(nullptr);
DCHECK(initial_step_load_finished_closure_);
std::move(initial_step_load_finished_closure_).Run();
}
void CancelPostSignInFlow() override { NOTREACHED(); }
void PickProfile(
const base::FilePath& profile_path,
ProfilePicker::ProfilePickingArgs args,
base::OnceCallback<void(bool)> pick_profile_complete_callback) override {
NOTREACHED();
}
Step step_;
ProfileManagementStepTestView::StepControllerFactory step_controller_factory_;
base::OnceClosure initial_step_load_finished_closure_;
base::WeakPtrFactory<TestProfileManagementFlowController> weak_ptr_factory_{
this};
};
} // namespace
// -- ViewAddedWaiter ----------------------------------------------------------
ViewAddedWaiter::ViewAddedWaiter(views::View* view) : view_(view) {}
ViewAddedWaiter::~ViewAddedWaiter() = default;
void ViewAddedWaiter::Wait() {
if (view_->GetWidget()) {
return;
}
observation_.Observe(view_.get());
run_loop_.Run();
}
void ViewAddedWaiter::OnViewAddedToWidget(views::View* observed_view) {
if (observed_view == view_) {
run_loop_.Quit();
}
}
// -- ViewDeletedWaiter --------------------------------------------------------
ViewDeletedWaiter::ViewDeletedWaiter(views::View* view) {
DCHECK(view);
observation_.Observe(view);
}
ViewDeletedWaiter::~ViewDeletedWaiter() = default;
void ViewDeletedWaiter::Wait() {
run_loop_.Run();
}
void ViewDeletedWaiter::OnViewIsDeleting(views::View* observed_view) {
// Reset the observation before the view is actually deleted.
observation_.Reset();
run_loop_.Quit();
}
// -- PickerLoadStopWaiter -----------------------------------------------------
PickerLoadStopWaiter::PickerLoadStopWaiter(views::WebView* web_view,
const GURL& expected_url,
Mode wait_mode)
: web_view_(*web_view), expected_url_(expected_url), wait_mode_(wait_mode) {
CHECK(!expected_url.is_empty() || wait_mode_ == Mode::kCheckUrlAtNextLoad);
// Observe attached WebContents changes. This can happen when navigating
// between a page rendered in the system profile and one rendered in a user's
// regular profile. Like the "profile type choice" -> "sign-in page"
// transition for example.
web_contents_attached_subscription_ =
web_view->AddWebContentsAttachedCallback(
base::BindRepeating(&PickerLoadStopWaiter::OnWebContentsAttached,
base::Unretained(this)));
}
void PickerLoadStopWaiter::DidStopLoading() {
if (ShouldKeepWaiting()) {
DVLOG(1) << "Load completed but stop condition not met, ignoring event.";
return;
}
// Quitting the loop via a posted task to make sure that any prod work that
// also is triggered via this same WebContents event can complete before the
// test code resumes.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, run_loop_.QuitClosure());
}
void PickerLoadStopWaiter::Wait() {
if (!ShouldKeepWaiting()) {
DVLOG(1) << "Stop condition already met, wait not needed.";
return;
}
DVLOG(1) << "Starting wait for the completed navigation to " << expected_url_;
Observe(web_view_->web_contents());
run_loop_.Run();
}
void PickerLoadStopWaiter::OnWebContentsAttached(views::WebView* web_view) {
DVLOG(1) << "New WebContents attached. Updating the observation target.";
auto* web_contents = web_view_->web_contents();
Observe(web_contents);
// Attempt to process a load that might have happened before the `WebContents`
// is swapped in. `ProfilePickerView` cross-profile page loads typically
// happen that way.
if (web_contents && !web_contents->IsLoading()) {
DidStopLoading();
}
}
bool PickerLoadStopWaiter::ShouldKeepWaiting() const {
auto* web_contents = web_view_->web_contents();
if (!web_contents || web_contents->IsLoading()) {
return true;
}
auto& current_url = web_contents->GetLastCommittedURL();
switch (wait_mode_) {
case Mode::kWaitUntilUrlLoaded:
if (current_url != expected_url_) {
DVLOG(1) << "WebContents stopped loading on URL that doesn't match the "
"expected one. Actual URL: "
<< current_url;
return true;
}
return false;
case Mode::kCheckUrlAtNextLoad:
if (!expected_url_.is_empty()) {
EXPECT_EQ(current_url, expected_url_);
}
return false;
}
}
// -- ProfileManagementStepTestView --------------------------------------------
ProfileManagementStepTestView::ProfileManagementStepTestView(
ProfilePicker::Params&& params,
ProfileManagementFlowController::Step step,
StepControllerFactory step_controller_factory)
: ProfilePickerView(std::move(params)),
step_(step),
step_controller_factory_(std::move(step_controller_factory)) {}
ProfileManagementStepTestView::~ProfileManagementStepTestView() = default;
void ProfileManagementStepTestView::ShowAndWait(
std::optional<gfx::Size> view_size) {
Display();
// waits for the view to be shown to return. If we don't wait enough
// and the test is flaky, try to poll the page to check the presence of some
// UI elements to know when to stop waiting.
run_loop_.Run();
if (view_size.has_value()) {
GetWidget()->SetSize(view_size.value());
}
}
std::unique_ptr<ProfileManagementFlowController>
ProfileManagementStepTestView::CreateFlowController(
Profile* picker_profile,
ClearHostClosure clear_host_callback) {
return std::make_unique<TestProfileManagementFlowController>(
this, std::move(clear_host_callback), step_, step_controller_factory_,
run_loop_.QuitClosure());
}
MockProfilePickerWebContentsHost::MockProfilePickerWebContentsHost() = default;
MockProfilePickerWebContentsHost::~MockProfilePickerWebContentsHost() = default;
// -- Other utils --------------------------------------------------------------
namespace profiles::testing {
void WaitForPickerWidgetCreated() {
if (!ProfilePicker::IsOpen()) {
base::RunLoop run_loop;
ProfilePicker::AddOnProfilePickerOpenedCallbackForTesting(
run_loop.QuitClosure());
run_loop.Run();
}
ViewAddedWaiter(ProfilePicker::GetViewForTesting()).Wait();
}
void WaitForPickerLoadStop(const GURL& expected_url) {
if (!ProfilePicker::IsOpen()) {
base::RunLoop run_loop;
ProfilePicker::AddOnProfilePickerOpenedCallbackForTesting(
run_loop.QuitClosure());
run_loop.Run();
}
auto* web_view = ProfilePicker::GetWebViewForTesting();
ASSERT_NE(web_view, nullptr);
PickerLoadStopWaiter(web_view, expected_url,
PickerLoadStopWaiter::Mode::kCheckUrlAtNextLoad)
.Wait();
}
void WaitForPickerUrl(const GURL& url) {
if (!ProfilePicker::IsOpen()) {
base::RunLoop run_loop;
ProfilePicker::AddOnProfilePickerOpenedCallbackForTesting(
run_loop.QuitClosure());
run_loop.Run();
}
auto* web_view = ProfilePicker::GetWebViewForTesting();
ASSERT_NE(web_view, nullptr);
PickerLoadStopWaiter(web_view, url,
PickerLoadStopWaiter::Mode::kWaitUntilUrlLoaded)
.Wait();
}
void WaitForPickerClosed() {
if (auto* view = ProfilePicker::GetViewForTesting()) {
ViewDeletedWaiter(view).Wait();
// The profile picker might still be open if for example it was scheduled to
// reopen on closure. But the view should not be the same anyway (it would
// just be null in most cases).
ASSERT_NE(view, ProfilePicker::GetViewForTesting());
} else {
ASSERT_FALSE(ProfilePicker::IsOpen());
}
}
ManagedUserProfileNoticeHandler* ExpectPickerNoticeScreenType(
ManagedUserProfileNoticeUI::ScreenType expected_type) {
content::WebContents* web_contents = GetPickerWebContents();
EXPECT_TRUE(web_contents);
ManagedUserProfileNoticeHandler* handler =
web_contents->GetWebUI()
->GetController()
->GetAs<ManagedUserProfileNoticeUI>()
->GetHandlerForTesting();
EXPECT_TRUE(handler);
EXPECT_EQ(handler->GetTypeForTesting(), expected_type);
return handler;
}
void ExpectPickerManagedUserNoticeScreenTypeAndProceed(
ManagedUserProfileNoticeUI::ScreenType expected_type,
signin::SigninChoice choice) {
ManagedUserProfileNoticeHandler* handler =
ExpectPickerNoticeScreenType(expected_type);
// Simulate clicking on the next button.
handler->CallProceedCallbackForTesting(choice);
}
} // namespace profiles::testing
|