Home | History | Annotate | Download | only in wallet
      1 // Copyright 2013 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 "components/autofill/content/browser/wallet/wallet_test_util.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     10 #include "base/strings/string16.h"
     11 #include "base/strings/string_number_conversions.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "base/values.h"
     14 #include "components/autofill/content/browser/wallet/full_wallet.h"
     15 #include "components/autofill/content/browser/wallet/gaia_account.h"
     16 #include "components/autofill/content/browser/wallet/instrument.h"
     17 #include "components/autofill/content/browser/wallet/required_action.h"
     18 #include "components/autofill/content/browser/wallet/wallet_address.h"
     19 
     20 using base::ASCIIToUTF16;
     21 
     22 namespace autofill {
     23 namespace wallet {
     24 
     25 namespace {
     26 
     27 int FutureYear() {
     28   // "In the Year 3000." - Richie "LaBamba" Rosenberg
     29   return 3000;
     30 }
     31 
     32 }  // namespace
     33 
     34 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrumentWithDetails(
     35     const std::string& id,
     36     scoped_ptr<Address> address,
     37     WalletItems::MaskedInstrument::Type type,
     38     WalletItems::MaskedInstrument::Status status) {
     39   return make_scoped_ptr(
     40       new WalletItems::MaskedInstrument(ASCIIToUTF16("descriptive_name"),
     41                                         type,
     42                                         ASCIIToUTF16("1111"),
     43                                         12,
     44                                         FutureYear(),
     45                                         address.Pass(),
     46                                         status,
     47                                         id));
     48 }
     49 
     50 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrumentWithId(
     51     const std::string& id) {
     52   return GetTestMaskedInstrumentWithDetails(
     53       id,
     54       GetTestAddress(),
     55       WalletItems::MaskedInstrument::VISA,
     56       WalletItems::MaskedInstrument::VALID);
     57 }
     58 
     59 scoped_ptr<WalletItems::MaskedInstrument>
     60 GetTestMaskedInstrumentWithIdAndAddress(
     61     const std::string& id, scoped_ptr<Address> address) {
     62   return GetTestMaskedInstrumentWithDetails(
     63       id,
     64       address.Pass(),
     65       WalletItems::MaskedInstrument::VISA,
     66       WalletItems::MaskedInstrument::VALID);
     67 }
     68 
     69 scoped_ptr<GaiaAccount> GetTestGaiaAccount() {
     70   return GaiaAccount::CreateForTesting("user (at) chromium.org",
     71                                        "obfuscated_id",
     72                                        0,
     73                                        true);
     74 }
     75 
     76 std::vector<base::string16> StreetAddress(const std::string& line1,
     77                                           const std::string& line2) {
     78   std::vector<base::string16> street_address;
     79   street_address.push_back(ASCIIToUTF16(line1));
     80   street_address.push_back(ASCIIToUTF16(line2));
     81   return street_address;
     82 }
     83 
     84 scoped_ptr<Address> GetTestAddress() {
     85   return make_scoped_ptr(
     86       new Address("US",
     87                   ASCIIToUTF16("recipient_name"),
     88                   StreetAddress("address_line_1", "address_line_2"),
     89                   ASCIIToUTF16("locality_name"),
     90                   ASCIIToUTF16("dependent_locality_name"),
     91                   ASCIIToUTF16("admin_area_name"),
     92                   ASCIIToUTF16("postal_code_number"),
     93                   ASCIIToUTF16("sorting_code"),
     94                   ASCIIToUTF16("phone_number"),
     95                   std::string(),
     96                   "language_code"));
     97 }
     98 
     99 scoped_ptr<Address> GetTestMinimalAddress() {
    100   scoped_ptr<Address> address = GetTestAddress();
    101   address->set_is_complete_address(false);
    102   return address.Pass();
    103 }
    104 
    105 scoped_ptr<FullWallet> GetTestFullWallet() {
    106   return GetTestFullWalletWithRequiredActions(std::vector<RequiredAction>());
    107 }
    108 
    109 scoped_ptr<FullWallet> GetTestFullWalletWithRequiredActions(
    110     const std::vector<RequiredAction>& actions) {
    111   scoped_ptr<FullWallet> wallet(new FullWallet(FutureYear(),
    112                                                12,
    113                                                "528512",
    114                                                "5ec4feecf9d6",
    115                                                GetTestAddress(),
    116                                                GetTestShippingAddress(),
    117                                                actions));
    118   std::vector<uint8> one_time_pad;
    119   base::HexStringToBytes("5F04A8704183", &one_time_pad);
    120   wallet->set_one_time_pad(one_time_pad);
    121   return wallet.Pass();
    122 }
    123 
    124 scoped_ptr<FullWallet> GetTestFullWalletInstrumentOnly() {
    125   scoped_ptr<FullWallet> wallet(new FullWallet(FutureYear(),
    126                                                12,
    127                                                "528512",
    128                                                "5ec4feecf9d6",
    129                                                GetTestAddress(),
    130                                                scoped_ptr<Address>(),
    131                                                std::vector<RequiredAction>()));
    132   std::vector<uint8> one_time_pad;
    133   base::HexStringToBytes("5F04A8704183", &one_time_pad);
    134   wallet->set_one_time_pad(one_time_pad);
    135   return wallet.Pass();
    136 }
    137 
    138 scoped_ptr<Instrument> GetTestInstrument() {
    139   return make_scoped_ptr(new Instrument(ASCIIToUTF16("4444444444444448"),
    140                                         ASCIIToUTF16("123"),
    141                                         12,
    142                                         FutureYear(),
    143                                         Instrument::VISA,
    144                                         GetTestAddress()));
    145 }
    146 
    147 scoped_ptr<Instrument> GetTestAddressUpgradeInstrument() {
    148   return make_scoped_ptr(new Instrument(base::string16(),
    149                                         base::string16(),
    150                                         12,
    151                                         FutureYear(),
    152                                         Instrument::UNKNOWN,
    153                                         GetTestAddress()));
    154 }
    155 
    156 scoped_ptr<Instrument> GetTestExpirationDateChangeInstrument() {
    157   return make_scoped_ptr(new Instrument(base::string16(),
    158                                         ASCIIToUTF16("123"),
    159                                         12,
    160                                         FutureYear() + 1,
    161                                         Instrument::UNKNOWN,
    162                                         scoped_ptr<Address>()));
    163 }
    164 
    165 scoped_ptr<Instrument> GetTestAddressNameChangeInstrument() {
    166   return make_scoped_ptr(new Instrument(base::string16(),
    167                                         ASCIIToUTF16("123"),
    168                                         12,
    169                                         FutureYear(),
    170                                         Instrument::UNKNOWN,
    171                                         GetTestAddress()));
    172 }
    173 
    174 scoped_ptr<WalletItems::LegalDocument> GetTestLegalDocument() {
    175   base::DictionaryValue dict;
    176   dict.SetString("legal_document_id", "document_id");
    177   dict.SetString("display_name", "display_name");
    178   return wallet::WalletItems::LegalDocument::CreateLegalDocument(dict);
    179 }
    180 
    181 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrument() {
    182   return GetTestMaskedInstrumentWithId("default_instrument_id");
    183 }
    184 
    185 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrumentExpired() {
    186   return GetTestMaskedInstrumentWithDetails(
    187       "default_instrument_id",
    188       GetTestAddress(),
    189       WalletItems::MaskedInstrument::VISA,
    190       WalletItems::MaskedInstrument::EXPIRED);
    191 }
    192 
    193 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrumentInvalid() {
    194   return GetTestMaskedInstrumentWithDetails(
    195       "default_instrument_id",
    196       GetTestAddress(),
    197       WalletItems::MaskedInstrument::VISA,
    198       WalletItems::MaskedInstrument::DECLINED);
    199 }
    200 
    201 scoped_ptr<WalletItems::MaskedInstrument> GetTestMaskedInstrumentAmex(
    202     AmexPermission amex_permission) {
    203   return GetTestMaskedInstrumentWithDetails(
    204       "default_instrument_id",
    205       GetTestAddress(),
    206       WalletItems::MaskedInstrument::AMEX,
    207       amex_permission == AMEX_ALLOWED ?
    208           WalletItems::MaskedInstrument::VALID :
    209           WalletItems::MaskedInstrument::AMEX_NOT_SUPPORTED);
    210 }
    211 
    212 scoped_ptr<WalletItems::MaskedInstrument> GetTestNonDefaultMaskedInstrument() {
    213   return GetTestMaskedInstrumentWithId("instrument_id");
    214 }
    215 
    216 scoped_ptr<Address> GetTestSaveableAddress() {
    217   return make_scoped_ptr(
    218       new Address("US",
    219                   ASCIIToUTF16("save_recipient_name"),
    220                   StreetAddress("save_address_line_1", "save_address_line_2"),
    221                   ASCIIToUTF16("save_locality_name"),
    222                   ASCIIToUTF16("save_dependent_locality_name"),
    223                   ASCIIToUTF16("save_admin_area_name"),
    224                   ASCIIToUTF16("save_postal_code_number"),
    225                   ASCIIToUTF16("save_sorting_code"),
    226                   ASCIIToUTF16("save_phone_number"),
    227                   std::string(),
    228                   "save_language_code"));
    229 }
    230 
    231 scoped_ptr<Address> GetTestShippingAddress() {
    232   return make_scoped_ptr(
    233       new Address("US",
    234                   ASCIIToUTF16("ship_recipient_name"),
    235                   StreetAddress("ship_address_line_1", "ship_address_line_2"),
    236                   ASCIIToUTF16("ship_locality_name"),
    237                   ASCIIToUTF16("ship_dependent_locality_name"),
    238                   ASCIIToUTF16("ship_admin_area_name"),
    239                   ASCIIToUTF16("ship_postal_code_number"),
    240                   ASCIIToUTF16("ship_sorting_code"),
    241                   ASCIIToUTF16("ship_phone_number"),
    242                   "default_address_id",
    243                   "ship_language_code"));
    244 }
    245 
    246 scoped_ptr<Address> GetTestNonDefaultShippingAddress() {
    247   scoped_ptr<Address> address = GetTestShippingAddress();
    248   address->set_object_id("address_id");
    249   return address.Pass();
    250 }
    251 
    252 scoped_ptr<WalletItems> GetTestWalletItemsWithDetails(
    253     const std::vector<RequiredAction>& required_actions,
    254     const std::string& default_instrument_id,
    255     const std::string& default_address_id,
    256     AmexPermission amex_permission) {
    257   return make_scoped_ptr(new wallet::WalletItems(required_actions,
    258                                                  "google_transaction_id",
    259                                                  default_instrument_id,
    260                                                  default_address_id,
    261                                                  amex_permission));
    262 }
    263 
    264 scoped_ptr<WalletItems> GetTestWalletItemsWithRequiredAction(
    265     RequiredAction action) {
    266   std::vector<RequiredAction> required_actions(1, action);
    267   scoped_ptr<WalletItems> items =
    268       GetTestWalletItemsWithDetails(required_actions,
    269                                     "default_instrument_id",
    270                                     "default_address_id",
    271                                     AMEX_ALLOWED);
    272 
    273   if (action != GAIA_AUTH)
    274     items->AddAccount(GetTestGaiaAccount());
    275 
    276   return items.Pass();
    277 }
    278 
    279 scoped_ptr<WalletItems> GetTestWalletItems(AmexPermission amex_permission) {
    280   return GetTestWalletItemsWithDefaultIds("default_instrument_id",
    281                                           "default_address_id",
    282                                           amex_permission);
    283 }
    284 
    285 scoped_ptr<WalletItems> GetTestWalletItemsWithUsers(
    286     const std::vector<std::string>& users, size_t active_index) {
    287   scoped_ptr<WalletItems> items =
    288       GetTestWalletItemsWithDetails(std::vector<RequiredAction>(),
    289                                     "default_instrument_id",
    290                                     "default_address_id",
    291                                     AMEX_ALLOWED);
    292   for (size_t i = 0; i < users.size(); ++i) {
    293     scoped_ptr<GaiaAccount> account(GaiaAccount::CreateForTesting(
    294         users[i], "obfuscated_id", i, i == active_index));
    295     items->AddAccount(account.Pass());
    296   }
    297   return items.Pass();
    298 }
    299 
    300 scoped_ptr<WalletItems> GetTestWalletItemsWithDefaultIds(
    301     const std::string& default_instrument_id,
    302     const std::string& default_address_id,
    303     AmexPermission amex_permission) {
    304   scoped_ptr<WalletItems> items =
    305       GetTestWalletItemsWithDetails(std::vector<RequiredAction>(),
    306                                     default_instrument_id,
    307                                     default_address_id,
    308                                     amex_permission);
    309   items->AddAccount(GetTestGaiaAccount());
    310   return items.Pass();
    311 }
    312 
    313 }  // namespace wallet
    314 }  // namespace autofill
    315