1 // Copyright (c) 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 org.chromium.base.test.util.Feature;
10 import org.chromium.chrome.testshell.ChromiumTestShellActivity;
11 import org.chromium.chrome.testshell.ChromiumTestShellTestBase;
12 import org.chromium.content.browser.test.util.TouchCommon;
13 import org.chromium.content.browser.test.util.UiUtils;
14 import org.chromium.content.browser.test.util.Criteria;
15 import org.chromium.content.browser.test.util.CriteriaHelper;
16 import org.chromium.ui.ViewAndroidDelegate;
17 import org.chromium.ui.WindowAndroid;
18 import org.chromium.ui.autofill.AutofillPopup;
19 import org.chromium.ui.autofill.AutofillPopup.AutofillPopupDelegate;
20 import org.chromium.ui.autofill.AutofillSuggestion;
22 import java.util.concurrent.atomic.AtomicBoolean;
25 * Tests the Autofill's java code for creating the AutofillPopup object, opening and selecting
28 public class AutofillTest extends ChromiumTestShellTestBase {
30 private AutofillPopup mAutofillPopup;
31 private WindowAndroid mWindowAndroid;
32 private MockAutofillCallback mMockAutofillCallback;
35 public void setUp() throws Exception {
37 ChromiumTestShellActivity activity = launchChromiumTestShellWithBlankPage();
38 assertNotNull(activity);
39 waitForActiveShellToBeDoneLoading();
41 mMockAutofillCallback = new MockAutofillCallback();
42 mWindowAndroid = new WindowAndroid(activity);
43 final ViewAndroidDelegate viewDelegate =
44 activity.getActiveContentView().getContentViewCore().getViewAndroidDelegate();
46 UiUtils.runOnUiThread(getActivity(), new Runnable() {
49 mAutofillPopup = new AutofillPopup(mWindowAndroid.getContext(),
51 mMockAutofillCallback);
52 mAutofillPopup.setAnchorRect(50, 500, 500, 50);
58 private class MockAutofillCallback implements AutofillPopupDelegate{
59 private static final int CALLBACK_TIMEOUT_MS = 4000;
60 private static final int CHECK_INTERVAL_MS = 100;
61 private final AtomicBoolean mGotPopupSelection = new AtomicBoolean(false);
62 public int mListIndex = -1;
65 public void suggestionSelected(int listIndex) {
66 mListIndex = listIndex;
67 mAutofillPopup.dismiss();
68 mGotPopupSelection.set(true);
71 public boolean waitForCallback() throws InterruptedException {
72 return CriteriaHelper.pollForCriteria(new Criteria() {
74 public boolean isSatisfied() {
75 return mGotPopupSelection.get();
77 }, CALLBACK_TIMEOUT_MS, CHECK_INTERVAL_MS);
81 public void requestHide() {
85 private AutofillSuggestion[] createTwoAutofillSuggestionArray() {
86 return new AutofillSuggestion[] {
87 new AutofillSuggestion("Sherlock Holmes", "221B Baker Street", 42),
88 new AutofillSuggestion("Arthur Dent", "West Country", 43),
92 private AutofillSuggestion[] createFiveAutofillSuggestionArray() {
93 return new AutofillSuggestion[] {
94 new AutofillSuggestion("Sherlock Holmes", "221B Baker Street", 42),
95 new AutofillSuggestion("Arthur Dent", "West Country", 43),
96 new AutofillSuggestion("Arthos", "France", 44),
97 new AutofillSuggestion("Porthos", "France", 45),
98 new AutofillSuggestion("Aramis", "France", 46),
102 public boolean openAutofillPopupAndWaitUntilReady(final AutofillSuggestion[] suggestions)
103 throws InterruptedException {
104 UiUtils.runOnUiThread(getActivity(), new Runnable() {
107 mAutofillPopup.show(suggestions);
110 return CriteriaHelper.pollForCriteria(new Criteria() {
112 public boolean isSatisfied() {
113 return mAutofillPopup.getListView().getChildCount() > 0;
119 @Feature({"autofill"})
120 public void testAutofillWithDifferentNumberSuggestions() throws Exception {
121 assertTrue(openAutofillPopupAndWaitUntilReady(createTwoAutofillSuggestionArray()));
122 assertEquals(2, mAutofillPopup.getListView().getCount());
124 assertTrue(openAutofillPopupAndWaitUntilReady(createFiveAutofillSuggestionArray()));
125 assertEquals(5, mAutofillPopup.getListView().getCount());
129 @Feature({"autofill"})
130 public void testAutofillClickFirstSuggestion() throws Exception {
131 AutofillSuggestion[] suggestions = createTwoAutofillSuggestionArray();
132 assertTrue(openAutofillPopupAndWaitUntilReady(suggestions));
133 assertEquals(2, mAutofillPopup.getListView().getCount());
135 TouchCommon touchCommon = new TouchCommon(this);
136 touchCommon.singleClickViewRelative(mAutofillPopup.getListView(), 10, 10);
137 assertTrue(mMockAutofillCallback.waitForCallback());
139 assertEquals(0, mMockAutofillCallback.mListIndex);