test_with_browser_view.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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}
45
46void TestWithBrowserView::SetUp() {
47  local_state_.reset(
48      new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
49#if defined(OS_CHROMEOS)
50  chromeos::input_method::InitializeForTesting(
51      new chromeos::input_method::MockInputMethodManager);
52#endif
53  testing_io_thread_state_.reset(new chrome::TestingIOThreadState());
54  BrowserWithTestWindowTest::SetUp();
55  predictor_db_.reset(new predictors::PredictorDatabase(GetProfile()));
56  browser_view_ = static_cast<BrowserView*>(browser()->window());
57}
58
59void TestWithBrowserView::TearDown() {
60  // Both BrowserView and BrowserWithTestWindowTest believe they have ownership
61  // of the Browser. Force BrowserWithTestWindowTest to give up ownership.
62  ASSERT_TRUE(release_browser());
63
64  // Clean up any tabs we opened, otherwise Browser crashes in destruction.
65  browser_view_->browser()->tab_strip_model()->CloseAllTabs();
66  // Ensure the Browser is reset before BrowserWithTestWindowTest cleans up
67  // the Profile.
68  browser_view_->GetWidget()->CloseNow();
69  browser_view_ = NULL;
70  content::RunAllPendingInMessageLoop(content::BrowserThread::DB);
71  BrowserWithTestWindowTest::TearDown();
72  testing_io_thread_state_.reset();
73  predictor_db_.reset();
74#if defined(OS_CHROMEOS)
75  chromeos::input_method::Shutdown();
76#endif
77  local_state_.reset(NULL);
78}
79
80TestingProfile* TestWithBrowserView::CreateProfile() {
81  TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
82  // TemplateURLService is normally NULL during testing. Instant extended
83  // needs this service so set a custom factory function.
84  TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
85      profile, &CreateTemplateURLService);
86  // TODO(jamescook): Eliminate this by introducing a mock toolbar or mock
87  // location bar.
88  AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
89      profile, &CreateAutocompleteClassifier);
90  return profile;
91}
92
93BrowserWindow* TestWithBrowserView::CreateBrowserWindow() {
94  // Allow BrowserWithTestWindowTest to use Browser to create the default
95  // BrowserView and BrowserFrame.
96  return NULL;
97}
98