Chromium Code Reviews| 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, CreateMaskedInstrumentMissingStatus) { | |
| 302 SetUpDictionary(kMaskedInstrumentMissingStatus); | |
| 303 ASSERT_EQ(NULL, | |
| 304 WalletItems::MaskedInstrument::CreateMaskedInstrument(dict.get())); | |
| 305 } | |
| 306 | |
| 307 TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingType) { | |
| 308 SetUpDictionary(kMaskedInstrumentMissingType); | |
| 309 ASSERT_EQ(NULL, | |
| 310 WalletItems::MaskedInstrument::CreateMaskedInstrument(dict.get())); | |
| 311 } | |
| 312 | |
| 313 TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingLastFourDigits) { | |
| 314 SetUpDictionary(kMaskedInstrumentMissingLastFourDigits); | |
| 315 ASSERT_EQ(NULL, | |
| 316 WalletItems::MaskedInstrument::CreateMaskedInstrument(dict.get())); | |
| 317 } | |
| 318 | |
| 319 TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingAddress) { | |
| 320 SetUpDictionary(kMaskedInstrumentMissingAddress); | |
| 321 ASSERT_EQ(NULL, | |
| 322 WalletItems::MaskedInstrument::CreateMaskedInstrument(dict.get())); | |
| 323 } | |
| 324 | |
| 325 TEST_F(WalletItemsTest, CreateMaskedInstrumentMalformedAddress) { | |
| 326 SetUpDictionary(kMaskedInstrumentMalformedAddress); | |
| 327 ASSERT_EQ(NULL, | |
| 328 WalletItems::MaskedInstrument::CreateMaskedInstrument(dict.get())); | |
| 329 } | |
| 330 | |
| 331 TEST_F(WalletItemsTest, CreateMaskedInstrumentMissingObjectId) { | |
| 332 SetUpDictionary(kMaskedInstrumentMissingObjectId); | |
| 333 ASSERT_EQ(NULL, | |
| 334 WalletItems::MaskedInstrument::CreateMaskedInstrument(dict.get())); | |
| 335 } | |
| 336 | |
| 337 TEST_F(WalletItemsTest, CreateMaskedInstrument) { | |
| 338 SetUpDictionary(kMaskedInstrument); | |
| 339 Address* address = new Address("country_code", | |
|
Dan Beam
2012/12/01 01:19:49
does ~MaskedInstrument delete |address|? else the
ahutter
2012/12/01 04:06:51
Yeah, address goes into a scoped_ptr in the constr
| |
| 340 "name", | |
| 341 "address1", | |
| 342 "address2", | |
| 343 "city", | |
| 344 "state", | |
| 345 "postal_code", | |
| 346 "phone_number", | |
| 347 ""); | |
| 348 std::vector<std::string> supported_currencies; | |
| 349 supported_currencies.push_back("currency"); | |
| 350 WalletItems::MaskedInstrument masked_instrument( | |
| 351 "descriptive_name", | |
| 352 WalletItems::MaskedInstrument::VISA, | |
| 353 supported_currencies, | |
| 354 "last_four_digits", | |
| 355 12, | |
| 356 2012, | |
| 357 "brand", | |
| 358 address, | |
| 359 WalletItems::MaskedInstrument::VALID, | |
| 360 "object_id"); | |
| 361 ASSERT_EQ(masked_instrument, | |
| 362 *WalletItems::MaskedInstrument::CreateMaskedInstrument(dict.get())); | |
| 363 } | |
| 364 | |
| 365 TEST_F(WalletItemsTest, CreateLegalDocumentMissingDocId) { | |
| 366 SetUpDictionary(kLegalDocumentMissingDocumentId); | |
| 367 ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateLegalDocument(dict.get())); | |
| 368 } | |
| 369 | |
| 370 TEST_F(WalletItemsTest, CreateLegalDocumentMissingDisplayName) { | |
| 371 SetUpDictionary(kLegalDocumentMissingDisplayName); | |
| 372 ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateLegalDocument(dict.get())); | |
| 373 } | |
| 374 | |
| 375 TEST_F(WalletItemsTest, CreateLegalDocumentMissingDocBody) { | |
| 376 SetUpDictionary(kLegalDocumentMissingDocumentBody); | |
| 377 ASSERT_EQ(NULL, WalletItems::LegalDocument::CreateLegalDocument(dict.get())); | |
| 378 } | |
| 379 | |
| 380 TEST_F(WalletItemsTest, CreateLegalDocument) { | |
| 381 SetUpDictionary(kLegalDocument); | |
| 382 WalletItems::LegalDocument expected("doc_id", "display_name", "doc_body"); | |
| 383 ASSERT_EQ(expected, | |
| 384 *WalletItems::LegalDocument::CreateLegalDocument(dict.get())); | |
| 385 } | |
| 386 | |
| 387 TEST_F(WalletItemsTest, CreateWalletItemsWithRequiredActions) { | |
| 388 SetUpDictionary(kWalletItemsWithRequiredActions); | |
| 389 std::vector<std::string> required_actions; | |
| 390 required_actions.push_back("required_action"); | |
| 391 WalletItems expected(required_actions, | |
|
Dan Beam
2012/12/01 01:19:49
nit:
WalletItems expected(required_actions, "",
ahutter
2012/12/01 04:06:51
Done.
| |
| 392 "", | |
| 393 "", | |
| 394 ""); | |
| 395 ASSERT_EQ(expected, *WalletItems::CreateWalletItems(dict.get())); | |
| 396 } | |
| 397 | |
| 398 TEST_F(WalletItemsTest, CreateWalletItems) { | |
| 399 SetUpDictionary(kWalletItems); | |
| 400 Address* address = new Address("country_code", | |
| 401 "name", | |
| 402 "address1", | |
| 403 "address2", | |
| 404 "city", | |
| 405 "state", | |
| 406 "postal_code", | |
| 407 "phone_number", | |
| 408 ""); | |
| 409 std::vector<std::string> supported_currencies; | |
| 410 supported_currencies.push_back("currency"); | |
| 411 WalletItems::MaskedInstrument* masked_instrument = | |
| 412 new WalletItems::MaskedInstrument("descriptive_name", | |
| 413 WalletItems::MaskedInstrument::VISA, | |
| 414 supported_currencies, | |
| 415 "last_four_digits", | |
| 416 12, | |
| 417 2012, | |
| 418 "brand", | |
| 419 address, | |
| 420 WalletItems::MaskedInstrument::VALID, | |
| 421 "object_id"); | |
| 422 WalletItems::LegalDocument* legal_document = | |
| 423 new WalletItems::LegalDocument("doc_id", | |
| 424 "display_name", | |
| 425 "doc_body"); | |
| 426 std::vector<std::string> required_actions; | |
| 427 WalletItems expected(required_actions, | |
| 428 "google_transaction_id", | |
| 429 "default_instrument_id", | |
| 430 "default_address_id"); | |
| 431 expected.AddInstrument(masked_instrument); | |
| 432 // TODO(ahutter): add an address | |
| 433 // expected.AddAddress(address); | |
|
Dan Beam
2012/12/01 01:19:49
nit: don't keep commented out source code, IMO
ahutter
2012/12/01 04:06:51
Done.
| |
| 434 expected.AddLegalDocument(legal_document); | |
| 435 ASSERT_EQ(expected, *WalletItems::CreateWalletItems(dict.get())); | |
| 436 } | |
| 437 | |
| 438 } // end wallet namespace | |
| OLD | NEW |