Search in sources :

Example 6 with WebComponent

use of org.apache.wicket.markup.html.WebComponent in project wicket by apache.

the class AjaxTimerBehaviorTest method addedInAjaxSetsTimout.

/**
 * Tests timer behavior in a component added to an AjaxRequestTarget
 */
@Test
public void addedInAjaxSetsTimout() {
    Duration dur = Duration.seconds(20);
    final AjaxSelfUpdatingTimerBehavior timer = new AjaxSelfUpdatingTimerBehavior(dur);
    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) {
            WebMarkupContainer wmc = new WebMarkupContainer(MockPageWithLinkAndComponent.COMPONENT_ID);
            wmc.setOutputMarkupId(true);
            wmc.add(timer);
            page.replace(wmc);
            target.add(wmc);
        }
    });
    tester.startPage(page);
    tester.clickLink(MockPageWithLinkAndComponent.LINK_ID);
    // first render sets timeout
    assertMatches("Wicket.Timer.set", 1);
    tester.executeBehavior(timer);
    assertMatches("Wicket.Timer.set", 1);
}
Also used : WebComponent(org.apache.wicket.markup.html.WebComponent) MockPageWithLinkAndComponent(org.apache.wicket.MockPageWithLinkAndComponent) Duration(org.apache.wicket.util.time.Duration) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) Test(org.junit.Test)

Example 7 with WebComponent

use of org.apache.wicket.markup.html.WebComponent in project wicket by apache.

the class ComponentTest method modelChange.

/**
 * https://issues.apache.org/jira/browse/WICKET-4483
 *
 * setDefaultModel() should call modelChanging/modelChanged only if the new model
 * is different that the old one. The same as setDefaultModelObject().
 */
@Test
public void modelChange() {
    final AtomicBoolean modelChanging = new AtomicBoolean(false);
    final AtomicBoolean modelChanged = new AtomicBoolean(false);
    WebComponent component = new WebComponent("someId") {

        @Override
        protected void onModelChanging() {
            super.onModelChanging();
            modelChanging.set(true);
        }

        @Override
        protected void onModelChanged() {
            super.onModelChanged();
            modelChanged.set(true);
        }
    };
    assertNull(component.getDefaultModel());
    IModel<Integer> model = Model.of(1);
    // set a model which is different that the old one (old = null, new = non-null)
    component.setDefaultModel(model);
    assertTrue(modelChanging.getAndSet(false));
    assertTrue(modelChanged.getAndSet(false));
    // set the same instance - no change notifications should happen
    component.setDefaultModel(model);
    assertFalse(modelChanging.get());
    assertFalse(modelChanged.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebComponent(org.apache.wicket.markup.html.WebComponent) Test(org.junit.Test)

Example 8 with WebComponent

use of org.apache.wicket.markup.html.WebComponent in project wicket by apache.

the class ComponentTest method isStateless.

/**
 * https://issues.apache.org/jira/browse/WICKET-4468
 *
 * Verifies that a stateful component can pretend to be stateless if both conditions are
 * fulfilled:
 * <ol>
 *     <li>it is either invisible or disabled (or both)</li>
 *     <li>it cannot call any listener interface method while invisible/disabled</li>
 * </ol>
 */
@Test
public void isStateless() {
    Behavior statefulBehavior = new Behavior() {

        @Override
        public boolean getStatelessHint(Component component) {
            return false;
        }
    };
    WebComponent component = new WebComponent("someId");
    // by default every component is stateless
    assertTrue(component.isStateless());
    // make the component stateful
    component.add(statefulBehavior);
    assertFalse(component.isStateless());
    // invisible component cannot be requested by default so it
    // can pretend being stateless
    component.setVisible(false);
    assertTrue(component.isStateless());
    // same for disabled component
    component.setVisible(true).setEnabled(false);
    assertTrue(component.isStateless());
    // make the component such that it can call listener interface
    // methods no matter whether it is visible or enabled
    component = new WebComponent("someId") {

        @Override
        public boolean canCallListener() {
            return true;
        }
    };
    component.add(statefulBehavior);
    component.setVisible(false);
    assertFalse(component.isStateless());
    // do the same set of tests on a component that is stateful "by itself"
    // rather than from a behavior
    Link<Void> link = new Link<Void>("someId") {

        @Override
        public void onClick() {
        }
    };
    // Links are stateful by default
    assertFalse(link.isStateless());
    // make the link invisible
    link.setVisible(false);
    // invisible link cannot be requested by default so it
    // can pretend being stateless
    assertTrue(link.isStateless());
    // same for disabled link
    link.setVisible(true).setEnabled(false);
    assertTrue(link.isStateless());
    // make the link such that it can call listener interface
    // methods no matter whether it is visible or enabled
    link = new Link<Void>("someId") {

        @Override
        public boolean canCallListener() {
            return true;
        }

        @Override
        public void onClick() {
        }
    };
    link.setVisible(false);
    assertFalse(link.isStateless());
}
Also used : WebComponent(org.apache.wicket.markup.html.WebComponent) Behavior(org.apache.wicket.behavior.Behavior) AjaxEventBehavior(org.apache.wicket.ajax.AjaxEventBehavior) WebComponent(org.apache.wicket.markup.html.WebComponent) Link(org.apache.wicket.markup.html.link.Link) Test(org.junit.Test)

Example 9 with WebComponent

use of org.apache.wicket.markup.html.WebComponent in project wicket by apache.

the class DifferentPageCheckerTest method serializingAnotherPage.

/**
 * https://issues.apache.org/jira/browse/WICKET-5634
 *
 * Tests that the serialization fails when a checking ObjectOutputStream is
 * used with DifferentPageChecker and there is a component in the object tree that
 * keeps a reference to a page which is not component.getPage()..
 */
@Test
public void serializingAnotherPage() {
    JavaSerializer serializer = new JavaSerializer("JavaSerializerTest") {

        @Override
        protected ObjectOutputStream newObjectOutputStream(OutputStream out) throws IOException {
            IObjectChecker checker = new DifferentPageChecker();
            return new CheckingObjectOutputStream(out, checker);
        }
    };
    WebComponent component = new ComponentThatKeepsAReferenceToAnotherPage(MockPageWithLink.LINK_ID);
    MockPageWithLink rootPage = new MockPageWithLink();
    rootPage.add(component);
    byte[] serialized = serializer.serialize(rootPage);
    assertNull("The produced byte[] must be null if there was an error", serialized);
}
Also used : WebComponent(org.apache.wicket.markup.html.WebComponent) OutputStream(java.io.OutputStream) ObjectOutputStream(java.io.ObjectOutputStream) JavaSerializer(org.apache.wicket.serialize.java.JavaSerializer) MockPageWithLink(org.apache.wicket.MockPageWithLink) Test(org.junit.Test)

Example 10 with WebComponent

use of org.apache.wicket.markup.html.WebComponent in project wicket by apache.

the class OrphanComponentCheckerTest method checkOrphanComponent.

@Test
public void checkOrphanComponent() {
    WebComponent component = new WebComponent("a");
    IObjectChecker checker = new OrphanComponentChecker();
    IObjectChecker.Result result = checker.check(component);
    assertEquals(IObjectChecker.Result.Status.FAILURE, result.status);
    assertEquals("A component without a parent is detected.", result.reason);
    WebPage parent = new TestPage_1();
    parent.add(component);
    IObjectChecker.Result result2 = checker.check(component);
    assertEquals(IObjectChecker.Result.SUCCESS, result2);
}
Also used : WebComponent(org.apache.wicket.markup.html.WebComponent) WebPage(org.apache.wicket.markup.html.WebPage) TestPage_1(org.apache.wicket.TestPage_1) Test(org.junit.Test)

Aggregations

WebComponent (org.apache.wicket.markup.html.WebComponent)10 Test (org.junit.Test)8 ObjectOutputStream (java.io.ObjectOutputStream)2 OutputStream (java.io.OutputStream)2 Component (org.apache.wicket.Component)2 MockPageWithLinkAndComponent (org.apache.wicket.MockPageWithLinkAndComponent)2 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 MockPageWithLink (org.apache.wicket.MockPageWithLink)1 TestPage_1 (org.apache.wicket.TestPage_1)1 AjaxEventBehavior (org.apache.wicket.ajax.AjaxEventBehavior)1 Behavior (org.apache.wicket.behavior.Behavior)1 CheckingObjectOutputStream (org.apache.wicket.core.util.objects.checker.CheckingObjectOutputStream)1 IObjectChecker (org.apache.wicket.core.util.objects.checker.IObjectChecker)1 NotDetachedModelChecker (org.apache.wicket.core.util.objects.checker.NotDetachedModelChecker)1