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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
// Copyright (c) 2011 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 <string>
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop.h"
#include "base/task.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/search_engines/search_provider_install_data.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_test_util.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/testing_pref_service.h"
#include "chrome/test/testing_profile.h"
#include "content/browser/browser_thread.h"
#include "content/common/notification_service.h"
#include "content/common/notification_source.h"
#include "testing/gtest/include/gtest/gtest.h"
// Create a TemplateURL. The caller owns the returned TemplateURL*.
static TemplateURL* CreateTemplateURL(const std::string& url,
const std::string& keyword) {
TemplateURL* t_url = new TemplateURL();
t_url->SetURL(url, 0, 0);
t_url->set_keyword(UTF8ToUTF16(keyword));
t_url->set_short_name(UTF8ToUTF16(keyword));
return t_url;
}
// Test the SearchProviderInstallData::GetInstallState.
class TestGetInstallState :
public base::RefCountedThreadSafe<TestGetInstallState> {
public:
explicit TestGetInstallState(SearchProviderInstallData* install_data)
: install_data_(install_data),
main_loop_(NULL),
passed_(false) {
}
void set_search_provider_host(
const std::string& search_provider_host) {
search_provider_host_ = search_provider_host;
}
void set_default_search_provider_host(
const std::string& default_search_provider_host) {
default_search_provider_host_ = default_search_provider_host;
}
// Runs the test. Returns true if all passed. False if any failed.
bool RunTests();
private:
friend class base::RefCountedThreadSafe<TestGetInstallState>;
~TestGetInstallState();
// Starts the test run on the IO thread.
void StartTestOnIOThread();
// Callback for when SearchProviderInstallData is ready to have
// GetInstallState called. Runs all of the test cases.
void DoInstallStateTests();
// Does a verification for one url and its expected state.
void VerifyInstallState(SearchProviderInstallData::State expected_state,
const std::string& url);
SearchProviderInstallData* install_data_;
MessageLoop* main_loop_;
// A host which should be a search provider but not the default.
std::string search_provider_host_;
// A host which should be a search provider but not the default.
std::string default_search_provider_host_;
// Used to indicate if DoInstallStateTests passed all test.
bool passed_;
DISALLOW_COPY_AND_ASSIGN(TestGetInstallState);
};
bool TestGetInstallState::RunTests() {
passed_ = true;
main_loop_ = MessageLoop::current();
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)->PostTask(
FROM_HERE,
NewRunnableMethod(this, &TestGetInstallState::StartTestOnIOThread));
// Run the current message loop. When the test is finished on the I/O thread,
// it invokes Quit, which unblocks this.
MessageLoop::current()->Run();
main_loop_ = NULL;
// Let the testing code know what the result is.
return passed_;
}
TestGetInstallState::~TestGetInstallState() {
}
void TestGetInstallState::StartTestOnIOThread() {
install_data_->CallWhenLoaded(
NewRunnableMethod(this,
&TestGetInstallState::DoInstallStateTests));
}
void TestGetInstallState::DoInstallStateTests() {
// Installed but not default.
VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT,
"http://" + search_provider_host_ + "/");
VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT,
"http://" + search_provider_host_ + ":80/");
// Not installed.
VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED,
"http://" + search_provider_host_ + ":96/");
// Not installed due to different scheme.
VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED,
"https://" + search_provider_host_ + "/");
// Not installed.
VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED,
"http://a" + search_provider_host_ + "/");
// Installed as default.
if (!default_search_provider_host_.empty()) {
VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT,
"http://" + default_search_provider_host_ + "/");
}
// All done.
main_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
}
void TestGetInstallState::VerifyInstallState(
SearchProviderInstallData::State expected_state,
const std::string& url) {
SearchProviderInstallData::State actual_state =
install_data_->GetInstallState(GURL(url));
if (expected_state == actual_state)
return;
passed_ = false;
LOG(ERROR) << "GetInstallState for " << url << " failed. Expected " <<
expected_state << ". Actual " << actual_state << ".";
}
// Provides basic test set-up/tear-down functionality needed by all tests
// that use TemplateURLServiceTestUtil.
class SearchProviderInstallDataTest : public testing::Test {
public:
SearchProviderInstallDataTest()
: install_data_(NULL) {}
virtual void SetUp() {
testing::Test::SetUp();
util_.SetUp();
util_.StartIOThread();
install_data_ = new SearchProviderInstallData(
util_.GetWebDataService(),
content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
Source<SearchProviderInstallDataTest>(this));
}
virtual void TearDown() {
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)->PostTask(
FROM_HERE,
new DeleteTask<SearchProviderInstallData>(install_data_));
install_data_ = NULL;
// Make sure that the install data class on the UI thread gets cleaned up.
// It doesn't matter that this happens after install_data_ is deleted.
NotificationService::current()->Notify(
content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
Source<SearchProviderInstallDataTest>(this),
NotificationService::NoDetails());
util_.TearDown();
testing::Test::TearDown();
}
void SimulateDefaultSearchIsManaged(const std::string& url) {
ASSERT_FALSE(url.empty());
TestingPrefService* service = util_.profile()->GetTestingPrefService();
service->SetManagedPref(
prefs::kDefaultSearchProviderEnabled,
Value::CreateBooleanValue(true));
service->SetManagedPref(
prefs::kDefaultSearchProviderSearchURL,
Value::CreateStringValue(url));
service->SetManagedPref(
prefs::kDefaultSearchProviderName,
Value::CreateStringValue("managed"));
// Clear the IDs that are not specified via policy.
service->SetManagedPref(
prefs::kDefaultSearchProviderID, new StringValue(""));
service->SetManagedPref(
prefs::kDefaultSearchProviderPrepopulateID, new StringValue(""));
util_.model()->Observe(
chrome::NOTIFICATION_PREF_CHANGED,
Source<PrefService>(util_.profile()->GetTestingPrefService()),
Details<std::string>(NULL));
}
protected:
TemplateURLServiceTestUtil util_;
// Provides the search provider install state on the I/O thread. It must be
// deleted on the I/O thread, which is why it isn't a scoped_ptr.
SearchProviderInstallData* install_data_;
DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallDataTest);
};
TEST_F(SearchProviderInstallDataTest, GetInstallState) {
// Set up the database.
util_.ChangeModelToLoadState();
std::string host = "www.unittest.com";
TemplateURL* t_url = CreateTemplateURL("http://" + host + "/path",
"unittest");
util_.model()->Add(t_url);
// Wait for the changes to be saved.
TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests();
// Verify the search providers install state (with no default set).
scoped_refptr<TestGetInstallState> test_get_install_state(
new TestGetInstallState(install_data_));
test_get_install_state->set_search_provider_host(host);
EXPECT_TRUE(test_get_install_state->RunTests());
// Set-up a default and try it all one more time.
std::string default_host = "www.mmm.com";
TemplateURL* default_url = CreateTemplateURL("http://" + default_host + "/",
"mmm");
util_.model()->Add(default_url);
util_.model()->SetDefaultSearchProvider(default_url);
test_get_install_state->set_default_search_provider_host(default_host);
EXPECT_TRUE(test_get_install_state->RunTests());
}
TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) {
// Set up the database.
util_.ChangeModelToLoadState();
std::string host = "www.unittest.com";
TemplateURL* t_url = CreateTemplateURL("http://" + host + "/path",
"unittest");
util_.model()->Add(t_url);
// Set a managed preference that establishes a default search provider.
std::string host2 = "www.managedtest.com";
std::string url2 = "http://" + host2 + "/p{searchTerms}";
SimulateDefaultSearchIsManaged(url2);
EXPECT_TRUE(util_.model()->is_default_search_managed());
// Wait for the changes to be saved.
util_.BlockTillServiceProcessesRequests();
// Verify the search providers install state. The default search should be
// the managed one we previously set.
scoped_refptr<TestGetInstallState> test_get_install_state(
new TestGetInstallState(install_data_));
test_get_install_state->set_search_provider_host(host);
test_get_install_state->set_default_search_provider_host(host2);
EXPECT_TRUE(test_get_install_state->RunTests());
}
TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) {
scoped_refptr<TestGetInstallState> test_get_install_state(
new TestGetInstallState(install_data_));
// Set up the database.
util_.ChangeModelToLoadState();
std::string google_host = "w.com";
util_.SetGoogleBaseURL("http://" + google_host + "/");
// Wait for the I/O thread to process the update notification.
TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests();
TemplateURL* t_url = CreateTemplateURL("{google:baseURL}?q={searchTerms}",
"t");
util_.model()->Add(t_url);
TemplateURL* default_url = CreateTemplateURL("http://d.com/",
"d");
util_.model()->Add(default_url);
util_.model()->SetDefaultSearchProvider(default_url);
// Wait for the changes to be saved.
TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests();
// Verify the search providers install state (with no default set).
test_get_install_state->set_search_provider_host(google_host);
EXPECT_TRUE(test_get_install_state->RunTests());
// Change the Google base url.
google_host = "foo.com";
util_.SetGoogleBaseURL("http://" + google_host + "/");
// Wait for the I/O thread to process the update notification.
TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests();
// Verify that the change got picked up.
test_get_install_state->set_search_provider_host(google_host);
EXPECT_TRUE(test_get_install_state->RunTests());
}
|