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