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
|
// 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.
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "components/invalidation/fake_invalidation_handler.h"
#include "components/invalidation/invalidator_registrar.h"
#include "components/invalidation/invalidator_test_template.h"
#include "google/cacheinvalidation/types.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
namespace {
// We test InvalidatorRegistrar by wrapping it in an Invalidator and
// running the usual Invalidator tests.
// Thin Invalidator wrapper around InvalidatorRegistrar.
class RegistrarInvalidator : public Invalidator {
public:
RegistrarInvalidator() {}
~RegistrarInvalidator() override {}
InvalidatorRegistrar* GetRegistrar() {
return ®istrar_;
}
// Invalidator implementation.
void RegisterHandler(InvalidationHandler* handler) override {
registrar_.RegisterHandler(handler);
}
void UpdateRegisteredIds(InvalidationHandler* handler,
const ObjectIdSet& ids) override {
registrar_.UpdateRegisteredIds(handler, ids);
}
void UnregisterHandler(InvalidationHandler* handler) override {
registrar_.UnregisterHandler(handler);
}
InvalidatorState GetInvalidatorState() const override {
return registrar_.GetInvalidatorState();
}
void UpdateCredentials(const std::string& email,
const std::string& token) override {
// Do nothing.
}
void RequestDetailedStatus(
base::Callback<void(const base::DictionaryValue&)> call) const override {
// Do nothing.
}
private:
InvalidatorRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(RegistrarInvalidator);
};
class RegistrarInvalidatorTestDelegate {
public:
RegistrarInvalidatorTestDelegate() {}
~RegistrarInvalidatorTestDelegate() {
DestroyInvalidator();
}
void CreateInvalidator(
const std::string& invalidator_client_id,
const std::string& initial_state,
const base::WeakPtr<InvalidationStateTracker>&
invalidation_state_tracker) {
DCHECK(!invalidator_.get());
invalidator_.reset(new RegistrarInvalidator());
}
RegistrarInvalidator* GetInvalidator() {
return invalidator_.get();
}
void DestroyInvalidator() {
invalidator_.reset();
}
void WaitForInvalidator() {
// Do nothing.
}
void TriggerOnInvalidatorStateChange(InvalidatorState state) {
invalidator_->GetRegistrar()->UpdateInvalidatorState(state);
}
void TriggerOnIncomingInvalidation(
const ObjectIdInvalidationMap& invalidation_map) {
invalidator_->GetRegistrar()->DispatchInvalidationsToHandlers(
invalidation_map);
}
private:
scoped_ptr<RegistrarInvalidator> invalidator_;
};
INSTANTIATE_TYPED_TEST_CASE_P(
RegistrarInvalidatorTest, InvalidatorTest,
RegistrarInvalidatorTestDelegate);
class InvalidatorRegistrarTest : public testing::Test {};
// Technically the test below can be part of InvalidatorTest, but we
// want to keep the number of death tests down.
// When we expect a death via CHECK(), we can't match against the
// CHECK() message since they are removed in official builds.
#if GTEST_HAS_DEATH_TEST
// Multiple registrations by different handlers on the same object ID should
// cause a CHECK.
TEST_F(InvalidatorRegistrarTest, MultipleRegistration) {
const invalidation::ObjectId id1(ipc::invalidation::ObjectSource::TEST, "a");
const invalidation::ObjectId id2(ipc::invalidation::ObjectSource::TEST, "a");
InvalidatorRegistrar registrar;
FakeInvalidationHandler handler1;
registrar.RegisterHandler(&handler1);
FakeInvalidationHandler handler2;
registrar.RegisterHandler(&handler2);
ObjectIdSet ids;
ids.insert(id1);
ids.insert(id2);
registrar.UpdateRegisteredIds(&handler1, ids);
registrar.DetachFromThreadForTest();
EXPECT_DEATH({ registrar.UpdateRegisteredIds(&handler2, ids); }, "");
registrar.UnregisterHandler(&handler2);
registrar.UnregisterHandler(&handler1);
}
#endif // GTEST_HAS_DEATH_TEST
} // namespace
} // namespace syncer
|