| Index: google_apis/gaia/oauth2_token_service_proxy_unittest.cc
|
| diff --git a/google_apis/gaia/oauth2_token_service_proxy_unittest.cc b/google_apis/gaia/oauth2_token_service_proxy_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..93a16adb533a3752e54f413cb2d99e48afa983aa
|
| --- /dev/null
|
| +++ b/google_apis/gaia/oauth2_token_service_proxy_unittest.cc
|
| @@ -0,0 +1,171 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "google_apis/gaia/oauth2_token_service_proxy.h"
|
| +
|
| +#include <string>
|
| +#include <vector>
|
| +
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/run_loop.h"
|
| +#include "google_apis/gaia/fake_oauth2_token_service.h"
|
| +#include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace {
|
| +
|
| +const char kRequesterId[] = "unittest";
|
| +const char kAccountId[] = "validaccountid";
|
| +
|
| +} // namespace
|
| +
|
| +class MockOAuth2TokenService : public FakeOAuth2TokenService {
|
| + public:
|
| + MockOAuth2TokenService();
|
| + virtual ~MockOAuth2TokenService();
|
| +
|
| + // Set the error to return in response to a RequestToken call.
|
| + void SetRequestTokenError(const GoogleServiceAuthError& error) {
|
| + error_ = error;
|
| + }
|
| +
|
| + // Return the number of times InvalidateOAuth2Token was called.
|
| + int GetNumInvalidateCalls() const { return num_invalidate_calls_; }
|
| +
|
| + protected:
|
| + virtual void FetchOAuth2Token(RequestImpl* request,
|
| + const std::string& account_id,
|
| + net::URLRequestContextGetter* getter,
|
| + const std::string& client_id,
|
| + const std::string& client_secret,
|
| + const ScopeSet& scopes) OVERRIDE;
|
| +
|
| + virtual void InvalidateOAuth2Token(const std::string& account_id,
|
| + const std::string& client_id,
|
| + const ScopeSet& scopes,
|
| + const std::string& access_token) OVERRIDE;
|
| +
|
| + private:
|
| + GoogleServiceAuthError error_;
|
| + int num_invalidate_calls_;
|
| +};
|
| +
|
| +MockOAuth2TokenService::MockOAuth2TokenService()
|
| + : error_(GoogleServiceAuthError::NONE), num_invalidate_calls_(0) {
|
| +}
|
| +
|
| +MockOAuth2TokenService::~MockOAuth2TokenService() {
|
| +}
|
| +
|
| +void MockOAuth2TokenService::FetchOAuth2Token(
|
| + RequestImpl* request,
|
| + const std::string& account_id,
|
| + net::URLRequestContextGetter* getter,
|
| + const std::string& client_id,
|
| + const std::string& client_secret,
|
| + const ScopeSet& scopes) {
|
| + base::MessageLoop::current()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&OAuth2TokenService::RequestImpl::InformConsumer,
|
| + request->AsWeakPtr(),
|
| + error_,
|
| + account_id,
|
| + base::Time()));
|
| +}
|
| +
|
| +void MockOAuth2TokenService::InvalidateOAuth2Token(
|
| + const std::string& account_id,
|
| + const std::string& client_id,
|
| + const ScopeSet& scopes,
|
| + const std::string& access_token) {
|
| + ++num_invalidate_calls_;
|
| +}
|
| +
|
| +class OAuth2TokenServiceProxyTest : public testing::Test {
|
| + public:
|
| + virtual void SetUp() OVERRIDE;
|
| +
|
| + virtual void TearDown() OVERRIDE;
|
| +
|
| + void OnRequestTokenDone(const GoogleServiceAuthError& error,
|
| + const std::string& access_token,
|
| + const base::Time& expiration_time);
|
| +
|
| + struct CallbackArguments {
|
| + GoogleServiceAuthError error;
|
| + std::string access_token;
|
| + base::Time expiration_time;
|
| + };
|
| +
|
| + base::MessageLoop message_loop_;
|
| +
|
| + OAuth2TokenServiceProxy::RequestTokenCallback callback;
|
| + std::vector<CallbackArguments> callback_invocations;
|
| + OAuth2TokenService::ScopeSet scopes;
|
| + MockOAuth2TokenService token_service;
|
| + scoped_ptr<OAuth2TokenServiceProxy> proxy;
|
| +};
|
| +
|
| +void OAuth2TokenServiceProxyTest::SetUp() {
|
| + token_service.AddAccount(kAccountId);
|
| + callback = base::Bind(&OAuth2TokenServiceProxyTest::OnRequestTokenDone,
|
| + base::Unretained(this));
|
| + proxy.reset(new OAuth2TokenServiceProxy(base::MessageLoopProxy::current(),
|
| + &token_service));
|
| +}
|
| +
|
| +void OAuth2TokenServiceProxyTest::TearDown() {
|
| + // Ensure that any tasks posted by OAuth2TokenServiceProxy's destructor get a
|
| + // chance to run so we don't leak memory.
|
| + proxy.reset();
|
| + base::RunLoop().RunUntilIdle();
|
| +}
|
| +
|
| +void OAuth2TokenServiceProxyTest::OnRequestTokenDone(
|
| + const GoogleServiceAuthError& error,
|
| + const std::string& access_token,
|
| + const base::Time& expiration_time) {
|
| + CallbackArguments args = {error, access_token, expiration_time};
|
| + callback_invocations.push_back(args);
|
| +}
|
| +
|
| +// See that a call to RequestToken is proxied and that an success response is
|
| +// proxied back.
|
| +TEST_F(OAuth2TokenServiceProxyTest, RequestToken_Succeeds) {
|
| + proxy->RequestToken(kAccountId, scopes, callback, kRequesterId);
|
| + base::RunLoop().RunUntilIdle();
|
| + ASSERT_EQ(1U, callback_invocations.size());
|
| + EXPECT_EQ(GoogleServiceAuthError::NONE,
|
| + callback_invocations[0].error.state());
|
| +}
|
| +
|
| +// See that a call to RequestToken is proxied and that an error response is
|
| +// proxied back.
|
| +TEST_F(OAuth2TokenServiceProxyTest, RequestToken_Fails) {
|
| + token_service.SetRequestTokenError(
|
| + GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED));
|
| + proxy->RequestToken(kAccountId, scopes, callback, kRequesterId);
|
| + base::RunLoop().RunUntilIdle();
|
| + ASSERT_EQ(1U, callback_invocations.size());
|
| + EXPECT_EQ(GoogleServiceAuthError::CONNECTION_FAILED,
|
| + callback_invocations[0].error.state());
|
| +}
|
| +
|
| +// See that destroying the proxy before the request has completed does not
|
| +// cancel the request.
|
| +TEST_F(OAuth2TokenServiceProxyTest, RequestToken_DestroyProxy) {
|
| + proxy->RequestToken(kAccountId, scopes, callback, kRequesterId);
|
| + proxy.reset();
|
| + // Proxy has been destroyed, but its Core may live on in the token service
|
| + // task runner thread.
|
| + base::RunLoop().RunUntilIdle();
|
| + ASSERT_EQ(1U, callback_invocations.size());
|
| +}
|
| +
|
| +// See that calls to InvalidateToken are proxied.
|
| +TEST_F(OAuth2TokenServiceProxyTest, InvalidateToken) {
|
| + proxy->InvalidateToken(kAccountId, scopes, "sometoken");
|
| + base::RunLoop().RunUntilIdle();
|
| + ASSERT_EQ(1, token_service.GetNumInvalidateCalls());
|
| +}
|
|
|