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..12e6caa4a5c1bd3ba08f67da4d046d643c89b15a |
--- /dev/null |
+++ b/chrome/browser/autofill/wallet/wallet_items_unittest.cc |
@@ -0,0 +1,438 @@ |
+// 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/memory/scoped_ptr.h" |
+#include "base/values.h" |
+#include "chrome/browser/autofill/wallet/wallet_items.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+static 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\"" |
+ "}"; |
+ |
+static 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\"" |
+ "}"; |
+ |
+static 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\"" |
+ "}"; |
+ |
+static 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\"" |
+ "}"; |
+ |
+static 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\"" |
+ "}"; |
+ |
+static 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\"" |
+ "}"; |
+ |
+static 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\"" |
+ "}"; |
+ |
+static const char kLegalDocument[] = |
+ "{" |
+ " \"legal_document_id\":\"doc_id\"," |
+ " \"display_name\":\"display_name\"," |
+ " \"document\":\"doc_body\"" |
+ "}"; |
+ |
+static const char kLegalDocumentMissingDocumentId[] = |
+ "{" |
+ " \"display_name\":\"display_name\"," |
+ " \"document\":\"doc_body\"" |
+ "}"; |
+ |
+static const char kLegalDocumentMissingDisplayName[] = |
+ "{" |
+ " \"legal_document_id\":\"doc_id\"," |
+ " \"document\":\"doc_body\"" |
+ "}"; |
+ |
+static const char kLegalDocumentMissingDocumentBody[] = |
+ "{" |
+ " \"legal_document_id\":\"doc_id\"," |
+ " \"display_name\":\"display_name\"" |
+ "}"; |
+ |
+static const char kWalletItemsWithRequiredActions[] = |
+ "{" |
+ " \"required_action\":" |
+ " [" |
+ " \"required_action\"" |
+ " ]" |
+ "}"; |
+ |
+static 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)); |
+ if (value.get() && value->IsType(Value::TYPE_DICTIONARY)) { |
+ dict.reset(static_cast<DictionaryValue*>(value.release())); |
+ } |
+ } |
+ scoped_ptr<DictionaryValue> dict; |
+}; |
+ |
+TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionaryMissingStatus) { |
+ SetUpDictionary(kMaskedInstrumentMissingStatus); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionaryMissingType) { |
+ SetUpDictionary(kMaskedInstrumentMissingType); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, |
+ MaskedInstrumentCreateFromDictionaryMissingLastFourDigits) { |
+ SetUpDictionary(kMaskedInstrumentMissingLastFourDigits); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionaryMissingAddress) { |
+ SetUpDictionary(kMaskedInstrumentMissingAddress); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionaryMalformedAddress) { |
+ SetUpDictionary(kMaskedInstrumentMalformedAddress); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionaryMissingObjectId) { |
+ SetUpDictionary(kMaskedInstrumentMissingObjectId); |
+ ASSERT_EQ(NULL, |
+ WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionary) { |
+ 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::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, LegalDocumentCreateFromDictionaryMissingDocId) { |
+ SetUpDictionary(kLegalDocumentMissingDocumentId); |
+ ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, LegalDocumentCreateFromDictionaryMissingDisplayName) { |
+ SetUpDictionary(kLegalDocumentMissingDisplayName); |
+ ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, LegalDocumentCreateFromDictionaryMissingDocBody) { |
+ SetUpDictionary(kLegalDocumentMissingDocumentBody); |
+ ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, LegalDocumentCreateFromDictionary) { |
+ SetUpDictionary(kLegalDocument); |
+ WalletItems::LegalDocument expected("doc_id", "display_name", "doc_body"); |
+ ASSERT_EQ(expected, |
+ *WalletItems::LegalDocument::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateFromDictionaryWithRequiredActions) { |
+ SetUpDictionary(kWalletItemsWithRequiredActions); |
+ std::vector<std::string> required_actions; |
+ required_actions.push_back("required_action"); |
+ WalletItems expected(required_actions, |
+ "", |
+ "", |
+ ""); |
+ ASSERT_EQ(expected, *WalletItems::CreateFromDictionary(dict.get())); |
+} |
+ |
+TEST_F(WalletItemsTest, CreateFromDictionary) { |
+ SetUpDictionary(kWalletItems); |
+ 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 = |
+ new WalletItems::MaskedInstrument("descriptive_name", |
+ WalletItems::MaskedInstrument::VISA, |
+ supported_currencies, |
+ "last_four_digits", |
+ 12, |
+ 2012, |
+ "brand", |
+ address, |
+ WalletItems::MaskedInstrument::VALID, |
+ "object_id"); |
+ WalletItems::LegalDocument* legal_document = |
+ new WalletItems::LegalDocument("doc_id", |
+ "display_name", |
+ "doc_body"); |
+ std::vector<std::string> required_actions; |
+ WalletItems expected(required_actions, |
+ "google_transaction_id", |
+ "default_instrument_id", |
+ "default_address_id"); |
+ expected.AddInstrument(masked_instrument); |
+ // expected.AddAddress(address); |
+ expected.AddLegalDocument(legal_document); |
+ ASSERT_EQ(expected, *WalletItems::CreateFromDictionary(dict.get())); |
+} |
+ |
+} // end wallet namespace |