Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 706b5d4

Browse files
author
Kaushik Iska
committed
add a unit test
1 parent 9fc1d17 commit 706b5d4

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
lines changed

shell/platform/android/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ action("robolectric_tests") {
456456
"test/io/flutter/plugin/localization/LocalizationPluginTest.java",
457457
"test/io/flutter/plugin/mouse/MouseCursorPluginTest.java",
458458
"test/io/flutter/plugin/platform/PlatformPluginTest.java",
459+
"test/io/flutter/plugin/platform/PlatformViewsControllerTest.java",
459460
"test/io/flutter/plugin/platform/SingleViewPresentationTest.java",
460461
"test/io/flutter/plugins/GeneratedPluginRegistrant.java",
461462
"test/io/flutter/util/FakeKeyEvent.java",

shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public static class PlatformViewTouch {
384384
/** TODO(iskakaushik): javadoc */
385385
public final long motionEventId;
386386

387-
PlatformViewTouch(
387+
public PlatformViewTouch(
388388
int viewId,
389389
@NonNull Number downTime,
390390
@NonNull Number eventTime,

shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,11 @@ public void onTouch(@NonNull PlatformViewsChannel.PlatformViewTouch touch) {
254254
final int viewId = touch.viewId;
255255
float density = context.getResources().getDisplayMetrics().density;
256256
ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT_WATCH);
257-
final MotionEvent event = toMotionEvent(density, touch);
258257
if (vdControllers.containsKey(viewId)) {
258+
final MotionEvent event = toMotionEvent(density, touch, true);
259259
vdControllers.get(touch.viewId).dispatchTouchEvent(event);
260260
} else if (platformViews.get(viewId) != null) {
261+
final MotionEvent event = toMotionEvent(density, touch, false);
261262
View view = platformViews.get(touch.viewId);
262263
view.dispatchTouchEvent(event);
263264
} else {
@@ -305,7 +306,9 @@ private void ensureValidAndroidVersion(int minSdkVersion) {
305306
}
306307
};
307308

308-
private MotionEvent toMotionEvent(float density, PlatformViewsChannel.PlatformViewTouch touch) {
309+
@VisibleForTesting
310+
public MotionEvent toMotionEvent(
311+
float density, PlatformViewsChannel.PlatformViewTouch touch, boolean usingVirtualDiplays) {
309312
MotionEventTracker.MotionEventId motionEventId =
310313
MotionEventTracker.MotionEventId.from(touch.motionEventId);
311314
MotionEvent trackedEvent = motionEventTracker.pop(motionEventId);
@@ -321,11 +324,11 @@ private MotionEvent toMotionEvent(float density, PlatformViewsChannel.PlatformVi
321324
parsePointerCoordsList(touch.rawPointerCoords, density)
322325
.toArray(new PointerCoords[touch.pointerCount]);
323326

324-
if (trackedEvent != null) {
327+
if (!usingVirtualDiplays && trackedEvent != null) {
325328
return MotionEvent.obtain(
326329
trackedEvent.getDownTime(),
327330
trackedEvent.getEventTime(),
328-
touch.action, // TODO (kaushikiska): https://github.com/flutter/flutter/issues/61169
331+
trackedEvent.getAction(),
329332
touch.pointerCount,
330333
pointerProperties,
331334
pointerCoords,

shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java

+102
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package io.flutter.plugin.platform;
22

3+
import static io.flutter.embedding.engine.systemchannels.PlatformViewsChannel.PlatformViewTouch;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNotEquals;
36
import static org.mockito.Matchers.eq;
47
import static org.mockito.Mockito.mock;
58
import static org.mockito.Mockito.never;
69
import static org.mockito.Mockito.times;
710
import static org.mockito.Mockito.verify;
811

12+
import android.view.MotionEvent;
913
import android.view.View;
14+
import io.flutter.embedding.android.MotionEventTracker;
15+
import java.util.Collections;
1016
import org.junit.Test;
1117
import org.junit.runner.RunWith;
1218
import org.robolectric.RobolectricTestRunner;
@@ -79,4 +85,100 @@ public void itCancelsOldPresentationOnResize() {
7985
assertEquals(fakeVdController1.presentation != presentation, true);
8086
assertEquals(presentation.isShowing(), false);
8187
}
88+
89+
@Test
90+
public void itUsesActionEventTypeFromFrameworkEventForVirtualDisplays() {
91+
MotionEventTracker motionEventTracker = MotionEventTracker.getInstance();
92+
PlatformViewsController platformViewsController = new PlatformViewsController();
93+
94+
MotionEvent original =
95+
MotionEvent.obtain(
96+
100, // downTime
97+
100, // eventTime
98+
1, // action
99+
0, // x
100+
0, // y
101+
0 // metaState
102+
);
103+
104+
// track an event that will later get passed to us from framework
105+
MotionEventTracker.MotionEventId motionEventId = motionEventTracker.track(original);
106+
107+
PlatformViewTouch frameWorkTouch =
108+
new PlatformViewTouch(
109+
0, // viewId
110+
original.getDownTime(),
111+
original.getEventTime(),
112+
2, // action
113+
0, // pointerCount
114+
Collections.emptyList(),
115+
Collections.emptyList(),
116+
original.getMetaState(),
117+
original.getButtonState(),
118+
original.getXPrecision(),
119+
original.getYPrecision(),
120+
original.getDeviceId(),
121+
original.getEdgeFlags(),
122+
original.getSource(),
123+
original.getFlags(),
124+
motionEventId.getId());
125+
126+
MotionEvent resolvedEvent =
127+
platformViewsController.toMotionEvent(
128+
1, // density
129+
frameWorkTouch,
130+
true // usingVirtualDisplays
131+
);
132+
133+
assertEquals(resolvedEvent.getAction(), frameWorkTouch.action);
134+
assertNotEquals(resolvedEvent.getAction(), original.getAction());
135+
}
136+
137+
@Test
138+
public void itUsesActionEventTypeFromMotionEventForHybridPlatformViews() {
139+
MotionEventTracker motionEventTracker = MotionEventTracker.getInstance();
140+
PlatformViewsController platformViewsController = new PlatformViewsController();
141+
142+
MotionEvent original =
143+
MotionEvent.obtain(
144+
100, // downTime
145+
100, // eventTime
146+
1, // action
147+
0, // x
148+
0, // y
149+
0 // metaState
150+
);
151+
152+
// track an event that will later get passed to us from framework
153+
MotionEventTracker.MotionEventId motionEventId = motionEventTracker.track(original);
154+
155+
PlatformViewTouch frameWorkTouch =
156+
new PlatformViewTouch(
157+
0, // viewId
158+
original.getDownTime(),
159+
original.getEventTime(),
160+
2, // action
161+
0, // pointerCount
162+
Collections.emptyList(),
163+
Collections.emptyList(),
164+
original.getMetaState(),
165+
original.getButtonState(),
166+
original.getXPrecision(),
167+
original.getYPrecision(),
168+
original.getDeviceId(),
169+
original.getEdgeFlags(),
170+
original.getSource(),
171+
original.getFlags(),
172+
motionEventId.getId());
173+
174+
MotionEvent resolvedEvent =
175+
platformViewsController.toMotionEvent(
176+
1, // density
177+
frameWorkTouch,
178+
false // usingVirtualDisplays
179+
);
180+
181+
assertNotEquals(resolvedEvent.getAction(), frameWorkTouch.action);
182+
assertEquals(resolvedEvent.getAction(), original.getAction());
183+
}
82184
}

0 commit comments

Comments
 (0)