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

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

Issue 7558014: Add a URL param to indicate group selection in Instant field trial. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Thread safe handling of Profiles Created 9 years, 4 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/callback.h" 5 #include "base/callback.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_vector.h" 7 #include "base/memory/scoped_vector.h"
8 #include "base/test/mock_time_provider.h" 8 #include "base/test/mock_time_provider.h"
9 #include "base/string_split.h" 9 #include "base/string_split.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
(...skipping 19 matching lines...) Expand all
30 30
31 using base::Time; 31 using base::Time;
32 using base::TimeDelta; 32 using base::TimeDelta;
33 using ::testing::Return; 33 using ::testing::Return;
34 using ::testing::StrictMock; 34 using ::testing::StrictMock;
35 35
36 // Test the GenerateSearchURL on a thread or the main thread. 36 // Test the GenerateSearchURL on a thread or the main thread.
37 class TestGenerateSearchURL 37 class TestGenerateSearchURL
38 : public base::RefCountedThreadSafe<TestGenerateSearchURL> { 38 : public base::RefCountedThreadSafe<TestGenerateSearchURL> {
39 public: 39 public:
40 explicit TestGenerateSearchURL(SearchTermsData* search_terms_data) 40 TestGenerateSearchURL(Profile* profile, SearchTermsData* search_terms_data)
41 : search_terms_data_(search_terms_data), 41 : profile_(profile),
42 search_terms_data_(search_terms_data),
42 passed_(false) { 43 passed_(false) {
43 } 44 }
44 45
45 // Run the test cases for GenerateSearchURL. 46 // Run the test cases for GenerateSearchURL.
46 void RunTest(); 47 void RunTest();
47 48
48 // Did the test pass? 49 // Did the test pass?
49 bool passed() const { return passed_; } 50 bool passed() const { return passed_; }
50 51
51 private: 52 private:
52 friend class base::RefCountedThreadSafe<TestGenerateSearchURL>; 53 friend class base::RefCountedThreadSafe<TestGenerateSearchURL>;
53 ~TestGenerateSearchURL() {} 54 ~TestGenerateSearchURL() {}
54 55
56 Profile* const profile_;
55 SearchTermsData* search_terms_data_; 57 SearchTermsData* search_terms_data_;
56 bool passed_; 58 bool passed_;
57 59
58 DISALLOW_COPY_AND_ASSIGN(TestGenerateSearchURL); 60 DISALLOW_COPY_AND_ASSIGN(TestGenerateSearchURL);
59 }; 61 };
60 62
61 // Simple implementation of SearchTermsData. 63 // Simple implementation of SearchTermsData.
62 class TestSearchTermsData : public SearchTermsData { 64 class TestSearchTermsData : public SearchTermsData {
63 public: 65 public:
64 explicit TestSearchTermsData(const char* google_base_url) 66 explicit TestSearchTermsData(const char* google_base_url)
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 // gtest documentation. 308 // gtest documentation.
307 bool everything_passed = true; 309 bool everything_passed = true;
308 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generate_url_cases); ++i) { 310 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generate_url_cases); ++i) {
309 TemplateURL t_url; 311 TemplateURL t_url;
310 if (generate_url_cases[i].url) 312 if (generate_url_cases[i].url)
311 t_url.SetURL(generate_url_cases[i].url, 0, 0); 313 t_url.SetURL(generate_url_cases[i].url, 0, 0);
312 314
313 std::string result = search_terms_data_ ? 315 std::string result = search_terms_data_ ?
314 TemplateURLService::GenerateSearchURLUsingTermsData( 316 TemplateURLService::GenerateSearchURLUsingTermsData(
315 &t_url, *search_terms_data_).spec() : 317 &t_url, *search_terms_data_).spec() :
316 TemplateURLService::GenerateSearchURL(&t_url).spec(); 318 TemplateURLService::GenerateSearchURL(&t_url, profile_).spec();
317 if (strcmp(generate_url_cases[i].expected, result.c_str())) { 319 if (strcmp(generate_url_cases[i].expected, result.c_str())) {
318 LOG(ERROR) << generate_url_cases[i].test_name << " failed. Expected " << 320 LOG(ERROR) << generate_url_cases[i].test_name << " failed. Expected " <<
319 generate_url_cases[i].expected << " Actual " << result; 321 generate_url_cases[i].expected << " Actual " << result;
320 322
321 everything_passed = false; 323 everything_passed = false;
322 } 324 }
323 } 325 }
324 passed_ = everything_passed; 326 passed_ = everything_passed;
325 } 327 }
326 328
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 // FTP shouldn't generate a keyword. 478 // FTP shouldn't generate a keyword.
477 ASSERT_EQ(string16(), 479 ASSERT_EQ(string16(),
478 TemplateURLService::GenerateKeyword(GURL("ftp://blah/"), true)); 480 TemplateURLService::GenerateKeyword(GURL("ftp://blah/"), true));
479 // Make sure we don't get a trailing / 481 // Make sure we don't get a trailing /
480 ASSERT_EQ(ASCIIToUTF16("blah"), 482 ASSERT_EQ(ASCIIToUTF16("blah"),
481 TemplateURLService::GenerateKeyword(GURL("http://blah/"), true)); 483 TemplateURLService::GenerateKeyword(GURL("http://blah/"), true));
482 } 484 }
483 485
484 TEST_F(TemplateURLServiceTest, GenerateSearchURL) { 486 TEST_F(TemplateURLServiceTest, GenerateSearchURL) {
485 scoped_refptr<TestGenerateSearchURL> test_generate_search_url( 487 scoped_refptr<TestGenerateSearchURL> test_generate_search_url(
486 new TestGenerateSearchURL(NULL)); 488 new TestGenerateSearchURL(profile(), NULL));
487 test_generate_search_url->RunTest(); 489 test_generate_search_url->RunTest();
488 EXPECT_TRUE(test_generate_search_url->passed()); 490 EXPECT_TRUE(test_generate_search_url->passed());
489 } 491 }
490 492
491 TEST_F(TemplateURLServiceTest, GenerateSearchURLUsingTermsData) { 493 TEST_F(TemplateURLServiceTest, GenerateSearchURLUsingTermsData) {
492 // Run the test for GenerateSearchURLUsingTermsData on the "IO" thread and 494 // Run the test for GenerateSearchURLUsingTermsData on the "IO" thread and
493 // wait for it to finish. 495 // wait for it to finish.
494 TestSearchTermsData search_terms_data("http://google.com/"); 496 TestSearchTermsData search_terms_data("http://google.com/");
495 scoped_refptr<TestGenerateSearchURL> test_generate_search_url( 497 scoped_refptr<TestGenerateSearchURL> test_generate_search_url(
496 new TestGenerateSearchURL(&search_terms_data)); 498 new TestGenerateSearchURL(profile(), &search_terms_data));
497 499
498 test_util_.StartIOThread(); 500 test_util_.StartIOThread();
499 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)->PostTask( 501 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)->PostTask(
500 FROM_HERE, 502 FROM_HERE,
501 NewRunnableMethod(test_generate_search_url.get(), 503 NewRunnableMethod(test_generate_search_url.get(),
502 &TestGenerateSearchURL::RunTest)); 504 &TestGenerateSearchURL::RunTest));
503 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); 505 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests();
504 EXPECT_TRUE(test_generate_search_url->passed()); 506 EXPECT_TRUE(test_generate_search_url->passed());
505 } 507 }
506 508
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 // Change the Google base url. 886 // Change the Google base url.
885 test_util_.ResetObserverCount(); 887 test_util_.ResetObserverCount();
886 SetGoogleBaseURL("http://foo.com/"); 888 SetGoogleBaseURL("http://foo.com/");
887 VerifyObserverCount(1); 889 VerifyObserverCount(1);
888 890
889 // Make sure the host->TemplateURL map was updated appropriately. 891 // Make sure the host->TemplateURL map was updated appropriately.
890 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("foo.com")); 892 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("foo.com"));
891 EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL); 893 EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL);
892 EXPECT_EQ("foo.com", t_url->url()->GetHost()); 894 EXPECT_EQ("foo.com", t_url->url()->GetHost());
893 EXPECT_EQ(ASCIIToUTF16("foo.com"), t_url->keyword()); 895 EXPECT_EQ(ASCIIToUTF16("foo.com"), t_url->keyword());
894 EXPECT_EQ("http://foo.com/?q=x", t_url->url()->ReplaceSearchTerms(*t_url, 896 EXPECT_EQ("http://foo.com/?q=x", t_url->url()->ReplaceSearchTerms(profile(),
Peter Kasting 2011/08/10 20:54:06 This call and the one below can pass NULL since yo
895 ASCIIToUTF16("x"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); 897 *t_url, ASCIIToUTF16("x"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
898 string16()));
896 } 899 }
897 900
898 struct QueryHistoryCallbackImpl { 901 struct QueryHistoryCallbackImpl {
899 QueryHistoryCallbackImpl() : success(false) {} 902 QueryHistoryCallbackImpl() : success(false) {}
900 903
901 void Callback(HistoryService::Handle handle, 904 void Callback(HistoryService::Handle handle,
902 bool success, const history::URLRow* row, 905 bool success, const history::URLRow* row,
903 history::VisitVector* visits) { 906 history::VisitVector* visits) {
904 this->success = success; 907 this->success = success;
905 if (row) 908 if (row)
(...skipping 16 matching lines...) Expand all
922 // Create a keyword. 925 // Create a keyword.
923 TemplateURL* t_url = AddKeywordWithDate( 926 TemplateURL* t_url = AddKeywordWithDate(
924 "keyword", false, "http://foo.com/foo?query={searchTerms}", 927 "keyword", false, "http://foo.com/foo?query={searchTerms}",
925 "http://sugg1", "http://icon1", "UTF-8;UTF-16", "keyword", 928 "http://sugg1", "http://icon1", "UTF-8;UTF-16", "keyword",
926 true, base::Time::Now(), base::Time::Now()); 929 true, base::Time::Now(), base::Time::Now());
927 930
928 // Add a visit that matches the url of the keyword. 931 // Add a visit that matches the url of the keyword.
929 HistoryService* history = 932 HistoryService* history =
930 profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); 933 profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
931 history->AddPage( 934 history->AddPage(
932 GURL(t_url->url()->ReplaceSearchTerms(*t_url, ASCIIToUTF16("blah"), 0, 935 GURL(t_url->url()->ReplaceSearchTerms(profile(), *t_url,
933 string16())), 936 ASCIIToUTF16("blah"), 0, string16())),
934 NULL, 0, GURL(), PageTransition::KEYWORD, history::RedirectList(), 937 NULL, 0, GURL(), PageTransition::KEYWORD, history::RedirectList(),
935 history::SOURCE_BROWSED, false); 938 history::SOURCE_BROWSED, false);
936 939
937 // Wait for history to finish processing the request. 940 // Wait for history to finish processing the request.
938 profile()->BlockUntilHistoryProcessesPendingRequests(); 941 profile()->BlockUntilHistoryProcessesPendingRequests();
939 942
940 // Query history for the generated url. 943 // Query history for the generated url.
941 CancelableRequestConsumer consumer; 944 CancelableRequestConsumer consumer;
942 QueryHistoryCallbackImpl callback; 945 QueryHistoryCallbackImpl callback;
943 history->QueryURL(GURL("http://keyword"), true, &consumer, 946 history->QueryURL(GURL("http://keyword"), true, &consumer,
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
1237 model()->SetDefaultSearchProvider(t_url); 1240 model()->SetDefaultSearchProvider(t_url);
1238 EXPECT_EQ(t_url, model()->GetDefaultSearchProvider()); 1241 EXPECT_EQ(t_url, model()->GetDefaultSearchProvider());
1239 1242
1240 // Now reset the model again but load it after setting the preferences. 1243 // Now reset the model again but load it after setting the preferences.
1241 ResetModel(false); 1244 ResetModel(false);
1242 SetManagedDefaultSearchPreferences(false, "", "", "", "", "", ""); 1245 SetManagedDefaultSearchPreferences(false, "", "", "", "", "", "");
1243 VerifyLoad(); 1246 VerifyLoad();
1244 EXPECT_TRUE(model()->is_default_search_managed()); 1247 EXPECT_TRUE(model()->is_default_search_managed());
1245 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL); 1248 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL);
1246 } 1249 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698