Index: chrome/browser/autofill/wallet/wallet_items_unittest.cc |
diff --git a/chrome/browser/autofill/wallet/wallet_items_unittest.cc b/chrome/browser/autofill/wallet/wallet_items_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..07827c4753fe717f142ad6674fb30b784fd1ab05 |
--- /dev/null |
+++ b/chrome/browser/autofill/wallet/wallet_items_unittest.cc |
@@ -0,0 +1,449 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/json/json_reader.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/values.h" |
+#include "chrome/browser/autofill/wallet/wallet_items.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+const char kMaskedInstrument[] = |
+ "{" |
+ " \"descriptive_name\":\"descriptive_name\"," |
+ " \"type\":\"VISA\"," |
+ " \"supported_currency\":" |
+ " [" |
+ " \"currency\"" |
+ " ]," |
+ " \"last_four_digits\":\"last_four_digits\"," |
+ " \"expiration_month\":12," |
+ " \"expiration_year\":2012," |
+ " \"brand\":\"brand\"," |
+ " \"billing_address\":" |
+ " {" |
+ " \"name\":\"name\"," |
+ " \"address1\":\"address1\"," |
+ " \"address2\":\"address2\"," |
+ " \"city\":\"city\"," |
+ " \"state\":\"state\"," |
+ " \"postal_code\":\"postal_code\"," |
+ " \"phone_number\":\"phone_number\"," |
+ " \"country_code\":\"country_code\"" |
+ " }," |
+ " \"status\":\"VALID\"," |
+ " \"object_id\":\"object_id\"" |
+ "}"; |
+ |
+const char kMaskedInstrumentMissingStatus[] = |
+ "{" |
+ " \"descriptive_name\":\"descriptive_name\"," |
+ " \"type\":\"VISA\"," |
+ " \"supported_currency\":" |
+ " [" |
+ " \"currency\"" |
+ " ]," |
+ " \"last_four_digits\":\"last_four_digits\"," |
+ " \"expiration_month\":12," |
+ " \"expiration_year\":2012," |
+ " \"brand\":\"brand\"," |
+ " \"billing_address\":" |
+ " {" |
+ " \"name\":\"name\"," |
+ " \"address1\":\"address1\"," |
+ " \"address2\":\"address2\"," |
+ " \"city\":\"city\"," |
+ " \"state\":\"state\"," |
+ " \"postal_code\":\"postal_code\"," |
+ " \"phone_number\":\"phone_number\"," |
+ " \"country_code\":\"country_code\"" |
+ " }," |
+ " \"object_id\":\"object_id\"" |
+ "}"; |
+ |
+const char kMaskedInstrumentMissingType[] = |
+ "{" |
+ " \"descriptive_name\":\"descriptive_name\"," |
+ " \"supported_currency\":" |
+ " [" |
+ " \"currency\"" |
+ " ]," |
+ " \"last_four_digits\":\"last_four_digits\"," |
+ " \"expiration_month\":12," |
+ " \"expiration_year\":2012," |
+ " \"brand\":\"brand\"," |
+ " \"billing_address\":" |
+ " {" |
+ " \"name\":\"name\"," |
+ " \"address1\":\"address1\"," |
+ " \"address2\":\"address2\"," |
+ " \"city\":\"city\"," |
+ " \"state\":\"state\"," |
+ " \"postal_code\":\"postal_code\"," |
+ " \"phone_number\":\"phone_number\"," |
+ " \"country_code\":\"country_code\"" |
+ " }," |
+ " \"status\":\"VALID\"," |
+ " \"object_id\":\"object_id\"" |
+ "}"; |
+ |
+const char kMaskedInstrumentMissingLastFourDigits[] = |
+ "{" |
+ " \"descriptive_name\":\"descriptive_name\"," |
+ " \"type\":\"VISA\"," |
+ " \"supported_currency\":" |
+ " [" |
+ " \"currency\"" |
+ " ]," |
+ " \"expiration_month\":12," |
+ " \"expiration_year\":2012," |
+ " \"brand\":\"brand\"," |
+ " \"billing_address\":" |
+ " {" |
+ " \"name\":\"name\"," |
+ " \"address1\":\"address1\"," |
+ " \"address2\":\"address2\"," |
+ " \"city\":\"city\"," |
+ " \"state\":\"state\"," |
+ " \"postal_code\":\"postal_code\"," |
+ " \"phone_number\":\"phone_number\"," |
+ " \"country_code\":\"country_code\"" |
+ " }," |
+ " \"status\":\"VALID\"," |
+ " \"object_id\":\"object_id\"" |
+ "}"; |
+ |
+const char kMaskedInstrumentMissingAddress[] = |
+ "{" |
+ " \"descriptive_name\":\"descriptive_name\"," |
+ " \"type\":\"VISA\"," |
+ " \"supported_currency\":" |
+ " [" |
+ " \"currency\"" |
+ " ]," |
+ " \"last_four_digits\":\"last_four_digits\"," |
+ " \"expiration_month\":12," |
+ " \"expiration_year\":2012," |
+ " \"brand\":\"brand\"," |
+ " \"status\":\"VALID\"," |
+ " \"object_id\":\"object_id\"" |
+ "}"; |
+ |
+const char kMaskedInstrumentMalformedAddress[] = |
+ "{" |
+ " \"descriptive_name\":\"descriptive_name\"," |
+ " \"type\":\"VISA\"," |
+ " \"supported_currency\":" |
+ " [" |
+ " \"currency\"" |
+ " ]," |
+ " \"last_four_digits\":\"last_four_digits\"," |
+ " \"expiration_month\":12," |
+ " \"expiration_year\":2012," |
+ " \"brand\":\"brand\"," |
+ " \"billing_address\":" |
+ " {" |
+ " \"address1\":\"address1\"," |
+ " \"address2\":\"address2\"," |
+ " \"city\":\"city\"," |
+ " \"state\":\"state\"," |
+ " \"phone_number\":\"phone_number\"," |
+ " \"country_code\":\"country_code\"" |
+ " }," |
+ " \"status\":\"VALID\"," |
+ " \"object_id\":\"object_id\"" |
+ "}"; |
+ |
+const char kMaskedInstrumentMissingObjectId[] = |
+ "{" |
+ " \"descriptive_name\":\"descriptive_name\"," |
+ " \"type\":\"VISA\"," |
+ " \"supported_currency\":" |
+ " [" |
+ " \"currency\"" |
+ " ]," |
+ " \"last_four_digits\":\"last_four_digits\"," |
+ " \"expiration_month\":12," |
+ " \"expiration_year\":2012," |
+ " \"brand\":\"brand\"," |
+ " \"billing_address\":" |
+ " {" |
+ " \"name\":\"name\"," |
+ " \"address1\":\"address1\"," |
+ " \"address2\":\"address2\"," |
+ " \"city\":\"city\"," |
+ " \"state\":\"state\"," |
+ " \"postal_code\":\"postal_code\"," |
+ " \"phone_number\":\"phone_number\"," |
+ " \"country_code\":\"country_code\"" |
+ " }," |
+ " \"status\":\"VALID\"" |
+ "}"; |
+ |
+const char kLegalDocument[] = |
+ "{" |
+ " \"legal_document_id\":\"doc_id\"," |
+ " \"display_name\":\"display_name\"," |
+ " \"document\":\"doc_body\"" |
+ "}"; |
+ |
+const char kLegalDocumentMissingDocumentId[] = |
+ "{" |
+ " \"display_name\":\"display_name\"," |
+ " \"document\":\"doc_body\"" |
+ "}"; |
+ |
+const char kLegalDocumentMissingDisplayName[] = |
+ "{" |
+ " \"legal_document_id\":\"doc_id\"," |
+ " \"document\":\"doc_body\"" |
+ "}"; |
+ |
+const char kLegalDocumentMissingDocumentBody[] = |
+ "{" |
+ " \"legal_document_id\":\"doc_id\"," |
+ " \"display_name\":\"display_name\"" |
+ "}"; |
+ |
+const char kWalletItemsWithRequiredActions[] = |
+ "{" |
+ " \"google_transaction_id\":\"google_transaction_id\"," |
+ " \"required_action\":" |
+ " [" |
+ " \"required_action\"" |
+ " ]" |
+ "}"; |
+ |
+const char kWalletItems[] = |
+ "{" |
+ " \"required_action\":" |
+ " [" |
+ " ]," |
+ " \"google_transaction_id\":\"google_transaction_id\"," |
+ " \"instrument\":" |
+ " [" |
+ " {" |
+ " \"descriptive_name\":\"descriptive_name\"," |
+ " \"type\":\"VISA\"," |
+ " \"supported_currency\":" |
+ " [" |
+ " \"currency\"" |
+ " ]," |
+ " \"last_four_digits\":\"last_four_digits\"," |
+ " \"expiration_month\":12," |
+ " \"expiration_year\":2012," |
+ " \"brand\":\"brand\"," |
+ " \"billing_address\":" |
+ " {" |
+ " \"name\":\"name\"," |
+ " \"address1\":\"address1\"," |
+ " \"address2\":\"address2\"," |
+ " \"city\":\"city\"," |
+ " \"state\":\"state\"," |
+ " \"postal_code\":\"postal_code\"," |
+ " \"phone_number\":\"phone_number\"," |
+ " \"country_code\":\"country_code\"" |
+ " }," |
+ " \"status\":\"VALID\"," |
+ " \"object_id\":\"object_id\"" |
+ " }" |
+ " ]," |
+ " \"default_instrument_id\":\"default_instrument_id\"," |
+ " \"address\":" |
+ " [" |
+ " {" |
+ " \"id\":\"id\"," |
+ " \"phone_number\":\"phone_number\"," |
+ " \"postal_address\":" |
+ " {" |
+ " \"recipient_name\":\"recipient_name\"," |
+ " \"address_line_1\":\"address_line_1\"," |
+ " \"address_line_2\":\"address_line_2\"," |
+ " \"locality_name\":\"locality_name\"," |
+ " \"administrative_area_name\":\"administrative_area_name\"," |
+ " \"postal_code_number\":\"postal_code_number\"," |
+ " \"country_name_code\":\"country_name_code\"" |
+ " }" |
+ " }" |
+ " ]," |
+ " \"default_address_id\":\"default_address_id\"," |
+ " \"required_legal_document\":" |
+ " [" |
+ " {" |
+ " \"legal_document_id\":\"doc_id\"," |
+ " \"display_name\":\"display_name\"," |
+ " \"document\":\"doc_body\"" |
+ " }" |
+ " ]" |
+ "}"; |
+ |
+} // end anonymous namespace |
+ |
+namespace wallet { |
+ |
+class WalletItemsTest : public testing::Test { |
+ public: |
+ WalletItemsTest() {} |
+ virtual void SetUp() { |
+ dict.reset(); |
+ } |
+ protected: |
+ void SetUpDictionary(const std::string& json) { |
+ scoped_ptr<Value> value(base::JSONReader::Read(json)); |
+ DCHECK(value.get()); |
+ DCHECK(value->IsType(Value::TYPE_DICTIONARY)); |
+ dict.reset(static_cast<DictionaryValue*>(value.release())); |
+ } |
+ scoped_ptr<DictionaryValue> dict; |
+}; |
+ |
+TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingStatus) { |
+ SetUpDictionary(kMaskedInstrumentMissingStatus); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingType) { |
+ SetUpDictionary(kMaskedInstrumentMissingType); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingLastFourDigits) { |
+ SetUpDictionary(kMaskedInstrumentMissingLastFourDigits); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingAddress) { |
+ SetUpDictionary(kMaskedInstrumentMissingAddress); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateMaskedInstrumentMalformedAddress) { |
+ SetUpDictionary(kMaskedInstrumentMalformedAddress); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingObjectId) { |
+ SetUpDictionary(kMaskedInstrumentMissingObjectId); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateMaskedInstrument) { |
+ SetUpDictionary(kMaskedInstrument); |
+ Address* address = new Address("country_code", |
+ "name", |
+ "address1", |
+ "address2", |
+ "city", |
+ "state", |
+ "postal_code", |
+ "phone_number", |
+ ""); |
+ std::vector<std::string> supported_currencies; |
+ supported_currencies.push_back("currency"); |
+ WalletItems::MaskedInstrument masked_instrument( |
+ "descriptive_name", |
+ WalletItems::MaskedInstrument::VISA, |
+ supported_currencies, |
+ "last_four_digits", |
+ 12, |
+ 2012, |
+ "brand", |
+ address, |
+ WalletItems::MaskedInstrument::VALID, |
+ "object_id"); |
+ ASSERT_EQ(masked_instrument, |
+ *WalletItems::MaskedInstrument::CreateMaskedInstrument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateLegalDocumentMissingDocId) { |
+ SetUpDictionary(kLegalDocumentMissingDocumentId); |
+ ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateLegalDocument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateLegalDocumentMissingDisplayName) { |
+ SetUpDictionary(kLegalDocumentMissingDisplayName); |
+ ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateLegalDocument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateLegalDocumentMissingDocBody) { |
+ SetUpDictionary(kLegalDocumentMissingDocumentBody); |
+ ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateLegalDocument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateLegalDocument) { |
+ SetUpDictionary(kLegalDocument); |
+ WalletItems::LegalDocument expected("doc_id", "display_name", "doc_body"); |
+ ASSERT_EQ(expected, |
+ *WalletItems::LegalDocument::CreateLegalDocument(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateWalletItemsWithRequiredActions) { |
+ SetUpDictionary(kWalletItemsWithRequiredActions); |
+ std::vector<std::string> required_actions; |
+ required_actions.push_back("required_action"); |
+ WalletItems expected(required_actions, "google_transaction_id", "", ""); |
+ ASSERT_EQ(expected, *WalletItems::CreateWalletItems(*dict)); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateWalletItems) { |
+ SetUpDictionary(kWalletItems); |
+ std::vector<std::string> required_actions; |
+ WalletItems expected(required_actions, |
+ "google_transaction_id", |
+ "default_instrument_id", |
+ "default_address_id"); |
+ |
+ Address* billing_address = new Address("country_code", |
+ "name", |
+ "address1", |
+ "address2", |
+ "city", |
+ "state", |
+ "postal_code", |
+ "phone_number", |
+ ""); |
+ std::vector<std::string> supported_currencies; |
+ supported_currencies.push_back("currency"); |
+ WalletItems::MaskedInstrument* masked_instrument = |
+ new WalletItems::MaskedInstrument("descriptive_name", |
+ WalletItems::MaskedInstrument::VISA, |
+ supported_currencies, |
+ "last_four_digits", |
+ 12, |
+ 2012, |
+ "brand", |
+ billing_address, |
+ WalletItems::MaskedInstrument::VALID, |
+ "object_id"); |
+ expected.AddInstrument(masked_instrument); |
+ |
+ Address* shipping_address = new Address("country_code", |
+ "name", |
+ "address1", |
+ "address2", |
+ "city", |
+ "state", |
+ "postal_code", |
+ "phone_number", |
+ "id"); |
+ expected.AddAddress(shipping_address); |
+ |
+ WalletItems::LegalDocument* legal_document = |
+ new WalletItems::LegalDocument("doc_id", |
+ "display_name", |
+ "doc_body"); |
+ expected.AddLegalDocument(legal_document); |
+ |
+ ASSERT_EQ(expected, *WalletItems::CreateWalletItems(*dict)); |
+} |
+ |
+} // end wallet namespace |