| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // A complete set of unit tests for GaiaOAuthClient. | 5 // A complete set of unit tests for GaiaOAuthClient. |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 | 82 |
| 83 class MockOAuthFetcherFactory : public net::URLFetcherFactory, | 83 class MockOAuthFetcherFactory : public net::URLFetcherFactory, |
| 84 public net::ScopedURLFetcherFactory { | 84 public net::ScopedURLFetcherFactory { |
| 85 public: | 85 public: |
| 86 MockOAuthFetcherFactory() | 86 MockOAuthFetcherFactory() |
| 87 : net::ScopedURLFetcherFactory(this), | 87 : net::ScopedURLFetcherFactory(this), |
| 88 response_code_(net::HTTP_OK), | 88 response_code_(net::HTTP_OK), |
| 89 complete_immediately_(true) { | 89 complete_immediately_(true) { |
| 90 } | 90 } |
| 91 ~MockOAuthFetcherFactory() override {} | 91 ~MockOAuthFetcherFactory() override {} |
| 92 scoped_ptr<net::URLFetcher> CreateURLFetcher( | 92 std::unique_ptr<net::URLFetcher> CreateURLFetcher( |
| 93 int id, | 93 int id, |
| 94 const GURL& url, | 94 const GURL& url, |
| 95 net::URLFetcher::RequestType request_type, | 95 net::URLFetcher::RequestType request_type, |
| 96 net::URLFetcherDelegate* d) override { | 96 net::URLFetcherDelegate* d) override { |
| 97 url_fetcher_ = new MockOAuthFetcher( | 97 url_fetcher_ = new MockOAuthFetcher( |
| 98 response_code_, | 98 response_code_, |
| 99 max_failure_count_, | 99 max_failure_count_, |
| 100 complete_immediately_, | 100 complete_immediately_, |
| 101 url, | 101 url, |
| 102 results_, | 102 results_, |
| 103 request_type, | 103 request_type, |
| 104 d); | 104 d); |
| 105 return scoped_ptr<net::URLFetcher>(url_fetcher_); | 105 return std::unique_ptr<net::URLFetcher>(url_fetcher_); |
| 106 } | 106 } |
| 107 void set_response_code(int response_code) { | 107 void set_response_code(int response_code) { |
| 108 response_code_ = response_code; | 108 response_code_ = response_code; |
| 109 } | 109 } |
| 110 void set_max_failure_count(int count) { | 110 void set_max_failure_count(int count) { |
| 111 max_failure_count_ = count; | 111 max_failure_count_ = count; |
| 112 } | 112 } |
| 113 void set_results(const std::string& results) { | 113 void set_results(const std::string& results) { |
| 114 results_ = results; | 114 results_ = results; |
| 115 } | 115 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 MOCK_METHOD0(OnOAuthError, void()); | 214 MOCK_METHOD0(OnOAuthError, void()); |
| 215 MOCK_METHOD1(OnNetworkError, void(int response_code)); | 215 MOCK_METHOD1(OnNetworkError, void(int response_code)); |
| 216 | 216 |
| 217 // gMock doesn't like methods that take or return scoped_ptr. A | 217 // gMock doesn't like methods that take or return scoped_ptr. A |
| 218 // work-around is to create a mock method that takes a raw ptr, and | 218 // work-around is to create a mock method that takes a raw ptr, and |
| 219 // override the problematic method to call through to it. | 219 // override the problematic method to call through to it. |
| 220 // https://groups.google.com/a/chromium.org/d/msg/chromium-dev/01sDxsJ1OYw/I_S
0xCBRF2oJ | 220 // https://groups.google.com/a/chromium.org/d/msg/chromium-dev/01sDxsJ1OYw/I_S
0xCBRF2oJ |
| 221 MOCK_METHOD1(OnGetUserInfoResponsePtr, | 221 MOCK_METHOD1(OnGetUserInfoResponsePtr, |
| 222 void(const base::DictionaryValue* user_info)); | 222 void(const base::DictionaryValue* user_info)); |
| 223 void OnGetUserInfoResponse( | 223 void OnGetUserInfoResponse( |
| 224 scoped_ptr<base::DictionaryValue> user_info) override { | 224 std::unique_ptr<base::DictionaryValue> user_info) override { |
| 225 user_info_.reset(user_info.release()); | 225 user_info_.reset(user_info.release()); |
| 226 OnGetUserInfoResponsePtr(user_info_.get()); | 226 OnGetUserInfoResponsePtr(user_info_.get()); |
| 227 } | 227 } |
| 228 MOCK_METHOD1(OnGetTokenInfoResponsePtr, | 228 MOCK_METHOD1(OnGetTokenInfoResponsePtr, |
| 229 void(const base::DictionaryValue* token_info)); | 229 void(const base::DictionaryValue* token_info)); |
| 230 void OnGetTokenInfoResponse( | 230 void OnGetTokenInfoResponse( |
| 231 scoped_ptr<base::DictionaryValue> token_info) override { | 231 std::unique_ptr<base::DictionaryValue> token_info) override { |
| 232 token_info_.reset(token_info.release()); | 232 token_info_.reset(token_info.release()); |
| 233 OnGetTokenInfoResponsePtr(token_info_.get()); | 233 OnGetTokenInfoResponsePtr(token_info_.get()); |
| 234 } | 234 } |
| 235 | 235 |
| 236 private: | 236 private: |
| 237 scoped_ptr<base::DictionaryValue> user_info_; | 237 std::unique_ptr<base::DictionaryValue> user_info_; |
| 238 scoped_ptr<base::DictionaryValue> token_info_; | 238 std::unique_ptr<base::DictionaryValue> token_info_; |
| 239 DISALLOW_COPY_AND_ASSIGN(MockGaiaOAuthClientDelegate); | 239 DISALLOW_COPY_AND_ASSIGN(MockGaiaOAuthClientDelegate); |
| 240 }; | 240 }; |
| 241 | 241 |
| 242 TEST_F(GaiaOAuthClientTest, NetworkFailure) { | 242 TEST_F(GaiaOAuthClientTest, NetworkFailure) { |
| 243 int response_code = net::HTTP_INTERNAL_SERVER_ERROR; | 243 int response_code = net::HTTP_INTERNAL_SERVER_ERROR; |
| 244 | 244 |
| 245 MockGaiaOAuthClientDelegate delegate; | 245 MockGaiaOAuthClientDelegate delegate; |
| 246 EXPECT_CALL(delegate, OnNetworkError(response_code)) | 246 EXPECT_CALL(delegate, OnNetworkError(response_code)) |
| 247 .Times(1); | 247 .Times(1); |
| 248 | 248 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 MockGaiaOAuthClientDelegate delegate; | 361 MockGaiaOAuthClientDelegate delegate; |
| 362 EXPECT_CALL(delegate, OnGetUserInfoResponsePtr(_)) | 362 EXPECT_CALL(delegate, OnGetUserInfoResponsePtr(_)) |
| 363 .WillOnce(SaveArg<0>(&captured_result)); | 363 .WillOnce(SaveArg<0>(&captured_result)); |
| 364 | 364 |
| 365 MockOAuthFetcherFactory factory; | 365 MockOAuthFetcherFactory factory; |
| 366 factory.set_results(kDummyFullUserInfoResult); | 366 factory.set_results(kDummyFullUserInfoResult); |
| 367 | 367 |
| 368 GaiaOAuthClient auth(GetRequestContext()); | 368 GaiaOAuthClient auth(GetRequestContext()); |
| 369 auth.GetUserInfo("access_token", 1, &delegate); | 369 auth.GetUserInfo("access_token", 1, &delegate); |
| 370 | 370 |
| 371 scoped_ptr<base::Value> value = | 371 std::unique_ptr<base::Value> value = |
| 372 base::JSONReader::Read(kDummyFullUserInfoResult); | 372 base::JSONReader::Read(kDummyFullUserInfoResult); |
| 373 DCHECK(value); | 373 DCHECK(value); |
| 374 ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); | 374 ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); |
| 375 base::DictionaryValue* expected_result; | 375 base::DictionaryValue* expected_result; |
| 376 value->GetAsDictionary(&expected_result); | 376 value->GetAsDictionary(&expected_result); |
| 377 | 377 |
| 378 ASSERT_TRUE(expected_result->Equals(captured_result)); | 378 ASSERT_TRUE(expected_result->Equals(captured_result)); |
| 379 } | 379 } |
| 380 | 380 |
| 381 TEST_F(GaiaOAuthClientTest, GetTokenInfo) { | 381 TEST_F(GaiaOAuthClientTest, GetTokenInfo) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 408 | 408 |
| 409 GaiaOAuthClient auth(GetRequestContext()); | 409 GaiaOAuthClient auth(GetRequestContext()); |
| 410 auth.GetTokenHandleInfo("some_handle", 1, &delegate); | 410 auth.GetTokenHandleInfo("some_handle", 1, &delegate); |
| 411 | 411 |
| 412 std::string audience; | 412 std::string audience; |
| 413 ASSERT_TRUE(captured_result->GetString("audience", &audience)); | 413 ASSERT_TRUE(captured_result->GetString("audience", &audience)); |
| 414 ASSERT_EQ("1234567890.apps.googleusercontent.com", audience); | 414 ASSERT_EQ("1234567890.apps.googleusercontent.com", audience); |
| 415 } | 415 } |
| 416 | 416 |
| 417 } // namespace gaia | 417 } // namespace gaia |
| OLD | NEW |