OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/invalidation/ticl_invalidation_service.h" | |
6 | |
7 #include "base/bind.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" | |
22 | |
23 namespace invalidation { | |
24 | |
25 namespace { | |
26 | |
27 class FakeTiclSettingsProvider : public TiclSettingsProvider { | |
28 public: | |
29 FakeTiclSettingsProvider(); | |
30 ~FakeTiclSettingsProvider() override; | |
31 | |
32 // TiclSettingsProvider: | |
33 bool UseGCMChannel() const override; | |
34 | |
35 private: | |
36 DISALLOW_COPY_AND_ASSIGN(FakeTiclSettingsProvider); | |
37 }; | |
38 | |
39 FakeTiclSettingsProvider::FakeTiclSettingsProvider() { | |
40 } | |
41 | |
42 FakeTiclSettingsProvider::~FakeTiclSettingsProvider() { | |
43 } | |
44 | |
45 bool FakeTiclSettingsProvider::UseGCMChannel() const { | |
46 return false; | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 class TiclInvalidationServiceTestDelegate { | |
52 public: | |
53 TiclInvalidationServiceTestDelegate() {} | |
54 | |
55 ~TiclInvalidationServiceTestDelegate() { | |
56 } | |
57 | |
58 void CreateInvalidationService() { | |
59 CreateUninitializedInvalidationService(); | |
60 InitializeInvalidationService(); | |
61 } | |
62 | |
63 void CreateUninitializedInvalidationService() { | |
64 gcm_driver_.reset(new gcm::FakeGCMDriver()); | |
65 invalidation_service_.reset(new TiclInvalidationService( | |
66 "TestUserAgent", | |
67 scoped_ptr<IdentityProvider>(new FakeIdentityProvider(&token_service_)), | |
68 scoped_ptr<TiclSettingsProvider>(new FakeTiclSettingsProvider), | |
69 gcm_driver_.get(), | |
70 NULL)); | |
71 } | |
72 | |
73 void InitializeInvalidationService() { | |
74 fake_invalidator_ = new syncer::FakeInvalidator(); | |
75 invalidation_service_->InitForTest( | |
76 scoped_ptr<syncer::InvalidationStateTracker>( | |
77 new syncer::FakeInvalidationStateTracker), | |
78 fake_invalidator_); | |
79 } | |
80 | |
81 InvalidationService* GetInvalidationService() { | |
82 return invalidation_service_.get(); | |
83 } | |
84 | |
85 void DestroyInvalidationService() { | |
86 invalidation_service_.reset(); | |
87 } | |
88 | |
89 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) { | |
90 fake_invalidator_->EmitOnInvalidatorStateChange(state); | |
91 } | |
92 | |
93 void TriggerOnIncomingInvalidation( | |
94 const syncer::ObjectIdInvalidationMap& invalidation_map) { | |
95 fake_invalidator_->EmitOnIncomingInvalidation(invalidation_map); | |
96 } | |
97 | |
98 FakeOAuth2TokenService token_service_; | |
99 scoped_ptr<gcm::GCMDriver> gcm_driver_; | |
100 syncer::FakeInvalidator* fake_invalidator_; // Owned by the service. | |
101 | |
102 scoped_ptr<TiclInvalidationService> invalidation_service_; | |
103 }; | |
104 | |
105 INSTANTIATE_TYPED_TEST_CASE_P( | |
106 TiclInvalidationServiceTest, InvalidationServiceTest, | |
107 TiclInvalidationServiceTestDelegate); | |
108 | |
109 namespace internal { | |
110 | |
111 class FakeCallbackContainer { | |
112 public: | |
113 FakeCallbackContainer() : called_(false), | |
114 weak_ptr_factory_(this) {} | |
115 | |
116 void FakeCallback(const base::DictionaryValue& value) { | |
117 called_ = true; | |
118 } | |
119 | |
120 bool called_; | |
121 base::WeakPtrFactory<FakeCallbackContainer> weak_ptr_factory_; | |
122 }; | |
123 | |
124 } // namespace internal | |
125 | |
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()); | |
131 | |
132 delegate->CreateUninitializedInvalidationService(); | |
133 invalidation::InvalidationService* const invalidator = | |
134 delegate->GetInvalidationService(); | |
135 | |
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_); | |
141 | |
142 delegate->InitializeInvalidationService(); | |
143 | |
144 invalidator->RequestDetailedStatus( | |
145 base::Bind(&internal::FakeCallbackContainer::FakeCallback, | |
146 fake_container.weak_ptr_factory_.GetWeakPtr())); | |
147 EXPECT_TRUE(fake_container.called_); | |
148 } | |
149 | |
150 } // namespace invalidation | |
OLD | NEW |