Home | History | Annotate | Download | only in invalidation
      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 "chrome/browser/invalidation/ticl_profile_settings_provider.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/prefs/pref_service.h"
      9 #include "chrome/browser/profiles/profile.h"
     10 #include "chrome/browser/services/gcm/gcm_profile_service.h"
     11 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
     12 #include "chrome/common/pref_names.h"
     13 #include "chrome/test/base/testing_profile.h"
     14 #include "components/invalidation/fake_invalidation_state_tracker.h"
     15 #include "components/invalidation/ticl_invalidation_service.h"
     16 #include "components/invalidation/ticl_settings_provider.h"
     17 #include "content/public/test/test_browser_thread_bundle.h"
     18 #include "google_apis/gaia/fake_identity_provider.h"
     19 #include "google_apis/gaia/fake_oauth2_token_service.h"
     20 #include "google_apis/gaia/identity_provider.h"
     21 #include "net/url_request/url_request_context_getter.h"
     22 #include "sync/notifier/invalidation_state_tracker.h"
     23 #include "testing/gtest/include/gtest/gtest.h"
     24 
     25 namespace invalidation {
     26 
     27 class TiclProfileSettingsProviderTest : public testing::Test {
     28  protected:
     29   TiclProfileSettingsProviderTest();
     30   virtual ~TiclProfileSettingsProviderTest();
     31 
     32   // testing::Test:
     33   virtual void SetUp() OVERRIDE;
     34   virtual void TearDown() OVERRIDE;
     35 
     36   TiclInvalidationService::InvalidationNetworkChannel GetNetworkChannel();
     37 
     38   content::TestBrowserThreadBundle thread_bundle_;
     39   TestingProfile profile_;
     40   FakeOAuth2TokenService token_service_;
     41 
     42   scoped_ptr<TiclInvalidationService> invalidation_service_;
     43 
     44  private:
     45   DISALLOW_COPY_AND_ASSIGN(TiclProfileSettingsProviderTest);
     46 };
     47 
     48 TiclProfileSettingsProviderTest::TiclProfileSettingsProviderTest() {
     49 }
     50 
     51 TiclProfileSettingsProviderTest::~TiclProfileSettingsProviderTest() {
     52 }
     53 
     54 void TiclProfileSettingsProviderTest::SetUp() {
     55   invalidation_service_.reset(new TiclInvalidationService(
     56       "TestUserAgent",
     57       scoped_ptr<IdentityProvider>(new FakeIdentityProvider(&token_service_)),
     58       scoped_ptr<TiclSettingsProvider>(
     59           new TiclProfileSettingsProvider(&profile_)),
     60       gcm::GCMProfileServiceFactory::GetForProfile(&profile_)->driver(),
     61       profile_.GetRequestContext()));
     62   invalidation_service_->Init(scoped_ptr<syncer::InvalidationStateTracker>(
     63       new syncer::FakeInvalidationStateTracker));
     64 }
     65 
     66 void TiclProfileSettingsProviderTest::TearDown() {
     67   invalidation_service_.reset();
     68 }
     69 
     70 TiclInvalidationService::InvalidationNetworkChannel
     71 TiclProfileSettingsProviderTest::GetNetworkChannel() {
     72   return invalidation_service_->network_channel_type_;
     73 }
     74 
     75 TEST_F(TiclProfileSettingsProviderTest, ChannelSelectionTest) {
     76   EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel());
     77   PrefService* prefs =  profile_.GetPrefs();
     78 
     79   // If stars align, use GCM channel.
     80   prefs->SetBoolean(prefs::kGCMChannelEnabled, true);
     81   prefs->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, true);
     82   EXPECT_EQ(TiclInvalidationService::GCM_NETWORK_CHANNEL, GetNetworkChannel());
     83 
     84   // If invalidation channel setting is not set or says false, fall back to push
     85   // channel.
     86   prefs->SetBoolean(prefs::kGCMChannelEnabled, true);
     87 
     88   prefs->ClearPref(prefs::kInvalidationServiceUseGCMChannel);
     89   EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel());
     90 
     91   prefs->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, false);
     92   EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel());
     93 
     94   // If invalidation channel setting says use GCM but GCM is not ALWAYS_ENABLED,
     95   // fall back to push channel.
     96   prefs->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, false);
     97 
     98   prefs->ClearPref(prefs::kGCMChannelEnabled);
     99   EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel());
    100 
    101   prefs->SetBoolean(prefs::kGCMChannelEnabled, false);
    102   EXPECT_EQ(TiclInvalidationService::PUSH_CLIENT_CHANNEL, GetNetworkChannel());
    103 
    104   // If invalidation channel setting is enabled first and the GCM setting is
    105   // enabled after that, switch to GCM channel.
    106   prefs->SetBoolean(prefs::kInvalidationServiceUseGCMChannel, true);
    107   prefs->SetBoolean(prefs::kGCMChannelEnabled, true);
    108   EXPECT_EQ(TiclInvalidationService::GCM_NETWORK_CHANNEL, GetNetworkChannel());
    109 }
    110 
    111 }  // namespace invalidation
    112