OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/json/json_reader.h" | 5 #include "base/json/json_reader.h" |
6 #include "base/json/json_writer.h" | 6 #include "base/json/json_writer.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
16 #include "components/autofill/content/browser/autocheckout_steps.h" | |
17 #include "components/autofill/content/browser/wallet/full_wallet.h" | 16 #include "components/autofill/content/browser/wallet/full_wallet.h" |
18 #include "components/autofill/content/browser/wallet/instrument.h" | 17 #include "components/autofill/content/browser/wallet/instrument.h" |
19 #include "components/autofill/content/browser/wallet/wallet_client.h" | 18 #include "components/autofill/content/browser/wallet/wallet_client.h" |
20 #include "components/autofill/content/browser/wallet/wallet_client_delegate.h" | 19 #include "components/autofill/content/browser/wallet/wallet_client_delegate.h" |
21 #include "components/autofill/content/browser/wallet/wallet_items.h" | 20 #include "components/autofill/content/browser/wallet/wallet_items.h" |
22 #include "components/autofill/content/browser/wallet/wallet_test_util.h" | 21 #include "components/autofill/content/browser/wallet/wallet_test_util.h" |
23 #include "components/autofill/core/browser/autofill_metrics.h" | 22 #include "components/autofill/core/browser/autofill_metrics.h" |
24 #include "components/autofill/core/common/autocheckout_status.h" | |
25 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
26 #include "net/base/escape.h" | 24 #include "net/base/escape.h" |
27 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
28 #include "net/http/http_request_headers.h" | 26 #include "net/http/http_request_headers.h" |
29 #include "net/http/http_status_code.h" | 27 #include "net/http/http_status_code.h" |
30 #include "net/url_request/test_url_fetcher_factory.h" | 28 #include "net/url_request/test_url_fetcher_factory.h" |
31 #include "net/url_request/url_fetcher_delegate.h" | 29 #include "net/url_request/url_fetcher_delegate.h" |
32 #include "net/url_request/url_request_status.h" | 30 #include "net/url_request/url_request_status.h" |
33 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 "\"administrative_area_name\":\"save_admin_area_name\"," | 460 "\"administrative_area_name\":\"save_admin_area_name\"," |
463 "\"country_name_code\":\"US\"," | 461 "\"country_name_code\":\"US\"," |
464 "\"locality_name\":\"save_locality_name\"," | 462 "\"locality_name\":\"save_locality_name\"," |
465 "\"postal_code_number\":\"save_postal_code_number\"," | 463 "\"postal_code_number\":\"save_postal_code_number\"," |
466 "\"recipient_name\":\"save_recipient_name\"" | 464 "\"recipient_name\":\"save_recipient_name\"" |
467 "}" | 465 "}" |
468 "}," | 466 "}," |
469 "\"use_minimal_addresses\":false" | 467 "\"use_minimal_addresses\":false" |
470 "}"; | 468 "}"; |
471 | 469 |
472 const char kSendAutocheckoutStatusOfSuccessValidRequest[] = | |
473 "{" | |
474 "\"google_transaction_id\":\"google_transaction_id\"," | |
475 "\"merchant_domain\":\"https://example.com/\"," | |
476 "\"success\":true" | |
477 "}"; | |
478 | |
479 const char kSendAutocheckoutStatusWithStatisticsValidRequest[] = | |
480 "{" | |
481 "\"google_transaction_id\":\"google_transaction_id\"," | |
482 "\"merchant_domain\":\"https://example.com/\"," | |
483 "\"steps\":[{\"step_description\":\"1_AUTOCHECKOUT_STEP_SHIPPING\"" | |
484 ",\"time_taken\":100}]," | |
485 "\"success\":true" | |
486 "}"; | |
487 | |
488 const char kSendAutocheckoutStatusOfFailureValidRequest[] = | |
489 "{" | |
490 "\"google_transaction_id\":\"google_transaction_id\"," | |
491 "\"merchant_domain\":\"https://example.com/\"," | |
492 "\"reason\":\"CANNOT_PROCEED\"," | |
493 "\"success\":false" | |
494 "}"; | |
495 | |
496 const char kUpdateAddressValidRequest[] = | 470 const char kUpdateAddressValidRequest[] = |
497 "{" | 471 "{" |
498 "\"merchant_domain\":\"https://example.com/\"," | 472 "\"merchant_domain\":\"https://example.com/\"," |
499 "\"phone_number_required\":true," | 473 "\"phone_number_required\":true," |
500 "\"risk_params\":\"risky business\"," | 474 "\"risk_params\":\"risky business\"," |
501 "\"shipping_address\":" | 475 "\"shipping_address\":" |
502 "{" | 476 "{" |
503 "\"id\":\"shipping_address_id\"," | 477 "\"id\":\"shipping_address_id\"," |
504 "\"phone_number\":\"ship_phone_number\"," | 478 "\"phone_number\":\"ship_phone_number\"," |
505 "\"postal_address\":" | 479 "\"postal_address\":" |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
818 " \"error_detail\":\"error_detail\"," | 792 " \"error_detail\":\"error_detail\"," |
819 " \"message_for_user\":" | 793 " \"message_for_user\":" |
820 " {" | 794 " {" |
821 " \"text\":\"text\"," | 795 " \"text\":\"text\"," |
822 " \"subtext\":\"subtext\"," | 796 " \"subtext\":\"subtext\"," |
823 " \"details\":\"details\"" | 797 " \"details\":\"details\"" |
824 " }" | 798 " }" |
825 " }" | 799 " }" |
826 "}"; | 800 "}"; |
827 EXPECT_CALL(delegate_, OnWalletError(expected_error_type)).Times(1); | 801 EXPECT_CALL(delegate_, OnWalletError(expected_error_type)).Times(1); |
828 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | 802 delegate_.ExpectLogWalletApiCallDuration( |
| 803 AutofillMetrics::GET_WALLET_ITEMS, 1); |
829 delegate_.ExpectBaselineMetrics(); | 804 delegate_.ExpectBaselineMetrics(); |
830 delegate_.ExpectWalletErrorMetric(expected_autofill_metric); | 805 delegate_.ExpectWalletErrorMetric(expected_autofill_metric); |
831 | 806 |
832 std::vector<AutocheckoutStatistic> statistics; | 807 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
833 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, | |
834 GURL(kMerchantUrl), | |
835 statistics, | |
836 "google_transaction_id"); | |
837 std::string buyer_error; | 808 std::string buyer_error; |
838 if (!buyer_error_type_string.empty()) { | 809 if (!buyer_error_type_string.empty()) { |
839 buyer_error = base::StringPrintf("\"buyer_error_type\":\"%s\",", | 810 buyer_error = base::StringPrintf("\"buyer_error_type\":\"%s\",", |
840 buyer_error_type_string.c_str()); | 811 buyer_error_type_string.c_str()); |
841 } | 812 } |
842 std::string response = base::StringPrintf(kResponseTemplate, | 813 std::string response = base::StringPrintf(kResponseTemplate, |
843 error_type_string.c_str(), | 814 error_type_string.c_str(), |
844 buyer_error.c_str()); | 815 buyer_error.c_str()); |
845 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, | 816 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, |
846 kSendAutocheckoutStatusOfSuccessValidRequest, | 817 kGetWalletItemsValidRequest, |
847 response); | 818 response); |
848 } | 819 } |
849 | 820 |
850 protected: | 821 protected: |
851 content::TestBrowserThreadBundle thread_bundle_; | 822 content::TestBrowserThreadBundle thread_bundle_; |
852 scoped_ptr<WalletClient> wallet_client_; | 823 scoped_ptr<WalletClient> wallet_client_; |
853 TestingProfile browser_context_; | 824 TestingProfile browser_context_; |
854 MockWalletClientDelegate delegate_; | 825 MockWalletClientDelegate delegate_; |
855 | 826 |
856 private: | 827 private: |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
962 TestWalletErrorCode(test_cases[i].error_type_string, | 933 TestWalletErrorCode(test_cases[i].error_type_string, |
963 test_cases[i].buyer_error_type_string, | 934 test_cases[i].buyer_error_type_string, |
964 test_cases[i].expected_error_type, | 935 test_cases[i].expected_error_type, |
965 test_cases[i].expected_autofill_metric); | 936 test_cases[i].expected_autofill_metric); |
966 } | 937 } |
967 } | 938 } |
968 | 939 |
969 TEST_F(WalletClientTest, WalletErrorResponseMissing) { | 940 TEST_F(WalletClientTest, WalletErrorResponseMissing) { |
970 EXPECT_CALL(delegate_, OnWalletError( | 941 EXPECT_CALL(delegate_, OnWalletError( |
971 WalletClient::UNKNOWN_ERROR)).Times(1); | 942 WalletClient::UNKNOWN_ERROR)).Times(1); |
972 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | 943 delegate_.ExpectLogWalletApiCallDuration( |
| 944 AutofillMetrics::GET_WALLET_ITEMS, 1); |
973 delegate_.ExpectBaselineMetrics(); | 945 delegate_.ExpectBaselineMetrics(); |
974 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_UNKNOWN_ERROR); | 946 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_UNKNOWN_ERROR); |
975 | 947 |
976 std::vector<AutocheckoutStatistic> statistics; | 948 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
977 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, | |
978 GURL(kMerchantUrl), | |
979 statistics, | |
980 "google_transaction_id"); | |
981 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, | 949 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, |
982 kSendAutocheckoutStatusOfSuccessValidRequest, | 950 kGetWalletItemsValidRequest, |
983 kErrorTypeMissingInResponse); | 951 kErrorTypeMissingInResponse); |
984 } | 952 } |
985 | 953 |
986 TEST_F(WalletClientTest, NetworkFailureOnExpectedVoidResponse) { | |
987 EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1); | |
988 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | |
989 delegate_.ExpectBaselineMetrics(); | |
990 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR); | |
991 | |
992 std::vector<AutocheckoutStatistic> statistics; | |
993 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, | |
994 GURL(kMerchantUrl), | |
995 statistics, | |
996 "google_transaction_id"); | |
997 VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED, | |
998 kSendAutocheckoutStatusOfSuccessValidRequest, | |
999 std::string()); | |
1000 } | |
1001 | |
1002 TEST_F(WalletClientTest, NetworkFailureOnExpectedResponse) { | 954 TEST_F(WalletClientTest, NetworkFailureOnExpectedResponse) { |
1003 EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1); | 955 EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1); |
1004 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, | 956 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, |
1005 1); | 957 1); |
1006 delegate_.ExpectBaselineMetrics(); | 958 delegate_.ExpectBaselineMetrics(); |
1007 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR); | 959 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR); |
1008 | 960 |
1009 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); | 961 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
1010 VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED, | 962 VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED, |
1011 kGetWalletItemsValidRequest, | 963 kGetWalletItemsValidRequest, |
1012 std::string()); | 964 std::string()); |
1013 } | 965 } |
1014 | 966 |
1015 TEST_F(WalletClientTest, RequestError) { | 967 TEST_F(WalletClientTest, RequestError) { |
1016 EXPECT_CALL(delegate_, OnWalletError(WalletClient::BAD_REQUEST)).Times(1); | 968 EXPECT_CALL(delegate_, OnWalletError(WalletClient::BAD_REQUEST)).Times(1); |
1017 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | 969 delegate_.ExpectLogWalletApiCallDuration( |
| 970 AutofillMetrics::GET_WALLET_ITEMS, 1); |
1018 delegate_.ExpectBaselineMetrics(); | 971 delegate_.ExpectBaselineMetrics(); |
1019 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_BAD_REQUEST); | 972 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_BAD_REQUEST); |
1020 | 973 |
1021 std::vector<AutocheckoutStatistic> statistics; | 974 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
1022 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, | |
1023 GURL(kMerchantUrl), | |
1024 statistics, | |
1025 "google_transaction_id"); | |
1026 VerifyAndFinishRequest(net::HTTP_BAD_REQUEST, | 975 VerifyAndFinishRequest(net::HTTP_BAD_REQUEST, |
1027 kSendAutocheckoutStatusOfSuccessValidRequest, | 976 kGetWalletItemsValidRequest, |
1028 std::string()); | 977 std::string()); |
1029 } | 978 } |
1030 | 979 |
1031 TEST_F(WalletClientTest, GetFullWalletSuccess) { | 980 TEST_F(WalletClientTest, GetFullWalletSuccess) { |
1032 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1); | 981 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1); |
1033 delegate_.ExpectBaselineMetrics(); | 982 delegate_.ExpectBaselineMetrics(); |
1034 | 983 |
1035 WalletClient::FullWalletRequest full_wallet_request( | 984 WalletClient::FullWalletRequest full_wallet_request( |
1036 "instrument_id", | 985 "instrument_id", |
1037 "shipping_address_id", | 986 "shipping_address_id", |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1655 | 1604 |
1656 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(), | 1605 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(), |
1657 scoped_ptr<Address>(), | 1606 scoped_ptr<Address>(), |
1658 GURL(kMerchantUrl)); | 1607 GURL(kMerchantUrl)); |
1659 | 1608 |
1660 VerifyAndFinishRequest(net::HTTP_OK, | 1609 VerifyAndFinishRequest(net::HTTP_OK, |
1661 kUpdateInstrumentAddressValidRequest, | 1610 kUpdateInstrumentAddressValidRequest, |
1662 kUpdateMalformedResponse); | 1611 kUpdateMalformedResponse); |
1663 } | 1612 } |
1664 | 1613 |
1665 TEST_F(WalletClientTest, SendAutocheckoutOfStatusSuccess) { | |
1666 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | |
1667 delegate_.ExpectBaselineMetrics(); | |
1668 | |
1669 AutocheckoutStatistic statistic; | |
1670 statistic.page_number = 1; | |
1671 statistic.steps.push_back(AUTOCHECKOUT_STEP_SHIPPING); | |
1672 statistic.time_taken = base::TimeDelta::FromMilliseconds(100); | |
1673 std::vector<AutocheckoutStatistic> statistics; | |
1674 statistics.push_back(statistic); | |
1675 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, | |
1676 GURL(kMerchantUrl), | |
1677 statistics, | |
1678 "google_transaction_id"); | |
1679 VerifyAndFinishRequest(net::HTTP_OK, | |
1680 kSendAutocheckoutStatusWithStatisticsValidRequest, | |
1681 ")]}"); // Invalid JSON. Should be ignored. | |
1682 } | |
1683 | |
1684 TEST_F(WalletClientTest, SendAutocheckoutStatusOfFailure) { | |
1685 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | |
1686 delegate_.ExpectBaselineMetrics(); | |
1687 | |
1688 std::vector<AutocheckoutStatistic> statistics; | |
1689 wallet_client_->SendAutocheckoutStatus(autofill::CANNOT_PROCEED, | |
1690 GURL(kMerchantUrl), | |
1691 statistics, | |
1692 "google_transaction_id"); | |
1693 VerifyAndFinishRequest(net::HTTP_OK, | |
1694 kSendAutocheckoutStatusOfFailureValidRequest, | |
1695 ")]}"); // Invalid JSON. Should be ignored. | |
1696 } | |
1697 | |
1698 TEST_F(WalletClientTest, HasRequestInProgress) { | 1614 TEST_F(WalletClientTest, HasRequestInProgress) { |
1699 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); | 1615 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); |
1700 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, | 1616 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, |
1701 1); | 1617 1); |
1702 delegate_.ExpectBaselineMetrics(); | 1618 delegate_.ExpectBaselineMetrics(); |
1703 | 1619 |
1704 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); | 1620 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
1705 EXPECT_TRUE(wallet_client_->HasRequestInProgress()); | 1621 EXPECT_TRUE(wallet_client_->HasRequestInProgress()); |
1706 | 1622 |
1707 VerifyAndFinishRequest(net::HTTP_OK, | 1623 VerifyAndFinishRequest(net::HTTP_OK, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1755 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); | 1671 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
1756 EXPECT_EQ(2U, wallet_client_->pending_requests_.size()); | 1672 EXPECT_EQ(2U, wallet_client_->pending_requests_.size()); |
1757 | 1673 |
1758 wallet_client_->CancelRequests(); | 1674 wallet_client_->CancelRequests(); |
1759 EXPECT_EQ(0U, wallet_client_->pending_requests_.size()); | 1675 EXPECT_EQ(0U, wallet_client_->pending_requests_.size()); |
1760 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); | 1676 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); |
1761 } | 1677 } |
1762 | 1678 |
1763 } // namespace wallet | 1679 } // namespace wallet |
1764 } // namespace autofill | 1680 } // namespace autofill |
OLD | NEW |