| 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 "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 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 223 |
| 224 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceSyncTest); | 224 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceSyncTest); |
| 225 }; | 225 }; |
| 226 | 226 |
| 227 TemplateURLServiceSyncTest::TemplateURLServiceSyncTest() | 227 TemplateURLServiceSyncTest::TemplateURLServiceSyncTest() |
| 228 : sync_processor_(new TestChangeProcessor), | 228 : sync_processor_(new TestChangeProcessor), |
| 229 sync_processor_wrapper_(new syncer::SyncChangeProcessorWrapperForTest( | 229 sync_processor_wrapper_(new syncer::SyncChangeProcessorWrapperForTest( |
| 230 sync_processor_.get())) {} | 230 sync_processor_.get())) {} |
| 231 | 231 |
| 232 void TemplateURLServiceSyncTest::SetUp() { | 232 void TemplateURLServiceSyncTest::SetUp() { |
| 233 TemplateURLService::DisableFallbackSearchEngines(); |
| 233 test_util_a_.SetUp(); | 234 test_util_a_.SetUp(); |
| 234 // Use ChangeToLoadState() instead of VerifyLoad() so we don't actually pull | 235 // Use ChangeToLoadState() instead of VerifyLoad() so we don't actually pull |
| 235 // in the prepopulate data, which the sync tests don't care about (and would | 236 // in the prepopulate data, which the sync tests don't care about (and would |
| 236 // just foul them up). | 237 // just foul them up). |
| 237 test_util_a_.ChangeModelToLoadState(); | 238 test_util_a_.ChangeModelToLoadState(); |
| 238 profile_b_.reset(new TestingProfile); | 239 profile_b_.reset(new TestingProfile); |
| 239 TemplateURLServiceFactory::GetInstance()-> | 240 TemplateURLServiceFactory::GetInstance()-> |
| 240 RegisterUserPrefsOnBrowserContextForTest(profile_b_.get()); | 241 RegisterUserPrefsOnBrowserContextForTest(profile_b_.get()); |
| 241 model_b_.reset(new TemplateURLService(profile_b_.get())); | 242 model_b_.reset(new TemplateURLService(profile_b_.get())); |
| 242 model_b_->Load(); | 243 model_b_->Load(); |
| (...skipping 1384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1627 // Now sync the initial data. | 1628 // Now sync the initial data. |
| 1628 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 1629 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
| 1629 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, | 1630 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, |
| 1630 PassProcessor(), CreateAndPassSyncErrorFactory()); | 1631 PassProcessor(), CreateAndPassSyncErrorFactory()); |
| 1631 | 1632 |
| 1632 // Ensure that the new entries were added and the default has not changed. | 1633 // Ensure that the new entries were added and the default has not changed. |
| 1633 EXPECT_EQ(4U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | 1634 EXPECT_EQ(4U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); |
| 1634 ASSERT_EQ(default_search, model()->GetDefaultSearchProvider()); | 1635 ASSERT_EQ(default_search, model()->GetDefaultSearchProvider()); |
| 1635 } | 1636 } |
| 1636 | 1637 |
| 1637 TEST_F(TemplateURLServiceSyncTest, NewDefaultIsAlreadySynced) { | |
| 1638 // Ensure that if the synced DSP pref changed to another synced entry (as | |
| 1639 // opposed to coming in as a new entry), it gets reset correctly. | |
| 1640 // Start by setting kSyncedDefaultSearchProviderGUID to the entry that should | |
| 1641 // end up as the default. Note that this must be done before the initial | |
| 1642 // entries are added as otherwise this call will set the DSP immediately. | |
| 1643 profile_a()->GetTestingPrefService()->SetString( | |
| 1644 prefs::kSyncedDefaultSearchProviderGUID, "key2"); | |
| 1645 | |
| 1646 syncer::SyncDataList initial_data = CreateInitialSyncData(); | |
| 1647 // Ensure that our candidate default supports replacement. | |
| 1648 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key2"), | |
| 1649 "http://key2.com/{searchTerms}", "key2", 90)); | |
| 1650 initial_data[1] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); | |
| 1651 for (syncer::SyncDataList::const_iterator iter = initial_data.begin(); | |
| 1652 iter != initial_data.end(); ++iter) { | |
| 1653 TemplateURL* converted = Deserialize(*iter); | |
| 1654 model()->Add(converted); | |
| 1655 } | |
| 1656 | |
| 1657 // Set the initial default to something other than the desired default. | |
| 1658 model()->SetUserSelectedDefaultSearchProvider( | |
| 1659 model()->GetTemplateURLForGUID("key1")); | |
| 1660 | |
| 1661 // Merge in the same data (i.e. already synced entries). | |
| 1662 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, | |
| 1663 PassProcessor(), CreateAndPassSyncErrorFactory()); | |
| 1664 | |
| 1665 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | |
| 1666 TemplateURL* current_default = model()->GetDefaultSearchProvider(); | |
| 1667 ASSERT_TRUE(current_default); | |
| 1668 EXPECT_EQ("key2", current_default->sync_guid()); | |
| 1669 EXPECT_EQ(ASCIIToUTF16("key2"), current_default->keyword()); | |
| 1670 } | |
| 1671 | |
| 1672 TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) { | 1638 TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) { |
| 1673 // First start off with a few entries and make sure we can set an unmanaged | 1639 // First start off with a few entries and make sure we can set an unmanaged |
| 1674 // default search provider. | 1640 // default search provider. |
| 1675 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 1641 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
| 1676 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, | 1642 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, |
| 1677 PassProcessor(), CreateAndPassSyncErrorFactory()); | 1643 PassProcessor(), CreateAndPassSyncErrorFactory()); |
| 1678 model()->SetUserSelectedDefaultSearchProvider( | 1644 model()->SetUserSelectedDefaultSearchProvider( |
| 1679 model()->GetTemplateURLForGUID("key2")); | 1645 model()->GetTemplateURLForGUID("key2")); |
| 1680 | 1646 |
| 1681 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | 1647 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1874 | 1840 |
| 1875 // Merge the prepopulate search engines. | 1841 // Merge the prepopulate search engines. |
| 1876 base::Time pre_merge_time = base::Time::Now(); | 1842 base::Time pre_merge_time = base::Time::Now(); |
| 1877 base::RunLoop().RunUntilIdle(); | 1843 base::RunLoop().RunUntilIdle(); |
| 1878 test_util_a_.ResetModel(true); | 1844 test_util_a_.ResetModel(true); |
| 1879 | 1845 |
| 1880 // The newly added search engine should have been safely merged, with an | 1846 // The newly added search engine should have been safely merged, with an |
| 1881 // updated time. | 1847 // updated time. |
| 1882 TemplateURL* added_turl = model()->GetTemplateURLForKeyword( | 1848 TemplateURL* added_turl = model()->GetTemplateURLForKeyword( |
| 1883 ASCIIToUTF16(kNewKeyword)); | 1849 ASCIIToUTF16(kNewKeyword)); |
| 1850 ASSERT_TRUE(added_turl); |
| 1884 base::Time new_timestamp = added_turl->last_modified(); | 1851 base::Time new_timestamp = added_turl->last_modified(); |
| 1885 EXPECT_GE(new_timestamp, pre_merge_time); | 1852 EXPECT_GE(new_timestamp, pre_merge_time); |
| 1886 ASSERT_TRUE(added_turl); | |
| 1887 std::string sync_guid = added_turl->sync_guid(); | 1853 std::string sync_guid = added_turl->sync_guid(); |
| 1888 | 1854 |
| 1889 // Bring down a copy of the prepopulate engine from Sync with the old values, | 1855 // Bring down a copy of the prepopulate engine from Sync with the old values, |
| 1890 // including the old timestamp and the same GUID. Ensure that it loses | 1856 // including the old timestamp and the same GUID. Ensure that it loses |
| 1891 // conflict resolution against the local value, and an update is sent to the | 1857 // conflict resolution against the local value, and an update is sent to the |
| 1892 // server. The new timestamp should be preserved. | 1858 // server. The new timestamp should be preserved. |
| 1893 syncer::SyncDataList initial_data; | 1859 syncer::SyncDataList initial_data; |
| 1894 data_copy.SetKeyword(original_keyword); | 1860 data_copy.SetKeyword(original_keyword); |
| 1895 data_copy.sync_guid = sync_guid; | 1861 data_copy.sync_guid = sync_guid; |
| 1896 scoped_ptr<TemplateURL> sync_turl(new TemplateURL(profile_a(), data_copy)); | 1862 scoped_ptr<TemplateURL> sync_turl(new TemplateURL(profile_a(), data_copy)); |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2242 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( | 2208 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( |
| 2243 syncer::SEARCH_ENGINES, list, PassProcessor(), | 2209 syncer::SEARCH_ENGINES, list, PassProcessor(), |
| 2244 CreateAndPassSyncErrorFactory()); | 2210 CreateAndPassSyncErrorFactory()); |
| 2245 | 2211 |
| 2246 const TemplateURL* result_turl = model()->GetTemplateURLForGUID("default"); | 2212 const TemplateURL* result_turl = model()->GetTemplateURLForGUID("default"); |
| 2247 EXPECT_TRUE(result_turl); | 2213 EXPECT_TRUE(result_turl); |
| 2248 EXPECT_EQ(default_turl->keyword(), result_turl->keyword()); | 2214 EXPECT_EQ(default_turl->keyword(), result_turl->keyword()); |
| 2249 EXPECT_EQ(default_turl->short_name, result_turl->short_name()); | 2215 EXPECT_EQ(default_turl->short_name, result_turl->short_name()); |
| 2250 EXPECT_EQ(default_turl->url(), result_turl->url()); | 2216 EXPECT_EQ(default_turl->url(), result_turl->url()); |
| 2251 } | 2217 } |
| 2218 |
| 2219 TEST_F(TemplateURLServiceSyncTest, GUIDUpdatedOnDefaultSearchChange) { |
| 2220 const char kGUID[] = "initdefault"; |
| 2221 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("what"), |
| 2222 "http://thewhat.com/{searchTerms}", |
| 2223 kGUID)); |
| 2224 model()->SetUserSelectedDefaultSearchProvider( |
| 2225 model()->GetTemplateURLForGUID(kGUID)); |
| 2226 |
| 2227 const TemplateURL* default_search = model()->GetDefaultSearchProvider(); |
| 2228 ASSERT_TRUE(default_search); |
| 2229 |
| 2230 const char kNewGUID[] = "newdefault"; |
| 2231 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("what"), |
| 2232 "http://thewhat.com/{searchTerms}", |
| 2233 kNewGUID)); |
| 2234 model()->SetUserSelectedDefaultSearchProvider( |
| 2235 model()->GetTemplateURLForGUID(kNewGUID)); |
| 2236 |
| 2237 EXPECT_EQ(kNewGUID, profile_a()->GetTestingPrefService()->GetString( |
| 2238 prefs::kSyncedDefaultSearchProviderGUID)); |
| 2239 } |
| OLD | NEW |