1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.app.Instrumentation;
20import android.test.ActivityInstrumentationTestCase2;
21import android.test.FlakyTest;
22
23// TODO: tests fail intermittently. Add back MediumTest annotation when fixed
24public class AutoCompleteTextViewCallbacks
25        extends ActivityInstrumentationTestCase2<AutoCompleteTextViewSimple> {
26
27    private static final int WAIT_TIME = 200;
28
29    public AutoCompleteTextViewCallbacks() {
30        super("com.android.frameworks.coretests", AutoCompleteTextViewSimple.class);
31    }
32
33    /** Test that the initial popup of the suggestions does not select anything.
34     */
35    @FlakyTest(tolerance=3)
36    public void testPopupNoSelection() throws Exception {
37        AutoCompleteTextViewSimple theActivity = getActivity();
38        AutoCompleteTextView textView = theActivity.getTextView();
39        final Instrumentation instrumentation = getInstrumentation();
40
41        // focus and type
42        textView.requestFocus();
43        instrumentation.waitForIdleSync();
44        sendKeys("A");
45        instrumentation.waitForIdleSync();
46        // give UI time to settle
47        Thread.sleep(WAIT_TIME);
48
49        // now check for selection callbacks.  Nothing should be clicked or selected.
50        assertFalse("onItemClick should not be called", theActivity.mItemClickCalled);
51        assertFalse("onItemSelected should not be called", theActivity.mItemSelectedCalled);
52
53        // arguably, this should be "false", because we aren't deselecting - we shouldn't
54        // really be calling it.  But it's not the end of the world, and we might wind up
55        // breaking something if we change this.
56        //assertTrue("onNothingSelected should be called", theActivity.mNothingSelectedCalled);
57    }
58
59    /** Test that arrow-down into the popup calls the onSelected callback. */
60    @FlakyTest(tolerance=3)
61    public void testPopupEnterSelection() throws Exception {
62        final AutoCompleteTextViewSimple theActivity = getActivity();
63        AutoCompleteTextView textView = theActivity.getTextView();
64        final Instrumentation instrumentation = getInstrumentation();
65
66        // focus and type
67        textView.requestFocus();
68        instrumentation.waitForIdleSync();
69        sendKeys("A");
70
71        textView.post(new Runnable() {
72            public void run() {
73                // prepare to move down into the popup
74                theActivity.resetItemListeners();
75            }
76        });
77
78        sendKeys("DPAD_DOWN");
79        instrumentation.waitForIdleSync();
80        // give UI time to settle
81        Thread.sleep(WAIT_TIME);
82
83        // now check for selection callbacks.
84        assertFalse("onItemClick should not be called", theActivity.mItemClickCalled);
85        assertTrue("onItemSelected should be called", theActivity.mItemSelectedCalled);
86        assertEquals("onItemSelected position", 0, theActivity.mItemSelectedPosition);
87        assertFalse("onNothingSelected should not be called", theActivity.mNothingSelectedCalled);
88
89        textView.post(new Runnable() {
90            public void run() {
91                // try one more time - should move from 0 to 1
92                theActivity.resetItemListeners();
93            }
94        });
95
96        sendKeys("DPAD_DOWN");
97        instrumentation.waitForIdleSync();
98        // give UI time to settle
99        Thread.sleep(WAIT_TIME);
100
101        // now check for selection callbacks.
102        assertFalse("onItemClick should not be called", theActivity.mItemClickCalled);
103        assertTrue("onItemSelected should be called", theActivity.mItemSelectedCalled);
104        assertEquals("onItemSelected position", 1, theActivity.mItemSelectedPosition);
105        assertFalse("onNothingSelected should not be called", theActivity.mNothingSelectedCalled);
106    }
107
108    /** Test that arrow-up out of the popup calls the onNothingSelected callback */
109    @FlakyTest(tolerance=3)
110    public void testPopupLeaveSelection() {
111        final AutoCompleteTextViewSimple theActivity = getActivity();
112        AutoCompleteTextView textView = theActivity.getTextView();
113        final Instrumentation instrumentation = getInstrumentation();
114
115        // focus and type
116        textView.requestFocus();
117        instrumentation.waitForIdleSync();
118        sendKeys("A");
119        instrumentation.waitForIdleSync();
120
121        // move down into the popup
122        sendKeys("DPAD_DOWN");
123        instrumentation.waitForIdleSync();
124
125        textView.post(new Runnable() {
126            public void run() {
127                // prepare to move down into the popup
128                theActivity.resetItemListeners();
129            }
130        });
131
132        sendKeys("DPAD_UP");
133        instrumentation.waitForIdleSync();
134
135        // now check for selection callbacks.
136        assertFalse("onItemClick should not be called", theActivity.mItemClickCalled);
137        assertFalse("onItemSelected should not be called", theActivity.mItemSelectedCalled);
138        assertTrue("onNothingSelected should be called", theActivity.mNothingSelectedCalled);
139    }
140
141}
142