1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/search_engines/template_url_service_test_util.h"
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/threading/thread.h"
#include "chrome/browser/search_engines/chrome_template_url_service_client.h"
#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
#include "chrome/browser/webdata/web_data_service_factory.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "components/google/core/browser/google_url_tracker.h"
#include "components/search_engines/default_search_manager.h"
#include "components/search_engines/default_search_pref_test_util.h"
#include "components/search_engines/template_url_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/google/google_brand_chromeos.h"
#endif
// Trivial subclass of TemplateURLService that records the last invocation of
// SetKeywordSearchTermsForURL.
class TestingTemplateURLService : public TemplateURLService {
public:
explicit TestingTemplateURLService(Profile* profile)
: TemplateURLService(
profile->GetPrefs(),
scoped_ptr<SearchTermsData>(new UIThreadSearchTermsData(profile)),
WebDataServiceFactory::GetKeywordWebDataForProfile(
profile, Profile::EXPLICIT_ACCESS),
scoped_ptr<TemplateURLServiceClient>(
new ChromeTemplateURLServiceClient(profile)), NULL, NULL,
base::Closure()) {
}
base::string16 GetAndClearSearchTerm() {
base::string16 search_term;
search_term.swap(search_term_);
return search_term;
}
protected:
virtual void SetKeywordSearchTermsForURL(
const TemplateURL* t_url,
const GURL& url,
const base::string16& term) OVERRIDE {
search_term_ = term;
}
private:
base::string16 search_term_;
DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
};
TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
changed_count_(0) {
// Make unique temp directory.
EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
profile_.reset(new TestingProfile(temp_dir_.path()));
profile()->CreateWebDataService();
model_.reset(new TestingTemplateURLService(profile()));
model_->AddObserver(this);
#if defined(OS_CHROMEOS)
google_brand::chromeos::ClearBrandForCurrentSession();
#endif
}
TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
ClearModel();
profile_.reset();
UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
// Flush the message loop to make application verifiers happy.
base::RunLoop().RunUntilIdle();
}
void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
changed_count_++;
}
int TemplateURLServiceTestUtil::GetObserverCount() {
return changed_count_;
}
void TemplateURLServiceTestUtil::ResetObserverCount() {
changed_count_ = 0;
}
void TemplateURLServiceTestUtil::VerifyLoad() {
ASSERT_FALSE(model()->loaded());
model()->Load();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, GetObserverCount());
ResetObserverCount();
}
void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
model()->ChangeToLoadedState();
// Initialize the web data service so that the database gets updated with
// any changes made.
model()->web_data_service_ =
WebDataServiceFactory::GetKeywordWebDataForProfile(
profile(), Profile::EXPLICIT_ACCESS);
base::RunLoop().RunUntilIdle();
}
void TemplateURLServiceTestUtil::ClearModel() {
model_->Shutdown();
model_.reset();
}
void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
if (model_)
ClearModel();
model_.reset(new TestingTemplateURLService(profile()));
model()->AddObserver(this);
changed_count_ = 0;
if (verify_load)
VerifyLoad();
}
base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
return model_->GetAndClearSearchTerm();
}
void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
DCHECK(base_url.is_valid());
UIThreadSearchTermsData data(profile());
UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec());
model_->GoogleBaseURLChanged();
}
void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
bool enabled,
const std::string& name,
const std::string& keyword,
const std::string& search_url,
const std::string& suggest_url,
const std::string& icon_url,
const std::string& encodings,
const std::string& alternate_url,
const std::string& search_terms_replacement_key) {
DefaultSearchPrefTestUtil::SetManagedPref(
profile()->GetTestingPrefService(),
enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
alternate_url, search_terms_replacement_key);
}
void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
DefaultSearchPrefTestUtil::RemoveManagedPref(
profile()->GetTestingPrefService());
}
TemplateURLService* TemplateURLServiceTestUtil::model() {
return model_.get();
}
|