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

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

Issue 376413002: Stop using TemplateURLServiceTestUtil to initialize TemplateURLServiceFactory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix TemplateURLServiceWithoutFallbackTest Created 6 years, 5 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/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/memory/scoped_vector.h" 6 #include "base/memory/scoped_vector.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 150
151 class TemplateURLServiceSyncTest : public testing::Test { 151 class TemplateURLServiceSyncTest : public testing::Test {
152 public: 152 public:
153 typedef TemplateURLService::SyncDataMap SyncDataMap; 153 typedef TemplateURLService::SyncDataMap SyncDataMap;
154 154
155 TemplateURLServiceSyncTest(); 155 TemplateURLServiceSyncTest();
156 156
157 virtual void SetUp() OVERRIDE; 157 virtual void SetUp() OVERRIDE;
158 virtual void TearDown() OVERRIDE; 158 virtual void TearDown() OVERRIDE;
159 159
160 TemplateURLService* model() { return test_util_a_.model(); } 160 TemplateURLService* model() { return test_util_a_->model(); }
161 // For readability, we redefine an accessor for Model A for use in tests that 161 // For readability, we redefine an accessor for Model A for use in tests that
162 // involve syncing two models. 162 // involve syncing two models.
163 TemplateURLService* model_a() { return test_util_a_.model(); } 163 TemplateURLService* model_a() { return test_util_a_->model(); }
164 TemplateURLService* model_b() { return model_b_.get(); } 164 TemplateURLService* model_b() { return model_b_.get(); }
165 TestingProfile* profile_a() { return test_util_a_.profile(); } 165 TestingProfile* profile_a() { return test_util_a_->profile(); }
166 TestChangeProcessor* processor() { return sync_processor_.get(); } 166 TestChangeProcessor* processor() { return sync_processor_.get(); }
167 scoped_ptr<syncer::SyncChangeProcessor> PassProcessor(); 167 scoped_ptr<syncer::SyncChangeProcessor> PassProcessor();
168 scoped_ptr<syncer::SyncErrorFactory> CreateAndPassSyncErrorFactory(); 168 scoped_ptr<syncer::SyncErrorFactory> CreateAndPassSyncErrorFactory();
169 169
170 // Creates a TemplateURL with some test values. The caller owns the returned 170 // Creates a TemplateURL with some test values. The caller owns the returned
171 // TemplateURL*. 171 // TemplateURL*.
172 TemplateURL* CreateTestTemplateURL(const base::string16& keyword, 172 TemplateURL* CreateTestTemplateURL(const base::string16& keyword,
173 const std::string& url, 173 const std::string& url,
174 const std::string& guid = std::string(), 174 const std::string& guid = std::string(),
175 time_t last_mod = 100, 175 time_t last_mod = 100,
(...skipping 27 matching lines...) Expand all
203 // Creates a new TemplateURL copying the fields of |turl| but replacing 203 // Creates a new TemplateURL copying the fields of |turl| but replacing
204 // the |url| and |guid| and initializing the date_created and last_modified 204 // the |url| and |guid| and initializing the date_created and last_modified
205 // timestamps to a default value of 100. The caller owns the returned 205 // timestamps to a default value of 100. The caller owns the returned
206 // TemplateURL*. 206 // TemplateURL*.
207 TemplateURL* CopyTemplateURL(const TemplateURLData* turl, 207 TemplateURL* CopyTemplateURL(const TemplateURLData* turl,
208 const std::string& url, 208 const std::string& url,
209 const std::string& guid); 209 const std::string& guid);
210 210
211 protected: 211 protected:
212 // We keep two TemplateURLServices to test syncing between them. 212 // We keep two TemplateURLServices to test syncing between them.
213 TemplateURLServiceTestUtil test_util_a_; 213 scoped_ptr<TemplateURLServiceTestUtil> test_util_a_;
214 scoped_ptr<TestingProfile> profile_b_; 214 scoped_ptr<TestingProfile> profile_b_;
215 scoped_ptr<TemplateURLService> model_b_; 215 scoped_ptr<TemplateURLService> model_b_;
216 216
217 // Our dummy ChangeProcessor used to inspect changes pushed to Sync. 217 // Our dummy ChangeProcessor used to inspect changes pushed to Sync.
218 scoped_ptr<TestChangeProcessor> sync_processor_; 218 scoped_ptr<TestChangeProcessor> sync_processor_;
219 scoped_ptr<syncer::SyncChangeProcessorWrapperForTest> sync_processor_wrapper_; 219 scoped_ptr<syncer::SyncChangeProcessorWrapperForTest> sync_processor_wrapper_;
220 220
221 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceSyncTest); 221 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceSyncTest);
222 }; 222 };
223 223
224 TemplateURLServiceSyncTest::TemplateURLServiceSyncTest() 224 TemplateURLServiceSyncTest::TemplateURLServiceSyncTest()
225 : sync_processor_(new TestChangeProcessor), 225 : sync_processor_(new TestChangeProcessor),
226 sync_processor_wrapper_(new syncer::SyncChangeProcessorWrapperForTest( 226 sync_processor_wrapper_(new syncer::SyncChangeProcessorWrapperForTest(
227 sync_processor_.get())) {} 227 sync_processor_.get())) {}
228 228
229 void TemplateURLServiceSyncTest::SetUp() { 229 void TemplateURLServiceSyncTest::SetUp() {
230 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true); 230 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true);
231 test_util_a_.SetUp(); 231 test_util_a_.reset(new TemplateURLServiceTestUtil);
232 // Use ChangeToLoadState() instead of VerifyLoad() so we don't actually pull 232 // Use ChangeToLoadState() instead of VerifyLoad() so we don't actually pull
233 // in the prepopulate data, which the sync tests don't care about (and would 233 // in the prepopulate data, which the sync tests don't care about (and would
234 // just foul them up). 234 // just foul them up).
235 test_util_a_.ChangeModelToLoadState(); 235 test_util_a_->ChangeModelToLoadState();
236 profile_b_.reset(new TestingProfile); 236 profile_b_.reset(new TestingProfile);
237 TemplateURLServiceFactory::GetInstance()-> 237 TemplateURLServiceFactory::GetInstance()->
238 RegisterUserPrefsOnBrowserContextForTest(profile_b_.get()); 238 RegisterUserPrefsOnBrowserContextForTest(profile_b_.get());
239 model_b_.reset(new TemplateURLService( 239 model_b_.reset(new TemplateURLService(
240 profile_b_->GetPrefs(), scoped_ptr<SearchTermsData>( 240 profile_b_->GetPrefs(), scoped_ptr<SearchTermsData>(
241 new UIThreadSearchTermsData(profile_b_.get())), NULL, 241 new UIThreadSearchTermsData(profile_b_.get())), NULL,
242 scoped_ptr<TemplateURLServiceClient>(), NULL, NULL, base::Closure())); 242 scoped_ptr<TemplateURLServiceClient>(), NULL, NULL, base::Closure()));
243 model_b_->Load(); 243 model_b_->Load();
244 } 244 }
245 245
246 void TemplateURLServiceSyncTest::TearDown() { 246 void TemplateURLServiceSyncTest::TearDown() {
247 test_util_a_.TearDown(); 247 test_util_a_.reset();
248 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false); 248 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false);
249 } 249 }
250 250
251 scoped_ptr<syncer::SyncChangeProcessor> 251 scoped_ptr<syncer::SyncChangeProcessor>
252 TemplateURLServiceSyncTest::PassProcessor() { 252 TemplateURLServiceSyncTest::PassProcessor() {
253 return sync_processor_wrapper_.PassAs<syncer::SyncChangeProcessor>(); 253 return sync_processor_wrapper_.PassAs<syncer::SyncChangeProcessor>();
254 } 254 }
255 255
256 scoped_ptr<syncer::SyncErrorFactory> TemplateURLServiceSyncTest:: 256 scoped_ptr<syncer::SyncErrorFactory> TemplateURLServiceSyncTest::
257 CreateAndPassSyncErrorFactory() { 257 CreateAndPassSyncErrorFactory() {
(...skipping 1396 matching lines...) Expand 10 before | Expand all | Expand 10 after
1654 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 1654 ASSERT_TRUE(model()->GetDefaultSearchProvider());
1655 1655
1656 // Change the default search provider to a managed one. 1656 // Change the default search provider to a managed one.
1657 const char kName[] = "manageddefault"; 1657 const char kName[] = "manageddefault";
1658 const char kSearchURL[] = "http://manageddefault.com/search?t={searchTerms}"; 1658 const char kSearchURL[] = "http://manageddefault.com/search?t={searchTerms}";
1659 const char kIconURL[] = "http://manageddefault.com/icon.jpg"; 1659 const char kIconURL[] = "http://manageddefault.com/icon.jpg";
1660 const char kEncodings[] = "UTF-16;UTF-32"; 1660 const char kEncodings[] = "UTF-16;UTF-32";
1661 const char kAlternateURL[] = 1661 const char kAlternateURL[] =
1662 "http://manageddefault.com/search#t={searchTerms}"; 1662 "http://manageddefault.com/search#t={searchTerms}";
1663 const char kSearchTermsReplacementKey[] = "espv"; 1663 const char kSearchTermsReplacementKey[] = "espv";
1664 test_util_a_.SetManagedDefaultSearchPreferences(true, kName, kName, 1664 test_util_a_->SetManagedDefaultSearchPreferences(true, kName, kName,
1665 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, 1665 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL,
1666 kSearchTermsReplacementKey); 1666 kSearchTermsReplacementKey);
1667 const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider(); 1667 const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider();
1668 1668
1669 EXPECT_TRUE(model()->is_default_search_managed()); 1669 EXPECT_TRUE(model()->is_default_search_managed());
1670 1670
1671 // Add a new entry from Sync. It should still sync in despite the default 1671 // Add a new entry from Sync. It should still sync in despite the default
1672 // being managed. 1672 // being managed.
1673 syncer::SyncChangeList changes; 1673 syncer::SyncChangeList changes;
1674 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_ADD, 1674 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_ADD,
(...skipping 10 matching lines...) Expand all
1685 prefs::kSyncedDefaultSearchProviderGUID, 1685 prefs::kSyncedDefaultSearchProviderGUID,
1686 "newdefault"); 1686 "newdefault");
1687 1687
1688 EXPECT_EQ(dsp_turl, model()->GetDefaultSearchProvider()); 1688 EXPECT_EQ(dsp_turl, model()->GetDefaultSearchProvider());
1689 EXPECT_TRUE(model()->is_default_search_managed()); 1689 EXPECT_TRUE(model()->is_default_search_managed());
1690 1690
1691 // Go unmanaged. Ensure that the DSP changes to the expected pending entry 1691 // Go unmanaged. Ensure that the DSP changes to the expected pending entry
1692 // from Sync. 1692 // from Sync.
1693 const TemplateURL* expected_default = 1693 const TemplateURL* expected_default =
1694 model()->GetTemplateURLForGUID("newdefault"); 1694 model()->GetTemplateURLForGUID("newdefault");
1695 test_util_a_.RemoveManagedDefaultSearchPreferences(); 1695 test_util_a_->RemoveManagedDefaultSearchPreferences();
1696 1696
1697 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); 1697 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider());
1698 } 1698 }
1699 1699
1700 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { 1700 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) {
1701 // If the value from Sync is a duplicate of the local default and is newer, it 1701 // If the value from Sync is a duplicate of the local default and is newer, it
1702 // should safely replace the local value and set as the new default. 1702 // should safely replace the local value and set as the new default.
1703 TemplateURL* default_turl = CreateTestTemplateURL(ASCIIToUTF16("key1"), 1703 TemplateURL* default_turl = CreateTestTemplateURL(ASCIIToUTF16("key1"),
1704 "http://key1.com/{searchTerms}", "whateverguid", 10); 1704 "http://key1.com/{searchTerms}", "whateverguid", 10);
1705 model()->Add(default_turl); 1705 model()->Add(default_turl);
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
1839 data_copy.last_modified = Time::FromTimeT(10); 1839 data_copy.last_modified = Time::FromTimeT(10);
1840 base::string16 original_keyword = data_copy.keyword(); 1840 base::string16 original_keyword = data_copy.keyword();
1841 data_copy.SetKeyword(ASCIIToUTF16(kNewKeyword)); 1841 data_copy.SetKeyword(ASCIIToUTF16(kNewKeyword));
1842 // Set safe_for_autoreplace to false so our keyword survives. 1842 // Set safe_for_autoreplace to false so our keyword survives.
1843 data_copy.safe_for_autoreplace = false; 1843 data_copy.safe_for_autoreplace = false;
1844 model()->Add(new TemplateURL(data_copy)); 1844 model()->Add(new TemplateURL(data_copy));
1845 1845
1846 // Merge the prepopulate search engines. 1846 // Merge the prepopulate search engines.
1847 base::Time pre_merge_time = base::Time::Now(); 1847 base::Time pre_merge_time = base::Time::Now();
1848 base::RunLoop().RunUntilIdle(); 1848 base::RunLoop().RunUntilIdle();
1849 test_util_a_.ResetModel(true); 1849 test_util_a_->ResetModel(true);
1850 1850
1851 // The newly added search engine should have been safely merged, with an 1851 // The newly added search engine should have been safely merged, with an
1852 // updated time. 1852 // updated time.
1853 TemplateURL* added_turl = model()->GetTemplateURLForKeyword( 1853 TemplateURL* added_turl = model()->GetTemplateURLForKeyword(
1854 ASCIIToUTF16(kNewKeyword)); 1854 ASCIIToUTF16(kNewKeyword));
1855 ASSERT_TRUE(added_turl); 1855 ASSERT_TRUE(added_turl);
1856 base::Time new_timestamp = added_turl->last_modified(); 1856 base::Time new_timestamp = added_turl->last_modified();
1857 EXPECT_GE(new_timestamp, pre_merge_time); 1857 EXPECT_GE(new_timestamp, pre_merge_time);
1858 std::string sync_guid = added_turl->sync_guid(); 1858 std::string sync_guid = added_turl->sync_guid();
1859 1859
(...skipping 28 matching lines...) Expand all
1888 EXPECT_EQ(0, merge_result.num_items_deleted()); 1888 EXPECT_EQ(0, merge_result.num_items_deleted());
1889 EXPECT_EQ(static_cast<int>(prepop_turls.size()), 1889 EXPECT_EQ(static_cast<int>(prepop_turls.size()),
1890 merge_result.num_items_before_association()); 1890 merge_result.num_items_before_association());
1891 EXPECT_EQ(static_cast<int>(prepop_turls.size()), 1891 EXPECT_EQ(static_cast<int>(prepop_turls.size()),
1892 merge_result.num_items_after_association()); 1892 merge_result.num_items_after_association());
1893 } 1893 }
1894 1894
1895 TEST_F(TemplateURLServiceSyncTest, SyncBaseURLs) { 1895 TEST_F(TemplateURLServiceSyncTest, SyncBaseURLs) {
1896 // Verify that bringing in a remote TemplateURL that uses Google base URLs 1896 // Verify that bringing in a remote TemplateURL that uses Google base URLs
1897 // causes it to get a local keyword that matches the local base URL. 1897 // causes it to get a local keyword that matches the local base URL.
1898 test_util_a_.SetGoogleBaseURL(GURL("http://google.com/")); 1898 test_util_a_->SetGoogleBaseURL(GURL("http://google.com/"));
1899 syncer::SyncDataList initial_data; 1899 syncer::SyncDataList initial_data;
1900 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL( 1900 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(
1901 ASCIIToUTF16("google.co.uk"), "{google:baseURL}search?q={searchTerms}", 1901 ASCIIToUTF16("google.co.uk"), "{google:baseURL}search?q={searchTerms}",
1902 "guid")); 1902 "guid"));
1903 initial_data.push_back( 1903 initial_data.push_back(
1904 TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); 1904 TemplateURLService::CreateSyncDataFromTemplateURL(*turl));
1905 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, 1905 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data,
1906 PassProcessor(), CreateAndPassSyncErrorFactory()); 1906 PassProcessor(), CreateAndPassSyncErrorFactory());
1907 TemplateURL* synced_turl = model()->GetTemplateURLForGUID("guid"); 1907 TemplateURL* synced_turl = model()->GetTemplateURLForGUID("guid");
1908 ASSERT_TRUE(synced_turl); 1908 ASSERT_TRUE(synced_turl);
1909 EXPECT_EQ(ASCIIToUTF16("google.com"), synced_turl->keyword()); 1909 EXPECT_EQ(ASCIIToUTF16("google.com"), synced_turl->keyword());
1910 EXPECT_EQ(0U, processor()->change_list_size()); 1910 EXPECT_EQ(0U, processor()->change_list_size());
1911 1911
1912 // Remote updates to this URL's keyword should be silently ignored. 1912 // Remote updates to this URL's keyword should be silently ignored.
1913 syncer::SyncChangeList changes; 1913 syncer::SyncChangeList changes;
1914 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_UPDATE, 1914 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_UPDATE,
1915 CreateTestTemplateURL(ASCIIToUTF16("google.de"), 1915 CreateTestTemplateURL(ASCIIToUTF16("google.de"),
1916 "{google:baseURL}search?q={searchTerms}", "guid"))); 1916 "{google:baseURL}search?q={searchTerms}", "guid")));
1917 model()->ProcessSyncChanges(FROM_HERE, changes); 1917 model()->ProcessSyncChanges(FROM_HERE, changes);
1918 EXPECT_EQ(ASCIIToUTF16("google.com"), synced_turl->keyword()); 1918 EXPECT_EQ(ASCIIToUTF16("google.com"), synced_turl->keyword());
1919 EXPECT_EQ(0U, processor()->change_list_size()); 1919 EXPECT_EQ(0U, processor()->change_list_size());
1920 1920
1921 // A local change to the Google base URL should update the keyword and 1921 // A local change to the Google base URL should update the keyword and
1922 // generate a sync change. 1922 // generate a sync change.
1923 test_util_a_.SetGoogleBaseURL(GURL("http://google.co.in/")); 1923 test_util_a_->SetGoogleBaseURL(GURL("http://google.co.in/"));
1924 EXPECT_EQ(ASCIIToUTF16("google.co.in"), synced_turl->keyword()); 1924 EXPECT_EQ(ASCIIToUTF16("google.co.in"), synced_turl->keyword());
1925 EXPECT_EQ(1U, processor()->change_list_size()); 1925 EXPECT_EQ(1U, processor()->change_list_size());
1926 ASSERT_TRUE(processor()->contains_guid("guid")); 1926 ASSERT_TRUE(processor()->contains_guid("guid"));
1927 syncer::SyncChange change(processor()->change_for_guid("guid")); 1927 syncer::SyncChange change(processor()->change_for_guid("guid"));
1928 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, change.change_type()); 1928 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, change.change_type());
1929 EXPECT_EQ("google.co.in", GetKeyword(change.sync_data())); 1929 EXPECT_EQ("google.co.in", GetKeyword(change.sync_data()));
1930 } 1930 }
1931 1931
1932 TEST_F(TemplateURLServiceSyncTest, MergeInSyncTemplateURL) { 1932 TEST_F(TemplateURLServiceSyncTest, MergeInSyncTemplateURL) {
1933 // An enumeration used to indicate which TemplateURL test value is expected 1933 // An enumeration used to indicate which TemplateURL test value is expected
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
2235 const char kNewGUID[] = "newdefault"; 2235 const char kNewGUID[] = "newdefault";
2236 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("what"), 2236 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("what"),
2237 "http://thewhat.com/{searchTerms}", 2237 "http://thewhat.com/{searchTerms}",
2238 kNewGUID)); 2238 kNewGUID));
2239 model()->SetUserSelectedDefaultSearchProvider( 2239 model()->SetUserSelectedDefaultSearchProvider(
2240 model()->GetTemplateURLForGUID(kNewGUID)); 2240 model()->GetTemplateURLForGUID(kNewGUID));
2241 2241
2242 EXPECT_EQ(kNewGUID, profile_a()->GetTestingPrefService()->GetString( 2242 EXPECT_EQ(kNewGUID, profile_a()->GetTestingPrefService()->GetString(
2243 prefs::kSyncedDefaultSearchProviderGUID)); 2243 prefs::kSyncedDefaultSearchProviderGUID));
2244 } 2244 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698