Class JavaScriptDispatcher

java.lang.Object
com.vaadin.browserless.JavaScriptDispatcher
All Implemented Interfaces:
Serializable

public final class JavaScriptDispatcher extends Object implements Serializable
Plays the browser for the JavaScript the server sends to a UI during a browserless test.

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:
  • Method Details

    • setUpSimulatedClient

      public static void setUpSimulatedClient(UI ui)
      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-side Focusable.focus() and blur() calls, followed by a new instance of each JavaScriptHandler configured for the environment with setJavaScriptHandlers(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 that getUICapture(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 the JavaScriptHandler classes 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 null
      handlers - the handler classes, in the order they run, not null
    • forUI

      public static JavaScriptDispatcher forUI(UI ui)
      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

      public static void dispatch(UI ui)
      Sends the pending JavaScript of the given UI to the simulated client: runs the tasks scheduled with UI.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 calling UIInternals.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

      public Registration addHandler(JavaScriptDispatcher.Handler handler)
      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

      public boolean markAnswered(PendingJavaScriptInvocation invocation)
      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

      public boolean isAnswered(PendingJavaScriptInvocation invocation)
      Checks whether the simulated client has answered an invocation.
      Parameters:
      invocation - the invocation, not null
      Returns:
      true if the invocation was answered