Class GeolocationSimulator
java.lang.Object
com.vaadin.flow.component.geolocation.GeolocationSimulator
- All Implemented Interfaces:
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:
- Permission —
grantPermission(),denyPermission(),resetPermission(),simulateUnsupported(). Default state isPROMPT: the browser would show a permission dialog on the next call. - Sensor reading —
setLocation(double, double),setLocation(GeolocationPosition),clearLocation(),setUnavailable(GeolocationErrorCode, String),clearUnavailable(). Default state: no fix.
Resolution rules for Geolocation.get(...):
PROMPTorUNKNOWN: the call stays pending until the permission is decided.DENIEDorUNSUPPORTED: the call resolves with an error.GRANTED: the call resolves with the cached fix ifsetLocation(com.vaadin.flow.component.geolocation.GeolocationPosition)was called, with the cached error ifsetUnavailable(com.vaadin.flow.component.geolocation.GeolocationErrorCode, java.lang.String)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(UI): idempotent, both create
the simulator on the first call and return the same instance afterward.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturns currently active tracker sessions for this UI.voidClears the cached sensor fix.voidClears any cached sensor error.static GeolocationSimulatorcurrent()Returns the simulator bound toUI.getCurrent().voidSets permission toDENIED.static GeolocationSimulatorReturns the simulator bound to the given UI.voidSets permission toGRANTED.Returns the most recent pendingGeolocation.get(...)request, if any.requests()Returns the pending one-shotGeolocation.get(...)requests in arrival order.voidResets permission toPROMPT— the default starting state, equivalent to a fresh page where the user has not yet responded to the permission dialog.voidsetLocation(double latitude, double longitude) Convenience overload using a default accuracy of 10 metres.voidsetLocation(double latitude, double longitude, double accuracy) Convenience overload constructing aGeolocationPositionwith a current-time timestamp; altitude, altitude accuracy, heading and speed arenull.voidsetLocation(GeolocationPosition position) Sets the cached sensor fix.voidsetUnavailable(GeolocationErrorCode code, String message) Sets a sticky sensor error.voidSets availability toUNSUPPORTED, simulating a browser without the Geolocation API or a page context where it is unusable.
-
Method Details
-
current
Returns the simulator bound toUI.getCurrent().- Returns:
- the simulator for the current UI
-
forUI
Returns the simulator bound to the given UI. The simulator is installed automatically whenever browserless mocking is active: theBrowserlessGeolocationClientFactoryis wired into Flow'sLookupbyMockVaadinHelper.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
- Throws:
IllegalStateException- if the simulator is not registered (browserless mocking is not active, or the UI was constructed before Flow'sLookupwas set up)
-
grantPermission
public void grantPermission()Sets permission toGRANTED. Pendingget(...)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 toDENIED. Any pendingget(...)call resolves with aPERMISSION_DENIEDerror. Active watches receive aPERMISSION_DENIEDerror and are stopped. -
resetPermission
public void resetPermission()Resets permission toPROMPT— 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 toUNSUPPORTED, simulating a browser without the Geolocation API or a page context where it is unusable. Pendingget(...)calls resolve with aPOSITION_UNAVAILABLEerror. -
setLocation
Sets the cached sensor fix. When permission is granted, pendingget(...)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 aGeolocationPositionwith a current-time timestamp; altitude, altitude accuracy, heading and speed arenull.- Parameters:
latitude- latitude in degreeslongitude- longitude in degreesaccuracy- 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 degreeslongitude- longitude in degrees
-
clearLocation
public void clearLocation()Clears the cached sensor fix. Pendingget(...)calls and active watches are unaffected; they wait for the next state change. -
requests
Returns the pending one-shotGeolocation.get(...)requests in arrival order.- Returns:
- an unmodifiable view of pending requests
-
lastRequest
Returns the most recent pendingGeolocation.get(...)request, if any.- Returns:
- the most recent pending request, or empty
-
activeTrackers
Returns currently active tracker sessions for this UI.- Returns:
- an unmodifiable view of active tracker sessions
-