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 BrowserContextKeyedService* CreateTemplateURLService(
29 content::BrowserContext* profile) {
30 return new TemplateURLService(static_cast<Profile*>(profile));
33 BrowserContextKeyedService* CreateAutocompleteClassifier(
34 content::BrowserContext* profile) {
35 return new AutocompleteClassifier(static_cast<Profile*>(profile));
40 TestWithBrowserView::TestWithBrowserView() {
43 TestWithBrowserView::TestWithBrowserView(
44 Browser::Type browser_type,
45 chrome::HostDesktopType host_desktop_type,
47 : BrowserWithTestWindowTest(browser_type,
52 TestWithBrowserView::~TestWithBrowserView() {
55 void TestWithBrowserView::SetUp() {
57 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
58 #if defined(OS_CHROMEOS)
59 chromeos::input_method::InitializeForTesting(
60 new chromeos::input_method::MockInputMethodManager);
62 testing_io_thread_state_.reset(new chrome::TestingIOThreadState());
63 BrowserWithTestWindowTest::SetUp();
64 predictor_db_.reset(new predictors::PredictorDatabase(GetProfile()));
65 browser_view_ = static_cast<BrowserView*>(browser()->window());
68 void TestWithBrowserView::TearDown() {
69 // Both BrowserView and BrowserWithTestWindowTest believe they have ownership
70 // of the Browser. Force BrowserWithTestWindowTest to give up ownership.
71 ASSERT_TRUE(release_browser());
73 // Clean up any tabs we opened, otherwise Browser crashes in destruction.
74 browser_view_->browser()->tab_strip_model()->CloseAllTabs();
75 // Ensure the Browser is reset before BrowserWithTestWindowTest cleans up
77 browser_view_->GetWidget()->CloseNow();
79 content::RunAllPendingInMessageLoop(content::BrowserThread::DB);
80 BrowserWithTestWindowTest::TearDown();
81 testing_io_thread_state_.reset();
82 predictor_db_.reset();
83 #if defined(OS_CHROMEOS)
84 chromeos::input_method::Shutdown();
86 local_state_.reset(NULL);
89 TestingProfile* TestWithBrowserView::CreateProfile() {
90 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
91 // TemplateURLService is normally NULL during testing. Instant extended
92 // needs this service so set a custom factory function.
93 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
94 profile, &CreateTemplateURLService);
95 // TODO(jamescook): Eliminate this by introducing a mock toolbar or mock
97 AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
98 profile, &CreateAutocompleteClassifier);
102 BrowserWindow* TestWithBrowserView::CreateBrowserWindow() {
103 // Allow BrowserWithTestWindowTest to use Browser to create the default
104 // BrowserView and BrowserFrame.