1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/invalidation/ticl_invalidation_service.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/weak_ptr.h"
10 #include "components/gcm_driver/fake_gcm_driver.h"
11 #include "components/gcm_driver/gcm_driver.h"
12 #include "components/invalidation/fake_invalidation_state_tracker.h"
13 #include "components/invalidation/fake_invalidator.h"
14 #include "components/invalidation/gcm_invalidation_bridge.h"
15 #include "components/invalidation/invalidation_service_test_template.h"
16 #include "components/invalidation/invalidation_state_tracker.h"
17 #include "components/invalidation/invalidator.h"
18 #include "google_apis/gaia/fake_identity_provider.h"
19 #include "google_apis/gaia/fake_oauth2_token_service.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace invalidation
{
27 class FakeTiclSettingsProvider
: public TiclSettingsProvider
{
29 FakeTiclSettingsProvider();
30 ~FakeTiclSettingsProvider() override
;
32 // TiclSettingsProvider:
33 bool UseGCMChannel() const override
;
36 DISALLOW_COPY_AND_ASSIGN(FakeTiclSettingsProvider
);
39 FakeTiclSettingsProvider::FakeTiclSettingsProvider() {
42 FakeTiclSettingsProvider::~FakeTiclSettingsProvider() {
45 bool FakeTiclSettingsProvider::UseGCMChannel() const {
51 class TiclInvalidationServiceTestDelegate
{
53 TiclInvalidationServiceTestDelegate() {}
55 ~TiclInvalidationServiceTestDelegate() {
58 void CreateInvalidationService() {
59 CreateUninitializedInvalidationService();
60 InitializeInvalidationService();
63 void CreateUninitializedInvalidationService() {
64 gcm_driver_
.reset(new gcm::FakeGCMDriver());
65 invalidation_service_
.reset(new TiclInvalidationService(
67 scoped_ptr
<IdentityProvider
>(new FakeIdentityProvider(&token_service_
)),
68 scoped_ptr
<TiclSettingsProvider
>(new FakeTiclSettingsProvider
),
73 void InitializeInvalidationService() {
74 fake_invalidator_
= new syncer::FakeInvalidator();
75 invalidation_service_
->InitForTest(
76 scoped_ptr
<syncer::InvalidationStateTracker
>(
77 new syncer::FakeInvalidationStateTracker
),
81 InvalidationService
* GetInvalidationService() {
82 return invalidation_service_
.get();
85 void DestroyInvalidationService() {
86 invalidation_service_
.reset();
89 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state
) {
90 fake_invalidator_
->EmitOnInvalidatorStateChange(state
);
93 void TriggerOnIncomingInvalidation(
94 const syncer::ObjectIdInvalidationMap
& invalidation_map
) {
95 fake_invalidator_
->EmitOnIncomingInvalidation(invalidation_map
);
98 FakeOAuth2TokenService token_service_
;
99 scoped_ptr
<gcm::GCMDriver
> gcm_driver_
;
100 syncer::FakeInvalidator
* fake_invalidator_
; // Owned by the service.
102 scoped_ptr
<TiclInvalidationService
> invalidation_service_
;
105 INSTANTIATE_TYPED_TEST_CASE_P(
106 TiclInvalidationServiceTest
, InvalidationServiceTest
,
107 TiclInvalidationServiceTestDelegate
);
111 class FakeCallbackContainer
{
113 FakeCallbackContainer() : called_(false),
114 weak_ptr_factory_(this) {}
116 void FakeCallback(const base::DictionaryValue
& value
) {
121 base::WeakPtrFactory
<FakeCallbackContainer
> weak_ptr_factory_
;
124 } // namespace internal
126 // Test that requesting for detailed status doesn't crash even if the
127 // underlying invalidator is not initialized.
128 TEST(TiclInvalidationServiceLoggingTest
, DetailedStatusCallbacksWork
) {
129 scoped_ptr
<TiclInvalidationServiceTestDelegate
> delegate (
130 new TiclInvalidationServiceTestDelegate());
132 delegate
->CreateUninitializedInvalidationService();
133 invalidation::InvalidationService
* const invalidator
=
134 delegate
->GetInvalidationService();
136 internal::FakeCallbackContainer fake_container
;
137 invalidator
->RequestDetailedStatus(
138 base::Bind(&internal::FakeCallbackContainer::FakeCallback
,
139 fake_container
.weak_ptr_factory_
.GetWeakPtr()));
140 EXPECT_FALSE(fake_container
.called_
);
142 delegate
->InitializeInvalidationService();
144 invalidator
->RequestDetailedStatus(
145 base::Bind(&internal::FakeCallbackContainer::FakeCallback
,
146 fake_container
.weak_ptr_factory_
.GetWeakPtr()));
147 EXPECT_TRUE(fake_container
.called_
);
150 } // namespace invalidation