• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 
17 package android.view.cts;
18 
19 import static com.android.compatibility.common.util.CtsMockitoUtils.within;
20 
21 import static org.junit.Assert.assertArrayEquals;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Mockito.doAnswer;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.reset;
31 import static org.mockito.Mockito.spy;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.verifyZeroInteractions;
35 
36 import android.app.Activity;
37 import android.app.Instrumentation;
38 import android.graphics.Bitmap;
39 import android.graphics.Color;
40 import android.support.test.uiautomator.UiDevice;
41 import android.view.View;
42 import android.view.View.OnClickListener;
43 import android.widget.Button;
44 import android.widget.EditText;
45 import android.widget.RelativeLayout;
46 import android.widget.TextView;
47 
48 import androidx.test.InstrumentationRegistry;
49 import androidx.test.annotation.UiThreadTest;
50 import androidx.test.filters.MediumTest;
51 import androidx.test.rule.ActivityTestRule;
52 import androidx.test.runner.AndroidJUnit4;
53 
54 import com.android.compatibility.common.util.CtsTouchUtils;
55 
56 import org.junit.Before;
57 import org.junit.Rule;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60 import org.mockito.invocation.InvocationOnMock;
61 
62 @MediumTest
63 @RunWith(AndroidJUnit4.class)
64 public class View_UsingViewsTest {
65     /**
66      * country of Argentina
67      */
68     private static final String ARGENTINA = "Argentina";
69 
70     /**
71      * country of America
72      */
73     private static final String AMERICA = "America";
74 
75     /**
76      * country of China
77      */
78     private static final String CHINA = "China";
79 
80     /**
81      * the symbol of Argentina is football
82      */
83     private static final String ARGENTINA_SYMBOL = "football";
84 
85     /**
86      * the symbol of America is basketball
87      */
88     private static final String AMERICA_SYMBOL = "basketball";
89 
90     /**
91      * the symbol of China is table tennis
92      */
93     private static final String CHINA_SYMBOL = "table tennis";
94 
95     private Instrumentation mInstrumentation;
96     private CtsTouchUtils mCtsTouchUtils;
97 
98     private Activity mActivity;
99 
100     private EditText mEditText;
101     private Button mButtonOk;
102     private Button mButtonCancel;
103     private TextView mSymbolTextView;
104     private TextView mWarningTextView;
105 
106     @Rule
107     public ActivityTestRule<UsingViewsCtsActivity> mActivityRule =
108             new ActivityTestRule<>(UsingViewsCtsActivity.class);
109 
110     @Before
setup()111     public void setup() {
112         mInstrumentation = InstrumentationRegistry.getInstrumentation();
113         mCtsTouchUtils = new CtsTouchUtils(mInstrumentation.getTargetContext());
114         mActivity = mActivityRule.getActivity();
115 
116         mEditText = (EditText) mActivity.findViewById(R.id.entry);
117         mButtonOk = (Button) mActivity.findViewById(R.id.ok);
118         mButtonCancel = (Button) mActivity.findViewById(R.id.cancel);
119         mSymbolTextView = (TextView) mActivity.findViewById(R.id.symbolball);
120         mWarningTextView = (TextView) mActivity.findViewById(R.id.warning);
121     }
122 
123     @UiThreadTest
124     @Test
testSetProperties()125     public void testSetProperties() {
126         // setClickable, setOnClickListener
127         mButtonOk.setClickable(true);
128         assertTrue(mButtonOk.isClickable());
129 
130         View.OnClickListener okButtonListener = spy(new MockOnClickOkListener());
131         mButtonOk.setOnClickListener(okButtonListener);
132 
133         mButtonOk.performClick();
134         verify(okButtonListener, times(1)).onClick(mButtonOk);
135 
136         mButtonCancel.setClickable(false);
137         assertFalse(mButtonCancel.isClickable());
138 
139         View.OnClickListener cancelButtonListener = mock(View.OnClickListener.class);
140         doAnswer((InvocationOnMock invocation) -> {
141             mEditText.setText(null);
142             return null;
143         }).when(cancelButtonListener).onClick(any(View.class));
144         mButtonCancel.setOnClickListener(cancelButtonListener);
145         assertTrue(mButtonCancel.isClickable());
146 
147         mButtonCancel.performClick();
148         verify(cancelButtonListener, times(1)).onClick(mButtonCancel);
149 
150         // setDrawingCacheEnabled, setDrawingCacheQuality, setDrawingCacheBackgroundColor,
151         mEditText.setDrawingCacheEnabled(true);
152         assertTrue(mEditText.isDrawingCacheEnabled());
153 
154         // the default quality is auto
155         assertEquals(View.DRAWING_CACHE_QUALITY_AUTO, mEditText.getDrawingCacheQuality());
156         mEditText.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
157         assertEquals(View.DRAWING_CACHE_QUALITY_LOW, mEditText.getDrawingCacheQuality());
158         mEditText.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
159         assertEquals(View.DRAWING_CACHE_QUALITY_HIGH, mEditText.getDrawingCacheQuality());
160 
161         mEditText.setDrawingCacheBackgroundColor(Color.GREEN);
162         assertEquals(Color.GREEN, mEditText.getDrawingCacheBackgroundColor());
163 
164         // create the cache
165         Bitmap b = mEditText.getDrawingCache();
166         assertNotNull(b);
167         assertEquals(mEditText.getHeight(), b.getHeight());
168         assertEquals(mEditText.getWidth(), b.getWidth());
169         assertEquals(Color.GREEN, b.getPixel(0, 0));
170 
171         // setDrawingCacheEnabled to false
172         mEditText.setDrawingCacheEnabled(false);
173         assertFalse(mEditText.isDrawingCacheEnabled());
174 
175         mEditText.setDrawingCacheBackgroundColor(Color.YELLOW);
176         assertEquals(Color.YELLOW, mEditText.getDrawingCacheBackgroundColor());
177 
178         // build drawable cache
179         mEditText.buildDrawingCache();
180         b = mEditText.getDrawingCache();
181         assertNotNull(b);
182         assertEquals(mEditText.getHeight(), b.getHeight());
183         assertEquals(mEditText.getWidth(), b.getWidth());
184         assertEquals(Color.YELLOW, b.getPixel(0, 0));
185         mEditText.destroyDrawingCache();
186 
187         // setDuplicateParentStateEnabled
188         TextView v = new TextView(mActivity);
189         v.setSingleLine(); // otherwise the multiline state interferes with theses tests
190         v.setEnabled(false);
191         v.setText("Test setDuplicateParentStateEnabled");
192 
193         v.setDuplicateParentStateEnabled(false);
194         assertFalse(v.isDuplicateParentStateEnabled());
195 
196         RelativeLayout parent = (RelativeLayout) mEditText.getParent();
197         parent.addView(v);
198 
199         assertFalse(parent.getDrawableState().length == v.getDrawableState().length);
200         parent.removeView(v);
201 
202         v.setDuplicateParentStateEnabled(true);
203         assertTrue(v.isDuplicateParentStateEnabled());
204 
205         parent.addView(v);
206         v.refreshDrawableState();
207 
208         assertArrayEquals(parent.getDrawableState(), v.getDrawableState());
209         parent.removeView(v);
210 
211         // setEnabled
212         mWarningTextView.setEnabled(false);
213         assertFalse(mWarningTextView.isEnabled());
214 
215         mWarningTextView.setEnabled(true);
216         assertTrue(mWarningTextView.isEnabled());
217 
218         // setFadingEdgeLength, setVerticalFadingEdgeEnabled and
219         // setHorizontalFadingEdgeEnabled(boolean)
220         mWarningTextView.setVerticalFadingEdgeEnabled(true);
221         assertTrue(mWarningTextView.isVerticalFadingEdgeEnabled());
222         mWarningTextView.setFadingEdgeLength(10);
223 
224         mSymbolTextView.setHorizontalFadingEdgeEnabled(true);
225         assertTrue(mSymbolTextView.isHorizontalFadingEdgeEnabled());
226         mSymbolTextView.setFadingEdgeLength(100);
227 
228         // setFocusable and setFocusableInTouchMode
229         mButtonCancel.setFocusable(false);
230         assertFalse(mButtonCancel.isFocusable());
231         assertFalse(mButtonCancel.isFocusableInTouchMode());
232 
233         mButtonCancel.setFocusable(true);
234         assertTrue(mButtonCancel.isFocusable());
235         assertFalse(mButtonCancel.isFocusableInTouchMode());
236 
237         mButtonCancel.setFocusableInTouchMode(true);
238         assertTrue(mButtonCancel.isFocusable());
239         assertTrue(mButtonCancel.isFocusableInTouchMode());
240 
241         mButtonOk.setFocusable(false);
242         assertFalse(mButtonOk.isFocusable());
243         assertFalse(mButtonOk.isFocusableInTouchMode());
244 
245         mButtonOk.setFocusableInTouchMode(true);
246         assertTrue(mButtonOk.isFocusable());
247         assertTrue(mButtonOk.isFocusableInTouchMode());
248 
249         // setHorizontalScrollBarEnabled and setVerticalScrollBarEnabled
250         // both two bar is not drawn by default
251         assertFalse(parent.isHorizontalScrollBarEnabled());
252         assertFalse(parent.isVerticalScrollBarEnabled());
253 
254         parent.setHorizontalScrollBarEnabled(true);
255         assertTrue(parent.isHorizontalScrollBarEnabled());
256 
257         parent.setVerticalScrollBarEnabled(true);
258         assertTrue(parent.isVerticalScrollBarEnabled());
259 
260         // setId
261         assertEquals(View.NO_ID, parent.getId());
262         assertEquals(R.id.entry, mEditText.getId());
263         assertEquals(R.id.symbolball, mSymbolTextView.getId());
264 
265         mSymbolTextView.setId(0x5555);
266         assertEquals(0x5555, mSymbolTextView.getId());
267         TextView t = (TextView) parent.findViewById(0x5555);
268         assertSame(mSymbolTextView, t);
269 
270         mSymbolTextView.setId(R.id.symbolball);
271         assertEquals(R.id.symbolball, mSymbolTextView.getId());
272     }
273 
274     @UiThreadTest
275     @Test
testSetFocus()276     public void testSetFocus() {
277         boolean focusWasOnEditText = mEditText.hasFocus();
278 
279         View.OnFocusChangeListener editListener = mock(View.OnFocusChangeListener.class);
280         View.OnFocusChangeListener okListener = mock(View.OnFocusChangeListener.class);
281         View.OnFocusChangeListener cancelListener = mock(View.OnFocusChangeListener.class);
282         View.OnFocusChangeListener symbolListener = mock(View.OnFocusChangeListener.class);
283         View.OnFocusChangeListener warningListener = mock(View.OnFocusChangeListener.class);
284 
285         mEditText.setOnFocusChangeListener(editListener);
286         mButtonOk.setOnFocusChangeListener(okListener);
287         mButtonCancel.setOnFocusChangeListener(cancelListener);
288         mSymbolTextView.setOnFocusChangeListener(symbolListener);
289         mWarningTextView.setOnFocusChangeListener(warningListener);
290 
291         mSymbolTextView.setText(ARGENTINA_SYMBOL);
292         mWarningTextView.setVisibility(View.VISIBLE);
293 
294         assertTrue(mEditText.requestFocus());
295         assertTrue(mEditText.hasFocus());
296         assertFalse(mButtonOk.hasFocus());
297         assertFalse(mButtonCancel.hasFocus());
298         assertFalse(mSymbolTextView.hasFocus());
299         assertFalse(mWarningTextView.hasFocus());
300 
301         if (!focusWasOnEditText) {
302             verify(editListener, times(1)).onFocusChange(mEditText, true);
303         }
304         verifyZeroInteractions(okListener);
305         verifyZeroInteractions(cancelListener);
306         verifyZeroInteractions(symbolListener);
307         verifyZeroInteractions(warningListener);
308 
309         // set ok button to focus
310         reset(editListener);
311         assertTrue(mButtonOk.requestFocus());
312         assertTrue(mButtonOk.hasFocus());
313         verify(okListener, times(1)).onFocusChange(mButtonOk, true);
314         assertFalse(mEditText.hasFocus());
315         verify(editListener, times(1)).onFocusChange(mEditText, false);
316         verifyZeroInteractions(cancelListener);
317         verifyZeroInteractions(symbolListener);
318         verifyZeroInteractions(warningListener);
319 
320         // set cancel button to focus
321         reset(okListener);
322         reset(editListener);
323         assertTrue(mButtonCancel.requestFocus());
324         assertTrue(mButtonCancel.hasFocus());
325         verify(cancelListener, times(1)).onFocusChange(mButtonCancel, true);
326         assertFalse(mButtonOk.hasFocus());
327         verify(okListener, times(1)).onFocusChange(mButtonOk, false);
328         verifyZeroInteractions(editListener);
329         verifyZeroInteractions(symbolListener);
330         verifyZeroInteractions(warningListener);
331 
332         // set symbol text to focus
333         mSymbolTextView.setFocusable(true);
334         assertTrue(mSymbolTextView.requestFocus());
335         assertTrue(mSymbolTextView.hasFocus());
336         verify(symbolListener, times(1)).onFocusChange(mSymbolTextView, true);
337         assertFalse(mButtonCancel.hasFocus());
338         verify(cancelListener, times(1)).onFocusChange(mButtonCancel, false);
339         verifyZeroInteractions(okListener);
340         verifyZeroInteractions(editListener);
341         verifyZeroInteractions(warningListener);
342 
343         // set warning text to focus
344         mWarningTextView.setFocusable(true);
345         assertTrue(mWarningTextView.requestFocus());
346         assertTrue(mWarningTextView.hasFocus());
347         verify(warningListener, times(1)).onFocusChange(mWarningTextView, true);
348         assertFalse(mSymbolTextView.hasFocus());
349         verify(symbolListener, times(1)).onFocusChange(mSymbolTextView, false);
350         verifyZeroInteractions(editListener);
351         verifyZeroInteractions(okListener);
352         verifyZeroInteractions(cancelListener);
353 
354         // set edit text to focus
355         assertTrue(mEditText.requestFocus());
356         assertTrue(mEditText.hasFocus());
357         verify(editListener, times(1)).onFocusChange(mEditText, true);
358         assertFalse(mWarningTextView.hasFocus());
359         verify(warningListener, times(1)).onFocusChange(mWarningTextView, false);
360         verifyZeroInteractions(cancelListener);
361         verifyZeroInteractions(symbolListener);
362         verifyZeroInteractions(okListener);
363     }
364 
365     @Test
testSetupListeners()366     public void testSetupListeners() throws Throwable {
367         // set ok button OnClick listener
368         mButtonOk.setClickable(true);
369         assertTrue(mButtonOk.isClickable());
370 
371         View.OnClickListener okButtonListener = spy(new MockOnClickOkListener());
372         mButtonOk.setOnClickListener(okButtonListener);
373 
374         // set cancel button OnClick listener
375         mButtonCancel.setClickable(true);
376         assertTrue(mButtonCancel.isClickable());
377 
378         View.OnClickListener cancelButtonListener = mock(View.OnClickListener.class);
379         doAnswer((InvocationOnMock invocation) -> {
380             mEditText.setText(null);
381             return null;
382         }).when(cancelButtonListener).onClick(any(View.class));
383         mButtonCancel.setOnClickListener(cancelButtonListener);
384 
385         // set edit text OnLongClick listener
386         mEditText.setLongClickable(true);
387         assertTrue(mEditText.isLongClickable());
388 
389         final View.OnLongClickListener onLongClickListener =
390                 mock(View.OnLongClickListener.class);
391         mEditText.setOnLongClickListener(onLongClickListener);
392 
393         // long click the edit text
394         mInstrumentation.waitForIdleSync();
395         mCtsTouchUtils.emulateLongPressOnViewCenter(mInstrumentation, mActivityRule, mEditText);
396         verify(onLongClickListener, within(1000)).onLongClick(mEditText);
397 
398         // Wait for the UI Thread to become idle.
399         final UiDevice device = UiDevice.getInstance(mInstrumentation);
400 
401         // click the Cancel button
402         mActivityRule.runOnUiThread(() -> mEditText.setText("Germany"));
403         mInstrumentation.waitForIdleSync();
404         device.waitForIdle();
405 
406         mCtsTouchUtils.emulateTapOnViewCenter(mInstrumentation, mActivityRule, mButtonCancel);
407         assertEquals("", mEditText.getText().toString());
408 
409         mInstrumentation.waitForIdleSync();
410         device.waitForIdle();
411 
412         // click the OK button
413         mActivityRule.runOnUiThread(() -> mEditText.setText(ARGENTINA));
414         mInstrumentation.waitForIdleSync();
415 
416         mCtsTouchUtils.emulateTapOnViewCenter(mInstrumentation, mActivityRule, mButtonOk);
417         assertEquals(ARGENTINA_SYMBOL, mSymbolTextView.getText().toString());
418 
419         mActivityRule.runOnUiThread(() -> mEditText.setText(AMERICA));
420         mInstrumentation.waitForIdleSync();
421 
422         mCtsTouchUtils.emulateTapOnViewCenter(mInstrumentation, mActivityRule, mButtonOk);
423         assertEquals(AMERICA_SYMBOL, mSymbolTextView.getText().toString());
424 
425         mActivityRule.runOnUiThread(() -> mEditText.setText(CHINA));
426         mInstrumentation.waitForIdleSync();
427 
428         mCtsTouchUtils.emulateTapOnViewCenter(mInstrumentation, mActivityRule, mButtonOk);
429         assertEquals(CHINA_SYMBOL, mSymbolTextView.getText().toString());
430 
431         mActivityRule.runOnUiThread(() -> mEditText.setText("Unknown"));
432         mInstrumentation.waitForIdleSync();
433 
434         mCtsTouchUtils.emulateTapOnViewCenter(mInstrumentation, mActivityRule, mButtonOk);
435         assertEquals(View.VISIBLE, mWarningTextView.getVisibility());
436     }
437 
438     @UiThreadTest
439     @Test
testSetVisibility()440     public void testSetVisibility() {
441         mActivity.setContentView(R.layout.view_visibility_layout);
442 
443         View v1 = mActivity.findViewById(R.id.textview1);
444         View v2 = mActivity.findViewById(R.id.textview2);
445         View v3 = mActivity.findViewById(R.id.textview3);
446 
447         assertNotNull(v1);
448         assertNotNull(v2);
449         assertNotNull(v3);
450 
451         assertEquals(View.VISIBLE, v1.getVisibility());
452         assertEquals(View.INVISIBLE, v2.getVisibility());
453         assertEquals(View.GONE, v3.getVisibility());
454 
455         v1.setVisibility(View.GONE);
456         assertEquals(View.GONE, v1.getVisibility());
457 
458         v2.setVisibility(View.VISIBLE);
459         assertEquals(View.VISIBLE, v2.getVisibility());
460 
461         v3.setVisibility(View.INVISIBLE);
462         assertEquals(View.INVISIBLE, v3.getVisibility());
463     }
464 
465     protected class MockOnClickOkListener implements OnClickListener {
showPicture(String country)466         private boolean showPicture(String country) {
467             if (ARGENTINA.equals(country)) {
468                 mSymbolTextView.setText(ARGENTINA_SYMBOL);
469                 return true;
470             } else if (AMERICA.equals(country)) {
471                 mSymbolTextView.setText(AMERICA_SYMBOL);
472                 return true;
473             } else if (CHINA.equals(country)) {
474                 mSymbolTextView.setText(CHINA_SYMBOL);
475                 return true;
476             }
477 
478             return false;
479         }
480 
onClick(View v)481         public void onClick(View v) {
482             String country = mEditText.getText().toString();
483             if (!showPicture(country)) {
484                 mWarningTextView.setVisibility(View.VISIBLE);
485             } else if (View.VISIBLE == mWarningTextView.getVisibility()) {
486                 mWarningTextView.setVisibility(View.INVISIBLE);
487             }
488         }
489     }
490 }
491