Skip to content

fix(android): decouple scalingRatio from size() method#2018

Open
quanru wants to merge 3 commits intomainfrom
fix/android-scaling-ratio-independent
Open

fix(android): decouple scalingRatio from size() method#2018
quanru wants to merge 3 commits intomainfrom
fix/android-scaling-ratio-independent

Conversation

@quanru
Copy link
Collaborator

@quanru quanru commented Feb 14, 2026

Summary

  • Extract scalingRatio calculation from size() into a dedicated getScalingRatio() method, so adjustCoordinates() computes the correct scaling ratio independently — even if size() is overridden by a subclass.
  • Add unit tests to prevent regression: verify coordinate adjustment works without calling size(), with overridden size(), after DPR changes, and with custom screenshotResizeScale.

Background

scalingRatio was previously set as a side effect inside size(). If a user overrode the size() method (e.g., in a subclass), scalingRatio would remain at its default value of 1, causing adjustCoordinates() to produce incorrect physical coordinates for all touch/scroll operations.

Changes

packages/android/src/device.ts:

  • Add getScalingRatio() private method that lazily computes and caches the scaling ratio
  • size() now calls getScalingRatio() instead of computing inline
  • adjustCoordinates() now calls getScalingRatio() directly instead of reading the instance variable
  • Reset scalingRatioInitialized flag when devicePixelRatio changes in initializeDevicePixelRatio()

packages/android/tests/unit-test/page.test.ts:

  • Add scalingRatio independent of size() test suite with 5 test cases

Test plan

  • npx nx test @midscene/android -- tests/unit-test/page.test.ts -t "scalingRatio" — 5 tests pass
  • npx nx build @midscene/android — builds successfully

The scalingRatio was computed as a side effect inside size(). If a user
overrode size() in a subclass, scalingRatio would remain at its default
value of 1, causing adjustCoordinates() to produce incorrect physical
coordinates.

Extract the computation into a dedicated getScalingRatio() method that
both size() and adjustCoordinates() call independently, so coordinate
adjustment works correctly regardless of whether size() is overridden.
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Feb 14, 2026

Deploying midscene with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0c07ed0
Status: ✅  Deploy successful!
Preview URL: https://cdeaa443.midscene.pages.dev
Branch Preview URL: https://fix-android-scaling-ratio-in.midscene.pages.dev

View logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant