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/bind.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "chrome/browser/search_engines/chrome_template_url_service_client.h" | 11 #include "chrome/browser/search_engines/chrome_template_url_service_client.h" |
12 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | |
13 #include "chrome/browser/webdata/web_data_service_factory.h" | 12 #include "chrome/browser/webdata/web_data_service_factory.h" |
14 #include "chrome/test/base/testing_pref_service_syncable.h" | 13 #include "chrome/test/base/testing_pref_service_syncable.h" |
15 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
16 #include "components/google/core/browser/google_url_tracker.h" | 15 #include "components/google/core/browser/google_url_tracker.h" |
17 #include "components/search_engines/default_search_manager.h" | 16 #include "components/search_engines/default_search_manager.h" |
18 #include "components/search_engines/default_search_pref_test_util.h" | 17 #include "components/search_engines/default_search_pref_test_util.h" |
19 #include "components/search_engines/template_url_service.h" | 18 #include "components/search_engines/template_url_service.h" |
| 19 #include "components/search_engines/testing_search_terms_data.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 #if defined(OS_CHROMEOS) | |
23 #include "chrome/browser/google/google_brand_chromeos.h" | |
24 #endif | |
25 | |
26 // Trivial subclass of TemplateURLService that records the last invocation of | 22 // Trivial subclass of TemplateURLService that records the last invocation of |
27 // SetKeywordSearchTermsForURL. | 23 // SetKeywordSearchTermsForURL. |
28 class TestingTemplateURLService : public TemplateURLService { | 24 class TestingTemplateURLService : public TemplateURLService { |
29 public: | 25 public: |
30 explicit TestingTemplateURLService(Profile* profile) | 26 TestingTemplateURLService(Profile* profile, |
| 27 scoped_ptr<SearchTermsData> search_terms_data) |
31 : TemplateURLService( | 28 : TemplateURLService( |
32 profile->GetPrefs(), | 29 profile->GetPrefs(), |
33 scoped_ptr<SearchTermsData>(new UIThreadSearchTermsData(profile)), | 30 search_terms_data.Pass(), |
34 WebDataServiceFactory::GetKeywordWebDataForProfile( | 31 WebDataServiceFactory::GetKeywordWebDataForProfile( |
35 profile, Profile::EXPLICIT_ACCESS), | 32 profile, Profile::EXPLICIT_ACCESS), |
36 scoped_ptr<TemplateURLServiceClient>( | 33 scoped_ptr<TemplateURLServiceClient>( |
37 new ChromeTemplateURLServiceClient(profile)), NULL, NULL, | 34 new ChromeTemplateURLServiceClient(profile)), NULL, NULL, |
38 base::Closure()) { | 35 base::Closure()) { |
39 } | 36 } |
40 | 37 |
41 base::string16 GetAndClearSearchTerm() { | 38 base::string16 GetAndClearSearchTerm() { |
42 base::string16 search_term; | 39 base::string16 search_term; |
43 search_term.swap(search_term_); | 40 search_term.swap(search_term_); |
(...skipping 10 matching lines...) Expand all Loading... |
54 | 51 |
55 private: | 52 private: |
56 base::string16 search_term_; | 53 base::string16 search_term_; |
57 | 54 |
58 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); | 55 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); |
59 }; | 56 }; |
60 | 57 |
61 | 58 |
62 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() | 59 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() |
63 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 60 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
64 changed_count_(0) { | 61 changed_count_(0), |
| 62 search_terms_data_(NULL) { |
65 // Make unique temp directory. | 63 // Make unique temp directory. |
66 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 64 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
67 profile_.reset(new TestingProfile(temp_dir_.path())); | 65 profile_.reset(new TestingProfile(temp_dir_.path())); |
68 | 66 |
69 profile()->CreateWebDataService(); | 67 profile()->CreateWebDataService(); |
70 | 68 |
71 model_.reset(new TestingTemplateURLService(profile())); | 69 search_terms_data_ = new TestingSearchTermsData("http://www.google.com/"); |
| 70 model_.reset(new TestingTemplateURLService( |
| 71 profile(), scoped_ptr<SearchTermsData>(search_terms_data_))); |
72 model_->AddObserver(this); | 72 model_->AddObserver(this); |
73 | |
74 #if defined(OS_CHROMEOS) | |
75 google_brand::chromeos::ClearBrandForCurrentSession(); | |
76 #endif | |
77 } | 73 } |
78 | 74 |
79 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { | 75 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { |
80 ClearModel(); | 76 ClearModel(); |
81 profile_.reset(); | 77 profile_.reset(); |
82 | 78 |
83 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); | |
84 | |
85 // Flush the message loop to make application verifiers happy. | 79 // Flush the message loop to make application verifiers happy. |
86 base::RunLoop().RunUntilIdle(); | 80 base::RunLoop().RunUntilIdle(); |
87 } | 81 } |
88 | 82 |
89 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() { | 83 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() { |
90 changed_count_++; | 84 changed_count_++; |
91 } | 85 } |
92 | 86 |
93 int TemplateURLServiceTestUtil::GetObserverCount() { | 87 int TemplateURLServiceTestUtil::GetObserverCount() { |
94 return changed_count_; | 88 return changed_count_; |
(...skipping 18 matching lines...) Expand all Loading... |
113 | 107 |
114 model()->web_data_service_ = | 108 model()->web_data_service_ = |
115 WebDataServiceFactory::GetKeywordWebDataForProfile( | 109 WebDataServiceFactory::GetKeywordWebDataForProfile( |
116 profile(), Profile::EXPLICIT_ACCESS); | 110 profile(), Profile::EXPLICIT_ACCESS); |
117 base::RunLoop().RunUntilIdle(); | 111 base::RunLoop().RunUntilIdle(); |
118 } | 112 } |
119 | 113 |
120 void TemplateURLServiceTestUtil::ClearModel() { | 114 void TemplateURLServiceTestUtil::ClearModel() { |
121 model_->Shutdown(); | 115 model_->Shutdown(); |
122 model_.reset(); | 116 model_.reset(); |
| 117 search_terms_data_ = NULL; |
123 } | 118 } |
124 | 119 |
125 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { | 120 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { |
126 if (model_) | 121 if (model_) |
127 ClearModel(); | 122 ClearModel(); |
128 model_.reset(new TestingTemplateURLService(profile())); | 123 search_terms_data_ = new TestingSearchTermsData("http://www.google.com/"); |
| 124 model_.reset(new TestingTemplateURLService( |
| 125 profile(), scoped_ptr<SearchTermsData>(search_terms_data_))); |
129 model()->AddObserver(this); | 126 model()->AddObserver(this); |
130 changed_count_ = 0; | 127 changed_count_ = 0; |
131 if (verify_load) | 128 if (verify_load) |
132 VerifyLoad(); | 129 VerifyLoad(); |
133 } | 130 } |
134 | 131 |
135 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() { | 132 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() { |
136 return model_->GetAndClearSearchTerm(); | 133 return model_->GetAndClearSearchTerm(); |
137 } | 134 } |
138 | 135 |
139 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) { | 136 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) { |
140 DCHECK(base_url.is_valid()); | 137 DCHECK(base_url.is_valid()); |
141 UIThreadSearchTermsData data(profile()); | 138 search_terms_data_->set_google_base_url(base_url.spec()); |
142 UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec()); | |
143 model_->GoogleBaseURLChanged(); | 139 model_->GoogleBaseURLChanged(); |
144 } | 140 } |
145 | 141 |
146 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences( | 142 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences( |
147 bool enabled, | 143 bool enabled, |
148 const std::string& name, | 144 const std::string& name, |
149 const std::string& keyword, | 145 const std::string& keyword, |
150 const std::string& search_url, | 146 const std::string& search_url, |
151 const std::string& suggest_url, | 147 const std::string& suggest_url, |
152 const std::string& icon_url, | 148 const std::string& icon_url, |
153 const std::string& encodings, | 149 const std::string& encodings, |
154 const std::string& alternate_url, | 150 const std::string& alternate_url, |
155 const std::string& search_terms_replacement_key) { | 151 const std::string& search_terms_replacement_key) { |
156 DefaultSearchPrefTestUtil::SetManagedPref( | 152 DefaultSearchPrefTestUtil::SetManagedPref( |
157 profile()->GetTestingPrefService(), | 153 profile()->GetTestingPrefService(), |
158 enabled, name, keyword, search_url, suggest_url, icon_url, encodings, | 154 enabled, name, keyword, search_url, suggest_url, icon_url, encodings, |
159 alternate_url, search_terms_replacement_key); | 155 alternate_url, search_terms_replacement_key); |
160 } | 156 } |
161 | 157 |
162 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() { | 158 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() { |
163 DefaultSearchPrefTestUtil::RemoveManagedPref( | 159 DefaultSearchPrefTestUtil::RemoveManagedPref( |
164 profile()->GetTestingPrefService()); | 160 profile()->GetTestingPrefService()); |
165 } | 161 } |
166 | 162 |
167 TemplateURLService* TemplateURLServiceTestUtil::model() { | 163 TemplateURLService* TemplateURLServiceTestUtil::model() { |
168 return model_.get(); | 164 return model_.get(); |
169 } | 165 } |
OLD | NEW |