| 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 kMaskedInstrumentMissingObjectId[] = | 
 |  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     "     \"name\":\"name\"," | 
 |  149     "     \"address1\":\"address1\"," | 
 |  150     "     \"address2\":\"address2\"," | 
 |  151     "     \"city\":\"city\"," | 
 |  152     "     \"state\":\"state\"," | 
 |  153     "     \"postal_code\":\"postal_code\"," | 
 |  154     "     \"phone_number\":\"phone_number\"," | 
 |  155     "     \"country_code\":\"country_code\"" | 
 |  156     "   }," | 
 |  157     "   \"status\":\"VALID\"" | 
 |  158     "}"; | 
 |  159  | 
 |  160 static const char kLegalDocument[] = | 
 |  161     "{" | 
 |  162     "   \"legal_document_id\":\"doc_id\"," | 
 |  163     "   \"display_name\":\"display_name\"," | 
 |  164     "   \"document\":\"doc_body\"" | 
 |  165     "}"; | 
 |  166  | 
 |  167 static const char kLegalDocumentMissingDocumentId[] = | 
 |  168     "{" | 
 |  169     "   \"display_name\":\"display_name\"," | 
 |  170     "   \"document\":\"doc_body\"" | 
 |  171     "}"; | 
 |  172  | 
 |  173 static const char kLegalDocumentMissingDisplayName[] = | 
 |  174     "{" | 
 |  175     "   \"legal_document_id\":\"doc_id\"," | 
 |  176     "   \"document\":\"doc_body\"" | 
 |  177     "}"; | 
 |  178  | 
 |  179 static const char kLegalDocumentMissingDocumentBody[] = | 
 |  180     "{" | 
 |  181     "   \"legal_document_id\":\"doc_id\"," | 
 |  182     "   \"display_name\":\"display_name\"" | 
 |  183     "}"; | 
 |  184  | 
 |  185 static const char kWalletItems[] = | 
 |  186     "{" | 
 |  187     "   \"required_action\":" | 
 |  188     "   [" | 
 |  189     "   ]," | 
 |  190     "   \"google_transaction_id\":\"google_transaction_id\"," | 
 |  191     "   \"instrument\":" | 
 |  192     "   [" | 
 |  193     "     {" | 
 |  194     "       \"descriptive_name\":\"descriptive_name\"," | 
 |  195     "       \"type\":\"VISA\"," | 
 |  196     "       \"supported_currency\":" | 
 |  197     "       [" | 
 |  198     "         \"currency\"" | 
 |  199     "       ]," | 
 |  200     "       \"last_four_digits\":\"last_four_digits\"," | 
 |  201     "       \"expiration_month\":12," | 
 |  202     "       \"expiration_year\":2012," | 
 |  203     "       \"brand\":\"brand\"," | 
 |  204     "       \"billing_address\":" | 
 |  205     "       {" | 
 |  206     "         \"name\":\"name\"," | 
 |  207     "         \"address1\":\"address1\"," | 
 |  208     "         \"address2\":\"address2\"," | 
 |  209     "         \"city\":\"city\"," | 
 |  210     "         \"state\":\"state\"," | 
 |  211     "         \"postal_code\":\"postal_code\"," | 
 |  212     "         \"phone_number\":\"phone_number\"," | 
 |  213     "         \"country_code\":\"country_code\"" | 
 |  214     "       }," | 
 |  215     "       \"status\":\"VALID\"," | 
 |  216     "       \"object_id\":\"object_id\"" | 
 |  217     "     }" | 
 |  218     "   ]," | 
 |  219     "   \"default_instrument_id\":\"default_instrument_id\"," | 
 |  220     "   \"address\":" | 
 |  221     "   [" | 
 |  222     "     {" | 
 |  223     "       \"id\":\"id\"," | 
 |  224     "       \"phone_number\":\"phone_number\"," | 
 |  225     "       \"postal_address\":" | 
 |  226     "       {" | 
 |  227     "         \"recipient_name\":\"recipient_name\"," | 
 |  228     "         \"address_line_1\":\"address_line_1\"," | 
 |  229     "         \"address_line_2\":\"address_line_2\"," | 
 |  230     "         \"locality_name\":\"locality_name\"," | 
 |  231     "         \"administrative_area_name\":\"administrative_area_name\"," | 
 |  232     "         \"postal_code_number\":\"postal_code_number\"," | 
 |  233     "         \"country_name_code\":\"country_name_code\"" | 
 |  234     "       }" | 
 |  235     "     }" | 
 |  236     "   ]," | 
 |  237     "   \"default_address_id\":\"default_address_id\"," | 
 |  238     "   \"required_legal_document\":" | 
 |  239     "   [" | 
 |  240     "     {" | 
 |  241     "       \"legal_document_id\":\"doc_id\"," | 
 |  242     "       \"display_name\":\"display_name\"," | 
 |  243     "       \"document\":\"doc_body\"" | 
 |  244     "     }" | 
 |  245     "   ]" | 
 |  246     "}"; | 
 |  247  | 
 |  248 } // end anonymous namespace | 
 |  249  | 
 |  250 namespace wallet { | 
 |  251  | 
 |  252 class WalletItemsTest : public testing::Test { | 
 |  253  public: | 
 |  254   WalletItemsTest() {} | 
 |  255   virtual void SetUp() { | 
 |  256     dict.reset(); | 
 |  257   } | 
 |  258  protected: | 
 |  259   void SetUpDictionary(const std::string& json) { | 
 |  260     scoped_ptr<Value> value(base::JSONReader::Read(json)); | 
 |  261     if (value.get() && value->IsType(Value::TYPE_DICTIONARY)) { | 
 |  262       dict.reset(static_cast<DictionaryValue*>(value.release())); | 
 |  263     } | 
 |  264   } | 
 |  265   scoped_ptr<DictionaryValue> dict; | 
 |  266 }; | 
 |  267  | 
 |  268 TEST_F(WalletItemsTest, MaskedInstrumentFromDictionaryMissingStatus) { | 
 |  269   SetUpDictionary(kMaskedInstrumentMissingStatus); | 
 |  270   ASSERT_EQ(NULL, WalletItems::MaskedInstrument::FromDictionary(dict.get())); | 
 |  271 } | 
 |  272  | 
 |  273 TEST_F(WalletItemsTest, MaskedInstrumentFromDictionaryMissingType) { | 
 |  274   SetUpDictionary(kMaskedInstrumentMissingType); | 
 |  275   ASSERT_EQ(NULL, WalletItems::MaskedInstrument::FromDictionary(dict.get())); | 
 |  276 } | 
 |  277  | 
 |  278 TEST_F(WalletItemsTest, MaskedInstrumentFromDictionaryMissingLastFourDigits) { | 
 |  279   SetUpDictionary(kMaskedInstrumentMissingLastFourDigits); | 
 |  280   ASSERT_EQ(NULL, WalletItems::MaskedInstrument::FromDictionary(dict.get())); | 
 |  281 } | 
 |  282  | 
 |  283 TEST_F(WalletItemsTest, MaskedInstrumentFromDictionaryMissingAddress) { | 
 |  284   SetUpDictionary(kMaskedInstrumentMissingAddress); | 
 |  285   ASSERT_EQ(NULL, WalletItems::MaskedInstrument::FromDictionary(dict.get())); | 
 |  286 } | 
 |  287  | 
 |  288 TEST_F(WalletItemsTest, MaskedInstrumentFromDictionaryMissingObjectId) { | 
 |  289   SetUpDictionary(kMaskedInstrumentMissingObjectId); | 
 |  290   ASSERT_EQ(NULL, WalletItems::MaskedInstrument::FromDictionary(dict.get())); | 
 |  291 } | 
 |  292  | 
 |  293 TEST_F(WalletItemsTest, MaskedInstrumentFromDictionary) { | 
 |  294   SetUpDictionary(kMaskedInstrument); | 
 |  295   Address* address = new Address("country_code", | 
 |  296                                  "name", | 
 |  297                                  "address1", | 
 |  298                                  "address2", | 
 |  299                                  "city", | 
 |  300                                  "state", | 
 |  301                                  "postal_code", | 
 |  302                                  "phone_number", | 
 |  303                                  ""); | 
 |  304   std::vector<std::string> supported_currencies; | 
 |  305   supported_currencies.push_back("currency"); | 
 |  306   WalletItems::MaskedInstrument masked_instrument( | 
 |  307       "descriptive_name", | 
 |  308       WalletItems::MaskedInstrument::VISA, | 
 |  309       supported_currencies, | 
 |  310       "last_four_digits", | 
 |  311       12, | 
 |  312       2012, | 
 |  313       "brand", | 
 |  314       address, | 
 |  315       WalletItems::MaskedInstrument::VALID, | 
 |  316       "object_id"); | 
 |  317   ASSERT_EQ(masked_instrument, | 
 |  318             *WalletItems::MaskedInstrument::FromDictionary(dict.get())); | 
 |  319 } | 
 |  320  | 
 |  321 TEST_F(WalletItemsTest, LegalDocumentFromDictionaryMissingDocId) { | 
 |  322   SetUpDictionary(kLegalDocumentMissingDocumentId); | 
 |  323   ASSERT_EQ(NULL, WalletItems::LegalDocument::FromDictionary(dict.get())); | 
 |  324 } | 
 |  325  | 
 |  326 TEST_F(WalletItemsTest, LegalDocumentFromDictionaryMissingDisplayName) { | 
 |  327   SetUpDictionary(kLegalDocumentMissingDisplayName); | 
 |  328   ASSERT_EQ(NULL, WalletItems::LegalDocument::FromDictionary(dict.get())); | 
 |  329 } | 
 |  330  | 
 |  331 TEST_F(WalletItemsTest, LegalDocumentFromDictionaryMissingDocBody) { | 
 |  332   SetUpDictionary(kLegalDocumentMissingDocumentBody); | 
 |  333   ASSERT_EQ(NULL, WalletItems::LegalDocument::FromDictionary(dict.get())); | 
 |  334 } | 
 |  335  | 
 |  336 TEST_F(WalletItemsTest, LegalDocumentFromDictionary) { | 
 |  337   SetUpDictionary(kLegalDocument); | 
 |  338   WalletItems::LegalDocument expected("doc_id", "display_name", "doc_body"); | 
 |  339   ASSERT_EQ(expected, | 
 |  340             *WalletItems::LegalDocument::FromDictionary(dict.get())); | 
 |  341 } | 
 |  342  | 
 |  343 TEST_F(WalletItemsTest, FromDictionary) { | 
 |  344   SetUpDictionary(kWalletItems); | 
 |  345   Address* address = new Address("country_code", | 
 |  346                                  "name", | 
 |  347                                  "address1", | 
 |  348                                  "address2", | 
 |  349                                  "city", | 
 |  350                                  "state", | 
 |  351                                  "postal_code", | 
 |  352                                  "phone_number", | 
 |  353                                  ""); | 
 |  354   std::vector<std::string> supported_currencies; | 
 |  355   supported_currencies.push_back("currency"); | 
 |  356   WalletItems::MaskedInstrument* masked_instrument = | 
 |  357       new WalletItems::MaskedInstrument("descriptive_name", | 
 |  358                                         WalletItems::MaskedInstrument::VISA, | 
 |  359                                         supported_currencies, | 
 |  360                                         "last_four_digits", | 
 |  361                                         12, | 
 |  362                                         2012, | 
 |  363                                         "brand", | 
 |  364                                         address, | 
 |  365                                         WalletItems::MaskedInstrument::VALID, | 
 |  366                                         "object_id"); | 
 |  367   WalletItems::LegalDocument* legal_document = | 
 |  368       new WalletItems::LegalDocument("doc_id", | 
 |  369                                      "display_name", | 
 |  370                                      "doc_body"); | 
 |  371   std::vector<std::string> required_actions; | 
 |  372   WalletItems expected(required_actions, | 
 |  373                        "google_transaction_id", | 
 |  374                        "default_instrument_id", | 
 |  375                        "default_address_id"); | 
 |  376   expected.AddInstrument(masked_instrument); | 
 |  377   // expected.AddAddress(address); | 
 |  378   expected.AddLegalDocument(legal_document); | 
 |  379   ASSERT_EQ(expected, *WalletItems::FromDictionary(dict.get())); | 
 |  380 } | 
 |  381  | 
 |  382 } // end wallet namespace | 
| OLD | NEW |