PopupZoomerTest.java revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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
5package org.chromium.content.browser;
6
7import android.content.Context;
8import android.graphics.Bitmap;
9import android.graphics.Canvas;
10import android.graphics.Rect;
11import android.os.SystemClock;
12import android.test.InstrumentationTestCase;
13import android.test.suitebuilder.annotation.SmallTest;
14import android.view.MotionEvent;
15import android.view.View;
16
17import org.chromium.base.test.util.Feature;
18
19/**
20 * Tests for PopupZoomer.
21 */
22public class PopupZoomerTest extends InstrumentationTestCase {
23    private CustomCanvasPopupZoomer mPopupZoomer;
24
25    private static class CustomCanvasPopupZoomer extends PopupZoomer {
26        Canvas mCanvas;
27        long mPendingDraws = 0;
28
29        CustomCanvasPopupZoomer(Context context, Canvas c) {
30            super(context);
31            mCanvas = c;
32        }
33
34        @Override
35        public void invalidate() {
36            mPendingDraws++;
37        }
38
39        @Override
40        public void onDraw(Canvas c) {
41            mPendingDraws--;
42            super.onDraw(c);
43        }
44
45        // Test doesn't attach PopupZoomer to the view hierarchy,
46        // but onDraw() should still go on.
47        @Override
48        protected boolean acceptZeroSizeView() {
49            return true;
50        }
51
52        public void finishPendingDraws() {
53            // Finish all pending draw calls. A draw call may change mPendingDraws.
54            while (mPendingDraws > 0) {
55                onDraw(mCanvas);
56            }
57        }
58
59    }
60
61    private CustomCanvasPopupZoomer createPopupZoomerForTest(Context context) {
62        return new CustomCanvasPopupZoomer(
63                context, new Canvas(Bitmap.createBitmap(100, 100, Bitmap.Config.ALPHA_8)));
64    }
65
66    private void sendSingleTapTouchEventOnView(View view, float x, float y) {
67        final long downEvent = SystemClock.uptimeMillis();
68        view.onTouchEvent(
69                MotionEvent.obtain(downEvent, downEvent, MotionEvent.ACTION_DOWN, x, y, 0));
70        view.onTouchEvent(
71                MotionEvent.obtain(downEvent, downEvent + 10, MotionEvent.ACTION_UP, x, y, 0));
72    }
73
74    @Override
75    public void setUp() {
76        mPopupZoomer = createPopupZoomerForTest(getInstrumentation().getTargetContext());
77    }
78
79    @SmallTest
80    @Feature({"Navigation"})
81    public void testDefaultCreateState() throws Exception {
82        assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility());
83        assertFalse(mPopupZoomer.isShowing());
84    }
85
86    @SmallTest
87    @Feature({"Navigation"})
88    public void testShowWithoutBitmap() throws Exception {
89        mPopupZoomer.show(new Rect(0, 0, 5, 5));
90
91        // The view should be invisible.
92        assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility());
93        assertFalse(mPopupZoomer.isShowing());
94    }
95
96    @SmallTest
97    @Feature({"Navigation"})
98    public void testShowWithBitmap() throws Exception {
99        mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8));
100        mPopupZoomer.show(new Rect(0, 0, 5, 5));
101
102        // The view should become visible.
103        assertEquals(View.VISIBLE, mPopupZoomer.getVisibility());
104        assertTrue(mPopupZoomer.isShowing());
105    }
106
107    @SmallTest
108    @Feature({"Navigation"})
109    public void testHide() throws Exception {
110        mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8));
111        mPopupZoomer.show(new Rect(0, 0, 5, 5));
112
113        // The view should become visible.
114        assertEquals(View.VISIBLE, mPopupZoomer.getVisibility());
115        assertTrue(mPopupZoomer.isShowing());
116
117        // Call hide without animation.
118        mPopupZoomer.hide(false);
119
120        // The view should be invisible.
121        assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility());
122        assertFalse(mPopupZoomer.isShowing());
123    }
124
125    @SmallTest
126    @Feature({"Navigation"})
127    public void testOnTouchEventOutsidePopup() throws Exception {
128        mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8));
129        mPopupZoomer.show(new Rect(0, 0, 5, 5));
130
131        // Wait for the show animation to finish.
132        mPopupZoomer.finishPendingDraws();
133
134        // The view should be visible.
135        assertEquals(View.VISIBLE, mPopupZoomer.getVisibility());
136        assertTrue(mPopupZoomer.isShowing());
137
138        // Send tap event at a point outside the popup.
139        // i.e. coordinates greater than 10 + PopupZoomer.ZOOM_BOUNDS_MARGIN
140        sendSingleTapTouchEventOnView(mPopupZoomer, 50, 50);
141
142        // Wait for the hide animation to finish.
143        mPopupZoomer.finishPendingDraws();
144
145        // The view should be invisible.
146        assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility());
147        assertFalse(mPopupZoomer.isShowing());
148    }
149
150    @SmallTest
151    @Feature({"Navigation"})
152    public void testOnTouchEventInsidePopupNoOnTapListener() throws Exception {
153        mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8));
154        mPopupZoomer.show(new Rect(0, 0, 5, 5));
155
156        // Wait for the animation to finish.
157        mPopupZoomer.finishPendingDraws();
158
159        // The view should be visible.
160        assertEquals(View.VISIBLE, mPopupZoomer.getVisibility());
161        assertTrue(mPopupZoomer.isShowing());
162
163        // Send tap event at a point inside the popup.
164        // i.e. coordinates between PopupZoomer.ZOOM_BOUNDS_MARGIN and
165        // PopupZoomer.ZOOM_BOUNDS_MARGIN + 10
166        sendSingleTapTouchEventOnView(mPopupZoomer, 30, 30);
167
168        // Wait for the animation to finish (if there is any).
169        mPopupZoomer.finishPendingDraws();
170
171        // The view should still be visible as no OnTapListener is set.
172        assertEquals(View.VISIBLE, mPopupZoomer.getVisibility());
173        assertTrue(mPopupZoomer.isShowing());
174    }
175}
176