[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / invalidation / ticl_invalidation_service_unittest.cc
blobbd838c5bda478b727aba36f9155fa904a4ec10d0
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
3 // found in the LICENSE file.
5 #include "chrome/browser/invalidation/ticl_invalidation_service.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/weak_ptr.h"
10 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h"
11 #include "chrome/browser/invalidation/invalidation_service_test_template.h"
12 #include "chrome/browser/services/gcm/gcm_driver.h"
13 #include "google_apis/gaia/fake_identity_provider.h"
14 #include "google_apis/gaia/fake_oauth2_token_service.h"
15 #include "net/url_request/url_request_context_getter.h"
16 #include "sync/notifier/fake_invalidation_state_tracker.h"
17 #include "sync/notifier/fake_invalidator.h"
18 #include "sync/notifier/invalidation_state_tracker.h"
19 #include "sync/notifier/invalidator.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace invalidation {
24 namespace {
26 class FakeTiclSettingsProvider : public TiclSettingsProvider {
27 public:
28 FakeTiclSettingsProvider();
29 virtual ~FakeTiclSettingsProvider();
31 // TiclSettingsProvider:
32 virtual bool UseGCMChannel() const OVERRIDE;
34 private:
35 DISALLOW_COPY_AND_ASSIGN(FakeTiclSettingsProvider);
38 class FakeGCMDriver : public gcm::GCMDriver {
39 public:
40 explicit FakeGCMDriver(OAuth2TokenService* token_service);
41 virtual ~FakeGCMDriver();
43 protected:
44 // gcm::GCMDriver:
45 virtual bool ShouldStartAutomatically() const OVERRIDE;
46 virtual base::FilePath GetStorePath() const OVERRIDE;
47 virtual scoped_refptr<net::URLRequestContextGetter>
48 GetURLRequestContextGetter() const OVERRIDE;
50 private:
51 DISALLOW_COPY_AND_ASSIGN(FakeGCMDriver);
54 FakeTiclSettingsProvider::FakeTiclSettingsProvider() {
57 FakeTiclSettingsProvider::~FakeTiclSettingsProvider() {
60 bool FakeTiclSettingsProvider::UseGCMChannel() const {
61 return false;
64 FakeGCMDriver::FakeGCMDriver(OAuth2TokenService* token_service)
65 : GCMDriver(scoped_ptr<IdentityProvider>(
66 new FakeIdentityProvider(token_service))) {
69 FakeGCMDriver::~FakeGCMDriver() {
72 bool FakeGCMDriver::ShouldStartAutomatically() const {
73 return false;
76 base::FilePath FakeGCMDriver::GetStorePath() const {
77 return base::FilePath();
80 scoped_refptr<net::URLRequestContextGetter>
81 FakeGCMDriver::GetURLRequestContextGetter() const {
82 return NULL;
85 } // namespace
87 class TiclInvalidationServiceTestDelegate {
88 public:
89 TiclInvalidationServiceTestDelegate() {}
91 ~TiclInvalidationServiceTestDelegate() {
92 DestroyInvalidationService();
95 void CreateInvalidationService() {
96 CreateUninitializedInvalidationService();
97 InitializeInvalidationService();
100 void CreateUninitializedInvalidationService() {
101 gcm_service_.reset(new FakeGCMDriver(&token_service_));
102 invalidation_service_.reset(new TiclInvalidationService(
103 scoped_ptr<IdentityProvider>(new FakeIdentityProvider(&token_service_)),
104 scoped_ptr<TiclSettingsProvider>(new FakeTiclSettingsProvider),
105 gcm_service_.get(),
106 NULL));
109 void InitializeInvalidationService() {
110 fake_invalidator_ = new syncer::FakeInvalidator();
111 invalidation_service_->InitForTest(
112 scoped_ptr<syncer::InvalidationStateTracker>(
113 new syncer::FakeInvalidationStateTracker),
114 fake_invalidator_);
117 InvalidationService* GetInvalidationService() {
118 return invalidation_service_.get();
121 void DestroyInvalidationService() {
122 invalidation_service_->Shutdown();
125 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) {
126 fake_invalidator_->EmitOnInvalidatorStateChange(state);
129 void TriggerOnIncomingInvalidation(
130 const syncer::ObjectIdInvalidationMap& invalidation_map) {
131 fake_invalidator_->EmitOnIncomingInvalidation(invalidation_map);
134 FakeOAuth2TokenService token_service_;
135 scoped_ptr<gcm::GCMDriver> gcm_service_;
136 syncer::FakeInvalidator* fake_invalidator_; // Owned by the service.
138 scoped_ptr<TiclInvalidationService> invalidation_service_;
141 INSTANTIATE_TYPED_TEST_CASE_P(
142 TiclInvalidationServiceTest, InvalidationServiceTest,
143 TiclInvalidationServiceTestDelegate);
145 namespace internal {
147 class FakeCallbackContainer {
148 public:
149 FakeCallbackContainer() : called_(false),
150 weak_ptr_factory_(this) {}
152 void FakeCallback(const base::DictionaryValue& value) {
153 called_ = true;
156 bool called_;
157 base::WeakPtrFactory<FakeCallbackContainer> weak_ptr_factory_;
160 } // namespace internal
162 // Test that requesting for detailed status doesn't crash even if the
163 // underlying invalidator is not initialized.
164 TEST(TiclInvalidationServiceLoggingTest, DetailedStatusCallbacksWork) {
165 scoped_ptr<TiclInvalidationServiceTestDelegate> delegate (
166 new TiclInvalidationServiceTestDelegate());
168 delegate->CreateUninitializedInvalidationService();
169 invalidation::InvalidationService* const invalidator =
170 delegate->GetInvalidationService();
172 internal::FakeCallbackContainer fake_container;
173 invalidator->RequestDetailedStatus(
174 base::Bind(&internal::FakeCallbackContainer::FakeCallback,
175 fake_container.weak_ptr_factory_.GetWeakPtr()));
176 EXPECT_FALSE(fake_container.called_);
178 delegate->InitializeInvalidationService();
180 invalidator->RequestDetailedStatus(
181 base::Bind(&internal::FakeCallbackContainer::FakeCallback,
182 fake_container.weak_ptr_factory_.GetWeakPtr()));
183 EXPECT_TRUE(fake_container.called_);
186 } // namespace invalidation