1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.autofill;
7 import junit.framework.Assert;
9 import org.chromium.base.ThreadUtils;
10 import org.chromium.chrome.browser.autofill.AutofillDialogControllerAndroid;
11 import org.chromium.chrome.browser.autofill.AutofillDialogControllerAndroid.AutofillDialog;
12 import org.chromium.chrome.browser.autofill.AutofillDialogControllerAndroid.AutofillDialogDelegate;
13 import org.chromium.chrome.browser.autofill.AutofillDialogControllerAndroid.AutofillDialogFactory;
14 import org.chromium.chrome.browser.autofill.AutofillDialogResult;
15 import org.chromium.ui.WindowAndroid;
18 * Mock of the AutofillDialog that returns a predefined set of values.
20 public class MockAutofillDialogController implements AutofillDialog {
21 private final AutofillDialogResult.ResultWallet mResult;
22 private final boolean mLastUsedChoiceIsAutofill;
23 private final String mLastUsedAccountName;
24 private final String mGuidDefaultBilling;
25 private final String mGuidDefaultShipping;
26 private final String mGuidDefaultCard;
28 private AutofillDialogDelegate mDelegate;
31 * Installs the mock AutofillDialog implementation.
33 public static void installMockFactory(
34 final long delayMillis,
35 final AutofillDialogResult.ResultWallet result,
36 final boolean lastUsedChoiceIsAutofill,
37 final String lastUsedAccountName,
38 final String guidDefaultBilling,
39 final String guidDefaultShipping,
40 final String guidDefaultCard,
41 final boolean shouldRequestFullBilling,
42 final boolean shouldRequestShipping,
43 final boolean shouldRequestPhoneNumbers) {
44 AutofillDialogControllerAndroid.setDialogFactory(new AutofillDialogFactory() {
46 public AutofillDialog createDialog(
47 final AutofillDialogDelegate delegate,
48 final WindowAndroid windowAndroid,
49 final boolean requestFullBillingAddress,
50 final boolean requestShippingAddress,
51 final boolean requestPhoneNumbers,
52 final boolean incognitoMode,
53 final boolean initialChoiceIsAutofill,
54 final String initialWalletAccountName,
55 final String initialBillingGuid,
56 final String initialShippingGuid,
57 final String initialCardGuid,
58 final String merchantDomain) {
59 Assert.assertEquals("Full billing details flag doesn't match",
60 shouldRequestFullBilling, requestFullBillingAddress);
61 Assert.assertEquals("Shipping details flag doesn't match",
62 shouldRequestShipping, requestShippingAddress);
63 Assert.assertEquals("Phone numbers flag doesn't match",
64 shouldRequestPhoneNumbers, requestPhoneNumbers);
65 return new MockAutofillDialogController(
69 lastUsedChoiceIsAutofill,
75 requestFullBillingAddress, requestShippingAddress,
78 initialChoiceIsAutofill, initialWalletAccountName,
79 initialBillingGuid, initialShippingGuid, initialCardGuid,
85 private MockAutofillDialogController(
86 final AutofillDialogControllerAndroid.AutofillDialogDelegate delegate,
87 final long delayMillis,
88 final AutofillDialogResult.ResultWallet result,
89 final boolean lastUsedChoiceIsAutofill,
90 final String lastUsedAccountName,
91 final String guidDefaultBilling,
92 final String guidDefaultShipping,
93 final String guidDefaultCard,
94 final WindowAndroid windowAndroid,
95 final boolean requestFullBillingAddress, final boolean requestShippingAddress,
96 final boolean requestPhoneNumbers,
97 final boolean incognitoMode,
98 final boolean initialChoiceIsAutofill, final String initialWalletAccountName,
99 final String initialBillingGuid, final String initialShippingGuid,
100 final String initialCardGuid,
101 final String merchantDomain) {
102 mDelegate = delegate;
104 mLastUsedChoiceIsAutofill = lastUsedChoiceIsAutofill;
105 mLastUsedAccountName = lastUsedAccountName;
106 mGuidDefaultBilling = guidDefaultBilling;
107 mGuidDefaultShipping = guidDefaultShipping;
108 mGuidDefaultCard = guidDefaultCard;
109 ThreadUtils.postOnUiThreadDelayed(new Runnable() {
112 triggerResultCallback();
117 // AutofillDialog implementation:
120 public void onDestroy() {
124 public void triggerResultCallback() {
125 if (mDelegate == null) return;
127 if (mResult == null) {
128 mDelegate.dialogCancel();
132 mDelegate.dialogContinue(
134 mLastUsedChoiceIsAutofill, mLastUsedAccountName,
135 mGuidDefaultBilling, mGuidDefaultShipping, mGuidDefaultCard);