Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: chrome/browser/search_engines/template_url_service_unittest.cc

Issue 9982018: Move most TemplateURL data members to a new struct, TemplateURLData. This allows us to eliminate t… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/bind_helpers.h" 6 #include "base/bind_helpers.h"
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
10 #include "base/string_split.h" 10 #include "base/string_split.h"
(...skipping 24 matching lines...) Expand all
35 using content::BrowserThread; 35 using content::BrowserThread;
36 using ::testing::Return; 36 using ::testing::Return;
37 using ::testing::StrictMock; 37 using ::testing::StrictMock;
38 38
39 namespace { 39 namespace {
40 40
41 // Create an URL that appears to have been prepopulated, but won't be in the 41 // Create an URL that appears to have been prepopulated, but won't be in the
42 // current data. The caller owns the returned TemplateURL*. 42 // current data. The caller owns the returned TemplateURL*.
43 TemplateURL* CreatePreloadedTemplateURL(bool safe_for_autoreplace, 43 TemplateURL* CreatePreloadedTemplateURL(bool safe_for_autoreplace,
44 int prepopulate_id) { 44 int prepopulate_id) {
45 TemplateURL* t_url = new TemplateURL(); 45 TemplateURLData data;
46 t_url->set_keyword(ASCIIToUTF16("unittest")); 46 data.short_name = ASCIIToUTF16("unittest");
47 t_url->set_short_name(ASCIIToUTF16("unittest")); 47 data.SetKeyword(ASCIIToUTF16("unittest"));
48 t_url->SetPrepopulateId(prepopulate_id); 48 data.SetURL("http://www.unittest.com/");
49 t_url->set_safe_for_autoreplace(safe_for_autoreplace); 49 data.favicon_url = GURL("http://favicon.url");
50 t_url->set_date_created(Time::FromTimeT(100)); 50 data.show_in_default_list = true;
51 t_url->set_last_modified(Time::FromTimeT(100)); 51 data.safe_for_autoreplace = safe_for_autoreplace;
52 t_url->SetURL("http://www.unittest.com/"); 52 data.input_encodings.push_back("UTF-8");
53 t_url->set_favicon_url(GURL("http://favicon.url")); 53 data.date_created = Time::FromTimeT(100);
54 return t_url; 54 data.last_modified = Time::FromTimeT(100);
55 data.prepopulate_id = prepopulate_id;
56 return new TemplateURL(data);
55 } 57 }
56 58
57 59
58 // TestGenerateSearchURL ------------------------------------------------------ 60 // TestGenerateSearchURL ------------------------------------------------------
59 61
60 // Test the GenerateSearchURL on a thread or the main thread. 62 // Test the GenerateSearchURL on a thread or the main thread.
61 class TestGenerateSearchURL 63 class TestGenerateSearchURL
62 : public base::RefCountedThreadSafe<TestGenerateSearchURL> { 64 : public base::RefCountedThreadSafe<TestGenerateSearchURL> {
63 public: 65 public:
64 explicit TestGenerateSearchURL(SearchTermsData* search_terms_data); 66 explicit TestGenerateSearchURL(SearchTermsData* search_terms_data);
(...skipping 30 matching lines...) Expand all
95 { "URL with no replacements", "http://foo/", "http://foo/" }, 97 { "URL with no replacements", "http://foo/", "http://foo/" },
96 { "basic functionality", "http://foo/{searchTerms}", 98 { "basic functionality", "http://foo/{searchTerms}",
97 "http://foo/blah.blah.blah.blah.blah" } 99 "http://foo/blah.blah.blah.blah.blah" }
98 }; 100 };
99 101
100 // Don't use ASSERT/EXPECT since this is run on a thread in one test 102 // Don't use ASSERT/EXPECT since this is run on a thread in one test
101 // and those macros aren't meant for threads at this time according to 103 // and those macros aren't meant for threads at this time according to
102 // gtest documentation. 104 // gtest documentation.
103 bool everything_passed = true; 105 bool everything_passed = true;
104 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generate_url_cases); ++i) { 106 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generate_url_cases); ++i) {
105 TemplateURL t_url; 107 TemplateURLData data;
106 if (generate_url_cases[i].url) 108 if (generate_url_cases[i].url)
107 t_url.SetURL(generate_url_cases[i].url); 109 data.SetURL(generate_url_cases[i].url);
110 TemplateURL t_url(data);
108 std::string result = (search_terms_data_ ? 111 std::string result = (search_terms_data_ ?
109 TemplateURLService::GenerateSearchURLUsingTermsData(&t_url, 112 TemplateURLService::GenerateSearchURLUsingTermsData(&t_url,
110 *search_terms_data_) : 113 *search_terms_data_) :
111 TemplateURLService::GenerateSearchURL(&t_url)).spec(); 114 TemplateURLService::GenerateSearchURL(&t_url)).spec();
112 if (result != generate_url_cases[i].expected) { 115 if (result != generate_url_cases[i].expected) {
113 LOG(ERROR) << generate_url_cases[i].test_name << " failed. Expected " << 116 LOG(ERROR) << generate_url_cases[i].test_name << " failed. Expected " <<
114 generate_url_cases[i].expected << " Actual " << result; 117 generate_url_cases[i].expected << " Actual " << result;
115 everything_passed = false; 118 everything_passed = false;
116 } 119 }
117 } 120 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 // TemplateURLServiceTest ----------------------------------------------------- 154 // TemplateURLServiceTest -----------------------------------------------------
152 155
153 class TemplateURLServiceTest : public testing::Test { 156 class TemplateURLServiceTest : public testing::Test {
154 public: 157 public:
155 TemplateURLServiceTest(); 158 TemplateURLServiceTest();
156 159
157 // testing::Test 160 // testing::Test
158 virtual void SetUp(); 161 virtual void SetUp();
159 virtual void TearDown(); 162 virtual void TearDown();
160 163
161 TemplateURL* AddKeywordWithDate(const std::string& keyword, 164 TemplateURL* AddKeywordWithDate(const std::string& short_name,
165 const std::string& keyword,
162 bool autogenerate_keyword, 166 bool autogenerate_keyword,
163 const std::string& url, 167 const std::string& url,
164 const std::string& suggest_url, 168 const std::string& suggest_url,
165 const std::string& favicon_url, 169 const std::string& favicon_url,
170 bool safe_for_autoreplace,
166 const std::string& encodings, 171 const std::string& encodings,
167 const std::string& short_name,
168 bool safe_for_autoreplace,
169 Time date_created, 172 Time date_created,
170 Time last_modified); 173 Time last_modified);
171 174
172 // Verifies the two TemplateURLs are equal. 175 // Verifies the two TemplateURLs are equal.
173 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual); 176 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual);
174 177
175 // Checks that the two TemplateURLs are similar. It does not check the id, the 178 // Checks that the two TemplateURLs are similar. It does not check the id, the
176 // date_created or the last_modified time. Neither pointer should be NULL. 179 // date_created or the last_modified time. Neither pointer should be NULL.
177 void ExpectSimilar(const TemplateURL* expected, const TemplateURL* actual); 180 void ExpectSimilar(const TemplateURL* expected, const TemplateURL* actual);
178 181
179 // Set the managed preferences for the default search provider and trigger 182 // Set the managed preferences for the default search provider and trigger
180 // notification. 183 // notification.
181 void SetManagedDefaultSearchPreferences(bool enabled, 184 void SetManagedDefaultSearchPreferences(bool enabled,
182 const std::string& name, 185 const std::string& name,
186 const std::string& keyword,
183 const std::string& search_url, 187 const std::string& search_url,
184 const std::string& suggest_url, 188 const std::string& suggest_url,
185 const std::string& icon_url, 189 const std::string& icon_url,
186 const std::string& encodings, 190 const std::string& encodings);
187 const std::string& keyword);
188 191
189 // Remove all the managed preferences for the default search provider and 192 // Remove all the managed preferences for the default search provider and
190 // trigger notification. 193 // trigger notification.
191 void RemoveManagedDefaultSearchPreferences(); 194 void RemoveManagedDefaultSearchPreferences();
192 195
193 // Creates a TemplateURL with the same prepopulated id as a real prepopulated 196 // Creates a TemplateURL with the same prepopulated id as a real prepopulated
194 // item. The input number determines which prepopulated item. The caller is 197 // item. The input number determines which prepopulated item. The caller is
195 // responsible for owning the returned TemplateURL*. 198 // responsible for owning the returned TemplateURL*.
196 TemplateURL* CreateReplaceablePreloadedTemplateURL( 199 TemplateURL* CreateReplaceablePreloadedTemplateURL(
197 bool safe_for_autoreplace, 200 bool safe_for_autoreplace,
(...skipping 23 matching lines...) Expand all
221 224
222 void TemplateURLServiceTest::SetUp() { 225 void TemplateURLServiceTest::SetUp() {
223 test_util_.SetUp(); 226 test_util_.SetUp();
224 } 227 }
225 228
226 void TemplateURLServiceTest::TearDown() { 229 void TemplateURLServiceTest::TearDown() {
227 test_util_.TearDown(); 230 test_util_.TearDown();
228 } 231 }
229 232
230 TemplateURL* TemplateURLServiceTest::AddKeywordWithDate( 233 TemplateURL* TemplateURLServiceTest::AddKeywordWithDate(
234 const std::string& short_name,
231 const std::string& keyword, 235 const std::string& keyword,
232 bool autogenerate_keyword, 236 bool autogenerate_keyword,
233 const std::string& url, 237 const std::string& url,
234 const std::string& suggest_url, 238 const std::string& suggest_url,
235 const std::string& favicon_url, 239 const std::string& favicon_url,
240 bool safe_for_autoreplace,
236 const std::string& encodings, 241 const std::string& encodings,
237 const std::string& short_name,
238 bool safe_for_autoreplace,
239 Time date_created, 242 Time date_created,
240 Time last_modified) { 243 Time last_modified) {
241 TemplateURL* t_url = new TemplateURL(); 244 TemplateURLData data;
242 t_url->set_short_name(UTF8ToUTF16(short_name)); 245 data.short_name = UTF8ToUTF16(short_name);
243 t_url->set_keyword(UTF8ToUTF16(keyword)); 246 data.SetKeyword(UTF8ToUTF16(keyword));
244 t_url->set_autogenerate_keyword(autogenerate_keyword); 247 data.SetAutogenerateKeyword(autogenerate_keyword);
245 t_url->set_safe_for_autoreplace(safe_for_autoreplace); 248 data.SetURL(url);
246 std::vector<std::string> encodings_vector; 249 data.suggestions_url = suggest_url;
247 base::SplitString(encodings, ';', &encodings_vector); 250 data.favicon_url = GURL(favicon_url);
248 t_url->set_input_encodings(encodings_vector); 251 data.safe_for_autoreplace = safe_for_autoreplace;
249 t_url->set_date_created(date_created); 252 base::SplitString(encodings, ';', &data.input_encodings);
250 t_url->set_last_modified(last_modified); 253 data.date_created = date_created;
251 t_url->SetSuggestionsURL(suggest_url); 254 data.last_modified = last_modified;
252 t_url->SetURL(url); 255 TemplateURL* t_url = new TemplateURL(data);
253 t_url->set_favicon_url(GURL(favicon_url));
254 model()->Add(t_url); 256 model()->Add(t_url);
255 EXPECT_NE(0, t_url->id()); 257 EXPECT_NE(0, t_url->id());
256 return t_url; 258 return t_url;
257 } 259 }
258 260
259 void TemplateURLServiceTest::AssertEquals(const TemplateURL& expected, 261 void TemplateURLServiceTest::AssertEquals(const TemplateURL& expected,
260 const TemplateURL& actual) { 262 const TemplateURL& actual) {
261 ASSERT_EQ(expected.short_name(), actual.short_name()); 263 ASSERT_EQ(expected.short_name(), actual.short_name());
264 ASSERT_EQ(expected.keyword(), actual.keyword());
262 ASSERT_EQ(expected.url(), actual.url()); 265 ASSERT_EQ(expected.url(), actual.url());
263 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); 266 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url());
264 ASSERT_EQ(expected.keyword(), actual.keyword()); 267 ASSERT_EQ(expected.favicon_url(), actual.favicon_url());
265 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); 268 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list());
266 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); 269 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace());
267 ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); 270 ASSERT_EQ(expected.input_encodings(), actual.input_encodings());
271 ASSERT_EQ(expected.id(), actual.id());
268 ASSERT_EQ(expected.date_created(), actual.date_created()); 272 ASSERT_EQ(expected.date_created(), actual.date_created());
269 ASSERT_EQ(expected.last_modified(), actual.last_modified()); 273 ASSERT_EQ(expected.last_modified(), actual.last_modified());
270 ASSERT_EQ(expected.input_encodings(), actual.input_encodings());
271 ASSERT_EQ(expected.id(), actual.id());
272 ASSERT_EQ(expected.sync_guid(), actual.sync_guid()); 274 ASSERT_EQ(expected.sync_guid(), actual.sync_guid());
273 } 275 }
274 276
275 void TemplateURLServiceTest::ExpectSimilar(const TemplateURL* expected, 277 void TemplateURLServiceTest::ExpectSimilar(const TemplateURL* expected,
276 const TemplateURL* actual) { 278 const TemplateURL* actual) {
277 ASSERT_TRUE(expected != NULL); 279 ASSERT_TRUE(expected != NULL);
278 ASSERT_TRUE(actual != NULL); 280 ASSERT_TRUE(actual != NULL);
279 EXPECT_EQ(expected->short_name(), actual->short_name()); 281 EXPECT_EQ(expected->short_name(), actual->short_name());
282 EXPECT_EQ(expected->keyword(), actual->keyword());
280 EXPECT_EQ(expected->url(), actual->url()); 283 EXPECT_EQ(expected->url(), actual->url());
281 EXPECT_EQ(expected->suggestions_url(), actual->suggestions_url()); 284 EXPECT_EQ(expected->suggestions_url(), actual->suggestions_url());
282 EXPECT_EQ(expected->keyword(), actual->keyword()); 285 EXPECT_EQ(expected->favicon_url(), actual->favicon_url());
283 EXPECT_EQ(expected->show_in_default_list(), actual->show_in_default_list()); 286 EXPECT_EQ(expected->show_in_default_list(), actual->show_in_default_list());
284 EXPECT_EQ(expected->safe_for_autoreplace(), actual->safe_for_autoreplace()); 287 EXPECT_EQ(expected->safe_for_autoreplace(), actual->safe_for_autoreplace());
285 EXPECT_EQ(expected->favicon_url(), actual->favicon_url());
286 EXPECT_EQ(expected->input_encodings(), actual->input_encodings()); 288 EXPECT_EQ(expected->input_encodings(), actual->input_encodings());
287 } 289 }
288 290
289 void TemplateURLServiceTest::SetManagedDefaultSearchPreferences( 291 void TemplateURLServiceTest::SetManagedDefaultSearchPreferences(
290 bool enabled, 292 bool enabled,
291 const std::string& name, 293 const std::string& name,
294 const std::string& keyword,
292 const std::string& search_url, 295 const std::string& search_url,
293 const std::string& suggest_url, 296 const std::string& suggest_url,
294 const std::string& icon_url, 297 const std::string& icon_url,
295 const std::string& encodings, 298 const std::string& encodings) {
296 const std::string& keyword) {
297 TestingPrefService* service = test_util_.profile()->GetTestingPrefService(); 299 TestingPrefService* service = test_util_.profile()->GetTestingPrefService();
298 service->SetManagedPref(prefs::kDefaultSearchProviderEnabled, 300 service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
299 Value::CreateBooleanValue(enabled)); 301 Value::CreateBooleanValue(enabled));
300 service->SetManagedPref(prefs::kDefaultSearchProviderName, 302 service->SetManagedPref(prefs::kDefaultSearchProviderName,
301 Value::CreateStringValue(name)); 303 Value::CreateStringValue(name));
304 service->SetManagedPref(prefs::kDefaultSearchProviderKeyword,
305 Value::CreateStringValue(keyword));
302 service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL, 306 service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL,
303 Value::CreateStringValue(search_url)); 307 Value::CreateStringValue(search_url));
304 service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL, 308 service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL,
305 Value::CreateStringValue(suggest_url)); 309 Value::CreateStringValue(suggest_url));
306 service->SetManagedPref(prefs::kDefaultSearchProviderIconURL, 310 service->SetManagedPref(prefs::kDefaultSearchProviderIconURL,
307 Value::CreateStringValue(icon_url)); 311 Value::CreateStringValue(icon_url));
308 service->SetManagedPref(prefs::kDefaultSearchProviderEncodings, 312 service->SetManagedPref(prefs::kDefaultSearchProviderEncodings,
309 Value::CreateStringValue(encodings)); 313 Value::CreateStringValue(encodings));
310 service->SetManagedPref(prefs::kDefaultSearchProviderKeyword,
311 Value::CreateStringValue(keyword));
312 } 314 }
313 315
314 void TemplateURLServiceTest::RemoveManagedDefaultSearchPreferences() { 316 void TemplateURLServiceTest::RemoveManagedDefaultSearchPreferences() {
315 TestingPrefService* service = test_util_.profile()->GetTestingPrefService(); 317 TestingPrefService* service = test_util_.profile()->GetTestingPrefService();
316 service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL);
317 service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled); 318 service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled);
318 service->RemoveManagedPref(prefs::kDefaultSearchProviderName); 319 service->RemoveManagedPref(prefs::kDefaultSearchProviderName);
320 service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword);
321 service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL);
319 service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL); 322 service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL);
320 service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL); 323 service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL);
321 service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings); 324 service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings);
322 service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword);
323 service->RemoveManagedPref(prefs::kDefaultSearchProviderID); 325 service->RemoveManagedPref(prefs::kDefaultSearchProviderID);
324 service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID); 326 service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID);
325 } 327 }
326 328
327 TemplateURL* TemplateURLServiceTest::CreateReplaceablePreloadedTemplateURL( 329 TemplateURL* TemplateURLServiceTest::CreateReplaceablePreloadedTemplateURL(
328 bool safe_for_autoreplace, 330 bool safe_for_autoreplace,
329 size_t index_offset_from_default, 331 size_t index_offset_from_default,
330 string16* prepopulated_display_url) { 332 string16* prepopulated_display_url) {
331 ScopedVector<TemplateURL> prepopulated_urls; 333 ScopedVector<TemplateURL> prepopulated_urls;
332 size_t default_search_provider_index = 0; 334 size_t default_search_provider_index = 0;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 394
393 TEST_F(TemplateURLServiceTest, Load) { 395 TEST_F(TemplateURLServiceTest, Load) {
394 test_util_.VerifyLoad(); 396 test_util_.VerifyLoad();
395 } 397 }
396 398
397 TEST_F(TemplateURLServiceTest, AddUpdateRemove) { 399 TEST_F(TemplateURLServiceTest, AddUpdateRemove) {
398 // Add a new TemplateURL. 400 // Add a new TemplateURL.
399 test_util_.VerifyLoad(); 401 test_util_.VerifyLoad();
400 const size_t initial_count = model()->GetTemplateURLs().size(); 402 const size_t initial_count = model()->GetTemplateURLs().size();
401 403
402 TemplateURL* t_url = new TemplateURL(); 404 TemplateURLData data;
403 t_url->set_short_name(ASCIIToUTF16("google")); 405 data.short_name = ASCIIToUTF16("google");
404 t_url->set_keyword(ASCIIToUTF16("keyword")); 406 data.SetKeyword(ASCIIToUTF16("keyword"));
405 t_url->set_safe_for_autoreplace(true); 407 data.SetURL("http://www.google.com/foo/bar");
406 t_url->set_date_created(Time::FromTimeT(100)); 408 data.favicon_url = GURL("http://favicon.url");
407 t_url->set_last_modified(Time::FromTimeT(100)); 409 data.safe_for_autoreplace = true;
408 t_url->set_sync_guid("00000000-0000-0000-0000-000000000001"); 410 data.date_created = Time::FromTimeT(100);
409 t_url->SetURL("http://www.google.com/foo/bar"); 411 data.last_modified = Time::FromTimeT(100);
410 t_url->set_favicon_url(GURL("http://favicon.url")); 412 data.sync_guid = "00000000-0000-0000-0000-000000000001";
413 TemplateURL* t_url = new TemplateURL(data);
411 model()->Add(t_url); 414 model()->Add(t_url);
412 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), 415 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
413 NULL)); 416 NULL));
414 VerifyObserverCount(1); 417 VerifyObserverCount(1);
415 test_util_.BlockTillServiceProcessesRequests(); 418 test_util_.BlockTillServiceProcessesRequests();
416 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 419 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
417 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(t_url->keyword())); 420 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(t_url->keyword()));
418 // We need to make a second copy as the model takes ownership of |t_url| and 421 // We need to make a second copy as the model takes ownership of |t_url| and
419 // will delete it. We have to do this after calling Add() since that gives 422 // will delete it. We have to do this after calling Add() since that gives
420 // |t_url| its ID. 423 // |t_url| its ID.
421 TemplateURL cloned_url(*t_url); 424 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
422 425
423 // Reload the model to verify it was actually saved to the database. 426 // Reload the model to verify it was actually saved to the database.
424 test_util_.ResetModel(true); 427 test_util_.ResetModel(true);
425 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 428 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
426 const TemplateURL* loaded_url = 429 const TemplateURL* loaded_url =
427 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); 430 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
428 ASSERT_TRUE(loaded_url != NULL); 431 ASSERT_TRUE(loaded_url != NULL);
429 AssertEquals(cloned_url, *loaded_url); 432 AssertEquals(*cloned_url, *loaded_url);
430 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), 433 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
431 NULL)); 434 NULL));
432 435
433 // We expect the last_modified time to be updated to the present time on an 436 // We expect the last_modified time to be updated to the present time on an
434 // explicit reset. We have to set up the expectation here because ResetModel 437 // explicit reset. We have to set up the expectation here because ResetModel
435 // resets the TimeProvider in the TemplateURLService. 438 // resets the TimeProvider in the TemplateURLService.
436 StrictMock<base::MockTimeProvider> mock_time; 439 StrictMock<base::MockTimeProvider> mock_time;
437 model()->set_time_provider(&base::MockTimeProvider::StaticNow); 440 model()->set_time_provider(&base::MockTimeProvider::StaticNow);
438 EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337))); 441 EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337)));
439 442
440 // Mutate an element and verify it succeeded. 443 // Mutate an element and verify it succeeded.
441 model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"), 444 model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"),
442 "c"); 445 "c");
443 ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name()); 446 ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name());
444 ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword()); 447 ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword());
445 ASSERT_EQ("c", loaded_url->url()); 448 ASSERT_EQ("c", loaded_url->url());
446 ASSERT_FALSE(loaded_url->safe_for_autoreplace()); 449 ASSERT_FALSE(loaded_url->safe_for_autoreplace());
447 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), 450 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
448 NULL)); 451 NULL));
449 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL)); 452 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL));
450 cloned_url = *loaded_url; 453 cloned_url.reset(new TemplateURL(loaded_url->data()));
451 test_util_.BlockTillServiceProcessesRequests(); 454 test_util_.BlockTillServiceProcessesRequests();
452 test_util_.ResetModel(true); 455 test_util_.ResetModel(true);
453 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 456 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
454 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")); 457 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b"));
455 ASSERT_TRUE(loaded_url != NULL); 458 ASSERT_TRUE(loaded_url != NULL);
456 AssertEquals(cloned_url, *loaded_url); 459 AssertEquals(*cloned_url, *loaded_url);
457 // We changed a TemplateURL in the service, so ensure that the time was 460 // We changed a TemplateURL in the service, so ensure that the time was
458 // updated. 461 // updated.
459 ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified()); 462 ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified());
460 463
461 // Remove an element and verify it succeeded. 464 // Remove an element and verify it succeeded.
462 model()->Remove(loaded_url); 465 model()->Remove(loaded_url);
463 VerifyObserverCount(1); 466 VerifyObserverCount(1);
464 test_util_.ResetModel(true); 467 test_util_.ResetModel(true);
465 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size()); 468 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size());
466 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL); 469 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 517
515 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) { 518 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) {
516 Time now = Time::Now(); 519 Time now = Time::Now();
517 TimeDelta one_day = TimeDelta::FromDays(1); 520 TimeDelta one_day = TimeDelta::FromDays(1);
518 Time month_ago = now - TimeDelta::FromDays(30); 521 Time month_ago = now - TimeDelta::FromDays(30);
519 522
520 // Nothing has been added. 523 // Nothing has been added.
521 EXPECT_EQ(0U, model()->GetTemplateURLs().size()); 524 EXPECT_EQ(0U, model()->GetTemplateURLs().size());
522 525
523 // Create one with a 0 time. 526 // Create one with a 0 time.
524 AddKeywordWithDate("key1", false, "http://foo1", "http://suggest1", 527 AddKeywordWithDate("name1", "key1", false, "http://foo1", "http://suggest1",
525 "http://icon1", "UTF-8;UTF-16", "name1", true, Time(), 528 "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
526 Time());
527 // Create one for now and +/- 1 day. 529 // Create one for now and +/- 1 day.
528 AddKeywordWithDate("key2", false, "http://foo2", "http://suggest2", 530 AddKeywordWithDate("name2", "key2", false, "http://foo2", "http://suggest2",
529 "http://icon2", "UTF-8;UTF-16", "name2", true, 531 "http://icon2", true, "UTF-8;UTF-16", now - one_day, Time());
530 now - one_day, Time()); 532 AddKeywordWithDate("name3", "key3", false, "http://foo3", std::string(),
531 AddKeywordWithDate("key3", false, "http://foo3", std::string(), std::string(), 533 std::string(), true, std::string(), now, Time());
532 std::string(), "name3", true, now, Time()); 534 AddKeywordWithDate("name4", "key4", false, "http://foo4", std::string(),
533 AddKeywordWithDate("key4", false, "http://foo4", std::string(), std::string(), 535 std::string(), true, std::string(), now + one_day, Time());
534 std::string(), "name4", true, now + one_day, Time());
535 // Try the other three states. 536 // Try the other three states.
536 AddKeywordWithDate("key5", false, "http://foo5", "http://suggest5", 537 AddKeywordWithDate("name5", "key5", false, "http://foo5", "http://suggest5",
537 "http://icon5", "UTF-8;UTF-16", "name5", false, now, 538 "http://icon5", false, "UTF-8;UTF-16", now, Time());
538 Time()); 539 AddKeywordWithDate("name6", "key6", false, "http://foo6", "http://suggest6",
539 AddKeywordWithDate("key6", false, "http://foo6", "http://suggest6", 540 "http://icon6", false, "UTF-8;UTF-16", month_ago, Time());
540 "http://icon6", "UTF-8;UTF-16", "name6", false,
541 month_ago, Time());
542 541
543 // We just added a few items, validate them. 542 // We just added a few items, validate them.
544 EXPECT_EQ(6U, model()->GetTemplateURLs().size()); 543 EXPECT_EQ(6U, model()->GetTemplateURLs().size());
545 544
546 // Try removing from current timestamp. This should delete the one in the 545 // Try removing from current timestamp. This should delete the one in the
547 // future and one very recent one. 546 // future and one very recent one.
548 model()->RemoveAutoGeneratedSince(now); 547 model()->RemoveAutoGeneratedSince(now);
549 EXPECT_EQ(4U, model()->GetTemplateURLs().size()); 548 EXPECT_EQ(4U, model()->GetTemplateURLs().size());
550 549
551 // Try removing from two months ago. This should only delete items that are 550 // Try removing from two months ago. This should only delete items that are
(...skipping 24 matching lines...) Expand all
576 575
577 TEST_F(TemplateURLServiceTest, ClearBrowsingData_KeywordsForOrigin) { 576 TEST_F(TemplateURLServiceTest, ClearBrowsingData_KeywordsForOrigin) {
578 Time now = Time::Now(); 577 Time now = Time::Now();
579 TimeDelta one_day = TimeDelta::FromDays(1); 578 TimeDelta one_day = TimeDelta::FromDays(1);
580 Time month_ago = now - TimeDelta::FromDays(30); 579 Time month_ago = now - TimeDelta::FromDays(30);
581 580
582 // Nothing has been added. 581 // Nothing has been added.
583 EXPECT_EQ(0U, model()->GetTemplateURLs().size()); 582 EXPECT_EQ(0U, model()->GetTemplateURLs().size());
584 583
585 // Create one for now and +/- 1 day. 584 // Create one for now and +/- 1 day.
586 AddKeywordWithDate("key1", false, "http://foo1", "http://suggest1", 585 AddKeywordWithDate("name1", "key1", false, "http://foo1", "http://suggest1",
587 "http://icon2", "UTF-8;UTF-16", "name2", true, 586 "http://icon2", true, "UTF-8;UTF-16", now - one_day, Time());
588 now - one_day, Time()); 587 AddKeywordWithDate("name2", "key2", false, "http://foo2", std::string(),
589 AddKeywordWithDate("key2", false, "http://foo2", std::string(), std::string(), 588 std::string(), true, std::string(), now, Time());
590 std::string(), "name2", true, now, Time()); 589 AddKeywordWithDate("name3", "key3", false, "http://foo3", std::string(),
591 AddKeywordWithDate("key3", false, "http://foo3", std::string(), std::string(), 590 std::string(), true, std::string(), now + one_day, Time());
592 std::string(), "name3", true, now + one_day, Time());
593 591
594 // We just added a few items, validate them. 592 // We just added a few items, validate them.
595 EXPECT_EQ(3U, model()->GetTemplateURLs().size()); 593 EXPECT_EQ(3U, model()->GetTemplateURLs().size());
596 594
597 // Try removing foo2. This should delete foo2, but leave foo1 and 3 untouched. 595 // Try removing foo2. This should delete foo2, but leave foo1 and 3 untouched.
598 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo2"), month_ago, 596 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo2"), month_ago,
599 now + one_day); 597 now + one_day);
600 EXPECT_EQ(2U, model()->GetTemplateURLs().size()); 598 EXPECT_EQ(2U, model()->GetTemplateURLs().size());
601 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword()); 599 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
602 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace()); 600 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
(...skipping 16 matching lines...) Expand all
619 now + one_day + one_day); 617 now + one_day + one_day);
620 EXPECT_EQ(1U, model()->GetTemplateURLs().size()); 618 EXPECT_EQ(1U, model()->GetTemplateURLs().size());
621 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword()); 619 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
622 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace()); 620 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
623 } 621 }
624 622
625 TEST_F(TemplateURLServiceTest, Reset) { 623 TEST_F(TemplateURLServiceTest, Reset) {
626 // Add a new TemplateURL. 624 // Add a new TemplateURL.
627 test_util_.VerifyLoad(); 625 test_util_.VerifyLoad();
628 const size_t initial_count = model()->GetTemplateURLs().size(); 626 const size_t initial_count = model()->GetTemplateURLs().size();
629 TemplateURL* t_url = new TemplateURL(); 627 TemplateURLData data;
630 t_url->set_short_name(ASCIIToUTF16("google")); 628 data.short_name = ASCIIToUTF16("google");
631 t_url->set_keyword(ASCIIToUTF16("keyword")); 629 data.SetKeyword(ASCIIToUTF16("keyword"));
632 t_url->set_date_created(Time::FromTimeT(100)); 630 data.SetURL("http://www.google.com/foo/bar");
633 t_url->set_last_modified(Time::FromTimeT(100)); 631 data.favicon_url = GURL("http://favicon.url");
634 t_url->SetURL("http://www.google.com/foo/bar"); 632 data.date_created = Time::FromTimeT(100);
635 t_url->set_favicon_url(GURL("http://favicon.url")); 633 data.last_modified = Time::FromTimeT(100);
634 TemplateURL* t_url = new TemplateURL(data);
636 model()->Add(t_url); 635 model()->Add(t_url);
637 636
638 VerifyObserverCount(1); 637 VerifyObserverCount(1);
639 test_util_.BlockTillServiceProcessesRequests(); 638 test_util_.BlockTillServiceProcessesRequests();
640 639
641 StrictMock<base::MockTimeProvider> mock_time; 640 StrictMock<base::MockTimeProvider> mock_time;
642 model()->set_time_provider(&base::MockTimeProvider::StaticNow); 641 model()->set_time_provider(&base::MockTimeProvider::StaticNow);
643 EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337))); 642 EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337)));
644 643
645 // Reset the short name, keyword, url and make sure it takes. 644 // Reset the short name, keyword, url and make sure it takes.
646 const string16 new_short_name(ASCIIToUTF16("a")); 645 const string16 new_short_name(ASCIIToUTF16("a"));
647 const string16 new_keyword(ASCIIToUTF16("b")); 646 const string16 new_keyword(ASCIIToUTF16("b"));
648 const std::string new_url("c"); 647 const std::string new_url("c");
649 model()->ResetTemplateURL(t_url, new_short_name, new_keyword, new_url); 648 model()->ResetTemplateURL(t_url, new_short_name, new_keyword, new_url);
650 ASSERT_EQ(new_short_name, t_url->short_name()); 649 ASSERT_EQ(new_short_name, t_url->short_name());
651 ASSERT_EQ(new_keyword, t_url->keyword()); 650 ASSERT_EQ(new_keyword, t_url->keyword());
652 ASSERT_EQ(new_url, t_url->url()); 651 ASSERT_EQ(new_url, t_url->url());
653 652
654 // Make sure the mappings in the model were updated. 653 // Make sure the mappings in the model were updated.
655 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword)); 654 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword));
656 ASSERT_TRUE( 655 ASSERT_TRUE(
657 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")) == NULL); 656 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")) == NULL);
658 657
659 TemplateURL last_url(*t_url); 658 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
660 659
661 // Reload the model from the database and make sure the change took. 660 // Reload the model from the database and make sure the change took.
662 test_util_.ResetModel(true); 661 test_util_.ResetModel(true);
663 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 662 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
664 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword); 663 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword);
665 ASSERT_TRUE(read_url); 664 ASSERT_TRUE(read_url);
666 AssertEquals(last_url, *read_url); 665 AssertEquals(*cloned_url, *read_url);
667 ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified()); 666 ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified());
668 } 667 }
669 668
670 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { 669 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) {
671 // Add a new TemplateURL. 670 // Add a new TemplateURL.
672 test_util_.VerifyLoad(); 671 test_util_.VerifyLoad();
673 const size_t initial_count = model()->GetTemplateURLs().size(); 672 const size_t initial_count = model()->GetTemplateURLs().size();
674 TemplateURL* t_url = AddKeywordWithDate("key1", false, "http://foo1", 673 TemplateURL* t_url = AddKeywordWithDate("name1", "key1", false, "http://foo1",
675 "http://sugg1", "http://icon1", "UTF-8;UTF-16", "name1", true, Time(), 674 "http://sugg1", "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
676 Time());
677 test_util_.ResetObserverCount(); 675 test_util_.ResetObserverCount();
678 676
679 model()->SetDefaultSearchProvider(t_url); 677 model()->SetDefaultSearchProvider(t_url);
680 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); 678 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider());
681 ASSERT_TRUE(t_url->safe_for_autoreplace()); 679 ASSERT_TRUE(t_url->safe_for_autoreplace());
682 ASSERT_TRUE(t_url->show_in_default_list()); 680 ASSERT_TRUE(t_url->show_in_default_list());
683 681
684 // Setting the default search provider should have caused notification. 682 // Setting the default search provider should have caused notification.
685 VerifyObserverCount(1); 683 VerifyObserverCount(1);
686 test_util_.BlockTillServiceProcessesRequests(); 684 test_util_.BlockTillServiceProcessesRequests();
687 685
688 TemplateURL cloned_url(*t_url); 686 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
689 687
690 // Make sure when we reload we get a default search provider. 688 // Make sure when we reload we get a default search provider.
691 test_util_.ResetModel(true); 689 test_util_.ResetModel(true);
692 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 690 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
693 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 691 ASSERT_TRUE(model()->GetDefaultSearchProvider());
694 AssertEquals(cloned_url, *model()->GetDefaultSearchProvider()); 692 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider());
695 } 693 }
696 694
697 TEST_F(TemplateURLServiceTest, TemplateURLWithNoKeyword) { 695 TEST_F(TemplateURLServiceTest, TemplateURLWithNoKeyword) {
698 test_util_.VerifyLoad(); 696 test_util_.VerifyLoad();
699 697
700 const size_t initial_count = model()->GetTemplateURLs().size(); 698 const size_t initial_count = model()->GetTemplateURLs().size();
701 699
702 AddKeywordWithDate("", false, "http://foo1", "http://sugg1", 700 AddKeywordWithDate("name1", std::string(), false, "http://foo1",
703 "http://icon1", "UTF-8;UTF-16", "name1", true, Time(), Time()); 701 "http://sugg1", "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
704 702
705 // We just added a few items, validate them. 703 // We just added a few items, validate them.
706 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 704 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
707 705
708 // Reload the model from the database and make sure we get the url back. 706 // Reload the model from the database and make sure we get the url back.
709 test_util_.ResetModel(true); 707 test_util_.ResetModel(true);
710 708
711 ASSERT_EQ(1 + initial_count, model()->GetTemplateURLs().size()); 709 ASSERT_EQ(1 + initial_count, model()->GetTemplateURLs().size());
712 710
713 bool found_keyword = false; 711 bool found_keyword = false;
714 for (size_t i = 0; i < initial_count + 1; ++i) { 712 for (size_t i = 0; i < initial_count + 1; ++i) {
715 if (model()->GetTemplateURLs()[i]->keyword().empty()) { 713 if (model()->GetTemplateURLs()[i]->keyword().empty()) {
716 found_keyword = true; 714 found_keyword = true;
717 break; 715 break;
718 } 716 }
719 } 717 }
720 ASSERT_TRUE(found_keyword); 718 ASSERT_TRUE(found_keyword);
721 } 719 }
722 720
723 TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) { 721 TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) {
724 test_util_.ChangeModelToLoadState(); 722 test_util_.ChangeModelToLoadState();
725 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL)); 723 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL));
726 TemplateURL* t_url = AddKeywordWithDate("foo", false, "http://foo1", 724 TemplateURL* t_url = AddKeywordWithDate("name1", "foo", false, "http://foo1",
727 "http://sugg1", "http://icon1", "UTF-8;UTF-16", "name1", true, Time(), 725 "http://sugg1", "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
728 Time());
729 726
730 // Can still replace, newly added template url is marked safe to replace. 727 // Can still replace, newly added template url is marked safe to replace.
731 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), 728 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
732 GURL("http://foo2"), NULL)); 729 GURL("http://foo2"), NULL));
733 730
734 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should 731 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should
735 // no longer be replaceable. 732 // no longer be replaceable.
736 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), 733 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(),
737 t_url->url()); 734 t_url->url());
738 735
739 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), 736 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
740 GURL("http://foo2"), NULL)); 737 GURL("http://foo2"), NULL));
741 } 738 }
742 739
743 TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) { 740 TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) {
744 test_util_.ChangeModelToLoadState(); 741 test_util_.ChangeModelToLoadState();
745 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), 742 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
746 GURL("http://foo.com"), NULL)); 743 GURL("http://foo.com"), NULL));
747 TemplateURL* t_url = AddKeywordWithDate("foo", false, "http://foo.com", 744 TemplateURL* t_url = AddKeywordWithDate("name1", "foo", false,
748 "http://sugg1", "http://icon1", "UTF-8;UTF-16", "name1", true, Time(), 745 "http://foo.com", "http://sugg1", "http://icon1", true, "UTF-8;UTF-16",
749 Time()); 746 Time(), Time());
750 747
751 // Can still replace, newly added template url is marked safe to replace. 748 // Can still replace, newly added template url is marked safe to replace.
752 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), 749 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"),
753 GURL("http://foo.com"), NULL)); 750 GURL("http://foo.com"), NULL));
754 751
755 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should 752 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should
756 // no longer be replaceable. 753 // no longer be replaceable.
757 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), 754 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(),
758 t_url->url()); 755 t_url->url());
759 756
760 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), 757 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"),
761 GURL("http://foo.com"), NULL)); 758 GURL("http://foo.com"), NULL));
762 } 759 }
763 760
764 TEST_F(TemplateURLServiceTest, HasDefaultSearchProvider) { 761 TEST_F(TemplateURLServiceTest, HasDefaultSearchProvider) {
765 // We should have a default search provider even if we haven't loaded. 762 // We should have a default search provider even if we haven't loaded.
766 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 763 ASSERT_TRUE(model()->GetDefaultSearchProvider());
767 764
768 // Now force the model to load and make sure we still have a default. 765 // Now force the model to load and make sure we still have a default.
769 test_util_.VerifyLoad(); 766 test_util_.VerifyLoad();
770 767
771 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 768 ASSERT_TRUE(model()->GetDefaultSearchProvider());
772 } 769 }
773 770
774 TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) { 771 TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) {
775 test_util_.VerifyLoad(); 772 test_util_.VerifyLoad();
776 773
777 TemplateURL* t_url = new TemplateURL(); 774 TemplateURLData data;
778 t_url->set_short_name(ASCIIToUTF16("a")); 775 data.short_name = ASCIIToUTF16("a");
779 t_url->set_safe_for_autoreplace(true); 776 data.safe_for_autoreplace = true;
780 t_url->set_date_created(Time::FromTimeT(100)); 777 data.SetURL("http://url");
781 t_url->set_last_modified(Time::FromTimeT(100)); 778 data.suggestions_url = "http://url2";
782 t_url->SetSuggestionsURL("http://url2"); 779 data.instant_url = "http://instant";
783 t_url->SetURL("http://url"); 780 data.date_created = Time::FromTimeT(100);
784 t_url->SetInstantURL("http://instant"); 781 data.last_modified = Time::FromTimeT(100);
782 TemplateURL* t_url = new TemplateURL(data);
785 model()->Add(t_url); 783 model()->Add(t_url);
786 const TemplateURLID id = t_url->id(); 784 const TemplateURLID id = t_url->id();
787 785
788 model()->SetDefaultSearchProvider(t_url); 786 model()->SetDefaultSearchProvider(t_url);
789 test_util_.BlockTillServiceProcessesRequests(); 787 test_util_.BlockTillServiceProcessesRequests();
790 TemplateURL first_default_search_provider(*t_url); 788 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
791 789
792 // Reset the model and don't load it. The template url we set as the default 790 // Reset the model and don't load it. The template url we set as the default
793 // should be pulled from prefs now. 791 // should be pulled from prefs now.
794 test_util_.ResetModel(false); 792 test_util_.ResetModel(false);
795 793
796 // NOTE: This doesn't use AssertEquals as only a subset of the TemplateURLs 794 // NOTE: This doesn't use AssertEquals as only a subset of the TemplateURLs
797 // value are persisted to prefs. 795 // value are persisted to prefs.
798 const TemplateURL* default_turl = model()->GetDefaultSearchProvider(); 796 const TemplateURL* default_turl = model()->GetDefaultSearchProvider();
799 ASSERT_TRUE(default_turl); 797 ASSERT_TRUE(default_turl);
800 ASSERT_EQ("http://url", default_turl->url()); 798 ASSERT_EQ("http://url", default_turl->url());
801 ASSERT_EQ("http://url2", default_turl->suggestions_url()); 799 ASSERT_EQ("http://url2", default_turl->suggestions_url());
802 EXPECT_EQ("http://instant", default_turl->instant_url()); 800 EXPECT_EQ("http://instant", default_turl->instant_url());
803 ASSERT_EQ(ASCIIToUTF16("a"), default_turl->short_name()); 801 ASSERT_EQ(ASCIIToUTF16("a"), default_turl->short_name());
804 ASSERT_EQ(id, default_turl->id()); 802 ASSERT_EQ(id, default_turl->id());
805 803
806 // Now do a load and make sure the default search provider really takes. 804 // Now do a load and make sure the default search provider really takes.
807 test_util_.VerifyLoad(); 805 test_util_.VerifyLoad();
808 806
809 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 807 ASSERT_TRUE(model()->GetDefaultSearchProvider());
810 AssertEquals(first_default_search_provider, 808 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider());
811 *model()->GetDefaultSearchProvider());
812 } 809 }
813 810
814 TEST_F(TemplateURLServiceTest, BuildQueryTerms) { 811 TEST_F(TemplateURLServiceTest, BuildQueryTerms) {
815 struct TestData { 812 struct TestData {
816 const std::string url; 813 const std::string url;
817 const bool result; 814 const bool result;
818 // Keys and values are a semicolon separated list of expected values in the 815 // Keys and values are a semicolon separated list of expected values in the
819 // map. 816 // map.
820 const std::string keys; 817 const std::string keys;
821 const std::string values; 818 const std::string values;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 { "http://foo/", string16() }, 862 { "http://foo/", string16() },
866 { "http://foo/foo?q=xx", string16() }, 863 { "http://foo/foo?q=xx", string16() },
867 { "http://x/bar?q=xx", string16() }, 864 { "http://x/bar?q=xx", string16() },
868 { "http://x/foo?y=xx", string16() }, 865 { "http://x/foo?y=xx", string16() },
869 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, 866 { "http://x/foo?q=xx", ASCIIToUTF16("xx") },
870 { "http://x/foo?a=b&q=xx", ASCIIToUTF16("xx") }, 867 { "http://x/foo?a=b&q=xx", ASCIIToUTF16("xx") },
871 { "http://x/foo?q=b&q=xx", string16() }, 868 { "http://x/foo?q=b&q=xx", string16() },
872 }; 869 };
873 870
874 test_util_.ChangeModelToLoadState(); 871 test_util_.ChangeModelToLoadState();
875 AddKeywordWithDate("x", false, "http://x/foo?q={searchTerms}", 872 AddKeywordWithDate("name", "x", false, "http://x/foo?q={searchTerms}",
876 "http://sugg1", "http://icon1", "UTF-8;UTF-16", "name", false, Time(), 873 "http://sugg1", "http://icon1", false, "UTF-8;UTF-16", Time(), Time());
877 Time());
878 874
879 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { 875 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
880 history::URLVisitedDetails details; 876 history::URLVisitedDetails details;
881 details.row = history::URLRow(GURL(data[i].url)); 877 details.row = history::URLRow(GURL(data[i].url));
882 details.transition = content::PageTransitionFromInt(0); 878 details.transition = content::PageTransitionFromInt(0);
883 model()->UpdateKeywordSearchTermsForURL(details); 879 model()->UpdateKeywordSearchTermsForURL(details);
884 EXPECT_EQ(data[i].term, test_util_.GetAndClearSearchTerm()); 880 EXPECT_EQ(data[i].term, test_util_.GetAndClearSearchTerm());
885 } 881 }
886 } 882 }
887 883
888 TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) { 884 TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) {
889 struct TestData { 885 struct TestData {
890 const std::string url; 886 const std::string url;
891 } data[] = { 887 } data[] = {
892 { "http://foo/" }, 888 { "http://foo/" },
893 { "http://x/bar?q=xx" }, 889 { "http://x/bar?q=xx" },
894 { "http://x/foo?y=xx" }, 890 { "http://x/foo?y=xx" },
895 }; 891 };
896 892
897 test_util_.ChangeModelToLoadState(); 893 test_util_.ChangeModelToLoadState();
898 AddKeywordWithDate("x", false, "http://x/foo", "http://sugg1", 894 AddKeywordWithDate("name", "x", false, "http://x/foo", "http://sugg1",
899 "http://icon1", "UTF-8;UTF-16", "name", false, Time(), Time()); 895 "http://icon1", false, "UTF-8;UTF-16", Time(), Time());
900 896
901 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { 897 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
902 history::URLVisitedDetails details; 898 history::URLVisitedDetails details;
903 details.row = history::URLRow(GURL(data[i].url)); 899 details.row = history::URLRow(GURL(data[i].url));
904 details.transition = content::PageTransitionFromInt(0); 900 details.transition = content::PageTransitionFromInt(0);
905 model()->UpdateKeywordSearchTermsForURL(details); 901 model()->UpdateKeywordSearchTermsForURL(details);
906 ASSERT_EQ(string16(), test_util_.GetAndClearSearchTerm()); 902 ASSERT_EQ(string16(), test_util_.GetAndClearSearchTerm());
907 } 903 }
908 } 904 }
909 905
910 TEST_F(TemplateURLServiceTest, ChangeGoogleBaseValue) { 906 TEST_F(TemplateURLServiceTest, ChangeGoogleBaseValue) {
911 // NOTE: Do not do a VerifyLoad() here as it will load the prepopulate data, 907 // NOTE: Do not do a VerifyLoad() here as it will load the prepopulate data,
912 // which also has a {google:baseURL} keyword in it, which will confuse this 908 // which also has a {google:baseURL} keyword in it, which will confuse this
913 // test. 909 // test.
914 test_util_.ChangeModelToLoadState(); 910 test_util_.ChangeModelToLoadState();
915 test_util_.SetGoogleBaseURL("http://google.com/"); 911 test_util_.SetGoogleBaseURL("http://google.com/");
916 const TemplateURL* t_url = AddKeywordWithDate("google.com", true, 912 const TemplateURL* t_url = AddKeywordWithDate("name", "google.com", true,
917 "{google:baseURL}?q={searchTerms}", "http://sugg1", "http://icon1", 913 "{google:baseURL}?q={searchTerms}", "http://sugg1", "http://icon1",
918 "UTF-8;UTF-16", "name", false, Time(), Time()); 914 false, "UTF-8;UTF-16", Time(), Time());
919 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com")); 915 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com"));
920 EXPECT_EQ("google.com", t_url->url_ref().GetHost()); 916 EXPECT_EQ("google.com", t_url->url_ref().GetHost());
921 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword()); 917 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword());
922 918
923 // Change the Google base url. 919 // Change the Google base url.
924 test_util_.ResetObserverCount(); 920 test_util_.ResetObserverCount();
925 test_util_.SetGoogleBaseURL("http://google.co.uk/"); 921 test_util_.SetGoogleBaseURL("http://google.co.uk/");
926 VerifyObserverCount(1); 922 VerifyObserverCount(1);
927 923
928 // Make sure the host->TemplateURL map was updated appropriately. 924 // Make sure the host->TemplateURL map was updated appropriately.
(...skipping 24 matching lines...) Expand all
953 history::VisitVector visits; 949 history::VisitVector visits;
954 }; 950 };
955 951
956 // Make sure TemplateURLService generates a KEYWORD_GENERATED visit for 952 // Make sure TemplateURLService generates a KEYWORD_GENERATED visit for
957 // KEYWORD visits. 953 // KEYWORD visits.
958 TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { 954 TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) {
959 test_util_.VerifyLoad(); 955 test_util_.VerifyLoad();
960 test_util_.profile()->CreateHistoryService(true, false); 956 test_util_.profile()->CreateHistoryService(true, false);
961 957
962 // Create a keyword. 958 // Create a keyword.
963 TemplateURL* t_url = AddKeywordWithDate( 959 TemplateURL* t_url = AddKeywordWithDate("keyword", "keyword", false,
964 "keyword", false, "http://foo.com/foo?query={searchTerms}", 960 "http://foo.com/foo?query={searchTerms}", "http://sugg1", "http://icon1",
965 "http://sugg1", "http://icon1", "UTF-8;UTF-16", "keyword", 961 true, "UTF-8;UTF-16", base::Time::Now(), base::Time::Now());
966 true, base::Time::Now(), base::Time::Now());
967 962
968 // Add a visit that matches the url of the keyword. 963 // Add a visit that matches the url of the keyword.
969 HistoryService* history = 964 HistoryService* history =
970 test_util_.profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); 965 test_util_.profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
971 history->AddPage( 966 history->AddPage(
972 GURL(t_url->url_ref().ReplaceSearchTerms(ASCIIToUTF16("blah"), 967 GURL(t_url->url_ref().ReplaceSearchTerms(ASCIIToUTF16("blah"),
973 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), 968 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())),
974 NULL, 0, GURL(), content::PAGE_TRANSITION_KEYWORD, 969 NULL, 0, GURL(), content::PAGE_TRANSITION_KEYWORD,
975 history::RedirectList(), history::SOURCE_BROWSED, false); 970 history::RedirectList(), history::SOURCE_BROWSED, false);
976 971
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1024 1019
1025 // Make sure that load routine doesn't delete prepopulated engines that no 1020 // Make sure that load routine doesn't delete prepopulated engines that no
1026 // longer exist in the prepopulate data if it has been modified by the user. 1021 // longer exist in the prepopulate data if it has been modified by the user.
1027 TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) { 1022 TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) {
1028 // Create a preloaded template url and add it to a loaded model. 1023 // Create a preloaded template url and add it to a loaded model.
1029 TemplateURL* t_url = CreatePreloadedTemplateURL(false, 999999); 1024 TemplateURL* t_url = CreatePreloadedTemplateURL(false, 999999);
1030 test_util_.ChangeModelToLoadState(); 1025 test_util_.ChangeModelToLoadState();
1031 model()->Add(t_url); 1026 model()->Add(t_url);
1032 1027
1033 // Do the copy after t_url is added so that the id is set. 1028 // Do the copy after t_url is added so that the id is set.
1034 TemplateURL copy_t_url = *t_url; 1029 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
1035 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"))); 1030 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")));
1036 1031
1037 // Wait for any saves to finish. 1032 // Wait for any saves to finish.
1038 test_util_.BlockTillServiceProcessesRequests(); 1033 test_util_.BlockTillServiceProcessesRequests();
1039 1034
1040 // Ensure that merging won't clear it if the user has edited it. 1035 // Ensure that merging won't clear it if the user has edited it.
1041 test_util_.ResetModel(true); 1036 test_util_.ResetModel(true);
1042 const TemplateURL* url_for_unittest = 1037 const TemplateURL* url_for_unittest =
1043 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); 1038 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
1044 ASSERT_TRUE(url_for_unittest != NULL); 1039 ASSERT_TRUE(url_for_unittest != NULL);
1045 AssertEquals(copy_t_url, *url_for_unittest); 1040 AssertEquals(*cloned_url, *url_for_unittest);
1046 1041
1047 // Wait for any saves to finish. 1042 // Wait for any saves to finish.
1048 test_util_.BlockTillServiceProcessesRequests(); 1043 test_util_.BlockTillServiceProcessesRequests();
1049 1044
1050 // Reload the model to verify that save/reload retains the item. 1045 // Reload the model to verify that save/reload retains the item.
1051 test_util_.ResetModel(true); 1046 test_util_.ResetModel(true);
1052 ASSERT_TRUE( 1047 ASSERT_TRUE(
1053 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL); 1048 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL);
1054 } 1049 }
1055 1050
1056 // Make sure that load routine doesn't delete 1051 // Make sure that load routine doesn't delete
1057 // prepopulated engines that no longer exist in the prepopulate data if 1052 // prepopulated engines that no longer exist in the prepopulate data if
1058 // it has been modified by the user. 1053 // it has been modified by the user.
1059 TEST_F(TemplateURLServiceTest, LoadSavesPrepopulatedDefaultSearchProvider) { 1054 TEST_F(TemplateURLServiceTest, LoadSavesPrepopulatedDefaultSearchProvider) {
1060 test_util_.VerifyLoad(); 1055 test_util_.VerifyLoad();
1061 // Verify that the default search provider is set to something. 1056 // Verify that the default search provider is set to something.
1062 ASSERT_TRUE(model()->GetDefaultSearchProvider() != NULL); 1057 const TemplateURL* default_search = model()->GetDefaultSearchProvider();
1063 TemplateURL default_url = *model()->GetDefaultSearchProvider(); 1058 ASSERT_TRUE(default_search != NULL);
1059 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(default_search->data()));
1064 1060
1065 // Wait for any saves to finish. 1061 // Wait for any saves to finish.
1066 test_util_.BlockTillServiceProcessesRequests(); 1062 test_util_.BlockTillServiceProcessesRequests();
1067 1063
1068 // Reload the model and check that the default search provider 1064 // Reload the model and check that the default search provider
1069 // was properly saved. 1065 // was properly saved.
1070 test_util_.ResetModel(true); 1066 test_util_.ResetModel(true);
1071 ASSERT_TRUE(model()->GetDefaultSearchProvider() != NULL); 1067 default_search = model()->GetDefaultSearchProvider();
1072 AssertEquals(default_url, *model()->GetDefaultSearchProvider()); 1068 ASSERT_TRUE(default_search != NULL);
1069 AssertEquals(*cloned_url, *default_search);
1073 } 1070 }
1074 1071
1075 // Make sure that the load routine doesn't delete 1072 // Make sure that the load routine doesn't delete
1076 // prepopulated engines that no longer exist in the prepopulate data if 1073 // prepopulated engines that no longer exist in the prepopulate data if
1077 // it is the default search provider. 1074 // it is the default search provider.
1078 TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) { 1075 TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) {
1079 // Set the default search provider to a preloaded template url which 1076 // Set the default search provider to a preloaded template url which
1080 // is not in the current set of preloaded template urls and save 1077 // is not in the current set of preloaded template urls and save
1081 // the result. 1078 // the result.
1082 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); 1079 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999);
1083 test_util_.ChangeModelToLoadState(); 1080 test_util_.ChangeModelToLoadState();
1084 model()->Add(t_url); 1081 model()->Add(t_url);
1085 model()->SetDefaultSearchProvider(t_url); 1082 model()->SetDefaultSearchProvider(t_url);
1086 // Do the copy after t_url is added and set as default so that its 1083 // Do the copy after t_url is added and set as default so that its
1087 // internal state is correct. 1084 // internal state is correct.
1088 TemplateURL copy_t_url = *t_url; 1085 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
1089 1086
1090 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"))); 1087 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")));
1091 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); 1088 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider());
1092 test_util_.BlockTillServiceProcessesRequests(); 1089 test_util_.BlockTillServiceProcessesRequests();
1093 1090
1094 // Ensure that merging won't clear the prepopulated template url 1091 // Ensure that merging won't clear the prepopulated template url
1095 // which is no longer present if it's the default engine. 1092 // which is no longer present if it's the default engine.
1096 test_util_.ResetModel(true); 1093 test_util_.ResetModel(true);
1097 { 1094 {
1098 const TemplateURL* keyword_url = 1095 const TemplateURL* keyword_url =
1099 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); 1096 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
1100 ASSERT_TRUE(keyword_url != NULL); 1097 ASSERT_TRUE(keyword_url != NULL);
1101 AssertEquals(copy_t_url, *keyword_url); 1098 AssertEquals(*cloned_url, *keyword_url);
1102 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider()); 1099 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider());
1103 } 1100 }
1104 1101
1105 // Wait for any saves to finish. 1102 // Wait for any saves to finish.
1106 test_util_.BlockTillServiceProcessesRequests(); 1103 test_util_.BlockTillServiceProcessesRequests();
1107 1104
1108 // Reload the model to verify that the update was saved. 1105 // Reload the model to verify that the update was saved.
1109 test_util_.ResetModel(true); 1106 test_util_.ResetModel(true);
1110 { 1107 {
1111 const TemplateURL* keyword_url = 1108 const TemplateURL* keyword_url =
1112 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); 1109 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
1113 ASSERT_TRUE(keyword_url != NULL); 1110 ASSERT_TRUE(keyword_url != NULL);
1114 AssertEquals(copy_t_url, *keyword_url); 1111 AssertEquals(*cloned_url, *keyword_url);
1115 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider()); 1112 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider());
1116 } 1113 }
1117 } 1114 }
1118 1115
1119 // Make sure that the load routine updates the url of a preexisting 1116 // Make sure that the load routine updates the url of a preexisting
1120 // default search engine provider and that the result is saved correctly. 1117 // default search engine provider and that the result is saved correctly.
1121 TEST_F(TemplateURLServiceTest, LoadUpdatesDefaultSearchURL) { 1118 TEST_F(TemplateURLServiceTest, LoadUpdatesDefaultSearchURL) {
1122 TestLoadUpdatingPreloadedURL(0); 1119 TestLoadUpdatingPreloadedURL(0);
1123 } 1120 }
1124 1121
1125 // Make sure that the load routine updates the url of a preexisting 1122 // Make sure that the load routine updates the url of a preexisting
1126 // non-default search engine provider and that the result is saved correctly. 1123 // non-default search engine provider and that the result is saved correctly.
1127 TEST_F(TemplateURLServiceTest, LoadUpdatesSearchURL) { 1124 TEST_F(TemplateURLServiceTest, LoadUpdatesSearchURL) {
1128 TestLoadUpdatingPreloadedURL(1); 1125 TestLoadUpdatingPreloadedURL(1);
1129 } 1126 }
1130 1127
1131 // Make sure that the load does update of auto-keywords correctly. 1128 // Make sure that the load does update of auto-keywords correctly.
1132 // This test basically verifies that no asserts or crashes occur 1129 // This test basically verifies that no asserts or crashes occur
1133 // during this operation. 1130 // during this operation.
1134 TEST_F(TemplateURLServiceTest, LoadDoesAutoKeywordUpdate) { 1131 TEST_F(TemplateURLServiceTest, LoadDoesAutoKeywordUpdate) {
1135 string16 prepopulated_url; 1132 string16 prepopulated_url;
1136 TemplateURL* t_url = CreateReplaceablePreloadedTemplateURL(false, 1133 scoped_ptr<TemplateURL> t_url(
1137 0, &prepopulated_url); 1134 CreateReplaceablePreloadedTemplateURL(false, 0, &prepopulated_url));
1138 t_url->SetURL("{google:baseURL}?q={searchTerms}"); 1135 TemplateURLData data(t_url->data());
1139 t_url->set_autogenerate_keyword(true); 1136 data.SetAutogenerateKeyword(true);
1137 data.SetURL("{google:baseURL}?q={searchTerms}");
1140 1138
1141 // Then add it to the model and save it all. 1139 // Then add it to the model and save it all.
1142 test_util_.ChangeModelToLoadState(); 1140 test_util_.ChangeModelToLoadState();
1143 model()->Add(t_url); 1141 model()->Add(new TemplateURL(data));
1144 test_util_.BlockTillServiceProcessesRequests(); 1142 test_util_.BlockTillServiceProcessesRequests();
1145 1143
1146 // Now reload the model and verify that the merge updates the url. 1144 // Now reload the model and verify that the merge updates the url.
1147 test_util_.ResetModel(true); 1145 test_util_.ResetModel(true);
1148 1146
1149 // Wait for any saves to finish. 1147 // Wait for any saves to finish.
1150 test_util_.BlockTillServiceProcessesRequests(); 1148 test_util_.BlockTillServiceProcessesRequests();
1151 } 1149 }
1152 1150
1153 // Simulates failing to load the webdb and makes sure the default search 1151 // Simulates failing to load the webdb and makes sure the default search
(...skipping 16 matching lines...) Expand all
1170 1168
1171 // Verifies that if the default search URL preference is managed, we report 1169 // Verifies that if the default search URL preference is managed, we report
1172 // the default search as managed. Also check that we are getting the right 1170 // the default search as managed. Also check that we are getting the right
1173 // values. 1171 // values.
1174 TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { 1172 TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) {
1175 test_util_.VerifyLoad(); 1173 test_util_.VerifyLoad();
1176 const size_t initial_count = model()->GetTemplateURLs().size(); 1174 const size_t initial_count = model()->GetTemplateURLs().size();
1177 test_util_.ResetObserverCount(); 1175 test_util_.ResetObserverCount();
1178 1176
1179 // Set a regular default search provider. 1177 // Set a regular default search provider.
1180 TemplateURL* regular_default = AddKeywordWithDate("key1", false, 1178 TemplateURL* regular_default = AddKeywordWithDate("name1", "key1", false,
1181 "http://foo1", "http://sugg1", "http://icon1", "UTF-8;UTF-16", "name1", 1179 "http://foo1", "http://sugg1", "http://icon1", true, "UTF-8;UTF-16",
1182 true, Time(), Time()); 1180 Time(), Time());
1183 VerifyObserverCount(1); 1181 VerifyObserverCount(1);
1184 model()->SetDefaultSearchProvider(regular_default); 1182 model()->SetDefaultSearchProvider(regular_default);
1185 // Adding the URL and setting the default search provider should have caused 1183 // Adding the URL and setting the default search provider should have caused
1186 // notifications. 1184 // notifications.
1187 VerifyObserverCount(1); 1185 VerifyObserverCount(1);
1188 EXPECT_FALSE(model()->is_default_search_managed()); 1186 EXPECT_FALSE(model()->is_default_search_managed());
1189 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1187 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1190 1188
1191 // Set a managed preference that establishes a default search provider. 1189 // Set a managed preference that establishes a default search provider.
1192 const char kName[] = "test1"; 1190 const char kName[] = "test1";
1193 const char kKeyword[] = "test.com"; 1191 const char kKeyword[] = "test.com";
1194 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; 1192 const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
1195 const char kIconURL[] = "http://test.com/icon.jpg"; 1193 const char kIconURL[] = "http://test.com/icon.jpg";
1196 const char kEncodings[] = "UTF-16;UTF-32"; 1194 const char kEncodings[] = "UTF-16;UTF-32";
1197 SetManagedDefaultSearchPreferences(true, kName, kSearchURL, std::string(), 1195 SetManagedDefaultSearchPreferences(true, kName, kKeyword, kSearchURL,
1198 kIconURL, kEncodings, kKeyword); 1196 std::string(), kIconURL, kEncodings);
1199 VerifyObserverFired(); 1197 VerifyObserverFired();
1200 EXPECT_TRUE(model()->is_default_search_managed()); 1198 EXPECT_TRUE(model()->is_default_search_managed());
1201 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); 1199 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
1202 1200
1203 // Verify that the default manager we are getting is the managed one. 1201 // Verify that the default manager we are getting is the managed one.
1204 scoped_ptr<TemplateURL> expected_managed_default1(new TemplateURL()); 1202 TemplateURLData data;
1205 expected_managed_default1->set_short_name(ASCIIToUTF16(kName)); 1203 data.short_name = ASCIIToUTF16(kName);
1206 expected_managed_default1->set_keyword(ASCIIToUTF16(kKeyword)); 1204 data.SetKeyword(ASCIIToUTF16(kKeyword));
1207 expected_managed_default1->set_show_in_default_list(true); 1205 data.SetURL(kSearchURL);
1208 std::vector<std::string> encodings_vector; 1206 data.favicon_url = GURL(kIconURL);
1209 base::SplitString(kEncodings, ';', &encodings_vector); 1207 data.show_in_default_list = true;
1210 expected_managed_default1->set_input_encodings(encodings_vector); 1208 base::SplitString(kEncodings, ';', &data.input_encodings);
1211 expected_managed_default1->SetURL(kSearchURL); 1209 scoped_ptr<TemplateURL> expected_managed_default1(new TemplateURL(data));
1212 expected_managed_default1->set_favicon_url(GURL(kIconURL));
1213 const TemplateURL* actual_managed_default = 1210 const TemplateURL* actual_managed_default =
1214 model()->GetDefaultSearchProvider(); 1211 model()->GetDefaultSearchProvider();
1215 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); 1212 ExpectSimilar(expected_managed_default1.get(), actual_managed_default);
1216 EXPECT_TRUE(actual_managed_default->show_in_default_list()); 1213 EXPECT_TRUE(actual_managed_default->show_in_default_list());
1217 1214
1218 // Update the managed preference and check that the model has changed. 1215 // Update the managed preference and check that the model has changed.
1219 const char kNewName[] = "test2"; 1216 const char kNewName[] = "test2";
1220 const char kNewKeyword[] = "other.com"; 1217 const char kNewKeyword[] = "other.com";
1221 const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}"; 1218 const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}";
1222 const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}"; 1219 const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}";
1223 SetManagedDefaultSearchPreferences(true, kNewName, kNewSearchURL, 1220 SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword, kNewSearchURL,
1224 kNewSuggestURL, std::string(), std::string(), kNewKeyword); 1221 kNewSuggestURL, std::string(), std::string());
1225 VerifyObserverFired(); 1222 VerifyObserverFired();
1226 EXPECT_TRUE(model()->is_default_search_managed()); 1223 EXPECT_TRUE(model()->is_default_search_managed());
1227 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); 1224 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
1228 1225
1229 // Verify that the default manager we are now getting is the correct one. 1226 // Verify that the default manager we are now getting is the correct one.
1230 scoped_ptr<TemplateURL> expected_managed_default2(new TemplateURL()); 1227 TemplateURLData data2;
1231 expected_managed_default2->set_short_name(ASCIIToUTF16(kNewName)); 1228 data2.short_name = ASCIIToUTF16(kNewName);
1232 expected_managed_default2->set_keyword(ASCIIToUTF16(kNewKeyword)); 1229 data2.SetKeyword(ASCIIToUTF16(kNewKeyword));
1233 expected_managed_default2->set_show_in_default_list(true); 1230 data2.SetURL(kNewSearchURL);
1234 expected_managed_default2->SetSuggestionsURL(kNewSuggestURL); 1231 data2.suggestions_url = kNewSuggestURL;
1235 expected_managed_default2->SetURL(kNewSearchURL); 1232 data2.show_in_default_list = true;
1233 scoped_ptr<TemplateURL> expected_managed_default2(new TemplateURL(data2));
1236 actual_managed_default = model()->GetDefaultSearchProvider(); 1234 actual_managed_default = model()->GetDefaultSearchProvider();
1237 ExpectSimilar(expected_managed_default2.get(), actual_managed_default); 1235 ExpectSimilar(expected_managed_default2.get(), actual_managed_default);
1238 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); 1236 EXPECT_EQ(actual_managed_default->show_in_default_list(), true);
1239 1237
1240 // Remove all the managed prefs and check that we are no longer managed. 1238 // Remove all the managed prefs and check that we are no longer managed.
1241 RemoveManagedDefaultSearchPreferences(); 1239 RemoveManagedDefaultSearchPreferences();
1242 VerifyObserverFired(); 1240 VerifyObserverFired();
1243 EXPECT_FALSE(model()->is_default_search_managed()); 1241 EXPECT_FALSE(model()->is_default_search_managed());
1244 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1242 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1245 1243
1246 // The default should now be the first URL added 1244 // The default should now be the first URL added
1247 const TemplateURL* actual_final_managed_default = 1245 const TemplateURL* actual_final_managed_default =
1248 model()->GetDefaultSearchProvider(); 1246 model()->GetDefaultSearchProvider();
1249 ExpectSimilar(model()->GetTemplateURLs()[0], actual_final_managed_default); 1247 ExpectSimilar(model()->GetTemplateURLs()[0], actual_final_managed_default);
1250 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true); 1248 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true);
1251 1249
1252 // Disable the default search provider through policy. 1250 // Disable the default search provider through policy.
1253 SetManagedDefaultSearchPreferences(false, std::string(), std::string(), 1251 SetManagedDefaultSearchPreferences(false, std::string(), std::string(),
1254 std::string(), std::string(), std::string(), std::string()); 1252 std::string(), std::string(), std::string(), std::string());
1255 VerifyObserverFired(); 1253 VerifyObserverFired();
1256 EXPECT_TRUE(model()->is_default_search_managed()); 1254 EXPECT_TRUE(model()->is_default_search_managed());
1257 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider()); 1255 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider());
1258 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1256 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1259 1257
1260 // Re-enable it. 1258 // Re-enable it.
1261 SetManagedDefaultSearchPreferences(true, kName, kSearchURL, std::string(), 1259 SetManagedDefaultSearchPreferences(true, kName, kKeyword, kSearchURL,
1262 kIconURL, kEncodings, kKeyword); 1260 std::string(), kIconURL, kEncodings);
1263 VerifyObserverFired(); 1261 VerifyObserverFired();
1264 EXPECT_TRUE(model()->is_default_search_managed()); 1262 EXPECT_TRUE(model()->is_default_search_managed());
1265 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size()); 1263 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
1266 1264
1267 // Verify that the default manager we are getting is the managed one. 1265 // Verify that the default manager we are getting is the managed one.
1268 actual_managed_default = model()->GetDefaultSearchProvider(); 1266 actual_managed_default = model()->GetDefaultSearchProvider();
1269 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); 1267 ExpectSimilar(expected_managed_default1.get(), actual_managed_default);
1270 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); 1268 EXPECT_EQ(actual_managed_default->show_in_default_list(), true);
1271 1269
1272 // Clear the model and disable the default search provider through policy. 1270 // Clear the model and disable the default search provider through policy.
1273 // Verify that there is no default search provider after loading the model. 1271 // Verify that there is no default search provider after loading the model.
1274 // This checks against regressions of http://crbug.com/67180 1272 // This checks against regressions of http://crbug.com/67180
1275 1273
1276 // First, remove the preferences, reset the model, and set a default. 1274 // First, remove the preferences, reset the model, and set a default.
1277 RemoveManagedDefaultSearchPreferences(); 1275 RemoveManagedDefaultSearchPreferences();
1278 test_util_.ResetModel(true); 1276 test_util_.ResetModel(true);
1279 TemplateURL* t_url = AddKeywordWithDate("key1", false, "http://foo1", 1277 TemplateURL* t_url = AddKeywordWithDate("name1", "key1", false, "http://foo1",
1280 "http://sugg1", "http://icon1", "UTF-8;UTF-16", "name1", true, Time(), 1278 "http://sugg1", "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
1281 Time());
1282 model()->SetDefaultSearchProvider(t_url); 1279 model()->SetDefaultSearchProvider(t_url);
1283 EXPECT_EQ(t_url, model()->GetDefaultSearchProvider()); 1280 EXPECT_EQ(t_url, model()->GetDefaultSearchProvider());
1284 1281
1285 // Now reset the model again but load it after setting the preferences. 1282 // Now reset the model again but load it after setting the preferences.
1286 test_util_.ResetModel(false); 1283 test_util_.ResetModel(false);
1287 SetManagedDefaultSearchPreferences(false, std::string(), std::string(), 1284 SetManagedDefaultSearchPreferences(false, std::string(), std::string(),
1288 std::string(), std::string(), std::string(), std::string()); 1285 std::string(), std::string(), std::string(), std::string());
1289 test_util_.VerifyLoad(); 1286 test_util_.VerifyLoad();
1290 EXPECT_TRUE(model()->is_default_search_managed()); 1287 EXPECT_TRUE(model()->is_default_search_managed());
1291 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL); 1288 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL);
1292 } 1289 }
1293 1290
1294 // Test that if we load a TemplateURL with an empty GUID, the load process 1291 // Test that if we load a TemplateURL with an empty GUID, the load process
1295 // assigns it a newly generated GUID. 1292 // assigns it a newly generated GUID.
1296 TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) { 1293 TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) {
1297 // Add a new TemplateURL. 1294 // Add a new TemplateURL.
1298 test_util_.VerifyLoad(); 1295 test_util_.VerifyLoad();
1299 const size_t initial_count = model()->GetTemplateURLs().size(); 1296 const size_t initial_count = model()->GetTemplateURLs().size();
1300 1297
1301 TemplateURL* t_url = new TemplateURL(); 1298 TemplateURLData data;
1302 t_url->set_short_name(ASCIIToUTF16("google")); 1299 data.short_name = ASCIIToUTF16("google");
1303 t_url->set_keyword(ASCIIToUTF16("keyword")); 1300 data.SetKeyword(ASCIIToUTF16("keyword"));
1304 t_url->set_sync_guid(std::string()); 1301 data.SetURL("http://www.google.com/foo/bar");
1305 t_url->SetURL("http://www.google.com/foo/bar"); 1302 data.sync_guid.clear();
1303 TemplateURL* t_url = new TemplateURL(data);
1306 model()->Add(t_url); 1304 model()->Add(t_url);
1307 1305
1308 VerifyObserverCount(1); 1306 VerifyObserverCount(1);
1309 test_util_.BlockTillServiceProcessesRequests(); 1307 test_util_.BlockTillServiceProcessesRequests();
1310 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1308 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1311 1309
1312 // Reload the model to verify it was actually saved to the database and 1310 // Reload the model to verify it was actually saved to the database and
1313 // assigned a new GUID when brought back. 1311 // assigned a new GUID when brought back.
1314 test_util_.ResetModel(true); 1312 test_util_.ResetModel(true);
1315 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); 1313 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1316 const TemplateURL* loaded_url = 1314 const TemplateURL* loaded_url =
1317 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); 1315 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
1318 ASSERT_TRUE(loaded_url != NULL); 1316 ASSERT_TRUE(loaded_url != NULL);
1319 ASSERT_FALSE(loaded_url->sync_guid().empty()); 1317 ASSERT_FALSE(loaded_url->sync_guid().empty());
1320 } 1318 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698