• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.android.internal.view.menu.ContextMenuBuilder;
21 
22 
23 import org.xmlpull.v1.XmlPullParser;
24 import org.xmlpull.v1.XmlPullParserException;
25 
26 import android.app.Activity;
27 import android.app.Instrumentation;
28 import android.content.Context;
29 import android.cts.util.WidgetTestUtils;
30 import android.os.SystemClock;
31 import android.test.ActivityInstrumentationTestCase2;
32 import android.test.UiThreadTest;
33 import android.test.ViewAsserts;
34 import android.util.AttributeSet;
35 import android.util.Xml;
36 import android.view.ContextMenu;
37 import android.view.Gravity;
38 import android.view.KeyEvent;
39 import android.view.MotionEvent;
40 import android.view.View;
41 import android.view.ViewGroup;
42 import android.view.ContextMenu.ContextMenuInfo;
43 import android.view.View.OnCreateContextMenuListener;
44 import android.view.ViewGroup.LayoutParams;
45 import android.view.animation.Transformation;
46 import android.widget.AdapterView;
47 import android.widget.BaseAdapter;
48 import android.widget.Gallery;
49 import android.widget.ImageView;
50 import android.widget.AdapterView.OnItemSelectedListener;
51 
52 import java.io.IOException;
53 
54 /**
55  * Test {@link Gallery}.
56  */
57 public class GalleryTest extends ActivityInstrumentationTestCase2<GalleryCtsActivity>  {
58     private Gallery mGallery;
59     private Activity mActivity;
60     private Instrumentation mInstrumentation;
61     private Context mContext;
62     private final static float DELTA = 0.01f;
63 
GalleryTest()64     public GalleryTest() {
65         super("com.android.cts.widget", GalleryCtsActivity.class);
66     }
67 
68     @Override
setUp()69     protected void setUp() throws Exception {
70         super.setUp();
71         mActivity = getActivity();
72         mInstrumentation = getInstrumentation();
73         mContext = mInstrumentation.getContext();
74         mGallery = (Gallery) mActivity.findViewById(R.id.gallery_test);
75     }
76 
77     @UiThreadTest
testConstructor()78     public void testConstructor() {
79         new Gallery(mContext);
80 
81         new Gallery(mContext, null);
82 
83         new Gallery(mContext, null, 0);
84 
85         XmlPullParser parser = getActivity().getResources().getXml(R.layout.gallery_test);
86         AttributeSet attrs = Xml.asAttributeSet(parser);
87         new Gallery(mContext, attrs);
88         new Gallery(mContext, attrs, 0);
89 
90         try {
91             new Gallery(null);
92             fail("should throw NullPointerException.");
93         } catch (NullPointerException e) {
94             // expected
95         }
96 
97         try {
98             new Gallery(null, null);
99             fail("should throw NullPointerException.");
100         } catch (NullPointerException e) {
101             // expected
102         }
103 
104         try {
105             new Gallery(null, null, 0);
106             fail("should throw NullPointerException.");
107         } catch (NullPointerException e) {
108             // expected
109         }
110     }
111 
testSetAnimationDuration()112     public void testSetAnimationDuration() {
113     }
114 
testSetSpacing()115     public void testSetSpacing() throws Throwable {
116         setSpacingAndCheck(0);
117 
118         setSpacingAndCheck(5);
119 
120         setSpacingAndCheck(-1);
121     }
122 
setSpacingAndCheck(final int spacing)123     private void setSpacingAndCheck(final int spacing) throws Throwable {
124         runTestOnUiThread(new Runnable() {
125             public void run() {
126                 mGallery.setSpacing(spacing);
127                 mGallery.requestLayout();
128             }
129         });
130         mInstrumentation.waitForIdleSync();
131 
132         View v0 = mGallery.getChildAt(0);
133         View v1 = mGallery.getChildAt(1);
134         assertEquals(v0.getRight() + spacing, v1.getLeft());
135     }
136 
testSetUnselectedAlpha()137     public void testSetUnselectedAlpha() {
138         final MyGallery gallery = (MyGallery) mActivity.findViewById(R.id.gallery_test);
139 
140         checkUnselectedAlpha(gallery, 0.0f);
141 
142         checkUnselectedAlpha(gallery, 0.5f);
143     }
144 
checkUnselectedAlpha(MyGallery gallery, float alpha)145     private void checkUnselectedAlpha(MyGallery gallery, float alpha) {
146         final float DEFAULT_ALPHA = 1.0f;
147         View v0 = gallery.getChildAt(0);
148         View v1 = gallery.getChildAt(1);
149 
150         gallery.setUnselectedAlpha(alpha);
151         Transformation t = new Transformation();
152         gallery.getChildStaticTransformation(v0, t);
153         // v0 is selected by default.
154         assertEquals(DEFAULT_ALPHA, t.getAlpha(), DELTA);
155         gallery.getChildStaticTransformation(v1, t);
156         assertEquals(alpha, t.getAlpha(), DELTA);
157     }
158 
159     @UiThreadTest
testGenerateLayoutParams()160     public void testGenerateLayoutParams() throws XmlPullParserException, IOException {
161         final int width = 320;
162         final int height = 240;
163         LayoutParams lp = new LayoutParams(width, height);
164         MyGallery gallery = new MyGallery(mContext);
165         LayoutParams layoutParams = gallery.generateLayoutParams(lp);
166         assertEquals(width, layoutParams.width);
167         assertEquals(height, layoutParams.height);
168 
169         XmlPullParser parser = getActivity().getResources().getXml(R.layout.gallery_test);
170         WidgetTestUtils.beginDocument(parser, "LinearLayout");
171         AttributeSet attrs = Xml.asAttributeSet(parser);
172         mGallery = new Gallery(mContext, attrs);
173 
174         layoutParams = mGallery.generateLayoutParams(attrs);
175         assertEquals(LayoutParams.MATCH_PARENT, layoutParams.width);
176         assertEquals(LayoutParams.MATCH_PARENT, layoutParams.height);
177     }
178 
testFoo()179     public void testFoo() {
180         // Do not test these APIs. They are callbacks which:
181         // 1. The callback machanism has been tested in super class
182         // 2. The functionality is implmentation details, no need to test
183     }
184 
testShowContextMenuForChild()185     public void testShowContextMenuForChild() {
186         // how to check whether the context menu for child is showing.
187     }
188 
testShowContextMenu()189     public void testShowContextMenu() {
190         // how to check whether the context menu is showing.
191     }
192 
193     @UiThreadTest
testDispatchKeyEvent()194     public void testDispatchKeyEvent() {
195         mGallery = new Gallery(mContext);
196         final KeyEvent validKeyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER);
197         assertTrue(mGallery.dispatchKeyEvent(validKeyEvent));
198         final long time = SystemClock.uptimeMillis();
199         final KeyEvent invalidKeyEvent
200                 = new KeyEvent(time, time, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_A, 5);
201         assertFalse(mGallery.dispatchKeyEvent(invalidKeyEvent));
202     }
203 
testSetGravity()204     public void testSetGravity() throws Throwable {
205         setGalleryGravity(Gravity.CENTER_HORIZONTAL);
206         View v0 = mGallery.getChildAt(0);
207         ViewAsserts.assertHorizontalCenterAligned(mGallery, v0);
208 
209         setGalleryGravity(Gravity.TOP);
210         v0 = mGallery.getChildAt(0);
211         ViewAsserts.assertTopAligned(mGallery, v0, mGallery.getPaddingTop());
212 
213         setGalleryGravity(Gravity.BOTTOM);
214         v0 = mGallery.getChildAt(0);
215         ViewAsserts.assertBottomAligned(mGallery, v0, mGallery.getPaddingBottom());
216     }
217 
setGalleryGravity(final int gravity)218     private void setGalleryGravity(final int gravity) throws Throwable {
219         runTestOnUiThread(new Runnable() {
220             public void run() {
221                 mGallery.setGravity(gravity);
222                 mGallery.invalidate();
223                 mGallery.requestLayout();
224             }
225         });
226         mInstrumentation.waitForIdleSync();
227     }
228 
229     @UiThreadTest
testCheckLayoutParams()230     public void testCheckLayoutParams() {
231         MyGallery gallery = new MyGallery(mContext);
232         ViewGroup.LayoutParams p1 = new ViewGroup.LayoutParams(320, 480);
233         assertFalse(gallery.checkLayoutParams(p1));
234 
235         Gallery.LayoutParams p2 = new Gallery.LayoutParams(320, 480);
236         assertTrue(gallery.checkLayoutParams(p2));
237     }
238 
239     @UiThreadTest
testComputeHorizontalScrollExtent()240     public void testComputeHorizontalScrollExtent() {
241         MyGallery gallery = new MyGallery(mContext);
242 
243         // only one item is considered to be selected.
244         assertEquals(1, gallery.computeHorizontalScrollExtent());
245     }
246 
247     @UiThreadTest
testComputeHorizontalScrollOffset()248     public void testComputeHorizontalScrollOffset() {
249         MyGallery gallery = new MyGallery(mContext);
250         assertEquals(AdapterView.INVALID_POSITION, gallery.computeHorizontalScrollOffset());
251         gallery.setAdapter(new ImageAdapter(mActivity));
252 
253         // Current scroll position is the same as the selected position
254         assertEquals(gallery.getSelectedItemPosition(), gallery.computeHorizontalScrollOffset());
255     }
256 
257     @UiThreadTest
testComputeHorizontalScrollRange()258     public void testComputeHorizontalScrollRange() {
259         MyGallery gallery = new MyGallery(mContext);
260         ImageAdapter adapter = new ImageAdapter(mActivity);
261         gallery.setAdapter(adapter);
262 
263         // Scroll range is the same as the item count
264         assertEquals(adapter.getCount(), gallery.computeHorizontalScrollRange());
265     }
266 
267     @UiThreadTest
testDispatchSetPressed()268     public void testDispatchSetPressed() {
269         final MyGallery gallery = (MyGallery) getActivity().findViewById(R.id.gallery_test);
270 
271         gallery.setSelection(0);
272         gallery.dispatchSetPressed(true);
273         assertTrue(gallery.getSelectedView().isPressed());
274         assertFalse(gallery.getChildAt(1).isPressed());
275 
276         gallery.dispatchSetPressed(false);
277         assertFalse(gallery.getSelectedView().isPressed());
278         assertFalse(gallery.getChildAt(1).isPressed());
279     }
280 
281     @UiThreadTest
testGenerateDefaultLayoutParams()282     public void testGenerateDefaultLayoutParams() {
283         MyGallery gallery = new MyGallery(mContext);
284         ViewGroup.LayoutParams p = gallery.generateDefaultLayoutParams();
285         assertNotNull(p);
286         assertTrue(p instanceof Gallery.LayoutParams);
287         assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, p.width);
288         assertEquals(ViewGroup.LayoutParams.WRAP_CONTENT, p.height);
289     }
290 
testGetChildDrawingOrder()291     public void testGetChildDrawingOrder() {
292         final MyGallery gallery = (MyGallery) getActivity().findViewById(R.id.gallery_test);
293 
294         int childCount = 3;
295         int index = 2;
296         assertEquals(gallery.getSelectedItemPosition(),
297                 gallery.getChildDrawingOrder(childCount, index));
298 
299         childCount = 5;
300         index = 2;
301         assertEquals(index + 1, gallery.getChildDrawingOrder(childCount, index));
302 
303         childCount = 5;
304         index = 3;
305         assertEquals(index + 1, gallery.getChildDrawingOrder(childCount, index));
306     }
307 
308     @UiThreadTest
testGetContextMenuInfo()309     public void testGetContextMenuInfo() {
310         MockOnCreateContextMenuListener listener = new MockOnCreateContextMenuListener();
311         MyGallery gallery = new MyGallery(mContext);
312         gallery.setOnCreateContextMenuListener(listener);
313         assertFalse(listener.hasCreatedContextMenu());
314         gallery.createContextMenu(new ContextMenuBuilder(mContext));
315         assertTrue(listener.hasCreatedContextMenu());
316         assertSame(gallery.getContextMenuInfo(), listener.getContextMenuInfo());
317     }
318 
319     private static class MockOnCreateContextMenuListener implements OnCreateContextMenuListener {
320         private boolean hasCreatedContextMenu;
321         private ContextMenuInfo mContextMenuInfo;
322 
hasCreatedContextMenu()323         public boolean hasCreatedContextMenu() {
324             return hasCreatedContextMenu;
325         }
326 
getContextMenuInfo()327         public ContextMenuInfo getContextMenuInfo() {
328             return mContextMenuInfo;
329         }
330 
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)331         public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
332             hasCreatedContextMenu = true;
333             mContextMenuInfo = menuInfo;
334         }
335     }
336 
337     private static class ImageAdapter extends BaseAdapter {
ImageAdapter(Context c)338         public ImageAdapter(Context c) {
339             mContext = c;
340         }
341 
getCount()342         public int getCount() {
343             return mImageIds.length;
344         }
345 
getItem(int position)346         public Object getItem(int position) {
347             return position;
348         }
349 
getItemId(int position)350         public long getItemId(int position) {
351             return position;
352         }
353 
getView(int position, View convertView, ViewGroup parent)354         public View getView(int position, View convertView, ViewGroup parent) {
355             ImageView i = new ImageView(mContext);
356 
357             i.setImageResource(mImageIds[position]);
358             i.setScaleType(ImageView.ScaleType.FIT_XY);
359             i.setLayoutParams(new Gallery.LayoutParams(136, 88));
360 
361             return i;
362         }
363 
364         private Context mContext;
365 
366         private Integer[] mImageIds = {
367                 R.drawable.faces,
368                 R.drawable.scenery,
369                 R.drawable.testimage,
370                 R.drawable.faces,
371                 R.drawable.scenery,
372                 R.drawable.testimage,
373                 R.drawable.faces,
374                 R.drawable.scenery,
375                 R.drawable.testimage,
376         };
377     }
378 
379     private static class MockOnItemSelectedListener implements OnItemSelectedListener {
380         private boolean mIsItemSelected;
381         private boolean mNothingSelected;
382         private int mItemSelectedCalledCount;
383 
isItemSelected()384         public boolean isItemSelected() {
385             return mIsItemSelected;
386         }
387 
hasNothingSelected()388         public boolean hasNothingSelected() {
389             return mNothingSelected;
390         }
391 
getItemSelectedCalledCount()392         public int getItemSelectedCalledCount() {
393             return mItemSelectedCalledCount;
394         }
395 
reset()396         public void reset() {
397             mIsItemSelected = false;
398             mNothingSelected = true;
399             mItemSelectedCalledCount = 0;
400         }
401 
onItemSelected(AdapterView<?> parent, View view, int position, long id)402         public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
403             mIsItemSelected = true;
404             mItemSelectedCalledCount++;
405         }
406 
onNothingSelected(AdapterView<?> parent)407         public void onNothingSelected(AdapterView<?> parent) {
408             mNothingSelected = true;
409         }
410     }
411 }
412