12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <map>
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <utility>
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/basictypes.h"
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/bind.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/bind_helpers.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/callback.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/command_line.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/guid.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
159ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/run_loop.h"
187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/strings/string_number_conversions.h"
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/strings/string_piece.h"
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
21ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/tuple.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/autofill/autofill_dialog_view.h"
25ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h"
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/autofill/mock_address_validator.h"
27ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/autofill/mock_new_credit_card_bubble_controller.h"
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/autofill/test_generated_credit_card_bubble_controller.h"
29a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/webdata/web_data_service_factory.h"
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
32ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chrome/common/render_messages.h"
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.h"
34ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chrome/test/base/chrome_render_view_host_test_harness.h"
350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/test/base/scoped_testing_local_state.h"
360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/test/base/testing_browser_process.h"
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/testing_profile.h"
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/full_wallet.h"
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/autofill/content/browser/wallet/gaia_account.h"
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/instrument.h"
427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "components/autofill/content/browser/wallet/mock_wallet_client.h"
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_address.h"
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_service_url.h"
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "components/autofill/content/browser/wallet/wallet_test_util.h"
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_metrics.h"
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/autofill/core/browser/autofill_test_utils.h"
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/test_personal_data_manager.h"
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/core/common/autofill_switches.h"
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "components/autofill/core/common/form_data.h"
520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "components/user_prefs/user_prefs.h"
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
54ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/public/test/mock_render_process_host.h"
55d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "google_apis/gaia/google_service_auth_error.h"
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "grit/components_scaled_resources.h"
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "grit/components_strings.h"
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/chromium/chrome_address_validator.h"
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_problem.h"
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/base/l10n/l10n_util.h"
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_WIN)
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/base/win/scoped_ole_initializer.h"
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::ASCIIToUTF16;
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::UTF8ToUTF16;
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace autofill {
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing ::i18n::addressinput::FieldProblemMap;
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using testing::AtLeast;
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using testing::DoAll;
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using testing::Return;
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using testing::SetArgPointee;
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)using testing::_;
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kSourceUrl[] = "http://localbike.shop";
85a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)const char kFakeEmail[] = "user@chromium.org";
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char kFakeFingerprintEncoded[] = "CgVaAwiACA==";
87a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)const char kEditedBillingAddress[] = "123 edited billing address";
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const char* kFieldsFromPage[] =
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    { "email",
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "cc-name",
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "cc-number",
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "cc-exp-month",
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "cc-exp-year",
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "cc-csc",
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "billing name",
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "billing address-line1",
97f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "billing address-level2",
98f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "billing address-level1",
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "billing postal-code",
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "billing country",
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "billing tel",
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "shipping name",
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "shipping address-line1",
104f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "shipping address-level2",
105f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "shipping address-level1",
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "shipping postal-code",
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "shipping country",
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      "shipping tel",
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    };
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const char kSettingsOrigin[] = "Chrome settings";
1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kTestCCNumberAmex[] = "376200000000002";
1127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kTestCCNumberVisa[] = "4111111111111111";
1137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kTestCCNumberMaster[] = "5555555555554444";
1147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kTestCCNumberDiscover[] = "6011111111111117";
1157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kTestCCNumberIncomplete[] = "4111111111";
1167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Credit card number fails Luhn check.
1177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)const char kTestCCNumberInvalid[] = "4111111111111112";
1187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
11958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Copies the initial values from |inputs| into |outputs|.
120f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void CopyInitialValues(const DetailInputs& inputs, FieldValueMap* outputs) {
12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (size_t i = 0; i < inputs.size(); ++i) {
12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const DetailInput& input = inputs[i];
123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    (*outputs)[input.type] = input.initial_value;
12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochscoped_ptr<wallet::WalletItems> CompleteAndValidWalletItems() {
12868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> items =
12968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
130a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  items->AddAccount(wallet::GetTestGaiaAccount());
131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  items->AddInstrument(wallet::GetTestMaskedInstrument());
132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  items->AddAddress(wallet::GetTestShippingAddress());
133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return items.Pass();
134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
13690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)scoped_ptr<risk::Fingerprint> GetFakeFingerprint() {
13790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<risk::Fingerprint> fingerprint(new risk::Fingerprint());
13890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Add some data to the proto, else the encoded content is empty.
13990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  fingerprint->mutable_machine_characteristics()->mutable_screen_size()->
14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      set_width(1024);
14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return fingerprint.Pass();
14290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool HasAnyError(const ValidityMessages& messages, ServerFieldType field) {
1454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return !messages.GetMessageOrDefault(field).text.empty();
1464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool HasUnsureError(const ValidityMessages& messages, ServerFieldType field) {
1494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const ValidityMessage& message = messages.GetMessageOrDefault(field);
1504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return !message.text.empty() && !message.sure;
1514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class TestAutofillDialogView : public AutofillDialogView {
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
15558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  TestAutofillDialogView()
15658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      : updates_started_(0), save_details_locally_checked_(true) {}
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~TestAutofillDialogView() {}
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Show() OVERRIDE {}
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Hide() OVERRIDE {}
1613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void UpdatesStarted() OVERRIDE {
1633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    updates_started_++;
1643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void UpdatesFinished() OVERRIDE {
1673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    updates_started_--;
1683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    EXPECT_GE(updates_started_, 0);
1693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void UpdateNotificationArea() OVERRIDE {
1723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    EXPECT_GE(updates_started_, 1);
1733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void UpdateAccountChooser() OVERRIDE {
1763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    EXPECT_GE(updates_started_, 1);
1773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void UpdateButtonStrip() OVERRIDE {
1803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    EXPECT_GE(updates_started_, 1);
1813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
18358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual void UpdateOverlay() OVERRIDE {
1843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    EXPECT_GE(updates_started_, 1);
1853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
18758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual void UpdateDetailArea() OVERRIDE {
1883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    EXPECT_GE(updates_started_, 1);
1893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual void UpdateSection(DialogSection section) OVERRIDE {
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    section_updates_[section]++;
1933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    EXPECT_GE(updates_started_, 1);
1943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  virtual void UpdateErrorBubble() OVERRIDE {
1974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    EXPECT_GE(updates_started_, 1);
1984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
1994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void FillSection(DialogSection section,
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                           ServerFieldType originating_type) OVERRIDE {}
202f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void GetUserInput(DialogSection section, FieldValueMap* output)
203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      OVERRIDE {
204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    *output = outputs_[section];
205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
207a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetCvc() OVERRIDE { return base::string16(); }
20858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
20958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual bool SaveDetailsLocally() OVERRIDE {
21058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return save_details_locally_checked_;
21158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
21258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const content::NavigationController* ShowSignIn() OVERRIDE {
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void HideSignIn() OVERRIDE {}
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MOCK_METHOD0(ModelChanged, void());
2197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MOCK_METHOD0(UpdateForErrors, void());
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void OnSignInResize(const gfx::Size& pref_size) OVERRIDE {}
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void ValidateSection(DialogSection) OVERRIDE {}
22390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
224f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void SetUserInput(DialogSection section, const FieldValueMap& map) {
225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    outputs_[section] = map;
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void CheckSaveDetailsLocallyCheckbox(bool checked) {
22958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    save_details_locally_checked_ = checked;
23058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
23158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void ClearSectionUpdates() {
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    section_updates_.clear();
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::map<DialogSection, size_t> section_updates() const {
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return section_updates_;
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
241f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  std::map<DialogSection, FieldValueMap> outputs_;
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::map<DialogSection, size_t> section_updates_;
243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  int updates_started_;
24558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool save_details_locally_checked_;
2463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView);
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class TestAutofillDialogController
251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : public AutofillDialogControllerImpl,
252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      public base::SupportsWeakPtr<TestAutofillDialogController> {
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TestAutofillDialogController(
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::WebContents* contents,
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const FormData& form_structure,
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const GURL& source_url,
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const AutofillMetrics& metric_logger,
259f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const AutofillClient::ResultCallback& callback,
260ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      MockNewCreditCardBubbleController* mock_new_card_bubble_controller)
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      : AutofillDialogControllerImpl(contents,
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     form_structure,
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     source_url,
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     callback),
265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        metric_logger_(metric_logger),
2667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        mock_wallet_client_(
267f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            Profile::FromBrowserContext(contents->GetBrowserContext())
268f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                ->GetRequestContext(),
269f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            this,
270f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            source_url),
27158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        mock_new_card_bubble_controller_(mock_new_card_bubble_controller),
27258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        submit_button_delay_count_(0) {}
273ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~TestAutofillDialogController() {}
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual AutofillDialogView* CreateView() OVERRIDE {
277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return new testing::NiceMock<TestAutofillDialogView>();
278c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
279c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void Init(content::BrowserContext* browser_context) {
281f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    test_manager_.Init(
282a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        WebDataServiceFactory::GetAutofillWebDataForProfile(
283a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)            Profile::FromBrowserContext(browser_context),
284a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)            Profile::EXPLICIT_ACCESS),
285f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        user_prefs::UserPrefs::Get(browser_context),
286f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        browser_context->IsOffTheRecord());
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TestAutofillDialogView* GetView() {
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return static_cast<TestAutofillDialogView*>(view());
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TestPersonalDataManager* GetTestingManager() {
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return &test_manager_;
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MockAddressValidator* GetMockValidator() {
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return &mock_validator_;
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  wallet::MockWalletClient* GetTestingWalletClient() {
3027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return &mock_wallet_client_;
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const GURL& open_tab_url() { return open_tab_url_; }
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
30758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void SimulateSigninError() {
30858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    OnWalletSigninError();
30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
31090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Skips past the 2 second wait between FinishSubmit and DoFinishSubmit.
31258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void ForceFinishSubmit() {
31358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    DoFinishSubmit();
31458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
31590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void SimulateSubmitButtonDelayBegin() {
31758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    AutofillDialogControllerImpl::SubmitButtonDelayBegin();
31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
32058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void SimulateSubmitButtonDelayEnd() {
32158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    AutofillDialogControllerImpl::SubmitButtonDelayEndForTesting();
32258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
32358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  using AutofillDialogControllerImpl::
3254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      ClearLastWalletItemsFetchTimestampForTesting;
3264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
32758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Returns the number of times that the submit button was delayed.
32858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  int get_submit_button_delay_count() const {
32958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return submit_button_delay_count_;
330ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
331ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
33290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MOCK_METHOD0(LoadRiskFingerprintData, void());
333a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  using AutofillDialogControllerImpl::AccountChooserModelForTesting;
33490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  using AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData;
335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  using AutofillDialogControllerImpl::IsEditingExistingData;
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  using AutofillDialogControllerImpl::IsManuallyEditingSection;
337a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  using AutofillDialogControllerImpl::IsPayingWithWallet;
3388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  using AutofillDialogControllerImpl::IsSubmitPausedOn;
3390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  using AutofillDialogControllerImpl::NOT_CHECKED;
340a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  using AutofillDialogControllerImpl::popup_input_type;
3410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  using AutofillDialogControllerImpl::SignedInState;
34290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
344a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual PersonalDataManager* GetManager() const OVERRIDE {
345a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return const_cast<TestAutofillDialogController*>(this)->
346a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        GetTestingManager();
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual AddressValidator* GetValidator() OVERRIDE {
3505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return &mock_validator_;
3515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual wallet::WalletClient* GetWalletClient() OVERRIDE {
3547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return &mock_wallet_client_;
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void OpenTabWithUrl(const GURL& url) OVERRIDE {
358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    open_tab_url_ = url;
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
361ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  virtual void ShowNewCreditCardBubble(
362ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      scoped_ptr<CreditCard> new_card,
363ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      scoped_ptr<AutofillProfile> billing_profile) OVERRIDE {
364ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    mock_new_card_bubble_controller_->Show(new_card.Pass(),
365ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                                           billing_profile.Pass());
366ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
367ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
36858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // AutofillDialogControllerImpl calls this method before showing the dialog
36958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // window.
37058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual void SubmitButtonDelayBegin() OVERRIDE {
37158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // Do not delay enabling the submit button in testing.
37258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    submit_button_delay_count_++;
37358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
37458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // To specify our own metric logger.
377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE {
378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return metric_logger_;
379c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
380c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
381c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const AutofillMetrics& metric_logger_;
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TestPersonalDataManager test_manager_;
3837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_;
3845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A mock validator object to prevent network requests and track when
3865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // validation rules are loaded or validation attempts occur.
3875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  testing::NiceMock<MockAddressValidator> mock_validator_;
3885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GURL open_tab_url_;
390ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  MockNewCreditCardBubbleController* mock_new_card_bubble_controller_;
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
39258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The number of times that the submit button was delayed.
39358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  int submit_button_delay_count_;
39458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController);
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
398ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochclass AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness {
3997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch protected:
4007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  AutofillDialogControllerTest(): form_structure_(NULL) {}
4017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // testing::Test implementation:
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetUp() OVERRIDE {
404ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    ChromeRenderViewHostTestHarness::SetUp();
405ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    Reset();
40690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
40790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
40890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void TearDown() OVERRIDE {
4097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (controller_)
41090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      controller_->ViewClosed();
411ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    ChromeRenderViewHostTestHarness::TearDown();
412ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
413ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
414ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  void Reset() {
415ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (controller_)
416ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      controller_->ViewClosed();
417ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
418ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    test_generated_bubble_controller_ =
419ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        new testing::NiceMock<TestGeneratedCreditCardBubbleController>(
420ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch            web_contents());
4218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ASSERT_TRUE(test_generated_bubble_controller_->IsInstalled());
4228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
423ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    mock_new_card_bubble_controller_.reset(
424ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        new MockNewCreditCardBubbleController);
425ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
42658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    profile()->GetPrefs()->ClearPref(::prefs::kAutofillDialogSaveData);
427ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
4280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    // We have to clear the old local state before creating a new one.
4290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_local_state_.reset();
4300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_local_state_.reset(new ScopedTestingLocalState(
4310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        TestingBrowserProcess::GetGlobal()));
4320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
433ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    SetUpControllerWithFormData(DefaultFormData());
43490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
43590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
436868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FormData DefaultFormData() {
437868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    FormData form_data;
438868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    for (size_t i = 0; i < arraysize(kFieldsFromPage); ++i) {
439868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      FormFieldData field;
440868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      field.autocomplete_attribute = kFieldsFromPage[i];
441868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      form_data.fields.push_back(field);
442868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
443868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return form_data;
444868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
445868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
4460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Creates a new controller for |form_data|.
4470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void ResetControllerWithFormData(const FormData& form_data) {
4487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (controller_)
44990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      controller_->ViewClosed();
45090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
451f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    AutofillClient::ResultCallback callback =
452c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        base::Bind(&AutofillDialogControllerTest::FinishedCallback,
453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   base::Unretained(this));
45490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    controller_ = (new testing::NiceMock<TestAutofillDialogController>(
455ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch        web_contents(),
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        form_data,
4575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        GURL(kSourceUrl),
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        metric_logger_,
459ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        callback,
460ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch        mock_new_card_bubble_controller_.get()))->AsWeakPtr();
461c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    controller_->Init(profile());
4620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
4630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Creates a new controller for |form_data| and sets up some initial wallet
4650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // data for it.
4660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void SetUpControllerWithFormData(const FormData& form_data) {
4670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    ResetControllerWithFormData(form_data);
468a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    controller()->Show();
4690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (controller() &&
4700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        !profile()->GetPrefs()->GetBoolean(
471f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            ::prefs::kAutofillDialogPayWithoutWallet)) {
472116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      EXPECT_CALL(*controller()->GetTestingWalletClient(),
473116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                  GetWalletItems(_, _));
474a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      controller()->OnDidFetchWalletCookieValue(std::string());
475f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
476f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Fills the inputs in SECTION_CC with data.
480c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void FillCreditCardInputs() {
481f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FieldValueMap cc_outputs;
482c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const DetailInputs& cc_inputs =
483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        controller()->RequestedFieldsForSection(SECTION_CC);
484c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (size_t i = 0; i < cc_inputs.size(); ++i) {
485f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      cc_outputs[cc_inputs[i].type] = cc_inputs[i].type == CREDIT_CARD_NUMBER ?
4867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          ASCIIToUTF16(kTestCCNumberVisa) : ASCIIToUTF16("11");
487c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
488c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    controller()->GetView()->SetUserInput(SECTION_CC, cc_outputs);
489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Fills the inputs in SECTION_CC_BILLING with valid data.
4928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void FillCCBillingInputs() {
493f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FieldValueMap outputs;
4948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const DetailInputs& inputs =
4958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
4968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    AutofillProfile full_profile(test::GetVerifiedProfile());
4978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    CreditCard full_card(test::GetCreditCard());
4988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    for (size_t i = 0; i < inputs.size(); ++i) {
499f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const ServerFieldType type = inputs[i].type;
500f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
5018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
502f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (outputs[type].empty())
503f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        outputs[type] = full_card.GetInfo(AutofillType(type), "en-US");
5048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
5058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
5068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
5078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
508d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Activates the 'Add new foo' option from the |section|'s suggestions
509d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // dropdown and fills the |section|'s inputs with the data from the
510d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // |data_model|.  If |section| is SECTION_CC, also fills in '123' for the CVC.
511d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  void FillInputs(DialogSection section, const AutofillDataModel& data_model) {
512d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    // Select the 'Add new foo' option.
513d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ui::MenuModel* model = GetMenuModelForSection(section);
5144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (model)
5154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      model->ActivatedAt(model->GetItemCount() - 2);
516d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
517d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    // Fill the inputs.
518f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FieldValueMap outputs;
519d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const DetailInputs& inputs =
520d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        controller()->RequestedFieldsForSection(section);
521d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    for (size_t i = 0; i < inputs.size(); ++i) {
522d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      ServerFieldType type = inputs[i].type;
523d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      base::string16 output;
524d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      if (type == CREDIT_CARD_VERIFICATION_CODE)
525d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        output = ASCIIToUTF16("123");
526d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      else
527d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        output = data_model.GetInfo(AutofillType(type), "en-US");
528f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      outputs[inputs[i].type] = output;
529d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    }
530d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    controller()->GetView()->SetUserInput(section, outputs);
531d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
532d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<DialogNotification> NotificationsOfType(
534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DialogNotification::Type type) {
535c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::vector<DialogNotification> right_type;
536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::vector<DialogNotification>& notifications =
537c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        controller()->CurrentNotifications();
538c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    for (size_t i = 0; i < notifications.size(); ++i) {
539c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (notifications[i].type() == type)
540c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        right_type.push_back(notifications[i]);
541c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
542c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return right_type;
543c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
544c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
545c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SwitchToAutofill() {
5464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ui::MenuModel* model = controller_->MenuModelForAccountChooser();
5474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    model->ActivatedAt(model->GetItemCount() - 1);
548c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
549c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
550c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SwitchToWallet() {
5514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    controller_->MenuModelForAccountChooser()->ActivatedAt(0);
552c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
553c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
554868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void SimulateSigninError() {
555868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    controller_->SimulateSigninError();
556868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
557868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
55890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void UseBillingForShipping() {
55990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(0);
56090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
56190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
562116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::string16 ValidateCCNumber(DialogSection section,
563116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                  const std::string& cc_number,
564116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                  bool should_pass) {
565f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FieldValueMap outputs;
5665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
567a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    outputs[CREDIT_CARD_NUMBER] = UTF8ToUTF16(cc_number);
5684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessages messages =
5694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        controller()->InputsAreValid(section, outputs);
5704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    EXPECT_EQ(should_pass, !messages.HasSureError(CREDIT_CARD_NUMBER));
571116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return messages.GetMessageOrDefault(CREDIT_CARD_NUMBER).text;
5727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
5737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
574eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void SubmitWithWalletItems(scoped_ptr<wallet::WalletItems> wallet_items) {
575eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    controller()->OnDidGetWalletItems(wallet_items.Pass());
576eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AcceptAndLoadFakeFingerprint();
577eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
578eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
579eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void AcceptAndLoadFakeFingerprint() {
580eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    controller()->OnAccept();
581eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass());
582eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
583eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
58458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Returns true if the given |section| contains a field of the given |type|.
58558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool SectionContainsField(DialogSection section, ServerFieldType type) {
58658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const DetailInputs& inputs =
58758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        controller()->RequestedFieldsForSection(section);
58858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    for (DetailInputs::const_iterator it = inputs.begin(); it != inputs.end();
58958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)         ++it) {
59058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (it->type == type)
59158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        return true;
59258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
59358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return false;
59458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
59558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
596d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  SuggestionsMenuModel* GetMenuModelForSection(DialogSection section) {
597d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ui::MenuModel* model = controller()->MenuModelForSection(section);
598d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    return static_cast<SuggestionsMenuModel*>(model);
599d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  }
600d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
6015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SubmitAndVerifyShippingAndBillingResults() {
6025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Test after setting use billing for shipping.
6035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    UseBillingForShipping();
6045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    controller()->OnAccept();
6065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_EQ(20U, form_structure()->field_count());
6085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(ADDRESS_HOME_COUNTRY,
6095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              form_structure()->field(11)->Type().GetStorableType());
6105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(11)->Type().group());
6115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(ADDRESS_HOME_COUNTRY,
6125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              form_structure()->field(18)->Type().GetStorableType());
6135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(ADDRESS_HOME, form_structure()->field(18)->Type().group());
6145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::string16 billing_country = form_structure()->field(11)->value;
6155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(2U, billing_country.size());
6165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::string16 shipping_country = form_structure()->field(18)->value;
6175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(2U, shipping_country.size());
6185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_FALSE(billing_country.empty());
6195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_FALSE(shipping_country.empty());
6205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(billing_country, shipping_country);
6215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(CREDIT_CARD_NAME,
6235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              form_structure()->field(1)->Type().GetStorableType());
6245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::string16 cc_name = form_structure()->field(1)->value;
6255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType());
6265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group());
6275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::string16 billing_name = form_structure()->field(6)->value;
6285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType());
6295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(NAME, form_structure()->field(13)->Type().group());
6305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::string16 shipping_name = form_structure()->field(13)->value;
6315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_FALSE(cc_name.empty());
6335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_FALSE(billing_name.empty());
6345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_FALSE(shipping_name.empty());
6355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(cc_name, billing_name);
6365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(cc_name, shipping_name);
6375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
6385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
639ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  TestAutofillDialogController* controller() { return controller_.get(); }
6402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
641c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const FormStructure* form_structure() { return form_structure_; }
642c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
643ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  TestGeneratedCreditCardBubbleController* test_generated_bubble_controller() {
644ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    return test_generated_bubble_controller_;
645ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  }
646ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
647ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  const MockNewCreditCardBubbleController* mock_new_card_bubble_controller() {
648ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    return mock_new_card_bubble_controller_.get();
6497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
6507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
6512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
652f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void FinishedCallback(AutofillClient::RequestAutocompleteResult result,
653f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                        const base::string16& debug_message,
654f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                        const FormStructure* form_structure) {
655c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    form_structure_ = form_structure;
656c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
657c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
658c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_WIN)
6591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // http://crbug.com/227221
6601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ui::ScopedOleInitializer ole_initializer_;
661c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
662c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The controller owns itself.
664c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::WeakPtr<TestAutofillDialogController> controller_;
6652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Must outlive the controller.
6672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AutofillMetrics metric_logger_;
6682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
669c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returned when the dialog closes successfully.
670c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const FormStructure* form_structure_;
6717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
6727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Used to monitor if the Autofill credit card bubble is shown. Owned by
673ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // |web_contents()|.
674ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  TestGeneratedCreditCardBubbleController* test_generated_bubble_controller_;
675ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
676ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // Used to record when new card bubbles would show. Created in |Reset()|.
677ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  scoped_ptr<MockNewCreditCardBubbleController>
678ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      mock_new_card_bubble_controller_;
6790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
6800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<ScopedTestingLocalState> scoped_local_state_;
6812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
6822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
6842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6850529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochTEST_F(AutofillDialogControllerTest, RefuseToShowWithNoAutocompleteAttributes) {
6860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  FormFieldData email_field;
6870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  email_field.name = ASCIIToUTF16("email");
6880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  FormFieldData cc_field;
6890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cc_field.name = ASCIIToUTF16("cc");
6900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  FormFieldData billing_field;
6910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  billing_field.name = ASCIIToUTF16("billing name");
6920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  FormData form_data;
6940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  form_data.fields.push_back(email_field);
6950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  form_data.fields.push_back(cc_field);
6960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  form_data.fields.push_back(billing_field);
6970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  SetUpControllerWithFormData(form_data);
6990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_FALSE(controller());
7000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
7010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
7020529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochTEST_F(AutofillDialogControllerTest, RefuseToShowWithNoCcField) {
7030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  FormFieldData shipping_tel;
7040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  shipping_tel.autocomplete_attribute = "shipping tel";
7050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
7060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  FormData form_data;
7070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  form_data.fields.push_back(shipping_tel);
7080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
7090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  SetUpControllerWithFormData(form_data);
7100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_FALSE(controller());
7110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
7120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Any cc- field will do.
7130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  FormFieldData cc_field;
7140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cc_field.autocomplete_attribute = "cc-csc";
7150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  form_data.fields.push_back(cc_field);
7160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
7170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  SetUpControllerWithFormData(form_data);
7180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_TRUE(controller());
7190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
7200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
7214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Ensure the default ValidityMessage has the expected values.
7224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, DefaultValidityMessage) {
7234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ValidityMessages messages;
7244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ValidityMessage message = messages.GetMessageOrDefault(UNKNOWN_TYPE);
7254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(message.sure);
7264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_TRUE(message.text.empty());
7274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
7284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
7292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This test makes sure nothing falls over when fields are being validity-
7302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// checked.
7312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, ValidityCheck) {
732868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
733868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DialogSection section = static_cast<DialogSection>(i);
7342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const DetailInputs& shipping_inputs =
7352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        controller()->RequestedFieldsForSection(section);
7362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (DetailInputs::const_iterator iter = shipping_inputs.begin();
7372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         iter != shipping_inputs.end(); ++iter) {
738a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      controller()->InputValidityMessage(section, iter->type, base::string16());
7392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
7402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
7412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
74390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Test for phone number validation.
74490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, PhoneNumberValidation) {
745f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Construct FieldValueMap from existing data.
74690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SwitchToAutofill();
74790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
748868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (size_t i = 0; i < 2; ++i) {
7493240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    ServerFieldType phone = i == 0 ? PHONE_HOME_WHOLE_NUMBER :
7503240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch                                     PHONE_BILLING_WHOLE_NUMBER;
7513240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    ServerFieldType address = i == 0 ? ADDRESS_HOME_COUNTRY :
7523240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch                                       ADDRESS_BILLING_COUNTRY;
753868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DialogSection section = i == 0 ? SECTION_SHIPPING : SECTION_BILLING;
75490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
755f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    FieldValueMap outputs;
756868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const DetailInputs& inputs =
757868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        controller()->RequestedFieldsForSection(section);
75858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    AutofillProfile full_profile(test::GetVerifiedProfile());
75958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    for (size_t i = 0; i < inputs.size(); ++i) {
760f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const ServerFieldType type = inputs[i].type;
761f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
76258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
76358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
764868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Make sure country is United States.
765a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    outputs[address] = ASCIIToUTF16("United States");
766868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
767868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Existing data should have no errors.
7684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ValidityMessages messages = controller()->InputsAreValid(section, outputs);
7694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    EXPECT_FALSE(HasAnyError(messages, phone));
770868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // Input an empty phone number.
772a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    outputs[phone] = base::string16();
7734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages = controller()->InputsAreValid(section, outputs);
7744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    EXPECT_TRUE(HasUnsureError(messages, phone));
775868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
776868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Input an invalid phone number.
777a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    outputs[phone] = ASCIIToUTF16("ABC");
7784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages = controller()->InputsAreValid(section, outputs);
7794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    EXPECT_TRUE(messages.HasSureError(phone));
780868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
781868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Input a local phone number.
782a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    outputs[phone] = ASCIIToUTF16("2155546699");
7834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages = controller()->InputsAreValid(section, outputs);
7844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    EXPECT_FALSE(HasAnyError(messages, phone));
785868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
786868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Input an invalid local phone number.
787a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    outputs[phone] = ASCIIToUTF16("215554669");
7884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages = controller()->InputsAreValid(section, outputs);
7894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    EXPECT_TRUE(messages.HasSureError(phone));
790868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
791868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Input an international phone number.
792a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    outputs[phone] = ASCIIToUTF16("+33 892 70 12 39");
7934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages = controller()->InputsAreValid(section, outputs);
7944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    EXPECT_FALSE(HasAnyError(messages, phone));
795868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
796868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Input an invalid international phone number.
797a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    outputs[phone] = ASCIIToUTF16("+112333 892 70 12 39");
7984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    messages = controller()->InputsAreValid(section, outputs);
7994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    EXPECT_TRUE(messages.HasSureError(phone));
80023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
80123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    // Input a valid Canadian number.
80223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    outputs[phone] = ASCIIToUTF16("+1 506 887 1234");
80323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    messages = controller()->InputsAreValid(section, outputs);
80423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    EXPECT_FALSE(HasAnyError(messages, phone));
80523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
80623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    // Input a valid Canadian number without the country code.
80723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    outputs[phone] = ASCIIToUTF16("506 887 1234");
80823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    messages = controller()->InputsAreValid(section, outputs);
80923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    EXPECT_TRUE(HasAnyError(messages, phone));
81023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
81123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    // Input a valid Canadian toll-free number.
81223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    outputs[phone] = ASCIIToUTF16("310 1234");
81323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    messages = controller()->InputsAreValid(section, outputs);
81423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    EXPECT_TRUE(HasAnyError(messages, phone));
815868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
81690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
81790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
818a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, ExpirationDateValidity) {
819a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  ui::ComboboxModel* exp_year_model =
820a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR);
821a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  ui::ComboboxModel* exp_month_model =
822a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH);
823a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
824a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  base::string16 default_year_value =
825a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      exp_year_model->GetItemAt(exp_year_model->GetDefaultIndex());
82658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::string16 default_month_value =
82758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      exp_month_model->GetItemAt(exp_month_model->GetDefaultIndex());
828a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
829a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  base::string16 other_year_value =
830a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      exp_year_model->GetItemAt(exp_year_model->GetItemCount() - 1);
831a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  base::string16 other_month_value =
832a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      exp_month_model->GetItemAt(exp_month_model->GetItemCount() - 1);
833a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
834a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  FieldValueMap outputs;
8355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
836a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[CREDIT_CARD_EXP_MONTH] = default_month_value;
837a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value;
838a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
8394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Expiration default values generate unsure validation errors (but not sure).
8404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ValidityMessages messages = controller()->InputsAreValid(SECTION_CC_BILLING,
8414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                           outputs);
8424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
8434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
8444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
8454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Expiration date with default month fails.
846a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = other_year_value;
8474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
8484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
8494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
8504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
8514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Expiration date with default year fails.
852a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[CREDIT_CARD_EXP_MONTH] = other_month_value;
853a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value;
8544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
8554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
8564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
857a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
858a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
8597dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(AutofillDialogControllerTest, BillingNameValidation) {
860f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Construct FieldValueMap from AutofillProfile data.
86190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SwitchToAutofill();
86290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
863a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  FieldValueMap outputs;
8645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
8655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Input an empty billing name.
867a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[NAME_BILLING_FULL] = base::string16();
8684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ValidityMessages messages = controller()->InputsAreValid(SECTION_BILLING,
8694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                           outputs);
8704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL));
87190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Input a non-empty billing name.
873a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob");
8744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  messages = controller()->InputsAreValid(SECTION_BILLING, outputs);
8754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
87690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
87790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Switch to Wallet which only considers names with with at least two names to
87890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // be valid.
87990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SwitchToWallet();
88090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
88190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Setup some wallet state.
88268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
88368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
88490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
88590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Input an empty billing name. Data source should not change this behavior.
8875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  outputs[NAME_BILLING_FULL] = base::string16();
8885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
8894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL));
89090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Input a one name billing name. Wallet does not currently support this.
8925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob");
8935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
8944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_TRUE(messages.HasSureError(NAME_BILLING_FULL));
89590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Input a two name billing name.
8975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob Barker");
8985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
8994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
90090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Input a more than two name billing name.
9025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  outputs[NAME_BILLING_FULL] = ASCIIToUTF16("John Jacob Jingleheimer Schmidt"),
9035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
9044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
90590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Input a billing name with lots of crazy whitespace.
9075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  outputs[NAME_BILLING_FULL] =
90858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      ASCIIToUTF16("     \\n\\r John \\n  Jacob Jingleheimer \\t Schmidt  "),
9095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
9104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
91190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
91290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, CreditCardNumberValidation) {
914f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Construct FieldValueMap from AutofillProfile data.
9157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  SwitchToAutofill();
9167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
9177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Should accept AMEX, Visa, Master and Discover.
9187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
9197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true);
9207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, true);
9217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ValidateCCNumber(SECTION_CC, kTestCCNumberAmex, true);
9227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ValidateCCNumber(SECTION_CC, kTestCCNumberIncomplete, false);
9237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ValidateCCNumber(SECTION_CC, kTestCCNumberInvalid, false);
9247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
9257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Switch to Wallet which will not accept AMEX.
9267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  SwitchToWallet();
9277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
92868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Setup some wallet state on a merchant for which Wallet doesn't
92968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // support AMEX.
93068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  controller()->OnDidGetWalletItems(
93168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
9327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
9337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Should accept Visa, Master and Discover, but not AMEX.
9347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true);
9357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true);
9367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
937116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(l10n_util::GetStringUTF16(
938116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                IDS_AUTOFILL_CREDIT_CARD_NOT_SUPPORTED_BY_WALLET_FOR_MERCHANT),
939116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, false));
940116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(
941116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      l10n_util::GetStringUTF16(
942116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER),
943116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false));
944116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(
945116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      l10n_util::GetStringUTF16(
946116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER),
947116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false));
94868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
94968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Setup some wallet state on a merchant for which Wallet supports AMEX.
95068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  controller()->OnDidGetWalletItems(
95168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_ALLOWED));
95268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
95368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Should accept Visa, Master, Discover, and AMEX.
95468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true);
95568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true);
95668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
95768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, true);
95868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false);
95968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false);
9607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
9617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
9622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, AutofillProfiles) {
9634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
9642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::MenuModel* shipping_model =
9652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_SHIPPING);
9662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Since the PersonalDataManager is empty, this should only have the
967c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // "use billing", "add new" and "manage" menu items.
96890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ASSERT_TRUE(shipping_model);
969c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(3, shipping_model->GetItemCount());
970c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // On the other hand, the other models should be NULL when there's no
971c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // suggestion.
972c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
973c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING));
9742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
97590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);
9762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Empty profiles are ignored.
978c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillProfile empty_profile(base::GenerateGUID(), kSettingsOrigin);
9792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  empty_profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
9802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&empty_profile);
9812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
98290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ASSERT_TRUE(shipping_model);
983c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(3, shipping_model->GetItemCount());
9842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
98590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // An otherwise full but unverified profile should be ignored.
986c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AutofillProfile full_profile(test::GetFullProfile());
98790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  full_profile.set_origin("https://www.example.com");
988a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
9892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
9902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
99190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ASSERT_TRUE(shipping_model);
99290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(3, shipping_model->GetItemCount());
99390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
99490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // A full, verified profile should be picked up.
99590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile verified_profile(test::GetVerifiedProfile());
996a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  verified_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
99790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
99890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
99990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ASSERT_TRUE(shipping_model);
1000c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(4, shipping_model->GetItemCount());
10012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
10022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1003a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Checks that a valid profile is selected by default, but if invalid is
1004a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// popped into edit mode.
1005a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, AutofillProfilesPopInvalidIntoEdit) {
1006a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SwitchToAutofill();
1007a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SuggestionsMenuModel* shipping_model =
1008a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      GetMenuModelForSection(SECTION_SHIPPING);
1009a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(3, shipping_model->GetItemCount());
1010a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // "Same as billing" is selected.
1011a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1012a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1013a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1014a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AutofillProfile verified_profile(test::GetVerifiedProfile());
1015a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
1016a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(4, shipping_model->GetItemCount());
1017a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1018a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1019a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1020a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Now make up a problem and make sure the profile isn't in the list.
1021a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Reset();
1022a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SwitchToAutofill();
1023116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FieldProblemMap problems;
1024116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  problems.insert(std::make_pair(::i18n::addressinput::POSTAL_CODE,
1025116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 ::i18n::addressinput::MISMATCHING_VALUE));
1026a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_CALL(*controller()->GetMockValidator(),
1027a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)              ValidateAddress(CountryCodeMatcher("US"), _, _)).
1028a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      WillRepeatedly(DoAll(SetArgPointee<2>(problems),
1029a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                           Return(AddressValidator::SUCCESS)));
1030a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1031a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
1032a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
1033a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(4, shipping_model->GetItemCount());
1034a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1035a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1036a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
1037a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1038a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Makes sure suggestion profiles are re-validated when validation rules load.
1039a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, AutofillProfilesRevalidateAfterRulesLoad) {
1040a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SwitchToAutofill();
1041a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SuggestionsMenuModel* shipping_model =
1042a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      GetMenuModelForSection(SECTION_SHIPPING);
1043a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(3, shipping_model->GetItemCount());
1044a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // "Same as billing" is selected.
1045a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1046a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1047a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AutofillProfile verified_profile(test::GetVerifiedProfile());
1048a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
1049a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(4, shipping_model->GetItemCount());
1050a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1051a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1052a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1053116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FieldProblemMap problems;
1054116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  problems.insert(std::make_pair(::i18n::addressinput::POSTAL_CODE,
1055116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 ::i18n::addressinput::MISMATCHING_VALUE));
1056a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_CALL(*controller()->GetMockValidator(),
1057a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)              ValidateAddress(CountryCodeMatcher("US"), _, _)).
1058a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      WillRepeatedly(DoAll(SetArgPointee<2>(problems),
1059a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                           Return(AddressValidator::SUCCESS)));
1060a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1061a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->OnAddressValidationRulesLoaded("US", true);
1062a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(4, shipping_model->GetItemCount());
1063a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
1064a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1065a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
1066a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
106790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Makes sure that the choice of which Autofill profile to use for each section
106890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is sticky.
106990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, AutofillProfileDefaults) {
10704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
1071d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  AutofillProfile profile(test::GetVerifiedProfile());
1072d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  AutofillProfile profile2(test::GetVerifiedProfile2());
1073d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&profile);
1074d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&profile2);
107590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
107690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Until a selection has been made, the default shipping suggestion is the
107790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // first one (after "use billing").
1078d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  SuggestionsMenuModel* shipping_model =
1079d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      GetMenuModelForSection(SECTION_SHIPPING);
108090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(1, shipping_model->checked_item());
108190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
108290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (int i = 2; i >= 0; --i) {
1083d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
108490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    shipping_model->ExecuteCommand(i, 0);
108590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    FillCreditCardInputs();
108690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    controller()->OnAccept();
108790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1088ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    Reset();
1089d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    controller()->GetTestingManager()->AddTestingProfile(&profile);
1090d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    controller()->GetTestingManager()->AddTestingProfile(&profile2);
1091d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
109290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    EXPECT_EQ(i, shipping_model->checked_item());
109390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
109490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
109590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Try again, but don't add the default profile to the PDM. The dialog
109690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // should fall back to the first profile.
109790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  shipping_model->ExecuteCommand(2, 0);
109890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  FillCreditCardInputs();
109990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnAccept();
1100ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  Reset();
1101d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&profile);
1102d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
110390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(1, shipping_model->checked_item());
110490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
110590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Makes sure that a newly added Autofill profile becomes set as the default
1107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// choice for the next run.
1108d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, NewAutofillProfileIsDefault) {
1109d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  SwitchToAutofill();
1110d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1111d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  AutofillProfile profile(test::GetVerifiedProfile());
1112d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CreditCard credit_card(test::GetVerifiedCreditCard());
1113d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&profile);
1114d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1115d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1116d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Until a selection has been made, the default suggestion is the first one.
1117d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // For the shipping section, this follows the "use billing" suggestion.
1118d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(0, GetMenuModelForSection(SECTION_CC)->checked_item());
1119d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(1, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
1120d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1121d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Fill in the shipping and credit card sections with new data.
1122d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  AutofillProfile new_profile(test::GetVerifiedProfile2());
1123d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CreditCard new_credit_card(test::GetVerifiedCreditCard2());
1124d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FillInputs(SECTION_SHIPPING, new_profile);
1125d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FillInputs(SECTION_CC, new_credit_card);
1126d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
1127d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->OnAccept();
1128d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1129d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Update the |new_profile| and |new_credit_card|'s guids to the saved ones.
1130d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  new_profile.set_guid(
1131d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      controller()->GetTestingManager()->imported_profile().guid());
1132d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  new_credit_card.set_guid(
1133d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      controller()->GetTestingManager()->imported_credit_card().guid());
1134d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1135d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Reload the dialog. The newly added address and credit card should now be
1136d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // set as the defaults.
1137d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  Reset();
1138d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&profile);
1139d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&new_profile);
1140d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1141d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&new_credit_card);
1142d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1143d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Until a selection has been made, the default suggestion is the first one.
1144d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // For the shipping section, this follows the "use billing" suggestion.
1145d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(1, GetMenuModelForSection(SECTION_CC)->checked_item());
1146d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
1147d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
1148d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
11492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, AutofillProfileVariants) {
11504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
11518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), ModelChanged());
115258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ui::MenuModel* shipping_model =
115358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_SHIPPING);
115458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_TRUE(!!shipping_model);
115558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(3, shipping_model->GetItemCount());
11562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set up some variant data.
115890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile full_profile(test::GetVerifiedProfile());
11595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<base::string16> names;
11602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  names.push_back(ASCIIToUTF16("John Doe"));
11612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  names.push_back(ASCIIToUTF16("Jane Doe"));
1162d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  full_profile.SetRawMultiInfo(NAME_FULL, names);
11635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<base::string16> emails;
116458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  emails.push_back(ASCIIToUTF16(kFakeEmail));
116558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  emails.push_back(ASCIIToUTF16("admin@example.com"));
11662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  full_profile.SetRawMultiInfo(EMAIL_ADDRESS, emails);
11672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
116858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Non-default variants are ignored by the dialog.
11692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(4, shipping_model->GetItemCount());
117190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
117290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1173eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(AutofillDialogControllerTest, SuggestValidEmail) {
11744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
11757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  AutofillProfile profile(test::GetVerifiedProfile());
1176a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16 kValidEmail = ASCIIToUTF16(kFakeEmail);
11777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  profile.SetRawInfo(EMAIL_ADDRESS, kValidEmail);
11787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&profile);
11797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
118058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // "add", "manage", and 1 suggestion.
118158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(
118258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount());
118358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // "add", "manage", 1 suggestion, and "same as billing".
118458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(
118558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
11867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
11877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1188eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidEmail) {
11894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
11907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  AutofillProfile profile(test::GetVerifiedProfile());
11917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(".!#$%&'*+/=?^_`-@-.."));
11927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&profile);
1193eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
119458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
119558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // "add", "manage", 1 suggestion, and "same as billing".
119658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(
119758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1198eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1199eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1200eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(AutofillDialogControllerTest, SuggestValidAddress) {
12014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
1202eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  AutofillProfile full_profile(test::GetVerifiedProfile());
1203eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  full_profile.set_origin(kSettingsOrigin);
1204eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
120558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // "add", "manage", and 1 suggestion.
1206eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EXPECT_EQ(
120758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount());
1208eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1209eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1210eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidAddress) {
12114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
1212eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  AutofillProfile full_profile(test::GetVerifiedProfile());
1213eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  full_profile.set_origin(kSettingsOrigin);
1214eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  full_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C"));
1215eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
121658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
121758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
121858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, DoNotSuggestIncompleteAddress) {
12194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
122058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  AutofillProfile profile(test::GetVerifiedProfile());
122158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  profile.SetRawInfo(ADDRESS_HOME_STATE, base::string16());
122258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&profile);
122358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Same as shipping, manage, add new.
1225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(3,
1226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
1228a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
1229a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1230a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, DoSuggestShippingAddressWithoutEmail) {
1231a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SwitchToAutofill();
1232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AutofillProfile profile(test::GetVerifiedProfile());
1233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  profile.SetRawInfo(EMAIL_ADDRESS, base::string16());
1234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&profile);
1235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Same as shipping, manage, add new, profile with missing email.
1237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(4,
1238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1239a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Billing addresses require email.
124058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
12417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
12427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
124390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, AutofillCreditCards) {
12444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
124590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Since the PersonalDataManager is empty, this should only have the
124690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // default menu items.
124790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
124890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
124990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);
125090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
125190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Empty cards are ignored.
125290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CreditCard empty_card(base::GenerateGUID(), kSettingsOrigin);
125390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  empty_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John Doe"));
125490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&empty_card);
125590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
125690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
125790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // An otherwise full but unverified card should be ignored.
125890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CreditCard full_card(test::GetCreditCard());
125990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  full_card.set_origin("https://www.example.com");
126090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&full_card);
126190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
126290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
126390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // A full, verified card should be picked up.
126490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CreditCard verified_card(test::GetCreditCard());
126590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  verified_card.set_origin(kSettingsOrigin);
126690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&verified_card);
126790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ui::MenuModel* credit_card_model =
126890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_CC);
126990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ASSERT_TRUE(credit_card_model);
127090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(3, credit_card_model->GetItemCount());
12712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
12722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1273b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Test selecting a shipping address different from billing as address.
1274b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, DontUseBillingAsShipping) {
12754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
127690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile full_profile(test::GetVerifiedProfile());
127790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile full_profile2(test::GetVerifiedProfile2());
127890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CreditCard credit_card(test::GetVerifiedCreditCard());
1279b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1280b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&full_profile2);
1281b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1282b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  ui::MenuModel* shipping_model =
1283b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_SHIPPING);
1284b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  shipping_model->ActivatedAt(2);
1285b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1286b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  controller()->OnAccept();
1287868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(20U, form_structure()->field_count());
12883240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  EXPECT_EQ(ADDRESS_HOME_STATE,
1289bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch            form_structure()->field(9)->Type().GetStorableType());
1290bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group());
1291bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(ADDRESS_HOME_STATE,
1292bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch            form_structure()->field(16)->Type().GetStorableType());
1293bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group());
1294a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 billing_state = form_structure()->field(9)->value;
1295a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 shipping_state = form_structure()->field(16)->value;
1296868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(billing_state.empty());
1297868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(shipping_state.empty());
1298868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_NE(billing_state, shipping_state);
1299868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1300bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(CREDIT_CARD_NAME,
1301bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch            form_structure()->field(1)->Type().GetStorableType());
1302a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 cc_name = form_structure()->field(1)->value;
1303bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType());
1304bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group());
1305a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 billing_name = form_structure()->field(6)->value;
1306bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType());
1307bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(NAME, form_structure()->field(13)->Type().group());
1308a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 shipping_name = form_structure()->field(13)->value;
1309868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(cc_name.empty());
1311868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(billing_name.empty());
1312868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(shipping_name.empty());
1313868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Billing name should always be the same as cardholder name.
13147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(cc_name, billing_name);
1315868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_NE(cc_name, shipping_name);
1316b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
1317b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1318b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Test selecting UseBillingForShipping.
1319b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, UseBillingAsShipping) {
13204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
13215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
132290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile full_profile(test::GetVerifiedProfile());
1323b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
13245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AutofillProfile full_profile2(test::GetVerifiedProfile2());
1326b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&full_profile2);
13275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CreditCard credit_card(test::GetVerifiedCreditCard());
1329b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1330b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
13315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC));
13325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));
1333b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
13345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SubmitAndVerifyShippingAndBillingResults();
13355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
13375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, UseBillingAsShippingManualInput) {
13385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SwitchToAutofill();
1339868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
13405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_CC));
13415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
13425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CreditCard credit_card(test::GetVerifiedCreditCard());
13445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FillInputs(SECTION_CC, credit_card);
13455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AutofillProfile full_profile(test::GetVerifiedProfile());
13475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FillInputs(SECTION_BILLING, full_profile);
13485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SubmitAndVerifyShippingAndBillingResults();
1350b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
1351b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
135290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Tests that shipping and billing telephone fields are supported, and filled
135390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// in by their respective profiles. http://crbug.com/244515
1354868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, BillingVsShippingPhoneNumber) {
135590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  FormFieldData shipping_tel;
135690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  shipping_tel.autocomplete_attribute = "shipping tel";
135790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  FormFieldData billing_tel;
135890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  billing_tel.autocomplete_attribute = "billing tel";
13590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  FormFieldData cc_field;
13600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cc_field.autocomplete_attribute = "cc-csc";
136190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
136290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  FormData form_data;
136390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  form_data.fields.push_back(shipping_tel);
136490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  form_data.fields.push_back(billing_tel);
13650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  form_data.fields.push_back(cc_field);
136690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SetUpControllerWithFormData(form_data);
136790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
13694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
137090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The profile that will be chosen for the shipping section.
137190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile shipping_profile(test::GetVerifiedProfile());
137290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The profile that will be chosen for the billing section.
137390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile billing_profile(test::GetVerifiedProfile2());
137490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CreditCard credit_card(test::GetVerifiedCreditCard());
137590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&shipping_profile);
137690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&billing_profile);
137790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
137890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ui::MenuModel* billing_model =
137990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_BILLING);
138090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  billing_model->ActivatedAt(1);
138190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
138290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnAccept();
13830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ASSERT_EQ(3U, form_structure()->field_count());
13843240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1385bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch            form_structure()->field(0)->Type().GetStorableType());
1386bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(PHONE_HOME, form_structure()->field(0)->Type().group());
1387bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1388bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch            form_structure()->field(1)->Type().GetStorableType());
1389bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(PHONE_BILLING, form_structure()->field(1)->Type().group());
139090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(shipping_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
139190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            form_structure()->field(0)->value);
1392424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  EXPECT_EQ(billing_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
139390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            form_structure()->field(1)->value);
139490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_NE(form_structure()->field(1)->value,
139590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            form_structure()->field(0)->value);
139690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
139790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Similar to the above, but tests that street-address (i.e. all lines of the
13995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// street address) is successfully filled for both shipping and billing
14005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// sections.
14015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, BillingVsShippingStreetAddress) {
14025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FormFieldData shipping_address;
14035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  shipping_address.autocomplete_attribute = "shipping street-address";
14045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FormFieldData billing_address;
14055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  billing_address.autocomplete_attribute = "billing street-address";
14065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FormFieldData shipping_address_textarea;
14075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  shipping_address_textarea.autocomplete_attribute = "shipping street-address";
14085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  shipping_address_textarea.form_control_type = "textarea";
14095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FormFieldData billing_address_textarea;
14105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  billing_address_textarea.autocomplete_attribute = "billing street-address";
14115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  billing_address_textarea.form_control_type = "textarea";
14120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  FormFieldData cc_field;
14130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cc_field.autocomplete_attribute = "cc-csc";
14145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FormData form_data;
14165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  form_data.fields.push_back(shipping_address);
14175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  form_data.fields.push_back(billing_address);
14185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  form_data.fields.push_back(shipping_address_textarea);
14195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  form_data.fields.push_back(billing_address_textarea);
14200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  form_data.fields.push_back(cc_field);
14215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetUpControllerWithFormData(form_data);
14225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SwitchToAutofill();
14245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The profile that will be chosen for the shipping section.
14265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AutofillProfile shipping_profile(test::GetVerifiedProfile());
14275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The profile that will be chosen for the billing section.
14285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AutofillProfile billing_profile(test::GetVerifiedProfile2());
14295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CreditCard credit_card(test::GetVerifiedCreditCard());
14305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&shipping_profile);
14315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&billing_profile);
14325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
14335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ui::MenuModel* billing_model =
14345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      controller()->MenuModelForSection(SECTION_BILLING);
14355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  billing_model->ActivatedAt(1);
14365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnAccept();
14380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ASSERT_EQ(5U, form_structure()->field_count());
14395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS,
14405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            form_structure()->field(0)->Type().GetStorableType());
14415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(ADDRESS_HOME, form_structure()->field(0)->Type().group());
14425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS,
14435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            form_structure()->field(1)->Type().GetStorableType());
14445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(1)->Type().group());
14455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Inexact matching; single-line inputs get the address data concatenated but
14465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // separated by commas.
14475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(StartsWith(form_structure()->field(0)->value,
14485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         shipping_profile.GetRawInfo(ADDRESS_HOME_LINE1),
14495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         true));
14505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(EndsWith(form_structure()->field(0)->value,
14515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       shipping_profile.GetRawInfo(ADDRESS_HOME_LINE2),
14525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       true));
14535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(StartsWith(form_structure()->field(1)->value,
14545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         billing_profile.GetRawInfo(ADDRESS_HOME_LINE1),
14555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         true));
14565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(EndsWith(form_structure()->field(1)->value,
14575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       billing_profile.GetRawInfo(ADDRESS_HOME_LINE2),
14585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       true));
14595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The textareas should be an exact match.
14605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(shipping_profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS),
14615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            form_structure()->field(2)->value);
14625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(billing_profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS),
14635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            form_structure()->field(3)->value);
14645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_NE(form_structure()->field(1)->value,
14665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            form_structure()->field(0)->value);
14675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_NE(form_structure()->field(3)->value,
14685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            form_structure()->field(2)->value);
14695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
14705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
14716d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Test asking for different pieces of the name.
14726d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, NamePieces) {
14736d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  const char* const attributes[] = {
14746d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "shipping name",
14756d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "billing name",
14766d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "billing given-name",
14776d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "billing family-name",
14786d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "billing additional-name",
14796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      "cc-csc"
14806d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  };
14816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
14826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  FormData form_data;
14836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  for (size_t i = 0; i < arraysize(attributes); ++i) {
14846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    FormFieldData field;
14856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    field.autocomplete_attribute.assign(attributes[i]);
14866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    form_data.fields.push_back(field);
14876d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
14886d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
14896d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  SetUpControllerWithFormData(form_data);
14906d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  SwitchToAutofill();
14916d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
14926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Billing.
14936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  AutofillProfile test_profile(test::GetVerifiedProfile());
1494116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  test_profile.SetInfo(AutofillType(NAME_FULL),
1495116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       ASCIIToUTF16("Fabian Jackson von Nacho"),
1496116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       "en-US");
14976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&test_profile);
14986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
14996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Credit card.
15006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  CreditCard credit_card(test::GetVerifiedCreditCard());
15016d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
15026d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
15036d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Make shipping name different from billing.
15046d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  AutofillProfile test_profile2(test::GetVerifiedProfile2());
1505116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  test_profile2.SetInfo(AutofillType(NAME_FULL),
1506116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                        ASCIIToUTF16("Don Ford"),
1507116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                        "en-US");
15086d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&test_profile2);
15096d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  ui::MenuModel* shipping_model =
15106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      controller()->MenuModelForSection(SECTION_SHIPPING);
15116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  shipping_model->ActivatedAt(2);
15126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
15136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  controller()->OnAccept();
15146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
15156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_EQ(NAME_FULL, form_structure()->field(0)->Type().GetStorableType());
15166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("Don Ford"),
15176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)            form_structure()->field(0)->value);
15186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
15196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_EQ(NAME_FULL, form_structure()->field(1)->Type().GetStorableType());
15206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("Fabian Jackson von Nacho"),
15216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)            form_structure()->field(1)->value);
15226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
15236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_EQ(NAME_FIRST, form_structure()->field(2)->Type().GetStorableType());
15246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("Fabian"),
15256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)            form_structure()->field(2)->value);
15266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
15276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_EQ(NAME_LAST, form_structure()->field(3)->Type().GetStorableType());
15286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("von Nacho"),
15296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)            form_structure()->field(3)->value);
15306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
15316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_EQ(NAME_MIDDLE, form_structure()->field(4)->Type().GetStorableType());
15326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("Jackson"),
15336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)            form_structure()->field(4)->value);
15346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}
15356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
15362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, AcceptLegalDocuments) {
15370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  for (size_t i = 0; i < 2; ++i) {
15380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Case " << i);
15392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    EXPECT_CALL(*controller()->GetTestingWalletClient(),
15410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                AcceptLegalDocuments(_, _));
15420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
15430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    EXPECT_CALL(*controller(), LoadRiskFingerprintData());
15441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
15450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
15460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
15470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
15481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
15490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_ptr<wallet::WalletItems> wallet_items =
15500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        CompleteAndValidWalletItems();
15510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
15520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
15530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    controller()->OnDidGetWalletItems(wallet_items.Pass());
15540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    EXPECT_FALSE(controller()->LegalDocumentLinks().empty());
15550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
15560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    controller()->OnAccept();
15570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    controller()->OnDidAcceptLegalDocuments();
15580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass());
15590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
15600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    // Now try it all over again with the location disclosure already accepted.
15610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    // Nothing should change.
15620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    Reset();
15635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::ListValue preexisting_list;
15640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    preexisting_list.AppendString(kFakeEmail);
15650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    g_browser_process->local_state()->Set(
15660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        ::prefs::kAutofillDialogWalletLocationAcceptance,
15670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        preexisting_list);
15680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
15692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, RejectLegalDocuments) {
15720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  for (size_t i = 0; i < 2; ++i) {
15730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    SCOPED_TRACE(testing::Message() << "Case " << i);
15741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
15750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    EXPECT_CALL(*controller()->GetTestingWalletClient(),
15760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                AcceptLegalDocuments(_, _)).Times(0);
15770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
15780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_ptr<wallet::WalletItems> wallet_items =
15790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        CompleteAndValidWalletItems();
15800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
15810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
15820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    controller()->OnDidGetWalletItems(wallet_items.Pass());
15830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    EXPECT_FALSE(controller()->LegalDocumentLinks().empty());
15840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
15850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    controller()->OnCancel();
15860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
15870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    // Now try it all over again with the location disclosure already accepted.
15880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    // Nothing should change.
15890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    Reset();
15905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::ListValue preexisting_list;
15910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    preexisting_list.AppendString(kFakeEmail);
15920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    g_browser_process->local_state()->Set(
15930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        ::prefs::kAutofillDialogWalletLocationAcceptance,
15940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        preexisting_list);
15950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
15960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
15970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
15980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, AcceptLocationDisclosure) {
15990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Check that accepting the dialog registers the user's name in the list
16000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // of users who have accepted the geolocation terms.
16010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(g_browser_process->local_state()->GetList(
16020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      ::prefs::kAutofillDialogWalletLocationAcceptance)->empty());
16030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
16040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
16050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(controller()->LegalDocumentsText().empty());
16060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
16070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  controller()->OnAccept();
16080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
16095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::ListValue* list = g_browser_process->local_state()->GetList(
16100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      ::prefs::kAutofillDialogWalletLocationAcceptance);
16110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ASSERT_EQ(1U, list->GetSize());
16120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  std::string accepted_username;
16130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(list->GetString(0, &accepted_username));
16140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_EQ(kFakeEmail, accepted_username);
16150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
16160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Now check it still works if that list starts off with some other username
16170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // in it.
16180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  Reset();
16190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  list = g_browser_process->local_state()->GetList(
16200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      ::prefs::kAutofillDialogWalletLocationAcceptance);
16210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ASSERT_TRUE(list->empty());
16220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
16230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  std::string kOtherUsername("spouse@example.com");
16245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ListValue preexisting_list;
16250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  preexisting_list.AppendString(kOtherUsername);
16260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  g_browser_process->local_state()->Set(
16270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      ::prefs::kAutofillDialogWalletLocationAcceptance,
16280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      preexisting_list);
16290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
16300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
16311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(controller()->LegalDocumentsText().empty());
16320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
16330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  controller()->OnAccept();
16341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
16350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  list = g_browser_process->local_state()->GetList(
16360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      ::prefs::kAutofillDialogWalletLocationAcceptance);
16370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ASSERT_EQ(2U, list->GetSize());
16380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_NE(list->end(), list->Find(base::StringValue(kFakeEmail)));
16390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_NE(list->end(), list->Find(base::StringValue(kOtherUsername)));
16400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
16410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Now check the list doesn't change if the user cancels out of the dialog.
16420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  Reset();
16430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  list = g_browser_process->local_state()->GetList(
16440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      ::prefs::kAutofillDialogWalletLocationAcceptance);
16450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ASSERT_TRUE(list->empty());
16460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
16470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  g_browser_process->local_state()->Set(
16480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      ::prefs::kAutofillDialogWalletLocationAcceptance,
16490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      preexisting_list);
16500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
16510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
16520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(controller()->LegalDocumentsText().empty());
16530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
16541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  controller()->OnCancel();
16550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
16560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  list = g_browser_process->local_state()->GetList(
16570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      ::prefs::kAutofillDialogWalletLocationAcceptance);
16580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ASSERT_EQ(1U, list->GetSize());
16590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_NE(list->end(), list->Find(base::StringValue(kOtherUsername)));
16600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_EQ(list->end(), list->Find(base::StringValue(kFakeEmail)));
16611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
16621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
16631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, LegalDocumentOverflow) {
16641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  for (size_t number_of_docs = 2; number_of_docs < 11; ++number_of_docs) {
16651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    scoped_ptr<wallet::WalletItems> wallet_items =
16661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        CompleteAndValidWalletItems();
16671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    for (size_t i = 0; i < number_of_docs; ++i)
16681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
16691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
16701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Reset();
16711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    controller()->OnDidGetWalletItems(wallet_items.Pass());
16721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
16731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // The dialog is only equipped to handle 2-6 legal documents. More than
16741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // 6 errors out.
16751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if (number_of_docs <= 6U) {
16761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      EXPECT_FALSE(controller()->LegalDocumentsText().empty());
16771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    } else {
16781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      EXPECT_TRUE(controller()->LegalDocumentsText().empty());
16791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      EXPECT_EQ(1U, NotificationsOfType(
16801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)          DialogNotification::WALLET_ERROR).size());
16811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
16821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
16831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
16841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  controller()->OnCancel();
16851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
16861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1687c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Makes sure the default object IDs are respected.
1688c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, WalletDefaultItems) {
168968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
169068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1692c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
1693c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
1694c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
16952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1696c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1697c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1698c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1699c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestShippingAddress());
1700c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
1701c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1702c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
1703c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // "add", "manage", and 4 suggestions.
1704c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(6,
1705c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
1706c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
1707c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      IsItemCheckedAt(2));
1708868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
1709c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // "use billing", "add", "manage", and 5 suggestions.
1710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(8,
1711c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
1712c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_SHIPPING)->
1713c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      IsItemCheckedAt(4));
1714868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_SHIPPING));
171590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
171690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
171790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Tests that invalid and AMEX default instruments are ignored.
171890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SelectInstrument) {
171968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
172068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
172190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Tests if default instrument is invalid, then, the first valid instrument is
172290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // selected instead of the default instrument.
172390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
172490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
172590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
172690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
172790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
172890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
172990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // 4 suggestions and "add", "manage".
173090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(6,
173190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
173290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
173390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      IsItemCheckedAt(0));
173490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
173568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Tests if default instrument is AMEX but Wallet doesn't support
173668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // AMEX on this merchant, then the first valid instrument is
173790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // selected instead of the default instrument.
173868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
173990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
174090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
174168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  wallet_items->AddInstrument(
174268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED));
174390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
174490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
174590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
174690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // 4 suggestions and "add", "manage".
174790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(6,
174890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
174990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
175090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      IsItemCheckedAt(0));
175190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
175268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Tests if default instrument is AMEX and it is allowed on this merchant,
175368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // then it is selected.
175468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_ALLOWED);
175568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
175668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
175768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  wallet_items->AddInstrument(
175868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_ALLOWED));
175968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
176068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
176168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
176268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // 4 suggestions and "add", "manage".
176368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_EQ(6,
176468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
176568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
176668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      IsItemCheckedAt(2));
176768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
176890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Tests if only have AMEX and invalid instrument, then "add" is selected.
176968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
177090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
177168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  wallet_items->AddInstrument(
177268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED));
177390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
177490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
177590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // 2 suggestions and "add", "manage".
177690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(4,
177790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
177890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // "add"
177990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
178090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      IsItemCheckedAt(2));
1781c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1782c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1783c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SaveAddress) {
17848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), ModelChanged());
17852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(),
17865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              SaveToWalletMock(testing::IsNull(),
17875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::NotNull(),
17885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull(),
17895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull()));
17902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
179168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
179268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
17932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
17942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
179590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // If there is no shipping address in wallet, it will default to
179690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // "same-as-billing" instead of "add-new-item". "same-as-billing" is covered
179758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // by the following tests. The penultimate item in the menu is "add-new-item".
179890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ui::MenuModel* shipping_model =
179990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_SHIPPING);
180058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  shipping_model->ActivatedAt(shipping_model->GetItemCount() - 2);
18014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
18024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  AutofillProfile test_profile(test::GetVerifiedProfile());
18034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FillInputs(SECTION_SHIPPING, test_profile);
18044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1805eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  AcceptAndLoadFakeFingerprint();
18062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SaveInstrument) {
18098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), ModelChanged());
18102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(),
18115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              SaveToWalletMock(testing::NotNull(),
18125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull(),
18135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull(),
18145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull()));
18152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  FillCCBillingInputs();
181768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
181868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
18192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestShippingAddress());
1820eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SubmitWithWalletItems(wallet_items.Pass());
18212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
182390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SaveInstrumentWithInvalidInstruments) {
18248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), ModelChanged());
182590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(),
18265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              SaveToWalletMock(testing::NotNull(),
18275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull(),
18285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull(),
18295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull()));
183090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
18318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  FillCCBillingInputs();
183268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
183368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
183490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestShippingAddress());
183590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
1836eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SubmitWithWalletItems(wallet_items.Pass());
183790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
183890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
18392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SaveInstrumentAndAddress) {
18402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(),
18415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              SaveToWalletMock(testing::NotNull(),
18425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::NotNull(),
18435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull(),
18445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull()));
18452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  FillCCBillingInputs();
18478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
18488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
18498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  SubmitWithWalletItems(wallet_items.Pass());
18502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
18512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1852ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben MurdochMATCHER(IsUpdatingExistingData, "updating existing Wallet data") {
1853ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return !arg->object_id().empty();
1854ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
1855ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
1856a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)MATCHER(UsesLocalBillingAddress, "uses the local billing address") {
18575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return arg->street_address()[0] == ASCIIToUTF16(kEditedBillingAddress);
1858a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
1859a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
186090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Tests that when using billing address for shipping, and there is no exact
186190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// matched shipping address, then a shipping address should be added.
186290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, BillingForShipping) {
186390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(),
18645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              SaveToWalletMock(testing::IsNull(),
18655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::NotNull(),
18665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull(),
18675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull()));
186890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1869eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
187090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Select "Same as billing" in the address menu.
187190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  UseBillingForShipping();
187290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1873eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  AcceptAndLoadFakeFingerprint();
187490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
187590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
187690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Tests that when using billing address for shipping, and there is an exact
187790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// matched shipping address, then a shipping address should not be added.
187890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, BillingForShippingHasMatch) {
187990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(),
18805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              SaveToWalletMock(_, _, _, _)).Times(0);
188190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
188268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
188368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
188490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument =
188590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      wallet::GetTestMaskedInstrument();
188690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Copy billing address as shipping address, and assign an id to it.
188790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<wallet::Address> shipping_address(
188890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      new wallet::Address(instrument->address()));
188990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  shipping_address->set_object_id("shipping_address_id");
189090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddAddress(shipping_address.Pass());
189190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(instrument.Pass());
189290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestShippingAddress());
189390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
189490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
189590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Select "Same as billing" in the address menu.
189690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  UseBillingForShipping();
189790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1898eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  AcceptAndLoadFakeFingerprint();
189990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
190090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1901a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Test that the local view contents is used when saving a new instrument and
1902a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// the user has selected "Same as billing".
1903a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SaveInstrumentSameAsBilling) {
190468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
190568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
1906a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
1907a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
1908a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
190958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ui::MenuModel* model = controller()->MenuModelForSection(SECTION_CC_BILLING);
191058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  model->ActivatedAt(model->GetItemCount() - 2);
1911a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1912f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FieldValueMap outputs;
1913a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  const DetailInputs& inputs =
1914a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
191558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  AutofillProfile full_profile(test::GetVerifiedProfile());
191658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CreditCard full_card(test::GetCreditCard());
1917a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  for (size_t i = 0; i < inputs.size(); ++i) {
1918f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const ServerFieldType type = inputs[i].type;
1919effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (type == ADDRESS_BILLING_STREET_ADDRESS)
1920f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      outputs[type] = ASCIIToUTF16(kEditedBillingAddress);
1921f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    else
1922f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
192358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1924f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (outputs[type].empty())
1925f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      outputs[type] = full_card.GetInfo(AutofillType(type), "en-US");
1926a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
1927a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
1928a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
192958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->OnAccept();
193058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1931a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(),
19325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              SaveToWalletMock(testing::NotNull(),
19335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               UsesLocalBillingAddress(),
19345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull(),
19355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               testing::IsNull()));
1936eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  AcceptAndLoadFakeFingerprint();
1937a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
1938a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, CancelNoSave) {
1940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(),
19415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              SaveToWalletMock(_, _, _, _)).Times(0);
19422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), ModelChanged());
19442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
194568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  controller()->OnDidGetWalletItems(
194668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
19472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  controller()->OnCancel();
19482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
19492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1950c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Checks that clicking the Manage menu item opens a new tab with a different
1951c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// URL for Wallet and Autofill.
1952c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, ManageItem) {
195390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile full_profile(test::GetVerifiedProfile());
1954c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  full_profile.set_origin(kSettingsOrigin);
1955a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
1956c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
1957c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SwitchToAutofill();
1958c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1959d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  SuggestionsMenuModel* shipping = GetMenuModelForSection(SECTION_SHIPPING);
196090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  shipping->ExecuteCommand(shipping->GetItemCount() - 1, 0);
1961c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GURL autofill_manage_url = controller()->open_tab_url();
1962c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ("chrome", autofill_manage_url.scheme());
1963c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1964c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SwitchToWallet();
196568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
196668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
196790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
196890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
1969c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
197090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->SuggestionItemSelected(shipping, shipping->GetItemCount() - 1);
197190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GURL wallet_manage_addresses_url = controller()->open_tab_url();
197290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ("https", wallet_manage_addresses_url.scheme());
197390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1974d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  SuggestionsMenuModel* billing = GetMenuModelForSection(SECTION_CC_BILLING);
197590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->SuggestionItemSelected(billing, billing->GetItemCount() - 1);
197690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GURL wallet_manage_instruments_url = controller()->open_tab_url();
197790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ("https", wallet_manage_instruments_url.scheme());
197890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
197990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_NE(autofill_manage_url, wallet_manage_instruments_url);
198090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_NE(wallet_manage_instruments_url, wallet_manage_addresses_url);
1981c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
1982c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1983c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Tests that adding an autofill profile and then submitting works.
1984c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, AddAutofillProfile) {
1985ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  SwitchToAutofill();
19867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2);
1987c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
198890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile full_profile(test::GetVerifiedProfile());
19897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CreditCard credit_card(test::GetVerifiedCreditCard());
1990c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
19917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
1992c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1993c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING);
1994c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Activate the "Add billing address" menu item.
1995c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  model->ActivatedAt(model->GetItemCount() - 2);
1996c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1997c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Fill in the inputs from the profile.
1998f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FieldValueMap outputs;
1999c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DetailInputs& inputs =
2000c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      controller()->RequestedFieldsForSection(SECTION_BILLING);
200190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile full_profile2(test::GetVerifiedProfile2());
2002c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < inputs.size(); ++i) {
2003f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const ServerFieldType type = inputs[i].type;
2004f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    outputs[type] = full_profile2.GetInfo(AutofillType(type), "en-US");
2005c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2006c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->GetView()->SetUserInput(SECTION_BILLING, outputs);
2007c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2008c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnAccept();
2009c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const AutofillProfile& added_profile =
2010c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      controller()->GetTestingManager()->imported_profile();
2011c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2012c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const DetailInputs& shipping_inputs =
2013c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      controller()->RequestedFieldsForSection(SECTION_SHIPPING);
2014c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (size_t i = 0; i < shipping_inputs.size(); ++i) {
2015f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const ServerFieldType type = shipping_inputs[i].type;
2016f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    EXPECT_EQ(full_profile2.GetInfo(AutofillType(type), "en-US"),
2017f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)              added_profile.GetInfo(AutofillType(type), "en-US"));
2018c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2019c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2020c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2021c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, VerifyCvv) {
20228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2023c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(),
20248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)              AuthenticateInstrument(_, _));
2025c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2026eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SubmitWithWalletItems(CompleteAndValidWalletItems());
2027c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2028c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty());
2029c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->SectionIsActive(SECTION_SHIPPING));
2030c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
2031c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
20327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2033c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2034c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SuggestionState suggestion_state =
2035c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      controller()->SuggestionStateForSection(SECTION_CC_BILLING);
2036c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(suggestion_state.extra_text.empty());
2037c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
20388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  controller()->OnDidGetFullWallet(
20398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      wallet::GetTestFullWalletWithRequiredActions(
20408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          std::vector<wallet::RequiredAction>(1, wallet::VERIFY_CVV)));
20418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_TRUE(controller()->IsSubmitPausedOn(wallet::VERIFY_CVV));
2042c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2043c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(
2044c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty());
2045c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
2046c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
2047c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2048c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  suggestion_state =
2049c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      controller()->SuggestionStateForSection(SECTION_CC_BILLING);
2050c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(suggestion_state.extra_text.empty());
2051c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
2052c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2053c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2054c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2055c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2056c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnAccept();
20570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
20580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
2059c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2060c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2061c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, ErrorDuringSubmit) {
20628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2063c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2064eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SubmitWithWalletItems(CompleteAndValidWalletItems());
2065c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2066c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
20677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2068c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2069c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);
2070c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2071c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2072c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2073c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2074c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2075c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, ErrorDuringVerifyCvv) {
20768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
2077c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2078eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SubmitWithWalletItems(CompleteAndValidWalletItems());
20798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  controller()->OnDidGetFullWallet(
20808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      wallet::GetTestFullWalletWithRequiredActions(
20818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          std::vector<wallet::RequiredAction>(1, wallet::VERIFY_CVV)));
2082c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2083c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2084c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2085c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2086c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);
2087c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2088c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2089c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
2090c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2091c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2092a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Simulates receiving an INVALID_FORM_FIELD required action while processing a
2093a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// |WalletClientDelegate::OnDid{Save,Update}*()| call. This can happen if Online
2094a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Wallet's server validation differs from Chrome's local validation.
20957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, WalletServerSideValidation) {
209668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
209768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2098a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2099a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
2100a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  controller()->OnAccept();
2101a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2102a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  std::vector<wallet::RequiredAction> required_actions;
2103a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  required_actions.push_back(wallet::INVALID_FORM_FIELD);
21047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
21057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  std::vector<wallet::FormFieldError> form_errors;
21067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  form_errors.push_back(
21077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      wallet::FormFieldError(wallet::FormFieldError::INVALID_POSTAL_CODE,
21087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                             wallet::FormFieldError::SHIPPING_ADDRESS));
21097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
21108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), UpdateForErrors());
2111ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  controller()->OnDidSaveToWallet(std::string(),
2112ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                  std::string(),
2113ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                  required_actions,
2114ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                  form_errors);
21157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
21167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
21177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Simulates receiving unrecoverable Wallet server validation errors.
21187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, WalletServerSideValidationUnrecoverable) {
211968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
212068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
21217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
21227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
21237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  controller()->OnAccept();
21247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
21257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  std::vector<wallet::RequiredAction> required_actions;
21267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  required_actions.push_back(wallet::INVALID_FORM_FIELD);
21277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
21287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  std::vector<wallet::FormFieldError> form_errors;
21297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  form_errors.push_back(
21307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      wallet::FormFieldError(wallet::FormFieldError::UNKNOWN_ERROR,
21317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                             wallet::FormFieldError::UNKNOWN_LOCATION));
21327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2133ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  controller()->OnDidSaveToWallet(std::string(),
2134ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                  std::string(),
2135ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                  required_actions,
2136ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                  form_errors);
2137a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2138a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_EQ(1U, NotificationsOfType(
2139a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      DialogNotification::REQUIRED_ACTION).size());
2140a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
2141a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2142d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Test Wallet banners are show in the right situations. These banners promote
2143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// saving details into Wallet (i.e. "[x] Save details to Wallet").
2144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, WalletBanners) {
2145d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Simulate non-signed-in case.
21467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  SetUpControllerWithFormData(DefaultFormData());
2147d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
21484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->OnPassiveSigninFailure(error);
2149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(0U, NotificationsOfType(
2150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Sign in a user with a completed account.
2153d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  SetUpControllerWithFormData(DefaultFormData());
2154eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Full account; should show "Details from Wallet" message.
2157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(1U, NotificationsOfType(
2158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SwitchToAutofill();
2160d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(1U, NotificationsOfType(
2161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2162c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2163d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Start over and sign in a user with an incomplete account.
2164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetUpControllerWithFormData(DefaultFormData());
216568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
216668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
2169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2170d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Partial account.
2171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(1U, NotificationsOfType(
2172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SwitchToAutofill();
2175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(1U, NotificationsOfType(
2176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // A Wallet error should kill any Wallet promos.
2179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);
2180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(1U, NotificationsOfType(
2182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DialogNotification::WALLET_ERROR).size());
2183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(0U, NotificationsOfType(
2184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DialogNotification::WALLET_USAGE_CONFIRMATION).size());
2185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, ViewCancelDoesntSetPref) {
2188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
2189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SwitchToAutofill();
2192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnCancel();
2194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->ViewClosed();
2195c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(
2197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, SubmitWithSigninErrorDoesntSetPref) {
2201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
2202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SimulateSigninError();
2205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FillCreditCardInputs();
2206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->OnAccept();
2207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(
2209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2210868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
221258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Tests that there's an overlay shown while waiting for full wallet items.
22137dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(AutofillDialogControllerTest, WalletFirstRun) {
22147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_TRUE(controller()->GetDialogOverlay().image.IsEmpty());
22157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
22167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  SubmitWithWalletItems(CompleteAndValidWalletItems());
22177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
22187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
22197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
22207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
22217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(form_structure());
22227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2223f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Don't make the test wait for 2 seconds.
222458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->ForceFinishSubmit();
22257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_TRUE(form_structure());
22267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
22277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, ViewSubmitSetsPref) {
2229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
2230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SwitchToAutofill();
2233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FillCreditCardInputs();
2234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->OnAccept();
2235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2241868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Try again with a signin error (just leaves the pref alone).
2242868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetUpControllerWithFormData(DefaultFormData());
2243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2244868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Setting up the controller again should not change the pref.
2245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2246868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2247868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2248868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2249868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SimulateSigninError();
2251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  FillCreditCardInputs();
2252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->OnAccept();
2253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
2256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
225858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Successfully choosing wallet does set the pref.
2259f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Note that OnDidGetWalletItems sets the account chooser to wallet mode.
2260868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetUpControllerWithFormData(DefaultFormData());
2261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2262f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  controller()->OnDidFetchWalletCookieValue(std::string());
226368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
226468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2265868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2266868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
2267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnAccept();
2268868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
226958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->ForceFinishSubmit();
2270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2271c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
2272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2273868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
2274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet));
2275c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, HideWalletEmail) {
2278c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SwitchToAutofill();
2279c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
228058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Email field should be showing when using Autofill.
228158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->SectionIsActive(SECTION_BILLING));
228258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->SectionIsActive(SECTION_CC_BILLING));
228358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(SectionContainsField(SECTION_BILLING, EMAIL_ADDRESS));
2284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SwitchToWallet();
2286c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
22874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Reset the wallet state.
22884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());
22894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2290c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Setup some wallet state, submit, and get a full wallet to end the flow.
2291eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
2292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Filling |form_structure()| depends on the current username and wallet items
2294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // being fetched. Until both of these have occurred, the user should not be
2295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // able to click Submit if using Wallet. The username fetch happened earlier.
2296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
2298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
230058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Email field should be absent when using Wallet.
230158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->SectionIsActive(SECTION_BILLING));
230258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
230358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(SectionContainsField(SECTION_CC_BILLING, EMAIL_ADDRESS));
2304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnAccept();
2306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
230758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->ForceFinishSubmit();
2308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
230958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_TRUE(form_structure());
2310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  size_t i = 0;
2311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (; i < form_structure()->field_count(); ++i) {
2312bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    if (form_structure()->field(i)->Type().GetStorableType() == EMAIL_ADDRESS) {
2313c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      EXPECT_EQ(ASCIIToUTF16(kFakeEmail), form_structure()->field(i)->value);
2314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      break;
2315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
2316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
231758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_LT(i, form_structure()->field_count());
2318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2320b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Test if autofill types of returned form structure are correct for billing
2321b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// entries.
2322b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, AutofillTypes) {
23237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2324b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  controller()->OnAccept();
2325b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
232658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->ForceFinishSubmit();
232758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_TRUE(form_structure());
2328868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_EQ(20U, form_structure()->field_count());
2329bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(EMAIL_ADDRESS,
2330bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch            form_structure()->field(0)->Type().GetStorableType());
23313240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  EXPECT_EQ(CREDIT_CARD_NUMBER,
2332bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch            form_structure()->field(2)->Type().GetStorableType());
2333bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(ADDRESS_HOME_STATE,
2334bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch            form_structure()->field(9)->Type().GetStorableType());
2335bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group());
23363240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  EXPECT_EQ(ADDRESS_HOME_STATE,
2337bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch            form_structure()->field(16)->Type().GetStorableType());
2338bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group());
2339b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
2340b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
2341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SaveDetailsInChrome) {
2342ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  SwitchToAutofill();
2343a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(4);
2344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
234590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AutofillProfile full_profile(test::GetVerifiedProfile());
2346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
2347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
234890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CreditCard card(test::GetVerifiedCreditCard());
2349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&card);
2350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
235258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(0);
2353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
235558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1);
2356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());
2357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2358a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);
2359a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2360a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2361a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, true);
2362a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1);
2363a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());
2364a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
23653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  profile()->ForceIncognito(true);
2366c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
2367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
2368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2369a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, DisabledAutofill) {
2370a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SwitchToAutofill();
2371a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kAutofillEnabled));
2372a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AutofillProfile verified_profile(test::GetVerifiedProfile());
2374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
2375a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CreditCard credit_card(test::GetVerifiedCreditCard());
2377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
2378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2379a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Verify suggestions menus should be showing when Autofill is enabled.
2380a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC));
2381a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_BILLING));
2382a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(
2383a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2385a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), ModelChanged());
2386a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);
2387a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Verify billing and credit card suggestions menus are hidden when Autofill
2389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // is disabled.
2390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
2391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING));
2392a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // And that the shipping suggestions menu has less selections.
2393a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(
2394a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2396a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Additionally, editing fields should not show Autofill popups.
2397a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(controller()->UserEditedOrActivatedInput(
2398a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      SECTION_BILLING,
2399a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      NAME_BILLING_FULL,
2400a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      gfx::NativeView(),
2401a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      gfx::Rect(),
2402116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      verified_profile.GetInfo(AutofillType(NAME_FULL), "en-US").substr(0, 1),
2403a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      true));
2404a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(UNKNOWN_TYPE, controller()->popup_input_type());
2405a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
2406a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
240790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Tests that user is prompted when using instrument with minimal address.
240890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, UpgradeMinimalAddress) {
2409eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // A minimal address being selected should trigger error validation in the
2410eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // view. Called once for each incomplete suggestion.
24118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), UpdateForErrors());
2412eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
241368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
241468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
241590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentWithIdAndAddress(
241690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      "id", wallet::GetTestMinimalAddress()));
241790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<wallet::Address> address(wallet::GetTestShippingAddress());
241890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  address->set_is_complete_address(false);
241990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddAddress(address.Pass());
242090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
242190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
242290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Assert that dialog's SECTION_CC_BILLING section is in edit mode.
2423868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
242490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Shipping section should be in edit mode because of
242590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // is_minimal_shipping_address.
2426868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_SHIPPING));
242790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
242890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
242990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, RiskNeverLoadsWithPendingLegalDocuments) {
243090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0);
243190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
24328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
243390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
24341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
243590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
243690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnAccept();
243790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
243890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
243990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, RiskLoadsAfterAcceptingLegalDocuments) {
244090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0);
244190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
24428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
244390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
24441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
244590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
244690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
244790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  testing::Mock::VerifyAndClear(controller());
24488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller(), LoadRiskFingerprintData());
244990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
245090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnAccept();
245190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
245290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Simulate a risk load and verify |GetRiskData()| matches the encoded value.
245390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidAcceptLegalDocuments();
245490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidLoadRiskFingerprintData(GetFakeFingerprint().Pass());
245590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(kFakeFingerprintEncoded, controller()->GetRiskData());
245690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
245790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
245890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, NoManageMenuItemForNewWalletUsers) {
245990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Make sure the menu model item is created for a returning Wallet user.
246068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
246168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
246290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
246390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestShippingAddress());
246490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
246590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
246690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING));
246790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // "Same as billing", "123 address", "Add address...", and "Manage addresses".
246890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(
246990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
247090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
247190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Make sure the menu model item is not created for new Wallet users.
247290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::DictionaryValue dict;
247390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<base::ListValue> required_actions(new base::ListValue);
247490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  required_actions->AppendString("setup_wallet");
247590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  dict.Set("required_action", required_actions.release());
247690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  controller()->OnDidGetWalletItems(
247790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      wallet::WalletItems::CreateWalletItems(dict).Pass());
247890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
247990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
248090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // "Same as billing" and "Add address...".
248190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(
248290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
248390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
248490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2485868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHidden) {
2486868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FormFieldData email_field;
2487868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  email_field.autocomplete_attribute = "email";
2488868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FormFieldData cc_field;
2489868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  cc_field.autocomplete_attribute = "cc-number";
2490868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FormFieldData billing_field;
2491f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  billing_field.autocomplete_attribute = "billing address-level1";
2492868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2493868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FormData form_data;
2494868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  form_data.fields.push_back(email_field);
2495868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  form_data.fields.push_back(cc_field);
2496868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  form_data.fields.push_back(billing_field);
2497868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2498868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AutofillProfile full_profile(test::GetVerifiedProfile());
2499868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
2500868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetUpControllerWithFormData(form_data);
25014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
25024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
25034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2504868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
2505868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2506868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FillCreditCardInputs();
2507868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->OnAccept();
2508868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(form_structure());
2509868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2510868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2511a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHiddenForWallet) {
2512a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  FormFieldData email_field;
2513a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  email_field.autocomplete_attribute = "email";
2514a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  FormFieldData cc_field;
2515a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  cc_field.autocomplete_attribute = "cc-number";
2516a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  FormFieldData billing_field;
2517f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  billing_field.autocomplete_attribute = "billing address-level1";
2518a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
2519a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  FormData form_data;
2520a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  form_data.fields.push_back(email_field);
2521a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  form_data.fields.push_back(cc_field);
2522a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  form_data.fields.push_back(billing_field);
2523a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
2524a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  SetUpControllerWithFormData(form_data);
2525a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
2526a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_FALSE(controller()->IsShippingAddressRequired());
2527a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
25288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
252968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
253068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2531a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
2532a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  SubmitWithWalletItems(wallet_items.Pass());
2533a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  controller()->OnDidGetFullWallet(wallet::GetTestFullWalletInstrumentOnly());
253458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->ForceFinishSubmit();
2535a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  EXPECT_TRUE(form_structure());
2536a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
2537a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
2538868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, NotProdNotification) {
2539868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // To make IsPayingWithWallet() true.
254068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  controller()->OnDidGetWalletItems(
254168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
2542868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2543868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CommandLine* command_line = CommandLine::ForCurrentProcess();
25444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ASSERT_EQ(
25454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      "",
25464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox));
25474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
25484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  command_line->AppendSwitchASCII(switches::kWalletServiceUseSandbox, "1");
25494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_EQ(1U,
25504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            NotificationsOfType(DialogNotification::DEVELOPER_WARNING).size());
2551868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2552868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, NoNotProdNotification) {
25545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // To make IsPayingWithWallet() true.
25555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidGetWalletItems(
25565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
25575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
25585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CommandLine* command_line = CommandLine::ForCurrentProcess();
25595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(
25605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "",
25615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox));
25625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
25635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  command_line->AppendSwitchASCII(switches::kWalletServiceUseSandbox, "0");
25645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(0U,
25655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            NotificationsOfType(DialogNotification::DEVELOPER_WARNING).size());
25665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
25675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2568868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Ensure Wallet instruments marked expired by the server are shown as invalid.
2569868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, WalletExpiredCard) {
257068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
257168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
2572868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired());
2573868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
2574868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2575868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
2576868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2577868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DetailInputs& inputs =
2578868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
2579f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FieldValueMap outputs;
258058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CopyInitialValues(inputs, &outputs);
2581868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The local inputs are invalid because the server said so. They'll
25834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // stay invalid until they differ from the remotely fetched model.
25844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ValidityMessages messages = controller()->InputsAreValid(SECTION_CC_BILLING,
25854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                           outputs);
25864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_TRUE(messages.HasSureError(CREDIT_CARD_EXP_MONTH));
25874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_TRUE(messages.HasSureError(CREDIT_CARD_EXP_4_DIGIT_YEAR));
2588868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2589868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Make the local input year differ from the instrument.
259058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CopyInitialValues(inputs, &outputs);
2591a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = ASCIIToUTF16("3002");
25924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
25934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_MONTH));
25944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
2595868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2596868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Make the local input month differ from the instrument.
259758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CopyInitialValues(inputs, &outputs);
2598a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[CREDIT_CARD_EXP_MONTH] = ASCIIToUTF16("06");
25994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
26004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_MONTH));
26014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
2602868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2603868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2604868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, ChooseAnotherInstrumentOrAddress) {
2605eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SubmitWithWalletItems(CompleteAndValidWalletItems());
2606868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2607868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(0U, NotificationsOfType(
2608868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DialogNotification::REQUIRED_ACTION).size());
2609116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
2610868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->OnDidGetFullWallet(
26118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      wallet::GetTestFullWalletWithRequiredActions(
26128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          std::vector<wallet::RequiredAction>(
26138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)              1, wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS)));
2614868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(1U, NotificationsOfType(
2615868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DialogNotification::REQUIRED_ACTION).size());
2616eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2617868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2618868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  controller()->OnAccept();
2619868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(0U, NotificationsOfType(
2620868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DialogNotification::REQUIRED_ACTION).size());
2621868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
2622868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
26237dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(AutofillDialogControllerTest, NewCardBubbleShown) {
26247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  SwitchToAutofill();
26257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  FillCreditCardInputs();
26267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  controller()->OnAccept();
26277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  controller()->ViewClosed();
2628ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
2629ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  EXPECT_EQ(1, mock_new_card_bubble_controller()->bubbles_shown());
26308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(0, test_generated_bubble_controller()->bubbles_shown());
26317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
26327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
26337dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(AutofillDialogControllerTest, GeneratedCardBubbleShown) {
26347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  SubmitWithWalletItems(CompleteAndValidWalletItems());
26357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
263658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->ForceFinishSubmit();
26377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  controller()->ViewClosed();
2638ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
2639ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  EXPECT_EQ(0, mock_new_card_bubble_controller()->bubbles_shown());
26408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1, test_generated_bubble_controller()->bubbles_shown());
26417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
26427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
264358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Verify that new Wallet data is fetched when the user switches away from the
264458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// tab hosting the Autofill dialog and back. Also verify that the user's
264558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// selection is preserved across this re-fetch.
264658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben MurdochTEST_F(AutofillDialogControllerTest, ReloadWalletItemsOnActivation) {
2647f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Initialize some Wallet data.
264868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
264968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
265058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
265158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
265258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
265358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  wallet_items->AddAddress(wallet::GetTestShippingAddress());
265458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  controller()->OnDidGetWalletItems(wallet_items.Pass());
265558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
265658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Initially, the default entries should be selected.
265758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  ui::MenuModel* cc_billing_model =
265858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      controller()->MenuModelForSection(SECTION_CC_BILLING);
265958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  ui::MenuModel* shipping_model =
266058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      controller()->MenuModelForSection(SECTION_SHIPPING);
266158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // "add", "manage", and 2 suggestions.
266258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  ASSERT_EQ(4, cc_billing_model->GetItemCount());
266358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(0));
266458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // "use billing", "add", "manage", and 2 suggestions.
266558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  ASSERT_EQ(5, shipping_model->GetItemCount());
266658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  EXPECT_TRUE(shipping_model->IsItemCheckedAt(2));
266758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
266858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Select entries other than the defaults.
266958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  cc_billing_model->ActivatedAt(1);
267058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  shipping_model->ActivatedAt(1);
267158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // 2 suggestions, "add", and "manage".
267258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  ASSERT_EQ(4, cc_billing_model->GetItemCount());
267358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(1));
267458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // "use billing", 2 suggestions, "add", "manage".
267558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  ASSERT_EQ(5, shipping_model->GetItemCount());
267658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  EXPECT_TRUE(shipping_model-> IsItemCheckedAt(1));
267758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
267858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Simulate switching away from the tab and back.  This should issue a request
267958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // for wallet items.
26804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->ClearLastWalletItemsFetchTimestampForTesting();
2681116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
268258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  controller()->TabActivated();
268358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
268458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Simulate a response that includes different items.
268568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
268658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired());
268758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
268858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
268958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
269058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  controller()->OnDidGetWalletItems(wallet_items.Pass());
269158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
269258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // The previously selected entries should still be selected.
269358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // 3 suggestions, "add", and "manage".
269458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  ASSERT_EQ(5, cc_billing_model->GetItemCount());
269558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(2));
269658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // "use billing", 1 suggestion, "add", and "manage".
269758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  ASSERT_EQ(4, shipping_model->GetItemCount());
269858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  EXPECT_TRUE(shipping_model->IsItemCheckedAt(1));
269958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch}
270058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
270158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Verify that if the default values change when re-fetching Wallet data, these
270258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// new default values are selected in the dialog.
270358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AutofillDialogControllerTest,
270458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       ReloadWalletItemsOnActivationWithNewDefaults) {
2705f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Initialize some Wallet data.
270668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
270768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
270858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
270958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
271058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
271158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestShippingAddress());
271258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
271358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
271458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Initially, the default entries should be selected.
271558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ui::MenuModel* cc_billing_model =
271658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_CC_BILLING);
271758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ui::MenuModel* shipping_model =
271858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      controller()->MenuModelForSection(SECTION_SHIPPING);
271958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // 2 suggestions, "add", and "manage".
272058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_EQ(4, cc_billing_model->GetItemCount());
272158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(0));
272258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // "use billing", 2 suggestions, "add", and "manage".
272358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_EQ(5, shipping_model->GetItemCount());
272458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(shipping_model->IsItemCheckedAt(2));
272558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
272658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Simulate switching away from the tab and back.  This should issue a request
272758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // for wallet items.
27284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->ClearLastWalletItemsFetchTimestampForTesting();
2729116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
273058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->TabActivated();
273158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
273258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Simulate a response that includes different default values.
273358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_items =
273458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      wallet::GetTestWalletItemsWithDefaultIds("new_default_instrument_id",
273568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                                               "new_default_address_id",
273668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                                               wallet::AMEX_DISALLOWED);
273758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_ptr<wallet::Address> other_address = wallet::GetTestShippingAddress();
273858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  other_address->set_object_id("other_address_id");
273958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  scoped_ptr<wallet::Address> new_default_address =
274058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      wallet::GetTestNonDefaultShippingAddress();
274158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  new_default_address->set_object_id("new_default_address_id");
274258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
274358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_items->AddInstrument(
274458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      wallet::GetTestMaskedInstrumentWithId("other_instrument_id"));
274558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_items->AddInstrument(
274658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      wallet::GetTestMaskedInstrumentWithId("new_default_instrument_id"));
274758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_items->AddAddress(new_default_address.Pass());
274858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  wallet_items->AddAddress(other_address.Pass());
274958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
275058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
275158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // The new default entries should be selected.
275258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // 2 suggestions, "add", and "manage".
275358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_EQ(4, cc_billing_model->GetItemCount());
275458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(1));
275558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // "use billing", 2 suggestions, "add", and "manage".
275658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ASSERT_EQ(5, shipping_model->GetItemCount());
275758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(shipping_model->IsItemCheckedAt(1));
275858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
275958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2760558790d6acca3451cf3a6b497803a5f07d0bec58Ben MurdochTEST_F(AutofillDialogControllerTest, ReloadWithEmptyWalletItems) {
2761558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
2762558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  controller()->MenuModelForSection(SECTION_CC_BILLING)->ActivatedAt(1);
2763558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
2764558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
27654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->ClearLastWalletItemsFetchTimestampForTesting();
2766116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
2767558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  controller()->TabActivated();
2768558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
276968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  controller()->OnDidGetWalletItems(
277068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
2771558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
2772558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
2773558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  EXPECT_EQ(
2774558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch      3, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
2775558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch}
2776558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
277758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SaveInChromeByDefault) {
277858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->ShouldSaveInChrome());
277958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  SwitchToAutofill();
278058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  FillCreditCardInputs();
278158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->OnAccept();
278258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->ShouldSaveInChrome());
278358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
2784a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
278558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AutofillDialogControllerTest,
278658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       SaveInChromePreferenceNotRememberedOnCancel) {
278758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->ShouldSaveInChrome());
278858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  SwitchToAutofill();
278958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
279058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->OnCancel();
279158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->ShouldSaveInChrome());
279258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
2793ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
279458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AutofillDialogControllerTest,
279558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       SaveInChromePreferenceRememberedOnSuccess) {
279658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->ShouldSaveInChrome());
279758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  SwitchToAutofill();
279858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  FillCreditCardInputs();
279958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
280058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->OnAccept();
280158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->ShouldSaveInChrome());
280258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
280358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
280458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AutofillDialogControllerTest,
280558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       SubmitButtonIsDisabled_SpinnerFinishesBeforeDelay) {
28064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Reset Wallet state.
28074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());
28084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
280958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(1, controller()->get_submit_button_delay_count());
281058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
281158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Begin the submit button delay.
281258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->SimulateSubmitButtonDelayBegin();
281358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
281458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->ShouldShowSpinner());
281558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
281658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
281758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Stop the spinner.
281858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
281958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
282058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->ShouldShowSpinner());
282158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
282258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
282358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // End the submit button delay.
282458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->SimulateSubmitButtonDelayEnd();
282558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
282658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->ShouldShowSpinner());
282758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
282858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
282958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
283058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AutofillDialogControllerTest,
283158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       SubmitButtonIsDisabled_SpinnerFinishesAfterDelay) {
28324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Reset Wallet state.
28334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());
28344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
283558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(1, controller()->get_submit_button_delay_count());
283658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
283758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Begin the submit button delay.
283858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->SimulateSubmitButtonDelayBegin();
283958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
284058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->ShouldShowSpinner());
284158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
284258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
284358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // End the submit button delay.
284458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->SimulateSubmitButtonDelayEnd();
284558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
284658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->ShouldShowSpinner());
284758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
284858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
284958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Stop the spinner.
285058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
285158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
285258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->ShouldShowSpinner());
285358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
285458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
285558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
285658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SubmitButtonIsDisabled_NoSpinner) {
28574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
28584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
285958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_EQ(1, controller()->get_submit_button_delay_count());
286058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
286158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Begin the submit button delay.
286258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->SimulateSubmitButtonDelayBegin();
286358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
286458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->ShouldShowSpinner());
286558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
286658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
286758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // End the submit button delay.
286858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  controller()->SimulateSubmitButtonDelayEnd();
286958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
287058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_FALSE(controller()->ShouldShowSpinner());
287158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
2872a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
2873a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
2874d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, IconsForFields_NoCreditCard) {
2875d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldValueMap values;
2876d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2877d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldIconMap icons = controller()->IconsForFields(values);
2878d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_TRUE(icons.empty());
2879d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
2880d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
2881d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, IconsForFields_CreditCardNumberOnly) {
2882d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldValueMap values;
2883d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2884d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa);
2885d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldIconMap icons = controller()->IconsForFields(values);
2886d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(1UL, icons.size());
2887d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER));
2888d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
2889d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
2890d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, IconsForFields_CvcOnly) {
2891d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldValueMap values;
2892d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2893d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123");
2894d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldIconMap icons = controller()->IconsForFields(values);
2895d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(1UL, icons.size());
2896d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE));
2897d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
2898d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
2899d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, IconsForFields_BothCreditCardAndCvc) {
2900d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldValueMap values;
2901d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
2902d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa);
2903d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123");
2904d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FieldIconMap icons = controller()->IconsForFields(values);
2905d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(2UL, icons.size());
2906d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE));
2907d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER));
2908d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
2909d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
2910d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, FieldControlsIcons) {
2911d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_TRUE(controller()->FieldControlsIcons(CREDIT_CARD_NUMBER));
2912d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_FALSE(controller()->FieldControlsIcons(CREDIT_CARD_VERIFICATION_CODE));
2913d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  EXPECT_FALSE(controller()->FieldControlsIcons(EMAIL_ADDRESS));
2914d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
2915d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
29164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SaveCreditCardIncludesName_NoBilling) {
29174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
29184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  CreditCard test_credit_card(test::GetVerifiedCreditCard());
29204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FillInputs(SECTION_CC, test_credit_card);
29214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  AutofillProfile test_profile(test::GetVerifiedProfile());
29234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FillInputs(SECTION_BILLING, test_profile);
29244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  UseBillingForShipping();
29264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
29284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->OnAccept();
29294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TestPersonalDataManager* test_pdm = controller()->GetTestingManager();
29314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const CreditCard& imported_card = test_pdm->imported_credit_card();
2932116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(test_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
29334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            imported_card.GetRawInfo(CREDIT_CARD_NAME));
29344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
29354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SaveCreditCardIncludesName_WithBilling) {
29374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  SwitchToAutofill();
29384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  TestPersonalDataManager* test_pdm = controller()->GetTestingManager();
29404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  AutofillProfile test_profile(test::GetVerifiedProfile());
29414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_CALL(*controller()->GetView(), ModelChanged());
29434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  test_pdm->AddTestingProfile(&test_profile);
29444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ASSERT_TRUE(controller()->MenuModelForSection(SECTION_BILLING));
29454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  CreditCard test_credit_card(test::GetVerifiedCreditCard());
29474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FillInputs(SECTION_CC, test_credit_card);
29484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
29504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->OnAccept();
29514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const CreditCard& imported_card = test_pdm->imported_credit_card();
2953116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(test_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
29544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            imported_card.GetRawInfo(CREDIT_CARD_NAME));
29554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  controller()->ViewClosed();
29574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
29584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
29598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, InputEditability) {
29608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Empty wallet items: all fields are editable.
29618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_ptr<wallet::WalletItems> items =
29628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
29638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  controller()->OnDidGetWalletItems(items.Pass());
29648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
29658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DialogSection sections[] = { SECTION_CC_BILLING, SECTION_SHIPPING };
29668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (size_t i = 0; i < arraysize(sections); ++i) {
29678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const DetailInputs& inputs =
29688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        controller()->RequestedFieldsForSection(sections[i]);
29698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    for (size_t j = 0; j < inputs.size(); ++j) {
29708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
29718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
29728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
29738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
29748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Expired instrument: CC number + CVV are not editable.
29758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
29768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_ptr<wallet::WalletItems::MaskedInstrument> expired_instrument =
29778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      wallet::GetTestMaskedInstrumentExpired();
29788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  items->AddInstrument(expired_instrument.Pass());
29798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  controller()->OnDidGetWalletItems(items.Pass());
29808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
29818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
29828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const DetailInputs& inputs =
29838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
2984f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  FieldValueMap outputs;
29858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  CopyInitialValues(inputs, &outputs);
29868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
29878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
29888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (size_t i = 0; i < arraysize(sections); ++i) {
29898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const DetailInputs& inputs =
29908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        controller()->RequestedFieldsForSection(sections[i]);
29918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    for (size_t j = 0; j < inputs.size(); ++j) {
29928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      if (inputs[j].type == CREDIT_CARD_NUMBER ||
29938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          inputs[j].type == CREDIT_CARD_VERIFICATION_CODE) {
29948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
29958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      } else {
29968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
29978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      }
29988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
29998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
30008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
30018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // User changes the billing address; same story.
3002a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[ADDRESS_BILLING_ZIP] = ASCIIToUTF16("77025");
30038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
30048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (size_t i = 0; i < arraysize(sections); ++i) {
30058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const DetailInputs& inputs =
30068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        controller()->RequestedFieldsForSection(sections[i]);
30078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    for (size_t j = 0; j < inputs.size(); ++j) {
30088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      if (inputs[j].type == CREDIT_CARD_NUMBER ||
30098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          inputs[j].type == CREDIT_CARD_VERIFICATION_CODE) {
30108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
30118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      } else {
30128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
30138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      }
30148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
30158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
30168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
30178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // User changes a detail of the CC itself (expiration date), CVV is now
30188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // editable (and mandatory).
3019a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  outputs[CREDIT_CARD_EXP_MONTH] = ASCIIToUTF16("06");
30208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
30218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (size_t i = 0; i < arraysize(sections); ++i) {
30228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const DetailInputs& inputs =
30238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        controller()->RequestedFieldsForSection(sections[i]);
30248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    for (size_t j = 0; j < inputs.size(); ++j) {
30258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      if (inputs[j].type == CREDIT_CARD_NUMBER)
30268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
30278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      else
30288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
30298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
30308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
30318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
30328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3033a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// When the default country is something besides US, wallet is not selected
3034a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// and the account chooser shouldn't be visible.
3035a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, HideWalletInOtherCountries) {
30365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Addresses from different countries.
30375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AutofillProfile us_profile(base::GenerateGUID(), kSettingsOrigin),
30385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      es_profile(base::GenerateGUID(), kSettingsOrigin),
30395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      es_profile2(base::GenerateGUID(), kSettingsOrigin);
30405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  us_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
30415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  es_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("ES"));
30425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  es_profile2.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("ES"));
30435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
30445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If US is indicated (via timezone), show Wallet.
3045a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ResetControllerWithFormData(DefaultFormData());
30465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->set_timezone_country_code("US");
3047a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  controller()->Show();
3048a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(
3049a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      controller()->AccountChooserModelForTesting()->WalletIsSelected());
3050a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  controller()->OnDidFetchWalletCookieValue(std::string());
3051a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3052a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(controller()->ShouldShowAccountChooser());
3053a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(
3054a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      controller()->AccountChooserModelForTesting()->WalletIsSelected());
3055a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
30565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If US is not indicated, don't show Wallet.
3057a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ResetControllerWithFormData(DefaultFormData());
30585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->set_timezone_country_code("ES");
3059a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  controller()->Show();
30605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidFetchWalletCookieValue(std::string());
30615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
3062a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_FALSE(controller()->ShouldShowAccountChooser());
30635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
30645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If US is indicated (via a profile), show Wallet.
30655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetControllerWithFormData(DefaultFormData());
30665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->set_timezone_country_code("ES");
30675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&us_profile);
30685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->Show();
30695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidFetchWalletCookieValue(std::string());
30705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
30715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(controller()->ShouldShowAccountChooser());
30725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(
30735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      controller()->AccountChooserModelForTesting()->WalletIsSelected());
30745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
30755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Make sure the profile doesn't just override the timezone.
30765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetControllerWithFormData(DefaultFormData());
30775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->set_timezone_country_code("US");
30785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&es_profile);
30795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->Show();
30805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidFetchWalletCookieValue(std::string());
30815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
30825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(controller()->ShouldShowAccountChooser());
30835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(
30845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      controller()->AccountChooserModelForTesting()->WalletIsSelected());
30855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
30865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Only takes one US address to enable Wallet.
30875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetControllerWithFormData(DefaultFormData());
30885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->set_timezone_country_code("FR");
30895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&es_profile);
30905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&es_profile2);
30915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&us_profile);
30925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->Show();
30935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidFetchWalletCookieValue(std::string());
30945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
30955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(controller()->ShouldShowAccountChooser());
30965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(
3097a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      controller()->AccountChooserModelForTesting()->WalletIsSelected());
3098a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
3099a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
31000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, DontGetWalletTillNecessary) {
31010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // When starting on local data mode, the dialog will provide a "Use Google
31020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Wallet" link.
31030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  profile()->GetPrefs()->SetBoolean(
31040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      ::prefs::kAutofillDialogPayWithoutWallet, true);
31050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ResetControllerWithFormData(DefaultFormData());
3106a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  controller()->Show();
31070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::string16 use_wallet_text = controller()->SignInLinkText();
31080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_EQ(TestAutofillDialogController::NOT_CHECKED,
31090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)            controller()->SignedInState());
31100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
31110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // When clicked, this link will ask for wallet items. If there's a signin
31120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // failure, the link will switch to "Sign in to use Google Wallet".
3113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
31140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  controller()->SignInLinkClicked();
31150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_NE(TestAutofillDialogController::NOT_CHECKED,
31160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)            controller()->SignedInState());
31170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  controller()->OnDidFetchWalletCookieValue(std::string());
31180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
31190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  controller()->OnPassiveSigninFailure(GoogleServiceAuthError(
31200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      GoogleServiceAuthError::CONNECTION_FAILED));
31210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  EXPECT_NE(use_wallet_text, controller()->SignInLinkText());
31220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
31230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
3124f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, MultiAccountSwitch) {
3125f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  std::vector<std::string> users;
3126f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  users.push_back("user_1@example.com");
3127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  users.push_back("user_2@example.com");
3128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  controller()->OnDidGetWalletItems(
3129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      wallet::GetTestWalletItemsWithUsers(users, 0));
3130f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3131f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Items should be: Account 1, account 2, add account, disable wallet.
3132f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_EQ(4, controller()->MenuModelForAccountChooser()->GetItemCount());
3133f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_EQ(0U, controller()->GetTestingWalletClient()->user_index());
3134f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3135f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // GetWalletItems should be called when the user switches accounts.
3136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
3137f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  controller()->MenuModelForAccountChooser()->ActivatedAt(1);
3138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The wallet client should be updated to the new user index.
3139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_EQ(1U, controller()->GetTestingWalletClient()->user_index());
3140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
3141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, PassiveAuthFailure) {
3143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  controller()->OnDidGetWalletItems(
3144f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      wallet::GetTestWalletItemsWithRequiredAction(
3145f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)           wallet::PASSIVE_GAIA_AUTH));
3146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(controller()->ShouldShowSpinner());
3147f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  controller()->OnPassiveSigninFailure(GoogleServiceAuthError(
3148f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      GoogleServiceAuthError::NONE));
3149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(controller()->ShouldShowSpinner());
3150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
3151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
31525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, WalletShippingSameAsBilling) {
31535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Assert initial state.
31545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
31555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ::prefs::kAutofillDialogWalletShippingSameAsBilling));
31565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
31575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Verify that false pref defaults to wallet defaults.
31585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<wallet::WalletItems> wallet_items =
31595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
31605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
31615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestShippingAddress());
31625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
31635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
31645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ::prefs::kAutofillDialogWalletShippingSameAsBilling));
31655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
31665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
31675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Set "Same as Billing" for the shipping address and verify it sets the pref
31685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // and selects the appropriate menu item.
31695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UseBillingForShipping();
31705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(0, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
31715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->ForceFinishSubmit();
31725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(
31735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ::prefs::kAutofillDialogWalletShippingSameAsBilling));
31745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
31755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Getting new wallet info shouldn't disrupt the preference and menu should be
31765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // set accordingly.
31775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Reset();
31785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
31795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
31805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  wallet_items->AddAddress(wallet::GetTestShippingAddress());
31815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnDidGetWalletItems(wallet_items.Pass());
31825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
31835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ::prefs::kAutofillDialogWalletShippingSameAsBilling));
31845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(0, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
31855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
31865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Choose a different address and ensure pref gets set to false.
31875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
31885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->ForceFinishSubmit();
31895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
31905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ::prefs::kAutofillDialogWalletShippingSameAsBilling));
31915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
31925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
31935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Verifies that a call to the IconsForFields() method before the card type is
31945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// known returns a placeholder image that is at least as large as the icons for
31955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// all of the supported major credit card issuers.
31965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, IconReservedForCreditCardField) {
31975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldValueMap inputs;
31985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  inputs[CREDIT_CARD_NUMBER] = base::string16();
31995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldIconMap icons = controller()->IconsForFields(inputs);
32015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, icons.size());
32025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(1U, icons.count(CREDIT_CARD_NUMBER));
32045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Image placeholder_icon = icons[CREDIT_CARD_NUMBER];
32055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Verify that the placeholder icon is at least as large as the icons for the
32075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // supported credit card issuers.
32085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const int kSupportedCardIdrs[] = {
32095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IDR_AUTOFILL_CC_AMEX,
32105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IDR_AUTOFILL_CC_DINERS,
32115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IDR_AUTOFILL_CC_DISCOVER,
32125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IDR_AUTOFILL_CC_GENERIC,
32135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IDR_AUTOFILL_CC_JCB,
32145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IDR_AUTOFILL_CC_MASTERCARD,
32155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IDR_AUTOFILL_CC_VISA,
32165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
32175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
32185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (size_t i = 0; i < arraysize(kSupportedCardIdrs); ++i) {
32195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SCOPED_TRACE(base::IntToString(i));
32205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::Image supported_card_icon = rb.GetImageNamed(kSupportedCardIdrs[i]);
32215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_GE(placeholder_icon.Width(), supported_card_icon.Width());
32225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_GE(placeholder_icon.Height(), supported_card_icon.Height());
32235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
32245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
32255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, CountryChangeUpdatesSection) {
32275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TestAutofillDialogView* view = controller()->GetView();
32285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  view->ClearSectionUpdates();
32295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->UserEditedOrActivatedInput(SECTION_SHIPPING,
32315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           ADDRESS_HOME_COUNTRY,
32325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           gfx::NativeView(),
32335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           gfx::Rect(),
3234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                           ASCIIToUTF16("Belarus"),
32355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           true);
32365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::map<DialogSection, size_t> updates = view->section_updates();
32375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, updates[SECTION_SHIPPING]);
32385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, updates.size());
32395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  view->ClearSectionUpdates();
32415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->UserEditedOrActivatedInput(SECTION_CC_BILLING,
32435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           ADDRESS_BILLING_COUNTRY,
32445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           gfx::NativeView(),
32455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           gfx::Rect(),
32465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           ASCIIToUTF16("France"),
32475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           true);
32485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  updates = view->section_updates();
32495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, updates[SECTION_CC_BILLING]);
32505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, updates.size());
32515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SwitchToAutofill();
32535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  view->ClearSectionUpdates();
32545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->UserEditedOrActivatedInput(SECTION_BILLING,
32565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           ADDRESS_BILLING_COUNTRY,
32575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           gfx::NativeView(),
32585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           gfx::Rect(),
32595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           ASCIIToUTF16("Italy"),
32605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           true);
32615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  updates = view->section_updates();
32625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, updates[SECTION_BILLING]);
32635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1U, updates.size());
32645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
32655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3266a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, CorrectCountryFromInputs) {
32675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*controller()->GetMockValidator(),
3268a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)              ValidateAddress(CountryCodeMatcher("DE"), _, _));
32695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldValueMap billing_inputs;
3271a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  billing_inputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("Germany");
32725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->InputsAreValid(SECTION_BILLING, billing_inputs);
32735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*controller()->GetMockValidator(),
32755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              ValidateAddress(CountryCodeMatcher("FR"), _, _));
32765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldValueMap shipping_inputs;
32785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  shipping_inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("France");
32795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->InputsAreValid(SECTION_SHIPPING, shipping_inputs);
32805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
32815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3282a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, ValidationRulesLoadedOnCountryChange) {
32835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetControllerWithFormData(DefaultFormData());
32840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_CALL(*controller()->GetMockValidator(),
32850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              LoadRules("US")).Times(AtLeast(1));
32865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->Show();
32875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("FR"));
32890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  controller()->UserEditedOrActivatedInput(SECTION_BILLING,
32905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           ADDRESS_BILLING_COUNTRY,
32915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           gfx::NativeView(),
32925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           gfx::Rect(),
32935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           ASCIIToUTF16("France"),
32945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           true);
32955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
32965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
32970529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochTEST_F(AutofillDialogControllerTest, UsValidationRulesLoadedForJpOnlyProfile) {
32980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ResetControllerWithFormData(DefaultFormData());
32990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  AutofillProfile jp_profile(base::GenerateGUID(), kSettingsOrigin);
33000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  jp_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"));
33010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  controller()->GetTestingManager()->AddTestingProfile(&jp_profile);
33020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("US"));
33030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  EXPECT_CALL(*controller()->GetMockValidator(),
33040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              LoadRules("JP")).Times(AtLeast(1));
33050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  controller()->Show();
33060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
33070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3308a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, InvalidWhenRulesNotReady) {
33095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Select "Add new shipping address...".
33105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
33115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If the rules haven't loaded yet, validation errors should show on submit.
33135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*controller()->GetMockValidator(),
33145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              ValidateAddress(CountryCodeMatcher("US"), _, _)).
33155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              WillRepeatedly(Return(AddressValidator::RULES_NOT_READY));
33165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldValueMap inputs;
33185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  inputs[ADDRESS_HOME_ZIP] = ASCIIToUTF16("1234");
33195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("United States");
33205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ValidityMessages messages =
33225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      controller()->InputsAreValid(SECTION_SHIPPING, inputs);
33235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(messages.GetMessageOrDefault(ADDRESS_HOME_ZIP).text.empty());
33245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(messages.HasSureError(ADDRESS_HOME_ZIP));
33255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Country should never show an error message as it's always valid.
33265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(messages.GetMessageOrDefault(ADDRESS_HOME_COUNTRY).text.empty());
33275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
33285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3329a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, ValidButUnverifiedWhenRulesFail) {
33305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SwitchToAutofill();
33315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Add suggestions so the credit card and billing sections aren't showing
33335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // their manual inputs (to isolate to just shipping).
33345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AutofillProfile verified_profile(test::GetVerifiedProfile());
33355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
33365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CreditCard verified_card(test::GetVerifiedCreditCard());
33375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&verified_card);
33385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Select "Add new shipping address...".
33405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(2);
33415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If the rules are unavailable, validation errors should not show.
33435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_CALL(*controller()->GetMockValidator(),
33445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              ValidateAddress(CountryCodeMatcher("US"), _, _)).
33455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              WillRepeatedly(Return(AddressValidator::RULES_UNAVAILABLE));
33465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FieldValueMap outputs;
33485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AutofillProfile full_profile(test::GetFullProfile());
33495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const DetailInputs& inputs =
33505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      controller()->RequestedFieldsForSection(SECTION_SHIPPING);
33515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (size_t i = 0; i < inputs.size(); ++i) {
33525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const ServerFieldType type = inputs[i].type;
33535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
33545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
33555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs);
33565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
33575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  controller()->OnAccept();
33585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Profiles saved while rules are unavailable shouldn't be verified.
33605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const AutofillProfile& imported_profile =
33615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      controller()->GetTestingManager()->imported_profile();
3362116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_EQ(imported_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
3363116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            full_profile.GetInfo(AutofillType(NAME_FULL), "en-US"));
33645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(imported_profile.origin(), GURL(kSourceUrl).GetOrigin().spec());
33655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(imported_profile.IsVerified());
33665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
33675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3368a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, LimitedCountryChoices) {
3369a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ui::ComboboxModel* shipping_country_model =
3370a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3371a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const int default_number_of_countries =
3372a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      shipping_country_model->GetItemCount();
3373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // We show a lot of countries by default, but the exact number doesn't matter.
3374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_GT(default_number_of_countries, 50);
3375a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Create a form data that simulates:
3377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //   <select autocomplete="billing country">
3378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //     <option value="AU">Down Under</option>
3379a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //     <option value="">fR</option>  <!-- Case doesn't matter -->
3380a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //     <option value="GRMNY">Germany</option>
3381a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //   </select>
3382a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Only country codes are respected, whether they're in value or the option's
3383a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // text content. Thus the first two options should be recognized.
3384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FormData form_data;
3385a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FormFieldData field;
3386a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  field.autocomplete_attribute = "billing country";
3387a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  field.option_contents.push_back(ASCIIToUTF16("Down Under"));
3388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  field.option_values.push_back(ASCIIToUTF16("AU"));
3389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  field.option_contents.push_back(ASCIIToUTF16("Fr"));
3390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  field.option_values.push_back(ASCIIToUTF16(""));
3391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  field.option_contents.push_back(ASCIIToUTF16("Germany"));
3392a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  field.option_values.push_back(ASCIIToUTF16("GRMNY"));
33930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
33940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  FormFieldData cc_field;
33950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cc_field.autocomplete_attribute = "cc-csc";
33960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
3397a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  form_data.fields.push_back(field);
33980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  form_data.fields.push_back(cc_field);
3399a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ResetControllerWithFormData(form_data);
3400a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->Show();
3401a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3402a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Shipping model shouldn't have changed.
3403a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  shipping_country_model =
3404a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3405a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(default_number_of_countries,
3406a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            shipping_country_model->GetItemCount());
3407a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Billing model now only has two items.
3408a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ui::ComboboxModel* billing_country_model =
3409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      controller()->ComboboxModelForAutofillType(ADDRESS_BILLING_COUNTRY);
3410a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_EQ(2, billing_country_model->GetItemCount());
3411a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(billing_country_model->GetItemAt(0), ASCIIToUTF16("Australia"));
3412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(billing_country_model->GetItemAt(1), ASCIIToUTF16("France"));
3413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3414a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Make sure it also applies to profile suggestions.
3415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AutofillProfile us_profile(test::GetVerifiedProfile());
3416a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  us_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
3417a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&us_profile);
3418a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Don't show a suggestion if the only one that exists is disabled.
3419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(
3420a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      controller()->SuggestionStateForSection(SECTION_BILLING).visible);
3421a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3422a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Add a profile with an acceptable country; suggestion should be shown.
3423a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ResetControllerWithFormData(form_data);
3424a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->Show();
3425a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  AutofillProfile au_profile(test::GetVerifiedProfile2());
3426a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  au_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("AU"));
3427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&us_profile);
3428a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&au_profile);
3429a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING);
3430a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(model);
3431a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(4, model->GetItemCount());
3432a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(model->IsEnabledAt(0));
3433a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(model->IsEnabledAt(1));
3434a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3435a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Add <input type="text" autocomplete="billing country"></input>
3436a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // This should open up selection of all countries again.
3437a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FormFieldData field2;
3438a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  field2.autocomplete_attribute = "billing country";
3439a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  form_data.fields.push_back(field2);
3440a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ResetControllerWithFormData(form_data);
3441a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->Show();
3442a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3443a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  billing_country_model =
3444a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      controller()->ComboboxModelForAutofillType(ADDRESS_BILLING_COUNTRY);
3445a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(default_number_of_countries,
3446a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            billing_country_model->GetItemCount());
3447a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
3448a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3449116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(AutofillDialogControllerTest, WalletUnsupportedCountries) {
3450116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Create a form data that simulates:
3451116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //   <select autocomplete="billing country">
3452116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //     <option value="IQ">Iraq</option>
3453116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //     <option value="MX">Mexico</option>
3454116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //   </select>
3455116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // i.e. contains a mix of supported and unsupported countries.
3456116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FormData form_data;
3457116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FormFieldData field;
3458116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  field.autocomplete_attribute = "shipping country";
3459116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  field.option_contents.push_back(ASCIIToUTF16("Iraq"));
3460116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  field.option_values.push_back(ASCIIToUTF16("IQ"));
3461116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  field.option_contents.push_back(ASCIIToUTF16("Mexico"));
3462116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  field.option_values.push_back(ASCIIToUTF16("MX"));
3463116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3464116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FormFieldData cc_field;
3465116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  cc_field.autocomplete_attribute = "cc-csc";
3466116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3467116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  form_data.fields.push_back(field);
3468116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  form_data.fields.push_back(cc_field);
3469116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ResetControllerWithFormData(form_data);
3470116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  controller()->Show();
3471116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3472116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ui::ComboboxModel* shipping_country_model =
3473116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3474116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_EQ(2, shipping_country_model->GetItemCount());
3475116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Iraq"));
3476116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(shipping_country_model->GetItemAt(1), ASCIIToUTF16("Mexico"));
3477116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3478116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Switch to Wallet, add limitations.
3479116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SetUpControllerWithFormData(form_data);
3480116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SwitchToWallet();
3481116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<wallet::WalletItems> wallet_items =
3482116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
3483116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  wallet_items->AddAllowedShippingCountry("MX");
3484116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  wallet_items->AddAllowedShippingCountry("GB");
3485116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  controller()->OnDidGetWalletItems(wallet_items.Pass());
3486116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3487116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Only the intersection is available.
3488116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(controller()->IsPayingWithWallet());
3489116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  shipping_country_model =
3490116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3491116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_EQ(1, shipping_country_model->GetItemCount());
3492116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Mexico"));
3493116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3494116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Empty intersection; Wallet's automatically disabled.
3495116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
3496116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  wallet_items->AddAllowedShippingCountry("CA");
3497116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  wallet_items->AddAllowedShippingCountry("GB");
3498116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  controller()->OnDidGetWalletItems(wallet_items.Pass());
3499116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_FALSE(controller()->IsPayingWithWallet());
3500116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3501116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Since it's disabled, we revert to accepting all the countries.
3502116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  shipping_country_model =
3503116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
3504116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ASSERT_EQ(2, shipping_country_model->GetItemCount());
3505116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Iraq"));
3506116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(shipping_country_model->GetItemAt(1), ASCIIToUTF16("Mexico"));
3507116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
3508116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3509116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// http://crbug.com/388018
3510116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTEST_F(AutofillDialogControllerTest, NoCountryChoices) {
3511116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Create a form data that simulates:
3512116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //   <select autocomplete="billing country">
3513116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //     <option value="ATL">Atlantis</option>
3514116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //     <option value="ELD">Eldorado</option>
3515116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //   </select>
3516116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // i.e. contains a list of no valid countries.
3517116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FormData form_data;
3518116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FormFieldData field;
3519116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  field.autocomplete_attribute = "billing country";
3520116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  field.option_contents.push_back(ASCIIToUTF16("Atlantis"));
3521116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  field.option_values.push_back(ASCIIToUTF16("ATL"));
3522116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  field.option_contents.push_back(ASCIIToUTF16("Eldorado"));
3523116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  field.option_values.push_back(ASCIIToUTF16("ELD"));
3524116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3525116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FormFieldData cc_field;
3526116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  cc_field.autocomplete_attribute = "cc-csc";
3527116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3528116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  form_data.fields.push_back(field);
3529116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  form_data.fields.push_back(cc_field);
3530116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ResetControllerWithFormData(form_data);
3531116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  controller()->Show();
3532116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3533116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Controller aborts and self destructs.
3534116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(0, controller());
3535116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
3536116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3537cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, LimitedCcChoices) {
3538cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SwitchToAutofill();
3539cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Typically, MC and Visa are both valid.
3540cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true);
3541cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
3542cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3543cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  FormData form_data;
3544cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  FormFieldData field;
3545cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  field.autocomplete_attribute = "billing cc-type";
3546cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  field.option_contents.push_back(ASCIIToUTF16("Visa"));
3547cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  field.option_values.push_back(ASCIIToUTF16("V"));
3548cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  field.option_contents.push_back(ASCIIToUTF16("American Express"));
3549cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  field.option_values.push_back(ASCIIToUTF16("AX"));
3550cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  form_data.fields.push_back(field);
3551cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ResetControllerWithFormData(form_data);
3552cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  controller()->Show();
3553cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3554cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // MC is not valid because it's missing from FormData.
3555116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ(l10n_util::GetStringUTF16(
3556116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                IDS_AUTOFILL_DIALOG_VALIDATION_UNACCEPTED_MASTERCARD),
3557116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, false));
3558cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
3559cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3560cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CreditCard visa_card(test::GetVerifiedCreditCard());
3561cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CreditCard amex_card(test::GetVerifiedCreditCard2());
3562cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3563cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CreditCard master_card(base::GenerateGUID(), "chrome settings");
3564cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  test::SetCreditCardInfo(
3565cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      &master_card, "Mr Foo", "5105105105105100", "07", "2099");
3566cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3567cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&visa_card);
3568cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&amex_card);
3569cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  controller()->GetTestingManager()->AddTestingCreditCard(&master_card);
3570cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3571cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The stored MC is disabled in the dropdown.
3572cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ui::MenuModel* model = controller()->MenuModelForSection(SECTION_CC);
3573cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_TRUE(model);
3574cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ASSERT_EQ(5, model->GetItemCount());
3575cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(model->IsEnabledAt(0));
3576cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(model->IsEnabledAt(1));
3577cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(model->IsEnabledAt(2));
3578cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(model->IsEnabledAt(3));
3579cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(model->IsEnabledAt(4));
3580cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3581cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // No MC; Wallet is disabled.
3582cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetUpControllerWithFormData(form_data);
3583cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(controller()->IsPayingWithWallet());
3584cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3585cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // In Autofill mode, Discover is disallowed because it's not in FormData.
3586cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, false);
3587cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3588cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  field.option_contents.push_back(ASCIIToUTF16("Mastercard"));
3589cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  field.option_values.push_back(ASCIIToUTF16("Mastercard"));
3590cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  form_data.fields[0] = field;
3591cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3592cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Add MC to FormData; Wallet is enabled.
3593cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetUpControllerWithFormData(form_data);
3594cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(controller()->IsPayingWithWallet());
3595cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Even though Discover isn't in FormData, it's allowed because Wallet always
3596cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // generates a MC Virtual card.
3597cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
3598cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
3599cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
360023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)TEST_F(AutofillDialogControllerTest, SuggestCountrylessProfiles) {
360123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  SwitchToAutofill();
360223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
3603effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  FieldValueMap outputs;
3604effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  outputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("US");
3605effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs);
3606effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
360723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  AutofillProfile profile(test::GetVerifiedProfile());
360823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("The Man Without a Country"));
360923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  profile.SetRawInfo(ADDRESS_HOME_COUNTRY, base::string16());
361023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  controller()->GetTestingManager()->AddTestingProfile(&profile);
3611a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3612a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  controller()->UserEditedOrActivatedInput(
3613a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      SECTION_SHIPPING,
3614a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      NAME_FULL,
3615a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      gfx::NativeView(),
3616a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      gfx::Rect(),
361723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      profile.GetRawInfo(NAME_FULL).substr(0, 1),
3618a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      true);
3619a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_EQ(NAME_FULL, controller()->popup_input_type());
3620a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
3621a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3622c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen MurdochTEST_F(AutofillDialogControllerTest, SwitchFromWalletWithFirstName) {
3623c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  controller()->MenuModelForSection(SECTION_CC_BILLING)->ActivatedAt(2);
3624c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
3625c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  FieldValueMap outputs;
3626c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  outputs[NAME_FULL] = ASCIIToUTF16("madonna");
3627c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
3628c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
3629c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ASSERT_NO_FATAL_FAILURE(SwitchToAutofill());
3630c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
3631c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
3632f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Regression test for http://crbug.com/382777
3633f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)TEST_F(AutofillDialogControllerTest, WalletBillingCountry) {
3634f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  FormFieldData cc_field;
3635f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  cc_field.autocomplete_attribute = "cc-number";
3636f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  FormFieldData billing_country, billing_country_name, shipping_country,
3637f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      shipping_country_name;
3638f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  billing_country.autocomplete_attribute = "billing country";
3639f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  billing_country_name.autocomplete_attribute = "billing country-name";
3640f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  shipping_country.autocomplete_attribute = "shipping country";
3641f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  shipping_country_name.autocomplete_attribute = "shipping country-name";
3642f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
3643f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  FormData form_data;
3644f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  form_data.fields.push_back(cc_field);
3645f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  form_data.fields.push_back(billing_country);
3646f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  form_data.fields.push_back(billing_country_name);
3647f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  form_data.fields.push_back(shipping_country);
3648f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  form_data.fields.push_back(shipping_country_name);
3649f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
3650f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  SetUpControllerWithFormData(form_data);
3651f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  AcceptAndLoadFakeFingerprint();
3652f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
3653f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  controller()->ForceFinishSubmit();
3654f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
3655f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  ASSERT_EQ(5U, form_structure()->field_count());
3656f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3657f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            form_structure()->field(1)->Type().GetStorableType());
3658f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("US"), form_structure()->field(1)->value);
3659f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3660f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            form_structure()->field(2)->Type().GetStorableType());
3661f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("United States"), form_structure()->field(2)->value);
3662f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3663f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            form_structure()->field(3)->Type().GetStorableType());
3664f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("US"), form_structure()->field(3)->value);
3665f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(ADDRESS_HOME_COUNTRY,
3666f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            form_structure()->field(4)->Type().GetStorableType());
3667f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(ASCIIToUTF16("United States"), form_structure()->field(4)->value);
3668f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
3669f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
36702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace autofill
3671