Class JavaScriptDispatcher
- All Implemented Interfaces:
Serializable
At each client boundary - a round trip, the end of a simulated user
interaction - the whole pending JavaScript queue of the UI is taken, in
order, the way a server response would send it. Each invocation is offered to
the registered handlers: the ones that simulate what the browser would do
with it, and each ExecutedJavaScript capture a test has started.
Taking the queue is what lets the framework react to scheduled JavaScript without hiding it from the application or from the test: this is the only place that consumes the queue, and every handler sees every invocation, so the simulation and any number of captures never take calls from each other.
For internal use only. May be renamed or removed in a future release.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceReceives each piece of JavaScript sent to the simulated client, to simulate what the browser does with it or to capture it for a test. -
Method Summary
Modifier and TypeMethodDescriptionaddHandler(JavaScriptDispatcher.Handler handler) Registers a handler that is offered every invocation sent from now on.static voidSends the pending JavaScript of the given UI to the simulated client: runs the tasks scheduled withUI.beforeClientResponse, then takes the whole queue and offers each invocation to the handlers.static JavaScriptDispatcherGets the dispatcher of the given UI, creating it on first use.booleanisAnswered(PendingJavaScriptInvocation invocation) Checks whether the simulated client has answered an invocation.booleanmarkAnswered(PendingJavaScriptInvocation invocation) Marks an invocation as answered by the simulated client, which answers each invocation at most once, like a browser.static voidsetJavaScriptHandlers(VaadinService service, Collection<Class<? extends JavaScriptHandler>> handlers) Sets theJavaScriptHandlerclasses that every UI created from now on for the given service gets an instance of, replacing the ones set before.static voidSets up the simulated client of a UI that the mocked environment has just created, before the UI is initialized: installs the handlers that simulate what a browser does with the JavaScript Browserless Test knows how to simulate, such as server-sideFocusable.focus()andblur()calls, followed by a new instance of eachJavaScriptHandlerconfigured for the environment withsetJavaScriptHandlers(VaadinService, Collection).
-
Method Details
-
setUpSimulatedClient
Sets up the simulated client of a UI that the mocked environment has just created, before the UI is initialized: installs the handlers that simulate what a browser does with the JavaScript Browserless Test knows how to simulate, such as server-sideFocusable.focus()andblur()calls, followed by a new instance of eachJavaScriptHandlerconfigured for the environment withsetJavaScriptHandlers(VaadinService, Collection). Installing them up front means a call sent before any tester interaction, while the initial view is built or by a lookup, is handled too. Also starts the capture thatgetUICapture(UI)returns, so that it holds everything the UI sends. Setting up the same UI again does nothing.- Parameters:
ui- the UI, not null- Throws:
BrowserlessTestSetupException- if a configured handler cannot be instantiated
-
setJavaScriptHandlers
public static void setJavaScriptHandlers(VaadinService service, Collection<Class<? extends JavaScriptHandler>> handlers) Sets theJavaScriptHandlerclasses that every UI created from now on for the given service gets an instance of, replacing the ones set before.- Parameters:
service- the service of the mocked environment, not nullhandlers- the handler classes, in the order they run, not null
-
forUI
Gets the dispatcher of the given UI, creating it on first use.- Parameters:
ui- the UI, not null- Returns:
- the dispatcher of the UI, not null
-
dispatch
Sends the pending JavaScript of the given UI to the simulated client: runs the tasks scheduled withUI.beforeClientResponse, then takes the whole queue and offers each invocation to the handlers.Does nothing for a UI that is closed and removed from its session, as there is no client left to send to.
- Parameters:
ui- the UI whose pending JavaScript to send, or null to do nothing- Throws:
IllegalStateException- if something else, such as a test callingUIInternals.dumpPendingJavaScriptInvocations(), took the pending JavaScript queue of the UI since the previous dispatch; any JavaScript taken that way reached neither the simulation nor any capture, so the test would no longer check what a browser gets
-
addHandler
Registers a handler that is offered every invocation sent from now on.- Parameters:
handler- the handler, not null- Returns:
- a handle for removing the handler, not null
-
markAnswered
Marks an invocation as answered by the simulated client, which answers each invocation at most once, like a browser.- Parameters:
invocation- the invocation, not null- Returns:
- true if the invocation was not answered before
-
isAnswered
Checks whether the simulated client has answered an invocation.- Parameters:
invocation- the invocation, not null- Returns:
- true if the invocation was answered
-