Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / invalidation / gcm_network_channel_delegate_impl_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_network_channel_delegate_impl.h"
7 #include "chrome/browser/services/gcm/fake_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_wrapper.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/test/test_browser_thread_bundle.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 class GCMNetworkChannelDelegateImplTest : public ::testing::Test {
22  protected:
23   GCMNetworkChannelDelegateImplTest() {}
24
25   virtual ~GCMNetworkChannelDelegateImplTest() {}
26
27   virtual void SetUp() OVERRIDE {
28     TestingProfile::Builder builder;
29     builder.AddTestingFactory(
30         ProfileOAuth2TokenServiceFactory::GetInstance(),
31         FakeProfileOAuth2TokenServiceWrapper::BuildAutoIssuingTokenService);
32     profile_ = builder.Build();
33
34     FakeProfileOAuth2TokenService* token_service =
35         (FakeProfileOAuth2TokenService*)
36         ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
37     token_service->IssueRefreshTokenForUser("", "refresh_token");
38
39     delegate_.reset(new GCMNetworkChannelDelegateImpl(profile_.get()));
40   }
41
42  public:
43   void RegisterFinished(const std::string& registration_id,
44                         gcm::GCMClient::Result result) {}
45
46   void RequestTokenFinished(const GoogleServiceAuthError& error,
47                             const std::string& token) {
48     issued_tokens_.push_back(token);
49     request_token_errors_.push_back(error);
50   }
51
52   content::TestBrowserThreadBundle thread_bundle_;
53   scoped_ptr<Profile> profile_;
54   FakeProfileOAuth2TokenService* token_service_;
55
56   std::vector<std::string> issued_tokens_;
57   std::vector<GoogleServiceAuthError> request_token_errors_;
58
59   scoped_ptr<GCMNetworkChannelDelegateImpl> delegate_;
60 };
61
62 TEST_F(GCMNetworkChannelDelegateImplTest, RequestToken) {
63   // Make sure that call to RequestToken reaches OAuth2TokenService and gets
64   // back to callback.
65   delegate_->RequestToken(
66       base::Bind(&GCMNetworkChannelDelegateImplTest::RequestTokenFinished,
67                  base::Unretained(this)));
68   base::RunLoop run_loop;
69   run_loop.RunUntilIdle();
70   EXPECT_EQ(1U, issued_tokens_.size());
71   EXPECT_NE("", issued_tokens_[0]);
72   EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[0]);
73 }
74
75 TEST_F(GCMNetworkChannelDelegateImplTest, RequestTokenTwoConcurrentRequests) {
76   // First call should finish with REQUEST_CANCELLED error.
77   delegate_->RequestToken(
78       base::Bind(&GCMNetworkChannelDelegateImplTest::RequestTokenFinished,
79                  base::Unretained(this)));
80   // Second request should succeed.
81   delegate_->RequestToken(
82       base::Bind(&GCMNetworkChannelDelegateImplTest::RequestTokenFinished,
83                  base::Unretained(this)));
84   base::RunLoop run_loop;
85   run_loop.RunUntilIdle();
86
87   EXPECT_EQ(2U, issued_tokens_.size());
88
89   EXPECT_EQ("", issued_tokens_[0]);
90   EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED,
91             request_token_errors_[0].state());
92
93   EXPECT_NE("", issued_tokens_[1]);
94   EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]);
95 }
96
97 }  // namespace
98 }  // namespace invalidation