*/
package com.vaadin.tests.components.ui;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
-import org.junit.Assert;
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
import org.junit.Test;
-import org.openqa.selenium.WebElement;
-import org.openqa.selenium.support.ui.ExpectedConditions;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.support.ui.ExpectedCondition;
-import com.vaadin.testbench.By;
-import com.vaadin.tests.tb3.MultiBrowserTest;
+import static org.junit.Assert.assertTrue;
public class UIAccessTest extends MultiBrowserTest {
+
+ @Override
+ public void setup() throws Exception {
+ super.setup();
+
+ setPush(false);
+ openTestURL();
+ }
+
@Test
public void testThreadLocals() {
setPush(true);
openTestURL();
- getCurrentInstanceWhenPushingButton().click();
- waitUntil(ExpectedConditions.textToBePresentInElement(
- vaadinLocatorById("Log_row_0"), "1."));
- Assert.assertEquals("0. Current UI matches in beforeResponse? true",
- vaadinElementById("Log_row_1").getText());
- Assert.assertEquals(
- "1. Current session matches in beforeResponse? true",
- vaadinElementById("Log_row_0").getText());
+ $(ButtonElement.class).get(7).click();
+
+ waitForLogToContainText("0. Current UI matches in beforeResponse? true");
+ waitForLogToContainText("1. Current session matches in beforeResponse? true");
+ }
+
+ @Test
+ public void canBeAccessedFromUIThread() {
+ $(ButtonElement.class).first().click();
+
+ assertTrue(logContainsText("0. Access from UI thread future is done? false"));
+ assertTrue(logContainsText("1. Access from UI thread is run"));
+ assertTrue(logContainsText("2. beforeClientResponse future is done? true"));
}
@Test
- public void testAccessMethod() throws Exception {
- openTestURL();
+ public void canBeAccessedFromBackgroundThread() {
+ $(ButtonElement.class).get(1).click();
- vaadinElement(
- "/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[0]/VButton[0]/domChild[0]/domChild[0]")
- .click();
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[0]/VButton[0]/domChild[0]/domChild[0]"))
- .click();
- assertTrue(driver
- .findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_2"))
- .getText()
- .matches(
- "^0\\. Access from UI thread future is done[\\s\\S] false$"));
- assertEquals(
- "1. Access from UI thread is run",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_1"))
- .getText());
- assertTrue(driver
- .findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_0"))
- .getText()
- .matches(
- "^2\\. beforeClientResponse future is done[\\s\\S] true$"));
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[1]/VButton[0]/domChild[0]/domChild[0]"))
- .click();
- assertEquals(
- "0. Initial background message",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_1"))
- .getText());
- assertTrue(driver
- .findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_0"))
- .getText()
- .matches("^1\\. Thread has current response[\\s\\S] false$"));
- for (int second = 0;; second++) {
- if (second >= 30) {
- fail("timeout");
- }
- try {
- if ("0. Initial background message"
- .equals(driver
- .findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_2"))
- .getText())) {
- break;
- }
- } catch (Exception e) {
+ assertTrue(logContainsText("0. Initial background message"));
+ assertTrue(logContainsText("1. Thread has current response? false"));
+
+ waitForLogToContainText("2. Thread got lock, inital future done? true");
+ }
+
+ private void waitForLogToContainText(final String text) {
+ waitUntil(new ExpectedCondition<Object>() {
+ @Override
+ public Object apply(WebDriver input) {
+ return logContainsText(text);
}
- Thread.sleep(1000);
- }
-
- assertTrue(driver
- .findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_0"))
- .getText()
- .matches(
- "^2\\. Thread got lock, inital future done[\\s\\S] true$"));
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[2]/VButton[0]/domChild[0]/domChild[0]"))
- .click();
- assertEquals(
- "0. Throwing exception in access",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_2"))
- .getText());
- assertTrue(driver
- .findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_1"))
- .getText().matches("^1\\. firstFuture is done[\\s\\S] true$"));
- assertEquals(
- "2. Got exception from firstFuture: java.lang.RuntimeException: Catch me if you can",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_0"))
- .getText());
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[3]/VButton[0]/domChild[0]/domChild[0]"))
- .click();
- assertEquals(
- "0. future was cancled, should not start",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_0"))
- .getText());
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[4]/VButton[0]/domChild[0]/domChild[0]"))
- .click();
- assertEquals(
- "0. Waiting for thread to start",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_2"))
- .getText());
- assertEquals(
- "1. Thread started, waiting for interruption",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_1"))
- .getText());
- assertEquals(
- "2. I was interrupted",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_0"))
- .getText());
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[5]/VButton[0]/domChild[0]/domChild[0]"))
- .click();
- assertTrue(driver
- .findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_3"))
- .getText()
- .matches("^0\\. accessSynchronously has request[\\s\\S] true$"));
- assertEquals(
- "1. Test value in accessSynchronously: Set before accessSynchronosly",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_2"))
- .getText());
- assertTrue(driver
- .findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_1"))
- .getText()
- .matches(
- "^2\\. has request after accessSynchronously[\\s\\S] true$"));
- assertEquals(
- "3. Test value after accessSynchornously: Set in accessSynchronosly",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_0"))
- .getText());
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[6]/VButton[0]/domChild[0]/domChild[0]"))
- .click();
- assertTrue(driver
- .findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_3"))
- .getText().matches("^0\\. access has request[\\s\\S] false$"));
- assertEquals(
- "1. Test value in access: Set before access",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_2"))
- .getText());
- assertTrue(driver
- .findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_1"))
- .getText()
- .matches("^2\\. has request after access[\\s\\S] true$"));
- assertEquals(
- "3. Test value after access: Set before run pending",
- driver.findElement(
- By.vaadin("runcomvaadintestscomponentsuiUIAccess::PID_SLog_row_0"))
- .getText());
+ });
+ }
+ @Test
+ public void exceptionCanBeThrown() {
+ $(ButtonElement.class).get(2).click();
+
+ assertTrue(logContainsText("0. Throwing exception in access"));
+ assertTrue(logContainsText("1. firstFuture is done? true"));
+ assertTrue(logContainsText("2. Got exception from firstFuture: java.lang.RuntimeException: Catch me if you can"));
}
- private WebElement getCurrentInstanceWhenPushingButton() {
- return vaadinElement("/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[7]/VButton[0]");
+ @Test
+ public void futureIsCancelledBeforeStarted() {
+ $(ButtonElement.class).get(3).click();
+
+ assertTrue(logContainsText("0. future was cancelled, should not start"));
}
+
+ @Test
+ public void runningThreadIsCancelled() {
+ $(ButtonElement.class).get(4).click();
+
+ waitForLogToContainText("0. Waiting for thread to start");
+ waitForLogToContainText("1. Thread started, waiting for interruption");
+ waitForLogToContainText("2. I was interrupted");
+ }
+
+ @Test
+ public void testAccessSynchronously() {
+ $(ButtonElement.class).get(5).click();
+
+ assertTrue(logContainsText("0. accessSynchronously has request? true"));
+ assertTrue(logContainsText("1. Test value in accessSynchronously: Set before accessSynchronosly"));
+ assertTrue(logContainsText("2. has request after accessSynchronously? true"));
+ assertTrue(logContainsText("3. Test value after accessSynchornously: Set in accessSynchronosly"));
+ }
+
+ @Test
+ public void currentInstanceCanAccessValue() {
+ $(ButtonElement.class).get(6).click();
+
+ assertTrue(logContainsText("0. access has request? false"));
+ assertTrue(logContainsText("1. Test value in access: Set before access"));
+ assertTrue(logContainsText("2. has request after access? true"));
+ assertTrue(logContainsText("3. Test value after access: Set before run pending"));
+ }
+
}