chromium/chrome/test/base/browser_with_test_window_test.cc

// Copyright 2012 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/test/base/browser_with_test_window_test.h"

#include <memory>
#include <vector>

#include "base/command_line.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile_destroyer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.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 "components/sync_preferences/pref_service_syncable.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h"
#include "ui/base/page_transition_types.h"

#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/ui/views/chrome_constrained_window_views_client.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "components/constrained_window/constrained_window_views.h"
#include "ui/views/test/views_test_utils.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "content/public/browser/context_factory.h"
#endif
#endif

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/crosapi/crosapi_manager.h"
#include "chrome/browser/ash/crosapi/idle_service_ash.h"
#include "chrome/browser/ash/crosapi/test_crosapi_dependency_registry.h"
#include "chrome/browser/browser_process.h"
#include "chromeos/ash/components/browser_context_helper/annotated_account_id.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_manager.h"
#endif

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chromeos/lacros/lacros_test_helper.h"
#endif

NavigationController;
RenderFrameHost;
RenderFrameHostTester;
WebContents;

BrowserWithTestWindowTest::~BrowserWithTestWindowTest() {}

void BrowserWithTestWindowTest::SetUp() {}

void BrowserWithTestWindowTest::TearDown() {}

void BrowserWithTestWindowTest::SetUpProfileManager(
    const base::FilePath& profiles_path,
    std::unique_ptr<ProfileManager> profile_manager) {}

gfx::NativeWindow BrowserWithTestWindowTest::GetContext() {}

void BrowserWithTestWindowTest::AddTab(Browser* browser, const GURL& url) {}

void BrowserWithTestWindowTest::CommitPendingLoad(
    NavigationController* controller) {}

void BrowserWithTestWindowTest::NavigateAndCommit(WebContents* web_contents,
                                                  const GURL& url) {}

void BrowserWithTestWindowTest::NavigateAndCommitActiveTab(const GURL& url) {}

void BrowserWithTestWindowTest::NavigateAndCommitActiveTabWithTitle(
    Browser* navigating_browser,
    const GURL& url,
    const std::u16string& title) {}

void BrowserWithTestWindowTest::FocusMainFrameOfActiveWebContents() {}

std::string BrowserWithTestWindowTest::GetDefaultProfileName() {}

TestingProfile* BrowserWithTestWindowTest::CreateProfile(
    const std::string& profile_name) {}

void BrowserWithTestWindowTest::DeleteProfile(const std::string& profile_name) {}

TestingProfile::TestingFactories
BrowserWithTestWindowTest::GetTestingFactories() {}

std::unique_ptr<BrowserWindow>
BrowserWithTestWindowTest::CreateBrowserWindow() {}

std::unique_ptr<Browser> BrowserWithTestWindowTest::CreateBrowser(
    Profile* profile,
    Browser::Type browser_type,
    bool hosted_app,
    BrowserWindow* browser_window) {}

#if BUILDFLAG(IS_CHROMEOS)
void BrowserWithTestWindowTest::LogIn(const std::string& email) {
#if BUILDFLAG(IS_CHROMEOS_ASH)
  const AccountId account_id = AccountId::FromUserEmail(email);
  user_manager_->AddUser(account_id);
  ash_test_helper()->test_session_controller_client()->AddUserSession(email);
  user_manager_->UserLoggedIn(
      account_id,
      user_manager::FakeUserManager::GetFakeUsernameHash(account_id),
      /*browser_restart=*/false,
      /*is_child=*/false);
#endif
}
#endif

#if BUILDFLAG(IS_CHROMEOS_ASH)
void BrowserWithTestWindowTest::OnUserProfileCreated(const std::string& email,
                                                     Profile* profile) {
  // TODO(b/40225390): Unset for_test explicit param after subclasses are
  // migrated.
  AccountId account_id = AccountId::FromUserEmail(email);
  ash::AnnotatedAccountId::Set(profile, account_id,
                               /*for_test=*/false);
  // Do not use the member directly, because another UserManager instance
  // may be injected.
  auto* user_manager = user_manager::UserManager::Get();
  user_manager->OnUserProfileCreated(account_id, profile->GetPrefs());
  auto observation =
      std::make_unique<base::ScopedObservation<Profile, ProfileObserver>>(this);
  observation->Observe(profile);
  profile_observations_.push_back(std::move(observation));
}

void BrowserWithTestWindowTest::SwitchActiveUser(const std::string& email) {
  ash_test_helper()->test_session_controller_client()->SwitchActiveUser(
      AccountId::FromUserEmail(email));
}

void BrowserWithTestWindowTest::OnProfileWillBeDestroyed(Profile* profile) {
  CHECK(
      std::erase_if(profile_observations_, [profile](const auto& observation) {
        return observation->IsObservingSource(profile);
      }));
  const AccountId* account_id = ash::AnnotatedAccountId::Get(profile);
  CHECK(account_id);
  // Do not use the member directly, because another UserManager instance
  // may be injected.
  user_manager::UserManager::Get()->OnUserProfileWillBeDestroyed(*account_id);
}

ash::ScopedCrosSettingsTestHelper*
BrowserWithTestWindowTest::GetCrosSettingsHelper() {
  return &cros_settings_test_helper_;
}

ash::StubInstallAttributes* BrowserWithTestWindowTest::GetInstallAttributes() {
  return GetCrosSettingsHelper()->InstallAttributes();
}
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

BrowserWithTestWindowTest::BrowserWithTestWindowTest(
    std::unique_ptr<content::BrowserTaskEnvironment> task_environment,
    Browser::Type browser_type,
    bool hosted_app)
    :{}