Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / android / javatests / src / org / chromium / chrome / browser / autofill / AutofillTest.java
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
5 package org.chromium.chrome.browser.autofill;
6
7 import android.test.suitebuilder.annotation.SmallTest;
8
9 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
10
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;
25
26 import java.util.concurrent.atomic.AtomicBoolean;
27
28 /**
29  * Tests the Autofill's java code for creating the AutofillPopup object, opening and selecting
30  * popups.
31  */
32 public class AutofillTest extends ChromeShellTestBase {
33
34     private AutofillPopup mAutofillPopup;
35     private WindowAndroid mWindowAndroid;
36     private MockAutofillCallback mMockAutofillCallback;
37
38     @Override
39     public void setUp() throws Exception {
40         super.setUp();
41         ChromeShellActivity activity = launchChromeShellWithBlankPage();
42         assertNotNull(activity);
43         waitForActiveShellToBeDoneLoading();
44
45         mMockAutofillCallback = new MockAutofillCallback();
46         mWindowAndroid = new ActivityWindowAndroid(activity);
47         final ViewAndroidDelegate viewDelegate =
48                 activity.getActiveContentViewCore().getViewAndroidDelegate();
49
50         ThreadUtils.runOnUiThreadBlocking(new Runnable() {
51             @Override
52             public void run() {
53                 mAutofillPopup = new AutofillPopup(mWindowAndroid.getActivity().get(),
54                         viewDelegate,
55                         mMockAutofillCallback);
56                 mAutofillPopup.filterAndShow(new AutofillSuggestion[0], false);
57                 mAutofillPopup.setAnchorRect(50, 500, 500, 50);
58             }
59         });
60     }
61
62     private static final long CALLBACK_TIMEOUT_MS = scaleTimeout(4000);
63     private static final int CHECK_INTERVAL_MS = 100;
64
65     private class MockAutofillCallback implements AutofillPopupDelegate{
66         private final AtomicBoolean mGotPopupSelection = new AtomicBoolean(false);
67         public int mListIndex = -1;
68
69         @Override
70         public void suggestionSelected(int listIndex) {
71             mListIndex = listIndex;
72             mAutofillPopup.dismiss();
73             mGotPopupSelection.set(true);
74         }
75
76         public boolean waitForCallback() throws InterruptedException {
77             return CriteriaHelper.pollForCriteria(new Criteria() {
78                 @Override
79                 public boolean isSatisfied() {
80                     return mGotPopupSelection.get();
81                 }
82             }, CALLBACK_TIMEOUT_MS, CHECK_INTERVAL_MS);
83         }
84
85         @Override
86         public void dismissed() {
87         }
88     }
89
90     private AutofillSuggestion[] createTwoAutofillSuggestionArray() {
91         return new AutofillSuggestion[] {
92             new AutofillSuggestion("Sherlock Holmes", "221B Baker Street", DropdownItem.NO_ICON,
93                 42),
94             new AutofillSuggestion("Arthur Dent", "West Country", DropdownItem.NO_ICON, 43),
95         };
96     }
97
98     private AutofillSuggestion[] createFiveAutofillSuggestionArray() {
99         return new AutofillSuggestion[] {
100             new AutofillSuggestion("Sherlock Holmes", "221B Baker Street", DropdownItem.NO_ICON,
101                 42),
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),
106         };
107     }
108
109     public boolean openAutofillPopupAndWaitUntilReady(final AutofillSuggestion[] suggestions)
110             throws InterruptedException {
111         ThreadUtils.runOnUiThreadBlocking(new Runnable() {
112             @Override
113             public void run() {
114                 mAutofillPopup.filterAndShow(suggestions, false);
115             }
116         });
117         return CriteriaHelper.pollForCriteria(new Criteria() {
118             @Override
119             public boolean isSatisfied() {
120                 return mAutofillPopup.getListView().getChildCount() > 0;
121             }
122         });
123     }
124
125     @SmallTest
126     @Feature({"autofill"})
127     public void testAutofillWithDifferentNumberSuggestions() throws Exception {
128         assertTrue(openAutofillPopupAndWaitUntilReady(createTwoAutofillSuggestionArray()));
129         assertEquals(2, mAutofillPopup.getListView().getCount());
130
131         assertTrue(openAutofillPopupAndWaitUntilReady(createFiveAutofillSuggestionArray()));
132         assertEquals(5, mAutofillPopup.getListView().getCount());
133     }
134
135     @SmallTest
136     @Feature({"autofill"})
137     public void testAutofillClickFirstSuggestion() throws Exception {
138         AutofillSuggestion[] suggestions = createTwoAutofillSuggestionArray();
139         assertTrue(openAutofillPopupAndWaitUntilReady(suggestions));
140         assertEquals(2, mAutofillPopup.getListView().getCount());
141
142         TouchCommon.singleClickViewRelative(mAutofillPopup.getListView(), 10, 10);
143         assertTrue(mMockAutofillCallback.waitForCallback());
144
145         assertEquals(0, mMockAutofillCallback.mListIndex);
146     }
147 }