Search in sources :

Example 1 with MockPageWithLinkAndComponent

use of org.apache.wicket.MockPageWithLinkAndComponent in project wicket by apache.

the class AjaxTimerBehaviorTest method restartResultsInAddTimeout.

/**
 * Validates the response, then makes sure the timer injects itself again when called. Tests
 * {@link AbstractAjaxTimerBehavior#restart(AjaxRequestTarget)} method
 *
 * WICKET-1525, WICKET-2152
 */
@Test
public void restartResultsInAddTimeout() {
    final Integer labelInitialValue = Integer.valueOf(0);
    final Label label = new Label(MockPageWithLinkAndComponent.COMPONENT_ID, new Model<Integer>(labelInitialValue));
    // the duration doesn't matter because we manually trigger the behavior
    final AbstractAjaxTimerBehavior timerBehavior = new AbstractAjaxTimerBehavior(Duration.seconds(2)) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onTimer(AjaxRequestTarget target) {
            // increment the label's model object
            label.setDefaultModelObject(((Integer) label.getDefaultModelObject()) + 1);
            target.add(label);
        }
    };
    final MockPageWithLinkAndComponent page = new MockPageWithLinkAndComponent();
    page.add(label);
    page.add(new AjaxLink<Void>(MockPageWithLinkAndComponent.LINK_ID) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
            if (timerBehavior.isStopped()) {
                timerBehavior.restart(target);
            } else {
                timerBehavior.stop(target);
            }
        }
    });
    label.setOutputMarkupId(true);
    label.add(timerBehavior);
    tester.startPage(page);
    final String labelPath = MockPageWithLinkAndComponent.COMPONENT_ID;
    // assert label == initial value (i.e. 0)
    tester.assertLabel(labelPath, String.valueOf(labelInitialValue));
    // increment to 1
    tester.executeBehavior(timerBehavior);
    // assert label == 1
    tester.assertLabel(labelPath, String.valueOf(labelInitialValue + 1));
    // stop the timer
    tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
    // trigger it, but it is stopped
    tester.executeBehavior(timerBehavior);
    // assert label is still 1
    tester.assertLabel(labelPath, String.valueOf(labelInitialValue + 1));
    // restart the timer
    tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
    assertMatches("Wicket.Timer.set", 1);
    // WICKET-5439 label is not updated automatically
    assertMatches("wicket:id=\"component\"", 0);
    // increment to 2
    tester.executeBehavior(timerBehavior);
    // assert label is now 2
    tester.assertLabel(labelPath, String.valueOf(labelInitialValue + 2));
}
Also used : MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Label(org.apache.wicket.markup.html.basic.Label) Test(org.junit.Test)

Example 2 with MockPageWithLinkAndComponent

use of org.apache.wicket.MockPageWithLinkAndComponent in project wicket by apache.

the class AjaxTimerBehaviorTest method setVisibleSetsTimeout.

/**
 */
@Test
public void setVisibleSetsTimeout() {
    Duration dur = Duration.seconds(20);
    final AjaxSelfUpdatingTimerBehavior timer = new AjaxSelfUpdatingTimerBehavior(dur);
    final MockPageWithLinkAndComponent page = new MockPageWithLinkAndComponent();
    final Label label = new Label(MockPageWithLinkAndComponent.COMPONENT_ID, "Hello");
    page.add(label);
    page.add(new AjaxLink<Void>(MockPageWithLinkAndComponent.LINK_ID) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
        }
    });
    label.setOutputMarkupId(true);
    label.setVisible(false);
    label.add(timer);
    tester.startPage(page);
    assertMatches("Wicket.Timer.set", 0);
    tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
    assertMatches("Wicket.Timer.set", 0);
    label.setVisible(true);
    tester.startPage(page);
    // no visible, so timeout is set
    assertMatches("Wicket.Timer.set", 1);
}
Also used : MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Label(org.apache.wicket.markup.html.basic.Label) Duration(org.apache.wicket.util.time.Duration) Test(org.junit.Test)

Example 3 with MockPageWithLinkAndComponent

use of org.apache.wicket.MockPageWithLinkAndComponent in project wicket by apache.

the class AjaxTimerBehaviorTest method setDisabledClearsTimeout.

/**
 */
@Test
public void setDisabledClearsTimeout() {
    final AbstractAjaxTimerBehavior timer = new AbstractAjaxTimerBehavior(Duration.seconds(20)) {

        private boolean enabled = true;

        @Override
        protected void onTimer(AjaxRequestTarget target) {
            enabled = false;
        }

        @Override
        public boolean isEnabled(Component component) {
            return enabled;
        }
    };
    final MockPageWithLinkAndComponent page = new MockPageWithLinkAndComponent();
    final Label label = new Label(MockPageWithLinkAndComponent.COMPONENT_ID, "Hello");
    page.add(label);
    page.add(new Link<Void>(MockPageWithLinkAndComponent.LINK_ID) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick() {
        }
    });
    label.setOutputMarkupId(true);
    label.add(timer);
    tester.startPage(page);
    assertMatches("Wicket.Timer.set", 1);
    tester.executeBehavior(timer);
    assertMatches("Wicket.Timer.clear", 0);
    assertMatches("Wicket.Timer.set", 0);
}
Also used : MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Label(org.apache.wicket.markup.html.basic.Label) WebComponent(org.apache.wicket.markup.html.WebComponent) Component(org.apache.wicket.Component) MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Test(org.junit.Test)

Example 4 with MockPageWithLinkAndComponent

use of org.apache.wicket.MockPageWithLinkAndComponent in project wicket by apache.

the class AjaxRequestHandlerTest method executeHeaderTest.

private <C extends Component> void executeHeaderTest(final Class<C> componentClass, String expectedFile) throws IOException {
    final MockPageWithLinkAndComponent page = new MockPageWithLinkAndComponent();
    page.add(new WebComponent(MockPageWithLinkAndComponent.COMPONENT_ID).setOutputMarkupId(true));
    page.add(new AjaxLink<Void>(MockPageWithLinkAndComponent.LINK_ID) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick(AjaxRequestTarget target) {
            // Create an instance of the component
            try {
                Constructor<? extends Component> con = componentClass.getConstructor(new Class[] { String.class });
                Component comp = con.newInstance(MockPageWithLinkAndComponent.COMPONENT_ID);
                page.replace(comp);
                comp.setOutputMarkupId(true);
                target.add(comp);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    });
    tester.startPage(page);
    // System.out.println(tester.getServletResponse().getDocument());
    tester.debugComponentTrees();
    tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
    String document = tester.getLastResponseAsString();
    String headerContribution = null;
    Pattern pat = Pattern.compile(".*<header-contribution.*?>(.*?)</header-contribution>.*", Pattern.DOTALL);
    Matcher mat = pat.matcher(document);
    if (mat.matches()) {
        headerContribution = mat.group(1);
    }
    // This means that it doesn't exist at all
    if (expectedFile == null) {
        assertNull("There was a header contribution on the response " + "(though we didn't expect one): <" + headerContribution + ">", headerContribution);
    } else if (headerContribution == null) {
        fail("Failed to find header contribution: \n" + document);
    } else {
        DiffUtil.validatePage(headerContribution, getClass(), expectedFile, true);
    }
}
Also used : Pattern(java.util.regex.Pattern) MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Matcher(java.util.regex.Matcher) Constructor(java.lang.reflect.Constructor) IOException(java.io.IOException) WebComponent(org.apache.wicket.markup.html.WebComponent) WebComponent(org.apache.wicket.markup.html.WebComponent) Component(org.apache.wicket.Component) MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent)

Example 5 with MockPageWithLinkAndComponent

use of org.apache.wicket.MockPageWithLinkAndComponent in project wicket by apache.

the class AjaxTimerBehaviorTest method pageRenderSetsTimeout.

/**
 * tests timer behavior in a WebPage.
 */
@Test
public void pageRenderSetsTimeout() {
    Duration dur = Duration.seconds(20);
    final AjaxSelfUpdatingTimerBehavior timer = new AjaxSelfUpdatingTimerBehavior(dur);
    final MockPageWithLinkAndComponent page = new MockPageWithLinkAndComponent();
    Label label = new Label(MockPageWithLinkAndComponent.COMPONENT_ID, "Hello");
    page.add(label);
    page.add(new Link<Void>(MockPageWithLinkAndComponent.LINK_ID) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick() {
        // do nothing, link is just used to simulate a roundtrip
        }
    });
    label.setOutputMarkupId(true);
    label.add(timer);
    tester.startPage(page);
    assertMatches("Wicket.Timer.set", 1);
    tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
    assertMatches("Wicket.Timer.set", 1);
    tester.executeBehavior(timer);
    assertMatches("Wicket.Timer.set", 1);
}
Also used : MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Label(org.apache.wicket.markup.html.basic.Label) Duration(org.apache.wicket.util.time.Duration) Test(org.junit.Test)

Aggregations

MockPageWithLinkAndComponent (org.apache.wicket.MockPageWithLinkAndComponent)7 Test (org.junit.Test)6 Label (org.apache.wicket.markup.html.basic.Label)5 Duration (org.apache.wicket.util.time.Duration)4 WebComponent (org.apache.wicket.markup.html.WebComponent)3 Component (org.apache.wicket.Component)2 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)1