Home | History | Annotate | Download | only in invalidation
      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.
      4 
      5 #include "chrome/browser/invalidation/invalidation_service_android.h"
      6 
      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"
     15 
     16 namespace invalidation {
     17 
     18 class MockInvalidationControllerAndroid : public InvalidationControllerAndroid {
     19  public:
     20   MockInvalidationControllerAndroid() {}
     21   virtual ~MockInvalidationControllerAndroid() {}
     22 
     23   virtual void SetRegisteredObjectIds(const syncer::ObjectIdSet& ids) OVERRIDE {
     24     registered_ids_ = ids;
     25   }
     26 
     27   syncer::ObjectIdSet registered_ids_;
     28 };
     29 
     30 class InvalidationServiceAndroidTestDelegate {
     31  public:
     32   InvalidationServiceAndroidTestDelegate() { }
     33 
     34   ~InvalidationServiceAndroidTestDelegate() {
     35     DestroyInvalidationService();
     36   }
     37 
     38   void CreateInvalidationService() {
     39     profile_.reset(new TestingProfile());
     40     invalidation_service_android_.reset(
     41         new InvalidationServiceAndroid(
     42             profile_.get(),
     43             new MockInvalidationControllerAndroid()));
     44   }
     45 
     46   InvalidationService* GetInvalidationService() {
     47     return invalidation_service_android_.get();
     48   }
     49 
     50   void DestroyInvalidationService() {
     51     invalidation_service_android_->Shutdown();
     52   }
     53 
     54   void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) {
     55     invalidation_service_android_->TriggerStateChangeForTest(state);
     56   }
     57 
     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));
     65   }
     66 
     67   scoped_ptr<TestingProfile> profile_;
     68   scoped_ptr<InvalidationServiceAndroid> invalidation_service_android_;
     69 };
     70 
     71 INSTANTIATE_TYPED_TEST_CASE_P(
     72     AndroidInvalidationServiceTest, InvalidationServiceTest,
     73     InvalidationServiceAndroidTestDelegate);
     74 
     75 class InvalidationServiceAndroidRegistrationTest : public testing::Test {
     76  protected:
     77   InvalidationServiceAndroidRegistrationTest()
     78       : invalidation_controller_(new MockInvalidationControllerAndroid()),
     79         invalidation_service_(&profile_, invalidation_controller_) {}
     80 
     81   virtual ~InvalidationServiceAndroidRegistrationTest() {
     82     invalidation_service_.Shutdown();
     83   }
     84 
     85   // Get the invalidation service being tested.
     86   InvalidationService& invalidation_service() {
     87     return invalidation_service_;
     88   }
     89 
     90   // Get the number of objects which are registered.
     91   size_t RegisteredObjectCount() {
     92     return registered_ids().size();
     93   }
     94 
     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();
     99   }
    100 
    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_;
    105   }
    106 
    107   TestingProfile profile_;
    108   MockInvalidationControllerAndroid* invalidation_controller_;
    109   InvalidationServiceAndroid invalidation_service_;
    110 };
    111 
    112 TEST_F(InvalidationServiceAndroidRegistrationTest, NoObjectRegistration) {
    113   syncer::FakeInvalidationHandler handler;
    114   invalidation_service().RegisterInvalidationHandler(&handler);
    115   EXPECT_EQ(0U, RegisteredObjectCount());
    116   invalidation_service().UnregisterInvalidationHandler(&handler);
    117 }
    118 
    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);
    125 
    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));
    133 
    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));
    139 
    140   // Unregister for object 1.
    141   ids.erase(id1);
    142   invalidation_service().UpdateRegisteredInvalidationIds(&handler, ids);
    143   EXPECT_EQ(0U, RegisteredObjectCount());
    144 
    145   invalidation_service().UnregisterInvalidationHandler(&handler);
    146 }
    147 
    148 #if defined(OS_ANDROID)
    149 
    150 class InvalidationServiceAndroidTest : public testing::Test {
    151  public:
    152   InvalidationServiceAndroidTest()
    153       : invalidation_service_(&profile_, new InvalidationControllerAndroid()) {}
    154   virtual ~InvalidationServiceAndroidTest() {}
    155 
    156   InvalidationService& invalidation_service() {
    157     return invalidation_service_;
    158   }
    159 
    160  private:
    161   TestingProfile profile_;
    162   InvalidationServiceAndroid invalidation_service_;
    163 };
    164 
    165 TEST_F(InvalidationServiceAndroidTest, FetchClientId) {
    166   const std::string id1 = invalidation_service().GetInvalidatorClientId();
    167   ASSERT_FALSE(id1.empty());
    168 
    169   // If nothing else, the ID should be consistent.
    170   const std::string id2 = invalidation_service().GetInvalidatorClientId();
    171   ASSERT_EQ(id1, id2);
    172 }
    173 
    174 #endif
    175 
    176 }  // namespace invalidation
    177