1// Copyright (c) 2012 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/search_engines/template_url_service_test_util.h"
6
7#include "base/message_loop/message_loop_proxy.h"
8#include "base/run_loop.h"
9#include "chrome/browser/search_engines/chrome_template_url_service_client.h"
10#include "chrome/test/base/testing_pref_service_syncable.h"
11#include "chrome/test/base/testing_profile.h"
12#include "components/search_engines/default_search_pref_test_util.h"
13#include "components/search_engines/keyword_table.h"
14#include "components/search_engines/keyword_web_data_service.h"
15#include "components/search_engines/template_url_service.h"
16#include "components/search_engines/testing_search_terms_data.h"
17#include "components/webdata/common/web_database_service.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20namespace {
21
22class TestingTemplateURLServiceClient : public ChromeTemplateURLServiceClient {
23 public:
24  TestingTemplateURLServiceClient(Profile* profile,
25                                  base::string16* search_term)
26      : ChromeTemplateURLServiceClient(profile),
27        search_term_(search_term) {}
28
29  virtual void SetKeywordSearchTermsForURL(
30      const GURL& url,
31      TemplateURLID id,
32      const base::string16& term) OVERRIDE {
33    *search_term_ = term;
34  }
35
36 private:
37  base::string16* search_term_;
38
39  DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLServiceClient);
40};
41
42}  // namespace
43
44TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
45    : changed_count_(0),
46      search_terms_data_(NULL) {
47  // Make unique temp directory.
48  EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
49  profile_.reset(new TestingProfile(temp_dir_.path()));
50
51  scoped_refptr<WebDatabaseService> web_database_service =
52      new WebDatabaseService(temp_dir_.path().AppendASCII("webdata"),
53                             base::MessageLoopProxy::current(),
54                             base::MessageLoopProxy::current());
55  web_database_service->AddTable(
56      scoped_ptr<WebDatabaseTable>(new KeywordTable()));
57  web_database_service->LoadDatabase();
58
59  web_data_service_ =  new KeywordWebDataService(
60      web_database_service.get(), base::MessageLoopProxy::current(),
61      KeywordWebDataService::ProfileErrorCallback());
62  web_data_service_->Init();
63
64  ResetModel(false);
65}
66
67TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
68  ClearModel();
69  profile_.reset();
70
71  // Flush the message loop to make application verifiers happy.
72  base::RunLoop().RunUntilIdle();
73}
74
75void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
76  changed_count_++;
77}
78
79int TemplateURLServiceTestUtil::GetObserverCount() {
80  return changed_count_;
81}
82
83void TemplateURLServiceTestUtil::ResetObserverCount() {
84  changed_count_ = 0;
85}
86
87void TemplateURLServiceTestUtil::VerifyLoad() {
88  ASSERT_FALSE(model()->loaded());
89  model()->Load();
90  base::RunLoop().RunUntilIdle();
91  EXPECT_EQ(1, GetObserverCount());
92  ResetObserverCount();
93}
94
95void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
96  model()->ChangeToLoadedState();
97  // Initialize the web data service so that the database gets updated with
98  // any changes made.
99
100  model()->web_data_service_ = web_data_service_;
101  base::RunLoop().RunUntilIdle();
102}
103
104void TemplateURLServiceTestUtil::ClearModel() {
105  model_->Shutdown();
106  model_.reset();
107  search_terms_data_ = NULL;
108}
109
110void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
111  if (model_)
112    ClearModel();
113  search_terms_data_ = new TestingSearchTermsData("http://www.google.com/");
114  model_.reset(new TemplateURLService(
115      profile()->GetPrefs(), scoped_ptr<SearchTermsData>(search_terms_data_),
116      web_data_service_.get(),
117      scoped_ptr<TemplateURLServiceClient>(
118          new TestingTemplateURLServiceClient(profile(), &search_term_)),
119      NULL, NULL, base::Closure()));
120  model()->AddObserver(this);
121  changed_count_ = 0;
122  if (verify_load)
123    VerifyLoad();
124}
125
126base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
127  base::string16 search_term;
128  search_term.swap(search_term_);
129  return search_term;
130}
131
132void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
133  DCHECK(base_url.is_valid());
134  search_terms_data_->set_google_base_url(base_url.spec());
135  model_->GoogleBaseURLChanged();
136}
137
138void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
139    bool enabled,
140    const std::string& name,
141    const std::string& keyword,
142    const std::string& search_url,
143    const std::string& suggest_url,
144    const std::string& icon_url,
145    const std::string& encodings,
146    const std::string& alternate_url,
147    const std::string& search_terms_replacement_key) {
148  DefaultSearchPrefTestUtil::SetManagedPref(
149      profile()->GetTestingPrefService(),
150      enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
151      alternate_url, search_terms_replacement_key);
152}
153
154void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
155  DefaultSearchPrefTestUtil::RemoveManagedPref(
156      profile()->GetTestingPrefService());
157}
158