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 // Caller owns the returned service.
35 KeyedService
* CreateTemplateURLService(content::BrowserContext
* context
) {
36 Profile
* profile
= static_cast<Profile
*>(context
);
37 return 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 KeyedService
* CreateAutocompleteClassifier(content::BrowserContext
* context
) {
49 Profile
* profile
= static_cast<Profile
*>(context
);
50 return new AutocompleteClassifier(
51 make_scoped_ptr(new AutocompleteController(
52 profile
, TemplateURLServiceFactory::GetForProfile(profile
), nullptr,
53 AutocompleteClassifier::kDefaultOmniboxProviders
)),
54 scoped_ptr
<AutocompleteSchemeClassifier
>(new TestSchemeClassifier()));
59 TestWithBrowserView::TestWithBrowserView() {
62 TestWithBrowserView::TestWithBrowserView(
63 Browser::Type browser_type
,
64 chrome::HostDesktopType host_desktop_type
,
66 : BrowserWithTestWindowTest(browser_type
,
71 TestWithBrowserView::~TestWithBrowserView() {
74 void TestWithBrowserView::SetUp() {
76 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
77 #if defined(OS_CHROMEOS)
78 chromeos::input_method::InitializeForTesting(
79 new chromeos::input_method::MockInputMethodManager
);
81 testing_io_thread_state_
.reset(new chrome::TestingIOThreadState());
82 BrowserWithTestWindowTest::SetUp();
83 predictor_db_
.reset(new predictors::PredictorDatabase(GetProfile()));
84 browser_view_
= static_cast<BrowserView
*>(browser()->window());
87 void TestWithBrowserView::TearDown() {
88 // Both BrowserView and BrowserWithTestWindowTest believe they have ownership
89 // of the Browser. Force BrowserWithTestWindowTest to give up ownership.
90 ASSERT_TRUE(release_browser());
92 // Clean up any tabs we opened, otherwise Browser crashes in destruction.
93 browser_view_
->browser()->tab_strip_model()->CloseAllTabs();
94 // Ensure the Browser is reset before BrowserWithTestWindowTest cleans up
96 browser_view_
->GetWidget()->CloseNow();
97 browser_view_
= nullptr;
98 content::RunAllPendingInMessageLoop(content::BrowserThread::DB
);
99 BrowserWithTestWindowTest::TearDown();
100 testing_io_thread_state_
.reset();
101 predictor_db_
.reset();
102 #if defined(OS_CHROMEOS)
103 chromeos::input_method::Shutdown();
105 local_state_
.reset(nullptr);
108 TestingProfile
* TestWithBrowserView::CreateProfile() {
109 TestingProfile
* profile
= BrowserWithTestWindowTest::CreateProfile();
110 // TemplateURLService is normally null during testing. Instant extended
111 // needs this service so set a custom factory function.
112 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
113 profile
, &CreateTemplateURLService
);
114 // TODO(jamescook): Eliminate this by introducing a mock toolbar or mock
116 AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
117 profile
, &CreateAutocompleteClassifier
);
121 BrowserWindow
* TestWithBrowserView::CreateBrowserWindow() {
122 // Allow BrowserWithTestWindowTest to use Browser to create the default
123 // BrowserView and BrowserFrame.