OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/json/json_reader.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/values.h" |
| 8 #include "chrome/browser/autofill/wallet/wallet_items.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 static const char kMaskedInstrument[] = |
| 14 "{" |
| 15 " \"descriptive_name\":\"descriptive_name\"," |
| 16 " \"type\":\"VISA\"," |
| 17 " \"supported_currency\":" |
| 18 " [" |
| 19 " \"currency\"" |
| 20 " ]," |
| 21 " \"last_four_digits\":\"last_four_digits\"," |
| 22 " \"expiration_month\":12," |
| 23 " \"expiration_year\":2012," |
| 24 " \"brand\":\"brand\"," |
| 25 " \"billing_address\":" |
| 26 " {" |
| 27 " \"name\":\"name\"," |
| 28 " \"address1\":\"address1\"," |
| 29 " \"address2\":\"address2\"," |
| 30 " \"city\":\"city\"," |
| 31 " \"state\":\"state\"," |
| 32 " \"postal_code\":\"postal_code\"," |
| 33 " \"phone_number\":\"phone_number\"," |
| 34 " \"country_code\":\"country_code\"" |
| 35 " }," |
| 36 " \"status\":\"VALID\"," |
| 37 " \"object_id\":\"object_id\"" |
| 38 "}"; |
| 39 |
| 40 static const char kMaskedInstrumentMissingStatus[] = |
| 41 "{" |
| 42 " \"descriptive_name\":\"descriptive_name\"," |
| 43 " \"type\":\"VISA\"," |
| 44 " \"supported_currency\":" |
| 45 " [" |
| 46 " \"currency\"" |
| 47 " ]," |
| 48 " \"last_four_digits\":\"last_four_digits\"," |
| 49 " \"expiration_month\":12," |
| 50 " \"expiration_year\":2012," |
| 51 " \"brand\":\"brand\"," |
| 52 " \"billing_address\":" |
| 53 " {" |
| 54 " \"name\":\"name\"," |
| 55 " \"address1\":\"address1\"," |
| 56 " \"address2\":\"address2\"," |
| 57 " \"city\":\"city\"," |
| 58 " \"state\":\"state\"," |
| 59 " \"postal_code\":\"postal_code\"," |
| 60 " \"phone_number\":\"phone_number\"," |
| 61 " \"country_code\":\"country_code\"" |
| 62 " }," |
| 63 " \"object_id\":\"object_id\"" |
| 64 "}"; |
| 65 |
| 66 static const char kMaskedInstrumentMissingType[] = |
| 67 "{" |
| 68 " \"descriptive_name\":\"descriptive_name\"," |
| 69 " \"supported_currency\":" |
| 70 " [" |
| 71 " \"currency\"" |
| 72 " ]," |
| 73 " \"last_four_digits\":\"last_four_digits\"," |
| 74 " \"expiration_month\":12," |
| 75 " \"expiration_year\":2012," |
| 76 " \"brand\":\"brand\"," |
| 77 " \"billing_address\":" |
| 78 " {" |
| 79 " \"name\":\"name\"," |
| 80 " \"address1\":\"address1\"," |
| 81 " \"address2\":\"address2\"," |
| 82 " \"city\":\"city\"," |
| 83 " \"state\":\"state\"," |
| 84 " \"postal_code\":\"postal_code\"," |
| 85 " \"phone_number\":\"phone_number\"," |
| 86 " \"country_code\":\"country_code\"" |
| 87 " }," |
| 88 " \"status\":\"VALID\"," |
| 89 " \"object_id\":\"object_id\"" |
| 90 "}"; |
| 91 |
| 92 static const char kMaskedInstrumentMissingLastFourDigits[] = |
| 93 "{" |
| 94 " \"descriptive_name\":\"descriptive_name\"," |
| 95 " \"type\":\"VISA\"," |
| 96 " \"supported_currency\":" |
| 97 " [" |
| 98 " \"currency\"" |
| 99 " ]," |
| 100 " \"expiration_month\":12," |
| 101 " \"expiration_year\":2012," |
| 102 " \"brand\":\"brand\"," |
| 103 " \"billing_address\":" |
| 104 " {" |
| 105 " \"name\":\"name\"," |
| 106 " \"address1\":\"address1\"," |
| 107 " \"address2\":\"address2\"," |
| 108 " \"city\":\"city\"," |
| 109 " \"state\":\"state\"," |
| 110 " \"postal_code\":\"postal_code\"," |
| 111 " \"phone_number\":\"phone_number\"," |
| 112 " \"country_code\":\"country_code\"" |
| 113 " }," |
| 114 " \"status\":\"VALID\"," |
| 115 " \"object_id\":\"object_id\"" |
| 116 "}"; |
| 117 |
| 118 static const char kMaskedInstrumentMissingAddress[] = |
| 119 "{" |
| 120 " \"descriptive_name\":\"descriptive_name\"," |
| 121 " \"type\":\"VISA\"," |
| 122 " \"supported_currency\":" |
| 123 " [" |
| 124 " \"currency\"" |
| 125 " ]," |
| 126 " \"last_four_digits\":\"last_four_digits\"," |
| 127 " \"expiration_month\":12," |
| 128 " \"expiration_year\":2012," |
| 129 " \"brand\":\"brand\"," |
| 130 " \"status\":\"VALID\"," |
| 131 " \"object_id\":\"object_id\"" |
| 132 "}"; |
| 133 |
| 134 static const char kMaskedInstrumentMalformedAddress[] = |
| 135 "{" |
| 136 " \"descriptive_name\":\"descriptive_name\"," |
| 137 " \"type\":\"VISA\"," |
| 138 " \"supported_currency\":" |
| 139 " [" |
| 140 " \"currency\"" |
| 141 " ]," |
| 142 " \"last_four_digits\":\"last_four_digits\"," |
| 143 " \"expiration_month\":12," |
| 144 " \"expiration_year\":2012," |
| 145 " \"brand\":\"brand\"," |
| 146 " \"billing_address\":" |
| 147 " {" |
| 148 " \"address1\":\"address1\"," |
| 149 " \"address2\":\"address2\"," |
| 150 " \"city\":\"city\"," |
| 151 " \"state\":\"state\"," |
| 152 " \"phone_number\":\"phone_number\"," |
| 153 " \"country_code\":\"country_code\"" |
| 154 " }," |
| 155 " \"status\":\"VALID\"," |
| 156 " \"object_id\":\"object_id\"" |
| 157 "}"; |
| 158 |
| 159 static const char kMaskedInstrumentMissingObjectId[] = |
| 160 "{" |
| 161 " \"descriptive_name\":\"descriptive_name\"," |
| 162 " \"type\":\"VISA\"," |
| 163 " \"supported_currency\":" |
| 164 " [" |
| 165 " \"currency\"" |
| 166 " ]," |
| 167 " \"last_four_digits\":\"last_four_digits\"," |
| 168 " \"expiration_month\":12," |
| 169 " \"expiration_year\":2012," |
| 170 " \"brand\":\"brand\"," |
| 171 " \"billing_address\":" |
| 172 " {" |
| 173 " \"name\":\"name\"," |
| 174 " \"address1\":\"address1\"," |
| 175 " \"address2\":\"address2\"," |
| 176 " \"city\":\"city\"," |
| 177 " \"state\":\"state\"," |
| 178 " \"postal_code\":\"postal_code\"," |
| 179 " \"phone_number\":\"phone_number\"," |
| 180 " \"country_code\":\"country_code\"" |
| 181 " }," |
| 182 " \"status\":\"VALID\"" |
| 183 "}"; |
| 184 |
| 185 static const char kLegalDocument[] = |
| 186 "{" |
| 187 " \"legal_document_id\":\"doc_id\"," |
| 188 " \"display_name\":\"display_name\"," |
| 189 " \"document\":\"doc_body\"" |
| 190 "}"; |
| 191 |
| 192 static const char kLegalDocumentMissingDocumentId[] = |
| 193 "{" |
| 194 " \"display_name\":\"display_name\"," |
| 195 " \"document\":\"doc_body\"" |
| 196 "}"; |
| 197 |
| 198 static const char kLegalDocumentMissingDisplayName[] = |
| 199 "{" |
| 200 " \"legal_document_id\":\"doc_id\"," |
| 201 " \"document\":\"doc_body\"" |
| 202 "}"; |
| 203 |
| 204 static const char kLegalDocumentMissingDocumentBody[] = |
| 205 "{" |
| 206 " \"legal_document_id\":\"doc_id\"," |
| 207 " \"display_name\":\"display_name\"" |
| 208 "}"; |
| 209 |
| 210 static const char kWalletItemsWithRequiredActions[] = |
| 211 "{" |
| 212 " \"required_action\":" |
| 213 " [" |
| 214 " \"required_action\"" |
| 215 " ]" |
| 216 "}"; |
| 217 |
| 218 static const char kWalletItems[] = |
| 219 "{" |
| 220 " \"required_action\":" |
| 221 " [" |
| 222 " ]," |
| 223 " \"google_transaction_id\":\"google_transaction_id\"," |
| 224 " \"instrument\":" |
| 225 " [" |
| 226 " {" |
| 227 " \"descriptive_name\":\"descriptive_name\"," |
| 228 " \"type\":\"VISA\"," |
| 229 " \"supported_currency\":" |
| 230 " [" |
| 231 " \"currency\"" |
| 232 " ]," |
| 233 " \"last_four_digits\":\"last_four_digits\"," |
| 234 " \"expiration_month\":12," |
| 235 " \"expiration_year\":2012," |
| 236 " \"brand\":\"brand\"," |
| 237 " \"billing_address\":" |
| 238 " {" |
| 239 " \"name\":\"name\"," |
| 240 " \"address1\":\"address1\"," |
| 241 " \"address2\":\"address2\"," |
| 242 " \"city\":\"city\"," |
| 243 " \"state\":\"state\"," |
| 244 " \"postal_code\":\"postal_code\"," |
| 245 " \"phone_number\":\"phone_number\"," |
| 246 " \"country_code\":\"country_code\"" |
| 247 " }," |
| 248 " \"status\":\"VALID\"," |
| 249 " \"object_id\":\"object_id\"" |
| 250 " }" |
| 251 " ]," |
| 252 " \"default_instrument_id\":\"default_instrument_id\"," |
| 253 " \"address\":" |
| 254 " [" |
| 255 " {" |
| 256 " \"id\":\"id\"," |
| 257 " \"phone_number\":\"phone_number\"," |
| 258 " \"postal_address\":" |
| 259 " {" |
| 260 " \"recipient_name\":\"recipient_name\"," |
| 261 " \"address_line_1\":\"address_line_1\"," |
| 262 " \"address_line_2\":\"address_line_2\"," |
| 263 " \"locality_name\":\"locality_name\"," |
| 264 " \"administrative_area_name\":\"administrative_area_name\"," |
| 265 " \"postal_code_number\":\"postal_code_number\"," |
| 266 " \"country_name_code\":\"country_name_code\"" |
| 267 " }" |
| 268 " }" |
| 269 " ]," |
| 270 " \"default_address_id\":\"default_address_id\"," |
| 271 " \"required_legal_document\":" |
| 272 " [" |
| 273 " {" |
| 274 " \"legal_document_id\":\"doc_id\"," |
| 275 " \"display_name\":\"display_name\"," |
| 276 " \"document\":\"doc_body\"" |
| 277 " }" |
| 278 " ]" |
| 279 "}"; |
| 280 |
| 281 } // end anonymous namespace |
| 282 |
| 283 namespace wallet { |
| 284 |
| 285 class WalletItemsTest : public testing::Test { |
| 286 public: |
| 287 WalletItemsTest() {} |
| 288 virtual void SetUp() { |
| 289 dict.reset(); |
| 290 } |
| 291 protected: |
| 292 void SetUpDictionary(const std::string& json) { |
| 293 scoped_ptr<Value> value(base::JSONReader::Read(json)); |
| 294 if (value.get() && value->IsType(Value::TYPE_DICTIONARY)) { |
| 295 dict.reset(static_cast<DictionaryValue*>(value.release())); |
| 296 } |
| 297 } |
| 298 scoped_ptr<DictionaryValue> dict; |
| 299 }; |
| 300 |
| 301 TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionaryMissingStatus) { |
| 302 SetUpDictionary(kMaskedInstrumentMissingStatus); |
| 303 ASSERT_EQ(NULL, |
| 304 WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
| 305 } |
| 306 |
| 307 TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionaryMissingType) { |
| 308 SetUpDictionary(kMaskedInstrumentMissingType); |
| 309 ASSERT_EQ(NULL, |
| 310 WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
| 311 } |
| 312 |
| 313 TEST_F(WalletItemsTest, |
| 314 MaskedInstrumentCreateFromDictionaryMissingLastFourDigits) { |
| 315 SetUpDictionary(kMaskedInstrumentMissingLastFourDigits); |
| 316 ASSERT_EQ(NULL, |
| 317 WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
| 318 } |
| 319 |
| 320 TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionaryMissingAddress) { |
| 321 SetUpDictionary(kMaskedInstrumentMissingAddress); |
| 322 ASSERT_EQ(NULL, |
| 323 WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
| 324 } |
| 325 |
| 326 TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionaryMalformedAddress) { |
| 327 SetUpDictionary(kMaskedInstrumentMalformedAddress); |
| 328 ASSERT_EQ(NULL, |
| 329 WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
| 330 } |
| 331 |
| 332 TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionaryMissingObjectId) { |
| 333 SetUpDictionary(kMaskedInstrumentMissingObjectId); |
| 334 ASSERT_EQ(NULL, |
| 335 WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
| 336 } |
| 337 |
| 338 TEST_F(WalletItemsTest, MaskedInstrumentCreateFromDictionary) { |
| 339 SetUpDictionary(kMaskedInstrument); |
| 340 Address* address = new Address("country_code", |
| 341 "name", |
| 342 "address1", |
| 343 "address2", |
| 344 "city", |
| 345 "state", |
| 346 "postal_code", |
| 347 "phone_number", |
| 348 ""); |
| 349 std::vector<std::string> supported_currencies; |
| 350 supported_currencies.push_back("currency"); |
| 351 WalletItems::MaskedInstrument masked_instrument( |
| 352 "descriptive_name", |
| 353 WalletItems::MaskedInstrument::VISA, |
| 354 supported_currencies, |
| 355 "last_four_digits", |
| 356 12, |
| 357 2012, |
| 358 "brand", |
| 359 address, |
| 360 WalletItems::MaskedInstrument::VALID, |
| 361 "object_id"); |
| 362 ASSERT_EQ(masked_instrument, |
| 363 *WalletItems::MaskedInstrument::CreateFromDictionary(dict.get())); |
| 364 } |
| 365 |
| 366 TEST_F(WalletItemsTest, LegalDocumentCreateFromDictionaryMissingDocId) { |
| 367 SetUpDictionary(kLegalDocumentMissingDocumentId); |
| 368 ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateFromDictionary(dict.get())); |
| 369 } |
| 370 |
| 371 TEST_F(WalletItemsTest, LegalDocumentCreateFromDictionaryMissingDisplayName) { |
| 372 SetUpDictionary(kLegalDocumentMissingDisplayName); |
| 373 ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateFromDictionary(dict.get())); |
| 374 } |
| 375 |
| 376 TEST_F(WalletItemsTest, LegalDocumentCreateFromDictionaryMissingDocBody) { |
| 377 SetUpDictionary(kLegalDocumentMissingDocumentBody); |
| 378 ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateFromDictionary(dict.get())); |
| 379 } |
| 380 |
| 381 TEST_F(WalletItemsTest, LegalDocumentCreateFromDictionary) { |
| 382 SetUpDictionary(kLegalDocument); |
| 383 WalletItems::LegalDocument expected("doc_id", "display_name", "doc_body"); |
| 384 ASSERT_EQ(expected, |
| 385 *WalletItems::LegalDocument::CreateFromDictionary(dict.get())); |
| 386 } |
| 387 |
| 388 TEST_F(WalletItemsTest, CreateFromDictionaryWithRequiredActions) { |
| 389 SetUpDictionary(kWalletItemsWithRequiredActions); |
| 390 std::vector<std::string> required_actions; |
| 391 required_actions.push_back("required_action"); |
| 392 WalletItems expected(required_actions, |
| 393 "", |
| 394 "", |
| 395 ""); |
| 396 ASSERT_EQ(expected, *WalletItems::CreateFromDictionary(dict.get())); |
| 397 } |
| 398 |
| 399 TEST_F(WalletItemsTest, CreateFromDictionary) { |
| 400 SetUpDictionary(kWalletItems); |
| 401 Address* address = new Address("country_code", |
| 402 "name", |
| 403 "address1", |
| 404 "address2", |
| 405 "city", |
| 406 "state", |
| 407 "postal_code", |
| 408 "phone_number", |
| 409 ""); |
| 410 std::vector<std::string> supported_currencies; |
| 411 supported_currencies.push_back("currency"); |
| 412 WalletItems::MaskedInstrument* masked_instrument = |
| 413 new WalletItems::MaskedInstrument("descriptive_name", |
| 414 WalletItems::MaskedInstrument::VISA, |
| 415 supported_currencies, |
| 416 "last_four_digits", |
| 417 12, |
| 418 2012, |
| 419 "brand", |
| 420 address, |
| 421 WalletItems::MaskedInstrument::VALID, |
| 422 "object_id"); |
| 423 WalletItems::LegalDocument* legal_document = |
| 424 new WalletItems::LegalDocument("doc_id", |
| 425 "display_name", |
| 426 "doc_body"); |
| 427 std::vector<std::string> required_actions; |
| 428 WalletItems expected(required_actions, |
| 429 "google_transaction_id", |
| 430 "default_instrument_id", |
| 431 "default_address_id"); |
| 432 expected.AddInstrument(masked_instrument); |
| 433 // expected.AddAddress(address); |
| 434 expected.AddLegalDocument(legal_document); |
| 435 ASSERT_EQ(expected, *WalletItems::CreateFromDictionary(dict.get())); |
| 436 } |
| 437 |
| 438 } // end wallet namespace |
OLD | NEW |