| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/managed_mode/managed_user_refresh_token_fetcher.h" |
| 9 #include "chrome/browser/managed_mode/managed_user_registration_service.h" | 12 #include "chrome/browser/managed_mode/managed_user_registration_service.h" |
| 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 11 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/testing_pref_service_syncable.h" | 15 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 13 #include "google_apis/gaia/google_service_auth_error.h" | 16 #include "google_apis/gaia/google_service_auth_error.h" |
| 14 #include "sync/api/sync_change.h" | 17 #include "sync/api/sync_change.h" |
| 15 #include "sync/api/sync_error_factory_mock.h" | 18 #include "sync/api/sync_error_factory_mock.h" |
| 16 #include "sync/protocol/sync.pb.h" | 19 #include "sync/protocol/sync.pb.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 21 |
| 19 using ::sync_pb::ManagedUserSpecifics; | 22 using ::sync_pb::ManagedUserSpecifics; |
| 20 using syncer::MANAGED_USERS; | 23 using syncer::MANAGED_USERS; |
| 21 using syncer::SyncChange; | 24 using syncer::SyncChange; |
| 22 using syncer::SyncChangeList; | 25 using syncer::SyncChangeList; |
| 23 using syncer::SyncChangeProcessor; | 26 using syncer::SyncChangeProcessor; |
| 24 using syncer::SyncData; | 27 using syncer::SyncData; |
| 25 using syncer::SyncDataList; | 28 using syncer::SyncDataList; |
| 26 using syncer::SyncError; | 29 using syncer::SyncError; |
| 27 using syncer::SyncErrorFactory; | 30 using syncer::SyncErrorFactory; |
| 28 using syncer::SyncMergeResult; | 31 using syncer::SyncMergeResult; |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 35 const char kManagedUserToken[] = "managedusertoken"; |
| 36 |
| 32 class MockChangeProcessor : public SyncChangeProcessor { | 37 class MockChangeProcessor : public SyncChangeProcessor { |
| 33 public: | 38 public: |
| 34 MockChangeProcessor() {} | 39 MockChangeProcessor() {} |
| 35 virtual ~MockChangeProcessor() {} | 40 virtual ~MockChangeProcessor() {} |
| 36 | 41 |
| 37 // SyncChangeProcessor implementation: | 42 // SyncChangeProcessor implementation: |
| 38 virtual SyncError ProcessSyncChanges( | 43 virtual SyncError ProcessSyncChanges( |
| 39 const tracked_objects::Location& from_here, | 44 const tracked_objects::Location& from_here, |
| 40 const SyncChangeList& change_list) OVERRIDE; | 45 const SyncChangeList& change_list) OVERRIDE; |
| 41 | 46 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 55 | 60 |
| 56 SyncChange MockChangeProcessor::GetChange(const std::string& id) const { | 61 SyncChange MockChangeProcessor::GetChange(const std::string& id) const { |
| 57 for (SyncChangeList::const_iterator it = change_list_.begin(); | 62 for (SyncChangeList::const_iterator it = change_list_.begin(); |
| 58 it != change_list_.end(); ++it) { | 63 it != change_list_.end(); ++it) { |
| 59 if (it->sync_data().GetSpecifics().managed_user().id() == id) | 64 if (it->sync_data().GetSpecifics().managed_user().id() == id) |
| 60 return *it; | 65 return *it; |
| 61 } | 66 } |
| 62 return SyncChange(); | 67 return SyncChange(); |
| 63 } | 68 } |
| 64 | 69 |
| 70 class MockManagedUserRefreshTokenFetcher |
| 71 : public ManagedUserRefreshTokenFetcher { |
| 72 public: |
| 73 MockManagedUserRefreshTokenFetcher() {} |
| 74 virtual ~MockManagedUserRefreshTokenFetcher() {} |
| 75 |
| 76 // ManagedUserRefreshTokenFetcher implementation: |
| 77 virtual void Start(const std::string& managed_user_id, |
| 78 const string16& name, |
| 79 const std::string& device_name, |
| 80 const TokenCallback& callback) OVERRIDE { |
| 81 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); |
| 82 callback.Run(error, kManagedUserToken); |
| 83 } |
| 84 }; |
| 85 |
| 65 } // namespace | 86 } // namespace |
| 66 | 87 |
| 67 class ManagedUserRegistrationServiceTest : public ::testing::Test { | 88 class ManagedUserRegistrationServiceTest : public ::testing::Test { |
| 68 public: | 89 public: |
| 69 ManagedUserRegistrationServiceTest(); | 90 ManagedUserRegistrationServiceTest(); |
| 70 virtual ~ManagedUserRegistrationServiceTest(); | 91 virtual ~ManagedUserRegistrationServiceTest(); |
| 71 | 92 |
| 72 protected: | 93 protected: |
| 73 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor(); | 94 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor(); |
| 74 scoped_ptr<SyncErrorFactory> CreateErrorFactory(); | 95 scoped_ptr<SyncErrorFactory> CreateErrorFactory(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 87 MockChangeProcessor* change_processor() { return change_processor_; } | 108 MockChangeProcessor* change_processor() { return change_processor_; } |
| 88 | 109 |
| 89 bool received_callback() const { return received_callback_; } | 110 bool received_callback() const { return received_callback_; } |
| 90 const GoogleServiceAuthError& error() const { return error_; } | 111 const GoogleServiceAuthError& error() const { return error_; } |
| 91 const std::string& token() const { return token_; } | 112 const std::string& token() const { return token_; } |
| 92 | 113 |
| 93 private: | 114 private: |
| 94 void OnManagedUserRegistered(const GoogleServiceAuthError& error, | 115 void OnManagedUserRegistered(const GoogleServiceAuthError& error, |
| 95 const std::string& token); | 116 const std::string& token); |
| 96 | 117 |
| 118 base::MessageLoop message_loop_; |
| 119 base::RunLoop run_loop_; |
| 97 base::WeakPtrFactory<ManagedUserRegistrationServiceTest> weak_ptr_factory_; | 120 base::WeakPtrFactory<ManagedUserRegistrationServiceTest> weak_ptr_factory_; |
| 98 TestingPrefServiceSyncable prefs_; | 121 TestingPrefServiceSyncable prefs_; |
| 99 scoped_ptr<ManagedUserRegistrationService> service_; | 122 scoped_ptr<ManagedUserRegistrationService> service_; |
| 100 | 123 |
| 101 // Owned by the ManagedUserRegistrationService. | 124 // Owned by the ManagedUserRegistrationService. |
| 102 MockChangeProcessor* change_processor_; | 125 MockChangeProcessor* change_processor_; |
| 103 | 126 |
| 104 // A unique ID for creating "remote" Sync data. | 127 // A unique ID for creating "remote" Sync data. |
| 105 int64 sync_data_id_; | 128 int64 sync_data_id_; |
| 106 | 129 |
| 107 // Whether OnManagedUserRegistered has been called. | 130 // Whether OnManagedUserRegistered has been called. |
| 108 bool received_callback_; | 131 bool received_callback_; |
| 109 | 132 |
| 110 // Hold the registration result (either an error, or a token). | 133 // Hold the registration result (either an error, or a token). |
| 111 GoogleServiceAuthError error_; | 134 GoogleServiceAuthError error_; |
| 112 std::string token_; | 135 std::string token_; |
| 113 }; | 136 }; |
| 114 | 137 |
| 115 ManagedUserRegistrationServiceTest::ManagedUserRegistrationServiceTest() | 138 ManagedUserRegistrationServiceTest::ManagedUserRegistrationServiceTest() |
| 116 : weak_ptr_factory_(this), | 139 : weak_ptr_factory_(this), |
| 117 service_(new ManagedUserRegistrationService(&prefs_)), | |
| 118 change_processor_(NULL), | 140 change_processor_(NULL), |
| 119 sync_data_id_(0), | 141 sync_data_id_(0), |
| 120 received_callback_(false), | 142 received_callback_(false), |
| 121 error_(GoogleServiceAuthError::NUM_STATES) { | 143 error_(GoogleServiceAuthError::NUM_STATES) { |
| 122 ManagedUserRegistrationService::RegisterUserPrefs(prefs_.registry()); | 144 ManagedUserRegistrationService::RegisterUserPrefs(prefs_.registry()); |
| 145 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher( |
| 146 new MockManagedUserRefreshTokenFetcher); |
| 147 service_.reset( |
| 148 new ManagedUserRegistrationService(&prefs_, token_fetcher.Pass())); |
| 123 } | 149 } |
| 124 | 150 |
| 125 ManagedUserRegistrationServiceTest::~ManagedUserRegistrationServiceTest() { | 151 ManagedUserRegistrationServiceTest::~ManagedUserRegistrationServiceTest() { |
| 126 EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs()); | 152 EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs()); |
| 127 } | 153 } |
| 128 | 154 |
| 129 scoped_ptr<SyncChangeProcessor> | 155 scoped_ptr<SyncChangeProcessor> |
| 130 ManagedUserRegistrationServiceTest::CreateChangeProcessor() { | 156 ManagedUserRegistrationServiceTest::CreateChangeProcessor() { |
| 131 EXPECT_FALSE(change_processor_); | 157 EXPECT_FALSE(change_processor_); |
| 132 change_processor_ = new MockChangeProcessor(); | 158 change_processor_ = new MockChangeProcessor(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 ++it) { | 199 ++it) { |
| 174 EXPECT_EQ(SyncChange::ACTION_ADD, it->change_type()); | 200 EXPECT_EQ(SyncChange::ACTION_ADD, it->change_type()); |
| 175 ::sync_pb::EntitySpecifics specifics = it->sync_data().GetSpecifics(); | 201 ::sync_pb::EntitySpecifics specifics = it->sync_data().GetSpecifics(); |
| 176 EXPECT_FALSE(specifics.managed_user().acknowledged()); | 202 EXPECT_FALSE(specifics.managed_user().acknowledged()); |
| 177 specifics.mutable_managed_user()->set_acknowledged(true); | 203 specifics.mutable_managed_user()->set_acknowledged(true); |
| 178 new_changes.push_back( | 204 new_changes.push_back( |
| 179 SyncChange(FROM_HERE, SyncChange::ACTION_UPDATE, | 205 SyncChange(FROM_HERE, SyncChange::ACTION_UPDATE, |
| 180 SyncData::CreateRemoteData(++sync_data_id_, specifics))); | 206 SyncData::CreateRemoteData(++sync_data_id_, specifics))); |
| 181 } | 207 } |
| 182 service()->ProcessSyncChanges(FROM_HERE, new_changes); | 208 service()->ProcessSyncChanges(FROM_HERE, new_changes); |
| 209 |
| 210 run_loop_.Run(); |
| 183 } | 211 } |
| 184 | 212 |
| 185 void ManagedUserRegistrationServiceTest::ResetService() { | 213 void ManagedUserRegistrationServiceTest::ResetService() { |
| 186 service_->StopSyncing(MANAGED_USERS); | 214 service_->StopSyncing(MANAGED_USERS); |
| 187 service_->Shutdown(); | 215 service_->Shutdown(); |
| 188 service_.reset(); | 216 service_.reset(); |
| 189 } | 217 } |
| 190 | 218 |
| 191 void ManagedUserRegistrationServiceTest::OnManagedUserRegistered( | 219 void ManagedUserRegistrationServiceTest::OnManagedUserRegistered( |
| 192 const GoogleServiceAuthError& error, | 220 const GoogleServiceAuthError& error, |
| 193 const std::string& token) { | 221 const std::string& token) { |
| 194 received_callback_ = true; | 222 received_callback_ = true; |
| 195 error_ = error; | 223 error_ = error; |
| 196 token_ = token; | 224 token_ = token; |
| 225 run_loop_.Quit(); |
| 197 } | 226 } |
| 198 | 227 |
| 199 TEST_F(ManagedUserRegistrationServiceTest, MergeEmpty) { | 228 TEST_F(ManagedUserRegistrationServiceTest, MergeEmpty) { |
| 200 SyncMergeResult result = StartInitialSync(); | 229 SyncMergeResult result = StartInitialSync(); |
| 201 EXPECT_EQ(0, result.num_items_added()); | 230 EXPECT_EQ(0, result.num_items_added()); |
| 202 EXPECT_EQ(0, result.num_items_modified()); | 231 EXPECT_EQ(0, result.num_items_modified()); |
| 203 EXPECT_EQ(0, result.num_items_deleted()); | 232 EXPECT_EQ(0, result.num_items_deleted()); |
| 204 EXPECT_EQ(0, result.num_items_before_association()); | 233 EXPECT_EQ(0, result.num_items_before_association()); |
| 205 EXPECT_EQ(0, result.num_items_after_association()); | 234 EXPECT_EQ(0, result.num_items_after_association()); |
| 206 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size()); | 235 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size()); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 } | 356 } |
| 328 | 357 |
| 329 TEST_F(ManagedUserRegistrationServiceTest, StopSyncing) { | 358 TEST_F(ManagedUserRegistrationServiceTest, StopSyncing) { |
| 330 StartInitialSync(); | 359 StartInitialSync(); |
| 331 service()->Register(ASCIIToUTF16("Mike"), GetRegistrationCallback()); | 360 service()->Register(ASCIIToUTF16("Mike"), GetRegistrationCallback()); |
| 332 service()->StopSyncing(MANAGED_USERS); | 361 service()->StopSyncing(MANAGED_USERS); |
| 333 EXPECT_TRUE(received_callback()); | 362 EXPECT_TRUE(received_callback()); |
| 334 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state()); | 363 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state()); |
| 335 EXPECT_EQ(std::string(), token()); | 364 EXPECT_EQ(std::string(), token()); |
| 336 } | 365 } |
| OLD | NEW |