OLD | NEW |
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 "chrome/browser/search_engines/template_url_service_test_util.h" | 5 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/message_loop/message_loop_proxy.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/string_split.h" | |
10 #include "base/threading/thread.h" | |
11 #include "chrome/browser/search_engines/chrome_template_url_service_client.h" | 9 #include "chrome/browser/search_engines/chrome_template_url_service_client.h" |
12 #include "chrome/browser/webdata/web_data_service_factory.h" | |
13 #include "chrome/test/base/testing_pref_service_syncable.h" | 10 #include "chrome/test/base/testing_pref_service_syncable.h" |
14 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
15 #include "components/google/core/browser/google_url_tracker.h" | |
16 #include "components/search_engines/default_search_manager.h" | |
17 #include "components/search_engines/default_search_pref_test_util.h" | 12 #include "components/search_engines/default_search_pref_test_util.h" |
| 13 #include "components/search_engines/keyword_table.h" |
| 14 #include "components/search_engines/keyword_web_data_service.h" |
18 #include "components/search_engines/template_url_service.h" | 15 #include "components/search_engines/template_url_service.h" |
19 #include "components/search_engines/testing_search_terms_data.h" | 16 #include "components/search_engines/testing_search_terms_data.h" |
| 17 #include "components/webdata/common/web_database_service.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
21 | 19 |
22 // Trivial subclass of TemplateURLService that records the last invocation of | 20 namespace { |
23 // SetKeywordSearchTermsForURL. | 21 |
24 class TestingTemplateURLService : public TemplateURLService { | 22 class TestingTemplateURLServiceClient : public ChromeTemplateURLServiceClient { |
25 public: | 23 public: |
26 TestingTemplateURLService(Profile* profile, | 24 TestingTemplateURLServiceClient(Profile* profile, |
27 scoped_ptr<SearchTermsData> search_terms_data) | 25 base::string16* search_term) |
28 : TemplateURLService( | 26 : ChromeTemplateURLServiceClient(profile), |
29 profile->GetPrefs(), | 27 search_term_(search_term) {} |
30 search_terms_data.Pass(), | |
31 WebDataServiceFactory::GetKeywordWebDataForProfile( | |
32 profile, Profile::EXPLICIT_ACCESS), | |
33 scoped_ptr<TemplateURLServiceClient>( | |
34 new ChromeTemplateURLServiceClient(profile)), NULL, NULL, | |
35 base::Closure()) { | |
36 } | |
37 | 28 |
38 base::string16 GetAndClearSearchTerm() { | |
39 base::string16 search_term; | |
40 search_term.swap(search_term_); | |
41 return search_term; | |
42 } | |
43 | |
44 protected: | |
45 virtual void SetKeywordSearchTermsForURL( | 29 virtual void SetKeywordSearchTermsForURL( |
46 const TemplateURL* t_url, | |
47 const GURL& url, | 30 const GURL& url, |
| 31 TemplateURLID id, |
48 const base::string16& term) OVERRIDE { | 32 const base::string16& term) OVERRIDE { |
49 search_term_ = term; | 33 *search_term_ = term; |
50 } | 34 } |
51 | 35 |
52 private: | 36 private: |
53 base::string16 search_term_; | 37 base::string16* search_term_; |
54 | 38 |
55 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); | 39 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLServiceClient); |
56 }; | 40 }; |
57 | 41 |
| 42 } // namespace |
58 | 43 |
59 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() | 44 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() |
60 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 45 : changed_count_(0), |
61 changed_count_(0), | |
62 search_terms_data_(NULL) { | 46 search_terms_data_(NULL) { |
63 // Make unique temp directory. | 47 // Make unique temp directory. |
64 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 48 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
65 profile_.reset(new TestingProfile(temp_dir_.path())); | 49 profile_.reset(new TestingProfile(temp_dir_.path())); |
66 | 50 |
67 profile()->CreateWebDataService(); | 51 scoped_refptr<WebDatabaseService> web_database_service = |
| 52 new WebDatabaseService(temp_dir_.path().AppendASCII("webdata"), |
| 53 base::MessageLoopProxy::current(), |
| 54 base::MessageLoopProxy::current()); |
| 55 web_database_service->AddTable( |
| 56 scoped_ptr<WebDatabaseTable>(new KeywordTable())); |
| 57 web_database_service->LoadDatabase(); |
68 | 58 |
69 search_terms_data_ = new TestingSearchTermsData("http://www.google.com/"); | 59 web_data_service_ = new KeywordWebDataService( |
70 model_.reset(new TestingTemplateURLService( | 60 web_database_service.get(), base::MessageLoopProxy::current(), |
71 profile(), scoped_ptr<SearchTermsData>(search_terms_data_))); | 61 KeywordWebDataService::ProfileErrorCallback()); |
72 model_->AddObserver(this); | 62 web_data_service_->Init(); |
| 63 |
| 64 ResetModel(false); |
73 } | 65 } |
74 | 66 |
75 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { | 67 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { |
76 ClearModel(); | 68 ClearModel(); |
77 profile_.reset(); | 69 profile_.reset(); |
78 | 70 |
79 // Flush the message loop to make application verifiers happy. | 71 // Flush the message loop to make application verifiers happy. |
80 base::RunLoop().RunUntilIdle(); | 72 base::RunLoop().RunUntilIdle(); |
81 } | 73 } |
82 | 74 |
(...skipping 15 matching lines...) Expand all Loading... |
98 base::RunLoop().RunUntilIdle(); | 90 base::RunLoop().RunUntilIdle(); |
99 EXPECT_EQ(1, GetObserverCount()); | 91 EXPECT_EQ(1, GetObserverCount()); |
100 ResetObserverCount(); | 92 ResetObserverCount(); |
101 } | 93 } |
102 | 94 |
103 void TemplateURLServiceTestUtil::ChangeModelToLoadState() { | 95 void TemplateURLServiceTestUtil::ChangeModelToLoadState() { |
104 model()->ChangeToLoadedState(); | 96 model()->ChangeToLoadedState(); |
105 // Initialize the web data service so that the database gets updated with | 97 // Initialize the web data service so that the database gets updated with |
106 // any changes made. | 98 // any changes made. |
107 | 99 |
108 model()->web_data_service_ = | 100 model()->web_data_service_ = web_data_service_; |
109 WebDataServiceFactory::GetKeywordWebDataForProfile( | |
110 profile(), Profile::EXPLICIT_ACCESS); | |
111 base::RunLoop().RunUntilIdle(); | 101 base::RunLoop().RunUntilIdle(); |
112 } | 102 } |
113 | 103 |
114 void TemplateURLServiceTestUtil::ClearModel() { | 104 void TemplateURLServiceTestUtil::ClearModel() { |
115 model_->Shutdown(); | 105 model_->Shutdown(); |
116 model_.reset(); | 106 model_.reset(); |
117 search_terms_data_ = NULL; | 107 search_terms_data_ = NULL; |
118 } | 108 } |
119 | 109 |
120 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { | 110 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { |
121 if (model_) | 111 if (model_) |
122 ClearModel(); | 112 ClearModel(); |
123 search_terms_data_ = new TestingSearchTermsData("http://www.google.com/"); | 113 search_terms_data_ = new TestingSearchTermsData("http://www.google.com/"); |
124 model_.reset(new TestingTemplateURLService( | 114 model_.reset(new TemplateURLService( |
125 profile(), scoped_ptr<SearchTermsData>(search_terms_data_))); | 115 profile()->GetPrefs(), scoped_ptr<SearchTermsData>(search_terms_data_), |
| 116 web_data_service_.get(), |
| 117 scoped_ptr<TemplateURLServiceClient>( |
| 118 new TestingTemplateURLServiceClient(profile(), &search_term_)), |
| 119 NULL, NULL, base::Closure())); |
126 model()->AddObserver(this); | 120 model()->AddObserver(this); |
127 changed_count_ = 0; | 121 changed_count_ = 0; |
128 if (verify_load) | 122 if (verify_load) |
129 VerifyLoad(); | 123 VerifyLoad(); |
130 } | 124 } |
131 | 125 |
132 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() { | 126 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() { |
133 return model_->GetAndClearSearchTerm(); | 127 base::string16 search_term; |
| 128 search_term.swap(search_term_); |
| 129 return search_term; |
134 } | 130 } |
135 | 131 |
136 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) { | 132 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) { |
137 DCHECK(base_url.is_valid()); | 133 DCHECK(base_url.is_valid()); |
138 search_terms_data_->set_google_base_url(base_url.spec()); | 134 search_terms_data_->set_google_base_url(base_url.spec()); |
139 model_->GoogleBaseURLChanged(); | 135 model_->GoogleBaseURLChanged(); |
140 } | 136 } |
141 | 137 |
142 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences( | 138 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences( |
143 bool enabled, | 139 bool enabled, |
144 const std::string& name, | 140 const std::string& name, |
145 const std::string& keyword, | 141 const std::string& keyword, |
146 const std::string& search_url, | 142 const std::string& search_url, |
147 const std::string& suggest_url, | 143 const std::string& suggest_url, |
148 const std::string& icon_url, | 144 const std::string& icon_url, |
149 const std::string& encodings, | 145 const std::string& encodings, |
150 const std::string& alternate_url, | 146 const std::string& alternate_url, |
151 const std::string& search_terms_replacement_key) { | 147 const std::string& search_terms_replacement_key) { |
152 DefaultSearchPrefTestUtil::SetManagedPref( | 148 DefaultSearchPrefTestUtil::SetManagedPref( |
153 profile()->GetTestingPrefService(), | 149 profile()->GetTestingPrefService(), |
154 enabled, name, keyword, search_url, suggest_url, icon_url, encodings, | 150 enabled, name, keyword, search_url, suggest_url, icon_url, encodings, |
155 alternate_url, search_terms_replacement_key); | 151 alternate_url, search_terms_replacement_key); |
156 } | 152 } |
157 | 153 |
158 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() { | 154 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() { |
159 DefaultSearchPrefTestUtil::RemoveManagedPref( | 155 DefaultSearchPrefTestUtil::RemoveManagedPref( |
160 profile()->GetTestingPrefService()); | 156 profile()->GetTestingPrefService()); |
161 } | 157 } |
162 | |
163 TemplateURLService* TemplateURLServiceTestUtil::model() { | |
164 return model_.get(); | |
165 } | |
OLD | NEW |