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/predictors/predictor_database.h"
10 #include "chrome/browser/search_engines/template_url_service.h"
11 #include "chrome/browser/search_engines/template_url_service_factory.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/test/base/browser_with_test_window_test.h"
15 #include "chrome/test/base/scoped_testing_local_state.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_io_thread_state.h"
18 #include "content/public/test/test_utils.h"
20 #if defined(OS_CHROMEOS)
21 #include "chrome/browser/chromeos/input_method/input_method_configuration.h"
22 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
27 // Caller owns the returned service.
28 KeyedService
* CreateTemplateURLService(content::BrowserContext
* profile
) {
29 return new TemplateURLService(static_cast<Profile
*>(profile
));
32 KeyedService
* CreateAutocompleteClassifier(content::BrowserContext
* profile
) {
33 return new AutocompleteClassifier(static_cast<Profile
*>(profile
));
38 TestWithBrowserView::TestWithBrowserView() {
41 TestWithBrowserView::TestWithBrowserView(
42 Browser::Type browser_type
,
43 chrome::HostDesktopType host_desktop_type
,
45 : BrowserWithTestWindowTest(browser_type
,
50 TestWithBrowserView::~TestWithBrowserView() {
53 void TestWithBrowserView::SetUp() {
55 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
56 #if defined(OS_CHROMEOS)
57 chromeos::input_method::InitializeForTesting(
58 new chromeos::input_method::MockInputMethodManager
);
60 testing_io_thread_state_
.reset(new chrome::TestingIOThreadState());
61 BrowserWithTestWindowTest::SetUp();
62 predictor_db_
.reset(new predictors::PredictorDatabase(GetProfile()));
63 browser_view_
= static_cast<BrowserView
*>(browser()->window());
66 void TestWithBrowserView::TearDown() {
67 // Both BrowserView and BrowserWithTestWindowTest believe they have ownership
68 // of the Browser. Force BrowserWithTestWindowTest to give up ownership.
69 ASSERT_TRUE(release_browser());
71 // Clean up any tabs we opened, otherwise Browser crashes in destruction.
72 browser_view_
->browser()->tab_strip_model()->CloseAllTabs();
73 // Ensure the Browser is reset before BrowserWithTestWindowTest cleans up
75 browser_view_
->GetWidget()->CloseNow();
77 content::RunAllPendingInMessageLoop(content::BrowserThread::DB
);
78 BrowserWithTestWindowTest::TearDown();
79 testing_io_thread_state_
.reset();
80 predictor_db_
.reset();
81 #if defined(OS_CHROMEOS)
82 chromeos::input_method::Shutdown();
84 local_state_
.reset(NULL
);
87 TestingProfile
* TestWithBrowserView::CreateProfile() {
88 TestingProfile
* profile
= BrowserWithTestWindowTest::CreateProfile();
89 // TemplateURLService is normally NULL during testing. Instant extended
90 // needs this service so set a custom factory function.
91 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
92 profile
, &CreateTemplateURLService
);
93 // TODO(jamescook): Eliminate this by introducing a mock toolbar or mock
95 AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
96 profile
, &CreateAutocompleteClassifier
);
100 BrowserWindow
* TestWithBrowserView::CreateBrowserWindow() {
101 // Allow BrowserWithTestWindowTest to use Browser to create the default
102 // BrowserView and BrowserFrame.