chromium/chrome/browser/ui/views/profiles/profile_picker_view_test_utils.cc

// 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/profiles/profile_ui_test_utils.h"

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/location.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/views/profiles/profile_management_step_controller.h"
#include "chrome/browser/ui/views/profiles/profile_picker_view.h"
#include "chrome/browser/ui/views/profiles/profile_picker_view_test_utils.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_CHROMEOS_LACROS)
#include "chrome/browser/browser_process.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
#include "chrome/browser/ui/webui/signin/signin_url_utils.h"
#include "chrome/browser/ui/webui/signin/sync_confirmation_ui.h"
#endif

#if BUILDFLAG(IS_ANDROID)
#error This file should only be included on desktop.
#endif

namespace {

content::WebContents* GetPickerWebContents() {}

class TestProfileManagementFlowController
    : public ProfileManagementFlowController,
      public content::WebContentsObserver {};

}  // namespace

// -- ViewAddedWaiter ----------------------------------------------------------

ViewAddedWaiter::ViewAddedWaiter(views::View* view) :{}
ViewAddedWaiter::~ViewAddedWaiter() = default;

void ViewAddedWaiter::Wait() {}

void ViewAddedWaiter::OnViewAddedToWidget(views::View* observed_view) {}

// -- ViewDeletedWaiter --------------------------------------------------------

ViewDeletedWaiter::ViewDeletedWaiter(views::View* view) {}

ViewDeletedWaiter::~ViewDeletedWaiter() = default;

void ViewDeletedWaiter::Wait() {}

void ViewDeletedWaiter::OnViewIsDeleting(views::View* observed_view) {}

// -- PickerLoadStopWaiter -----------------------------------------------------

PickerLoadStopWaiter::PickerLoadStopWaiter(views::WebView* web_view,
                                           const GURL& expected_url,
                                           Mode wait_mode)
    :{}

void PickerLoadStopWaiter::DidStopLoading() {}

void PickerLoadStopWaiter::Wait() {}

void PickerLoadStopWaiter::OnWebContentsAttached(views::WebView* web_view) {}

bool PickerLoadStopWaiter::ShouldKeepWaiting() const {}

// -- ProfileManagementStepTestView --------------------------------------------

ProfileManagementStepTestView::ProfileManagementStepTestView(
    ProfilePicker::Params&& params,
    ProfileManagementFlowController::Step step,
    StepControllerFactory step_controller_factory)
    :{}

ProfileManagementStepTestView::~ProfileManagementStepTestView() = default;

void ProfileManagementStepTestView::ShowAndWait(
    std::optional<gfx::Size> view_size) {}

std::unique_ptr<ProfileManagementFlowController>
ProfileManagementStepTestView::CreateFlowController(
    Profile* picker_profile,
    ClearHostClosure clear_host_callback) {}

// -- Other utils --------------------------------------------------------------
namespace profiles::testing {

void WaitForPickerWidgetCreated() {}

void WaitForPickerLoadStop(const GURL& expected_url) {}

void WaitForPickerUrl(const GURL& url) {}

void WaitForPickerClosed() {}

ManagedUserProfileNoticeHandler* ExpectPickerNoticeScreenType(
    ManagedUserProfileNoticeUI::ScreenType expected_type) {}

void ExpectPickerManagedUserNoticeScreenTypeAndProceed(
    ManagedUserProfileNoticeUI::ScreenType expected_type,
    signin::SigninChoice choice) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS)
void CompleteLacrosFirstRun(
    LoginUIService::SyncConfirmationUIClosedResult result) {
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  Profile& profile = profiles::testing::CreateProfileSync(
      profile_manager, profile_manager->GetPrimaryUserProfilePath());

  WaitForPickerWidgetCreated();
  WaitForPickerLoadStop(GURL(chrome::kChromeUIIntroURL));

  ASSERT_TRUE(ProfilePicker::IsFirstRunOpen());
  EXPECT_EQ(0u, BrowserList::GetInstance()->size());

  base::Value::List args;
  GetPickerWebContents()->GetWebUI()->ProcessWebUIMessage(
      GetPickerWebContents()->GetURL(), "continueWithAccount", std::move(args));

  WaitForPickerLoadStop(AppendSyncConfirmationQueryParams(
      GURL("chrome://sync-confirmation/"), SyncConfirmationStyle::kWindow,
      /*is_sync_promo=*/true));

  if (result == LoginUIService::UI_CLOSED) {
    // `UI_CLOSED` is not provided via webui handlers. Instead, it gets sent
    // when the profile picker gets closed by some external source. If we only
    // send the result notification like for other types, the view will stay
    // open.
    ProfilePicker::Hide();
  } else {
    LoginUIServiceFactory::GetForProfile(&profile)->SyncConfirmationUIClosed(
        result);
  }
}
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)

}  // namespace profiles::testing