test_with_browser_view.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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
25namespace {
26
27// Caller owns the returned service.
28BrowserContextKeyedService* CreateTemplateURLService(
29    content::BrowserContext* profile) {
30  return new TemplateURLService(static_cast<Profile*>(profile));
31}
32
33BrowserContextKeyedService* CreateAutocompleteClassifier(
34    content::BrowserContext* profile) {
35  return new AutocompleteClassifier(static_cast<Profile*>(profile));
36}
37
38}  // namespace
39
40TestWithBrowserView::TestWithBrowserView() {
41}
42
43TestWithBrowserView::TestWithBrowserView(
44    Browser::Type browser_type,
45    chrome::HostDesktopType host_desktop_type,
46    bool hosted_app)
47    : BrowserWithTestWindowTest(browser_type,
48                                host_desktop_type,
49                                hosted_app) {
50}
51
52TestWithBrowserView::~TestWithBrowserView() {
53}
54
55void TestWithBrowserView::SetUp() {
56  local_state_.reset(
57      new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
58#if defined(OS_CHROMEOS)
59  chromeos::input_method::InitializeForTesting(
60      new chromeos::input_method::MockInputMethodManager);
61#endif
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());
66}
67
68void 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());
72
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
76  // the Profile.
77  browser_view_->GetWidget()->CloseNow();
78  browser_view_ = NULL;
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();
85#endif
86  local_state_.reset(NULL);
87}
88
89TestingProfile* 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
96  // location bar.
97  AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
98      profile, &CreateAutocompleteClassifier);
99  return profile;
100}
101
102BrowserWindow* TestWithBrowserView::CreateBrowserWindow() {
103  // Allow BrowserWithTestWindowTest to use Browser to create the default
104  // BrowserView and BrowserFrame.
105  return NULL;
106}
107