OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.graphics.Bitmap; | 8 import android.graphics.Bitmap; |
9 import android.graphics.Canvas; | 9 import android.graphics.Canvas; |
10 import android.graphics.Rect; | 10 import android.graphics.Rect; |
11 import android.os.SystemClock; | 11 import android.os.SystemClock; |
| 12 import android.support.test.InstrumentationRegistry; |
12 import android.support.test.filters.SmallTest; | 13 import android.support.test.filters.SmallTest; |
13 import android.view.MotionEvent; | 14 import android.view.MotionEvent; |
14 import android.view.View; | 15 import android.view.View; |
15 | 16 |
| 17 import org.junit.Assert; |
| 18 import org.junit.Before; |
| 19 import org.junit.Rule; |
| 20 import org.junit.Test; |
| 21 import org.junit.runner.RunWith; |
| 22 |
| 23 import org.chromium.base.test.BaseJUnit4ClassRunner; |
16 import org.chromium.base.test.util.Feature; | 24 import org.chromium.base.test.util.Feature; |
17 import org.chromium.content.browser.input.ImeAdapter; | 25 import org.chromium.content.browser.input.ImeAdapter; |
18 import org.chromium.content.browser.input.TestImeAdapterDelegate; | 26 import org.chromium.content.browser.input.TestImeAdapterDelegate; |
19 import org.chromium.content.browser.test.util.TestInputMethodManagerWrapper; | 27 import org.chromium.content.browser.test.util.TestInputMethodManagerWrapper; |
20 import org.chromium.content_shell_apk.ContentShellTestBase; | 28 import org.chromium.content_shell_apk.ContentShellActivityTestRule; |
21 | 29 |
22 /** | 30 /** |
23 * Tests for PopupZoomer. | 31 * Tests for PopupZoomer. |
24 */ | 32 */ |
25 public class PopupZoomerTest extends ContentShellTestBase { | 33 @RunWith(BaseJUnit4ClassRunner.class) |
| 34 public class PopupZoomerTest { |
| 35 @Rule |
| 36 public ContentShellActivityTestRule mActivityTestRule = new ContentShellActi
vityTestRule(); |
| 37 |
26 private CustomCanvasPopupZoomer mPopupZoomer; | 38 private CustomCanvasPopupZoomer mPopupZoomer; |
27 private ContentViewCore mContentViewCore; | 39 private ContentViewCore mContentViewCore; |
28 | 40 |
29 private static class CustomCanvasPopupZoomer extends PopupZoomer { | 41 private static class CustomCanvasPopupZoomer extends PopupZoomer { |
30 Canvas mCanvas; | 42 Canvas mCanvas; |
31 long mPendingDraws = 0; | 43 long mPendingDraws = 0; |
32 | 44 |
33 CustomCanvasPopupZoomer(Context context, Canvas c) { | 45 CustomCanvasPopupZoomer(Context context, Canvas c) { |
34 super(context); | 46 super(context); |
35 mCanvas = c; | 47 mCanvas = c; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 } | 80 } |
69 | 81 |
70 private void sendSingleTapTouchEventOnView(View view, float x, float y) { | 82 private void sendSingleTapTouchEventOnView(View view, float x, float y) { |
71 final long downEvent = SystemClock.uptimeMillis(); | 83 final long downEvent = SystemClock.uptimeMillis(); |
72 view.onTouchEvent( | 84 view.onTouchEvent( |
73 MotionEvent.obtain(downEvent, downEvent, MotionEvent.ACTION_DOWN
, x, y, 0)); | 85 MotionEvent.obtain(downEvent, downEvent, MotionEvent.ACTION_DOWN
, x, y, 0)); |
74 view.onTouchEvent( | 86 view.onTouchEvent( |
75 MotionEvent.obtain(downEvent, downEvent + 10, MotionEvent.ACTION
_UP, x, y, 0)); | 87 MotionEvent.obtain(downEvent, downEvent + 10, MotionEvent.ACTION
_UP, x, y, 0)); |
76 } | 88 } |
77 | 89 |
78 @Override | 90 @Before |
79 public void setUp() throws Exception { | 91 public void setUp() throws Throwable { |
80 super.setUp(); | 92 mActivityTestRule.launchActivity(null); |
81 mPopupZoomer = createPopupZoomerForTest(getInstrumentation().getTargetCo
ntext()); | 93 mActivityTestRule.runOnUiThread(new Runnable() { |
82 mContentViewCore = new ContentViewCore(getActivity(), ""); | 94 @Override |
83 ImeAdapter imeAdapter = new ImeAdapter(new TestInputMethodManagerWrapper
(mContentViewCore), | 95 public void run() { |
84 new TestImeAdapterDelegate(getContentViewCore().getContainerView
())); | 96 mPopupZoomer = createPopupZoomerForTest( |
85 mContentViewCore.setSelectionPopupControllerForTesting( | 97 InstrumentationRegistry.getInstrumentation().getTargetCo
ntext()); |
86 new SelectionPopupController(getActivity(), null, null, null, | 98 mContentViewCore = new ContentViewCore(mActivityTestRule.getActi
vity(), ""); |
87 mContentViewCore.getRenderCoordinates(), imeAdapter)); | 99 ImeAdapter imeAdapter = |
88 mContentViewCore.setPopupZoomerForTest(mPopupZoomer); | 100 new ImeAdapter(new TestInputMethodManagerWrapper(mConten
tViewCore), |
89 mContentViewCore.setImeAdapterForTest(imeAdapter); | 101 new TestImeAdapterDelegate( |
| 102 mActivityTestRule.getContentViewCore().g
etContainerView())); |
| 103 mContentViewCore.setSelectionPopupControllerForTesting( |
| 104 new SelectionPopupController(mActivityTestRule.getActivi
ty(), null, null, |
| 105 null, mContentViewCore.getRenderCoordinates(), i
meAdapter)); |
| 106 mContentViewCore.setPopupZoomerForTest(mPopupZoomer); |
| 107 mContentViewCore.setImeAdapterForTest(imeAdapter); |
| 108 } |
| 109 }); |
90 } | 110 } |
91 | 111 |
| 112 @Test |
92 @SmallTest | 113 @SmallTest |
93 @Feature({"Navigation"}) | 114 @Feature({"Navigation"}) |
94 public void testDefaultCreateState() throws Exception { | 115 public void testDefaultCreateState() throws Exception { |
95 assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility()); | 116 Assert.assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility()); |
96 assertFalse(mPopupZoomer.isShowing()); | 117 Assert.assertFalse(mPopupZoomer.isShowing()); |
97 } | 118 } |
98 | 119 |
| 120 @Test |
99 @SmallTest | 121 @SmallTest |
100 @Feature({"Navigation"}) | 122 @Feature({"Navigation"}) |
101 public void testShowWithoutBitmap() throws Exception { | 123 public void testShowWithoutBitmap() throws Exception { |
102 mPopupZoomer.show(new Rect(0, 0, 5, 5)); | 124 mPopupZoomer.show(new Rect(0, 0, 5, 5)); |
103 | 125 |
104 // The view should be invisible. | 126 // The view should be invisible. |
105 assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility()); | 127 Assert.assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility()); |
106 assertFalse(mPopupZoomer.isShowing()); | 128 Assert.assertFalse(mPopupZoomer.isShowing()); |
107 } | 129 } |
108 | 130 |
| 131 @Test |
109 @SmallTest | 132 @SmallTest |
110 @Feature({"Navigation"}) | 133 @Feature({"Navigation"}) |
111 public void testShowWithBitmap() throws Exception { | 134 public void testShowWithBitmap() throws Exception { |
112 mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8
)); | 135 mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8
)); |
113 mPopupZoomer.show(new Rect(0, 0, 5, 5)); | 136 mPopupZoomer.show(new Rect(0, 0, 5, 5)); |
114 | 137 |
115 // The view should become visible. | 138 // The view should become visible. |
116 assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); | 139 Assert.assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); |
117 assertTrue(mPopupZoomer.isShowing()); | 140 Assert.assertTrue(mPopupZoomer.isShowing()); |
118 } | 141 } |
119 | 142 |
| 143 @Test |
120 @SmallTest | 144 @SmallTest |
121 @Feature({"Navigation"}) | 145 @Feature({"Navigation"}) |
122 public void testHide() throws Exception { | 146 public void testHide() throws Exception { |
123 mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8
)); | 147 mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8
)); |
124 mPopupZoomer.show(new Rect(0, 0, 5, 5)); | 148 mPopupZoomer.show(new Rect(0, 0, 5, 5)); |
125 | 149 |
126 // The view should become visible. | 150 // The view should become visible. |
127 assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); | 151 Assert.assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); |
128 assertTrue(mPopupZoomer.isShowing()); | 152 Assert.assertTrue(mPopupZoomer.isShowing()); |
129 | 153 |
130 // Call hide without animation. | 154 // Call hide without animation. |
131 mPopupZoomer.hide(false); | 155 mPopupZoomer.hide(false); |
132 | 156 |
133 // The view should be invisible. | 157 // The view should be invisible. |
134 assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility()); | 158 Assert.assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility()); |
135 assertFalse(mPopupZoomer.isShowing()); | 159 Assert.assertFalse(mPopupZoomer.isShowing()); |
136 } | 160 } |
137 | 161 |
| 162 @Test |
138 @SmallTest | 163 @SmallTest |
139 @Feature({"Navigation"}) | 164 @Feature({"Navigation"}) |
140 public void testOnTouchEventOutsidePopup() throws Exception { | 165 public void testOnTouchEventOutsidePopup() throws Exception { |
141 mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8
)); | 166 mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8
)); |
142 mPopupZoomer.show(new Rect(0, 0, 5, 5)); | 167 mPopupZoomer.show(new Rect(0, 0, 5, 5)); |
143 | 168 |
144 // Wait for the show animation to finish. | 169 // Wait for the show animation to finish. |
145 mPopupZoomer.finishPendingDraws(); | 170 mPopupZoomer.finishPendingDraws(); |
146 | 171 |
147 // The view should be visible. | 172 // The view should be visible. |
148 assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); | 173 Assert.assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); |
149 assertTrue(mPopupZoomer.isShowing()); | 174 Assert.assertTrue(mPopupZoomer.isShowing()); |
150 | 175 |
151 // Send tap event at a point outside the popup. | 176 // Send tap event at a point outside the popup. |
152 // i.e. coordinates greater than 10 + PopupZoomer.ZOOM_BOUNDS_MARGIN | 177 // i.e. coordinates greater than 10 + PopupZoomer.ZOOM_BOUNDS_MARGIN |
153 sendSingleTapTouchEventOnView(mPopupZoomer, 50, 50); | 178 sendSingleTapTouchEventOnView(mPopupZoomer, 50, 50); |
154 | 179 |
155 // Wait for the hide animation to finish. | 180 // Wait for the hide animation to finish. |
156 mPopupZoomer.finishPendingDraws(); | 181 mPopupZoomer.finishPendingDraws(); |
157 | 182 |
158 // The view should be invisible. | 183 // The view should be invisible. |
159 assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility()); | 184 Assert.assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility()); |
160 assertFalse(mPopupZoomer.isShowing()); | 185 Assert.assertFalse(mPopupZoomer.isShowing()); |
161 } | 186 } |
162 | 187 |
| 188 @Test |
163 @SmallTest | 189 @SmallTest |
164 @Feature({"Navigation"}) | 190 @Feature({"Navigation"}) |
165 public void testOnTouchEventInsidePopupNoOnTapListener() throws Exception { | 191 public void testOnTouchEventInsidePopupNoOnTapListener() throws Exception { |
166 mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8
)); | 192 mPopupZoomer.setBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8
)); |
167 mPopupZoomer.show(new Rect(0, 0, 5, 5)); | 193 mPopupZoomer.show(new Rect(0, 0, 5, 5)); |
168 | 194 |
169 // Wait for the animation to finish. | 195 // Wait for the animation to finish. |
170 mPopupZoomer.finishPendingDraws(); | 196 mPopupZoomer.finishPendingDraws(); |
171 | 197 |
172 // The view should be visible. | 198 // The view should be visible. |
173 assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); | 199 Assert.assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); |
174 assertTrue(mPopupZoomer.isShowing()); | 200 Assert.assertTrue(mPopupZoomer.isShowing()); |
175 | 201 |
176 // Send tap event at a point inside the popup. | 202 // Send tap event at a point inside the popup. |
177 // i.e. coordinates between PopupZoomer.ZOOM_BOUNDS_MARGIN and | 203 // i.e. coordinates between PopupZoomer.ZOOM_BOUNDS_MARGIN and |
178 // PopupZoomer.ZOOM_BOUNDS_MARGIN + 10 | 204 // PopupZoomer.ZOOM_BOUNDS_MARGIN + 10 |
179 sendSingleTapTouchEventOnView(mPopupZoomer, 30, 30); | 205 sendSingleTapTouchEventOnView(mPopupZoomer, 30, 30); |
180 | 206 |
181 // Wait for the animation to finish (if there is any). | 207 // Wait for the animation to finish (if there is any). |
182 mPopupZoomer.finishPendingDraws(); | 208 mPopupZoomer.finishPendingDraws(); |
183 | 209 |
184 // The view should still be visible as no OnTapListener is set. | 210 // The view should still be visible as no OnTapListener is set. |
185 assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); | 211 Assert.assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); |
186 assertTrue(mPopupZoomer.isShowing()); | 212 Assert.assertTrue(mPopupZoomer.isShowing()); |
187 } | 213 } |
188 | 214 |
| 215 @Test |
189 @SmallTest | 216 @SmallTest |
190 @Feature({"Navigation"}) | 217 @Feature({"Navigation"}) |
191 public void testHidePopupOnLosingFocus() throws Exception { | 218 public void testHidePopupOnLosingFocus() throws Exception { |
192 mPopupZoomer.setBitmap( | 219 mPopupZoomer.setBitmap( |
193 Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8)); | 220 Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8)); |
194 mPopupZoomer.show(new Rect(0, 0, 5, 5)); | 221 mPopupZoomer.show(new Rect(0, 0, 5, 5)); |
195 | 222 |
196 // Wait for the animation to finish. | 223 // Wait for the animation to finish. |
197 mPopupZoomer.finishPendingDraws(); | 224 mPopupZoomer.finishPendingDraws(); |
198 | 225 |
199 // The view should be visible. | 226 // The view should be visible. |
200 assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); | 227 Assert.assertEquals(View.VISIBLE, mPopupZoomer.getVisibility()); |
201 assertTrue(mPopupZoomer.isShowing()); | 228 Assert.assertTrue(mPopupZoomer.isShowing()); |
202 | 229 |
203 // Simulate losing the focus. | 230 // Simulate losing the focus. |
204 mContentViewCore.onFocusChanged(false, true); | 231 mContentViewCore.onFocusChanged(false, true); |
205 | 232 |
206 // Wait for the hide animation to finish. | 233 // Wait for the hide animation to finish. |
207 mPopupZoomer.finishPendingDraws(); | 234 mPopupZoomer.finishPendingDraws(); |
208 | 235 |
209 // Now that another view has been focused, the view should be invisible. | 236 // Now that another view has been focused, the view should be invisible. |
210 assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility()); | 237 Assert.assertEquals(View.INVISIBLE, mPopupZoomer.getVisibility()); |
211 assertFalse(mPopupZoomer.isShowing()); | 238 Assert.assertFalse(mPopupZoomer.isShowing()); |
212 } | 239 } |
213 } | 240 } |
OLD | NEW |