- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / android / javatests / src / org / chromium / chrome / browser / autofill / AutofillTest.java
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.
4
5 package org.chromium.chrome.browser.autofill;
6
7 import android.test.suitebuilder.annotation.SmallTest;
8
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;
21
22 import java.util.concurrent.atomic.AtomicBoolean;
23
24 /**
25  * Tests the Autofill's java code for creating the AutofillPopup object, opening and selecting
26  * popups.
27  */
28 public class AutofillTest extends ChromiumTestShellTestBase {
29
30     private AutofillPopup mAutofillPopup;
31     private WindowAndroid mWindowAndroid;
32     private MockAutofillCallback mMockAutofillCallback;
33
34     @Override
35     public void setUp() throws Exception {
36         super.setUp();
37         ChromiumTestShellActivity activity = launchChromiumTestShellWithBlankPage();
38         assertNotNull(activity);
39         waitForActiveShellToBeDoneLoading();
40
41         mMockAutofillCallback = new MockAutofillCallback();
42         mWindowAndroid = new WindowAndroid(activity);
43         final ViewAndroidDelegate viewDelegate =
44                 activity.getActiveContentView().getContentViewCore().getViewAndroidDelegate();
45
46         UiUtils.runOnUiThread(getActivity(), new Runnable() {
47             @Override
48             public void run() {
49                 mAutofillPopup = new AutofillPopup(mWindowAndroid.getContext(),
50                         viewDelegate,
51                         mMockAutofillCallback);
52                 mAutofillPopup.setAnchorRect(50, 500, 500, 50);
53             }
54         });
55
56     }
57
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;
63
64         @Override
65         public void suggestionSelected(int listIndex) {
66             mListIndex = listIndex;
67             mAutofillPopup.dismiss();
68             mGotPopupSelection.set(true);
69         }
70
71         public boolean waitForCallback() throws InterruptedException {
72             return CriteriaHelper.pollForCriteria(new Criteria() {
73                 @Override
74                 public boolean isSatisfied() {
75                     return mGotPopupSelection.get();
76                 }
77             }, CALLBACK_TIMEOUT_MS, CHECK_INTERVAL_MS);
78         }
79
80         @Override
81         public void requestHide() {
82         }
83     }
84
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),
89         };
90     }
91
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),
99         };
100     }
101
102     public boolean openAutofillPopupAndWaitUntilReady(final AutofillSuggestion[] suggestions)
103             throws InterruptedException {
104         UiUtils.runOnUiThread(getActivity(), new Runnable() {
105             @Override
106             public void run() {
107                 mAutofillPopup.show(suggestions);
108             }
109         });
110         return CriteriaHelper.pollForCriteria(new Criteria() {
111             @Override
112             public boolean isSatisfied() {
113                 return mAutofillPopup.getListView().getChildCount() > 0;
114             }
115         });
116     }
117
118     @SmallTest
119     @Feature({"autofill"})
120     public void testAutofillWithDifferentNumberSuggestions() throws Exception {
121         assertTrue(openAutofillPopupAndWaitUntilReady(createTwoAutofillSuggestionArray()));
122         assertEquals(2, mAutofillPopup.getListView().getCount());
123
124         assertTrue(openAutofillPopupAndWaitUntilReady(createFiveAutofillSuggestionArray()));
125         assertEquals(5, mAutofillPopup.getListView().getCount());
126     }
127
128     @SmallTest
129     @Feature({"autofill"})
130     public void testAutofillClickFirstSuggestion() throws Exception {
131         AutofillSuggestion[] suggestions = createTwoAutofillSuggestionArray();
132         assertTrue(openAutofillPopupAndWaitUntilReady(suggestions));
133         assertEquals(2, mAutofillPopup.getListView().getCount());
134
135         TouchCommon touchCommon = new TouchCommon(this);
136         touchCommon.singleClickViewRelative(mAutofillPopup.getListView(), 10, 10);
137         assertTrue(mMockAutofillCallback.waitForCallback());
138
139         assertEquals(0, mMockAutofillCallback.mListIndex);
140     }
141 }