Upstream version 7.36.149.0
[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/invalidation/gcm_invalidation_bridge.h"
7 #include "chrome/browser/services/gcm/gcm_profile_service.h"
8 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
9 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
10 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12 #include "chrome/test/base/testing_profile.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 "testing/gtest/include/gtest/gtest.h"
17
18 namespace invalidation {
19 namespace {
20
21 // Implementation of GCMProfileService::Register that always succeeds with the
22 // same registrationId.
23 class FakeGCMProfileService : public gcm::GCMProfileService {
24  public:
25   static KeyedService* Build(content::BrowserContext* context) {
26     Profile* profile = static_cast<Profile*>(context);
27     return new FakeGCMProfileService(profile);
28   }
29
30   explicit FakeGCMProfileService(Profile* profile)
31       : gcm::GCMProfileService(profile) {}
32
33   virtual void Register(const std::string& app_id,
34                         const std::vector<std::string>& sender_ids,
35                         RegisterCallback callback) OVERRIDE {
36     base::MessageLoop::current()->PostTask(
37         FROM_HERE,
38         base::Bind(
39             callback, std::string("registration.id"), gcm::GCMClient::SUCCESS));
40   }
41
42  private:
43   DISALLOW_COPY_AND_ASSIGN(FakeGCMProfileService);
44 };
45
46 class GCMInvalidationBridgeTest : public ::testing::Test {
47  protected:
48   GCMInvalidationBridgeTest() {}
49
50   virtual ~GCMInvalidationBridgeTest() {}
51
52   virtual void SetUp() OVERRIDE {
53     TestingProfile::Builder builder;
54     builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
55                               &BuildAutoIssuingFakeProfileOAuth2TokenService);
56     builder.AddTestingFactory(gcm::GCMProfileServiceFactory::GetInstance(),
57                               &FakeGCMProfileService::Build);
58     profile_ = builder.Build();
59
60     FakeProfileOAuth2TokenService* token_service =
61         (FakeProfileOAuth2TokenService*)
62         ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
63     token_service->IssueRefreshTokenForUser("", "fake_refresh_token");
64     gcm_profile_service_ =
65         (FakeGCMProfileService*)gcm::GCMProfileServiceFactory::GetForProfile(
66             profile_.get());
67
68     identity_provider_.reset(new FakeIdentityProvider(token_service));
69     bridge_.reset(new GCMInvalidationBridge(gcm_profile_service_,
70                                             identity_provider_.get()));
71
72     delegate_ = bridge_->CreateDelegate();
73     delegate_->Initialize();
74     base::RunLoop run_loop;
75     run_loop.RunUntilIdle();
76   }
77
78  public:
79   void RegisterFinished(const std::string& registration_id,
80                         gcm::GCMClient::Result result) {
81     registration_id_ = registration_id;
82   }
83
84   void RequestTokenFinished(const GoogleServiceAuthError& error,
85                             const std::string& token) {
86     issued_tokens_.push_back(token);
87     request_token_errors_.push_back(error);
88   }
89
90   content::TestBrowserThreadBundle thread_bundle_;
91   scoped_ptr<Profile> profile_;
92   FakeGCMProfileService* gcm_profile_service_;
93   scoped_ptr<FakeIdentityProvider> identity_provider_;
94
95   std::vector<std::string> issued_tokens_;
96   std::vector<GoogleServiceAuthError> request_token_errors_;
97   std::string registration_id_;
98
99   scoped_ptr<GCMInvalidationBridge> bridge_;
100   scoped_ptr<syncer::GCMNetworkChannelDelegate> delegate_;
101 };
102
103 TEST_F(GCMInvalidationBridgeTest, RequestToken) {
104   // Make sure that call to RequestToken reaches OAuth2TokenService and gets
105   // back to callback.
106   delegate_->RequestToken(
107       base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
108                  base::Unretained(this)));
109   base::RunLoop run_loop;
110   run_loop.RunUntilIdle();
111   EXPECT_EQ(1U, issued_tokens_.size());
112   EXPECT_NE("", issued_tokens_[0]);
113   EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]);
114 }
115
116 TEST_F(GCMInvalidationBridgeTest, RequestTokenTwoConcurrentRequests) {
117   // First call should finish with REQUEST_CANCELLED error.
118   delegate_->RequestToken(
119       base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
120                  base::Unretained(this)));
121   // Second request should succeed.
122   delegate_->RequestToken(
123       base::Bind(&GCMInvalidationBridgeTest::RequestTokenFinished,
124                  base::Unretained(this)));
125   base::RunLoop run_loop;
126   run_loop.RunUntilIdle();
127
128   EXPECT_EQ(2U, issued_tokens_.size());
129
130   EXPECT_EQ("", issued_tokens_[0]);
131   EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED,
132             request_token_errors_[0].state());
133
134   EXPECT_NE("", issued_tokens_[1]);
135   EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]);
136 }
137
138 TEST_F(GCMInvalidationBridgeTest, Register) {
139   EXPECT_TRUE(registration_id_.empty());
140   delegate_->Register(base::Bind(&GCMInvalidationBridgeTest::RegisterFinished,
141                                  base::Unretained(this)));
142   base::RunLoop run_loop;
143   run_loop.RunUntilIdle();
144
145   EXPECT_FALSE(registration_id_.empty());
146 }
147
148 }  // namespace
149 }  // namespace invalidation