|
13 | 13 | import android.content.Context; |
14 | 14 | import android.graphics.Canvas; |
15 | 15 | import android.graphics.Color; |
| 16 | +import android.graphics.Matrix; |
| 17 | +import android.view.Gravity; |
16 | 18 | import android.view.MotionEvent; |
17 | 19 | import android.view.Surface; |
18 | 20 | import android.view.View; |
|
25 | 27 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
26 | 28 | import io.flutter.embedding.android.AndroidTouchProcessor; |
27 | 29 | import io.flutter.embedding.engine.renderer.FlutterRenderer; |
| 30 | +import java.util.List; |
28 | 31 | import org.junit.Test; |
29 | 32 | import org.junit.runner.RunWith; |
30 | 33 | import org.mockito.ArgumentCaptor; |
@@ -300,6 +303,123 @@ void requestUnbuffered(MotionEvent event) { |
300 | 303 | assertFalse(view.getFlutterWonGesture()); |
301 | 304 | } |
302 | 305 |
|
| 306 | + /** Adds {@code wrapper} to a laid out parent at the given position. */ |
| 307 | + private FrameLayout layOutInParent(PlatformViewWrapper wrapper, int left, int top) { |
| 308 | + final FrameLayout.LayoutParams params = |
| 309 | + new FrameLayout.LayoutParams(10, 10, Gravity.LEFT | Gravity.TOP); |
| 310 | + params.leftMargin = left; |
| 311 | + params.topMargin = top; |
| 312 | + wrapper.setLayoutParams(params); |
| 313 | + final FrameLayout parent = new FrameLayout(ctx); |
| 314 | + parent.addView(wrapper); |
| 315 | + parent.measure( |
| 316 | + View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY), |
| 317 | + View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY)); |
| 318 | + parent.layout(0, 0, 100, 100); |
| 319 | + return parent; |
| 320 | + } |
| 321 | + |
| 322 | + @Test |
| 323 | + public void setOffset_movesViewWithoutRequestingLayout() { |
| 324 | + final int[] scrollChangedCalls = new int[1]; |
| 325 | + final PlatformViewWrapper wrapper = |
| 326 | + new PlatformViewWrapper(ctx) { |
| 327 | + @Override |
| 328 | + protected void onScrollChanged(int l, int t, int oldl, int oldt) { |
| 329 | + super.onScrollChanged(l, t, oldl, oldt); |
| 330 | + scrollChangedCalls[0]++; |
| 331 | + } |
| 332 | + }; |
| 333 | + final FrameLayout parent = layOutInParent(wrapper, 1, 2); |
| 334 | + assertEquals(1, wrapper.getLeft()); |
| 335 | + assertEquals(2, wrapper.getTop()); |
| 336 | + assertFalse(wrapper.isLayoutRequested()); |
| 337 | + |
| 338 | + wrapper.setOffset(30, 40); |
| 339 | + |
| 340 | + // The view is moved immediately, without a layout pass, and scroll listeners are notified. |
| 341 | + assertFalse(wrapper.isLayoutRequested()); |
| 342 | + assertFalse(parent.isLayoutRequested()); |
| 343 | + assertEquals(30, wrapper.getLeft()); |
| 344 | + assertEquals(40, wrapper.getTop()); |
| 345 | + assertEquals(40, wrapper.getRight()); |
| 346 | + assertEquals(50, wrapper.getBottom()); |
| 347 | + assertEquals(1, scrollChangedCalls[0]); |
| 348 | + |
| 349 | + // Setting the same offset again is a no-op. |
| 350 | + wrapper.setOffset(30, 40); |
| 351 | + assertEquals(1, scrollChangedCalls[0]); |
| 352 | + |
| 353 | + // The layout params are kept in sync, so a later layout pass keeps the position. |
| 354 | + final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) wrapper.getLayoutParams(); |
| 355 | + assertEquals(30, params.leftMargin); |
| 356 | + assertEquals(40, params.topMargin); |
| 357 | + parent.requestLayout(); |
| 358 | + parent.measure( |
| 359 | + View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY), |
| 360 | + View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY)); |
| 361 | + parent.layout(0, 0, 100, 100); |
| 362 | + assertEquals(30, wrapper.getLeft()); |
| 363 | + assertEquals(40, wrapper.getTop()); |
| 364 | + } |
| 365 | + |
| 366 | + @Test |
| 367 | + public void setOffset_beforeLayout_onlyUpdatesLayoutParams() { |
| 368 | + final PlatformViewWrapper wrapper = new PlatformViewWrapper(ctx); |
| 369 | + wrapper.setLayoutParams(new FrameLayout.LayoutParams(10, 10)); |
| 370 | + |
| 371 | + wrapper.setOffset(30, 40); |
| 372 | + |
| 373 | + final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) wrapper.getLayoutParams(); |
| 374 | + assertEquals(30, params.leftMargin); |
| 375 | + assertEquals(40, params.topMargin); |
| 376 | + assertEquals(Gravity.LEFT | Gravity.TOP, params.gravity); |
| 377 | + assertEquals(0, wrapper.getLeft()); |
| 378 | + assertEquals(0, wrapper.getTop()); |
| 379 | + } |
| 380 | + |
| 381 | + @Test |
| 382 | + public void setOffset_whileLayoutRequested_stillMovesImmediately() { |
| 383 | + final PlatformViewWrapper wrapper = new PlatformViewWrapper(ctx); |
| 384 | + layOutInParent(wrapper, 1, 2); |
| 385 | + wrapper.requestLayout(); |
| 386 | + assertTrue(wrapper.isLayoutRequested()); |
| 387 | + |
| 388 | + wrapper.setOffset(30, 40); |
| 389 | + |
| 390 | + assertEquals(30, wrapper.getLeft()); |
| 391 | + assertEquals(40, wrapper.getTop()); |
| 392 | + } |
| 393 | + |
| 394 | + @Test |
| 395 | + public void onTouchEvent_translatesByCurrentPosition() { |
| 396 | + final AndroidTouchProcessor touchProcessor = mock(AndroidTouchProcessor.class); |
| 397 | + final PlatformViewWrapper wrapper = new PlatformViewWrapper(ctx); |
| 398 | + wrapper.setTouchProcessor(touchProcessor); |
| 399 | + layOutInParent(wrapper, 5, 6); |
| 400 | + final ArgumentCaptor<Matrix> matrixCaptor = ArgumentCaptor.forClass(Matrix.class); |
| 401 | + |
| 402 | + final MotionEvent downEvent = |
| 403 | + MotionEvent.obtain(100, 100, MotionEvent.ACTION_DOWN, 1.0f, 1.0f, 0); |
| 404 | + wrapper.onTouchEvent(downEvent); |
| 405 | + |
| 406 | + // The view moves (e.g. the list scrolls) in the middle of the gesture. |
| 407 | + wrapper.setOffset(15, 26); |
| 408 | + final MotionEvent moveEvent = |
| 409 | + MotionEvent.obtain(100, 101, MotionEvent.ACTION_MOVE, 2.0f, 2.0f, 0); |
| 410 | + wrapper.onTouchEvent(moveEvent); |
| 411 | + |
| 412 | + verify(touchProcessor, times(2)).onTouchEvent(any(MotionEvent.class), matrixCaptor.capture()); |
| 413 | + final List<Matrix> matrices = matrixCaptor.getAllValues(); |
| 414 | + final float[] values = new float[9]; |
| 415 | + matrices.get(0).getValues(values); |
| 416 | + assertEquals(5f, values[Matrix.MTRANS_X], 0f); |
| 417 | + assertEquals(6f, values[Matrix.MTRANS_Y], 0f); |
| 418 | + matrices.get(1).getValues(values); |
| 419 | + assertEquals(15f, values[Matrix.MTRANS_X], 0f); |
| 420 | + assertEquals(26f, values[Matrix.MTRANS_Y], 0f); |
| 421 | + } |
| 422 | + |
303 | 423 | @Implements(ViewGroup.class) |
304 | 424 | public static class ShadowViewGroup extends org.robolectric.shadows.ShadowViewGroup { |
305 | 425 | @Implementation |
|
0 commit comments