Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / autofill / content / browser / wallet / wallet_client_unittest.cc
index 0c25ce9..8f805dd 100644 (file)
@@ -6,13 +6,13 @@
 #include "base/json/json_writer.h"
 #include "base/logging.h"
 #include "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop.h"
 #include "base/run_loop.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/string_split.h"
 #include "base/strings/string_util.h"
 #include "base/strings/stringprintf.h"
 #include "base/values.h"
-#include "chrome/test/base/testing_profile.h"
 #include "components/autofill/content/browser/wallet/full_wallet.h"
 #include "components/autofill/content/browser/wallet/instrument.h"
 #include "components/autofill/content/browser/wallet/wallet_client.h"
@@ -28,6 +28,7 @@
 #include "net/url_request/test_url_fetcher_factory.h"
 #include "net/url_request/url_fetcher_delegate.h"
 #include "net/url_request/url_request_status.h"
+#include "net/url_request/url_request_test_util.h"
 #include "testing/gmock/include/gmock/gmock.h"
 #include "testing/gtest/include/gtest/gtest.h"
 #include "url/gurl.h"
@@ -61,7 +62,8 @@ const char kGetFullWalletValidResponse[] =
     "      \"locality_name\":\"locality_name\","
     "      \"administrative_area_name\":\"administrative_area_name\","
     "      \"postal_code_number\":\"postal_code_number\","
-    "      \"country_name_code\":\"US\""
+    "      \"country_name_code\":\"US\","
+    "      \"language_code\":\"language_code\""
     "    }"
     "  },"
     "  \"shipping_address\":"
@@ -79,7 +81,8 @@ const char kGetFullWalletValidResponse[] =
     "      \"locality_name\":\"ship_locality_name\","
     "      \"administrative_area_name\":\"ship_administrative_area_name\","
     "      \"postal_code_number\":\"ship_postal_code_number\","
-    "      \"country_name_code\":\"US\""
+    "      \"country_name_code\":\"US\","
+    "      \"language_code\":\"ship_language_code\""
     "    }"
     "  },"
     "  \"required_action\":"
@@ -103,7 +106,6 @@ const char kGetWalletItemsValidResponse[] =
     "    {"
     "      \"descriptive_name\":\"descriptive_name\","
     "      \"type\":\"VISA\","
-    "      \"supported_currency\":\"currency_code\","
     "      \"last_four_digits\":\"4111\","
     "      \"expiration_month\":12,"
     "      \"expiration_year\":3000,"
@@ -117,7 +119,8 @@ const char kGetWalletItemsValidResponse[] =
     "        \"state\":\"state\","
     "        \"postal_code\":\"postal_code\","
     "        \"phone_number\":\"phone_number\","
-    "        \"country_code\":\"US\""
+    "        \"country_code\":\"US\","
+    "        \"language_code\":\"language_code\""
     "      },"
     "      \"status\":\"VALID\","
     "      \"object_id\":\"default_instrument_id\""
@@ -278,6 +281,30 @@ const char kErrorResponse[] =
     "  }"
     "}";
 
+const char kErrorResponseSpendingLimitExceeded[] =
+    "{"
+    "  \"error_type\":\"APPLICATION_ERROR\","
+    "  \"error_detail\":\"error_detail\","
+    "  \"application_error\":\"application_error\","
+    "  \"debug_data\":"
+    "  {"
+    "    \"debug_message\":\"debug_message\","
+    "    \"stack_trace\":\"stack_trace\""
+    "  },"
+    "  \"application_error_data\":\"application_error_data\","
+    "  \"wallet_error\":"
+    "  {"
+    "    \"error_type\":\"SPENDING_LIMIT_EXCEEDED\","
+    "    \"error_detail\":\"error_detail\","
+    "    \"message_for_user\":"
+    "    {"
+    "      \"text\":\"text\","
+    "      \"subtext\":\"subtext\","
+    "      \"details\":\"details\""
+    "    }"
+    "  }"
+    "}";
+
 const char kErrorTypeMissingInResponse[] =
     "{"
     "  \"error_type\":\"Not APPLICATION_ERROR\","
@@ -370,6 +397,16 @@ const char kGetWalletItemsValidRequest[] =
         "\"use_minimal_addresses\":false"
     "}";
 
+const char kGetWalletItemsWithTransactionDetails[] =
+    "{"
+        "\"currency_code\":\"USD\","
+        "\"estimated_total_price\":\"100.00\","
+        "\"merchant_domain\":\"https://example.com/\","
+        "\"phone_number_required\":true,"
+        "\"shipping_address_required\":true,"
+        "\"use_minimal_addresses\":false"
+    "}";
+
 const char kGetWalletItemsNoShippingRequest[] =
     "{"
         "\"merchant_domain\":\"https://example.com/\","
@@ -396,6 +433,7 @@ const char kSaveAddressValidRequest[] =
                 "\"administrative_area_name\":\"save_admin_area_name\","
                 "\"country_name_code\":\"US\","
                 "\"dependent_locality_name\":\"save_dependent_locality_name\","
+                "\"language_code\":\"save_language_code\","
                 "\"locality_name\":\"save_locality_name\","
                 "\"postal_code_number\":\"save_postal_code_number\","
                 "\"recipient_name\":\"save_recipient_name\","
@@ -421,6 +459,7 @@ const char kSaveInstrumentValidRequest[] =
                     "\"administrative_area_name\":\"admin_area_name\","
                     "\"country_name_code\":\"US\","
                     "\"dependent_locality_name\":\"dependent_locality_name\","
+                    "\"language_code\":\"language_code\","
                     "\"locality_name\":\"locality_name\","
                     "\"postal_code_number\":\"postal_code_number\","
                     "\"recipient_name\":\"recipient_name\","
@@ -456,6 +495,7 @@ const char kSaveInstrumentAndAddressValidRequest[] =
                     "\"administrative_area_name\":\"admin_area_name\","
                     "\"country_name_code\":\"US\","
                     "\"dependent_locality_name\":\"dependent_locality_name\","
+                    "\"language_code\":\"language_code\","
                     "\"locality_name\":\"locality_name\","
                     "\"postal_code_number\":\"postal_code_number\","
                     "\"recipient_name\":\"recipient_name\","
@@ -485,6 +525,7 @@ const char kSaveInstrumentAndAddressValidRequest[] =
                 "\"administrative_area_name\":\"save_admin_area_name\","
                 "\"country_name_code\":\"US\","
                 "\"dependent_locality_name\":\"save_dependent_locality_name\","
+                "\"language_code\":\"save_language_code\","
                 "\"locality_name\":\"save_locality_name\","
                 "\"postal_code_number\":\"save_postal_code_number\","
                 "\"recipient_name\":\"save_recipient_name\","
@@ -513,6 +554,7 @@ const char kUpdateAddressValidRequest[] =
                 "\"administrative_area_name\":\"ship_admin_area_name\","
                 "\"country_name_code\":\"US\","
                 "\"dependent_locality_name\":\"ship_dependent_locality_name\","
+                "\"language_code\":\"ship_language_code\","
                 "\"locality_name\":\"ship_locality_name\","
                 "\"postal_code_number\":\"ship_postal_code_number\","
                 "\"recipient_name\":\"ship_recipient_name\","
@@ -538,6 +580,7 @@ const char kUpdateInstrumentAddressValidRequest[] =
             "\"administrative_area_name\":\"admin_area_name\","
             "\"country_name_code\":\"US\","
             "\"dependent_locality_name\":\"dependent_locality_name\","
+            "\"language_code\":\"language_code\","
             "\"locality_name\":\"locality_name\","
             "\"postal_code_number\":\"postal_code_number\","
             "\"recipient_name\":\"recipient_name\","
@@ -563,6 +606,7 @@ const char kUpdateInstrumentAddressWithNameChangeValidRequest[] =
             "\"administrative_area_name\":\"admin_area_name\","
             "\"country_name_code\":\"US\","
             "\"dependent_locality_name\":\"dependent_locality_name\","
+            "\"language_code\":\"language_code\","
             "\"locality_name\":\"locality_name\","
             "\"postal_code_number\":\"postal_code_number\","
             "\"recipient_name\":\"recipient_name\","
@@ -613,19 +657,19 @@ class MockWalletClientDelegate : public WalletClientDelegate {
         is_shipping_required_(true) {}
   ~MockWalletClientDelegate() {}
 
-  virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE {
+  virtual const AutofillMetrics& GetMetricLogger() const override {
     return metric_logger_;
   }
 
-  virtual std::string GetRiskData() const OVERRIDE {
+  virtual std::string GetRiskData() const override {
     return "risky business";
   }
 
-  virtual std::string GetWalletCookieValue() const OVERRIDE {
+  virtual std::string GetWalletCookieValue() const override {
     return "gdToken";
   }
 
-  virtual bool IsShippingAddressRequired() const OVERRIDE {
+  virtual bool IsShippingAddressRequired() const override {
     return is_shipping_required_;
   }
 
@@ -679,12 +723,12 @@ class MockWalletClientDelegate : public WalletClientDelegate {
                     const std::vector<FormFieldError>& form_field_errors));
   MOCK_METHOD1(OnWalletError, void(WalletClient::ErrorType error_type));
 
-  virtual void OnDidGetFullWallet(scoped_ptr<FullWallet> full_wallet) OVERRIDE {
+  virtual void OnDidGetFullWallet(scoped_ptr<FullWallet> full_wallet) override {
     EXPECT_TRUE(full_wallet);
     ++full_wallets_received_;
   }
   virtual void OnDidGetWalletItems(scoped_ptr<WalletItems> wallet_items)
-      OVERRIDE {
+      override {
     EXPECT_TRUE(wallet_items);
     ++wallet_items_received_;
   }
@@ -703,16 +747,17 @@ class MockWalletClientDelegate : public WalletClientDelegate {
 
 class WalletClientTest : public testing::Test {
  public:
-  virtual void SetUp() OVERRIDE {
-    wallet_client_.reset(
-        new WalletClient(browser_context_.GetRequestContext(),
-                         &delegate_,
-                         GURL(kMerchantUrl)));
+  WalletClientTest()
+      : request_context_(new net::TestURLRequestContextGetter(
+            base::MessageLoopProxy::current())) {}
+  ~WalletClientTest() override {}
+
+  void SetUp() override {
+    wallet_client_.reset(new WalletClient(
+        request_context_.get(), &delegate_, GURL(kMerchantUrl)));
   }
 
-  virtual void TearDown() OVERRIDE {
-    wallet_client_.reset();
-  }
+  void TearDown() override { wallet_client_.reset(); }
 
   void VerifyAndFinishRequest(net::HttpStatusCode response_code,
                               const std::string& request_body,
@@ -755,14 +800,16 @@ class WalletClientTest : public testing::Test {
     EXPECT_EQ("GoogleLogin auth=gdToken", auth_header_value);
 
     const std::string& upload_data = fetcher->upload_data();
-    std::vector<std::pair<std::string, std::string> > tokens;
+    base::StringPairs tokens;
     base::SplitStringIntoKeyValuePairs(upload_data, '=', '&', &tokens);
     EXPECT_EQ(tokens.size(), expected_parameter_number);
 
     size_t num_params = 0U;
-    for (size_t i = 0; i < tokens.size(); ++i) {
-      const std::string& key = tokens[i].first;
-      const std::string& value = tokens[i].second;
+    for (base::StringPairs::const_iterator iter = tokens.begin();
+         iter != tokens.end();
+         ++iter) {
+      const std::string& key = iter->first;
+      const std::string& value = iter->second;
 
       if (key == "request_content_type") {
         EXPECT_EQ("application/json", value);
@@ -835,7 +882,7 @@ class WalletClientTest : public testing::Test {
     delegate_.ExpectBaselineMetrics();
     delegate_.ExpectWalletErrorMetric(expected_autofill_metric);
 
-    wallet_client_->GetWalletItems();
+    wallet_client_->GetWalletItems(base::string16(), base::string16());
     std::string buyer_error;
     if (!message_type_for_buyer_string.empty()) {
       buyer_error = base::StringPrintf("\"message_type_for_buyer\":\"%s\",",
@@ -852,7 +899,7 @@ class WalletClientTest : public testing::Test {
  protected:
   content::TestBrowserThreadBundle thread_bundle_;
   scoped_ptr<WalletClient> wallet_client_;
-  TestingProfile browser_context_;
+  scoped_refptr<net::TestURLRequestContextGetter> request_context_;
   MockWalletClientDelegate delegate_;
 
  private:
@@ -958,7 +1005,7 @@ TEST_F(WalletClientTest, WalletErrorCodes) {
       },
   };
 
-  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
+  for (size_t i = 0; i < arraysize(test_cases); ++i) {
     SCOPED_TRACE(
         base::StringPrintf(
             "%s - %s",
@@ -971,6 +1018,7 @@ TEST_F(WalletClientTest, WalletErrorCodes) {
   }
 }
 
+
 TEST_F(WalletClientTest, WalletErrorResponseMissing) {
   EXPECT_CALL(delegate_, OnWalletError(
       WalletClient::UNKNOWN_ERROR)).Times(1);
@@ -979,7 +1027,7 @@ TEST_F(WalletClientTest, WalletErrorResponseMissing) {
   delegate_.ExpectBaselineMetrics();
   delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_UNKNOWN_ERROR);
 
-  wallet_client_->GetWalletItems();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
   VerifyAndFinishRequest(net::HTTP_INTERNAL_SERVER_ERROR,
                          kGetWalletItemsValidRequest,
                          kErrorTypeMissingInResponse);
@@ -992,7 +1040,7 @@ TEST_F(WalletClientTest, NetworkFailureOnExpectedResponse) {
   delegate_.ExpectBaselineMetrics();
   delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR);
 
-  wallet_client_->GetWalletItems();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
   VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED,
                          kGetWalletItemsValidRequest,
                          std::string());
@@ -1005,7 +1053,7 @@ TEST_F(WalletClientTest, RequestError) {
   delegate_.ExpectBaselineMetrics();
   delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_BAD_REQUEST);
 
-  wallet_client_->GetWalletItems();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
   VerifyAndFinishRequest(net::HTTP_BAD_REQUEST,
                          kGetWalletItemsValidRequest,
                          std::string());
@@ -1177,7 +1225,7 @@ TEST_F(WalletClientTest, GetWalletItems) {
                                            1);
   delegate_.ExpectBaselineMetrics();
 
-  wallet_client_->GetWalletItems();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
 
   VerifyAndFinishRequest(net::HTTP_OK,
                          kGetWalletItemsValidRequest,
@@ -1185,13 +1233,27 @@ TEST_F(WalletClientTest, GetWalletItems) {
   EXPECT_EQ(1U, delegate_.wallet_items_received());
 }
 
+TEST_F(WalletClientTest, GetWalletItemsWithTransactionDetails) {
+  delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
+                                           1);
+  delegate_.ExpectBaselineMetrics();
+
+  wallet_client_->GetWalletItems(base::ASCIIToUTF16("100.00"),
+                                 base::ASCIIToUTF16("USD"));
+
+  VerifyAndFinishRequest(net::HTTP_OK,
+                         kGetWalletItemsWithTransactionDetails,
+                         kGetWalletItemsValidResponse);
+  EXPECT_EQ(1U, delegate_.wallet_items_received());
+}
+
 TEST_F(WalletClientTest, GetWalletItemsRespectsDelegateForShippingRequired) {
   delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
                                            1);
   delegate_.ExpectBaselineMetrics();
   delegate_.SetIsShippingAddressRequired(false);
 
-  wallet_client_->GetWalletItems();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
 
   VerifyAndFinishRequest(net::HTTP_OK,
                          kGetWalletItemsNoShippingRequest,
@@ -1708,7 +1770,7 @@ TEST_F(WalletClientTest, HasRequestInProgress) {
                                            1);
   delegate_.ExpectBaselineMetrics();
 
-  wallet_client_->GetWalletItems();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
   EXPECT_TRUE(wallet_client_->HasRequestInProgress());
 
   VerifyAndFinishRequest(net::HTTP_OK,
@@ -1717,10 +1779,11 @@ TEST_F(WalletClientTest, HasRequestInProgress) {
   EXPECT_FALSE(wallet_client_->HasRequestInProgress());
 }
 
-TEST_F(WalletClientTest, ErrorResponse) {
+// 500 (INTERNAL_SERVER_ERROR) - response json is parsed.
+TEST_F(WalletClientTest, ErrorResponse500) {
   EXPECT_FALSE(wallet_client_->HasRequestInProgress());
   delegate_.ExpectBaselineMetrics();
-  wallet_client_->GetWalletItems();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
   EXPECT_TRUE(wallet_client_->HasRequestInProgress());
   testing::Mock::VerifyAndClear(delegate_.metric_logger());
 
@@ -1736,13 +1799,67 @@ TEST_F(WalletClientTest, ErrorResponse) {
                          kErrorResponse);
 }
 
+// 403 (FORBIDDEN) - response json is parsed.
+TEST_F(WalletClientTest, ErrorResponse403) {
+  EXPECT_FALSE(wallet_client_->HasRequestInProgress());
+  delegate_.ExpectBaselineMetrics();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
+  EXPECT_TRUE(wallet_client_->HasRequestInProgress());
+  testing::Mock::VerifyAndClear(delegate_.metric_logger());
+
+  EXPECT_CALL(delegate_, OnWalletError(WalletClient::SPENDING_LIMIT_EXCEEDED))
+      .Times(1);
+  delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
+                                           1);
+  delegate_.ExpectWalletErrorMetric(
+      AutofillMetrics::WALLET_SPENDING_LIMIT_EXCEEDED);
+
+  VerifyAndFinishRequest(net::HTTP_FORBIDDEN,
+                         kGetWalletItemsValidRequest,
+                         kErrorResponseSpendingLimitExceeded);
+}
+
+// 400 (BAD_REQUEST) - response json is ignored.
+TEST_F(WalletClientTest, ErrorResponse400) {
+  EXPECT_FALSE(wallet_client_->HasRequestInProgress());
+  delegate_.ExpectBaselineMetrics();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
+  EXPECT_TRUE(wallet_client_->HasRequestInProgress());
+  testing::Mock::VerifyAndClear(delegate_.metric_logger());
+
+  EXPECT_CALL(delegate_, OnWalletError(WalletClient::BAD_REQUEST)).Times(1);
+  delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
+                                           1);
+  delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_BAD_REQUEST);
+
+  VerifyAndFinishRequest(
+      net::HTTP_BAD_REQUEST, kGetWalletItemsValidRequest, kErrorResponse);
+}
+
+// Anything else - response json is ignored.
+TEST_F(WalletClientTest, ErrorResponseOther) {
+  EXPECT_FALSE(wallet_client_->HasRequestInProgress());
+  delegate_.ExpectBaselineMetrics();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
+  EXPECT_TRUE(wallet_client_->HasRequestInProgress());
+  testing::Mock::VerifyAndClear(delegate_.metric_logger());
+
+  EXPECT_CALL(delegate_, OnWalletError(WalletClient::NETWORK_ERROR)).Times(1);
+  delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
+                                           1);
+  delegate_.ExpectWalletErrorMetric(AutofillMetrics::WALLET_NETWORK_ERROR);
+
+  VerifyAndFinishRequest(
+      net::HTTP_NOT_FOUND, kGetWalletItemsValidRequest, kErrorResponse);
+}
+
 TEST_F(WalletClientTest, CancelRequest) {
   EXPECT_FALSE(wallet_client_->HasRequestInProgress());
   delegate_.ExpectLogWalletApiCallDuration(AutofillMetrics::GET_WALLET_ITEMS,
                                            0);
   delegate_.ExpectBaselineMetrics();
 
-  wallet_client_->GetWalletItems();
+  wallet_client_->GetWalletItems(base::string16(), base::string16());
   EXPECT_TRUE(wallet_client_->HasRequestInProgress());
   wallet_client_->CancelRequest();
   EXPECT_FALSE(wallet_client_->HasRequestInProgress());