/** * Simulate that an AJAX event has been fired. You add an AJAX event to a component by using: * * <pre> * ... * component.add(new AjaxEventBehavior(ClientEvent.DBLCLICK) { * public void onEvent(AjaxRequestTarget) { * // Do something. * } * }); * ... * </pre> * * You can then test that the code inside onEvent actually does what it's supposed to, using the * WicketTester: * * <pre> * ... * tester.executeAjaxEvent(component, ClientEvent.DBLCLICK); * * // Test that the code inside onEvent is correct. * ... * </pre> * * PLEASE NOTE! This method doesn't actually insert the component in the client DOM tree, using * javascript. * * @param component The component which has the AjaxEventBehavior we wan't to test. If the * component is null, the test will fail. * @param event The event which we simulate is fired. If the event is null, the test will fail. */ @SuppressWarnings("unchecked") public void executeAjaxEvent(Component component, ClientEvent event) { String failMessage = "Can't execute event on a component which is null."; Assert.assertNotNull(failMessage, component); failMessage = "event must not be null"; Assert.assertNotNull(failMessage, event); // Run through all the behavior and select the LAST ADDED behavior which // matches the event parameter. AjaxEventBehavior ajaxEventBehavior = null; List<IBehavior> behaviors = component.getBehaviors(); for (IBehavior behavior : behaviors) { // AjaxEventBehavior is the one to look for if (behavior instanceof AjaxEventBehavior) { AjaxEventBehavior tmp = (AjaxEventBehavior) behavior; if (tmp.getEvent() == event) { ajaxEventBehavior = tmp; } } } // If there haven't been found any event behaviors on the component // which maches the parameters we fail. failMessage = "No AjaxEventBehavior found on component: " + component.getId() + " which matches the event: " + event.toString(); Assert.assertNotNull(failMessage, ajaxEventBehavior); setupRequestAndResponse(); RequestCycle requestCycle = createRequestCycle(); ajaxEventBehavior.onRequest(); // process the request target requestCycle.getRequestTarget().respond(requestCycle); }