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

Side by Side Diff: chrome/browser/sync/profile_sync_service_preference_unittest.cc

Issue 218903005: Make push messaging not create InvalidationService for login and guest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make code resilient against tests that do not create the Default profile first. Created 6 years, 8 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 <map> 5 #include <map>
6 #include <string> 6 #include <string>
7 #include <utility>
7 8
8 #include "base/bind.h" 9 #include "base/bind.h"
9 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
10 #include "base/callback.h" 11 #include "base/callback.h"
11 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
12 #include "base/json/json_string_value_serializer.h" 13 #include "base/json/json_string_value_serializer.h"
13 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
14 #include "base/location.h" 15 #include "base/location.h"
15 #include "base/prefs/scoped_user_pref_update.h" 16 #include "base/prefs/scoped_user_pref_update.h"
16 #include "base/stl_util.h" 17 #include "base/stl_util.h"
17 #include "base/strings/string_piece.h" 18 #include "base/strings/string_piece.h"
19 #include "base/strings/utf_string_conversions.h"
18 #include "chrome/browser/invalidation/fake_invalidation_service.h" 20 #include "chrome/browser/invalidation/fake_invalidation_service.h"
19 #include "chrome/browser/invalidation/invalidation_service_factory.h" 21 #include "chrome/browser/invalidation/invalidation_service_factory.h"
20 #include "chrome/browser/prefs/pref_model_associator.h" 22 #include "chrome/browser/prefs/pref_model_associator.h"
23 #include "chrome/browser/prefs/pref_service_syncable.h"
21 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" 24 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
22 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" 25 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 26 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
24 #include "chrome/browser/signin/signin_manager_factory.h" 27 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" 28 #include "chrome/browser/sync/abstract_profile_sync_service_test.h"
26 #include "chrome/browser/sync/glue/generic_change_processor.h" 29 #include "chrome/browser/sync/glue/generic_change_processor.h"
27 #include "chrome/browser/sync/glue/sync_backend_host.h" 30 #include "chrome/browser/sync/glue/sync_backend_host.h"
28 #include "chrome/browser/sync/glue/ui_data_type_controller.h" 31 #include "chrome/browser/sync/glue/ui_data_type_controller.h"
29 #include "chrome/browser/sync/profile_sync_service_factory.h" 32 #include "chrome/browser/sync/profile_sync_service_factory.h"
30 #include "chrome/browser/sync/profile_sync_test_util.h" 33 #include "chrome/browser/sync/profile_sync_test_util.h"
31 #include "chrome/browser/sync/test_profile_sync_service.h" 34 #include "chrome/browser/sync/test_profile_sync_service.h"
32 #include "chrome/common/pref_names.h" 35 #include "chrome/common/pref_names.h"
36 #include "chrome/test/base/testing_browser_process.h"
33 #include "chrome/test/base/testing_pref_service_syncable.h" 37 #include "chrome/test/base/testing_pref_service_syncable.h"
34 #include "chrome/test/base/testing_profile.h" 38 #include "chrome/test/base/testing_profile.h"
39 #include "chrome/test/base/testing_profile_manager.h"
35 #include "components/signin/core/browser/signin_manager.h" 40 #include "components/signin/core/browser/signin_manager.h"
36 #include "components/user_prefs/pref_registry_syncable.h" 41 #include "components/user_prefs/pref_registry_syncable.h"
37 #include "google_apis/gaia/gaia_constants.h" 42 #include "google_apis/gaia/gaia_constants.h"
38 #include "sync/api/attachments/fake_attachment_service.h" 43 #include "sync/api/attachments/fake_attachment_service.h"
39 #include "sync/api/sync_data.h" 44 #include "sync/api/sync_data.h"
40 #include "sync/internal_api/public/base/model_type.h" 45 #include "sync/internal_api/public/base/model_type.h"
41 #include "sync/internal_api/public/change_record.h" 46 #include "sync/internal_api/public/change_record.h"
42 #include "sync/internal_api/public/data_type_debug_info_listener.h" 47 #include "sync/internal_api/public/data_type_debug_info_listener.h"
43 #include "sync/internal_api/public/read_node.h" 48 #include "sync/internal_api/public/read_node.h"
44 #include "sync/internal_api/public/read_transaction.h" 49 #include "sync/internal_api/public/read_transaction.h"
45 #include "sync/internal_api/public/write_node.h" 50 #include "sync/internal_api/public/write_node.h"
46 #include "sync/internal_api/public/write_transaction.h" 51 #include "sync/internal_api/public/write_transaction.h"
47 #include "sync/protocol/preference_specifics.pb.h" 52 #include "sync/protocol/preference_specifics.pb.h"
48 #include "testing/gmock/include/gmock/gmock.h" 53 #include "testing/gmock/include/gmock/gmock.h"
49 #include "testing/gtest/include/gtest/gtest.h" 54 #include "testing/gtest/include/gtest/gtest.h"
50 55
51 using base::JSONReader; 56 using base::JSONReader;
52 using browser_sync::GenericChangeProcessor; 57 using browser_sync::GenericChangeProcessor;
53 using browser_sync::SharedChangeProcessor; 58 using browser_sync::SharedChangeProcessor;
54 using browser_sync::UIDataTypeController; 59 using browser_sync::UIDataTypeController;
55 using syncer::ChangeRecord; 60 using syncer::ChangeRecord;
56 using testing::Invoke; 61 using testing::Invoke;
57 using testing::Return; 62 using testing::Return;
58 using testing::_; 63 using testing::_;
59 64
65 namespace {
66 const char kTestProfileName[] = "test-profile";
67 }
68
60 typedef std::map<const std::string, const base::Value*> PreferenceValues; 69 typedef std::map<const std::string, const base::Value*> PreferenceValues;
61 70
62 ACTION_P(CreateAndSaveChangeProcessor, change_processor) { 71 ACTION_P(CreateAndSaveChangeProcessor, change_processor) {
63 syncer::UserShare* user_share = arg0->GetUserShare(); 72 syncer::UserShare* user_share = arg0->GetUserShare();
64 *change_processor = new GenericChangeProcessor( 73 *change_processor = new GenericChangeProcessor(
65 arg1, 74 arg1,
66 arg2, 75 arg2,
67 arg3, 76 arg3,
68 user_share, 77 user_share,
69 syncer::FakeAttachmentService::CreateForTest()); 78 syncer::FakeAttachmentService::CreateForTest());
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 // DataTypeDebugInfoListener implementation. 121 // DataTypeDebugInfoListener implementation.
113 virtual void OnDataTypeConfigureComplete( 122 virtual void OnDataTypeConfigureComplete(
114 const std::vector<syncer::DataTypeConfigurationStats>& 123 const std::vector<syncer::DataTypeConfigurationStats>&
115 configuration_stats) OVERRIDE { 124 configuration_stats) OVERRIDE {
116 ASSERT_EQ(1u, configuration_stats.size()); 125 ASSERT_EQ(1u, configuration_stats.size());
117 association_stats_ = configuration_stats[0].association_stats; 126 association_stats_ = configuration_stats[0].association_stats;
118 } 127 }
119 128
120 protected: 129 protected:
121 ProfileSyncServicePreferenceTest() 130 ProfileSyncServicePreferenceTest()
122 : debug_ptr_factory_(this), 131 : profile_manager_(TestingBrowserProcess::GetGlobal()),
132 debug_ptr_factory_(this),
123 example_url0_("http://example.com/0"), 133 example_url0_("http://example.com/0"),
124 example_url1_("http://example.com/1"), 134 example_url1_("http://example.com/1"),
125 example_url2_("http://example.com/2"), 135 example_url2_("http://example.com/2"),
126 not_synced_preference_name_("nonsense_pref_name"), 136 not_synced_preference_name_("nonsense_pref_name"),
127 not_synced_preference_default_value_("default"), 137 not_synced_preference_default_value_("default"),
128 non_default_charset_value_("foo") {} 138 non_default_charset_value_("foo") {}
129 139
130 virtual void SetUp() { 140 virtual void SetUp() {
131 AbstractProfileSyncServiceTest::SetUp(); 141 AbstractProfileSyncServiceTest::SetUp();
132 TestingProfile::Builder builder; 142 ASSERT_TRUE(profile_manager_.SetUp());
133 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), 143 TestingProfile::TestingFactories testing_factories;
134 BuildAutoIssuingFakeProfileOAuth2TokenService); 144 testing_factories.push_back(std::make_pair(
135 profile_ = builder.Build().Pass(); 145 ProfileOAuth2TokenServiceFactory::GetInstance(),
146 BuildAutoIssuingFakeProfileOAuth2TokenService));
147 profile_ = profile_manager_.CreateTestingProfile(
148 kTestProfileName,
149 scoped_ptr<PrefServiceSyncable>(),
150 base::UTF8ToUTF16(kTestProfileName),
151 0,
152 std::string(),
153 testing_factories);
136 invalidation::InvalidationServiceFactory::GetInstance()->SetTestingFactory( 154 invalidation::InvalidationServiceFactory::GetInstance()->SetTestingFactory(
137 profile_.get(), invalidation::FakeInvalidationService::Build); 155 profile_, invalidation::FakeInvalidationService::Build);
138 prefs_ = profile_->GetTestingPrefService(); 156 prefs_ = profile_->GetTestingPrefService();
139 157
140 prefs_->registry()->RegisterStringPref( 158 prefs_->registry()->RegisterStringPref(
141 not_synced_preference_name_.c_str(), 159 not_synced_preference_name_.c_str(),
142 not_synced_preference_default_value_, 160 not_synced_preference_default_value_,
143 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 161 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
144 } 162 }
145 163
146 virtual void TearDown() { 164 virtual void TearDown() {
147 profile_.reset(); 165 profile_ = NULL;
166 profile_manager_.DeleteTestingProfile(kTestProfileName);
148 AbstractProfileSyncServiceTest::TearDown(); 167 AbstractProfileSyncServiceTest::TearDown();
149 } 168 }
150 169
151 int GetSyncPreferenceCount() { 170 int GetSyncPreferenceCount() {
152 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); 171 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare());
153 syncer::ReadNode node(&trans); 172 syncer::ReadNode node(&trans);
154 if (node.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::PREFERENCES)) != 173 if (node.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::PREFERENCES)) !=
155 syncer::BaseNode::INIT_OK) 174 syncer::BaseNode::INIT_OK)
156 return 0; 175 return 0;
157 return node.GetTotalNodeCount() - 1; 176 return node.GetTotalNodeCount() - 1;
158 } 177 }
159 178
160 bool StartSyncService(const base::Closure& callback, 179 bool StartSyncService(const base::Closure& callback,
161 bool will_fail_association) { 180 bool will_fail_association) {
162 if (sync_service_) 181 if (sync_service_)
163 return false; 182 return false;
164 183
165 SigninManagerBase* signin = 184 SigninManagerBase* signin =
166 SigninManagerFactory::GetForProfile(profile_.get()); 185 SigninManagerFactory::GetForProfile(profile_);
167 signin->SetAuthenticatedUsername("test"); 186 signin->SetAuthenticatedUsername("test");
168 sync_service_ = TestProfileSyncService::BuildAutoStartAsyncInit( 187 sync_service_ = TestProfileSyncService::BuildAutoStartAsyncInit(profile_,
169 profile_.get(), callback); 188 callback);
170 pref_sync_service_ = reinterpret_cast<PrefModelAssociator*>( 189 pref_sync_service_ = reinterpret_cast<PrefModelAssociator*>(
171 prefs_->GetSyncableService(syncer::PREFERENCES)); 190 prefs_->GetSyncableService(syncer::PREFERENCES));
172 if (!pref_sync_service_) 191 if (!pref_sync_service_)
173 return false; 192 return false;
174 ProfileSyncComponentsFactoryMock* components = 193 ProfileSyncComponentsFactoryMock* components =
175 sync_service_->components_factory_mock(); 194 sync_service_->components_factory_mock();
176 EXPECT_CALL(*components, GetSyncableServiceForType(syncer::PREFERENCES)). 195 EXPECT_CALL(*components, GetSyncableServiceForType(syncer::PREFERENCES)).
177 WillOnce(Return(pref_sync_service_->AsWeakPtr())); 196 WillOnce(Return(pref_sync_service_->AsWeakPtr()));
178 197
179 EXPECT_CALL(*components, CreateDataTypeManager(_, _, _, _, _, _)). 198 EXPECT_CALL(*components, CreateDataTypeManager(_, _, _, _, _, _)).
180 WillOnce(ReturnNewDataTypeManagerWithDebugListener( 199 WillOnce(ReturnNewDataTypeManagerWithDebugListener(
181 syncer::MakeWeakHandle(debug_ptr_factory_.GetWeakPtr()))); 200 syncer::MakeWeakHandle(debug_ptr_factory_.GetWeakPtr())));
182 dtc_ = new UIDataTypeController(base::MessageLoopProxy::current(), 201 dtc_ = new UIDataTypeController(base::MessageLoopProxy::current(),
183 base::Closure(), 202 base::Closure(),
184 syncer::PREFERENCES, 203 syncer::PREFERENCES,
185 components, 204 components,
186 profile_.get(), 205 profile_,
187 sync_service_); 206 sync_service_);
188 EXPECT_CALL(*components, CreateSharedChangeProcessor()). 207 EXPECT_CALL(*components, CreateSharedChangeProcessor()).
189 WillOnce(Return(new SharedChangeProcessor())); 208 WillOnce(Return(new SharedChangeProcessor()));
190 EXPECT_CALL(*components, CreateGenericChangeProcessor(_, _, _, _)). 209 EXPECT_CALL(*components, CreateGenericChangeProcessor(_, _, _, _)).
191 WillOnce(CreateAndSaveChangeProcessor( 210 WillOnce(CreateAndSaveChangeProcessor(
192 &change_processor_)); 211 &change_processor_));
193 sync_service_->RegisterDataTypeController(dtc_); 212 sync_service_->RegisterDataTypeController(dtc_);
194 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()) 213 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)
195 ->UpdateCredentials("test", "oauth2_login_token"); 214 ->UpdateCredentials("test", "oauth2_login_token");
196 215
197 sync_service_->Initialize(); 216 sync_service_->Initialize();
198 base::MessageLoop::current()->Run(); 217 base::MessageLoop::current()->Run();
199 218
200 // It's possible this test triggered an unrecoverable error, in which case 219 // It's possible this test triggered an unrecoverable error, in which case
201 // we can't get the preference count. 220 // we can't get the preference count.
202 if (sync_service_->ShouldPushChanges()) { 221 if (sync_service_->ShouldPushChanges()) {
203 EXPECT_EQ(GetSyncPreferenceCount(), 222 EXPECT_EQ(GetSyncPreferenceCount(),
204 association_stats_.num_sync_items_after_association); 223 association_stats_.num_sync_items_after_association);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 282
264 // Returns whether a given preference name is a new name of a migrated 283 // Returns whether a given preference name is a new name of a migrated
265 // preference. Exposed here for testing. 284 // preference. Exposed here for testing.
266 static bool IsMigratedPreference(const char* preference_name) { 285 static bool IsMigratedPreference(const char* preference_name) {
267 return PrefModelAssociator::IsMigratedPreference(preference_name); 286 return PrefModelAssociator::IsMigratedPreference(preference_name);
268 } 287 }
269 static bool IsOldMigratedPreference(const char* old_preference_name) { 288 static bool IsOldMigratedPreference(const char* old_preference_name) {
270 return PrefModelAssociator::IsOldMigratedPreference(old_preference_name); 289 return PrefModelAssociator::IsOldMigratedPreference(old_preference_name);
271 } 290 }
272 291
273 scoped_ptr<TestingProfile> profile_; 292 TestingProfileManager profile_manager_;
293 TestingProfile* profile_;
274 TestingPrefServiceSyncable* prefs_; 294 TestingPrefServiceSyncable* prefs_;
275 295
276 UIDataTypeController* dtc_; 296 UIDataTypeController* dtc_;
277 PrefModelAssociator* pref_sync_service_; 297 PrefModelAssociator* pref_sync_service_;
278 GenericChangeProcessor* change_processor_; 298 GenericChangeProcessor* change_processor_;
279 syncer::DataTypeAssociationStats association_stats_; 299 syncer::DataTypeAssociationStats association_stats_;
280 base::WeakPtrFactory<DataTypeDebugInfoListener> debug_ptr_factory_; 300 base::WeakPtrFactory<DataTypeDebugInfoListener> debug_ptr_factory_;
281 301
282 std::string example_url0_; 302 std::string example_url0_;
283 std::string example_url1_; 303 std::string example_url1_;
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after
887 EXPECT_FALSE(pref->IsDefaultValue()); 907 EXPECT_FALSE(pref->IsDefaultValue());
888 // There should be no synced value. 908 // There should be no synced value.
889 EXPECT_TRUE(GetSyncedValue(prefs::kHomePage) == NULL); 909 EXPECT_TRUE(GetSyncedValue(prefs::kHomePage) == NULL);
890 // Switch kHomePage back to unmanaged. 910 // Switch kHomePage back to unmanaged.
891 profile_->GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage); 911 profile_->GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage);
892 // The original value should be picked up. 912 // The original value should be picked up.
893 EXPECT_TRUE(pref->IsDefaultValue()); 913 EXPECT_TRUE(pref->IsDefaultValue());
894 // There should still be no synced value. 914 // There should still be no synced value.
895 EXPECT_TRUE(GetSyncedValue(prefs::kHomePage) == NULL); 915 EXPECT_TRUE(GetSyncedValue(prefs::kHomePage) == NULL);
896 } 916 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698