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
GeolocationAPI.Two orthogonal axes drive the model:
- Permission — grantPermission, denyPermission, resetPermission, simulateUnsupported. Default state is PROMPT: the browser would show a permission dialog on the next call.
- Sensor reading — setLocation, setLocation, clearLocation, setUnavailable, clearUnavailable. Default state: no fix.
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_DENIEDerror 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
-
-
Method Summary
Modifier and Type Method Description static GeolocationSimulatorcurrent()Returns the simulator bound to getCurrent. static GeolocationSimulatorforUI(UI ui)Returns the simulator bound to the given UI. voidgrantPermission()Sets permission to GRANTED. voiddenyPermission()Sets permission to DENIED. voidresetPermission()Resets permission to PROMPT — the default starting state, equivalent to a fresh page where the user has not yet responded to the permission dialog. voidsimulateUnsupported()Sets availability to UNSUPPORTED, simulating a browser without the Geolocation API or a page context where it is unusable. voidsetLocation(GeolocationPosition position)Sets the cached sensor fix. voidsetLocation(double latitude, double longitude, double accuracy)Convenience overload constructing a GeolocationPosition with a current-time timestamp; altitude, altitude accuracy, heading and speed are null.voidsetLocation(double latitude, double longitude)Convenience overload using a default accuracy of 10 metres. voidclearLocation()Clears the cached sensor fix. voidsetUnavailable(GeolocationErrorCode code, String message)Sets a sticky sensor error. voidclearUnavailable()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. -
-
Method Detail
-
current
static GeolocationSimulator current()
Returns the simulator bound to getCurrent.
- Returns:
the simulator for the current UI
-
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.BrowserlessLookupInitializerand 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 aPERMISSION_DENIEDerror. Active watches receive aPERMISSION_DENIEDerror 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 aPOSITION_UNAVAILABLEerror.
-
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 degreeslongitude- longitude in degreesaccuracy- horizontal accuracy in metres
-
setLocation
void setLocation(double latitude, double longitude)
Convenience overload using a default accuracy of 10 metres.
- Parameters:
latitude- latitude in degreeslongitude- 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 codemessage- 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
-
lastRequest
Optional<GeolocationRequest> lastRequest()
Returns the most recent pending
Geolocation.get(...)request, if any.- Returns:
the most recent pending request, or empty
-
activeTrackers
List<GeolocationTrackerSession> activeTrackers()
Returns currently active tracker sessions for this UI.
- Returns:
an unmodifiable view of active tracker sessions
-
-
-
-