Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
| 7 #include "base/time.h" | 7 #include "base/time.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/search_engines/template_url.h" | 9 #include "chrome/browser/search_engines/template_url.h" |
| 10 #include "chrome/browser/search_engines/template_url_service.h" | 10 #include "chrome/browser/search_engines/template_url_service.h" |
| 11 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 11 #include "chrome/browser/sync/protocol/search_engine_specifics.pb.h" | 12 #include "chrome/browser/sync/protocol/search_engine_specifics.pb.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | |
| 14 #include "chrome/common/pref_names.h" | |
| 15 #include "chrome/test/base/testing_pref_service.h" | |
| 12 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/public/browser/notification_service.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 19 |
| 15 using base::Time; | 20 using base::Time; |
| 16 | 21 |
| 17 namespace { | 22 namespace { |
| 18 | 23 |
| 19 // Extract the GUID from a search engine SyncData. | 24 // Extract the GUID from a search engine SyncData. |
| 20 std::string GetGUID(const SyncData& sync_data) { | 25 std::string GetGUID(const SyncData& sync_data) { |
| 21 return sync_data.GetSpecifics().GetExtension( | 26 return sync_data.GetSpecifics().GetExtension( |
| 22 sync_pb::search_engine).sync_guid(); | 27 sync_pb::search_engine).sync_guid(); |
| 23 } | 28 } |
| 24 | 29 |
| 25 // Extract the keyword from a search engine SyncData. | 30 // Extract the URL from a search engine SyncData. |
| 26 std::string GetURL(const SyncData& sync_data) { | 31 std::string GetURL(const SyncData& sync_data) { |
| 27 return sync_data.GetSpecifics().GetExtension( | 32 return sync_data.GetSpecifics().GetExtension( |
| 28 sync_pb::search_engine).url(); | 33 sync_pb::search_engine).url(); |
| 29 } | 34 } |
| 30 | 35 |
| 31 // Extract the keyword from a search engine SyncData. | 36 // Extract the keyword from a search engine SyncData. |
| 32 std::string GetKeyword(const SyncData& sync_data) { | 37 std::string GetKeyword(const SyncData& sync_data) { |
| 33 return sync_data.GetSpecifics().GetExtension( | 38 return sync_data.GetSpecifics().GetExtension( |
| 34 sync_pb::search_engine).keyword(); | 39 sync_pb::search_engine).keyword(); |
| 35 } | 40 } |
| 36 | 41 |
| 42 // TODO(stevet): Share these with template_url_service_unittest. | |
| 43 // Set the managed preferences for the default search provider and trigger | |
| 44 // notification. | |
| 45 void SetManagedDefaultSearchPreferences(TemplateURLService* turl_service, | |
| 46 TestingProfile* profile, | |
| 47 bool enabled, | |
| 48 const char* name, | |
| 49 const char* search_url, | |
| 50 const char* suggest_url, | |
| 51 const char* icon_url, | |
| 52 const char* encodings, | |
| 53 const char* keyword) { | |
| 54 TestingPrefService* pref_service = profile->GetTestingPrefService(); | |
| 55 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled, | |
|
Nicolas Zea
2011/11/04 23:08:06
Are these prefs always set all-at-once like this?
SteveT
2011/11/07 21:48:56
I believe the policy system allows an admin to set
| |
| 56 Value::CreateBooleanValue(enabled)); | |
| 57 pref_service->SetManagedPref(prefs::kDefaultSearchProviderName, | |
| 58 Value::CreateStringValue(name)); | |
| 59 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL, | |
| 60 Value::CreateStringValue(search_url)); | |
| 61 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL, | |
| 62 Value::CreateStringValue(suggest_url)); | |
| 63 pref_service->SetManagedPref(prefs::kDefaultSearchProviderIconURL, | |
| 64 Value::CreateStringValue(icon_url)); | |
| 65 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEncodings, | |
| 66 Value::CreateStringValue(encodings)); | |
| 67 pref_service->SetManagedPref(prefs::kDefaultSearchProviderKeyword, | |
| 68 Value::CreateStringValue(keyword)); | |
| 69 } | |
| 70 | |
| 71 // Remove all the managed preferences for the default search provider and | |
| 72 // trigger notification. | |
| 73 void RemoveManagedDefaultSearchPreferences(TemplateURLService* turl_service, | |
| 74 TestingProfile* profile) { | |
| 75 TestingPrefService* pref_service = profile->GetTestingPrefService(); | |
| 76 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL); | |
| 77 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled); | |
| 78 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderName); | |
| 79 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL); | |
| 80 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL); | |
| 81 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings); | |
| 82 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword); | |
| 83 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderID); | |
| 84 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID); | |
| 85 } | |
| 86 | |
| 37 // Dummy SyncChangeProcessor used to help review what SyncChanges are pushed | 87 // Dummy SyncChangeProcessor used to help review what SyncChanges are pushed |
| 38 // back up to Sync. | 88 // back up to Sync. |
| 39 class TestChangeProcessor : public SyncChangeProcessor { | 89 class TestChangeProcessor : public SyncChangeProcessor { |
| 40 public: | 90 public: |
| 41 TestChangeProcessor() : erroneous_(false) { | 91 TestChangeProcessor() : erroneous_(false) { |
| 42 } | 92 } |
| 43 virtual ~TestChangeProcessor() { } | 93 virtual ~TestChangeProcessor() { } |
| 44 | 94 |
| 45 // Store a copy of all the changes passed in so we can examine them later. | 95 // Store a copy of all the changes passed in so we can examine them later. |
| 46 virtual SyncError ProcessSyncChanges( | 96 virtual SyncError ProcessSyncChanges( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 }; | 130 }; |
| 81 | 131 |
| 82 class TemplateURLServiceSyncTest : public testing::Test { | 132 class TemplateURLServiceSyncTest : public testing::Test { |
| 83 public: | 133 public: |
| 84 typedef TemplateURLService::SyncDataMap SyncDataMap; | 134 typedef TemplateURLService::SyncDataMap SyncDataMap; |
| 85 | 135 |
| 86 TemplateURLServiceSyncTest() {} | 136 TemplateURLServiceSyncTest() {} |
| 87 | 137 |
| 88 virtual void SetUp() { | 138 virtual void SetUp() { |
| 89 profile_a_.reset(new TestingProfile); | 139 profile_a_.reset(new TestingProfile); |
| 140 TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( | |
| 141 profile_a_.get()); | |
| 90 model_a_.reset(new TemplateURLService(profile_a_.get())); | 142 model_a_.reset(new TemplateURLService(profile_a_.get())); |
| 91 model_a_->Load(); | 143 model_a_->Load(); |
| 92 profile_b_.reset(new TestingProfile); | 144 profile_b_.reset(new TestingProfile); |
| 145 TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile( | |
| 146 profile_b_.get()); | |
| 93 model_b_.reset(new TemplateURLService(profile_b_.get())); | 147 model_b_.reset(new TemplateURLService(profile_b_.get())); |
| 94 model_b_->Load(); | 148 model_b_->Load(); |
| 95 } | 149 } |
| 96 | 150 |
| 97 virtual void TearDown() { } | 151 virtual void TearDown() { } |
| 98 | 152 |
| 99 TemplateURLService* model() { return model_a_.get(); } | 153 TemplateURLService* model() { return model_a_.get(); } |
| 100 // For readability, we redefine an accessor for Model A for use in tests that | 154 // For readability, we redefine an accessor for Model A for use in tests that |
| 101 // involve syncing two models. | 155 // involve syncing two models. |
| 102 TemplateURLService* model_a() { return model_a_.get(); } | 156 TemplateURLService* model_a() { return model_a_.get(); } |
| (...skipping 951 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1054 error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, | 1108 error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, |
| 1055 initial_data, | 1109 initial_data, |
| 1056 processor()); | 1110 processor()); |
| 1057 ASSERT_FALSE(error.IsSet()); | 1111 ASSERT_FALSE(error.IsSet()); |
| 1058 | 1112 |
| 1059 // Check that the TemplateURL was not modified. | 1113 // Check that the TemplateURL was not modified. |
| 1060 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1"); | 1114 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1"); |
| 1061 ASSERT_TRUE(reupdated_turl); | 1115 ASSERT_TRUE(reupdated_turl); |
| 1062 AssertEquals(updated_turl, *reupdated_turl); | 1116 AssertEquals(updated_turl, *reupdated_turl); |
| 1063 } | 1117 } |
| 1118 | |
| 1119 TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) { | |
| 1120 // First start off with a few entries and make sure we can set an unmanaged | |
| 1121 // default search provider. | |
| 1122 SyncDataList initial_data = CreateInitialSyncData(); | |
| 1123 model()->MergeDataAndStartSyncing( | |
| 1124 syncable::SEARCH_ENGINES, | |
| 1125 initial_data, | |
| 1126 processor()); | |
| 1127 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2")); | |
| 1128 | |
| 1129 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); | |
| 1130 ASSERT_FALSE(model()->is_default_search_managed()); | |
| 1131 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | |
| 1132 | |
| 1133 // Change the default search provider to a managed one. | |
| 1134 const char kName[] = "manageddefault"; | |
| 1135 const char kSearchURL[] = "http://manageddefault.com/search?t={searchTerms}"; | |
| 1136 const char kIconURL[] = "http://manageddefault.com/icon.jpg"; | |
| 1137 const char kEncodings[] = "UTF-16;UTF-32"; | |
| 1138 SetManagedDefaultSearchPreferences(model(), profile_a_.get(), true, kName, | |
| 1139 kSearchURL, "", kIconURL, kEncodings, ""); | |
| 1140 const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider(); | |
| 1141 | |
| 1142 EXPECT_TRUE(model()->is_default_search_managed()); | |
| 1143 | |
| 1144 // Add a new entry from Sync. It should still sync in despite the default | |
| 1145 // being managed. | |
| 1146 SyncChangeList changes; | |
| 1147 changes.push_back(CreateTestSyncChange( | |
| 1148 SyncChange::ACTION_ADD, | |
| 1149 CreateTestTemplateURL("newkeyword", "http://new.com", "newdefault"))); | |
| 1150 model()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1151 | |
| 1152 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); | |
| 1153 | |
| 1154 // Change kSyncedDefaultSearchProviderGUID to point to the new entry and | |
| 1155 // ensure that the DSP remains managed. | |
| 1156 profile_a_->GetTestingPrefService()->SetString( | |
| 1157 prefs::kSyncedDefaultSearchProviderGUID, | |
| 1158 "newdefault"); | |
| 1159 | |
| 1160 EXPECT_EQ(dsp_turl, model()->GetDefaultSearchProvider()); | |
| 1161 EXPECT_TRUE(model()->is_default_search_managed()); | |
| 1162 EXPECT_TRUE(profile_a_->GetTestingPrefService()->GetBoolean( | |
| 1163 prefs::kPendingSyncedDefaultSearchProviderChange)); | |
| 1164 | |
| 1165 // Go unmanaged. Ensure that the DSP changes to the expected pending entry | |
| 1166 // from Sync. | |
| 1167 const TemplateURL* expected_default = | |
| 1168 model()->GetTemplateURLForGUID("newdefault"); | |
| 1169 RemoveManagedDefaultSearchPreferences(model(), profile_a_.get()); | |
| 1170 | |
| 1171 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); | |
| 1172 EXPECT_FALSE(profile_a_->GetTestingPrefService()->GetBoolean( | |
| 1173 prefs::kPendingSyncedDefaultSearchProviderChange)); | |
|
Nicolas Zea
2011/11/04 23:08:06
add a case where the synceddefaultsearchprovidergu
SteveT
2011/11/07 21:48:56
Ah yes, of course :)
Added SyncedDefaultGUIDArriv
| |
| 1174 } | |
| OLD | NEW |