Skip to content

Make the MapsActivity pipelines testable: extract a coordinator #50

Open

Description

@wanderdev0

MapsActivity is ~1500 lines and orchestrates three RxJava pipelines — startup, import, and refresh — against the Apple service, the Room repositories and the map. None of it is reachable from a test, and the failures it produces do not throw.

Why this matters more than usual here

The bug that prompted this was found by a user, on a phone, by counting cards.

onImportFilePicked forked into an Observable.zip: one branch fetching location reports, the other parsing beacon metadata. zip completes as soon as its shortest source does and disposes the rest. That was harmless while Python returned a single dict for the whole batch — both branches emitted exactly once. Changing the fetch to run one accessory at a time made that branch emit per accessory, so zip took the first pair, completed, and cancelled every remaining accessory before it was requested.

Importing three tags fetched one. The other two had no locations and, because a beacon with no location cannot be drawn, no card either. No exception, no error log, nothing in logcat but an absence. A disposal is not a failure, so there was nothing downstream to notice, and the tags sat in the database looking entirely correct.

That is the shape of every bug this class produces: a stream that ends early, a subscription that never fires, a result that arrives after the thing waiting for it gave up. None of them throw. The only assertion that catches them is how many times did the work actually run — which needs a seam.

What already exists

#49 extracted the pieces that were pure, and covered them with 57 JVM tests:

Class What it pins down
RxFlows allThen / oneAtATime — a fast branch must not truncate a slow one; accessories run sequentially; a failure skips one item rather than the batch
RefreshPolicy when the periodic refresh may run; how many hours of history to request
LongFetchBannerState the show/hide counter for the "locating your tags" banner
BeaconLocationHistory location merging, and the rule that a beacon with no location is not drawable

Writing those found two latent bugs — an unsorted first fetch putting a stale position on the map, and a zero-hour fetch window reachable from the refresh button.

But those are the parts. The pipelines that wire them together are still untested, and the wiring is where the bug was.

What blocks testing the Activity directly

onCreate constructs its own dependencies:

this.userSettingsRepo  = new UserSettingsRepository(...);
this.userDataRepository = new UserDataRepository(...);
this.userAuthRepo      = new UserAuthRepository(...);
this.beaconRepo        = new BeaconRepository(...);

There is no injection point, so any test of the real Activity needs Google Play services, a Room database, a Python interpreter and the keystore. Robolectric does not help — it hits the same wall, plus the map provider. That is not a test, it is the app.

Proposal

Move the three pipelines into a MapsCoordinator (or similar) that takes its collaborators as interfaces and reports results through a UI callback, leaving MapsActivity responsible for views, the map, and lifecycle.

Roughly:

interface MapsView {
    void showBeacons(List<BeaconInformation> beacons);
    void showLocations(Map<String, List<BeaconLocationReport>> reports);
    void setFetchProgress(int done, int total);
    void showImportError(Throwable error);
}

final class MapsCoordinator {
    MapsCoordinator(AppleService apple, BeaconRepo beacons, MapsView view, Scheduler scheduler);

    Completable importFrom(ImportData data);
    Completable refreshAll();
    Completable start();
}

With fakes and a TestScheduler, the tests that were impossible become ordinary:

  • importing three accessories issues three fetches and stores three results — the exact regression above, asserted at the level it occurred rather than at the level it was extracted to
  • a fetch that fails for one accessory still stores the other two
  • quitting mid-import keeps the accessories that already resolved (this matters: their updated key alignment is what stops the next launch re-searching tens of thousands of key indices)
  • the startup path shows cached locations before the network fetch returns, and does not double-fetch on rotation
  • a refresh arriving while an import is running does not interleave into Python
  • geocoding runs once per batch rather than once per accessory

Scope

Deliberately not part of #49. That branch is a fork merge plus the key-alignment work, and threading a coordinator refactor through the same review is how the interesting changes get skimmed.

Suggested order, each independently shippable:

  1. Extract MapsCoordinator with the import path only, plus fakes and its tests
  2. Move the refresh path across
  3. Move the startup path across
  4. Delete the now-dead orchestration from MapsActivity

Afterwards, what remains untested in MapsActivity is view wiring and marker drawing — which is the right thing to leave to a human looking at a screen.

This is also what unlocks UI testing

Worth stating plainly, because on its own the above reads as a refactor for tidiness. It is not: it is the prerequisite for testing the map's behaviour on an emulator at all.

Gare's IMapProvider abstraction already provides half of the seam. MapProviderFactory.create(type) is a static factory, so a test can hand back a fake provider that simply records what it was asked to draw. What blocks it is the other half — MapsActivity constructing UserSettingsRepository, UserDataRepository, UserAuthRepository, BeaconRepository and the Python-backed Apple service inside onCreate, with nowhere to put a fake.

With both halves in place, an instrumented test can drive the real Activity against a seeded Room database and a fake Apple service, and assert on behaviour rather than pixels:

  • three beacons stored → three cards rendered and three addMarker calls
  • tapping the second card → setMarkerZIndex(marker2, TOP) and setMarkerZIndex(marker1, DEFAULT)
  • a beacon with no location reports → no card and no marker
  • importing three accessories → three fetches, three stored results

Every one of those is a bug that actually shipped on this branch and was found by a person looking at a phone. The z-index assertion in particular would have failed the moment the map provider abstraction dropped the call, instead of surviving an entire fork merge unnoticed — the constants it used were left behind unreferenced, so the code still looked present to a reader.

Note the shape: they assert that a call happens. Every failure in this area so far has been a capability quietly ceasing to be invoked — a stream disposed early, a setZIndex no longer reached, a flag never set. None of them threw, none appeared in logcat, and none would be caught by a screenshot comparison.

Screenshot tests are deliberately not proposed. Diffing a rendered map means diffing tiles, label placement and animation frames, which goes red for reasons unrelated to the change until someone switches it off. Card-layout regressions (uneven heights, clipped shadows, the navigation bar overlap) are better covered by measuring inflated layouts directly, which needs none of this work and is tracked separately.

The instrumented infrastructure is already in place: :app:testEmulatorDebugAndroidTest provisions and destroys its own emulator, needs nothing booted first, and runs in CI.


Issue co-authored by Claude.

Activity

  1. added
    enhancement
    @app
    on Aug 9, 2026
  2. wanderdev0 commented on Aug 10, 2026

    @wanderdev0
    OwnerAuthor

    The Anisette settings UI on feat/native-anisette-poc is a second case for this, and a
    better-shaped one than MapsActivity in some ways — the states are enumerable, but every one of
    them is currently reachable only by arranging the real world.

    To see them all by hand today you would need: a device with no network, a device where Apple
    has replaced the Apple Music APK, a user upgrading from 1.0.5 with an existing session, a user
    who has chosen a remote server, and a user who has supplied their own APK file. Several of
    those cannot be produced on demand at all — "Apple shipped a new build" is not something you
    can arrange for a screenshot.

    The states worth driving from fakes:

    • local Anisette ready / still setting up / failed for an ordinary reason (no network)
    • failed because the manifest no longer matches, which is the only state that reveals the
      "supply your own APK" controls
    • a user-supplied APK accepted, and rejected for being the wrong version
    • remote selected, with the existing URL and test-button flow
    • the one-time upgrade prompt, which by construction appears exactly once per install

    What makes this tractable is that the seam already exists. LocalAnisette is the single thing
    the UI asks, and it answers through a small surface — ensureReady(), unavailableReason(),
    isChangingMachineIdentity(), otp(), machine(). A fake behind that interface reaches every
    state above without a network, without Apple, and without an APK. That is roughly the same
    argument as extracting the MapsActivity coordinator: the hard part is not the assertions, it is
    having something to substitute.

    The upgrade prompt deserves particular attention. It fires once, for people who already have a
    working login, and getting it wrong moves an existing session onto a different machine identity
    — which forces a re-login. That is not a bug anybody would notice in review, and it is not one
    you would hit again on the same device.

    Co-authored by Claude.

  3. added theissue type on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    @appenhancement

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions