1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This class defines tests that implementations of Invalidator should pass in
// order to be conformant. Here's how you use it to test your implementation.
//
// Say your class is called MyInvalidator. Then you need to define a class
// called MyInvalidatorTestDelegate in my_sync_notifier_unittest.cc like this:
//
// class MyInvalidatorTestDelegate {
// public:
// MyInvalidatorTestDelegate() ...
//
// ~MyInvalidatorTestDelegate() {
// // DestroyInvalidator() may not be explicitly called by tests.
// DestroyInvalidator();
// }
//
// // Create the Invalidator implementation with the given parameters.
// void CreateInvalidator(
// const std::string& initial_state,
// const base::WeakPtr<InvalidationStateTracker>&
// invalidation_state_tracker) {
// ...
// }
//
// // Should return the Invalidator implementation. Only called after
// // CreateInvalidator and before DestroyInvalidator.
// MyInvalidator* GetInvalidator() {
// ...
// }
//
// // Destroy the Invalidator implementation.
// void DestroyInvalidator() {
// ...
// }
//
// // Called after a call to SetUniqueId(), or UpdateCredentials() on the
// // Invalidator implementation. Should block until the effects of the
// // call are visible on the current thread.
// void WaitForInvalidator() {
// ...
// }
//
// // The Trigger* functions below should block until the effects of
// // the call are visible on the current thread.
//
// // Should cause OnInvalidatorStateChange() to be called on all
// // observers of the Invalidator implementation with the given
// // parameters.
// void TriggerOnInvalidatorStateChange(InvalidatorState state) {
// ...
// }
//
// // Should cause OnIncomingInvalidation() to be called on all
// // observers of the Invalidator implementation with the given
// // parameters.
// void TriggerOnIncomingInvalidation(
// const ObjectIdInvalidationMap& invalidation_map) {
// ...
// }
// };
//
// The InvalidatorTest test harness will have a member variable of
// this delegate type and will call its functions in the various
// tests.
//
// Then you simply #include this file as well as gtest.h and add the
// following statement to my_sync_notifier_unittest.cc:
//
// INSTANTIATE_TYPED_TEST_CASE_P(
// MyInvalidator, InvalidatorTest, MyInvalidatorTestDelegate);
//
// Easy!
#ifndef COMPONENTS_INVALIDATION_IMPL_INVALIDATOR_TEST_TEMPLATE_H_
#define COMPONENTS_INVALIDATION_IMPL_INVALIDATOR_TEST_TEMPLATE_H_
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "components/invalidation/impl/fake_invalidation_handler.h"
#include "components/invalidation/impl/fake_invalidation_state_tracker.h"
#include "components/invalidation/impl/invalidator.h"
#include "components/invalidation/impl/object_id_invalidation_map_test_util.h"
#include "google/cacheinvalidation/include/types.h"
#include "google/cacheinvalidation/types.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
template <typename InvalidatorTestDelegate>
class InvalidatorTest : public testing::Test {
protected:
InvalidatorTest()
: id1(ipc::invalidation::ObjectSource::TEST, "a"),
id2(ipc::invalidation::ObjectSource::TEST, "b"),
id3(ipc::invalidation::ObjectSource::TEST, "c"),
id4(ipc::invalidation::ObjectSource::TEST, "d") {
}
Invalidator* CreateAndInitializeInvalidator() {
this->delegate_.CreateInvalidator("fake_invalidator_client_id",
"fake_initial_state",
this->fake_tracker_.AsWeakPtr());
Invalidator* const invalidator = this->delegate_.GetInvalidator();
this->delegate_.WaitForInvalidator();
invalidator->UpdateCredentials("foo@bar.com", "fake_token");
this->delegate_.WaitForInvalidator();
return invalidator;
}
FakeInvalidationStateTracker fake_tracker_;
InvalidatorTestDelegate delegate_;
const invalidation::ObjectId id1;
const invalidation::ObjectId id2;
const invalidation::ObjectId id3;
const invalidation::ObjectId id4;
};
TYPED_TEST_CASE_P(InvalidatorTest);
// Initialize the invalidator, register a handler, register some IDs for that
// handler, and then unregister the handler, dispatching invalidations in
// between. The handler should only see invalidations when its registered and
// its IDs are registered.
TYPED_TEST_P(InvalidatorTest, Basic) {
Invalidator* const invalidator = this->CreateAndInitializeInvalidator();
FakeInvalidationHandler handler;
invalidator->RegisterHandler(&handler);
ObjectIdInvalidationMap invalidation_map;
invalidation_map.Insert(Invalidation::Init(this->id1, 1, "1"));
invalidation_map.Insert(Invalidation::Init(this->id2, 2, "2"));
invalidation_map.Insert(Invalidation::Init(this->id3, 3, "3"));
// Should be ignored since no IDs are registered to |handler|.
this->delegate_.TriggerOnIncomingInvalidation(invalidation_map);
EXPECT_EQ(0, handler.GetInvalidationCount());
ObjectIdSet ids;
ids.insert(this->id1);
ids.insert(this->id2);
EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler, ids));
this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED);
EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetInvalidatorState());
ObjectIdInvalidationMap expected_invalidations;
expected_invalidations.Insert(Invalidation::Init(this->id1, 1, "1"));
expected_invalidations.Insert(Invalidation::Init(this->id2, 2, "2"));
this->delegate_.TriggerOnIncomingInvalidation(invalidation_map);
EXPECT_EQ(1, handler.GetInvalidationCount());
EXPECT_THAT(expected_invalidations, Eq(handler.GetLastInvalidationMap()));
ids.erase(this->id1);
ids.insert(this->id3);
EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler, ids));
expected_invalidations = ObjectIdInvalidationMap();
expected_invalidations.Insert(Invalidation::Init(this->id2, 2, "2"));
expected_invalidations.Insert(Invalidation::Init(this->id3, 3, "3"));
// Removed object IDs should not be notified, newly-added ones should.
this->delegate_.TriggerOnIncomingInvalidation(invalidation_map);
EXPECT_EQ(2, handler.GetInvalidationCount());
EXPECT_THAT(expected_invalidations, Eq(handler.GetLastInvalidationMap()));
this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR);
EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR,
handler.GetInvalidatorState());
this->delegate_.TriggerOnInvalidatorStateChange(
INVALIDATION_CREDENTIALS_REJECTED);
EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED,
handler.GetInvalidatorState());
invalidator->UnregisterHandler(&handler);
// Should be ignored since |handler| isn't registered anymore.
this->delegate_.TriggerOnIncomingInvalidation(invalidation_map);
EXPECT_EQ(2, handler.GetInvalidationCount());
}
// Register handlers and some IDs for those handlers, register a handler with
// no IDs, and register a handler with some IDs but unregister it. Then,
// dispatch some invalidations and invalidations. Handlers that are registered
// should get invalidations, and the ones that have registered IDs should
// receive invalidations for those IDs.
TYPED_TEST_P(InvalidatorTest, MultipleHandlers) {
Invalidator* const invalidator = this->CreateAndInitializeInvalidator();
FakeInvalidationHandler handler1;
FakeInvalidationHandler handler2;
FakeInvalidationHandler handler3;
FakeInvalidationHandler handler4;
invalidator->RegisterHandler(&handler1);
invalidator->RegisterHandler(&handler2);
invalidator->RegisterHandler(&handler3);
invalidator->RegisterHandler(&handler4);
{
ObjectIdSet ids;
ids.insert(this->id1);
ids.insert(this->id2);
EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler1, ids));
}
{
ObjectIdSet ids;
ids.insert(this->id3);
EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler2, ids));
}
// Don't register any IDs for handler3.
{
ObjectIdSet ids;
ids.insert(this->id4);
EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler4, ids));
}
invalidator->UnregisterHandler(&handler4);
this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED);
EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState());
EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState());
EXPECT_EQ(INVALIDATIONS_ENABLED, handler3.GetInvalidatorState());
EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState());
{
ObjectIdInvalidationMap invalidation_map;
invalidation_map.Insert(Invalidation::Init(this->id1, 1, "1"));
invalidation_map.Insert(Invalidation::Init(this->id2, 2, "2"));
invalidation_map.Insert(Invalidation::Init(this->id3, 3, "3"));
invalidation_map.Insert(Invalidation::Init(this->id4, 4, "4"));
this->delegate_.TriggerOnIncomingInvalidation(invalidation_map);
ObjectIdInvalidationMap expected_invalidations;
expected_invalidations.Insert(Invalidation::Init(this->id1, 1, "1"));
expected_invalidations.Insert(Invalidation::Init(this->id2, 2, "2"));
EXPECT_EQ(1, handler1.GetInvalidationCount());
EXPECT_THAT(expected_invalidations, Eq(handler1.GetLastInvalidationMap()));
expected_invalidations = ObjectIdInvalidationMap();
expected_invalidations.Insert(Invalidation::Init(this->id3, 3, "3"));
EXPECT_EQ(1, handler2.GetInvalidationCount());
EXPECT_THAT(expected_invalidations, Eq(handler2.GetLastInvalidationMap()));
EXPECT_EQ(0, handler3.GetInvalidationCount());
EXPECT_EQ(0, handler4.GetInvalidationCount());
}
this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR);
EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState());
EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState());
EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler3.GetInvalidatorState());
EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler4.GetInvalidatorState());
invalidator->UnregisterHandler(&handler3);
invalidator->UnregisterHandler(&handler2);
invalidator->UnregisterHandler(&handler1);
}
// Multiple registrations by different handlers on the same object ID should
// return false.
TYPED_TEST_P(InvalidatorTest, MultipleRegistrations) {
Invalidator* const invalidator = this->CreateAndInitializeInvalidator();
FakeInvalidationHandler handler1;
FakeInvalidationHandler handler2;
invalidator->RegisterHandler(&handler1);
invalidator->RegisterHandler(&handler2);
// Registering both handlers for the same ObjectId. First call should succeed,
// second should fail.
ObjectIdSet ids;
ids.insert(this->id1);
EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler1, ids));
EXPECT_FALSE(invalidator->UpdateRegisteredIds(&handler2, ids));
invalidator->UnregisterHandler(&handler2);
invalidator->UnregisterHandler(&handler1);
}
// Make sure that passing an empty set to UpdateRegisteredIds clears the
// corresponding entries for the handler.
TYPED_TEST_P(InvalidatorTest, EmptySetUnregisters) {
Invalidator* const invalidator = this->CreateAndInitializeInvalidator();
FakeInvalidationHandler handler1;
// Control observer.
FakeInvalidationHandler handler2;
invalidator->RegisterHandler(&handler1);
invalidator->RegisterHandler(&handler2);
{
ObjectIdSet ids;
ids.insert(this->id1);
ids.insert(this->id2);
EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler1, ids));
}
{
ObjectIdSet ids;
ids.insert(this->id3);
EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler2, ids));
}
// Unregister the IDs for the first observer. It should not receive any
// further invalidations.
EXPECT_TRUE(invalidator->UpdateRegisteredIds(&handler1, ObjectIdSet()));
this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED);
EXPECT_EQ(INVALIDATIONS_ENABLED, handler1.GetInvalidatorState());
EXPECT_EQ(INVALIDATIONS_ENABLED, handler2.GetInvalidatorState());
{
ObjectIdInvalidationMap invalidation_map;
invalidation_map.Insert(Invalidation::Init(this->id1, 1, "1"));
invalidation_map.Insert(Invalidation::Init(this->id2, 2, "2"));
invalidation_map.Insert(Invalidation::Init(this->id3, 3, "3"));
this->delegate_.TriggerOnIncomingInvalidation(invalidation_map);
EXPECT_EQ(0, handler1.GetInvalidationCount());
EXPECT_EQ(1, handler2.GetInvalidationCount());
}
this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR);
EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler1.GetInvalidatorState());
EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler2.GetInvalidatorState());
invalidator->UnregisterHandler(&handler2);
invalidator->UnregisterHandler(&handler1);
}
namespace internal {
// A FakeInvalidationHandler that is "bound" to a specific
// Invalidator. This is for cross-referencing state information with
// the bound Invalidator.
class BoundFakeInvalidationHandler : public FakeInvalidationHandler {
public:
explicit BoundFakeInvalidationHandler(const Invalidator& invalidator);
~BoundFakeInvalidationHandler() override;
// Returns the last return value of GetInvalidatorState() on the
// bound invalidator from the last time the invalidator state
// changed.
InvalidatorState GetLastRetrievedState() const;
// InvalidationHandler implementation.
void OnInvalidatorStateChange(InvalidatorState state) override;
private:
const Invalidator& invalidator_;
InvalidatorState last_retrieved_state_;
DISALLOW_COPY_AND_ASSIGN(BoundFakeInvalidationHandler);
};
} // namespace internal
TYPED_TEST_P(InvalidatorTest, GetInvalidatorStateAlwaysCurrent) {
Invalidator* const invalidator = this->CreateAndInitializeInvalidator();
internal::BoundFakeInvalidationHandler handler(*invalidator);
invalidator->RegisterHandler(&handler);
this->delegate_.TriggerOnInvalidatorStateChange(INVALIDATIONS_ENABLED);
EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetInvalidatorState());
EXPECT_EQ(INVALIDATIONS_ENABLED, handler.GetLastRetrievedState());
this->delegate_.TriggerOnInvalidatorStateChange(TRANSIENT_INVALIDATION_ERROR);
EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler.GetInvalidatorState());
EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, handler.GetLastRetrievedState());
invalidator->UnregisterHandler(&handler);
}
REGISTER_TYPED_TEST_CASE_P(InvalidatorTest,
Basic,
MultipleHandlers,
MultipleRegistrations,
EmptySetUnregisters,
GetInvalidatorStateAlwaysCurrent);
} // namespace syncer
#endif // COMPONENTS_INVALIDATION_IMPL_INVALIDATOR_TEST_TEMPLATE_H_
|