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
|
// Copyright 2013 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/frame/test_with_browser_view.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "build/build_config.h"
#include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
#include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/extensions/load_error_reporter.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/search_engines/template_url_service_test_util.h"
#include "chrome/browser/signin/chrome_signin_client_factory.h"
#include "chrome/browser/signin/chrome_signin_client_test_util.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/omnibox/browser/autocomplete_classifier.h"
#include "components/omnibox/browser/autocomplete_controller.h"
#include "components/omnibox/browser/test_scheme_classifier.h"
#include "components/signin/public/base/list_accounts_test_utils.h"
#include "content/public/test/test_utils.h"
#include "services/network/test/test_url_loader_factory.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/input_method/input_method_configuration.h"
#include "ui/base/ime/ash/mock_input_method_manager_impl.h"
#endif
namespace {
std::unique_ptr<KeyedService> CreateAutocompleteClassifier(
content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
return std::make_unique<AutocompleteClassifier>(
std::make_unique<AutocompleteController>(
std::make_unique<ChromeAutocompleteProviderClient>(profile),
AutocompleteClassifier::DefaultOmniboxProviders()),
std::make_unique<TestSchemeClassifier>());
}
} // namespace
TestWithBrowserView::~TestWithBrowserView() = default;
void TestWithBrowserView::SetUp() {
#if BUILDFLAG(IS_CHROMEOS)
ash::input_method::InitializeForTesting(
new ash::input_method::MockInputMethodManagerImpl);
#endif
BrowserWithTestWindowTest::SetUp();
browser_view_ = static_cast<BrowserView*>(browser()->window());
}
void TestWithBrowserView::TearDown() {
// Destroy Browsers directly managed by TestWithBrowserView.
for (std::unique_ptr<Browser>& browser : additional_browsers_) {
// For Browsers created with a corresponding BrowserView, the Browser is
// ultimately deleted by its owning NativeWidget. To avoid a double-free
// situation we must release the Browser's unique_ptr and request the
// NativeWidget close via its BrowserView.
// TODO(crbug.com/413168662): Eliminate this once Browser ownership changes
// have landed.
BrowserView* browser_view = static_cast<BrowserView*>(browser->window());
browser.release()->tab_strip_model()->CloseAllTabs();
browser_view->GetWidget()->CloseNow();
}
// Because CreateBrowserWindow() is overridden to return null, a real
// BrowserView is created, and BrowserView has a unique_ptr that owns the
// Browser for which it is the view. This is a problem because
// BrowserWithTestWindowTest also has a unique_ptr to the Browser. Therefore,
// steal the BrowserWithTestWindowTest ownership and release it to fix the
// double-ownership problem.
ASSERT_TRUE(release_browser().release());
// Then trigger the close of the browser window via the view. It's critical
// that the Browser is gone before BrowserWithTestWindowTest::TearDown() is
// called so that the dependencies aren't closed out from underneath the
// browser.
browser_view_->browser()->tab_strip_model()->CloseAllTabs();
browser_view_.ExtractAsDangling()->GetWidget()->CloseNow();
content::RunAllTasksUntilIdle();
BrowserWithTestWindowTest::TearDown();
#if BUILDFLAG(IS_CHROMEOS)
ash::input_method::Shutdown();
#endif
}
TestingProfile* TestWithBrowserView::CreateProfile(
const std::string& profile_name) {
TestingProfile* profile =
BrowserWithTestWindowTest::CreateProfile(profile_name);
// TemplateURLService is normally null during testing. Instant extended
// needs this service so set a custom factory function.
TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
profile,
TemplateURLServiceTestUtil::GetTemplateURLServiceTestingFactory());
// TODO(jamescook): Eliminate this by introducing a mock toolbar or mock
// location bar.
AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
profile, base::BindRepeating(&CreateAutocompleteClassifier));
// ToolbarActionsModel must exist before the toolbar initializes the
// extensions area.
extensions::LoadErrorReporter::Init(/*enable_noisy_errors=*/false);
extensions::extension_action_test_util::CreateToolbarModelForProfile(profile);
// Configure the GaiaCookieManagerService to return no accounts.
signin::SetListAccountsResponseHttpNotFound(test_url_loader_factory());
return profile;
}
std::unique_ptr<BrowserWindow> TestWithBrowserView::CreateBrowserWindow() {
// Allow BrowserWithTestWindowTest to use Browser to create the default
// BrowserView and BrowserFrame.
return nullptr;
}
TestingProfile::TestingFactories TestWithBrowserView::GetTestingFactories() {
return {TestingProfile::TestingFactory{
ChromeSigninClientFactory::GetInstance(),
base::BindRepeating(&BuildChromeSigninClientWithURLLoader,
test_url_loader_factory())}};
}
Browser* TestWithBrowserView::CreateBrowserWithBrowserView(
Profile* profile,
Browser::Type browser_type) {
additional_browsers_.emplace_back(CreateBrowser(
profile, browser_type, /*hosted_app=*/false, /*browser_window=*/nullptr));
return additional_browsers_.back().get();
}
|