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.h"
8 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
9 #include "chrome/browser/autocomplete/autocomplete_controller.h"
10 #include "chrome/browser/history/history_service_factory.h"
11 #include "chrome/browser/predictors/predictor_database.h"
12 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/browser/ui/views/frame/browser_view.h"
17 #include "chrome/browser/webdata/web_data_service_factory.h"
18 #include "chrome/test/base/browser_with_test_window_test.h"
19 #include "chrome/test/base/scoped_testing_local_state.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_io_thread_state.h"
22 #include "components/omnibox/test_scheme_classifier.h"
23 #include "components/search_engines/search_terms_data.h"
24 #include "components/search_engines/template_url_service.h"
25 #include "content/public/test/test_utils.h"
27 #if defined(OS_CHROMEOS)
28 #include "chrome/browser/chromeos/input_method/input_method_configuration.h"
29 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
34 scoped_ptr
<KeyedService
> CreateTemplateURLService(
35 content::BrowserContext
* context
) {
36 Profile
* profile
= static_cast<Profile
*>(context
);
37 return make_scoped_ptr(new TemplateURLService(
39 scoped_ptr
<SearchTermsData
>(new UIThreadSearchTermsData(profile
)),
40 WebDataServiceFactory::GetKeywordWebDataForProfile(
41 profile
, ServiceAccessType::EXPLICIT_ACCESS
),
42 scoped_ptr
<TemplateURLServiceClient
>(new ChromeTemplateURLServiceClient(
43 HistoryServiceFactory::GetForProfile(
44 profile
, ServiceAccessType::EXPLICIT_ACCESS
))),
45 nullptr, nullptr, base::Closure()));
48 scoped_ptr
<KeyedService
> CreateAutocompleteClassifier(
49 content::BrowserContext
* context
) {
50 Profile
* profile
= static_cast<Profile
*>(context
);
51 return make_scoped_ptr(new AutocompleteClassifier(
52 make_scoped_ptr(new AutocompleteController(
53 profile
, TemplateURLServiceFactory::GetForProfile(profile
), nullptr,
54 AutocompleteClassifier::kDefaultOmniboxProviders
)),
55 scoped_ptr
<AutocompleteSchemeClassifier
>(new TestSchemeClassifier())));
60 TestWithBrowserView::TestWithBrowserView() {
63 TestWithBrowserView::TestWithBrowserView(
64 Browser::Type browser_type
,
65 chrome::HostDesktopType host_desktop_type
,
67 : BrowserWithTestWindowTest(browser_type
,
72 TestWithBrowserView::~TestWithBrowserView() {
75 void TestWithBrowserView::SetUp() {
77 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
78 #if defined(OS_CHROMEOS)
79 chromeos::input_method::InitializeForTesting(
80 new chromeos::input_method::MockInputMethodManager
);
82 testing_io_thread_state_
.reset(new chrome::TestingIOThreadState());
83 BrowserWithTestWindowTest::SetUp();
84 predictor_db_
.reset(new predictors::PredictorDatabase(GetProfile()));
85 browser_view_
= static_cast<BrowserView
*>(browser()->window());
88 void TestWithBrowserView::TearDown() {
89 // Both BrowserView and BrowserWithTestWindowTest believe they have ownership
90 // of the Browser. Force BrowserWithTestWindowTest to give up ownership.
91 ASSERT_TRUE(release_browser());
93 // Clean up any tabs we opened, otherwise Browser crashes in destruction.
94 browser_view_
->browser()->tab_strip_model()->CloseAllTabs();
95 // Ensure the Browser is reset before BrowserWithTestWindowTest cleans up
97 browser_view_
->GetWidget()->CloseNow();
98 browser_view_
= nullptr;
99 content::RunAllPendingInMessageLoop(content::BrowserThread::DB
);
100 BrowserWithTestWindowTest::TearDown();
101 testing_io_thread_state_
.reset();
102 predictor_db_
.reset();
103 #if defined(OS_CHROMEOS)
104 chromeos::input_method::Shutdown();
106 local_state_
.reset(nullptr);
109 TestingProfile
* TestWithBrowserView::CreateProfile() {
110 TestingProfile
* profile
= BrowserWithTestWindowTest::CreateProfile();
111 // TemplateURLService is normally null during testing. Instant extended
112 // needs this service so set a custom factory function.
113 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
114 profile
, &CreateTemplateURLService
);
115 // TODO(jamescook): Eliminate this by introducing a mock toolbar or mock
117 AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
118 profile
, &CreateAutocompleteClassifier
);
122 BrowserWindow
* TestWithBrowserView::CreateBrowserWindow() {
123 // Allow BrowserWithTestWindowTest to use Browser to create the default
124 // BrowserView and BrowserFrame.