OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <windows.h> | |
6 | |
7 #include "base/file_util.h" | 5 #include "base/file_util.h" |
8 #include "base/logging.h" | 6 #include "base/logging.h" |
9 #include "base/path_service.h" | 7 #include "base/path_service.h" |
10 #include "base/string_util.h" | 8 #include "base/string_util.h" |
11 #include "chrome/browser/template_url.h" | 9 #include "chrome/browser/template_url.h" |
12 #include "chrome/browser/template_url_model.h" | 10 #include "chrome/browser/template_url_model.h" |
13 #include "chrome/common/pref_service.h" | 11 #include "chrome/common/pref_service.h" |
14 #include "chrome/test/testing_profile.h" | 12 #include "chrome/test/testing_profile.h" |
15 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 22 matching lines...) Expand all Loading... |
39 public: | 37 public: |
40 TemplateURLModelTestingProfile() : TestingProfile() { } | 38 TemplateURLModelTestingProfile() : TestingProfile() { } |
41 | 39 |
42 void SetUp() { | 40 void SetUp() { |
43 // Name a subdirectory of the temp directory. | 41 // Name a subdirectory of the temp directory. |
44 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | 42 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
45 file_util::AppendToPath(&test_dir_, L"TemplateURLModelTest"); | 43 file_util::AppendToPath(&test_dir_, L"TemplateURLModelTest"); |
46 | 44 |
47 // Create a fresh, empty copy of this directory. | 45 // Create a fresh, empty copy of this directory. |
48 file_util::Delete(test_dir_, true); | 46 file_util::Delete(test_dir_, true); |
49 CreateDirectory(test_dir_.c_str(), NULL); | 47 file_util::CreateDirectory(test_dir_); |
50 | 48 |
51 std::wstring path = test_dir_; | 49 std::wstring path = test_dir_; |
52 file_util::AppendToPath(&path, L"TestDataService.db"); | 50 file_util::AppendToPath(&path, L"TestDataService.db"); |
53 service_ = new WebDataService; | 51 service_ = new WebDataService; |
54 EXPECT_TRUE(service_->InitWithPath(path)); | 52 EXPECT_TRUE(service_->InitWithPath(path)); |
55 } | 53 } |
56 | 54 |
57 void TearDown() { | 55 void TearDown() { |
58 // Clean up the test directory. | 56 // Clean up the test directory. |
59 service_->Shutdown(); | 57 service_->Shutdown(); |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 ASSERT_EQ(L"blah", TemplateURLModel::GenerateKeyword(GURL("http://blah/"), | 281 ASSERT_EQ(L"blah", TemplateURLModel::GenerateKeyword(GURL("http://blah/"), |
284 true)); | 282 true)); |
285 } | 283 } |
286 | 284 |
287 TEST_F(TemplateURLModelTest, ClearBrowsingData_Keywords) { | 285 TEST_F(TemplateURLModelTest, ClearBrowsingData_Keywords) { |
288 Time now = Time::Now(); | 286 Time now = Time::Now(); |
289 TimeDelta one_day = TimeDelta::FromDays(1); | 287 TimeDelta one_day = TimeDelta::FromDays(1); |
290 Time month_ago = now - TimeDelta::FromDays(30); | 288 Time month_ago = now - TimeDelta::FromDays(30); |
291 | 289 |
292 // Nothing has been added. | 290 // Nothing has been added. |
293 EXPECT_EQ(0, model_->GetTemplateURLs().size()); | 291 EXPECT_EQ(0U, model_->GetTemplateURLs().size()); |
294 | 292 |
295 // Create one with a 0 time. | 293 // Create one with a 0 time. |
296 AddKeywordWithDate(L"key1", false, L"http://foo1", L"name1", true, Time()); | 294 AddKeywordWithDate(L"key1", false, L"http://foo1", L"name1", true, Time()); |
297 // Create one for now and +/- 1 day. | 295 // Create one for now and +/- 1 day. |
298 AddKeywordWithDate(L"key2", false, L"http://foo2", L"name2", true, | 296 AddKeywordWithDate(L"key2", false, L"http://foo2", L"name2", true, |
299 now - one_day); | 297 now - one_day); |
300 AddKeywordWithDate(L"key3", false, L"http://foo3", L"name3", true, now); | 298 AddKeywordWithDate(L"key3", false, L"http://foo3", L"name3", true, now); |
301 AddKeywordWithDate(L"key4", false, L"http://foo4", L"name4", true, | 299 AddKeywordWithDate(L"key4", false, L"http://foo4", L"name4", true, |
302 now + one_day); | 300 now + one_day); |
303 // Try the other three states. | 301 // Try the other three states. |
304 AddKeywordWithDate(L"key5", false, L"http://foo5", L"name5", false, now); | 302 AddKeywordWithDate(L"key5", false, L"http://foo5", L"name5", false, now); |
305 AddKeywordWithDate(L"key6", false, L"http://foo6", L"name6", false, month_ago)
; | 303 AddKeywordWithDate(L"key6", false, L"http://foo6", L"name6", false, month_ago)
; |
306 | 304 |
307 // We just added a few items, validate them. | 305 // We just added a few items, validate them. |
308 EXPECT_EQ(6, model_->GetTemplateURLs().size()); | 306 EXPECT_EQ(6U, model_->GetTemplateURLs().size()); |
309 | 307 |
310 // Try removing from current timestamp. This should delete the one in the | 308 // Try removing from current timestamp. This should delete the one in the |
311 // future and one very recent one. | 309 // future and one very recent one. |
312 model_->RemoveAutoGeneratedSince(now); | 310 model_->RemoveAutoGeneratedSince(now); |
313 EXPECT_EQ(4, model_->GetTemplateURLs().size()); | 311 EXPECT_EQ(4U, model_->GetTemplateURLs().size()); |
314 | 312 |
315 // Try removing from two months ago. This should only delete items that are | 313 // Try removing from two months ago. This should only delete items that are |
316 // auto-generated. | 314 // auto-generated. |
317 model_->RemoveAutoGeneratedSince(now - TimeDelta::FromDays(60)); | 315 model_->RemoveAutoGeneratedSince(now - TimeDelta::FromDays(60)); |
318 EXPECT_EQ(3, model_->GetTemplateURLs().size()); | 316 EXPECT_EQ(3U, model_->GetTemplateURLs().size()); |
319 | 317 |
320 // Make sure the right values remain. | 318 // Make sure the right values remain. |
321 EXPECT_EQ(L"key1", model_->GetTemplateURLs()[0]->keyword()); | 319 EXPECT_EQ(L"key1", model_->GetTemplateURLs()[0]->keyword()); |
322 EXPECT_TRUE(model_->GetTemplateURLs()[0]->safe_for_autoreplace()); | 320 EXPECT_TRUE(model_->GetTemplateURLs()[0]->safe_for_autoreplace()); |
323 EXPECT_EQ(0, model_->GetTemplateURLs()[0]->date_created().ToInternalValue()); | 321 EXPECT_EQ(0U, model_->GetTemplateURLs()[0]->date_created().ToInternalValue()); |
324 | 322 |
325 EXPECT_EQ(L"key5", model_->GetTemplateURLs()[1]->keyword()); | 323 EXPECT_EQ(L"key5", model_->GetTemplateURLs()[1]->keyword()); |
326 EXPECT_FALSE(model_->GetTemplateURLs()[1]->safe_for_autoreplace()); | 324 EXPECT_FALSE(model_->GetTemplateURLs()[1]->safe_for_autoreplace()); |
327 EXPECT_EQ(now.ToInternalValue(), | 325 EXPECT_EQ(now.ToInternalValue(), |
328 model_->GetTemplateURLs()[1]->date_created().ToInternalValue()); | 326 model_->GetTemplateURLs()[1]->date_created().ToInternalValue()); |
329 | 327 |
330 EXPECT_EQ(L"key6", model_->GetTemplateURLs()[2]->keyword()); | 328 EXPECT_EQ(L"key6", model_->GetTemplateURLs()[2]->keyword()); |
331 EXPECT_FALSE(model_->GetTemplateURLs()[2]->safe_for_autoreplace()); | 329 EXPECT_FALSE(model_->GetTemplateURLs()[2]->safe_for_autoreplace()); |
332 EXPECT_EQ(month_ago.ToInternalValue(), | 330 EXPECT_EQ(month_ago.ToInternalValue(), |
333 model_->GetTemplateURLs()[2]->date_created().ToInternalValue()); | 331 model_->GetTemplateURLs()[2]->date_created().ToInternalValue()); |
334 | 332 |
335 // Try removing from Time=0. This should delete one more. | 333 // Try removing from Time=0. This should delete one more. |
336 model_->RemoveAutoGeneratedSince(Time()); | 334 model_->RemoveAutoGeneratedSince(Time()); |
337 EXPECT_EQ(2, model_->GetTemplateURLs().size()); | 335 EXPECT_EQ(2U, model_->GetTemplateURLs().size()); |
338 } | 336 } |
339 | 337 |
340 TEST_F(TemplateURLModelTest, Reset) { | 338 TEST_F(TemplateURLModelTest, Reset) { |
341 // Add a new TemplateURL. | 339 // Add a new TemplateURL. |
342 VerifyLoad(); | 340 VerifyLoad(); |
343 const size_t initial_count = model_->GetTemplateURLs().size(); | 341 const size_t initial_count = model_->GetTemplateURLs().size(); |
344 TemplateURL* t_url = new TemplateURL(); | 342 TemplateURL* t_url = new TemplateURL(); |
345 t_url->SetURL(L"http://www.google.com/foo/bar", 0, 0); | 343 t_url->SetURL(L"http://www.google.com/foo/bar", 0, 0); |
346 t_url->set_keyword(L"keyword"); | 344 t_url->set_keyword(L"keyword"); |
347 t_url->set_short_name(L"google"); | 345 t_url->set_short_name(L"google"); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 // Query with key occurring multiple times should return false. | 537 // Query with key occurring multiple times should return false. |
540 { "http://blah/foo?x=y&x=z", false, "", "" }, | 538 { "http://blah/foo?x=y&x=z", false, "", "" }, |
541 | 539 |
542 { "http://blah/foo?x=y", true, "x", "y" }, | 540 { "http://blah/foo?x=y", true, "x", "y" }, |
543 { "http://blah/foo?x=y&y=z", true, "x;y", "y;z" }, | 541 { "http://blah/foo?x=y&y=z", true, "x;y", "y;z" }, |
544 | 542 |
545 // Key occurring multiple times should get an empty string. | 543 // Key occurring multiple times should get an empty string. |
546 { "http://blah/foo?x=y&x=z&y=z", true, "x;y", ";z" }, | 544 { "http://blah/foo?x=y&x=z&y=z", true, "x;y", ";z" }, |
547 }; | 545 }; |
548 | 546 |
549 for (size_t i = 0; i < arraysize(data); ++i) { | 547 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
550 TemplateURLModel::QueryTerms terms; | 548 TemplateURLModel::QueryTerms terms; |
551 ASSERT_EQ(data[i].result, | 549 ASSERT_EQ(data[i].result, |
552 TemplateURLModel::BuildQueryTerms(GURL(data[i].url), &terms)); | 550 TemplateURLModel::BuildQueryTerms(GURL(data[i].url), &terms)); |
553 if (data[i].result) { | 551 if (data[i].result) { |
554 std::vector<std::string> keys; | 552 std::vector<std::string> keys; |
555 std::vector<std::string> values; | 553 std::vector<std::string> values; |
556 SplitString(data[i].keys, ';', &keys); | 554 SplitString(data[i].keys, ';', &keys); |
557 SplitString(data[i].values, ';', &values); | 555 SplitString(data[i].values, ';', &values); |
558 ASSERT_TRUE(keys.size() == values.size()); | 556 ASSERT_TRUE(keys.size() == values.size()); |
559 ASSERT_EQ(keys.size(), terms.size()); | 557 ASSERT_EQ(keys.size(), terms.size()); |
(...skipping 17 matching lines...) Expand all Loading... |
577 { "http://x/bar?q=xx", L"" }, | 575 { "http://x/bar?q=xx", L"" }, |
578 { "http://x/foo?y=xx", L"" }, | 576 { "http://x/foo?y=xx", L"" }, |
579 { "http://x/foo?q=xx", L"xx" }, | 577 { "http://x/foo?q=xx", L"xx" }, |
580 { "http://x/foo?a=b&q=xx", L"xx" }, | 578 { "http://x/foo?a=b&q=xx", L"xx" }, |
581 { "http://x/foo?q=b&q=xx", L"" }, | 579 { "http://x/foo?q=b&q=xx", L"" }, |
582 }; | 580 }; |
583 | 581 |
584 AddKeywordWithDate(L"x", false, L"http://x/foo?q={searchTerms}", L"name", | 582 AddKeywordWithDate(L"x", false, L"http://x/foo?q={searchTerms}", L"name", |
585 false, Time()); | 583 false, Time()); |
586 | 584 |
587 for (size_t i = 0; i < arraysize(data); ++i) { | 585 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
588 model_->UpdateKeywordSearchTermsForURL(history::URLRow(GURL(data[i].url))); | 586 model_->UpdateKeywordSearchTermsForURL(history::URLRow(GURL(data[i].url))); |
589 EXPECT_EQ(data[i].term, GetAndClearSearchTerm()); | 587 EXPECT_EQ(data[i].term, GetAndClearSearchTerm()); |
590 } | 588 } |
591 } | 589 } |
592 | 590 |
593 TEST_F(TemplateURLModelTest, DontUpdateKeywordSearchForNonReplaceable) { | 591 TEST_F(TemplateURLModelTest, DontUpdateKeywordSearchForNonReplaceable) { |
594 struct TestData { | 592 struct TestData { |
595 const std::string url; | 593 const std::string url; |
596 } data[] = { | 594 } data[] = { |
597 { "http://foo/" }, | 595 { "http://foo/" }, |
598 { "http://x/bar?q=xx" }, | 596 { "http://x/bar?q=xx" }, |
599 { "http://x/foo?y=xx" }, | 597 { "http://x/foo?y=xx" }, |
600 }; | 598 }; |
601 | 599 |
602 AddKeywordWithDate(L"x", false, L"http://x/foo", L"name", false, Time()); | 600 AddKeywordWithDate(L"x", false, L"http://x/foo", L"name", false, Time()); |
603 | 601 |
604 for (size_t i = 0; i < arraysize(data); ++i) { | 602 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
605 model_->UpdateKeywordSearchTermsForURL(history::URLRow(GURL(data[i].url))); | 603 model_->UpdateKeywordSearchTermsForURL(history::URLRow(GURL(data[i].url))); |
606 ASSERT_EQ(std::wstring(), GetAndClearSearchTerm()); | 604 ASSERT_EQ(std::wstring(), GetAndClearSearchTerm()); |
607 } | 605 } |
608 } | 606 } |
609 | 607 |
610 TEST_F(TemplateURLModelTest, ChangeGoogleBaseValue) { | 608 TEST_F(TemplateURLModelTest, ChangeGoogleBaseValue) { |
611 // NOTE: Do not do a VerifyLoad() here as it will load the prepopulate data, | 609 // NOTE: Do not do a VerifyLoad() here as it will load the prepopulate data, |
612 // which also has a {google:baseURL} keyword in it, which will confuse this | 610 // which also has a {google:baseURL} keyword in it, which will confuse this |
613 // test. | 611 // test. |
614 SetGoogleBaseURL(L"http://google.com/"); | 612 SetGoogleBaseURL(L"http://google.com/"); |
(...skipping 11 matching lines...) Expand all Loading... |
626 VerifyObserverCount(1); | 624 VerifyObserverCount(1); |
627 | 625 |
628 // Make sure the host->TemplateURL map was updated appropriately. | 626 // Make sure the host->TemplateURL map was updated appropriately. |
629 ASSERT_EQ(t_url, model_->GetTemplateURLForHost("foo.com")); | 627 ASSERT_EQ(t_url, model_->GetTemplateURLForHost("foo.com")); |
630 EXPECT_TRUE(model_->GetTemplateURLForHost("google.com") == NULL); | 628 EXPECT_TRUE(model_->GetTemplateURLForHost("google.com") == NULL); |
631 EXPECT_EQ("foo.com", t_url->url()->GetHost()); | 629 EXPECT_EQ("foo.com", t_url->url()->GetHost()); |
632 EXPECT_EQ(L"foo.com", t_url->keyword()); | 630 EXPECT_EQ(L"foo.com", t_url->keyword()); |
633 EXPECT_EQ("http://foo.com/?q=x", t_url->url()->ReplaceSearchTerms(*t_url, | 631 EXPECT_EQ("http://foo.com/?q=x", t_url->url()->ReplaceSearchTerms(*t_url, |
634 L"x", TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()).spec()); | 632 L"x", TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()).spec()); |
635 } | 633 } |
OLD | NEW |