Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / invalidation / invalidation_service_android_unittest.cc
blobd652bee9aa1a8000cc5d77b209d891f001910bdb
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/invalidation_service_android.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/invalidation/invalidation_controller_android.h"
9 #include "chrome/browser/invalidation/invalidation_service_factory.h"
10 #include "chrome/browser/invalidation/invalidation_service_test_template.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/browser/notification_service.h"
13 #include "sync/notifier/fake_invalidation_handler.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace invalidation {
18 class MockInvalidationControllerAndroid : public InvalidationControllerAndroid {
19 public:
20 MockInvalidationControllerAndroid() {}
21 virtual ~MockInvalidationControllerAndroid() {}
23 virtual void SetRegisteredObjectIds(const syncer::ObjectIdSet& ids) OVERRIDE {
24 registered_ids_ = ids;
27 syncer::ObjectIdSet registered_ids_;
30 class InvalidationServiceAndroidTestDelegate {
31 public:
32 InvalidationServiceAndroidTestDelegate() { }
34 ~InvalidationServiceAndroidTestDelegate() {
35 DestroyInvalidationService();
38 void CreateInvalidationService() {
39 profile_.reset(new TestingProfile());
40 invalidation_service_android_.reset(
41 new InvalidationServiceAndroid(
42 profile_.get(),
43 new MockInvalidationControllerAndroid()));
46 InvalidationService* GetInvalidationService() {
47 return invalidation_service_android_.get();
50 void DestroyInvalidationService() {
51 invalidation_service_android_->Shutdown();
54 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) {
55 invalidation_service_android_->TriggerStateChangeForTest(state);
58 void TriggerOnIncomingInvalidation(
59 const syncer::ObjectIdInvalidationMap& invalidation_map) {
60 content::NotificationService::current()->Notify(
61 chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
62 content::Source<Profile>(profile_.get()),
63 content::Details<const syncer::ObjectIdInvalidationMap>(
64 &invalidation_map));
67 scoped_ptr<TestingProfile> profile_;
68 scoped_ptr<InvalidationServiceAndroid> invalidation_service_android_;
71 INSTANTIATE_TYPED_TEST_CASE_P(
72 AndroidInvalidationServiceTest, InvalidationServiceTest,
73 InvalidationServiceAndroidTestDelegate);
75 class InvalidationServiceAndroidRegistrationTest : public testing::Test {
76 protected:
77 InvalidationServiceAndroidRegistrationTest()
78 : invalidation_controller_(new MockInvalidationControllerAndroid()),
79 invalidation_service_(&profile_, invalidation_controller_) {}
81 virtual ~InvalidationServiceAndroidRegistrationTest() {
82 invalidation_service_.Shutdown();
85 // Get the invalidation service being tested.
86 InvalidationService& invalidation_service() {
87 return invalidation_service_;
90 // Get the number of objects which are registered.
91 size_t RegisteredObjectCount() {
92 return registered_ids().size();
95 // Determines if the given object id is registered with the invalidation
96 // controller.
97 bool IsRegistered(const invalidation::ObjectId& id) {
98 return registered_ids().find(id) != registered_ids().end();
101 private:
102 // Get the set of objects registered with the invalidation controller.
103 const syncer::ObjectIdSet& registered_ids() {
104 return invalidation_controller_->registered_ids_;
107 TestingProfile profile_;
108 MockInvalidationControllerAndroid* invalidation_controller_;
109 InvalidationServiceAndroid invalidation_service_;
112 TEST_F(InvalidationServiceAndroidRegistrationTest, NoObjectRegistration) {
113 syncer::FakeInvalidationHandler handler;
114 invalidation_service().RegisterInvalidationHandler(&handler);
115 EXPECT_EQ(0U, RegisteredObjectCount());
116 invalidation_service().UnregisterInvalidationHandler(&handler);
119 TEST_F(InvalidationServiceAndroidRegistrationTest, UpdateObjectRegistration) {
120 syncer::FakeInvalidationHandler handler;
121 invalidation::ObjectId id1(1, "A");
122 invalidation::ObjectId id2(2, "B");
123 syncer::ObjectIdSet ids;
124 invalidation_service().RegisterInvalidationHandler(&handler);
126 // Register for both objects.
127 ids.insert(id1);
128 ids.insert(id2);
129 invalidation_service().UpdateRegisteredInvalidationIds(&handler, ids);
130 EXPECT_EQ(2U, RegisteredObjectCount());
131 EXPECT_TRUE(IsRegistered(id1));
132 EXPECT_TRUE(IsRegistered(id2));
134 // Unregister for object 2.
135 ids.erase(id2);
136 invalidation_service().UpdateRegisteredInvalidationIds(&handler, ids);
137 EXPECT_EQ(1U, RegisteredObjectCount());
138 EXPECT_TRUE(IsRegistered(id1));
140 // Unregister for object 1.
141 ids.erase(id1);
142 invalidation_service().UpdateRegisteredInvalidationIds(&handler, ids);
143 EXPECT_EQ(0U, RegisteredObjectCount());
145 invalidation_service().UnregisterInvalidationHandler(&handler);
148 #if defined(OS_ANDROID)
150 class InvalidationServiceAndroidTest : public testing::Test {
151 public:
152 InvalidationServiceAndroidTest()
153 : invalidation_service_(&profile_, new InvalidationControllerAndroid()) {}
154 virtual ~InvalidationServiceAndroidTest() {}
156 InvalidationService& invalidation_service() {
157 return invalidation_service_;
160 private:
161 TestingProfile profile_;
162 InvalidationServiceAndroid invalidation_service_;
165 TEST_F(InvalidationServiceAndroidTest, FetchClientId) {
166 const std::string id1 = invalidation_service().GetInvalidatorClientId();
167 ASSERT_FALSE(id1.empty());
169 // If nothing else, the ID should be consistent.
170 const std::string id2 = invalidation_service().GetInvalidatorClientId();
171 ASSERT_EQ(id1, id2);
174 #endif
176 } // namespace invalidation