Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / android / javatests / src / org / chromium / chrome / browser / autofill / AutofillDialogControllerTest.java
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.autofill;
6
7 import android.test.suitebuilder.annotation.MediumTest;
8 import android.test.suitebuilder.annotation.SmallTest;
9 import android.text.TextUtils;
10
11 import org.chromium.base.CommandLine;
12 import org.chromium.base.test.util.Feature;
13 import org.chromium.base.test.util.UrlUtils;
14 import org.chromium.chrome.shell.ChromeShellTestBase;
15 import org.chromium.content.browser.ContentViewCore;
16 import org.chromium.content.browser.test.util.Criteria;
17 import org.chromium.content.browser.test.util.CriteriaHelper;
18 import org.chromium.content.browser.test.util.DOMUtils;
19
20 import java.util.concurrent.TimeoutException;
21
22 /**
23  * Integration tests for the AutofillPopup.
24  */
25 public class AutofillDialogControllerTest extends ChromeShellTestBase {
26     private static final String SWITCH_REDUCE_SECURITY_FOR_TESTING = "reduce-security-for-testing";
27     private static final long DIALOG_CALLBACK_DELAY_MILLISECONDS = 50;
28
29     private static final String TEST_NAME = "Joe Doe";
30     private static final String TEST_PHONE = "(415)413-0703";
31     private static final String TEST_PHONE_UNFORMATTED = "4154130703";
32     private static final String TEST_EMAIL = "email@server.com";
33
34     private static final String TEST_CC_NUMBER = "4111111111111111";
35     private static final String TEST_CC_CSC = "123";
36     private static final int TEST_CC_EXP_MONTH = 11;
37     private static final int TEST_CC_EXP_YEAR = 2015;
38
39     private static final String TEST_BILLING1 = "123 Main street";
40     private static final String TEST_BILLING2 = "apt 456";
41     private static final String TEST_BILLING3 = "leave at the office";
42     private static final String TEST_BILLING_STREET =
43             TEST_BILLING1 + "\n" + TEST_BILLING2 + "\n" + TEST_BILLING3;
44     private static final String TEST_BILLING_CITY = "Schenectady";
45     private static final String TEST_BILLING_DL = "";  // dependent locality
46     private static final String TEST_BILLING_STATE = "NY";
47     private static final String TEST_BILLING_ZIP = "12345";
48     private static final String TEST_BILLING_SORTING_CODE = "";  // sorting code
49     private static final String TEST_BILLING_COUNTRY = "US";
50     private static final String TEST_BILLING_LANGUAGE = "";  // language
51
52     private static final String TEST_SHIPPING_NAME = "Mister Receiver";
53     private static final String TEST_SHIPPING_PHONE = "+46 8 713 99 99";
54     private static final String TEST_SHIPPING_PHONE_UNFORMATTED = "4687139999";
55     private static final String TEST_SHIPPING1 = "19 Farstaplan";
56     private static final String TEST_SHIPPING2 = "Third floor";
57     private static final String TEST_SHIPPING3 = "please call first";
58     private static final String TEST_SHIPPING_STREET =
59             TEST_SHIPPING1 + "\n" + TEST_SHIPPING2 + "\n" + TEST_SHIPPING3;
60     private static final String TEST_SHIPPING_CITY = "Farsta";
61     private static final String TEST_SHIPPING_DL = "";  // dependent locality
62     private static final String TEST_SHIPPING_STATE = "Stockholm";
63     private static final String TEST_SHIPPING_ZIP = "12346";
64     private static final String TEST_SHIPPING_SORTING_CODE = "";  // sorting code
65     private static final String TEST_SHIPPING_COUNTRY = "SE";
66     private static final String TEST_SHIPPING_LANGUAGE = "";  // language
67
68     private static final String HTML_PRELUDE = "<html>"
69             + "<head>"
70             + "  <meta name=\"viewport\""
71             + "      content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0\" />"
72             + "</head>"
73             + "<body>"
74             + "<form id=\"id-form\">"
75             + "  <button id=\"id-button\">DO INTERACTIVE AUTOCOMPLETE</button>";
76
77     private static final String HTML_POSTLUDE = "</form>"
78             + "<div id=\"was-autocompleted\">no</div>"
79             + ":<div id=\"autocomplete-failure-reason\"></div>"
80             + "<script>"
81             + "var form = document.forms[0];"
82             + "form.onsubmit = function(e) {"
83             + "  e.preventDefault();"
84             + "  form.requestAutocomplete();"
85             + "};"
86             + "form.onautocomplete = function() {"
87             + "  document.getElementById('was-autocompleted').textContent = 'succeeded';"
88             + "};"
89             + "form.onautocompleteerror = function(e) {"
90             + "  document.getElementById('was-autocompleted').textContent = 'failed';"
91             + "  document.getElementById('autocomplete-failure-reason').textContent = e.reason;"
92             + "};"
93             + "</script></body></html>";
94
95     private static String generatePage(
96             boolean requestFullBilling, boolean requestShipping, boolean requestPhoneNumbers) {
97         StringBuilder sb = new StringBuilder();
98         sb.append(HTML_PRELUDE);
99         sb.append("<fieldset>"
100                 + "<input id=\"id-billing-name\" autocomplete=\"billing name\" value=\"W\">"
101                 + "<input id=\"id-cc-name\" autocomplete=\"cc-name\" value=\"W\">"
102                 + "<input id=\"id-email\" autocomplete=\"email\" type=\"email\" value=\"W@W.W\">"
103                 + "<input id=\"id-cc-number\" autocomplete=\"cc-number\" value=\"W\">"
104                 + "<input id=\"id-cc-exp\" "
105                 + "  autocomplete=\"cc-exp\" type=\"month\" value=\"1111-11\">"
106                 + "<select id=\"id-cc-exp-month\" autocomplete=\"cc-exp-month\">"
107                 + "  <option value=\"1\" selected>1</option>"
108                 + "  <option value=\"2\">2</option>"
109                 + "  <option value=\"3\">3</option>"
110                 + "  <option value=\"4\">4</option>"
111                 + "  <option value=\"5\">5</option>"
112                 + "  <option value=\"6\">6</option>"
113                 + "  <option value=\"7\">7</option>"
114                 + "  <option value=\"8\">8</option>"
115                 + "  <option value=\"9\">9</option>"
116                 + "  <option value=\"10\">10</option>"
117                 + "  <option value=\"11\">11</option>"
118                 + "  <option value=\"12\">12</option>"
119                 + "</select>"
120                 + "<select id=\"id-cc-exp-year\" autocomplete=\"cc-exp-year\">"
121                 + "  <option value=\"2011\" selected>2011</option>"
122                 + "  <option value=\"2012\">2012</option>"
123                 + "  <option value=\"2013\">2013</option>"
124                 + "  <option value=\"2014\">2014</option>"
125                 + "  <option value=\"2015\">2015</option>"
126                 + "</select>"
127                 + "<input id=\"id-cc-csc\" autocomplete=\"cc-csc\" value=\"W\">"
128                 + "<input id=\"id-cc-zip\" autocomplete=\"billing postal-code\" value=\"W\">");
129         if (requestFullBilling) {
130             sb.append("<input id=\"id-cc-1\" autocomplete=\"billing address-line1\" value=\"W\">"
131                     + "<input id=\"id-cc-2\" autocomplete=\"billing address-line2\" value=\"W\">"
132                     + "<textarea id=\"id-cc-str\""
133                     + "    autocomplete=\"billing street-address\">W</textarea>"
134                     + "<input id=\"id-cc-city\" autocomplete=\"billing locality\" value=\"W\">"
135                     + "<input id=\"id-cc-state\" autocomplete=\"billing region\" value=\"W\">"
136                     + "<select id=\"id-cc-country\" autocomplete=\"billing country\">"
137                     + "  <option value=\"NL\" selected>Netherlands</option>"
138                     + "  <option value=\"US\">United States</option>"
139                     + "  <option value=\"SE\">Sweden</option>"
140                     + "  <option value=\"RU\">Russia</option>"
141                     + "</select>");
142         }
143         sb.append("</fieldset>");
144         if (requestShipping) {
145             sb.append("<fieldset>"
146                     + "<input id=\"id-name\" autocomplete=\"name\" value=\"W\">"
147                     + "<input id=\"id-h-name\" autocomplete=\"shipping name\" value=\"W\">"
148                     + "<input id=\"id-h-1\" autocomplete=\"shipping address-line1\" value=\"W\">"
149                     + "<input id=\"id-h-2\" autocomplete=\"shipping address-line2\" value=\"W\">"
150                     + "<textarea id=\"id-h-str\""
151                     + "    autocomplete=\"shipping street-address\">W</textarea>"
152                     + "<input id=\"id-h-city\" autocomplete=\"shipping locality\" value=\"W\">"
153                     + "<input id=\"id-h-state\" autocomplete=\"shipping region\" value=\"W\">"
154                     + "<input id=\"id-h-zip\" autocomplete=\"shipping postal-code\" value=\"W\">"
155                     + "<select id=\"id-h-country\" autocomplete=\"shipping country\">"
156                     + "  <option value=\"NL\" selected>Netherlands</option>"
157                     + "  <option value=\"US\">United States</option>"
158                     + "  <option value=\"SE\">Sweden</option>"
159                     + "  <option value=\"RU\">Russia</option>"
160                     + "</select>"
161                     + "</fieldset>");
162         }
163         if (requestPhoneNumbers) {
164             sb.append("<fieldset>"
165                     + "<input id=\"id-cc-tel\" autocomplete=\"billing tel\" value=\"W\">");
166             if (requestShipping) {
167                 sb.append("<input id=\"id-h-tel\" autocomplete=\"shipping tel\" value=\"W\">"
168                         + "<input id=\"id-tel\" autocomplete=\"tel\" value=\"W\">");
169             }
170             sb.append("</fieldset>");
171         }
172         sb.append(HTML_POSTLUDE);
173         return UrlUtils.encodeHtmlDataUri(sb.toString());
174     }
175
176     @Override
177     public void setUp() throws Exception {
178         super.setUp();
179         clearAppData();
180         CommandLine.init(new String[]{});
181         CommandLine.getInstance().appendSwitch(SWITCH_REDUCE_SECURITY_FOR_TESTING);
182     }
183
184     @MediumTest
185     @Feature({"autofill"})
186     public void testFieldsAreFilledMinimal() throws InterruptedException, TimeoutException {
187         final boolean requestFullBilling = false;
188         final boolean requestShipping = false;
189         final boolean requestPhoneNumbers = false;
190         verifyFieldsAreFilled(requestFullBilling, requestShipping, requestPhoneNumbers);
191     }
192
193     @MediumTest
194     @Feature({"autofill"})
195     public void testFieldsAreFilledFullBilling() throws InterruptedException, TimeoutException {
196         final boolean requestFullBilling = true;
197         final boolean requestShipping = false;
198         final boolean requestPhoneNumbers = false;
199         verifyFieldsAreFilled(requestFullBilling, requestShipping, requestPhoneNumbers);
200     }
201
202     @MediumTest
203     @Feature({"autofill"})
204     public void testFieldsAreFilledShipping() throws InterruptedException, TimeoutException {
205         final boolean requestFullBilling = true;
206         final boolean requestShipping = true;
207         final boolean requestPhoneNumbers = false;
208         verifyFieldsAreFilled(requestFullBilling, requestShipping, requestPhoneNumbers);
209     }
210
211     @MediumTest
212     @Feature({"autofill"})
213     public void testFieldsAreFilledBillingPhone() throws InterruptedException, TimeoutException {
214         final boolean requestFullBilling = true;
215         final boolean requestShipping = false;
216         final boolean requestPhoneNumbers = true;
217         verifyFieldsAreFilled(requestFullBilling, requestShipping, requestPhoneNumbers);
218     }
219
220     @MediumTest
221     @Feature({"autofill"})
222     public void testFieldsAreFilledEverything() throws InterruptedException, TimeoutException {
223         final boolean requestFullBilling = true;
224         final boolean requestShipping = true;
225         final boolean requestPhoneNumbers = true;
226         verifyFieldsAreFilled(requestFullBilling, requestShipping, requestPhoneNumbers);
227     }
228
229     // It is currently unspecified whether autocomplete="name" gives a SHIPPING or a BILLING name.
230     // I'm assuming here that this is a shipping name.
231     @SmallTest
232     @Feature({"autofill"})
233     public void testRacTypeName() throws InterruptedException, TimeoutException {
234         verifyOneFieldWithCc(
235                 "<input id=\"id\" autocomplete=\"name\">",
236                 TEST_SHIPPING_NAME, "id", false, true, false);
237     }
238
239     @SmallTest
240     @Feature({"autofill"})
241     public void testRacTypeBillingName() throws InterruptedException, TimeoutException {
242         verifyOneFieldWithCc(
243                 "<input id=\"id\" autocomplete=\"billing name\">",
244                 TEST_NAME, "id", false, false, false);
245     }
246
247     @SmallTest
248     @Feature({"autofill"})
249     public void testRacTypeShippingName() throws InterruptedException, TimeoutException {
250         verifyOneFieldWithCc(
251                 "<input id=\"id\" autocomplete=\"shipping name\">",
252                 TEST_SHIPPING_NAME, "id", false, true, false);
253     }
254
255     // It is currently unspecified whether autocomplete="name" gives a SHIPPING or a BILLING phone.
256     // I'm assuming here that this is a shipping phone.
257     @SmallTest
258     @Feature({"autofill"})
259     public void testRacTypeTel() throws InterruptedException, TimeoutException {
260         verifyOneFieldWithCc(
261                 "<input id=\"id\" autocomplete=\"tel\">",
262                 TEST_SHIPPING_PHONE_UNFORMATTED, "id", false, true, true);
263     }
264
265     @SmallTest
266     @Feature({"autofill"})
267     public void testRacTypeBillingTel() throws InterruptedException, TimeoutException {
268         verifyOneFieldWithCc(
269                 "<input id=\"id\" autocomplete=\"billing tel\">",
270                 TEST_PHONE_UNFORMATTED, "id", true, false, true);
271     }
272
273     @SmallTest
274     @Feature({"autofill"})
275     public void testRacTypeShippingTel() throws InterruptedException, TimeoutException {
276         verifyOneFieldWithCc(
277                 "<input id=\"id\" autocomplete=\"shipping tel\">",
278                 TEST_SHIPPING_PHONE_UNFORMATTED, "id", false, true, true);
279     }
280
281     @SmallTest
282     @Feature({"autofill"})
283     public void testRacTypeEmail() throws InterruptedException, TimeoutException {
284         verifyOneFieldWithCc(
285                 "<input id=\"id\" autocomplete=\"email\" type=\"email\">",
286                 TEST_EMAIL, "id", false, false, false);
287     }
288
289     @SmallTest
290     @Feature({"autofill"})
291     public void testRacTypeCCName() throws InterruptedException, TimeoutException {
292         verifyOneField(
293                 "<input id=\"id\" autocomplete=\"cc-name\">",
294                 TEST_NAME, "id", false, false, false);
295     }
296
297     @SmallTest
298     @Feature({"autofill"})
299     public void testRacTypeCCNumber() throws InterruptedException, TimeoutException {
300         verifyOneField(
301                 "<input id=\"id\" autocomplete=\"cc-number\">",
302                 TEST_CC_NUMBER, "id", false, false, false);
303     }
304
305     @SmallTest
306     @Feature({"autofill"})
307     public void testRacTypeCCCsc() throws InterruptedException, TimeoutException {
308         verifyOneField(
309                 "<input id=\"id\" autocomplete=\"cc-csc\">",
310                 TEST_CC_CSC, "id", false, false, false);
311     }
312
313     @SmallTest
314     @Feature({"autofill"})
315     public void testRacTypeCCExp() throws InterruptedException, TimeoutException {
316         verifyOneField(
317                 "<input id=\"id\" autocomplete=\"cc-exp\" type=\"month\" value=\"1111-11\">",
318                 "" + TEST_CC_EXP_YEAR + "-" + TEST_CC_EXP_MONTH,
319                 "id", false, false, false);
320     }
321
322     @SmallTest
323     @Feature({"autofill"})
324     public void testRacTypeCCExpMonth() throws InterruptedException, TimeoutException {
325         verifyOneField(
326                 "<select id=\"id\" autocomplete=\"cc-exp-month\">"
327                 + "  <option value=\"1\" selected>1</option>"
328                 + "  <option value=\"2\">2</option>"
329                 + "  <option value=\"3\">3</option>"
330                 + "  <option value=\"4\">4</option>"
331                 + "  <option value=\"5\">5</option>"
332                 + "  <option value=\"6\">6</option>"
333                 + "  <option value=\"7\">7</option>"
334                 + "  <option value=\"8\">8</option>"
335                 + "  <option value=\"9\">9</option>"
336                 + "  <option value=\"10\">10</option>"
337                 + "  <option value=\"11\">11</option>"
338                 + "  <option value=\"12\">12</option>"
339                 + "</select>",
340                 "" + TEST_CC_EXP_MONTH, "id", false, false, false);
341     }
342
343     @SmallTest
344     @Feature({"autofill"})
345     public void testRacTypeCCExpYear() throws InterruptedException, TimeoutException {
346         verifyOneField(
347                 "<select id=\"id\" autocomplete=\"cc-exp-year\">"
348                 + "  <option value=\"2011\" selected>2011</option>"
349                 + "  <option value=\"2012\">2012</option>"
350                 + "  <option value=\"2013\">2013</option>"
351                 + "  <option value=\"2014\">2014</option>"
352                 + "  <option value=\"2015\">2015</option>"
353                 + "</select>",
354                 "" + TEST_CC_EXP_YEAR, "id", false, false, false);
355     }
356
357     @SmallTest
358     @Feature({"autofill"})
359     public void testRacTypeBillingCountry() throws InterruptedException, TimeoutException {
360         verifyOneFieldWithCc(
361                 "<select id=\"id\" autocomplete=\"billing country\">"
362                 + "  <option value=\"NL\" selected>Netherlands</option>"
363                 + "  <option value=\"US\">United States</option>"
364                 + "  <option value=\"SE\">Sweden</option>"
365                 + "  <option value=\"RU\">Russia</option>"
366                 + "</select>",
367                 TEST_BILLING_COUNTRY, "id", true, false, false);
368     }
369
370     @SmallTest
371     @Feature({"autofill"})
372     public void testRacTypeBillingPostalCode() throws InterruptedException, TimeoutException {
373         verifyOneFieldWithCc(
374                 "<input id=\"id\" autocomplete=\"billing postal-code\">",
375                 TEST_BILLING_ZIP, "id", false, false, false);
376     }
377
378     @SmallTest
379     @Feature({"autofill"})
380     public void testRacTypeBillingAddressLine1() throws InterruptedException, TimeoutException {
381         verifyOneFieldWithCc(
382                 "<input id=\"id\" autocomplete=\"billing address-line1\">",
383                 TEST_BILLING1, "id", true, false, false);
384     }
385
386     @SmallTest
387     @Feature({"autofill"})
388     public void testRacTypeBillingAddressLine2() throws InterruptedException, TimeoutException {
389         verifyOneFieldWithCc(
390                 "<input id=\"id\" autocomplete=\"billing address-line2\">",
391                 TEST_BILLING2, "id", true, false, false);
392     }
393
394     @SmallTest
395     @Feature({"autofill"})
396     public void testRacTypeBillingStreetAddress() throws InterruptedException, TimeoutException {
397         verifyOneFieldWithCc(
398                 "<textarea id=\"id\" autocomplete=\"billing street-address\"></textarea>",
399                 TEST_BILLING_STREET, "id", true, false, false);
400     }
401
402     @SmallTest
403     @Feature({"autofill"})
404     public void testRacTypeBillingLocality() throws InterruptedException, TimeoutException {
405         verifyOneFieldWithCc(
406                 "<input id=\"id\" autocomplete=\"billing locality\">",
407                 TEST_BILLING_CITY, "id", true, false, false);
408     }
409
410     @SmallTest
411     @Feature({"autofill"})
412     public void testRacTypeBillingRegion() throws InterruptedException, TimeoutException {
413         verifyOneFieldWithCc(
414                 "<input id=\"id\" autocomplete=\"billing region\">",
415                 TEST_BILLING_STATE, "id", true, false, false);
416     }
417
418     @SmallTest
419     @Feature({"autofill"})
420     public void testRacTypeShippingCountry() throws InterruptedException, TimeoutException {
421         verifyOneFieldWithCc(
422                 "<select id=\"id\" autocomplete=\"shipping country\">"
423                 + "  <option value=\"NL\" selected>Netherlands</option>"
424                 + "  <option value=\"US\">United States</option>"
425                 + "  <option value=\"SE\">Sweden</option>"
426                 + "  <option value=\"RU\">Russia</option>"
427                 + "</select>",
428                 TEST_SHIPPING_COUNTRY, "id", false, true, false);
429     }
430
431     @SmallTest
432     @Feature({"autofill"})
433     public void testRacTypeShippingPostalCode() throws InterruptedException, TimeoutException {
434         verifyOneFieldWithCc(
435                 "<input id=\"id\" autocomplete=\"shipping postal-code\">",
436                 TEST_SHIPPING_ZIP, "id", false, true, false);
437     }
438
439     @SmallTest
440     @Feature({"autofill"})
441     public void testRacTypeShippingAddressLine1() throws InterruptedException, TimeoutException {
442         verifyOneFieldWithCc(
443                 "<input id=\"id\" autocomplete=\"shipping address-line1\">",
444                 TEST_SHIPPING1, "id", false, true, false);
445     }
446
447     @SmallTest
448     @Feature({"autofill"})
449     public void testRacTypeShippingAddressLine2() throws InterruptedException, TimeoutException {
450         verifyOneFieldWithCc(
451                 "<input id=\"id\" autocomplete=\"shipping address-line2\">",
452                 TEST_SHIPPING2, "id", false, true, false);
453     }
454
455     @SmallTest
456     @Feature({"autofill"})
457     public void testRacTypeShippingStreetAddress() throws InterruptedException, TimeoutException {
458         verifyOneFieldWithCc(
459                 "<textarea id=\"id\" autocomplete=\"shipping street-address\"></textarea>",
460                 TEST_SHIPPING_STREET, "id", false, true, false);
461     }
462
463     @SmallTest
464     @Feature({"autofill"})
465     public void testRacTypeShippingLocality() throws InterruptedException, TimeoutException {
466         verifyOneFieldWithCc(
467                 "<input id=\"id\" autocomplete=\"shipping locality\">",
468                 TEST_SHIPPING_CITY, "id", false, true, false);
469     }
470
471     @SmallTest
472     @Feature({"autofill"})
473     public void testRacTypeShippingRegion() throws InterruptedException, TimeoutException {
474         verifyOneFieldWithCc(
475                 "<input id=\"id\" autocomplete=\"shipping region\">",
476                 TEST_SHIPPING_STATE, "id", false, true, false);
477     }
478
479     @SmallTest
480     @Feature({"autofill"})
481     public void testRefuseToShowWithNoCcField() throws InterruptedException, TimeoutException {
482         String requested = "<input id=\"id\" autocomplete=\"shipping locality\">";
483         setUpAndExpectFailedRequestAutocomplete(
484                 UrlUtils.encodeHtmlDataUri(HTML_PRELUDE + requested + HTML_POSTLUDE),
485                 false, true, false);
486     }
487
488     @SmallTest
489     @Feature({"autofill"})
490     public void testRefuseToShowWithNoAutocompleteAttributes()
491             throws InterruptedException, TimeoutException {
492         String requested = "<input id=\"id-cc-csc\">"
493                 + "<input id=\"id-email\" type=\"email\">"
494                 + "<input id=\"id-cc-name\">"
495                 + "<input id=\"id-shipping-locality\">";
496         setUpAndExpectFailedRequestAutocomplete(
497                 UrlUtils.encodeHtmlDataUri(HTML_PRELUDE + requested + HTML_POSTLUDE),
498                 false, true, false);
499     }
500
501     private void verifyOneField(
502             final String htmlFragment,
503             final String expected, final String actualId,
504             final boolean requestFullBilling,
505             final boolean requestShipping, final boolean requestPhoneNumbers)
506             throws InterruptedException, TimeoutException {
507         verifyOneFieldWithOptionalCc(htmlFragment, expected, actualId,
508                 requestFullBilling, requestShipping, requestPhoneNumbers, false);
509     }
510
511     private void verifyOneFieldWithCc(
512             final String htmlFragment,
513             final String expected, final String actualId,
514             final boolean requestFullBilling,
515             final boolean requestShipping, final boolean requestPhoneNumbers)
516             throws InterruptedException, TimeoutException {
517         verifyOneFieldWithOptionalCc(htmlFragment, expected, actualId,
518                 requestFullBilling, requestShipping, requestPhoneNumbers, true);
519     }
520
521     private void verifyOneFieldWithOptionalCc(
522             final String htmlFragment,
523             final String expected, final String actualId,
524             final boolean requestFullBilling,
525             final boolean requestShipping, final boolean requestPhoneNumbers,
526             final boolean requestCcInfo)
527             throws InterruptedException, TimeoutException {
528         final String optionalCcFragment = requestCcInfo
529                 ? "<input id=\"id-opt-cc-csc\" autocomplete=\"cc-csc\">"
530                 : "";
531         final String url = UrlUtils.encodeHtmlDataUri(
532                 HTML_PRELUDE
533                 + htmlFragment
534                 + optionalCcFragment
535                 + HTML_POSTLUDE);
536
537         setUpAndRequestAutocomplete(url, requestFullBilling, requestShipping, requestPhoneNumbers);
538
539         final ContentViewCore viewCore = getActivity().getActiveContentViewCore();
540
541         assertEquals(actualId + " did not match",
542                 expected, DOMUtils.getNodeValue(viewCore, actualId));
543         if (requestCcInfo) {
544             assertEquals("cc-csc did not match",
545                     TEST_CC_CSC, DOMUtils.getNodeValue(viewCore, "id-opt-cc-csc"));
546         }
547     }
548
549     private void verifyFieldsAreFilled(final boolean requestFullBilling,
550             final boolean requestShipping, final boolean requestPhoneNumbers)
551             throws InterruptedException, TimeoutException {
552         setUpAndRequestAutocomplete(
553                 generatePage(requestFullBilling, requestShipping, requestPhoneNumbers),
554                 requestFullBilling, requestShipping, requestPhoneNumbers);
555
556         final ContentViewCore viewCore = getActivity().getActiveContentViewCore();
557
558         assertEquals("billing name did not match",
559                 TEST_NAME, DOMUtils.getNodeValue(viewCore, "id-billing-name"));
560         assertEquals("email did not match",
561                 TEST_EMAIL, DOMUtils.getNodeValue(viewCore, "id-email"));
562
563         assertEquals("cc-name did not match",
564                 TEST_NAME, DOMUtils.getNodeValue(viewCore, "id-cc-name"));
565
566         assertEquals("cc-number did not match",
567                 TEST_CC_NUMBER, DOMUtils.getNodeValue(viewCore, "id-cc-number"));
568         assertEquals("cc-csc did not match",
569                 TEST_CC_CSC, DOMUtils.getNodeValue(viewCore, "id-cc-csc"));
570
571         assertEquals("cc-exp did not match",
572                 "" + TEST_CC_EXP_YEAR + "-" + TEST_CC_EXP_MONTH,
573                 DOMUtils.getNodeValue(viewCore, "id-cc-exp"));
574
575         assertEquals("cc-exp-month did not match",
576                 "" + TEST_CC_EXP_MONTH,
577                 DOMUtils.getNodeValue(viewCore, "id-cc-exp-month"));
578         assertEquals("cc-exp-year did not match",
579                 "" + TEST_CC_EXP_YEAR,
580                 DOMUtils.getNodeValue(viewCore, "id-cc-exp-year"));
581
582         assertEquals("billing postal-code did not match",
583                 TEST_BILLING_ZIP, DOMUtils.getNodeValue(viewCore, "id-cc-zip"));
584
585         if (requestFullBilling) {
586             assertEquals("billing address-line1 did not match",
587                     TEST_BILLING1, DOMUtils.getNodeValue(viewCore, "id-cc-1"));
588             assertEquals("billing address-line2 did not match",
589                     TEST_BILLING2, DOMUtils.getNodeValue(viewCore, "id-cc-2"));
590             assertEquals("billing street-address did not match",
591                     TEST_BILLING_STREET, DOMUtils.getNodeValue(viewCore, "id-cc-str"));
592             assertEquals("billing locality did not match",
593                     TEST_BILLING_CITY, DOMUtils.getNodeValue(viewCore, "id-cc-city"));
594             assertEquals("billing region did not match",
595                     TEST_BILLING_STATE, DOMUtils.getNodeValue(viewCore, "id-cc-state"));
596             assertEquals("billing country did not match",
597                     TEST_BILLING_COUNTRY, DOMUtils.getNodeValue(viewCore, "id-cc-country"));
598
599             if (requestPhoneNumbers) {
600                 assertEquals("billing tel did not match",
601                         TEST_PHONE_UNFORMATTED,
602                         DOMUtils.getNodeValue(viewCore, "id-cc-tel"));
603             }
604         }
605
606         if (requestShipping) {
607             assertEquals("shipping name did not match",
608                     TEST_SHIPPING_NAME, DOMUtils.getNodeValue(viewCore, "id-h-name"));
609             assertEquals("shipping postal-code did not match",
610                     TEST_SHIPPING_ZIP, DOMUtils.getNodeValue(viewCore, "id-h-zip"));
611             assertEquals("shipping address-line1 did not match",
612                     TEST_SHIPPING1, DOMUtils.getNodeValue(viewCore, "id-h-1"));
613             assertEquals("shipping address-line2 did not match",
614                     TEST_SHIPPING2, DOMUtils.getNodeValue(viewCore, "id-h-2"));
615             assertEquals("shipping street-address did not match",
616                     TEST_SHIPPING_STREET, DOMUtils.getNodeValue(viewCore, "id-h-str"));
617             assertEquals("shipping locality did not match",
618                     TEST_SHIPPING_CITY, DOMUtils.getNodeValue(viewCore, "id-h-city"));
619             assertEquals("shipping region did not match",
620                     TEST_SHIPPING_STATE, DOMUtils.getNodeValue(viewCore, "id-h-state"));
621             assertEquals("shipping country did not match",
622                     TEST_SHIPPING_COUNTRY,
623                     DOMUtils.getNodeValue(viewCore, "id-h-country"));
624
625             // It is currently unspecified whether autocomplete="name" gives a SHIPPING or
626             // a BILLING name. I'm assuming here that this is a shipping name.
627             assertEquals("name did not match",
628                     TEST_SHIPPING_NAME, DOMUtils.getNodeValue(viewCore, "id-name"));
629
630             if (requestPhoneNumbers) {
631                 assertEquals("shipping tel did not match",
632                         TEST_SHIPPING_PHONE_UNFORMATTED,
633                         DOMUtils.getNodeValue(viewCore, "id-h-tel"));
634
635                 // It is currently unspecified whether autocomplete="name" gives a SHIPPING or
636                 // a BILLING phone. I'm assuming here that this is a shipping phone.
637                 assertEquals("tel did not match",
638                         TEST_SHIPPING_PHONE_UNFORMATTED,
639                         DOMUtils.getNodeValue(viewCore, "id-tel"));
640             }
641         }
642     }
643
644     // Wait and assert helper methods -------------------------------------------------------------
645
646     private void setUpAndRequestAutocomplete(final String url,
647             final boolean requestFullBilling,
648             final boolean requestShipping,
649             final boolean requestPhoneNumbers) throws InterruptedException, TimeoutException {
650         setUpAndRequestAutocompleteImpl(url,
651                 requestFullBilling, requestShipping, requestPhoneNumbers,
652                 false);
653     }
654
655     private void setUpAndExpectFailedRequestAutocomplete(final String url,
656             final boolean requestFullBilling,
657             final boolean requestShipping,
658             final boolean requestPhoneNumbers) throws InterruptedException, TimeoutException {
659         setUpAndRequestAutocompleteImpl(url,
660                 requestFullBilling, requestShipping, requestPhoneNumbers,
661                 true);
662     }
663
664     private void setUpAndRequestAutocompleteImpl(final String url,
665             final boolean requestFullBilling,
666             final boolean requestShipping,
667             final boolean requestPhoneNumbers,
668             final boolean expectFailure)
669             throws InterruptedException, TimeoutException {
670         launchChromeShellWithUrl(url);
671         assertTrue(waitForActiveShellToBeDoneLoading());
672
673         final ContentViewCore viewCore = getActivity().getActiveContentViewCore();
674
675         AutofillDialogResult.ResultWallet result = new AutofillDialogResult.ResultWallet(
676                 TEST_EMAIL, "Google Transaction ID",
677                 new AutofillDialogResult.ResultCard(
678                         TEST_CC_EXP_MONTH, TEST_CC_EXP_YEAR,
679                         TEST_CC_NUMBER, TEST_CC_CSC),
680                 new AutofillDialogResult.ResultAddress(
681                         TEST_NAME, TEST_PHONE,
682                         TEST_BILLING_STREET,
683                         TEST_BILLING_CITY, TEST_BILLING_DL, TEST_BILLING_STATE,
684                         TEST_BILLING_ZIP, TEST_BILLING_SORTING_CODE, TEST_BILLING_COUNTRY,
685                         TEST_BILLING_LANGUAGE),
686                 new AutofillDialogResult.ResultAddress(
687                         TEST_SHIPPING_NAME, TEST_SHIPPING_PHONE,
688                         TEST_SHIPPING_STREET,
689                         TEST_SHIPPING_CITY, TEST_SHIPPING_DL, TEST_SHIPPING_STATE,
690                         TEST_SHIPPING_ZIP, TEST_SHIPPING_SORTING_CODE, TEST_SHIPPING_COUNTRY,
691                         TEST_SHIPPING_LANGUAGE));
692         MockAutofillDialogController.installMockFactory(
693                 DIALOG_CALLBACK_DELAY_MILLISECONDS,
694                 result,
695                 true, "", "", "", "",
696                 requestFullBilling, requestShipping, requestPhoneNumbers);
697
698         DOMUtils.clickNode(this, viewCore, "id-button");
699         waitForInputFieldFill(viewCore);
700
701         if (!expectFailure) {
702             assertEquals("requestAutocomplete failed",
703                     "succeeded",
704                     DOMUtils.getNodeContents(viewCore, "was-autocompleted"));
705         } else {
706             assertEquals("requestAutocomplete succeeded when it should be failing",
707                     "failed",
708                     DOMUtils.getNodeContents(viewCore, "was-autocompleted"));
709         }
710     }
711
712     private void waitForInputFieldFill(final ContentViewCore viewCore) throws InterruptedException {
713         assertTrue("requestAutocomplete() never completed.",
714                 CriteriaHelper.pollForCriteria(new Criteria() {
715                     @Override
716                     public boolean isSatisfied() {
717                         String wasAutocompleted;
718                         try {
719                             wasAutocompleted = DOMUtils.getNodeContents(
720                                     viewCore, "was-autocompleted");
721                         } catch (InterruptedException e) {
722                             return false;
723                         } catch (TimeoutException e) {
724                             return false;
725                         }
726                         return TextUtils.equals("succeeded", wasAutocompleted)
727                                 || TextUtils.equals("failed", wasAutocompleted);
728                     }
729                 }));
730     }
731 }