OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include <algorithm> | 5 #include <algorithm> |
6 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 91 |
92 void SimulateMergeSessionFailure(GaiaAuthConsumer* consumer, | 92 void SimulateMergeSessionFailure(GaiaAuthConsumer* consumer, |
93 const GoogleServiceAuthError& error) { | 93 const GoogleServiceAuthError& error) { |
94 consumer->OnMergeSessionFailure(error); | 94 consumer->OnMergeSessionFailure(error); |
95 } | 95 } |
96 | 96 |
97 void SimulateLogoutSuccess(net::URLFetcherDelegate* consumer) { | 97 void SimulateLogoutSuccess(net::URLFetcherDelegate* consumer) { |
98 consumer->OnURLFetchComplete(NULL); | 98 consumer->OnURLFetchComplete(NULL); |
99 } | 99 } |
100 | 100 |
| 101 void SimulateGetCheckConnctionInfoSuccess( |
| 102 net::TestURLFetcher* fetcher, |
| 103 const std::string& data) { |
| 104 fetcher->set_status(net::URLRequestStatus()); |
| 105 fetcher->set_response_code(200); |
| 106 fetcher->SetResponseString(data); |
| 107 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 108 } |
| 109 |
| 110 void SimulateGetCheckConnctionInfoResult( |
| 111 net::URLFetcher* fetcher, |
| 112 const std::string& result) { |
| 113 net::TestURLFetcher* test_fetcher = |
| 114 static_cast<net::TestURLFetcher*>(fetcher); |
| 115 test_fetcher->set_status(net::URLRequestStatus()); |
| 116 test_fetcher->set_response_code(200); |
| 117 test_fetcher->SetResponseString(result); |
| 118 test_fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 119 } |
| 120 |
101 const GoogleServiceAuthError& no_error() { return no_error_; } | 121 const GoogleServiceAuthError& no_error() { return no_error_; } |
102 const GoogleServiceAuthError& error() { return error_; } | 122 const GoogleServiceAuthError& error() { return error_; } |
103 const GoogleServiceAuthError& canceled() { return canceled_; } | 123 const GoogleServiceAuthError& canceled() { return canceled_; } |
104 | 124 |
| 125 net::TestURLFetcherFactory* factory() { return &factory_; } |
| 126 |
105 private: | 127 private: |
106 base::MessageLoop message_loop_; | 128 base::MessageLoop message_loop_; |
107 net::TestURLFetcherFactory factory_; | 129 net::TestURLFetcherFactory factory_; |
108 FakeOAuth2TokenService token_service_; | 130 FakeOAuth2TokenService token_service_; |
109 GoogleServiceAuthError no_error_; | 131 GoogleServiceAuthError no_error_; |
110 GoogleServiceAuthError error_; | 132 GoogleServiceAuthError error_; |
111 GoogleServiceAuthError canceled_; | 133 GoogleServiceAuthError canceled_; |
112 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 134 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
113 }; | 135 }; |
114 | 136 |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 EXPECT_CALL(helper, StartLogOutUrlFetch()); | 320 EXPECT_CALL(helper, StartLogOutUrlFetch()); |
299 | 321 |
300 helper.LogIn("acc1@gmail.com"); | 322 helper.LogIn("acc1@gmail.com"); |
301 helper.LogOut("acc2@gmail.com", current_accounts1); | 323 helper.LogOut("acc2@gmail.com", current_accounts1); |
302 helper.LogOut("acc3@gmail.com", current_accounts2); | 324 helper.LogOut("acc3@gmail.com", current_accounts2); |
303 | 325 |
304 SimulateMergeSessionSuccess(&helper, "token1"); | 326 SimulateMergeSessionSuccess(&helper, "token1"); |
305 SimulateLogoutSuccess(&helper); | 327 SimulateLogoutSuccess(&helper); |
306 SimulateMergeSessionSuccess(&helper, "token1"); | 328 SimulateMergeSessionSuccess(&helper, "token1"); |
307 } | 329 } |
| 330 |
| 331 TEST_F(MergeSessionHelperTest, ExternalCcResultFetcher) { |
| 332 InstrumentedMergeSessionHelper helper(token_service(), request_context()); |
| 333 MergeSessionHelper::ExternalCcResultFetcher result_fetcher(&helper); |
| 334 result_fetcher.Start(); |
| 335 |
| 336 // Simulate a successful completion of GetCheckConnctionInfo. |
| 337 net::TestURLFetcher* fetcher = factory()->GetFetcherByID(0); |
| 338 ASSERT_TRUE(NULL != fetcher); |
| 339 SimulateGetCheckConnctionInfoSuccess(fetcher, |
| 340 "[{\"carryBackToken\": \"yt\", \"url\": \"http://www.yt.com\"}," |
| 341 " {\"carryBackToken\": \"bl\", \"url\": \"http://www.bl.com\"}]"); |
| 342 |
| 343 // Simulate responses for the two connection URLs. |
| 344 MergeSessionHelper::ExternalCcResultFetcher::URLToTokenAndFetcher fetchers = |
| 345 result_fetcher.get_fetcher_map_for_testing(); |
| 346 ASSERT_EQ(2u, fetchers.size()); |
| 347 ASSERT_EQ(1u, fetchers.count(GURL("http://www.yt.com"))); |
| 348 ASSERT_EQ(1u, fetchers.count(GURL("http://www.bl.com"))); |
| 349 |
| 350 ASSERT_EQ("bl:null,yt:null", result_fetcher.GetExternalCcResult()); |
| 351 SimulateGetCheckConnctionInfoResult( |
| 352 fetchers[GURL("http://www.yt.com")].second, "yt_result"); |
| 353 ASSERT_EQ("bl:null,yt:yt_result", result_fetcher.GetExternalCcResult()); |
| 354 SimulateGetCheckConnctionInfoResult( |
| 355 fetchers[GURL("http://www.bl.com")].second, "bl_result"); |
| 356 ASSERT_EQ("bl:bl_result,yt:yt_result", result_fetcher.GetExternalCcResult()); |
| 357 } |
| 358 |
| 359 TEST_F(MergeSessionHelperTest, ExternalCcResultFetcherTimeout) { |
| 360 InstrumentedMergeSessionHelper helper(token_service(), request_context()); |
| 361 MergeSessionHelper::ExternalCcResultFetcher result_fetcher(&helper); |
| 362 result_fetcher.Start(); |
| 363 |
| 364 // Simulate a successful completion of GetCheckConnctionInfo. |
| 365 net::TestURLFetcher* fetcher = factory()->GetFetcherByID(0); |
| 366 ASSERT_TRUE(NULL != fetcher); |
| 367 SimulateGetCheckConnctionInfoSuccess(fetcher, |
| 368 "[{\"carryBackToken\": \"yt\", \"url\": \"http://www.yt.com\"}," |
| 369 " {\"carryBackToken\": \"bl\", \"url\": \"http://www.bl.com\"}]"); |
| 370 |
| 371 MergeSessionHelper::ExternalCcResultFetcher::URLToTokenAndFetcher fetchers = |
| 372 result_fetcher.get_fetcher_map_for_testing(); |
| 373 ASSERT_EQ(2u, fetchers.size()); |
| 374 ASSERT_EQ(1u, fetchers.count(GURL("http://www.yt.com"))); |
| 375 ASSERT_EQ(1u, fetchers.count(GURL("http://www.bl.com"))); |
| 376 |
| 377 // Simulate response only for "yt". |
| 378 ASSERT_EQ("bl:null,yt:null", result_fetcher.GetExternalCcResult()); |
| 379 SimulateGetCheckConnctionInfoResult( |
| 380 fetchers[GURL("http://www.yt.com")].second, "yt_result"); |
| 381 ASSERT_EQ("bl:null,yt:yt_result", result_fetcher.GetExternalCcResult()); |
| 382 |
| 383 // Now timeout. |
| 384 result_fetcher.TimeoutForTests(); |
| 385 ASSERT_EQ("bl:null,yt:yt_result", result_fetcher.GetExternalCcResult()); |
| 386 fetchers = result_fetcher.get_fetcher_map_for_testing(); |
| 387 ASSERT_EQ(0u, fetchers.size()); |
| 388 } |
| 389 |
| 390 TEST_F(MergeSessionHelperTest, ExternalCcResultFetcherTruncate) { |
| 391 InstrumentedMergeSessionHelper helper(token_service(), request_context()); |
| 392 MergeSessionHelper::ExternalCcResultFetcher result_fetcher(&helper); |
| 393 result_fetcher.Start(); |
| 394 |
| 395 // Simulate a successful completion of GetCheckConnctionInfo. |
| 396 net::TestURLFetcher* fetcher = factory()->GetFetcherByID(0); |
| 397 ASSERT_TRUE(NULL != fetcher); |
| 398 SimulateGetCheckConnctionInfoSuccess(fetcher, |
| 399 "[{\"carryBackToken\": \"yt\", \"url\": \"http://www.yt.com\"}]"); |
| 400 |
| 401 MergeSessionHelper::ExternalCcResultFetcher::URLToTokenAndFetcher fetchers = |
| 402 result_fetcher.get_fetcher_map_for_testing(); |
| 403 ASSERT_EQ(1u, fetchers.size()); |
| 404 ASSERT_EQ(1u, fetchers.count(GURL("http://www.yt.com"))); |
| 405 |
| 406 // Simulate response for "yt" with a string that is too long. |
| 407 SimulateGetCheckConnctionInfoResult( |
| 408 fetchers[GURL("http://www.yt.com")].second, "1234567890123456trunc"); |
| 409 ASSERT_EQ("yt:1234567890123456", result_fetcher.GetExternalCcResult()); |
| 410 } |
OLD | NEW |