diff options
Diffstat (limited to 'uitest')
-rw-r--r-- | uitest/src/com/vaadin/tests/components/link/LinkInsideDisabledContainer.java | 61 | ||||
-rw-r--r-- | uitest/src/com/vaadin/tests/components/link/LinkInsideDisabledContainerTest.java | 141 |
2 files changed, 202 insertions, 0 deletions
diff --git a/uitest/src/com/vaadin/tests/components/link/LinkInsideDisabledContainer.java b/uitest/src/com/vaadin/tests/components/link/LinkInsideDisabledContainer.java new file mode 100644 index 0000000000..2a040b413f --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/link/LinkInsideDisabledContainer.java @@ -0,0 +1,61 @@ +package com.vaadin.tests.components.link; + +import java.util.concurrent.atomic.AtomicInteger; + +import com.vaadin.server.ExternalResource; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.ui.Button; +import com.vaadin.ui.JavaScriptFunction; +import com.vaadin.ui.Link; +import com.vaadin.ui.VerticalLayout; + +import elemental.json.JsonArray; + +public class LinkInsideDisabledContainer extends AbstractTestUIWithLog { + + private final AtomicInteger clickCounter = new AtomicInteger(0); + public static final String CLICK_COUNT_TEXT = "Click count: "; + + @Override + protected void setup(VaadinRequest request) { + + VerticalLayout layout = new VerticalLayout(); + + final Link link = new Link(CLICK_COUNT_TEXT + 0, + new ExternalResource("javascript:__linkClicked()")); + + getPage().getJavaScript().addFunction("__linkClicked", + new JavaScriptFunction() { + + @Override + public void call(JsonArray arguments) { + log(CLICK_COUNT_TEXT + clickCounter.incrementAndGet()); + } + }); + + final VerticalLayout vlayout = new VerticalLayout(); + vlayout.addComponent(link); + vlayout.setId("testContainer"); + layout.addComponent(vlayout); + + Button toggleLinkButton = new Button("enable/disable link"); + toggleLinkButton.addClickListener(new Button.ClickListener() { + @Override + public void buttonClick(Button.ClickEvent clickEvent) { + link.setEnabled(!link.isEnabled()); + } + }); + layout.addComponent(toggleLinkButton); + + Button toggleContainerButton = new Button("enable/disable container"); + toggleContainerButton.addClickListener(new Button.ClickListener() { + @Override + public void buttonClick(Button.ClickEvent clickEvent) { + vlayout.setEnabled(!vlayout.isEnabled()); + } + }); + layout.addComponent(toggleContainerButton); + addComponent(layout); + } +} diff --git a/uitest/src/com/vaadin/tests/components/link/LinkInsideDisabledContainerTest.java b/uitest/src/com/vaadin/tests/components/link/LinkInsideDisabledContainerTest.java new file mode 100644 index 0000000000..8d65101be3 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/link/LinkInsideDisabledContainerTest.java @@ -0,0 +1,141 @@ +package com.vaadin.tests.components.link; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.LinkElement; +import com.vaadin.testbench.elements.VerticalLayoutElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class LinkInsideDisabledContainerTest extends MultiBrowserTest { + + private static final Pattern CLICK_MATCHER = Pattern + .compile(LinkInsideDisabledContainer.CLICK_COUNT_TEXT + "(\\d+)"); + + @Before + public void init() { + openTestURL(); + } + + @Test + public void clickOnEnabledLinkInEnabledContainerShouldPerformAction() + throws InterruptedException { + + clickLink(); + assertTrue(isLinkEnabled()); + assertThat(clicksOnLink(), is(1)); + + clickLink(); + assertThat(clicksOnLink(), is(2)); + + } + + @Test + public void clickOnEnabledLinkInDisabledContainerShouldNotPerformAction() + throws InterruptedException { + + disableContainer(); + + clickLink(); + + assertFalse(isLinkEnabled()); + assertThat(clicksOnLink(), is(0)); + } + + @Test + public void linkShouldMaintainDisabledStatusWhenTogglingContainerEnabledStatus() + throws InterruptedException { + + toggleLinkEnabledStatus(); + clickLink(); + assertFalse(isLinkEnabled()); + assertThat(clicksOnLink(), is(0)); + + disableContainer(); + clickLink(); + assertFalse(isLinkEnabled()); + assertThat(clicksOnLink(), is(0)); + + enableContainer(); + clickLink(); + assertFalse(isLinkEnabled()); + assertThat(clicksOnLink(), is(0)); + + } + + @Test + public void linkShouldMaintainEnabledStatusWhenTogglingContainerEnabledStatus() + throws InterruptedException { + + clickLink(); + assertTrue(isLinkEnabled()); + assertThat(clicksOnLink(), is(1)); + + disableContainer(); + clickLink(); + assertFalse(isLinkEnabled()); + assertThat(clicksOnLink(), is(1)); + + enableContainer(); + clickLink(); + assertTrue(isLinkEnabled()); + assertThat(clicksOnLink(), is(2)); + + } + + private void disableContainer() { + VerticalLayoutElement container = $(VerticalLayoutElement.class) + .id("testContainer"); + if (container.isEnabled()) { + toggleContainerEnabledStatus(); + } + } + + private void enableContainer() { + VerticalLayoutElement container = $(VerticalLayoutElement.class) + .id("testContainer"); + if (!container.isEnabled()) { + toggleContainerEnabledStatus(); + } + } + + private void toggleContainerEnabledStatus() { + ButtonElement button = $(ButtonElement.class).get(1); + button.click(); + } + + private void toggleLinkEnabledStatus() { + ButtonElement button = $(ButtonElement.class).get(0); + button.click(); + } + + protected void clickLink() throws InterruptedException { + findElement(By.tagName("A")).click(); + sleep(250); + } + + private boolean isLinkEnabled() { + return $(LinkElement.class).first().isEnabled(); + } + + private Integer clicksOnLink() { + + if (!getLogs().isEmpty()) { + Matcher m = CLICK_MATCHER.matcher(getLogRow(0)); + if (m.find()) { + return Integer.valueOf(m.group(1)); + } + } + return 0; + } +} |