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.
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"
18 namespace invalidation {
21 class GCMNetworkChannelDelegateImplTest : public ::testing::Test {
23 GCMNetworkChannelDelegateImplTest() {}
25 virtual ~GCMNetworkChannelDelegateImplTest() {}
27 virtual void SetUp() OVERRIDE {
28 TestingProfile::Builder builder;
29 builder.AddTestingFactory(
30 ProfileOAuth2TokenServiceFactory::GetInstance(),
31 FakeProfileOAuth2TokenServiceWrapper::BuildAutoIssuingTokenService);
32 profile_ = builder.Build();
34 FakeProfileOAuth2TokenService* token_service =
35 (FakeProfileOAuth2TokenService*)
36 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
37 token_service->IssueRefreshTokenForUser("", "refresh_token");
39 delegate_.reset(new GCMNetworkChannelDelegateImpl(profile_.get()));
43 void RegisterFinished(const std::string& registration_id,
44 gcm::GCMClient::Result result) {}
46 void RequestTokenFinished(const GoogleServiceAuthError& error,
47 const std::string& token) {
48 issued_tokens_.push_back(token);
49 request_token_errors_.push_back(error);
52 content::TestBrowserThreadBundle thread_bundle_;
53 scoped_ptr<Profile> profile_;
54 FakeProfileOAuth2TokenService* token_service_;
56 std::vector<std::string> issued_tokens_;
57 std::vector<GoogleServiceAuthError> request_token_errors_;
59 scoped_ptr<GCMNetworkChannelDelegateImpl> delegate_;
62 TEST_F(GCMNetworkChannelDelegateImplTest, RequestToken) {
63 // Make sure that call to RequestToken reaches OAuth2TokenService and gets
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]);
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();
87 EXPECT_EQ(2U, issued_tokens_.size());
89 EXPECT_EQ("", issued_tokens_[0]);
90 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED,
91 request_token_errors_[0].state());
93 EXPECT_NE("", issued_tokens_[1]);
94 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), request_token_errors_[1]);
98 } // namespace invalidation