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 
17 package android.widget.cts;
18 
19 import com.android.cts.widget.R;
20 
21 import org.xmlpull.v1.XmlPullParser;
22 import org.xmlpull.v1.XmlPullParserException;
23 
24 import android.app.Activity;
25 import android.app.Instrumentation;
26 import android.content.Context;
27 import android.cts.util.PollingCheck;
28 import android.cts.util.WidgetTestUtils;
29 import android.graphics.Canvas;
30 import android.graphics.Color;
31 import android.graphics.Rect;
32 import android.graphics.drawable.Drawable;
33 import android.test.ActivityInstrumentationTestCase2;
34 import android.test.TouchUtils;
35 import android.text.Editable;
36 import android.text.SpannableStringBuilder;
37 import android.util.AttributeSet;
38 import android.util.Xml;
39 import android.view.ContextMenu.ContextMenuInfo;
40 import android.view.View;
41 import android.view.ViewGroup;
42 import android.widget.AbsListView;
43 import android.widget.AbsListView.OnScrollListener;
44 import android.widget.AbsListView.RecyclerListener;
45 import android.widget.AdapterView;
46 import android.widget.AdapterView.OnItemLongClickListener;
47 import android.widget.ArrayAdapter;
48 import android.widget.ListAdapter;
49 import android.widget.ListView;
50 import android.widget.TextView;
51 
52 import java.io.IOException;
53 import java.util.ArrayList;
54 import java.util.List;
55 
56 public class AbsListViewTest extends ActivityInstrumentationTestCase2<ListViewCtsActivity> {
57     private final String[] mShortList = new String[] {
58         "This", "is", "short", "!",
59     };
60     private final String[] mCountryList = new String[] {
61         "Argentina", "Australia", "China", "France", "Germany", "Italy", "Japan", "United States",
62         "Argentina", "Australia", "China", "France", "Germany", "Italy", "Japan", "United States",
63         "Argentina", "Australia", "China", "France", "Germany", "Italy", "Japan", "United States"
64     };
65 
66     private ListView mListView;
67     private Activity mActivity;
68     private Instrumentation mInstrumentation;
69     private AttributeSet mAttributeSet;
70     private ArrayAdapter<String> mAdapter_short;
71     private ArrayAdapter<String> mAdapter_countries;
72 
73     private static final float DELTA = 0.001f;
74 
AbsListViewTest()75     public AbsListViewTest() {
76         super("com.android.cts.widget", ListViewCtsActivity.class);
77     }
78 
79 
80     @Override
setUp()81     protected void setUp() throws Exception {
82         super.setUp();
83 
84         mActivity = getActivity();
85         new PollingCheck() {
86             @Override
87                 protected boolean check() {
88                 return mActivity.hasWindowFocus();
89             }
90         }.run();
91         mInstrumentation = getInstrumentation();
92 
93         XmlPullParser parser = mActivity.getResources().getXml(R.layout.listview_layout);
94         WidgetTestUtils.beginDocument(parser, "LinearLayout");
95         mAttributeSet = Xml.asAttributeSet(parser);
96 
97         mAdapter_short = new ArrayAdapter<String>(mActivity,
98                 android.R.layout.simple_list_item_1, mShortList);
99         mAdapter_countries = new ArrayAdapter<String>(mActivity,
100                 android.R.layout.simple_list_item_1, mCountryList);
101 
102         mListView = (ListView)mActivity.findViewById(R.id.listview_default);
103     }
104 
testConstructor()105     public void testConstructor() {
106         /**
107          * We can not test the constructors.
108          */
109     }
110 
testAccessFastScrollEnabled()111     public void testAccessFastScrollEnabled() {
112         mListView.setFastScrollEnabled(false);
113         assertFalse(mListView.isFastScrollEnabled());
114 
115         mListView.setFastScrollEnabled(true);
116         assertTrue(mListView.isFastScrollEnabled());
117     }
118 
testAccessSmoothScrollbarEnabled()119     public void testAccessSmoothScrollbarEnabled() {
120         mListView.setSmoothScrollbarEnabled(false);
121         assertFalse(mListView.isSmoothScrollbarEnabled());
122 
123         mListView.setSmoothScrollbarEnabled(true);
124         assertTrue(mListView.isSmoothScrollbarEnabled());
125     }
126 
testAccessScrollingCacheEnabled()127     public void testAccessScrollingCacheEnabled() {
128         mListView.setScrollingCacheEnabled(false);
129         assertFalse(mListView.isScrollingCacheEnabled());
130 
131         mListView.setScrollingCacheEnabled(true);
132         assertTrue(mListView.isScrollingCacheEnabled());
133     }
134 
setAdapter()135     private void setAdapter() throws Throwable {
136         setAdapter(mAdapter_countries);
137     }
138 
setAdapter(final ListAdapter adapter)139     private void setAdapter(final ListAdapter adapter) throws Throwable {
140         runTestOnUiThread(new Runnable() {
141             public void run() {
142                 mListView.setAdapter(adapter);
143             }
144         });
145         mInstrumentation.waitForIdleSync();
146     }
147 
setListSelection(int index)148     private void setListSelection(int index) throws Throwable {
149         final int i = index;
150 
151         runTestOnUiThread(new Runnable() {
152             public void run() {
153                 mListView.setSelection(i);
154             }
155         });
156         mInstrumentation.waitForIdleSync();
157     }
testSetOnScrollListener()158     public void testSetOnScrollListener() throws Throwable {
159         MockOnScrollListener onScrollListener = new MockOnScrollListener();
160 
161         assertNull(onScrollListener.getView());
162         assertEquals(0, onScrollListener.getFirstVisibleItem());
163         assertEquals(0, onScrollListener.getVisibleItemCount());
164         assertEquals(0, onScrollListener.getTotalItemCount());
165         assertEquals(-1, onScrollListener.getScrollState());
166 
167         assertFalse(onScrollListener.isOnScrollCalled());
168         assertFalse(onScrollListener.isOnScrollStateChangedCalled());
169 
170         mListView.setOnScrollListener(onScrollListener);
171         assertSame(mListView, onScrollListener.getView());
172         assertEquals(0, onScrollListener.getFirstVisibleItem());
173         assertEquals(0, onScrollListener.getVisibleItemCount());
174         assertEquals(0, onScrollListener.getTotalItemCount());
175         assertEquals(-1, onScrollListener.getScrollState());
176 
177         assertTrue(onScrollListener.isOnScrollCalled());
178         assertFalse(onScrollListener.isOnScrollStateChangedCalled());
179         onScrollListener.reset();
180 
181         setAdapter();
182 
183         assertSame(mListView, onScrollListener.getView());
184         assertEquals(0, onScrollListener.getFirstVisibleItem());
185         assertEquals(mListView.getChildCount(), onScrollListener.getVisibleItemCount());
186         assertEquals(mCountryList.length, onScrollListener.getTotalItemCount());
187         assertEquals(-1, onScrollListener.getScrollState());
188 
189         assertTrue(onScrollListener.isOnScrollCalled());
190         assertFalse(onScrollListener.isOnScrollStateChangedCalled());
191         onScrollListener.reset();
192 
193         TouchUtils.scrollToBottom(this, mActivity, mListView);
194         assertSame(mListView, onScrollListener.getView());
195         assertEquals(mListView.getChildCount(), onScrollListener.getVisibleItemCount());
196         assertEquals(mCountryList.length, onScrollListener.getTotalItemCount());
197 
198         assertTrue(onScrollListener.isOnScrollCalled());
199         assertTrue(onScrollListener.isOnScrollStateChangedCalled());
200     }
201 
testFling()202     public void testFling() throws Throwable {
203         MockOnScrollListener onScrollListener = new MockOnScrollListener();
204         mListView.setOnScrollListener(onScrollListener);
205 
206         setAdapter();
207 
208         // Fling down from top, expect a scroll.
209         fling(10000, onScrollListener);
210         assertTrue(onScrollListener.isOnScrollCalled());
211         assertTrue(0 < onScrollListener.getFirstVisibleItem());
212 
213         // Fling up the same amount, expect a scroll to the original position.
214         fling(-10000, onScrollListener);
215         assertTrue(onScrollListener.isOnScrollCalled());
216         assertEquals(0, onScrollListener.getFirstVisibleItem());
217 
218         // Fling up again, expect no scroll, as the viewport is already at top.
219         fling(-10000, onScrollListener);
220         assertFalse(onScrollListener.isOnScrollCalled());
221         assertEquals(0, onScrollListener.getFirstVisibleItem());
222 
223         // Fling up again with a huge velocity, expect no scroll.
224         fling(-50000, onScrollListener);
225         assertFalse(onScrollListener.isOnScrollCalled());
226         assertEquals(0, onScrollListener.getFirstVisibleItem());
227     }
228 
fling(int velocityY, MockOnScrollListener onScrollListener)229     private void fling(int velocityY, MockOnScrollListener onScrollListener) throws Throwable {
230         onScrollListener.reset();
231 
232         final int v = velocityY;
233         runTestOnUiThread(new Runnable() {
234             public void run() {
235                 mListView.fling(v);
236             }
237         });
238 
239         do {
240             mInstrumentation.waitForIdleSync();
241         } while (onScrollListener.getScrollState() != OnScrollListener.SCROLL_STATE_IDLE);
242     }
243 
testGetFocusedRect()244     public void testGetFocusedRect() throws Throwable {
245         setAdapter(mAdapter_short);
246         setListSelection(0);
247 
248         Rect r1 = new Rect();
249         mListView.getFocusedRect(r1);
250 
251         assertEquals(0, r1.top);
252         assertTrue(r1.bottom > 0);
253         assertEquals(0, r1.left);
254         assertTrue(r1.right > 0);
255 
256         setListSelection(3);
257         Rect r2 = new Rect();
258         mListView.getFocusedRect(r2);
259         assertTrue(r2.top > 0);
260         assertTrue(r2.bottom > 0);
261         assertEquals(0, r2.left);
262         assertTrue(r2.right > 0);
263 
264         assertTrue(r2.top > r1.top);
265         assertEquals(r1.bottom - r1.top, r2.bottom - r2.top);
266         assertEquals(r1.right, r2.right);
267     }
268 
testAccessStackFromBottom()269     public void testAccessStackFromBottom() throws Throwable {
270         setAdapter();
271 
272         runTestOnUiThread(new Runnable() {
273             public void run() {
274                 mListView.setStackFromBottom(false);
275             }
276         });
277         assertFalse(mListView.isStackFromBottom());
278         assertEquals(0, mListView.getSelectedItemPosition());
279 
280         runTestOnUiThread(new Runnable() {
281             public void run() {
282                 mListView.setStackFromBottom(true);
283             }
284         });
285 
286         mInstrumentation.waitForIdleSync();
287         assertTrue(mListView.isStackFromBottom());
288         // ensure last item in list is selected
289         assertEquals(mCountryList.length-1, mListView.getSelectedItemPosition());
290     }
291 
testAccessSelectedItem()292     public void testAccessSelectedItem() throws Throwable {
293         assertNull(mListView.getSelectedView());
294 
295         setAdapter();
296         TextView tv = (TextView) mListView.getSelectedView();
297         assertEquals(mCountryList[0], tv.getText().toString());
298 
299         setListSelection(5);
300         tv = (TextView) mListView.getSelectedView();
301         assertEquals(mCountryList[5], tv.getText().toString());
302 
303         setListSelection(2);
304         tv = (TextView) mListView.getSelectedView();
305         assertEquals(mCountryList[2], tv.getText().toString());
306     }
307 
testAccessListPadding()308     public void testAccessListPadding() throws Throwable {
309         setAdapter();
310 
311         assertEquals(0, mListView.getListPaddingLeft());
312         assertEquals(0, mListView.getListPaddingTop());
313         assertEquals(0, mListView.getListPaddingRight());
314         assertEquals(0, mListView.getListPaddingBottom());
315 
316         final Rect r = new Rect(0, 0, 40, 60);
317         runTestOnUiThread(new Runnable() {
318             public void run() {
319                 mListView.setPadding(r.left, r.top, r.right, r.bottom);
320             }
321         });
322         mInstrumentation.waitForIdleSync();
323 
324         assertEquals(r.left, mListView.getListPaddingLeft());
325         assertEquals(r.top, mListView.getListPaddingTop());
326         assertEquals(r.right, mListView.getListPaddingRight());
327         assertEquals(r.bottom, mListView.getListPaddingBottom());
328     }
329 
testAccessSelector()330     public void testAccessSelector() throws Throwable {
331         setAdapter();
332 
333         final Drawable d = mActivity.getResources().getDrawable(R.drawable.pass);
334         mListView.setSelector(d);
335 
336         runTestOnUiThread(new Runnable() {
337             public void run() {
338                 mListView.requestLayout();
339             }
340         });
341         mInstrumentation.waitForIdleSync();
342         assertSame(d, mListView.getSelector());
343         assertTrue(mListView.verifyDrawable(d));
344 
345         mListView.setSelector(R.drawable.failed);
346         mListView.setDrawSelectorOnTop(true);
347 
348         runTestOnUiThread(new Runnable() {
349             public void run() {
350                 mListView.requestLayout();
351             }
352         });
353         mInstrumentation.waitForIdleSync();
354 
355         Drawable drawable = mListView.getSelector();
356         assertNotNull(drawable);
357         final Rect r = drawable.getBounds();
358 
359         final TextView v = (TextView) mListView.getSelectedView();
360         new PollingCheck() {
361             @Override
362             protected boolean check() {
363                 return v.getRight() == r.right;
364             }
365         }.run();
366         assertEquals(v.getLeft(), r.left);
367         assertEquals(v.getTop(), r.top);
368         assertEquals(v.getBottom(), r.bottom);
369     }
370 
testSetScrollIndicators()371     public void testSetScrollIndicators() throws Throwable {
372         TextView tv1 = (TextView) mActivity.findViewById(R.id.headerview1);
373         TextView tv2 = (TextView) mActivity.findViewById(R.id.footerview1);
374 
375         setAdapter();
376 
377         mListView.setScrollIndicators(tv1, tv2);
378 
379         runTestOnUiThread(new Runnable() {
380             public void run() {
381                 mListView.requestLayout();
382             }
383         });
384         mInstrumentation.waitForIdleSync();
385     }
386 
testShowContextMenuForChild()387     public void testShowContextMenuForChild() throws Throwable {
388         setAdapter();
389         setListSelection(1);
390 
391         TextView tv = (TextView) mListView.getSelectedView();
392         assertFalse(mListView.showContextMenuForChild(tv));
393 
394         // TODO: how to show the contextMenu success
395     }
396 
testPointToPosition()397     public void testPointToPosition() throws Throwable {
398         assertEquals(AbsListView.INVALID_POSITION, mListView.pointToPosition(-1, -1));
399         assertEquals(AbsListView.INVALID_ROW_ID, mListView.pointToRowId(-1, -1));
400 
401         setAdapter();
402 
403         View row = mListView.getChildAt(0);
404         int rowHeight = row.getHeight();
405         int middleOfSecondRow = rowHeight + rowHeight/2;
406 
407         int position1 = mListView.pointToPosition(0, 0);
408         int position2 = mListView.pointToPosition(50, middleOfSecondRow);
409 
410         assertEquals(mAdapter_countries.getItemId(position1), mListView.pointToRowId(0, 0));
411         assertEquals(mAdapter_countries.getItemId(position2),
412                 mListView.pointToRowId(50, middleOfSecondRow));
413 
414         assertTrue(position2 > position1);
415     }
416 
testDraw()417     public void testDraw() {
418         Canvas canvas = new Canvas();
419         mListView.draw(canvas);
420 
421         MyListView listView = new MyListView(mActivity);
422         listView.dispatchDraw(canvas);
423 
424         // TODO: how to check
425     }
426 
testSetRecyclerListener()427     public void testSetRecyclerListener() throws Throwable {
428         setAdapter();
429 
430         MockRecyclerListener recyclerListener = new MockRecyclerListener();
431         List<View> views = new ArrayList<View>();
432 
433         assertNull(recyclerListener.getView());
434         mListView.setRecyclerListener(recyclerListener);
435         mListView.reclaimViews(views);
436 
437         assertTrue(views.size() > 0);
438         assertNotNull(recyclerListener.getView());
439 
440         assertSame(recyclerListener.getView(), views.get(views.size() - 1));
441     }
442 
testAccessCacheColorHint()443     public void testAccessCacheColorHint() {
444         mListView.setCacheColorHint(Color.RED);
445         assertEquals(Color.RED, mListView.getCacheColorHint());
446         assertEquals(Color.RED, mListView.getSolidColor());
447 
448         mListView.setCacheColorHint(Color.LTGRAY);
449         assertEquals(Color.LTGRAY, mListView.getCacheColorHint());
450         assertEquals(Color.LTGRAY, mListView.getSolidColor());
451 
452         mListView.setCacheColorHint(Color.GRAY);
453         assertEquals(Color.GRAY, mListView.getCacheColorHint());
454         assertEquals(Color.GRAY, mListView.getSolidColor());
455     }
456 
testAccessTranscriptMode()457     public void testAccessTranscriptMode() {
458         mListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
459         assertEquals(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL, mListView.getTranscriptMode());
460 
461         mListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_DISABLED);
462         assertEquals(AbsListView.TRANSCRIPT_MODE_DISABLED, mListView.getTranscriptMode());
463 
464         mListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_NORMAL);
465         assertEquals(AbsListView.TRANSCRIPT_MODE_NORMAL, mListView.getTranscriptMode());
466     }
467 
testCheckLayoutParams()468     public void testCheckLayoutParams() {
469         MyListView listView = new MyListView(mActivity);
470 
471         AbsListView.LayoutParams param1 = new AbsListView.LayoutParams(10, 10);
472         assertTrue(listView.checkLayoutParams(param1));
473 
474         ViewGroup.LayoutParams param2 = new ViewGroup.LayoutParams(10, 10);
475         assertFalse(listView.checkLayoutParams(param2));
476     }
477 
testComputeVerticalScrollValues()478     public void testComputeVerticalScrollValues() {
479         MyListView listView = new MyListView(mActivity);
480         assertEquals(0, listView.computeVerticalScrollRange());
481         assertEquals(0, listView.computeVerticalScrollOffset());
482         assertEquals(0, listView.computeVerticalScrollExtent());
483 
484         listView.setAdapter(mAdapter_countries);
485         listView.setSmoothScrollbarEnabled(false);
486         assertEquals(mAdapter_countries.getCount(), listView.computeVerticalScrollRange());
487         assertEquals(0, listView.computeVerticalScrollOffset());
488         assertEquals(0, listView.computeVerticalScrollExtent());
489 
490         listView.setSmoothScrollbarEnabled(true);
491         assertEquals(0, listView.computeVerticalScrollOffset());
492         assertEquals(0, listView.computeVerticalScrollExtent());
493     }
494 
testGenerateLayoutParams()495     public void testGenerateLayoutParams() throws XmlPullParserException, IOException {
496         ViewGroup.LayoutParams res = mListView.generateLayoutParams(mAttributeSet);
497         assertNotNull(res);
498         assertTrue(res instanceof AbsListView.LayoutParams);
499 
500         MyListView listView = new MyListView(mActivity);
501         ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
502                                                               ViewGroup.LayoutParams.WRAP_CONTENT);
503 
504         res = listView.generateLayoutParams(p);
505         assertNotNull(res);
506         assertTrue(res instanceof AbsListView.LayoutParams);
507         assertEquals(ViewGroup.LayoutParams.MATCH_PARENT, res.width);
508         assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, res.height);
509     }
510 
testBeforeAndAfterTextChanged()511     public void testBeforeAndAfterTextChanged() {
512         // The java doc says these two methods do nothing
513         CharSequence str = "test";
514         SpannableStringBuilder sb = new SpannableStringBuilder();
515 
516         mListView.beforeTextChanged(str, 0, str.length(), str.length());
517         mListView.afterTextChanged(sb);
518 
519         // test callback
520         MyListView listView = new MyListView(mActivity);
521         TextView tv = new TextView(mActivity);
522 
523         assertFalse(listView.isBeforeTextChangedCalled());
524         assertFalse(listView.isOnTextChangedCalled());
525         assertFalse(listView.isAfterTextChangedCalled());
526 
527         tv.addTextChangedListener(listView);
528         assertFalse(listView.isBeforeTextChangedCalled());
529         assertFalse(listView.isOnTextChangedCalled());
530         assertFalse(listView.isAfterTextChangedCalled());
531 
532         tv.setText("abc");
533         assertTrue(listView.isBeforeTextChangedCalled());
534         assertTrue(listView.isOnTextChangedCalled());
535         assertTrue(listView.isAfterTextChangedCalled());
536     }
537 
testAddTouchables()538     public void testAddTouchables() throws Throwable {
539         ArrayList<View> views = new ArrayList<View>();
540         assertEquals(0, views.size());
541 
542         setAdapter();
543 
544         mListView.addTouchables(views);
545         assertEquals(mListView.getChildCount(), views.size());
546     }
547 
testInvalidateViews()548     public void testInvalidateViews() throws Throwable {
549         TextView tv1 = (TextView) mActivity.findViewById(R.id.headerview1);
550         TextView tv2 = (TextView) mActivity.findViewById(R.id.footerview1);
551 
552         setAdapter();
553 
554         mListView.setScrollIndicators(tv1, tv2);
555 
556         runTestOnUiThread(new Runnable() {
557             public void run() {
558                 mListView.invalidateViews();
559             }
560         });
561         mInstrumentation.waitForIdleSync();
562     }
563 
testGetContextMenuInfo()564     public void testGetContextMenuInfo() throws Throwable {
565         final MyListView listView = new MyListView(mActivity, mAttributeSet);
566 
567         runTestOnUiThread(new Runnable() {
568             public void run() {
569                 mActivity.setContentView(listView);
570                 listView.setAdapter(mAdapter_countries);
571                 listView.setSelection(2);
572             }
573         });
574         mInstrumentation.waitForIdleSync();
575 
576         final TextView v = (TextView) listView.getSelectedView();
577         assertNull(listView.getContextMenuInfo());
578 
579         final MockOnItemLongClickListener listener = new MockOnItemLongClickListener();
580         listView.setOnItemLongClickListener(listener);
581 
582         assertNull(listener.getParent());
583         assertNull(listener.getView());
584         assertEquals(0, listener.getPosition());
585         assertEquals(0, listener.getID());
586 
587         mInstrumentation.waitForIdleSync();
588         TouchUtils.longClickView(this, v);
589 
590         new PollingCheck() {
591             @Override
592             protected boolean check() {
593                 return v == listener.getView();
594             }
595         }.run();
596 
597         assertSame(listView, listener.getParent());
598         assertEquals(2, listener.getPosition());
599         assertEquals(listView.getItemIdAtPosition(2), listener.getID());
600 
601         ContextMenuInfo cmi = listView.getContextMenuInfo();
602         assertNotNull(cmi);
603     }
604 
testGetTopBottomFadingEdgeStrength()605     public void testGetTopBottomFadingEdgeStrength() {
606         MyListView listView = new MyListView(mActivity);
607 
608         assertEquals(0.0f, listView.getTopFadingEdgeStrength(), DELTA);
609         assertEquals(0.0f, listView.getBottomFadingEdgeStrength(), DELTA);
610     }
611 
testHandleDataChanged()612     public void testHandleDataChanged() {
613         MyListView listView = new MyListView(mActivity, mAttributeSet, 0);
614         listView.handleDataChanged();
615         // TODO: how to check?
616     }
617 
testSetFilterText()618     public void testSetFilterText() {
619         MyListView listView = new MyListView(mActivity, mAttributeSet, 0);
620         String filterText = "xyz";
621 
622         assertFalse(listView.isTextFilterEnabled());
623         assertFalse(listView.hasTextFilter());
624         assertFalse(listView.isInFilterMode());
625         assertTrue(mListView.checkInputConnectionProxy(null));
626 
627         listView.setTextFilterEnabled(false);
628         listView.setFilterText(filterText);
629         assertFalse(listView.isTextFilterEnabled());
630         assertFalse(listView.hasTextFilter());
631         assertFalse(listView.isInFilterMode());
632 
633         listView.setTextFilterEnabled(true);
634         listView.setFilterText(null);
635         assertTrue(listView.isTextFilterEnabled());
636         assertFalse(listView.hasTextFilter());
637         assertFalse(listView.isInFilterMode());
638 
639         listView.setTextFilterEnabled(true);
640         listView.setFilterText(filterText);
641         assertTrue(listView.isTextFilterEnabled());
642         assertTrue(listView.hasTextFilter());
643         assertTrue(listView.isInFilterMode());
644 
645         listView.clearTextFilter();
646         assertTrue(listView.isTextFilterEnabled());
647         assertFalse(listView.hasTextFilter());
648         assertFalse(listView.isInFilterMode());
649     }
650 
testLayoutChildren()651     public void testLayoutChildren() {
652         /**
653          * the subclass ListView and GridView override this method, so we can not test
654          * this method.
655          */
656     }
657 
testFoo()658     public void testFoo() {
659         /**
660          * Do not test these APIs. They are callbacks which:
661          *
662          * 1. The callback machanism has been tested in super class
663          * 2. The functionality is implmentation details, no need to test
664          */
665     }
666 
667     private static class MockOnScrollListener implements OnScrollListener {
668         private AbsListView mView;
669         private int mFirstVisibleItem;
670         private int mVisibleItemCount;
671         private int mTotalItemCount;
672         private int mScrollState;
673 
674         private boolean mIsOnScrollCalled;
675         private boolean mIsOnScrollStateChangedCalled;
676 
MockOnScrollListener()677         private MockOnScrollListener() {
678             mView = null;
679             mFirstVisibleItem = 0;
680             mVisibleItemCount = 0;
681             mTotalItemCount = 0;
682             mScrollState = -1;
683 
684             mIsOnScrollCalled = false;
685             mIsOnScrollStateChangedCalled = false;
686         }
687 
onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)688         public void onScroll(AbsListView view, int firstVisibleItem,
689                 int visibleItemCount, int totalItemCount) {
690             mView = view;
691             mFirstVisibleItem = firstVisibleItem;
692             mVisibleItemCount = visibleItemCount;
693             mTotalItemCount = totalItemCount;
694             mIsOnScrollCalled = true;
695         }
696 
onScrollStateChanged(AbsListView view, int scrollState)697         public void onScrollStateChanged(AbsListView view, int scrollState) {
698             mScrollState = scrollState;
699             mIsOnScrollStateChangedCalled = true;
700         }
701 
getView()702         public AbsListView getView() {
703             return mView;
704         }
705 
getFirstVisibleItem()706         public int getFirstVisibleItem() {
707             return mFirstVisibleItem;
708         }
709 
getVisibleItemCount()710         public int getVisibleItemCount() {
711             return mVisibleItemCount;
712         }
713 
getTotalItemCount()714         public int getTotalItemCount() {
715             return mTotalItemCount;
716         }
717 
getScrollState()718         public int getScrollState() {
719             return mScrollState;
720         }
721 
isOnScrollCalled()722         public boolean isOnScrollCalled() {
723             return mIsOnScrollCalled;
724         }
725 
isOnScrollStateChangedCalled()726         public boolean isOnScrollStateChangedCalled() {
727             return mIsOnScrollStateChangedCalled;
728         }
729 
reset()730         public void reset() {
731             mIsOnScrollCalled = false;
732             mIsOnScrollStateChangedCalled = false;
733         }
734     }
735 
736     private static class MockRecyclerListener implements RecyclerListener {
737         private View mView;
738 
MockRecyclerListener()739         private MockRecyclerListener() {
740             mView = null;
741         }
742 
onMovedToScrapHeap(View view)743         public void onMovedToScrapHeap(View view) {
744             mView = view;
745         }
746 
getView()747         public View getView() {
748             return mView;
749         }
750     }
751 
752     private static class MockOnItemLongClickListener implements OnItemLongClickListener {
753         private AdapterView<?> parent;
754         private View view;
755         private int position;
756         private long id;
757 
onItemLongClick(AdapterView<?> parent, View view, int position, long id)758         public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
759             this.parent = parent;
760             this.view = view;
761             this.position = position;
762             this.id = id;
763             return false;
764         }
765 
getParent()766         public AdapterView<?> getParent() {
767             return parent;
768         }
769 
getView()770         public View getView() {
771             return view;
772         }
773 
getPosition()774         public int getPosition() {
775             return position;
776         }
777 
getID()778         public long getID() {
779             return id;
780         }
781     }
782 
783     /**
784      * MyListView for test
785      */
786     private static class MyListView extends ListView {
MyListView(Context context)787         public MyListView(Context context) {
788             super(context);
789         }
790 
MyListView(Context context, AttributeSet attrs)791         public MyListView(Context context, AttributeSet attrs) {
792             super(context, attrs);
793         }
794 
MyListView(Context context, AttributeSet attrs, int defStyle)795         public MyListView(Context context, AttributeSet attrs, int defStyle) {
796             super(context, attrs, defStyle);
797         }
798 
799         @Override
checkLayoutParams(ViewGroup.LayoutParams p)800         protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
801             return super.checkLayoutParams(p);
802         }
803 
804         @Override
computeVerticalScrollExtent()805         protected int computeVerticalScrollExtent() {
806             return super.computeVerticalScrollExtent();
807         }
808 
809         @Override
computeVerticalScrollOffset()810         protected int computeVerticalScrollOffset() {
811             return super.computeVerticalScrollOffset();
812         }
813 
814         @Override
computeVerticalScrollRange()815         protected int computeVerticalScrollRange() {
816             return super.computeVerticalScrollRange();
817         }
818 
819         @Override
dispatchDraw(Canvas canvas)820         protected void dispatchDraw(Canvas canvas) {
821             super.dispatchDraw(canvas);
822         }
823 
824         @Override
dispatchSetPressed(boolean pressed)825         protected void dispatchSetPressed(boolean pressed) {
826             super.dispatchSetPressed(pressed);
827         }
828 
829         @Override
generateLayoutParams(ViewGroup.LayoutParams p)830         protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
831             return super.generateLayoutParams(p);
832         }
833 
834         @Override
getBottomFadingEdgeStrength()835         protected float getBottomFadingEdgeStrength() {
836             return super.getBottomFadingEdgeStrength();
837         }
838 
839         @Override
getContextMenuInfo()840         protected ContextMenuInfo getContextMenuInfo() {
841             return super.getContextMenuInfo();
842         }
843 
844         @Override
getTopFadingEdgeStrength()845         protected float getTopFadingEdgeStrength() {
846             return super.getTopFadingEdgeStrength();
847         }
848 
849         @Override
handleDataChanged()850         protected void handleDataChanged() {
851             super.handleDataChanged();
852         }
853 
854         @Override
isInFilterMode()855         protected boolean isInFilterMode() {
856             return super.isInFilterMode();
857         }
858 
859         private boolean mIsBeforeTextChangedCalled;
860         private boolean mIsOnTextChangedCalled;
861         private boolean mIsAfterTextChangedCalled;
862 
863         @Override
beforeTextChanged(CharSequence s, int start, int count, int after)864         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
865             mIsBeforeTextChangedCalled = true;
866             super.beforeTextChanged(s, start, count, after);
867         }
868 
869         @Override
onTextChanged(CharSequence s, int start, int before, int count)870         public void onTextChanged(CharSequence s, int start, int before, int count) {
871             mIsOnTextChangedCalled = true;
872             super.onTextChanged(s, start, before, count);
873         }
874 
875         @Override
afterTextChanged(Editable s)876         public void afterTextChanged(Editable s) {
877             mIsAfterTextChangedCalled = true;
878             super.afterTextChanged(s);
879         }
880 
isBeforeTextChangedCalled()881         public boolean isBeforeTextChangedCalled() {
882             return mIsBeforeTextChangedCalled;
883         }
884 
isOnTextChangedCalled()885         public boolean isOnTextChangedCalled() {
886             return mIsOnTextChangedCalled;
887         }
888 
isAfterTextChangedCalled()889         public boolean isAfterTextChangedCalled() {
890             return mIsAfterTextChangedCalled;
891         }
892     }
893 }
894