Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / test_with_browser_view.cc
blob0867c920d390f68a54e2cb1dd19125d240a77903
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/frame/test_with_browser_view.h"
7 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
8 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h"
9 #include "chrome/browser/history/history_service_factory.h"
10 #include "chrome/browser/predictors/predictor_database.h"
11 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/browser/ui/views/frame/browser_view.h"
16 #include "chrome/browser/web_data_service_factory.h"
17 #include "chrome/test/base/browser_with_test_window_test.h"
18 #include "chrome/test/base/scoped_testing_local_state.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_io_thread_state.h"
21 #include "components/omnibox/browser/autocomplete_classifier.h"
22 #include "components/omnibox/browser/autocomplete_controller.h"
23 #include "components/omnibox/browser/test_scheme_classifier.h"
24 #include "components/search_engines/search_terms_data.h"
25 #include "components/search_engines/template_url_service.h"
26 #include "content/public/test/test_utils.h"
28 #if defined(OS_CHROMEOS)
29 #include "chrome/browser/chromeos/input_method/input_method_configuration.h"
30 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
31 #endif
33 namespace {
35 scoped_ptr<KeyedService> CreateTemplateURLService(
36 content::BrowserContext* context) {
37 Profile* profile = static_cast<Profile*>(context);
38 return make_scoped_ptr(new TemplateURLService(
39 profile->GetPrefs(),
40 scoped_ptr<SearchTermsData>(new UIThreadSearchTermsData(profile)),
41 WebDataServiceFactory::GetKeywordWebDataForProfile(
42 profile, ServiceAccessType::EXPLICIT_ACCESS),
43 scoped_ptr<TemplateURLServiceClient>(new ChromeTemplateURLServiceClient(
44 HistoryServiceFactory::GetForProfile(
45 profile, ServiceAccessType::EXPLICIT_ACCESS))),
46 nullptr, nullptr, base::Closure()));
49 scoped_ptr<KeyedService> CreateAutocompleteClassifier(
50 content::BrowserContext* context) {
51 Profile* profile = static_cast<Profile*>(context);
52 return make_scoped_ptr(new AutocompleteClassifier(
53 make_scoped_ptr(new AutocompleteController(
54 make_scoped_ptr(new ChromeAutocompleteProviderClient(profile)),
56 nullptr, AutocompleteClassifier::kDefaultOmniboxProviders)),
57 scoped_ptr<AutocompleteSchemeClassifier>(new TestSchemeClassifier())));
60 } // namespace
62 TestWithBrowserView::TestWithBrowserView() {
65 TestWithBrowserView::TestWithBrowserView(
66 Browser::Type browser_type,
67 chrome::HostDesktopType host_desktop_type,
68 bool hosted_app)
69 : BrowserWithTestWindowTest(browser_type,
70 host_desktop_type,
71 hosted_app) {
74 TestWithBrowserView::~TestWithBrowserView() {
77 void TestWithBrowserView::SetUp() {
78 local_state_.reset(
79 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
80 #if defined(OS_CHROMEOS)
81 chromeos::input_method::InitializeForTesting(
82 new chromeos::input_method::MockInputMethodManager);
83 #endif
84 testing_io_thread_state_.reset(new chrome::TestingIOThreadState());
85 BrowserWithTestWindowTest::SetUp();
86 predictor_db_.reset(new predictors::PredictorDatabase(GetProfile()));
87 browser_view_ = static_cast<BrowserView*>(browser()->window());
90 void TestWithBrowserView::TearDown() {
91 // Both BrowserView and BrowserWithTestWindowTest believe they have ownership
92 // of the Browser. Force BrowserWithTestWindowTest to give up ownership.
93 ASSERT_TRUE(release_browser());
95 // Clean up any tabs we opened, otherwise Browser crashes in destruction.
96 browser_view_->browser()->tab_strip_model()->CloseAllTabs();
97 // Ensure the Browser is reset before BrowserWithTestWindowTest cleans up
98 // the Profile.
99 browser_view_->GetWidget()->CloseNow();
100 browser_view_ = nullptr;
101 content::RunAllPendingInMessageLoop(content::BrowserThread::DB);
102 BrowserWithTestWindowTest::TearDown();
103 testing_io_thread_state_.reset();
104 predictor_db_.reset();
105 #if defined(OS_CHROMEOS)
106 chromeos::input_method::Shutdown();
107 #endif
108 local_state_.reset(nullptr);
111 TestingProfile* TestWithBrowserView::CreateProfile() {
112 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
113 // TemplateURLService is normally null during testing. Instant extended
114 // needs this service so set a custom factory function.
115 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
116 profile, &CreateTemplateURLService);
117 // TODO(jamescook): Eliminate this by introducing a mock toolbar or mock
118 // location bar.
119 AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
120 profile, &CreateAutocompleteClassifier);
121 return profile;
124 BrowserWindow* TestWithBrowserView::CreateBrowserWindow() {
125 // Allow BrowserWithTestWindowTest to use Browser to create the default
126 // BrowserView and BrowserFrame.
127 return nullptr;