Skip to content

Commit 9a3ea26

Browse files
committed
fix(android): reject a controlled scroll that leaves the release tail no room, instead of shrinking it
The previous fix capped the tail near GESTURE_DURATION_MAX_MS, which silently dropped the fling-suppressing braking step for scrolls close to the ceiling -- defeating the point of the fix exactly where a long scroll needs it. A controlled scroll's own accepted duration range now stops CONTROLLED_RELEASE_TAIL_MS short of that shared ceiling (9840ms), rejecting anything past it with a clear INVALID_ARGS error instead of truncating the move or the tail. Every accepted controlled scroll now runs the full, unshortened tail; 'inertial' releases are unaffected and keep the full 10000ms range. Also drop the multi-line explanatory comment on the corrected android-lifecycle.test.ts expectation (a same-line comment instead) so the fix doesn't grow that file past its line-count tripwire.
1 parent 4a67a37 commit 9a3ea26

3 files changed

Lines changed: 63 additions & 33 deletions

File tree

‎packages/platform-android/src/__tests__/input-actions.test.ts‎

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ test('scrollAndroid composes the duration floor with the default controlled-rele
158158
assert.equal(touchCalls[0]!.durationMs, GESTURE_SAMPLE_INTERVAL_MS + 160);
159159
});
160160

161-
test('scrollAndroid caps the controlled-release tail at the maximum gesture duration without shortening the move', async () => {
161+
test('scrollAndroid runs the full controlled-release tail at the largest duration that still leaves it room', async () => {
162162
const touchCalls: Parameters<AndroidTouchInjector>[0][] = [];
163+
const maxControlledMoveMs = GESTURE_DURATION_MAX_MS - 160;
163164
const result = await withAndroidAdbProvider(
164165
{
165166
exec: async () => {
@@ -174,23 +175,45 @@ test('scrollAndroid caps the controlled-release tail at the maximum gesture dura
174175
async () =>
175176
await scrollAndroid(ANDROID_EMULATOR, 'down', {
176177
pixels: 1800,
177-
durationMs: GESTURE_DURATION_MAX_MS,
178+
durationMs: maxControlledMoveMs,
178179
}),
179180
);
180181

181-
// The requested move is honored in full — never truncated to make room for the tail.
182-
assert.equal(result.durationMs, GESTURE_DURATION_MAX_MS);
182+
// The requested move is honored in full, and the tail always runs at its full length — the
183+
// dispatched plan lands exactly at GESTURE_DURATION_MAX_MS, never past it.
184+
assert.equal(result.durationMs, maxControlledMoveMs);
183185
const [touch] = touchCalls;
184-
// At the exact ceiling there is no headroom left for a tail; the dispatched plan still never
185-
// exceeds GESTURE_DURATION_MAX_MS, the same boundary every gesture plan is built under.
186186
assert.equal(touch!.durationMs, GESTURE_DURATION_MAX_MS);
187187
assert.equal(touch!.pointers[0]!.samples.at(-1)!.offsetMs, GESTURE_DURATION_MAX_MS);
188188
});
189189

190-
test('scrollAndroid shrinks (but keeps) the controlled-release tail just under the maximum gesture duration', async () => {
190+
test('scrollAndroid rejects a controlled-release durationMs that would leave the release tail no room, without shortening the move', async () => {
191+
await withAndroidAdbProvider(
192+
{
193+
exec: async () => {
194+
throw new Error('adb must not run');
195+
},
196+
gestureViewport: async () => ({ x: 0, y: 0, width: 1080, height: 1920 }),
197+
touch: async () => {
198+
throw new Error('touch must not run for a rejected request');
199+
},
200+
},
201+
{ serial: ANDROID_EMULATOR.id },
202+
async () => {
203+
await assert.rejects(
204+
scrollAndroid(ANDROID_EMULATOR, 'down', {
205+
pixels: 1800,
206+
durationMs: GESTURE_DURATION_MAX_MS - 159,
207+
}),
208+
/scroll durationMs must be at most 9840 for a controlled release/,
209+
);
210+
},
211+
);
212+
});
213+
214+
test("scrollAndroid accepts the full GESTURE_DURATION_MAX_MS for an 'inertial' release, which needs no tail", async () => {
191215
const touchCalls: Parameters<AndroidTouchInjector>[0][] = [];
192-
const nearMaxDurationMs = GESTURE_DURATION_MAX_MS - 50;
193-
const result = await withAndroidAdbProvider(
216+
await withAndroidAdbProvider(
194217
{
195218
exec: async () => {
196219
throw new Error('adb must not run');
@@ -204,16 +227,13 @@ test('scrollAndroid shrinks (but keeps) the controlled-release tail just under t
204227
async () =>
205228
await scrollAndroid(ANDROID_EMULATOR, 'down', {
206229
pixels: 1800,
207-
durationMs: nearMaxDurationMs,
230+
durationMs: GESTURE_DURATION_MAX_MS,
231+
releaseBehavior: 'inertial',
208232
}),
209233
);
210234

211-
assert.equal(result.durationMs, nearMaxDurationMs);
212235
const [touch] = touchCalls;
213-
// Only 50ms of headroom below the ceiling: the tail shrinks to 3 whole 16ms steps (48ms)
214-
// rather than pushing the plan past GESTURE_DURATION_MAX_MS.
215-
assert.equal(touch!.durationMs, nearMaxDurationMs + 48);
216-
assert.ok(touch!.durationMs <= GESTURE_DURATION_MAX_MS);
236+
assert.equal(touch!.durationMs, GESTURE_DURATION_MAX_MS);
217237
});
218238

219239
test('scrollAndroid jitters the axis orthogonal to a horizontal scroll, holding the scroll axis fixed', async () => {

‎packages/platform-android/src/input-actions.ts‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ export async function scrollAndroid(
209209
options?.durationMs ?? DEFAULT_MOBILE_SCROLL_DURATION_MS,
210210
GESTURE_DURATION_MIN_MS,
211211
);
212+
const releaseBehavior = options?.releaseBehavior ?? 'controlled';
213+
if (releaseBehavior === 'controlled') assertRoomForControlledReleaseTail(durationMs);
212214
const gesturePlan = buildGesturePlan(
213215
{
214216
intent: 'pan',
@@ -222,7 +224,6 @@ export async function scrollAndroid(
222224
viewport,
223225
'android',
224226
);
225-
const releaseBehavior = options?.releaseBehavior ?? 'controlled';
226227
const backend = await executeAndroidTouchPlan(
227228
device,
228229
releaseBehavior === 'controlled'
@@ -242,8 +243,28 @@ export async function scrollAndroid(
242243
// still differs — but would leave the release 1px off the requested endpoint).
243244
const CONTROLLED_RELEASE_TAIL_MS = 160;
244245

246+
// The dispatched plan (move + tail) must never exceed GESTURE_DURATION_MAX_MS, the same ceiling
247+
// every gesture plan is built under. Rather than silently dropping the tail for a move that
248+
// leaves it no room, a controlled scroll's own accepted range stops short of the shared ceiling
249+
// by the tail's length — the full tail runs for every accepted controlled scroll, and a request
250+
// past this narrower range is rejected with the reason, not truncated.
251+
const CONTROLLED_RELEASE_MAX_MOVE_MS = GESTURE_DURATION_MAX_MS - CONTROLLED_RELEASE_TAIL_MS;
252+
253+
function assertRoomForControlledReleaseTail(durationMs: number): void {
254+
if (durationMs <= CONTROLLED_RELEASE_MAX_MOVE_MS) return;
255+
throw new AppError(
256+
'INVALID_ARGS',
257+
`scroll durationMs must be at most ${CONTROLLED_RELEASE_MAX_MOVE_MS} for a controlled release ` +
258+
`(leaves room for the ${CONTROLLED_RELEASE_TAIL_MS}ms release tail within the ` +
259+
`${GESTURE_DURATION_MAX_MS}ms gesture ceiling)`,
260+
{
261+
hint: "Pass a shorter durationMs, or releaseBehavior 'inertial' if the fling is acceptable.",
262+
},
263+
);
264+
}
265+
245266
/**
246-
* A short, quivering tail appended after a 'controlled' scroll's endpoint, adding up to
267+
* A short, quivering tail appended after a 'controlled' scroll's endpoint, adding
247268
* `CONTROLLED_RELEASE_TAIL_MS` of real time to the gesture. AOSP's `InputConsumer::rewriteMessage`
248269
* collapses a MOVE that repeats the previous coordinates into a "resampled" sample, and
249270
* `VelocityTracker` skips resampled samples — so a truly stationary tail never reaches the
@@ -255,22 +276,16 @@ const CONTROLLED_RELEASE_TAIL_MS = 160;
255276
* verified against every OEM skin or a Compose `LazyColumn`. An 'inertial' release (the
256277
* `scroll top`/`scroll bottom` edge passes) lifts at the pan's endpoint unchanged.
257278
*
258-
* The dispatched plan never exceeds `GESTURE_DURATION_MAX_MS` — the same ceiling every gesture
259-
* plan is built under — so the tail shrinks (and, at the exact maximum, disappears) for a move
260-
* already at or near that limit; the requested move itself is never shortened to make room.
279+
* Callers must have already checked `assertRoomForControlledReleaseTail` on the move duration —
280+
* this always appends the full tail.
261281
*/
262282
function withControlledReleaseTail(
263283
plan: GesturePlan,
264284
viewport: Rect,
265285
direction: ScrollDirection,
266286
): GesturePlan {
267287
if (plan.topology !== 'single') return plan;
268-
const tailMs = Math.max(
269-
0,
270-
Math.min(CONTROLLED_RELEASE_TAIL_MS, GESTURE_DURATION_MAX_MS - plan.durationMs),
271-
);
272-
const steps = Math.floor(tailMs / GESTURE_SAMPLE_INTERVAL_MS);
273-
if (steps === 0) return plan;
288+
const steps = CONTROLLED_RELEASE_TAIL_MS / GESTURE_SAMPLE_INTERVAL_MS;
274289
const [pointer] = plan.pointers;
275290
const end = pointer.samples.at(-1)!;
276291
const horizontal = direction === 'left' || direction === 'right';
@@ -293,7 +308,7 @@ function withControlledReleaseTail(
293308
];
294309
return {
295310
...plan,
296-
durationMs: plan.durationMs + steps * GESTURE_SAMPLE_INTERVAL_MS,
311+
durationMs: plan.durationMs + CONTROLLED_RELEASE_TAIL_MS,
297312
pointers: [{ ...pointer, samples }],
298313
};
299314
}

‎test/integration/provider-scenarios/android-lifecycle.test.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,7 @@ test(ANDROID_TOUCH_CONTRACT_EVIDENCE.testName, async () => {
226226
}));
227227
assert.deepEqual(touchCalls, [
228228
{ topology: 'single', intent: 'longPress', pointerCount: 1, durationMs: 750 },
229-
// `scroll down` has no edge, so it resolves to the default 'controlled' release: the
230-
// requested 350ms move plus the 160ms release tail that defeats Android's fling (#2371).
231-
// 'inertial' (the `scroll top`/`scroll bottom` edge passes, which lift at the pan's
232-
// endpoint unchanged) is covered at the unit level in
233-
// packages/platform-android/src/__tests__/input-actions.test.ts.
234-
{ topology: 'single', intent: 'pan', pointerCount: 1, durationMs: 510 },
229+
{ topology: 'single', intent: 'pan', pointerCount: 1, durationMs: 510 }, // 'controlled': 350ms move + 160ms tail (#2371)
235230
{ topology: 'single', intent: 'pan', pointerCount: 1, durationMs: 300 },
236231
{ topology: 'single', intent: 'pan', pointerCount: 1, durationMs: 500 },
237232
{ topology: 'two', intent: 'pan', pointerCount: 2, durationMs: 500 },

0 commit comments

Comments
 (0)