| 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 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 "\"administrative_area_name\":\"save_admin_area_name\"," | 478 "\"administrative_area_name\":\"save_admin_area_name\"," |
| 481 "\"country_name_code\":\"US\"," | 479 "\"country_name_code\":\"US\"," |
| 482 "\"locality_name\":\"save_locality_name\"," | 480 "\"locality_name\":\"save_locality_name\"," |
| 483 "\"postal_code_number\":\"save_postal_code_number\"," | 481 "\"postal_code_number\":\"save_postal_code_number\"," |
| 484 "\"recipient_name\":\"save_recipient_name\"" | 482 "\"recipient_name\":\"save_recipient_name\"" |
| 485 "}" | 483 "}" |
| 486 "}," | 484 "}," |
| 487 "\"use_minimal_addresses\":false" | 485 "\"use_minimal_addresses\":false" |
| 488 "}"; | 486 "}"; |
| 489 | 487 |
| 490 const char kSendAutocheckoutStatusOfSuccessValidRequest[] = | |
| 491 "{" | |
| 492 "\"google_transaction_id\":\"google_transaction_id\"," | |
| 493 "\"merchant_domain\":\"https://example.com/\"," | |
| 494 "\"success\":true" | |
| 495 "}"; | |
| 496 | |
| 497 const char kSendAutocheckoutStatusWithStatisticsValidRequest[] = | |
| 498 "{" | |
| 499 "\"google_transaction_id\":\"google_transaction_id\"," | |
| 500 "\"merchant_domain\":\"https://example.com/\"," | |
| 501 "\"steps\":[{\"step_description\":\"1_AUTOCHECKOUT_STEP_SHIPPING\"" | |
| 502 ",\"time_taken\":100}]," | |
| 503 "\"success\":true" | |
| 504 "}"; | |
| 505 | |
| 506 const char kSendAutocheckoutStatusOfFailureValidRequest[] = | |
| 507 "{" | |
| 508 "\"google_transaction_id\":\"google_transaction_id\"," | |
| 509 "\"merchant_domain\":\"https://example.com/\"," | |
| 510 "\"reason\":\"CANNOT_PROCEED\"," | |
| 511 "\"success\":false" | |
| 512 "}"; | |
| 513 | |
| 514 const char kUpdateAddressValidRequest[] = | 488 const char kUpdateAddressValidRequest[] = |
| 515 "{" | 489 "{" |
| 516 "\"merchant_domain\":\"https://example.com/\"," | 490 "\"merchant_domain\":\"https://example.com/\"," |
| 517 "\"phone_number_required\":true," | 491 "\"phone_number_required\":true," |
| 518 "\"risk_params\":\"risky business\"," | 492 "\"risk_params\":\"risky business\"," |
| 519 "\"shipping_address\":" | 493 "\"shipping_address\":" |
| 520 "{" | 494 "{" |
| 521 "\"id\":\"shipping_address_id\"," | 495 "\"id\":\"shipping_address_id\"," |
| 522 "\"phone_number\":\"ship_phone_number\"," | 496 "\"phone_number\":\"ship_phone_number\"," |
| 523 "\"postal_address\":" | 497 "\"postal_address\":" |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 " \"error_detail\":\"error_detail\"," | 810 " \"error_detail\":\"error_detail\"," |
| 837 " \"message_for_user\":" | 811 " \"message_for_user\":" |
| 838 " {" | 812 " {" |
| 839 " \"text\":\"text\"," | 813 " \"text\":\"text\"," |
| 840 " \"subtext\":\"subtext\"," | 814 " \"subtext\":\"subtext\"," |
| 841 " \"details\":\"details\"" | 815 " \"details\":\"details\"" |
| 842 " }" | 816 " }" |
| 843 " }" | 817 " }" |
| 844 "}"; | 818 "}"; |
| 845 EXPECT_CALL(delegate_, OnWalletError(expected_error_type)).Times(1); | 819 EXPECT_CALL(delegate_, OnWalletError(expected_error_type)).Times(1); |
| 846 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | 820 delegate_.ExpectLogWalletApiCallDuration( |
| 821 AutofillMetrics::GET_WALLET_ITEMS, 1); |
| 847 delegate_.ExpectBaselineMetrics(); | 822 delegate_.ExpectBaselineMetrics(); |
| 848 delegate_.ExpectWalletErrorMetric(expected_autofill_metric); | 823 delegate_.ExpectWalletErrorMetric(expected_autofill_metric); |
| 849 | 824 |
| 850 std::vector<AutocheckoutStatistic> statistics; | 825 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
| 851 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, | |
| 852 GURL(kMerchantUrl), | |
| 853 statistics, | |
| 854 "google_transaction_id"); | |
| 855 std::string buyer_error; | 826 std::string buyer_error; |
| 856 if (!buyer_error_type_string.empty()) { | 827 if (!buyer_error_type_string.empty()) { |
| 857 buyer_error = base::StringPrintf("\"buyer_error_type\":\"%s\",", | 828 buyer_error = base::StringPrintf("\"buyer_error_type\":\"%s\",", |
| 858 buyer_error_type_string.c_str()); | 829 buyer_error_type_string.c_str()); |
| 859 } | 830 } |
| 860 std::string response = base::StringPrintf(kResponseTemplate, | 831 std::string response = base::StringPrintf(kResponseTemplate, |
| 861 error_type_string.c_str(), | 832 error_type_string.c_str(), |
| 862 buyer_error.c_str()); | 833 buyer_error.c_str()); |
| 863 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, | 834 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, |
| 864 kSendAutocheckoutStatusOfSuccessValidRequest, | 835 kGetWalletItemsValidRequest, |
| 865 response); | 836 response); |
| 866 } | 837 } |
| 867 | 838 |
| 868 protected: | 839 protected: |
| 869 content::TestBrowserThreadBundle thread_bundle_; | 840 content::TestBrowserThreadBundle thread_bundle_; |
| 870 scoped_ptr<WalletClient> wallet_client_; | 841 scoped_ptr<WalletClient> wallet_client_; |
| 871 TestingProfile browser_context_; | 842 TestingProfile browser_context_; |
| 872 MockWalletClientDelegate delegate_; | 843 MockWalletClientDelegate delegate_; |
| 873 | 844 |
| 874 private: | 845 private: |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 980 TestWalletErrorCode(test_cases[i].error_type_string, | 951 TestWalletErrorCode(test_cases[i].error_type_string, |
| 981 test_cases[i].buyer_error_type_string, | 952 test_cases[i].buyer_error_type_string, |
| 982 test_cases[i].expected_error_type, | 953 test_cases[i].expected_error_type, |
| 983 test_cases[i].expected_autofill_metric); | 954 test_cases[i].expected_autofill_metric); |
| 984 } | 955 } |
| 985 } | 956 } |
| 986 | 957 |
| 987 TEST_F(WalletClientTest, WalletErrorResponseMissing) { | 958 TEST_F(WalletClientTest, WalletErrorResponseMissing) { |
| 988 EXPECT_CALL(delegate_, OnWalletError( | 959 EXPECT_CALL(delegate_, OnWalletError( |
| 989 WalletClient::UNKNOWN_ERROR)).Times(1); | 960 WalletClient::UNKNOWN_ERROR)).Times(1); |
| 990 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | 961 delegate_.ExpectLogWalletApiCallDuration( |
| 962 AutofillMetrics::GET_WALLET_ITEMS, 1); |
| 991 delegate_.ExpectBaselineMetrics(); | 963 delegate_.ExpectBaselineMetrics(); |
| 992 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_UNKNOWN_ERROR); | 964 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_UNKNOWN_ERROR); |
| 993 | 965 |
| 994 std::vector<AutocheckoutStatistic> statistics; | 966 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
| 995 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, | |
| 996 GURL(kMerchantUrl), | |
| 997 statistics, | |
| 998 "google_transaction_id"); | |
| 999 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, | 967 VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR, |
| 1000 kSendAutocheckoutStatusOfSuccessValidRequest, | 968 kGetWalletItemsValidRequest, |
| 1001 kErrorTypeMissingInResponse); | 969 kErrorTypeMissingInResponse); |
| 1002 } | 970 } |
| 1003 | 971 |
| 1004 TEST_F(WalletClientTest, NetworkFailureOnExpectedVoidResponse) { | |
| 1005 EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1); | |
| 1006 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | |
| 1007 delegate_.ExpectBaselineMetrics(); | |
| 1008 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR); | |
| 1009 | |
| 1010 std::vector<AutocheckoutStatistic> statistics; | |
| 1011 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, | |
| 1012 GURL(kMerchantUrl), | |
| 1013 statistics, | |
| 1014 "google_transaction_id"); | |
| 1015 VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED, | |
| 1016 kSendAutocheckoutStatusOfSuccessValidRequest, | |
| 1017 std::string()); | |
| 1018 } | |
| 1019 | |
| 1020 TEST_F(WalletClientTest, NetworkFailureOnExpectedResponse) { | 972 TEST_F(WalletClientTest, NetworkFailureOnExpectedResponse) { |
| 1021 EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1); | 973 EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1); |
| 1022 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, | 974 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, |
| 1023 1); | 975 1); |
| 1024 delegate_.ExpectBaselineMetrics(); | 976 delegate_.ExpectBaselineMetrics(); |
| 1025 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR); | 977 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR); |
| 1026 | 978 |
| 1027 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); | 979 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
| 1028 VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED, | 980 VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED, |
| 1029 kGetWalletItemsValidRequest, | 981 kGetWalletItemsValidRequest, |
| 1030 std::string()); | 982 std::string()); |
| 1031 } | 983 } |
| 1032 | 984 |
| 1033 TEST_F(WalletClientTest, RequestError) { | 985 TEST_F(WalletClientTest, RequestError) { |
| 1034 EXPECT_CALL(delegate_, OnWalletError(WalletClient::BAD_REQUEST)).Times(1); | 986 EXPECT_CALL(delegate_, OnWalletError(WalletClient::BAD_REQUEST)).Times(1); |
| 1035 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | 987 delegate_.ExpectLogWalletApiCallDuration( |
| 988 AutofillMetrics::GET_WALLET_ITEMS, 1); |
| 1036 delegate_.ExpectBaselineMetrics(); | 989 delegate_.ExpectBaselineMetrics(); |
| 1037 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_BAD_REQUEST); | 990 delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_BAD_REQUEST); |
| 1038 | 991 |
| 1039 std::vector<AutocheckoutStatistic> statistics; | 992 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
| 1040 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, | |
| 1041 GURL(kMerchantUrl), | |
| 1042 statistics, | |
| 1043 "google_transaction_id"); | |
| 1044 VerifyAndFinishRequest(net::HTTP_BAD_REQUEST, | 993 VerifyAndFinishRequest(net::HTTP_BAD_REQUEST, |
| 1045 kSendAutocheckoutStatusOfSuccessValidRequest, | 994 kGetWalletItemsValidRequest, |
| 1046 std::string()); | 995 std::string()); |
| 1047 } | 996 } |
| 1048 | 997 |
| 1049 TEST_F(WalletClientTest, GetFullWalletSuccess) { | 998 TEST_F(WalletClientTest, GetFullWalletSuccess) { |
| 1050 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1); | 999 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_FULL_WALLET, 1); |
| 1051 delegate_.ExpectBaselineMetrics(); | 1000 delegate_.ExpectBaselineMetrics(); |
| 1052 | 1001 |
| 1053 WalletClient::FullWalletRequest full_wallet_request( | 1002 WalletClient::FullWalletRequest full_wallet_request( |
| 1054 "instrument_id", | 1003 "instrument_id", |
| 1055 "shipping_address_id", | 1004 "shipping_address_id", |
| (...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1696 | 1645 |
| 1697 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(), | 1646 wallet_client_->SaveToWallet(GetTestAddressUpgradeInstrument(), |
| 1698 scoped_ptr<Address>(), | 1647 scoped_ptr<Address>(), |
| 1699 GURL(kMerchantUrl)); | 1648 GURL(kMerchantUrl)); |
| 1700 | 1649 |
| 1701 VerifyAndFinishRequest(net::HTTP_OK, | 1650 VerifyAndFinishRequest(net::HTTP_OK, |
| 1702 kUpdateInstrumentAddressValidRequest, | 1651 kUpdateInstrumentAddressValidRequest, |
| 1703 kUpdateMalformedResponse); | 1652 kUpdateMalformedResponse); |
| 1704 } | 1653 } |
| 1705 | 1654 |
| 1706 TEST_F(WalletClientTest, SendAutocheckoutOfStatusSuccess) { | |
| 1707 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | |
| 1708 delegate_.ExpectBaselineMetrics(); | |
| 1709 | |
| 1710 AutocheckoutStatistic statistic; | |
| 1711 statistic.page_number = 1; | |
| 1712 statistic.steps.push_back(AUTOCHECKOUT_STEP_SHIPPING); | |
| 1713 statistic.time_taken = base::TimeDelta::FromMilliseconds(100); | |
| 1714 std::vector<AutocheckoutStatistic> statistics; | |
| 1715 statistics.push_back(statistic); | |
| 1716 wallet_client_->SendAutocheckoutStatus(autofill::SUCCESS, | |
| 1717 GURL(kMerchantUrl), | |
| 1718 statistics, | |
| 1719 "google_transaction_id"); | |
| 1720 VerifyAndFinishRequest(net::HTTP_OK, | |
| 1721 kSendAutocheckoutStatusWithStatisticsValidRequest, | |
| 1722 ")]}"); // Invalid JSON. Should be ignored. | |
| 1723 } | |
| 1724 | |
| 1725 TEST_F(WalletClientTest, SendAutocheckoutStatusOfFailure) { | |
| 1726 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::SEND_STATUS, 1); | |
| 1727 delegate_.ExpectBaselineMetrics(); | |
| 1728 | |
| 1729 std::vector<AutocheckoutStatistic> statistics; | |
| 1730 wallet_client_->SendAutocheckoutStatus(autofill::CANNOT_PROCEED, | |
| 1731 GURL(kMerchantUrl), | |
| 1732 statistics, | |
| 1733 "google_transaction_id"); | |
| 1734 VerifyAndFinishRequest(net::HTTP_OK, | |
| 1735 kSendAutocheckoutStatusOfFailureValidRequest, | |
| 1736 ")]}"); // Invalid JSON. Should be ignored. | |
| 1737 } | |
| 1738 | |
| 1739 TEST_F(WalletClientTest, HasRequestInProgress) { | 1655 TEST_F(WalletClientTest, HasRequestInProgress) { |
| 1740 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); | 1656 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); |
| 1741 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, | 1657 delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS, |
| 1742 1); | 1658 1); |
| 1743 delegate_.ExpectBaselineMetrics(); | 1659 delegate_.ExpectBaselineMetrics(); |
| 1744 | 1660 |
| 1745 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); | 1661 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
| 1746 EXPECT_TRUE(wallet_client_->HasRequestInProgress()); | 1662 EXPECT_TRUE(wallet_client_->HasRequestInProgress()); |
| 1747 | 1663 |
| 1748 VerifyAndFinishRequest(net::HTTP_OK, | 1664 VerifyAndFinishRequest(net::HTTP_OK, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1796 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); | 1712 wallet_client_->GetWalletItems(GURL(kMerchantUrl)); |
| 1797 EXPECT_EQ(2U, wallet_client_->pending_requests_.size()); | 1713 EXPECT_EQ(2U, wallet_client_->pending_requests_.size()); |
| 1798 | 1714 |
| 1799 wallet_client_->CancelRequests(); | 1715 wallet_client_->CancelRequests(); |
| 1800 EXPECT_EQ(0U, wallet_client_->pending_requests_.size()); | 1716 EXPECT_EQ(0U, wallet_client_->pending_requests_.size()); |
| 1801 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); | 1717 EXPECT_FALSE(wallet_client_->HasRequestInProgress()); |
| 1802 } | 1718 } |
| 1803 | 1719 |
| 1804 } // namespace wallet | 1720 } // namespace wallet |
| 1805 } // namespace autofill | 1721 } // namespace autofill |
| OLD | NEW |