Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/invalidation/invalidation_service_android.h" | 5 #include "chrome/browser/invalidation/invalidation_service_android.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 7 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/invalidation/invalidation_service_factory.h" | 12 #include "chrome/browser/invalidation/invalidation_service_factory.h" |
| 9 #include "chrome/browser/invalidation/invalidation_service_test_template.h" | 13 #include "chrome/browser/invalidation/invalidation_service_test_template.h" |
| 10 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 11 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
| 16 #include "sync/notifier/fake_invalidation_handler.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 18 |
| 14 namespace invalidation { | 19 namespace invalidation { |
| 15 | 20 |
| 16 class InvalidationServiceAndroidTestDelegate { | 21 class InvalidationServiceAndroidTestDelegate { |
| 17 public: | 22 public: |
| 18 InvalidationServiceAndroidTestDelegate() { } | 23 InvalidationServiceAndroidTestDelegate() { } |
| 19 | 24 |
| 20 ~InvalidationServiceAndroidTestDelegate() { | 25 ~InvalidationServiceAndroidTestDelegate() { |
| 21 DestroyInvalidationService(); | 26 DestroyInvalidationService(); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 49 } | 54 } |
| 50 | 55 |
| 51 scoped_ptr<TestingProfile> profile_; | 56 scoped_ptr<TestingProfile> profile_; |
| 52 scoped_ptr<InvalidationServiceAndroid> invalidation_service_android_; | 57 scoped_ptr<InvalidationServiceAndroid> invalidation_service_android_; |
| 53 }; | 58 }; |
| 54 | 59 |
| 55 INSTANTIATE_TYPED_TEST_CASE_P( | 60 INSTANTIATE_TYPED_TEST_CASE_P( |
| 56 AndroidInvalidationServiceTest, InvalidationServiceTest, | 61 AndroidInvalidationServiceTest, InvalidationServiceTest, |
| 57 InvalidationServiceAndroidTestDelegate); | 62 InvalidationServiceAndroidTestDelegate); |
| 58 | 63 |
| 64 class InvalidationServiceAndroidRegistrationTest : public testing::Test { | |
| 65 protected: | |
| 66 InvalidationServiceAndroidRegistrationTest() | |
| 67 : invalidation_service_(&profile_), | |
| 68 object_registration_updated_(false) { | |
| 69 invalidation_service_.SetUpdateRegistrationCallbackForTest(base::Bind( | |
| 70 &InvalidationServiceAndroidRegistrationTest::UpdateRegistration, | |
| 71 base::Unretained(this))); | |
| 72 OBJECT_SET_A.insert(invalidation::ObjectId(1, "A")); | |
| 73 OBJECT_SET_A.insert(invalidation::ObjectId(2, "B")); | |
| 74 OBJECT_SET_A.insert(invalidation::ObjectId(3, "C")); | |
| 75 OBJECT_SET_B.insert(invalidation::ObjectId(1, "D")); | |
| 76 OBJECT_SET_B.insert(invalidation::ObjectId(2, "E")); | |
| 77 OBJECT_SET_C.insert(invalidation::ObjectId(3, "F")); | |
| 78 OBJECT_SET_D.insert( | |
| 79 invalidation::ObjectId(ipc::invalidation::ObjectSource::CHROME_SYNC, | |
| 80 "A")); | |
| 81 OBJECT_SET_D.insert( | |
| 82 invalidation::ObjectId(ipc::invalidation::ObjectSource::CHROME_SYNC, | |
| 83 "B")); | |
| 84 OBJECT_SET_E.insert(OBJECT_SET_A.begin(), OBJECT_SET_A.end()); | |
| 85 OBJECT_SET_E.insert(OBJECT_SET_D.begin(), OBJECT_SET_D.end()); | |
| 86 } | |
| 87 | |
| 88 virtual ~InvalidationServiceAndroidRegistrationTest() { | |
| 89 invalidation_service_.Shutdown(); | |
| 90 } | |
| 91 | |
| 92 // Determines if the object registration was updated since the last time this | |
| 93 // method was called. | |
| 94 bool ObjectRegistrationUpdated() { | |
| 95 bool updated = object_registration_updated_; | |
| 96 object_registration_updated_ = false; | |
| 97 return updated; | |
| 98 } | |
| 99 | |
| 100 // Determines if all of the object ids in the given set were registered in | |
| 101 // the last registration update. | |
| 102 bool ObjectsRegistered(const syncer::ObjectIdSet& ids) { | |
| 103 syncer::ObjectIdSet::const_iterator id; | |
|
nyquist
2013/09/05 22:50:08
Nit: Do you want to extract a method, and pass in
Steve Condie
2013/09/06 20:05:22
Done.
| |
| 104 for (id = ids.begin(); id != ids.end(); ++id) | |
| 105 if (!IsRegistered(*id)) | |
| 106 return false; | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 // Determines if none of the object ids in the given set were registered in | |
| 111 // the last registration update. | |
| 112 bool ObjectsNotRegistered(const syncer::ObjectIdSet& ids) { | |
| 113 syncer::ObjectIdSet::const_iterator id; | |
| 114 for (id = ids.begin(); id != ids.end(); ++id) | |
| 115 if (IsRegistered(*id)) | |
| 116 return false; | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 // Get the number of objects which are registered. | |
| 121 size_t RegisteredObjectCount() { | |
| 122 return registered_object_ids_.size(); | |
| 123 } | |
| 124 | |
| 125 // Determines if the given object id was registered in the last registration | |
| 126 // update. | |
| 127 bool IsRegistered(const invalidation::ObjectId& id) { | |
| 128 return registered_object_ids_.find(id) != registered_object_ids_.end(); | |
| 129 } | |
| 130 | |
| 131 // Disjoint object id sets containing non-Sync object ids. | |
| 132 syncer::ObjectIdSet OBJECT_SET_A; | |
| 133 syncer::ObjectIdSet OBJECT_SET_B; | |
| 134 syncer::ObjectIdSet OBJECT_SET_C; | |
| 135 | |
| 136 // An object id set containing Sync object ids. | |
| 137 syncer::ObjectIdSet OBJECT_SET_D; | |
|
nyquist
2013/09/05 22:50:08
Nit: OBJECT_SET_SYNC?
Fine with keeping as is tho
Steve Condie
2013/09/06 20:05:22
Done.
| |
| 138 | |
| 139 // An object id set comprised of the union of OBJECT_SET_A and OBJECT_SET_D. | |
| 140 syncer::ObjectIdSet OBJECT_SET_E; | |
|
nyquist
2013/09/05 22:50:08
Nit: OBJECT_SET_A_UNION_D or OBJECT_SET_A_UNION_SY
Steve Condie
2013/09/06 20:05:22
How about OBJECT_SET_MIXED to keep the name a bit
nyquist
2013/09/07 05:10:47
Great idea!
| |
| 141 | |
| 142 TestingProfile profile_; | |
| 143 | |
| 144 // The invalidation service used for testing. | |
| 145 InvalidationServiceAndroid invalidation_service_; | |
| 146 | |
| 147 private: | |
| 148 // Callback invoked when the invalidation service updates the registration. | |
| 149 void UpdateRegistration(const std::vector<int>& object_sources, | |
| 150 const std::vector<std::string>& object_names) { | |
| 151 object_registration_updated_ = true; | |
| 152 registered_object_ids_.clear(); | |
| 153 for (size_t i = 0; i < object_sources.size(); ++i) { | |
| 154 registered_object_ids_.insert(invalidation::ObjectId( | |
| 155 object_sources[i], object_names[i])); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 bool object_registration_updated_; | |
| 160 syncer::ObjectIdSet registered_object_ids_; | |
| 161 }; | |
| 162 | |
| 163 TEST_F(InvalidationServiceAndroidRegistrationTest, NoObjectRegistration) { | |
| 164 syncer::FakeInvalidationHandler handler; | |
| 165 invalidation_service_.RegisterInvalidationHandler(&handler); | |
| 166 EXPECT_EQ(0U, RegisteredObjectCount()); | |
| 167 invalidation_service_.UnregisterInvalidationHandler(&handler); | |
| 168 } | |
| 169 | |
| 170 TEST_F(InvalidationServiceAndroidRegistrationTest, UpdateObjectRegistration) { | |
| 171 syncer::FakeInvalidationHandler handler1; | |
| 172 syncer::FakeInvalidationHandler handler2; | |
| 173 syncer::FakeInvalidationHandler handler3; | |
| 174 syncer::FakeInvalidationHandler handler4; | |
| 175 invalidation_service_.RegisterInvalidationHandler(&handler1); | |
| 176 invalidation_service_.RegisterInvalidationHandler(&handler2); | |
| 177 invalidation_service_.RegisterInvalidationHandler(&handler3); | |
| 178 invalidation_service_.RegisterInvalidationHandler(&handler4); | |
| 179 | |
| 180 // Register for object set A. | |
| 181 invalidation_service_.UpdateRegisteredInvalidationIds(&handler1, | |
| 182 OBJECT_SET_A); | |
| 183 EXPECT_TRUE(ObjectRegistrationUpdated()); | |
| 184 EXPECT_EQ(OBJECT_SET_A.size(), RegisteredObjectCount()); | |
| 185 EXPECT_TRUE(ObjectsRegistered(OBJECT_SET_A)); | |
| 186 | |
| 187 // Register for object sets B and C. | |
| 188 invalidation_service_.UpdateRegisteredInvalidationIds(&handler2, | |
| 189 OBJECT_SET_B); | |
| 190 EXPECT_TRUE(ObjectRegistrationUpdated()); | |
| 191 invalidation_service_.UpdateRegisteredInvalidationIds(&handler3, | |
| 192 OBJECT_SET_C); | |
| 193 EXPECT_TRUE(ObjectRegistrationUpdated()); | |
| 194 EXPECT_EQ(OBJECT_SET_A.size() + OBJECT_SET_B.size() + OBJECT_SET_C.size(), | |
| 195 RegisteredObjectCount()); | |
| 196 EXPECT_TRUE(ObjectsRegistered(OBJECT_SET_A)); | |
| 197 EXPECT_TRUE(ObjectsRegistered(OBJECT_SET_B)); | |
| 198 EXPECT_TRUE(ObjectsRegistered(OBJECT_SET_C)); | |
| 199 | |
| 200 // Register for object set D. Expect no registration update. | |
| 201 invalidation_service_.UpdateRegisteredInvalidationIds(&handler4, | |
| 202 OBJECT_SET_D); | |
| 203 EXPECT_FALSE(ObjectRegistrationUpdated()); | |
| 204 EXPECT_TRUE(ObjectsNotRegistered(OBJECT_SET_D)); | |
| 205 | |
| 206 // Unregister for object set C. | |
| 207 invalidation_service_.UpdateRegisteredInvalidationIds(&handler3, | |
| 208 syncer::ObjectIdSet()); | |
| 209 EXPECT_TRUE(ObjectRegistrationUpdated()); | |
| 210 EXPECT_TRUE(ObjectsNotRegistered(OBJECT_SET_C)); | |
| 211 | |
| 212 // Unregister for object set A. | |
| 213 invalidation_service_.UpdateRegisteredInvalidationIds(&handler1, | |
| 214 syncer::ObjectIdSet()); | |
| 215 EXPECT_TRUE(ObjectRegistrationUpdated()); | |
| 216 EXPECT_TRUE(ObjectsNotRegistered(OBJECT_SET_A)); | |
| 217 | |
| 218 // Unregister for object set D. Expect no registration update. | |
| 219 invalidation_service_.UpdateRegisteredInvalidationIds(&handler4, | |
| 220 syncer::ObjectIdSet()); | |
| 221 EXPECT_FALSE(ObjectRegistrationUpdated()); | |
| 222 | |
| 223 // Register for object set E. Expect only the objects in set A to be | |
| 224 // registered. | |
| 225 invalidation_service_.UpdateRegisteredInvalidationIds(&handler3, | |
| 226 OBJECT_SET_E); | |
| 227 EXPECT_TRUE(ObjectRegistrationUpdated()); | |
| 228 EXPECT_TRUE(ObjectsRegistered(OBJECT_SET_A)); | |
| 229 EXPECT_TRUE(ObjectsNotRegistered(OBJECT_SET_D)); | |
| 230 | |
| 231 // Unregister the handler mapped to set B. Expect no registration update. | |
| 232 invalidation_service_.UnregisterInvalidationHandler(&handler2); | |
| 233 EXPECT_FALSE(ObjectRegistrationUpdated()); | |
| 234 EXPECT_TRUE(ObjectsRegistered(OBJECT_SET_B)); | |
| 235 | |
| 236 // Replace object set A with object set C. | |
| 237 invalidation_service_.UpdateRegisteredInvalidationIds(&handler3, | |
| 238 OBJECT_SET_C); | |
| 239 EXPECT_TRUE(ObjectRegistrationUpdated()); | |
| 240 EXPECT_TRUE(ObjectsNotRegistered(OBJECT_SET_A)); | |
| 241 EXPECT_TRUE(ObjectsRegistered(OBJECT_SET_C)); | |
| 242 | |
| 243 invalidation_service_.UnregisterInvalidationHandler(&handler1); | |
| 244 invalidation_service_.UnregisterInvalidationHandler(&handler3); | |
| 245 invalidation_service_.UnregisterInvalidationHandler(&handler4); | |
| 246 } | |
| 247 | |
| 59 } // namespace invalidation | 248 } // namespace invalidation |
| OLD | NEW |