AutofillTest.java revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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. 4 5package org.chromium.chrome.browser.autofill; 6 7import android.test.suitebuilder.annotation.SmallTest; 8 9import org.chromium.base.test.util.Feature; 10import org.chromium.chrome.testshell.ChromiumTestShellActivity; 11import org.chromium.chrome.testshell.ChromiumTestShellTestBase; 12import org.chromium.content.browser.test.util.Criteria; 13import org.chromium.content.browser.test.util.CriteriaHelper; 14import org.chromium.content.browser.test.util.TouchCommon; 15import org.chromium.content.browser.test.util.UiUtils; 16import org.chromium.ui.autofill.AutofillPopup; 17import org.chromium.ui.autofill.AutofillPopup.AutofillPopupDelegate; 18import org.chromium.ui.autofill.AutofillSuggestion; 19import org.chromium.ui.base.ActivityWindowAndroid; 20import org.chromium.ui.base.ViewAndroidDelegate; 21import org.chromium.ui.base.WindowAndroid; 22 23import java.util.concurrent.atomic.AtomicBoolean; 24 25/** 26 * Tests the Autofill's java code for creating the AutofillPopup object, opening and selecting 27 * popups. 28 */ 29public class AutofillTest extends ChromiumTestShellTestBase { 30 31 private AutofillPopup mAutofillPopup; 32 private WindowAndroid mWindowAndroid; 33 private MockAutofillCallback mMockAutofillCallback; 34 35 @Override 36 public void setUp() throws Exception { 37 super.setUp(); 38 ChromiumTestShellActivity activity = launchChromiumTestShellWithBlankPage(); 39 assertNotNull(activity); 40 waitForActiveShellToBeDoneLoading(); 41 42 mMockAutofillCallback = new MockAutofillCallback(); 43 mWindowAndroid = new ActivityWindowAndroid(activity); 44 final ViewAndroidDelegate viewDelegate = 45 activity.getActiveContentView().getContentViewCore().getViewAndroidDelegate(); 46 47 UiUtils.runOnUiThread(getActivity(), new Runnable() { 48 @Override 49 public void run() { 50 mAutofillPopup = new AutofillPopup(mWindowAndroid.getContext(), 51 viewDelegate, 52 mMockAutofillCallback); 53 mAutofillPopup.setAnchorRect(50, 500, 500, 50); 54 } 55 }); 56 57 } 58 59 private class MockAutofillCallback implements AutofillPopupDelegate{ 60 private static final int CALLBACK_TIMEOUT_MS = 4000; 61 private static final int CHECK_INTERVAL_MS = 100; 62 private final AtomicBoolean mGotPopupSelection = new AtomicBoolean(false); 63 public int mListIndex = -1; 64 65 @Override 66 public void suggestionSelected(int listIndex) { 67 mListIndex = listIndex; 68 mAutofillPopup.dismiss(); 69 mGotPopupSelection.set(true); 70 } 71 72 public boolean waitForCallback() throws InterruptedException { 73 return CriteriaHelper.pollForCriteria(new Criteria() { 74 @Override 75 public boolean isSatisfied() { 76 return mGotPopupSelection.get(); 77 } 78 }, CALLBACK_TIMEOUT_MS, CHECK_INTERVAL_MS); 79 } 80 81 @Override 82 public void requestHide() { 83 } 84 } 85 86 private AutofillSuggestion[] createTwoAutofillSuggestionArray() { 87 return new AutofillSuggestion[] { 88 new AutofillSuggestion("Sherlock Holmes", "221B Baker Street", 42), 89 new AutofillSuggestion("Arthur Dent", "West Country", 43), 90 }; 91 } 92 93 private AutofillSuggestion[] createFiveAutofillSuggestionArray() { 94 return new AutofillSuggestion[] { 95 new AutofillSuggestion("Sherlock Holmes", "221B Baker Street", 42), 96 new AutofillSuggestion("Arthur Dent", "West Country", 43), 97 new AutofillSuggestion("Arthos", "France", 44), 98 new AutofillSuggestion("Porthos", "France", 45), 99 new AutofillSuggestion("Aramis", "France", 46), 100 }; 101 } 102 103 public boolean openAutofillPopupAndWaitUntilReady(final AutofillSuggestion[] suggestions) 104 throws InterruptedException { 105 UiUtils.runOnUiThread(getActivity(), new Runnable() { 106 @Override 107 public void run() { 108 mAutofillPopup.show(suggestions); 109 } 110 }); 111 return CriteriaHelper.pollForCriteria(new Criteria() { 112 @Override 113 public boolean isSatisfied() { 114 return mAutofillPopup.getListView().getChildCount() > 0; 115 } 116 }); 117 } 118 119 @SmallTest 120 @Feature({"autofill"}) 121 public void testAutofillWithDifferentNumberSuggestions() throws Exception { 122 assertTrue(openAutofillPopupAndWaitUntilReady(createTwoAutofillSuggestionArray())); 123 assertEquals(2, mAutofillPopup.getListView().getCount()); 124 125 assertTrue(openAutofillPopupAndWaitUntilReady(createFiveAutofillSuggestionArray())); 126 assertEquals(5, mAutofillPopup.getListView().getCount()); 127 } 128 129 @SmallTest 130 @Feature({"autofill"}) 131 public void testAutofillClickFirstSuggestion() throws Exception { 132 AutofillSuggestion[] suggestions = createTwoAutofillSuggestionArray(); 133 assertTrue(openAutofillPopupAndWaitUntilReady(suggestions)); 134 assertEquals(2, mAutofillPopup.getListView().getCount()); 135 136 TouchCommon touchCommon = new TouchCommon(this); 137 touchCommon.singleClickViewRelative(mAutofillPopup.getListView(), 10, 10); 138 assertTrue(mMockAutofillCallback.waitForCallback()); 139 140 assertEquals(0, mMockAutofillCallback.mListIndex); 141 } 142} 143