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 |
| 93 virtual void TearDown() OVERRIDE; |
| 94 |
72 protected: | 95 protected: |
73 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor(); | 96 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor(); |
74 scoped_ptr<SyncErrorFactory> CreateErrorFactory(); | 97 scoped_ptr<SyncErrorFactory> CreateErrorFactory(); |
75 SyncData CreateRemoteData(const std::string& id, const std::string& name); | 98 SyncData CreateRemoteData(const std::string& id, const std::string& name); |
76 | 99 |
77 SyncMergeResult StartInitialSync(); | 100 SyncMergeResult StartInitialSync(); |
78 | 101 |
79 ManagedUserRegistrationService::RegistrationCallback | 102 ManagedUserRegistrationService::RegistrationCallback |
80 GetRegistrationCallback(); | 103 GetRegistrationCallback(); |
81 | 104 |
82 void Acknowledge(); | 105 void Acknowledge(); |
83 void ResetService(); | 106 void ResetService(); |
84 | 107 |
85 PrefService* prefs() { return &prefs_; } | 108 PrefService* prefs() { return &prefs_; } |
86 ManagedUserRegistrationService* service() { return service_.get(); } | 109 ManagedUserRegistrationService* service() { return service_.get(); } |
87 MockChangeProcessor* change_processor() { return change_processor_; } | 110 MockChangeProcessor* change_processor() { return change_processor_; } |
88 | 111 |
89 bool received_callback() const { return received_callback_; } | 112 bool received_callback() const { return received_callback_; } |
90 const GoogleServiceAuthError& error() const { return error_; } | 113 const GoogleServiceAuthError& error() const { return error_; } |
91 const std::string& token() const { return token_; } | 114 const std::string& token() const { return token_; } |
92 | 115 |
93 private: | 116 private: |
94 void OnManagedUserRegistered(const GoogleServiceAuthError& error, | 117 void OnManagedUserRegistered(const GoogleServiceAuthError& error, |
95 const std::string& token); | 118 const std::string& token); |
96 | 119 |
| 120 base::MessageLoop message_loop_; |
| 121 base::RunLoop run_loop_; |
97 base::WeakPtrFactory<ManagedUserRegistrationServiceTest> weak_ptr_factory_; | 122 base::WeakPtrFactory<ManagedUserRegistrationServiceTest> weak_ptr_factory_; |
98 TestingPrefServiceSyncable prefs_; | 123 TestingPrefServiceSyncable prefs_; |
99 scoped_ptr<ManagedUserRegistrationService> service_; | 124 scoped_ptr<ManagedUserRegistrationService> service_; |
100 | 125 |
101 // Owned by the ManagedUserRegistrationService. | 126 // Owned by the ManagedUserRegistrationService. |
102 MockChangeProcessor* change_processor_; | 127 MockChangeProcessor* change_processor_; |
103 | 128 |
104 // A unique ID for creating "remote" Sync data. | 129 // A unique ID for creating "remote" Sync data. |
105 int64 sync_data_id_; | 130 int64 sync_data_id_; |
106 | 131 |
107 // Whether OnManagedUserRegistered has been called. | 132 // Whether OnManagedUserRegistered has been called. |
108 bool received_callback_; | 133 bool received_callback_; |
109 | 134 |
110 // Hold the registration result (either an error, or a token). | 135 // Hold the registration result (either an error, or a token). |
111 GoogleServiceAuthError error_; | 136 GoogleServiceAuthError error_; |
112 std::string token_; | 137 std::string token_; |
113 }; | 138 }; |
114 | 139 |
115 ManagedUserRegistrationServiceTest::ManagedUserRegistrationServiceTest() | 140 ManagedUserRegistrationServiceTest::ManagedUserRegistrationServiceTest() |
116 : weak_ptr_factory_(this), | 141 : weak_ptr_factory_(this), |
117 service_(new ManagedUserRegistrationService(&prefs_)), | |
118 change_processor_(NULL), | 142 change_processor_(NULL), |
119 sync_data_id_(0), | 143 sync_data_id_(0), |
120 received_callback_(false), | 144 received_callback_(false), |
121 error_(GoogleServiceAuthError::NUM_STATES) { | 145 error_(GoogleServiceAuthError::NUM_STATES) { |
122 ManagedUserRegistrationService::RegisterUserPrefs(prefs_.registry()); | 146 ManagedUserRegistrationService::RegisterUserPrefs(prefs_.registry()); |
| 147 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher( |
| 148 new MockManagedUserRefreshTokenFetcher); |
| 149 service_.reset( |
| 150 new ManagedUserRegistrationService(&prefs_, token_fetcher.Pass())); |
123 } | 151 } |
124 | 152 |
125 ManagedUserRegistrationServiceTest::~ManagedUserRegistrationServiceTest() { | 153 ManagedUserRegistrationServiceTest::~ManagedUserRegistrationServiceTest() { |
126 EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs()); | 154 EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs()); |
127 } | 155 } |
128 | 156 |
| 157 void ManagedUserRegistrationServiceTest::TearDown() { |
| 158 base::RunLoop().RunUntilIdle(); |
| 159 } |
| 160 |
129 scoped_ptr<SyncChangeProcessor> | 161 scoped_ptr<SyncChangeProcessor> |
130 ManagedUserRegistrationServiceTest::CreateChangeProcessor() { | 162 ManagedUserRegistrationServiceTest::CreateChangeProcessor() { |
131 EXPECT_FALSE(change_processor_); | 163 EXPECT_FALSE(change_processor_); |
132 change_processor_ = new MockChangeProcessor(); | 164 change_processor_ = new MockChangeProcessor(); |
133 return scoped_ptr<SyncChangeProcessor>(change_processor_); | 165 return scoped_ptr<SyncChangeProcessor>(change_processor_); |
134 } | 166 } |
135 | 167 |
136 scoped_ptr<SyncErrorFactory> | 168 scoped_ptr<SyncErrorFactory> |
137 ManagedUserRegistrationServiceTest::CreateErrorFactory() { | 169 ManagedUserRegistrationServiceTest::CreateErrorFactory() { |
138 return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock()); | 170 return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 ++it) { | 205 ++it) { |
174 EXPECT_EQ(SyncChange::ACTION_ADD, it->change_type()); | 206 EXPECT_EQ(SyncChange::ACTION_ADD, it->change_type()); |
175 ::sync_pb::EntitySpecifics specifics = it->sync_data().GetSpecifics(); | 207 ::sync_pb::EntitySpecifics specifics = it->sync_data().GetSpecifics(); |
176 EXPECT_FALSE(specifics.managed_user().acknowledged()); | 208 EXPECT_FALSE(specifics.managed_user().acknowledged()); |
177 specifics.mutable_managed_user()->set_acknowledged(true); | 209 specifics.mutable_managed_user()->set_acknowledged(true); |
178 new_changes.push_back( | 210 new_changes.push_back( |
179 SyncChange(FROM_HERE, SyncChange::ACTION_UPDATE, | 211 SyncChange(FROM_HERE, SyncChange::ACTION_UPDATE, |
180 SyncData::CreateRemoteData(++sync_data_id_, specifics))); | 212 SyncData::CreateRemoteData(++sync_data_id_, specifics))); |
181 } | 213 } |
182 service()->ProcessSyncChanges(FROM_HERE, new_changes); | 214 service()->ProcessSyncChanges(FROM_HERE, new_changes); |
| 215 |
| 216 run_loop_.Run(); |
183 } | 217 } |
184 | 218 |
185 void ManagedUserRegistrationServiceTest::ResetService() { | 219 void ManagedUserRegistrationServiceTest::ResetService() { |
186 service_->StopSyncing(MANAGED_USERS); | 220 service_->StopSyncing(MANAGED_USERS); |
187 service_->Shutdown(); | 221 service_->Shutdown(); |
188 service_.reset(); | 222 service_.reset(); |
189 } | 223 } |
190 | 224 |
191 void ManagedUserRegistrationServiceTest::OnManagedUserRegistered( | 225 void ManagedUserRegistrationServiceTest::OnManagedUserRegistered( |
192 const GoogleServiceAuthError& error, | 226 const GoogleServiceAuthError& error, |
193 const std::string& token) { | 227 const std::string& token) { |
194 received_callback_ = true; | 228 received_callback_ = true; |
195 error_ = error; | 229 error_ = error; |
196 token_ = token; | 230 token_ = token; |
| 231 run_loop_.Quit(); |
197 } | 232 } |
198 | 233 |
199 TEST_F(ManagedUserRegistrationServiceTest, MergeEmpty) { | 234 TEST_F(ManagedUserRegistrationServiceTest, MergeEmpty) { |
200 SyncMergeResult result = StartInitialSync(); | 235 SyncMergeResult result = StartInitialSync(); |
201 EXPECT_EQ(0, result.num_items_added()); | 236 EXPECT_EQ(0, result.num_items_added()); |
202 EXPECT_EQ(0, result.num_items_modified()); | 237 EXPECT_EQ(0, result.num_items_modified()); |
203 EXPECT_EQ(0, result.num_items_deleted()); | 238 EXPECT_EQ(0, result.num_items_deleted()); |
204 EXPECT_EQ(0, result.num_items_before_association()); | 239 EXPECT_EQ(0, result.num_items_before_association()); |
205 EXPECT_EQ(0, result.num_items_after_association()); | 240 EXPECT_EQ(0, result.num_items_after_association()); |
206 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size()); | 241 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size()); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 } | 362 } |
328 | 363 |
329 TEST_F(ManagedUserRegistrationServiceTest, StopSyncing) { | 364 TEST_F(ManagedUserRegistrationServiceTest, StopSyncing) { |
330 StartInitialSync(); | 365 StartInitialSync(); |
331 service()->Register(ASCIIToUTF16("Mike"), GetRegistrationCallback()); | 366 service()->Register(ASCIIToUTF16("Mike"), GetRegistrationCallback()); |
332 service()->StopSyncing(MANAGED_USERS); | 367 service()->StopSyncing(MANAGED_USERS); |
333 EXPECT_TRUE(received_callback()); | 368 EXPECT_TRUE(received_callback()); |
334 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state()); | 369 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state()); |
335 EXPECT_EQ(std::string(), token()); | 370 EXPECT_EQ(std::string(), token()); |
336 } | 371 } |
OLD | NEW |