Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / invalidation / gcm_invalidation_bridge_unittest.cc
index 6ab650e..3cb9046 100644 (file)
@@ -3,74 +3,77 @@
 // found in the LICENSE file.
 
 #include "base/run_loop.h"
-#include "chrome/browser/invalidation/gcm_invalidation_bridge.h"
-#include "chrome/browser/services/gcm/gcm_profile_service.h"
-#include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
 #include "chrome/test/base/testing_profile.h"
+#include "components/gcm_driver/fake_gcm_driver.h"
+#include "components/gcm_driver/gcm_driver.h"
+#include "components/invalidation/gcm_invalidation_bridge.h"
 #include "content/public/test/test_browser_thread_bundle.h"
 #include "google_apis/gaia/fake_identity_provider.h"
 #include "google_apis/gaia/google_service_auth_error.h"
+#include "net/base/ip_endpoint.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 namespace invalidation {
 namespace {
 
-// Implementation of GCMProfileService::Register that always succeeds with the
-// same registrationId.
-class FakeGCMProfileService : public gcm::GCMProfileService {
+// Implementation of GCMDriver::Register that always succeeds with the same
+// registrationId.
+class CustomFakeGCMDriver : public gcm::FakeGCMDriver {
  public:
-  static KeyedService* Build(content::BrowserContext* context) {
-    Profile* profile = static_cast<Profile*>(context);
-    return new FakeGCMProfileService(profile);
-  }
-
-  explicit FakeGCMProfileService(Profile* profile)
-      : gcm::GCMProfileService(profile) {}
+  CustomFakeGCMDriver() {}
+  ~CustomFakeGCMDriver() override {}
 
-  virtual void Register(const std::string& app_id,
-                        const std::vector<std::string>& sender_ids,
-                        RegisterCallback callback) OVERRIDE {
+ protected:
+  // FakeGCMDriver override:
+  void RegisterImpl(const std::string& app_id,
+                    const std::vector<std::string>& sender_ids) override {
     base::MessageLoop::current()->PostTask(
         FROM_HERE,
-        base::Bind(
-            callback, std::string("registration.id"), gcm::GCMClient::SUCCESS));
+        base::Bind(&CustomFakeGCMDriver::RegisterFinished,
+                   base::Unretained(this),
+                   app_id,
+                   std::string("registration.id"),
+                   gcm::GCMClient::SUCCESS));
   }
 
  private:
-  DISALLOW_COPY_AND_ASSIGN(FakeGCMProfileService);
+  DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
 };
 
 class GCMInvalidationBridgeTest : public ::testing::Test {
  protected:
-  GCMInvalidationBridgeTest() {}
+  GCMInvalidationBridgeTest()
+      : connection_online_(false) {}
 
-  virtual ~GCMInvalidationBridgeTest() {}
+  ~GCMInvalidationBridgeTest() override {}
 
-  virtual void SetUp() OVERRIDE {
+  void SetUp() override {
     TestingProfile::Builder builder;
     builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
                               &BuildAutoIssuingFakeProfileOAuth2TokenService);
-    builder.AddTestingFactory(gcm::GCMProfileServiceFactory::GetInstance(),
-                              &FakeGCMProfileService::Build);
     profile_ = builder.Build();
 
     FakeProfileOAuth2TokenService* token_service =
         (FakeProfileOAuth2TokenService*)
         ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
     token_service->IssueRefreshTokenForUser("", "fake_refresh_token");
-    gcm_profile_service_ =
-        (FakeGCMProfileService*)gcm::GCMProfileServiceFactory::GetForProfile(
-            profile_.get());
+    gcm_driver_.reset(new CustomFakeGCMDriver());
 
     identity_provider_.reset(new FakeIdentityProvider(token_service));
-    bridge_.reset(new GCMInvalidationBridge(gcm_profile_service_,
+    bridge_.reset(new GCMInvalidationBridge(gcm_driver_.get(),
                                             identity_provider_.get()));
 
     delegate_ = bridge_->CreateDelegate();
-    delegate_->Initialize();
+    delegate_->Initialize(
+        base::Bind(&GCMInvalidationBridgeTest::ConnectionStateChanged,
+                   base::Unretained(this)));
+    RunLoop();
+  }
+
+  void RunLoop() {
     base::RunLoop run_loop;
     run_loop.RunUntilIdle();
   }
@@ -87,14 +90,19 @@ class GCMInvalidationBridgeTest : public ::testing::Test {
     request_token_errors_.push_back(error);
   }
 
+  void ConnectionStateChanged(bool online) {
+    connection_online_ = online;
+  }
+
   content::TestBrowserThreadBundle thread_bundle_;
   scoped_ptr<Profile> profile_;
-  FakeGCMProfileService* gcm_profile_service_;
+  scoped_ptr<gcm::GCMDriver> gcm_driver_;
   scoped_ptr<FakeIdentityProvider> identity_provider_;
 
   std::vector<std::string> issued_tokens_;
   std::vector<GoogleServiceAuthError> request_token_errors_;
   std::string registration_id_;
+  bool connection_online_;
 
   scoped_ptr<GCMInvalidationBridge> bridge_;
   scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_;
@@ -106,8 +114,7 @@ TEST_F(GCMInvalidationBridgeTest, RequestToken) {
   delegate_->RequestToken(
       base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
                  base::Unretained(this)));
-  base::RunLoop run_loop;
-  run_loop.RunUntilIdle();
+  RunLoop();
   EXPECT_EQ(1U, issued_tokens_.size());
   EXPECT_NE("", issued_tokens_[0]);
   EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]);
@@ -122,8 +129,7 @@ TEST_F(GCMInvalidationBridgeTest, RequestTokenTwoConcurrentRequests) {
   delegate_->RequestToken(
       base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
                  base::Unretained(this)));
-  base::RunLoop run_loop;
-  run_loop.RunUntilIdle();
+  RunLoop();
 
   EXPECT_EQ(2U, issued_tokens_.size());
 
@@ -139,11 +145,20 @@ TEST_F(GCMInvalidationBridgeTest, Register) {
   EXPECT_TRUE(registration_id_.empty());
   delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished,
                                  base::Unretained(this)));
-  base::RunLoop run_loop;
-  run_loop.RunUntilIdle();
+  RunLoop();
 
   EXPECT_FALSE(registration_id_.empty());
 }
 
+TEST_F(GCMInvalidationBridgeTest, ConnectionState) {
+  EXPECT_FALSE(connection_online_);
+  bridge_->OnConnected(net::IPEndPoint());
+  RunLoop();
+  EXPECT_TRUE(connection_online_);
+  bridge_->OnDisconnected();
+  RunLoop();
+  EXPECT_FALSE(connection_online_);
+}
+
 }  // namespace
 }  // namespace invalidation