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.test.util.Feature;
12 import org.chromium.chrome.testshell.ChromiumTestShellActivity;
13 import org.chromium.chrome.testshell.ChromiumTestShellTestBase;
14 import org.chromium.content.browser.test.util.Criteria;
15 import org.chromium.content.browser.test.util.CriteriaHelper;
16 import org.chromium.content.browser.test.util.TouchCommon;
17 import org.chromium.content.browser.test.util.UiUtils;
18 import org.chromium.ui.autofill.AutofillPopup;
19 import org.chromium.ui.autofill.AutofillPopup.AutofillPopupDelegate;
20 import org.chromium.ui.autofill.AutofillSuggestion;
21 import org.chromium.ui.base.ActivityWindowAndroid;
22 import org.chromium.ui.base.ViewAndroidDelegate;
23 import org.chromium.ui.base.WindowAndroid;
25 import java.util.concurrent.atomic.AtomicBoolean;
28 * Tests the Autofill's java code for creating the AutofillPopup object, opening and selecting
31 public class AutofillTest extends ChromiumTestShellTestBase {
33 private AutofillPopup mAutofillPopup;
34 private WindowAndroid mWindowAndroid;
35 private MockAutofillCallback mMockAutofillCallback;
38 public void setUp() throws Exception {
40 ChromiumTestShellActivity activity = launchChromiumTestShellWithBlankPage();
41 assertNotNull(activity);
42 waitForActiveShellToBeDoneLoading();
44 mMockAutofillCallback = new MockAutofillCallback();
45 mWindowAndroid = new ActivityWindowAndroid(activity);
46 final ViewAndroidDelegate viewDelegate =
47 activity.getActiveContentView().getContentViewCore().getViewAndroidDelegate();
49 UiUtils.runOnUiThread(getActivity(), new Runnable() {
52 mAutofillPopup = new AutofillPopup(mWindowAndroid.getContext(),
54 mMockAutofillCallback);
55 mAutofillPopup.setAnchorRect(50, 500, 500, 50);
61 private static final long CALLBACK_TIMEOUT_MS = scaleTimeout(4000);
62 private static final int CHECK_INTERVAL_MS = 100;
64 private class MockAutofillCallback implements AutofillPopupDelegate{
65 private final AtomicBoolean mGotPopupSelection = new AtomicBoolean(false);
66 public int mListIndex = -1;
69 public void suggestionSelected(int listIndex) {
70 mListIndex = listIndex;
71 mAutofillPopup.dismiss();
72 mGotPopupSelection.set(true);
75 public boolean waitForCallback() throws InterruptedException {
76 return CriteriaHelper.pollForCriteria(new Criteria() {
78 public boolean isSatisfied() {
79 return mGotPopupSelection.get();
81 }, CALLBACK_TIMEOUT_MS, CHECK_INTERVAL_MS);
85 public void requestHide() {
89 private AutofillSuggestion[] createTwoAutofillSuggestionArray() {
90 return new AutofillSuggestion[] {
91 new AutofillSuggestion("Sherlock Holmes", "221B Baker Street", 42),
92 new AutofillSuggestion("Arthur Dent", "West Country", 43),
96 private AutofillSuggestion[] createFiveAutofillSuggestionArray() {
97 return new AutofillSuggestion[] {
98 new AutofillSuggestion("Sherlock Holmes", "221B Baker Street", 42),
99 new AutofillSuggestion("Arthur Dent", "West Country", 43),
100 new AutofillSuggestion("Arthos", "France", 44),
101 new AutofillSuggestion("Porthos", "France", 45),
102 new AutofillSuggestion("Aramis", "France", 46),
106 public boolean openAutofillPopupAndWaitUntilReady(final AutofillSuggestion[] suggestions)
107 throws InterruptedException {
108 UiUtils.runOnUiThread(getActivity(), new Runnable() {
111 mAutofillPopup.show(suggestions);
114 return CriteriaHelper.pollForCriteria(new Criteria() {
116 public boolean isSatisfied() {
117 return mAutofillPopup.getListView().getChildCount() > 0;
123 @Feature({"autofill"})
124 public void testAutofillWithDifferentNumberSuggestions() throws Exception {
125 assertTrue(openAutofillPopupAndWaitUntilReady(createTwoAutofillSuggestionArray()));
126 assertEquals(2, mAutofillPopup.getListView().getCount());
128 assertTrue(openAutofillPopupAndWaitUntilReady(createFiveAutofillSuggestionArray()));
129 assertEquals(5, mAutofillPopup.getListView().getCount());
133 @Feature({"autofill"})
134 public void testAutofillClickFirstSuggestion() throws Exception {
135 AutofillSuggestion[] suggestions = createTwoAutofillSuggestionArray();
136 assertTrue(openAutofillPopupAndWaitUntilReady(suggestions));
137 assertEquals(2, mAutofillPopup.getListView().getCount());
139 TouchCommon touchCommon = new TouchCommon(this);
140 touchCommon.singleClickViewRelative(mAutofillPopup.getListView(), 10, 10);
141 assertTrue(mMockAutofillCallback.waitForCallback());
143 assertEquals(0, mMockAutofillCallback.mListIndex);