| Index: chrome/browser/search_engines/template_url_service_sync_unittest.cc
|
| ===================================================================
|
| --- chrome/browser/search_engines/template_url_service_sync_unittest.cc (revision 108905)
|
| +++ chrome/browser/search_engines/template_url_service_sync_unittest.cc (working copy)
|
| @@ -8,8 +8,13 @@
|
| #include "base/utf_string_conversions.h"
|
| #include "chrome/browser/search_engines/template_url.h"
|
| #include "chrome/browser/search_engines/template_url_service.h"
|
| +#include "chrome/browser/search_engines/template_url_service_factory.h"
|
| #include "chrome/browser/sync/protocol/search_engine_specifics.pb.h"
|
| +#include "chrome/common/chrome_notification_types.h"
|
| +#include "chrome/common/pref_names.h"
|
| +#include "chrome/test/base/testing_pref_service.h"
|
| #include "chrome/test/base/testing_profile.h"
|
| +#include "content/public/browser/notification_service.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| using base::Time;
|
| @@ -22,7 +27,7 @@
|
| sync_pb::search_engine).sync_guid();
|
| }
|
|
|
| -// Extract the keyword from a search engine SyncData.
|
| +// Extract the URL from a search engine SyncData.
|
| std::string GetURL(const SyncData& sync_data) {
|
| return sync_data.GetSpecifics().GetExtension(
|
| sync_pb::search_engine).url();
|
| @@ -34,6 +39,51 @@
|
| sync_pb::search_engine).keyword();
|
| }
|
|
|
| +// TODO(stevet): Share these with template_url_service_unittest.
|
| +// Set the managed preferences for the default search provider and trigger
|
| +// notification.
|
| +void SetManagedDefaultSearchPreferences(TemplateURLService* turl_service,
|
| + TestingProfile* profile,
|
| + bool enabled,
|
| + const char* name,
|
| + const char* search_url,
|
| + const char* suggest_url,
|
| + const char* icon_url,
|
| + const char* encodings,
|
| + const char* keyword) {
|
| + TestingPrefService* pref_service = profile->GetTestingPrefService();
|
| + pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
|
| + Value::CreateBooleanValue(enabled));
|
| + pref_service->SetManagedPref(prefs::kDefaultSearchProviderName,
|
| + Value::CreateStringValue(name));
|
| + pref_service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL,
|
| + Value::CreateStringValue(search_url));
|
| + pref_service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL,
|
| + Value::CreateStringValue(suggest_url));
|
| + pref_service->SetManagedPref(prefs::kDefaultSearchProviderIconURL,
|
| + Value::CreateStringValue(icon_url));
|
| + pref_service->SetManagedPref(prefs::kDefaultSearchProviderEncodings,
|
| + Value::CreateStringValue(encodings));
|
| + pref_service->SetManagedPref(prefs::kDefaultSearchProviderKeyword,
|
| + Value::CreateStringValue(keyword));
|
| +}
|
| +
|
| +// Remove all the managed preferences for the default search provider and
|
| +// trigger notification.
|
| +void RemoveManagedDefaultSearchPreferences(TemplateURLService* turl_service,
|
| + TestingProfile* profile) {
|
| + TestingPrefService* pref_service = profile->GetTestingPrefService();
|
| + pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL);
|
| + pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled);
|
| + pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderName);
|
| + pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL);
|
| + pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL);
|
| + pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings);
|
| + pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword);
|
| + pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderID);
|
| + pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID);
|
| +}
|
| +
|
| // Dummy SyncChangeProcessor used to help review what SyncChanges are pushed
|
| // back up to Sync.
|
| class TestChangeProcessor : public SyncChangeProcessor {
|
| @@ -87,9 +137,13 @@
|
|
|
| virtual void SetUp() {
|
| profile_a_.reset(new TestingProfile);
|
| + TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile(
|
| + profile_a_.get());
|
| model_a_.reset(new TemplateURLService(profile_a_.get()));
|
| model_a_->Load();
|
| profile_b_.reset(new TestingProfile);
|
| + TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile(
|
| + profile_b_.get());
|
| model_b_.reset(new TemplateURLService(profile_b_.get()));
|
| model_b_->Load();
|
| }
|
| @@ -1061,3 +1115,134 @@
|
| ASSERT_TRUE(reupdated_turl);
|
| AssertEquals(updated_turl, *reupdated_turl);
|
| }
|
| +
|
| +TEST_F(TemplateURLServiceSyncTest, SyncedDefaultGUIDArrivesFirst) {
|
| + SyncDataList initial_data = CreateInitialSyncData();
|
| + model()->MergeDataAndStartSyncing(
|
| + syncable::SEARCH_ENGINES,
|
| + initial_data,
|
| + processor());
|
| + model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2"));
|
| +
|
| + EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
|
| + const TemplateURL* default_search = model()->GetDefaultSearchProvider();
|
| + ASSERT_TRUE(default_search);
|
| +
|
| + // Change kSyncedDefaultSearchProviderGUID to a GUID that does not exist in
|
| + // the model yet. Ensure that the default has not changed in any way.
|
| + profile_a_->GetTestingPrefService()->SetString(
|
| + prefs::kSyncedDefaultSearchProviderGUID,
|
| + "newdefault");
|
| +
|
| + ASSERT_EQ(default_search, model()->GetDefaultSearchProvider());
|
| +
|
| + // Bring in a random new search engine with a different GUID. Ensure that
|
| + // it doesn't change the default.
|
| + SyncChangeList changes1;
|
| + changes1.push_back(CreateTestSyncChange(
|
| + SyncChange::ACTION_ADD,
|
| + CreateTestTemplateURL("random", "http://random.com", "random")));
|
| + model()->ProcessSyncChanges(FROM_HERE, changes1);
|
| +
|
| + EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
|
| + ASSERT_EQ(default_search, model()->GetDefaultSearchProvider());
|
| +
|
| + // Finally, bring in the expected entry with the right GUID. Ensure that
|
| + // the default has changed to the new search engine.
|
| + SyncChangeList changes2;
|
| + changes2.push_back(CreateTestSyncChange(
|
| + SyncChange::ACTION_ADD,
|
| + CreateTestTemplateURL("new", "http://new.com", "newdefault")));
|
| + model()->ProcessSyncChanges(FROM_HERE, changes2);
|
| +
|
| + EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
|
| + ASSERT_NE(default_search, model()->GetDefaultSearchProvider());
|
| + ASSERT_EQ("newdefault", model()->GetDefaultSearchProvider()->sync_guid());
|
| +}
|
| +
|
| +TEST_F(TemplateURLServiceSyncTest, SyncedDefaultArrivesAfterStartup) {
|
| + // Start with the default set to something in the model before we start
|
| + // syncing.
|
| + model()->Add(CreateTestTemplateURL(
|
| + "what", "http://thewhat.com", "initdefault"));
|
| + model()->SetDefaultSearchProvider(
|
| + model()->GetTemplateURLForGUID("initdefault"));
|
| +
|
| + const TemplateURL* default_search = model()->GetDefaultSearchProvider();
|
| + ASSERT_TRUE(default_search);
|
| +
|
| + // Set kSyncedDefaultSearchProviderGUID to something that is not yet in
|
| + // the model but is expected in the initial sync. Ensure that this doesn't
|
| + // change our default since we're not quite syncing yet.
|
| + profile_a_->GetTestingPrefService()->SetString(
|
| + prefs::kSyncedDefaultSearchProviderGUID,
|
| + "key2");
|
| +
|
| + EXPECT_EQ(default_search, model()->GetDefaultSearchProvider());
|
| +
|
| + // Now sync the initial data, which will include the search engine entry
|
| + // destined to become the new default.
|
| + SyncDataList initial_data = CreateInitialSyncData();
|
| + model()->MergeDataAndStartSyncing(
|
| + syncable::SEARCH_ENGINES,
|
| + initial_data,
|
| + processor());
|
| +
|
| + // Ensure that the new default has been set.
|
| + EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
|
| + ASSERT_NE(default_search, model()->GetDefaultSearchProvider());
|
| + ASSERT_EQ("key2", model()->GetDefaultSearchProvider()->sync_guid());
|
| +}
|
| +
|
| +TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) {
|
| + // First start off with a few entries and make sure we can set an unmanaged
|
| + // default search provider.
|
| + SyncDataList initial_data = CreateInitialSyncData();
|
| + model()->MergeDataAndStartSyncing(
|
| + syncable::SEARCH_ENGINES,
|
| + initial_data,
|
| + processor());
|
| + model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2"));
|
| +
|
| + EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
|
| + ASSERT_FALSE(model()->is_default_search_managed());
|
| + ASSERT_TRUE(model()->GetDefaultSearchProvider());
|
| +
|
| + // Change the default search provider to a managed one.
|
| + const char kName[] = "manageddefault";
|
| + const char kSearchURL[] = "http://manageddefault.com/search?t={searchTerms}";
|
| + const char kIconURL[] = "http://manageddefault.com/icon.jpg";
|
| + const char kEncodings[] = "UTF-16;UTF-32";
|
| + SetManagedDefaultSearchPreferences(model(), profile_a_.get(), true, kName,
|
| + kSearchURL, "", kIconURL, kEncodings, "");
|
| + const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider();
|
| +
|
| + EXPECT_TRUE(model()->is_default_search_managed());
|
| +
|
| + // Add a new entry from Sync. It should still sync in despite the default
|
| + // being managed.
|
| + SyncChangeList changes;
|
| + changes.push_back(CreateTestSyncChange(
|
| + SyncChange::ACTION_ADD,
|
| + CreateTestTemplateURL("newkeyword", "http://new.com", "newdefault")));
|
| + model()->ProcessSyncChanges(FROM_HERE, changes);
|
| +
|
| + EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
|
| +
|
| + // Change kSyncedDefaultSearchProviderGUID to point to the new entry and
|
| + // ensure that the DSP remains managed.
|
| + profile_a_->GetTestingPrefService()->SetString(
|
| + prefs::kSyncedDefaultSearchProviderGUID,
|
| + "newdefault");
|
| +
|
| + EXPECT_EQ(dsp_turl, model()->GetDefaultSearchProvider());
|
| + EXPECT_TRUE(model()->is_default_search_managed());
|
| +
|
| + // Go unmanaged. Ensure that the DSP changes to the expected pending entry
|
| + // from Sync.
|
| + const TemplateURL* expected_default =
|
| + model()->GetTemplateURLForGUID("newdefault");
|
| + RemoveManagedDefaultSearchPreferences(model(), profile_a_.get());
|
| +
|
| + EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider());
|
| +}
|
|
|