3cb9046b77aae8f5ad4bd4da315498a69cad9e16
[platform/framework/web/crosswalk.git] / src / chrome / browser / invalidation / gcm_invalidation_bridge_unittest.cc
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 "base/run_loop.h"
6 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
7 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
8 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "components/gcm_driver/fake_gcm_driver.h"
11 #include "components/gcm_driver/gcm_driver.h"
12 #include "components/invalidation/gcm_invalidation_bridge.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "google_apis/gaia/fake_identity_provider.h"
15 #include "google_apis/gaia/google_service_auth_error.h"
16 #include "net/base/ip_endpoint.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace invalidation {
20 namespace {
21
22 // Implementation of GCMDriver::Register that always succeeds with the same
23 // registrationId.
24 class CustomFakeGCMDriver : public gcm::FakeGCMDriver {
25  public:
26   CustomFakeGCMDriver() {}
27   ~CustomFakeGCMDriver() override {}
28
29  protected:
30   // FakeGCMDriver override:
31   void RegisterImpl(const std::string& app_id,
32                     const std::vector<std::string>& sender_ids) override {
33     base::MessageLoop::current()->PostTask(
34         FROM_HERE,
35         base::Bind(&CustomFakeGCMDriver::RegisterFinished,
36                    base::Unretained(this),
37                    app_id,
38                    std::string("registration.id"),
39                    gcm::GCMClient::SUCCESS));
40   }
41
42  private:
43   DISALLOW_COPY_AND_ASSIGN(CustomFakeGCMDriver);
44 };
45
46 class GCMInvalidationBridgeTest : public ::testing::Test {
47  protected:
48   GCMInvalidationBridgeTest()
49       : connection_online_(false) {}
50
51   ~GCMInvalidationBridgeTest() override {}
52
53   void SetUp() override {
54     TestingProfile::Builder builder;
55     builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
56                               &BuildAutoIssuingFakeProfileOAuth2TokenService);
57     profile_ = builder.Build();
58
59     FakeProfileOAuth2TokenService* token_service =
60         (FakeProfileOAuth2TokenService*)
61         ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
62     token_service->IssueRefreshTokenForUser("", "fake_refresh_token");
63     gcm_driver_.reset(new CustomFakeGCMDriver());
64
65     identity_provider_.reset(new FakeIdentityProvider(token_service));
66     bridge_.reset(new GCMInvalidationBridge(gcm_driver_.get(),
67                                             identity_provider_.get()));
68
69     delegate_ = bridge_->CreateDelegate();
70     delegate_->Initialize(
71         base::Bind(&GCMInvalidationBridgeTest::ConnectionStateChanged,
72                    base::Unretained(this)));
73     RunLoop();
74   }
75
76   void RunLoop() {
77     base::RunLoop run_loop;
78     run_loop.RunUntilIdle();
79   }
80
81  public:
82   void RegisterFinished(const std::string& registration_id,
83                         gcm::GCMClient::Result result) {
84     registration_id_ = registration_id;
85   }
86
87   void RequestTokenFinished(const GoogleServiceAuthError& error,
88                             const std::string& token) {
89     issued_tokens_.push_back(token);
90     request_token_errors_.push_back(error);
91   }
92
93   void ConnectionStateChanged(bool online) {
94     connection_online_ = online;
95   }
96
97   content::TestBrowserThreadBundle thread_bundle_;
98   scoped_ptr<Profile> profile_;
99   scoped_ptr<gcm::GCMDriver> gcm_driver_;
100   scoped_ptr<FakeIdentityProvider> identity_provider_;
101
102   std::vector<std::string> issued_tokens_;
103   std::vector<GoogleServiceAuthError> request_token_errors_;
104   std::string registration_id_;
105   bool connection_online_;
106
107   scoped_ptr<GCMInvalidationBridge> bridge_;
108   scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_;
109 };
110
111 TEST_F(GCMInvalidationBridgeTest, RequestToken) {
112   // Make sure that call to RequestToken reaches OAuth2TokenService and gets
113   // back to callback.
114   delegate_->RequestToken(
115       base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
116                  base::Unretained(this)));
117   RunLoop();
118   EXPECT_EQ(1U, issued_tokens_.size());
119   EXPECT_NE("", issued_tokens_[0]);
120   EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]);
121 }
122
123 TEST_F(GCMInvalidationBridgeTest, RequestTokenTwoConcurrentRequests) {
124   // First call should finish with REQUEST_CANCELLED error.
125   delegate_->RequestToken(
126       base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
127                  base::Unretained(this)));
128   // Second request should succeed.
129   delegate_->RequestToken(
130       base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
131                  base::Unretained(this)));
132   RunLoop();
133
134   EXPECT_EQ(2U, issued_tokens_.size());
135
136   EXPECT_EQ("", issued_tokens_[0]);
137   EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED,
138             request_token_errors_[0].state());
139
140   EXPECT_NE("", issued_tokens_[1]);
141   EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]);
142 }
143
144 TEST_F(GCMInvalidationBridgeTest, Register) {
145   EXPECT_TRUE(registration_id_.empty());
146   delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished,
147                                  base::Unretained(this)));
148   RunLoop();
149
150   EXPECT_FALSE(registration_id_.empty());
151 }
152
153 TEST_F(GCMInvalidationBridgeTest, ConnectionState) {
154   EXPECT_FALSE(connection_online_);
155   bridge_->OnConnected(net::IPEndPoint());
156   RunLoop();
157   EXPECT_TRUE(connection_online_);
158   bridge_->OnDisconnected();
159   RunLoop();
160   EXPECT_FALSE(connection_online_);
161 }
162
163 }  // namespace
164 }  // namespace invalidation