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. 4 5 #include "chrome/browser/ui/views/frame/test_with_browser_view.h" 6 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" 19 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" 23 #endif 24 25 namespace { 26 27 // Caller owns the returned service. 28 KeyedService* CreateTemplateURLService(content::BrowserContext* profile) { 29 return new TemplateURLService(static_cast<Profile*>(profile)); 30 } 31 32 KeyedService* CreateAutocompleteClassifier(content::BrowserContext* profile) { 33 return new AutocompleteClassifier(static_cast<Profile*>(profile)); 34 } 35 36 } // namespace 37 38 TestWithBrowserView::TestWithBrowserView() { 39 } 40 41 TestWithBrowserView::TestWithBrowserView( 42 Browser::Type browser_type, 43 chrome::HostDesktopType host_desktop_type, 44 bool hosted_app) 45 : BrowserWithTestWindowTest(browser_type, 46 host_desktop_type, 47 hosted_app) { 48 } 49 50 TestWithBrowserView::~TestWithBrowserView() { 51 } 52 53 void TestWithBrowserView::SetUp() { 54 local_state_.reset( 55 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal())); 56 #if defined(OS_CHROMEOS) 57 chromeos::input_method::InitializeForTesting( 58 new chromeos::input_method::MockInputMethodManager); 59 #endif 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()); 64 } 65 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()); 70 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 74 // the Profile. 75 browser_view_->GetWidget()->CloseNow(); 76 browser_view_ = NULL; 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(); 83 #endif 84 local_state_.reset(NULL); 85 } 86 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 94 // location bar. 95 AutocompleteClassifierFactory::GetInstance()->SetTestingFactory( 96 profile, &CreateAutocompleteClassifier); 97 return profile; 98 } 99 100 BrowserWindow* TestWithBrowserView::CreateBrowserWindow() { 101 // Allow BrowserWithTestWindowTest to use Browser to create the default 102 // BrowserView and BrowserFrame. 103 return NULL; 104 } 105