Class GeolocationSimulator

  • All Implemented Interfaces:
    java.io.Serializable

    
    public final class GeolocationSimulator
     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(...):

    • PROMPT or UNKNOWN: the call stays pending until the permission is decided.
    • DENIED or UNSUPPORTED: the call resolves with an error.
    • GRANTED: the call resolves with the cached fix if setLocation was called, with the cached error if setUnavailable was called, or stays pending otherwise.

    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: idempotent, both create the simulator on the first call and return the same instance afterward.

    Since:

    1.1

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      static GeolocationSimulator current() Returns the simulator bound to getCurrent.
      static GeolocationSimulator forUI(UI ui) Returns the simulator bound to the given UI.
      void grantPermission() Sets permission to GRANTED.
      void denyPermission() Sets permission to DENIED.
      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.
      void simulateUnsupported() Sets availability to UNSUPPORTED, simulating a browser without the Geolocation API or a page context where it is unusable.
      void setLocation(GeolocationPosition position) Sets the cached sensor fix.
      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.
      void setLocation(double latitude, double longitude) Convenience overload using a default accuracy of 10 metres.
      void clearLocation() Clears the cached sensor fix.
      void setUnavailable(GeolocationErrorCode code, String message) Sets a sticky sensor error.
      void clearUnavailable() Clears any cached sensor error.
      List<GeolocationRequest> requests() Returns the pending one-shot Geolocation.get(...) requests in arrival order.
      Optional<GeolocationRequest> lastRequest() Returns the most recent pending Geolocation.get(...) request, if any.
      List<GeolocationTrackerSession> activeTrackers() Returns currently active tracker sessions for this UI.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • forUI

         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

      • grantPermission

         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

         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

         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

         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

         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

         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

         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

         void clearLocation()

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

      • setUnavailable

         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

         void clearUnavailable()

        Clears any cached sensor error. Pending get(...) calls stay pending; active watches are unaffected.

      • requests

         List<GeolocationRequest> requests()

        Returns the pending one-shot Geolocation.get(...) requests in arrival order.

        Returns:

        an unmodifiable view of pending requests