Class GeolocationSimulator

java.lang.Object
com.vaadin.flow.component.geolocation.GeolocationSimulator
All Implemented Interfaces:
Serializable

public final class GeolocationSimulator extends Object implements Serializable
Browserless test driver for the Geolocation facade. Provides a state-based simulation of the browser's geolocation: tests describe the world (the user's permission and the sensor's reading), then exercise the application, which observes that world through the Geolocation API.

Two orthogonal axes drive the model:

Resolution rules for Geolocation.get(...):

Trackers behave the same way: the active watch fires on setLocation (when permission is granted) and on setUnavailable; calling denyPermission() delivers a PERMISSION_DENIED error to active watches and stops them.

Obtain via current() or forUI(UI): idempotent, both create the simulator on the first call and return the same instance afterward.

See Also:
  • Method Details

    • current

      public static GeolocationSimulator current()
      Returns the simulator bound to UI.getCurrent().
      Returns:
      the simulator for the current UI
    • forUI

      public static GeolocationSimulator forUI(UI ui)
      Returns the simulator bound to the given UI. The simulator is installed automatically whenever browserless mocking is active: the BrowserlessGeolocationClientFactory is wired into Flow's Lookup by MockVaadinHelper.BrowserlessLookupInitializer and the factory publishes the simulator at UI construction time. This call is a pure lookup with no side effects.
      Parameters:
      ui - the UI to query
      Returns:
      the simulator bound to the UI's in-memory client
      Throws:
      IllegalStateException - if the simulator is not registered (browserless mocking is not active, or the UI was constructed before Flow's Lookup was set up)
    • grantPermission

      public void grantPermission()
      Sets permission to GRANTED. Pending get(...) calls resolve with the current fix or error if either has been set; otherwise they stay pending until one is.
    • denyPermission

      public void denyPermission()
      Sets permission to DENIED. Any pending get(...) call resolves with a PERMISSION_DENIED error. Active watches receive a PERMISSION_DENIED error and are stopped.
    • resetPermission

      public void resetPermission()
      Resets permission to PROMPT — the default starting state, equivalent to a fresh page where the user has not yet responded to the permission dialog.
    • simulateUnsupported

      public void simulateUnsupported()
      Sets availability to UNSUPPORTED, simulating a browser without the Geolocation API or a page context where it is unusable. Pending get(...) calls resolve with a POSITION_UNAVAILABLE error.
    • setLocation

      public void setLocation(GeolocationPosition position)
      Sets the cached sensor fix. When permission is granted, pending get(...) calls resolve with this position and active watches receive it.
      Parameters:
      position - the position the sensor reports
    • setLocation

      public void setLocation(double latitude, double longitude, double accuracy)
      Convenience overload constructing a GeolocationPosition with a current-time timestamp; altitude, altitude accuracy, heading and speed are null.
      Parameters:
      latitude - latitude in degrees
      longitude - longitude in degrees
      accuracy - horizontal accuracy in metres
    • setLocation

      public void setLocation(double latitude, double longitude)
      Convenience overload using a default accuracy of 10 metres.
      Parameters:
      latitude - latitude in degrees
      longitude - longitude in degrees
    • clearLocation

      public void clearLocation()
      Clears the cached sensor fix. Pending get(...) calls and active watches are unaffected; they wait for the next state change.
    • setUnavailable

      public void setUnavailable(GeolocationErrorCode code, String message)
      Sets a sticky sensor error. When permission is granted, pending get(...) calls resolve with this error and active watches receive it. Setting an error clears any cached fix.
      Parameters:
      code - the error code
      message - the error message
    • clearUnavailable

      public void clearUnavailable()
      Clears any cached sensor error. Pending get(...) calls stay pending; active watches are unaffected.
    • requests

      public List<GeolocationRequest> requests()
      Returns the pending one-shot Geolocation.get(...) requests in arrival order.
      Returns:
      an unmodifiable view of pending requests
    • lastRequest

      public Optional<GeolocationRequest> lastRequest()
      Returns the most recent pending Geolocation.get(...) request, if any.
      Returns:
      the most recent pending request, or empty
    • activeTrackers

      public List<GeolocationTrackerSession> activeTrackers()
      Returns currently active tracker sessions for this UI.
      Returns:
      an unmodifiable view of active tracker sessions