1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.autofill;
7 import android.test.suitebuilder.annotation.SmallTest;
9 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
11 import org.chromium.base.ThreadUtils;
12 import org.chromium.base.test.util.Feature;
13 import org.chromium.chrome.shell.ChromeShellActivity;
14 import org.chromium.chrome.shell.ChromeShellTestBase;
15 import org.chromium.content.browser.test.util.Criteria;
16 import org.chromium.content.browser.test.util.CriteriaHelper;
17 import org.chromium.content.browser.test.util.TouchCommon;
18 import org.chromium.ui.DropdownItem;
19 import org.chromium.ui.autofill.AutofillPopup;
20 import org.chromium.ui.autofill.AutofillPopup.AutofillPopupDelegate;
21 import org.chromium.ui.autofill.AutofillSuggestion;
22 import org.chromium.ui.base.ActivityWindowAndroid;
23 import org.chromium.ui.base.ViewAndroidDelegate;
24 import org.chromium.ui.base.WindowAndroid;
26 import java.util.concurrent.atomic.AtomicBoolean;
29 * Tests the Autofill's java code for creating the AutofillPopup object, opening and selecting
32 public class AutofillTest extends ChromeShellTestBase {
34 private AutofillPopup mAutofillPopup;
35 private WindowAndroid mWindowAndroid;
36 private MockAutofillCallback mMockAutofillCallback;
39 public void setUp() throws Exception {
41 ChromeShellActivity activity = launchChromeShellWithBlankPage();
42 assertNotNull(activity);
43 waitForActiveShellToBeDoneLoading();
45 mMockAutofillCallback = new MockAutofillCallback();
46 mWindowAndroid = new ActivityWindowAndroid(activity);
47 final ViewAndroidDelegate viewDelegate =
48 activity.getActiveContentViewCore().getViewAndroidDelegate();
50 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
53 mAutofillPopup = new AutofillPopup(mWindowAndroid.getActivity().get(),
55 mMockAutofillCallback);
56 mAutofillPopup.filterAndShow(new AutofillSuggestion[0], false);
57 mAutofillPopup.setAnchorRect(50, 500, 500, 50);
62 private static final long CALLBACK_TIMEOUT_MS = scaleTimeout(4000);
63 private static final int CHECK_INTERVAL_MS = 100;
65 private class MockAutofillCallback implements AutofillPopupDelegate{
66 private final AtomicBoolean mGotPopupSelection = new AtomicBoolean(false);
67 public int mListIndex = -1;
70 public void suggestionSelected(int listIndex) {
71 mListIndex = listIndex;
72 mAutofillPopup.dismiss();
73 mGotPopupSelection.set(true);
76 public boolean waitForCallback() throws InterruptedException {
77 return CriteriaHelper.pollForCriteria(new Criteria() {
79 public boolean isSatisfied() {
80 return mGotPopupSelection.get();
82 }, CALLBACK_TIMEOUT_MS, CHECK_INTERVAL_MS);
86 public void dismissed() {
90 private AutofillSuggestion[] createTwoAutofillSuggestionArray() {
91 return new AutofillSuggestion[] {
92 new AutofillSuggestion("Sherlock Holmes", "221B Baker Street", DropdownItem.NO_ICON,
94 new AutofillSuggestion("Arthur Dent", "West Country", DropdownItem.NO_ICON, 43),
98 private AutofillSuggestion[] createFiveAutofillSuggestionArray() {
99 return new AutofillSuggestion[] {
100 new AutofillSuggestion("Sherlock Holmes", "221B Baker Street", DropdownItem.NO_ICON,
102 new AutofillSuggestion("Arthur Dent", "West Country", DropdownItem.NO_ICON, 43),
103 new AutofillSuggestion("Arthos", "France", DropdownItem.NO_ICON, 44),
104 new AutofillSuggestion("Porthos", "France", DropdownItem.NO_ICON, 45),
105 new AutofillSuggestion("Aramis", "France", DropdownItem.NO_ICON, 46),
109 public boolean openAutofillPopupAndWaitUntilReady(final AutofillSuggestion[] suggestions)
110 throws InterruptedException {
111 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
114 mAutofillPopup.filterAndShow(suggestions, false);
117 return CriteriaHelper.pollForCriteria(new Criteria() {
119 public boolean isSatisfied() {
120 return mAutofillPopup.getListView().getChildCount() > 0;
126 @Feature({"autofill"})
127 public void testAutofillWithDifferentNumberSuggestions() throws Exception {
128 assertTrue(openAutofillPopupAndWaitUntilReady(createTwoAutofillSuggestionArray()));
129 assertEquals(2, mAutofillPopup.getListView().getCount());
131 assertTrue(openAutofillPopupAndWaitUntilReady(createFiveAutofillSuggestionArray()));
132 assertEquals(5, mAutofillPopup.getListView().getCount());
136 @Feature({"autofill"})
137 public void testAutofillClickFirstSuggestion() throws Exception {
138 AutofillSuggestion[] suggestions = createTwoAutofillSuggestionArray();
139 assertTrue(openAutofillPopupAndWaitUntilReady(suggestions));
140 assertEquals(2, mAutofillPopup.getListView().getCount());
142 TouchCommon.singleClickViewRelative(mAutofillPopup.getListView(), 10, 10);
143 assertTrue(mMockAutofillCallback.waitForCallback());
145 assertEquals(0, mMockAutofillCallback.mListIndex);