Fix experimental app list search box disappearing on profile switch.
[chromium-blink-merge.git] / google_apis / gaia / ubertoken_fetcher_unittest.cc
blobdc1edc32b76e3f68d7466cc6ca0f18e47d179c69
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 "google_apis/gaia/ubertoken_fetcher.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "google_apis/gaia/fake_oauth2_token_service.h"
11 #include "google_apis/gaia/gaia_constants.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace {
18 const char kTestAccountId[] = "test@gmail.com";
20 class MockUbertokenConsumer : public UbertokenConsumer {
21 public:
22 MockUbertokenConsumer()
23 : nb_correct_token_(0),
24 last_error_(GoogleServiceAuthError::AuthErrorNone()),
25 nb_error_(0) {
27 ~MockUbertokenConsumer() override {}
29 void OnUbertokenSuccess(const std::string& token) override {
30 last_token_ = token;
31 ++ nb_correct_token_;
34 void OnUbertokenFailure(const GoogleServiceAuthError& error) override {
35 last_error_ = error;
36 ++nb_error_;
39 std::string last_token_;
40 int nb_correct_token_;
41 GoogleServiceAuthError last_error_;
42 int nb_error_;
45 } // namespace
47 class UbertokenFetcherTest : public testing::Test {
48 public:
49 void SetUp() override {
50 request_context_getter_ = new net::TestURLRequestContextGetter(
51 base::MessageLoopProxy::current());
52 fetcher_.reset(new UbertokenFetcher(&token_service_,
53 &consumer_,
54 GaiaConstants::kChromeSource,
55 request_context_getter_.get()));
58 void TearDown() override { fetcher_.reset(); }
60 protected:
61 base::MessageLoop message_loop_;
62 net::TestURLFetcherFactory factory_;
63 FakeOAuth2TokenService token_service_;
64 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
65 MockUbertokenConsumer consumer_;
66 scoped_ptr<UbertokenFetcher> fetcher_;
69 TEST_F(UbertokenFetcherTest, Basic) {
72 TEST_F(UbertokenFetcherTest, Success) {
73 fetcher_->StartFetchingToken(kTestAccountId);
74 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
75 fetcher_->OnUberAuthTokenSuccess("uberToken");
77 EXPECT_EQ(0, consumer_.nb_error_);
78 EXPECT_EQ(1, consumer_.nb_correct_token_);
79 EXPECT_EQ("uberToken", consumer_.last_token_);
82 TEST_F(UbertokenFetcherTest, NoRefreshToken) {
83 fetcher_->StartFetchingToken(kTestAccountId);
84 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
85 fetcher_->OnGetTokenFailure(NULL, error);
87 EXPECT_EQ(1, consumer_.nb_error_);
88 EXPECT_EQ(0, consumer_.nb_correct_token_);
91 TEST_F(UbertokenFetcherTest, FailureToGetAccessToken) {
92 fetcher_->StartFetchingToken(kTestAccountId);
93 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
94 fetcher_->OnGetTokenFailure(NULL, error);
96 EXPECT_EQ(1, consumer_.nb_error_);
97 EXPECT_EQ(0, consumer_.nb_correct_token_);
98 EXPECT_EQ("", consumer_.last_token_);
101 TEST_F(UbertokenFetcherTest, TransientFailureEventualFailure) {
102 fetcher_->StartFetchingToken(kTestAccountId);
103 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED);
104 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
106 for (int i = 0; i < UbertokenFetcher::kMaxRetries; ++i) {
107 fetcher_->OnUberAuthTokenFailure(error);
108 EXPECT_EQ(0, consumer_.nb_error_);
109 EXPECT_EQ(0, consumer_.nb_correct_token_);
110 EXPECT_EQ("", consumer_.last_token_);
113 fetcher_->OnUberAuthTokenFailure(error);
114 EXPECT_EQ(1, consumer_.nb_error_);
115 EXPECT_EQ(0, consumer_.nb_correct_token_);
116 EXPECT_EQ("", consumer_.last_token_);
119 TEST_F(UbertokenFetcherTest, TransientFailureEventualSuccess) {
120 fetcher_->StartFetchingToken(kTestAccountId);
121 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED);
122 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
124 for (int i = 0; i < UbertokenFetcher::kMaxRetries; ++i) {
125 fetcher_->OnUberAuthTokenFailure(error);
126 EXPECT_EQ(0, consumer_.nb_error_);
127 EXPECT_EQ(0, consumer_.nb_correct_token_);
128 EXPECT_EQ("", consumer_.last_token_);
131 fetcher_->OnUberAuthTokenSuccess("uberToken");
132 EXPECT_EQ(0, consumer_.nb_error_);
133 EXPECT_EQ(1, consumer_.nb_correct_token_);
134 EXPECT_EQ("uberToken", consumer_.last_token_);
137 TEST_F(UbertokenFetcherTest, PermanentFailureEventualFailure) {
138 fetcher_->StartFetchingToken(kTestAccountId);
139 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
141 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
142 fetcher_->OnUberAuthTokenFailure(error);
143 EXPECT_EQ(0, consumer_.nb_error_);
144 EXPECT_EQ(0, consumer_.nb_correct_token_);
145 EXPECT_EQ("", consumer_.last_token_);
147 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
148 fetcher_->OnUberAuthTokenFailure(error);
149 EXPECT_EQ(1, consumer_.nb_error_);
150 EXPECT_EQ(0, consumer_.nb_correct_token_);
151 EXPECT_EQ("", consumer_.last_token_);
154 TEST_F(UbertokenFetcherTest, PermanentFailureEventualSuccess) {
155 fetcher_->StartFetchingToken(kTestAccountId);
156 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP);
157 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
159 fetcher_->OnUberAuthTokenFailure(error);
160 EXPECT_EQ(0, consumer_.nb_error_);
161 EXPECT_EQ(0, consumer_.nb_correct_token_);
162 EXPECT_EQ("", consumer_.last_token_);
164 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time());
165 fetcher_->OnUberAuthTokenSuccess("uberToken");
166 EXPECT_EQ(0, consumer_.nb_error_);
167 EXPECT_EQ(1, consumer_.nb_correct_token_);
168 EXPECT_EQ("uberToken", consumer_.last_token_);