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