From: Henri Sara Date: Mon, 24 Mar 2014 14:32:20 +0000 (+0200) Subject: Fix tooltip test problems caused by #12458 X-Git-Tag: 7.3.0.beta1~2^2~56 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=49bc3b9ffba204c32885e9ed97da2df2eb0dad68;p=vaadin-framework.git Fix tooltip test problems caused by #12458 Tooltips are no longer removed but moved outside the viewport, which broke several tests. Rewrite related tests using TB4 to be stable across all browsers. There are differences between browsers (e.g. position -999 or -1000, gwt-uid-* varied between browsers etc.) so TB2 tests kept failing on some or all browsers. Some tests have been renamed to better indicate what they do. Change-Id: Ia7276871f3c26d506dc70e1719347d020b547bfc --- diff --git a/uitest/src/com/vaadin/tests/components/form/FormTooltips.html b/uitest/src/com/vaadin/tests/components/form/FormTooltips.html deleted file mode 100644 index 5e412c3aad..0000000000 --- a/uitest/src/com/vaadin/tests/components/form/FormTooltips.html +++ /dev/null @@ -1,148 +0,0 @@ - - - - - - -FormTooltips - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FormTooltips
open/run/com.vaadin.tests.components.form.FormTooltips?restartApplication
showTooltipvaadin=runcomvaadintestscomponentsformFormTooltips::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VForm[0]/VFormLayout[0]/VFormLayout$VFormLayoutTable[0]/VTextField[0]
waitForElementPresentvaadin=runcomvaadintestscomponentsformFormTooltips::Root/VTooltip[0]/FlowPanel[0]/domChild[1]
assertTextvaadin=runcomvaadintestscomponentsformFormTooltips::Root/VTooltip[0]/FlowPanel[0]/domChild[1]Fields own tooltip
showTooltipvaadin=runcomvaadintestscomponentsformFormTooltips::
pause1000
assertElementPositionLeftvaadin=runcomvaadintestscomponentsformFormTooltips::Root/VTooltip[0]/FlowPanel[0]/domChild[1]-1000
showTooltipvaadin=runcomvaadintestscomponentsformFormTooltips::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VForm[0]/VFormLayout[0]/domChild[0]/domChild[1]/domChild[0]/domChild[0]/domChild[0]/domChild[0]
waitForElementPresentvaadin=runcomvaadintestscomponentsformFormTooltips::Root/VTooltip[0]/FlowPanel[0]/domChild[1]
assertTextvaadin=runcomvaadintestscomponentsformFormTooltips::Root/VTooltip[0]/FlowPanel[0]/domChild[1]Fields own tooltip
mouseMovevaadin=runcomvaadintestscomponentsformFormTooltips::
pause1000
assertElementPositionLeftvaadin=runcomvaadintestscomponentsformFormTooltips::Root/VTooltip[0]/FlowPanel[0]/domChild[1]-1000
showTooltipvaadin=runcomvaadintestscomponentsformFormTooltips::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VForm[0]/domChild[0]/domChild[1]
pause1000
assertElementPositionLeftvaadin=runcomvaadintestscomponentsformFormTooltips::Root/VTooltip[0]-1000
showTooltipvaadin=runcomvaadintestscomponentsformFormTooltips::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VForm[0]/domChild[0]/domChild[3]/domChild[0]/domChild[0]
pause1000
assertElementPositionLeftvaadin=runcomvaadintestscomponentsformFormTooltips::Root/VTooltip[0]-1000
mouseMovevaadin=runcomvaadintestscomponentsformFormTooltips::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VForm[0]/VFormLayout[0]/VFormLayout$VFormLayoutTable[0]/VTextField[1]
pause1000
assertElementPositionLeftvaadin=runcomvaadintestscomponentsformFormTooltips::Root/VTooltip[0]/FlowPanel[0]/domChild[1]-1000
mouseMovevaadin=runcomvaadintestscomponentsformFormTooltips::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VForm[0]/VFormLayout[0]/domChild[0]/domChild[1]/domChild[1]/domChild[0]/domChild[0]/domChild[0]
pause1000
assertElementPositionLeftvaadin=runcomvaadintestscomponentsformFormTooltips::Root/VTooltip[0]/FlowPanel[0]/domChild[1]-1000
- - diff --git a/uitest/src/com/vaadin/tests/components/form/FormTooltips.java b/uitest/src/com/vaadin/tests/components/form/FormTooltips.java index b70f66c710..6e823204e9 100644 --- a/uitest/src/com/vaadin/tests/components/form/FormTooltips.java +++ b/uitest/src/com/vaadin/tests/components/form/FormTooltips.java @@ -31,6 +31,7 @@ public class FormTooltips extends AbstractTestUI { @Override protected void setup(VaadinRequest request) { final Form form = new Form(); + form.setId("tooltipForm"); form.setDescription("Some description"); form.setItemDataSource(new BeanItem(new Person("foo", "bar", "baz", 12, Sex.MALE, null)), Arrays.asList(new String[] { diff --git a/uitest/src/com/vaadin/tests/components/form/FormTooltipsTest.java b/uitest/src/com/vaadin/tests/components/form/FormTooltipsTest.java new file mode 100644 index 0000000000..df18d4082d --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/form/FormTooltipsTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.form; + +import org.junit.Test; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.By; +import com.vaadin.testbench.elements.FormElement; +import com.vaadin.testbench.elements.TextFieldElement; +import com.vaadin.tests.tb3.TooltipTest; + +public class FormTooltipsTest extends TooltipTest { + + @Test + public void testTooltipConfiguration() throws Exception { + openTestURL(); + // first name tooltip + + WebElement fieldElement = $(FormElement.class).first() + .$(TextFieldElement.class).first(); + checkTooltip(fieldElement, "Fields own tooltip"); + + moveToRoot(); + Thread.sleep(1000); + checkTooltipNotPresent(); + + // first name caption tooltip + checkTooltip( + $(FormElement.class).first().findElement( + By.className("v-caption")), "Fields own tooltip"); + + moveToRoot(); + Thread.sleep(1000); + checkTooltipNotPresent(); + + // Form should not have a description tooltip + checkTooltip($(FormElement.class).first(), null); + + // Form error message should not have a tooltip + checkTooltip(By.className("v-form-errormessage"), null); + + // last name should have no tooltip + checkTooltip($(TextFieldElement.class).get(1), null); + + // last name caption should have no tooltip + checkTooltip( + $(FormElement.class).first() + .findElements(By.className("v-caption")).get(1), null); + } + +} \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/components/orderedlayout/TooltipOnRequiredIndicator.java b/uitest/src/com/vaadin/tests/components/orderedlayout/TooltipOnRequiredIndicator.java new file mode 100644 index 0000000000..233049b0ba --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/orderedlayout/TooltipOnRequiredIndicator.java @@ -0,0 +1,81 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +/** + * + */ +package com.vaadin.tests.components.orderedlayout; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.TextField; +import com.vaadin.ui.VerticalLayout; + +public class TooltipOnRequiredIndicator extends AbstractTestUI { + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#setup(com.vaadin.server. + * VaadinRequest) + */ + @Override + protected void setup(VaadinRequest request) { + VerticalLayout layout = new VerticalLayout(); + + TextField inVertical = new TextField(); + inVertical.setRequired(true); + inVertical.setRequiredError("Vertical layout tooltip"); + inVertical.setCaption("Vertical layout caption"); + inVertical.setId("verticalField"); + + layout.addComponent(inVertical); + addComponent(layout); + + HorizontalLayout horizontalLayout = new HorizontalLayout(); + + TextField inHorizontal = new TextField(); + inHorizontal.setRequired(true); + inHorizontal.setRequiredError("Horizontal layout tooltip"); + inHorizontal.setCaption("Horizontal layout caption"); + inHorizontal.setId("horizontalField"); + + horizontalLayout.addComponent(inHorizontal); + layout.addComponent(horizontalLayout); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#getTestDescription() + */ + @Override + protected String getTestDescription() { + return "Show tooltip for caption and required indicator"; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#getTicketNumber() + */ + @Override + protected Integer getTicketNumber() { + return 10046; + } + +} diff --git a/uitest/src/com/vaadin/tests/components/orderedlayout/TooltipOnRequiredIndicatorTest.java b/uitest/src/com/vaadin/tests/components/orderedlayout/TooltipOnRequiredIndicatorTest.java new file mode 100644 index 0000000000..815c00bfac --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/orderedlayout/TooltipOnRequiredIndicatorTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.orderedlayout; + +import org.junit.Test; +import org.openqa.selenium.By; + +import com.vaadin.testbench.elements.HorizontalLayoutElement; +import com.vaadin.testbench.elements.VerticalLayoutElement; +import com.vaadin.tests.tb3.TooltipTest; + +public class TooltipOnRequiredIndicatorTest extends TooltipTest { + + @Test + public void testTooltipOnRequiredIndicator() throws Exception { + openTestURL(); + + // gwt-uid-* are not stable across browsers etc. so need to look them up + + // caption + checkTooltip( + $(VerticalLayoutElement.class).get(1).findElement( + By.className("v-captiontext")), + "Vertical layout tooltip"); + // required indicator + checkTooltip(By.className("v-required-field-indicator"), + "Vertical layout tooltip"); + + // caption + checkTooltip( + $(HorizontalLayoutElement.class).first().findElement( + By.className("v-captiontext")), + "Horizontal layout tooltip"); + // required indicator + checkTooltip( + $(HorizontalLayoutElement.class).first().findElement( + By.className("v-required-field-indicator")), + "Horizontal layout tooltip"); + } +} \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.html b/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.html deleted file mode 100644 index 338e4a2c5b..0000000000 --- a/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.html +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - -TooltipConfiguration - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TooltipConfiguration
open/run/com.vaadin.tests.components.ui.TooltipConfiguration?restartApplication
typevaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SClose timeout0
mouseMoveAtvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SshortTooltip0,0
waitForElementPresentvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::Root/VTooltip[0]/FlowPanel[0]/domChild[1]
assertTextvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::Root/VTooltip[0]/FlowPanel[0]/domChild[1]This is a short tooltip
mouseClickvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::
assertElementPositionLeftvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::Root/VTooltip[0]/FlowPanel[0]/domChild[1]-1000
typevaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SClose timeout3000
mouseMoveAtvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::0,0
mouseMoveAtvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SshortTooltip0,0
waitForElementPresentvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::Root/VTooltip[0]/FlowPanel[0]/domChild[1]
assertTextvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::Root/VTooltip[0]/FlowPanel[0]/domChild[1]This is a short tooltip
mouseClickvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::
assertElementPresentvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::Root/VTooltip[0]/FlowPanel[0]/domChild[1]
mouseClickvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SClose timeout60,9
typevaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SClose timeout0
mouseMoveAtvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SlongTooltip0,0
waitForElementPresentvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::Root/VTooltip[0]/FlowPanel[0]/domChild[1]
assertElementWidthvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::Root/VTooltip[0]/FlowPanel[0]/domChild[1]500
mouseClickvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::
typevaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SMax width100
mouseMoveAtvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::0,0
mouseMoveAtvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::PID_SlongTooltip0,0
waitForElementPresentvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::Root/VTooltip[0]/FlowPanel[0]/domChild[1]
assertElementWidthvaadin=runcomvaadintestscomponentsuiTooltipConfiguration::Root/VTooltip[0]/FlowPanel[0]/domChild[1]100
- - diff --git a/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.java b/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.java index 4d201d2a1a..eeea91b638 100644 --- a/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.java +++ b/uitest/src/com/vaadin/tests/components/ui/TooltipConfiguration.java @@ -32,8 +32,10 @@ public class TooltipConfiguration extends AbstractTestUIWithLog { closeTimeout.addValueChangeListener(new Property.ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { - getTooltipConfiguration().setCloseTimeout( - (Integer) closeTimeout.getConvertedValue()); + if (closeTimeout.getConvertedValue() != null) { + getTooltipConfiguration().setCloseTimeout( + (Integer) closeTimeout.getConvertedValue()); + } } }); maxWidth = createIntegerTextField("Max width", @@ -41,8 +43,10 @@ public class TooltipConfiguration extends AbstractTestUIWithLog { maxWidth.addValueChangeListener(new Property.ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { - getTooltipConfiguration().setMaxWidth( - (Integer) maxWidth.getConvertedValue()); + if (maxWidth.getConvertedValue() != null) { + getTooltipConfiguration().setMaxWidth( + (Integer) maxWidth.getConvertedValue()); + } } }); openDelay = createIntegerTextField("Open delay", @@ -50,8 +54,10 @@ public class TooltipConfiguration extends AbstractTestUIWithLog { openDelay.addValueChangeListener(new Property.ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { - getTooltipConfiguration().setOpenDelay( - (Integer) openDelay.getConvertedValue()); + if (openDelay.getConvertedValue() != null) { + getTooltipConfiguration().setOpenDelay( + (Integer) openDelay.getConvertedValue()); + } } }); @@ -61,8 +67,11 @@ public class TooltipConfiguration extends AbstractTestUIWithLog { .addValueChangeListener(new Property.ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { - getTooltipConfiguration().setQuickOpenDelay( - (Integer) quickOpenDelay.getConvertedValue()); + if (quickOpenDelay.getConvertedValue() != null) { + getTooltipConfiguration().setQuickOpenDelay( + (Integer) quickOpenDelay + .getConvertedValue()); + } } }); @@ -72,8 +81,11 @@ public class TooltipConfiguration extends AbstractTestUIWithLog { .addValueChangeListener(new Property.ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { - getTooltipConfiguration().setQuickOpenTimeout( - (Integer) quickOpenTimeout.getConvertedValue()); + if (quickOpenTimeout.getConvertedValue() != null) { + getTooltipConfiguration().setQuickOpenTimeout( + (Integer) quickOpenTimeout + .getConvertedValue()); + } } }); @@ -91,6 +103,8 @@ public class TooltipConfiguration extends AbstractTestUIWithLog { tf.setConverter(Integer.class); tf.setImmediate(true); tf.setConvertedValue(initialValue); + // makes TB3 tests simpler - no "null" added when clearing a field + tf.setNullRepresentation(""); return tf; } diff --git a/uitest/src/com/vaadin/tests/components/ui/TooltipConfigurationTest.java b/uitest/src/com/vaadin/tests/components/ui/TooltipConfigurationTest.java new file mode 100644 index 0000000000..f3e7554579 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/ui/TooltipConfigurationTest.java @@ -0,0 +1,106 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.ui; + +import java.util.NoSuchElementException; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.Keys; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; + +import com.vaadin.testbench.By; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class TooltipConfigurationTest extends MultiBrowserTest { + + private org.openqa.selenium.By tooltipBy = By + .vaadin("Root/VTooltip[0]/FlowPanel[0]/domChild[1]"); + + @Test + public void testTooltipConfiguration() throws Exception { + openTestURL(); + + WebElement uiRoot = getDriver().findElement(By.vaadin("Root")); + WebElement closeTimeout = vaadinElementById("Close timeout"); + WebElement shortTooltip = vaadinElementById("shortTooltip"); + WebElement longTooltip = vaadinElementById("longTooltip"); + WebElement maxWidth = vaadinElementById("Max width"); + + selectAndType(closeTimeout, "0"); + testBenchElement(shortTooltip).showTooltip(); + waitForElementToBePresent(tooltipBy); + Assert.assertEquals("This is a short tooltip", getTooltip().getText()); + + new Actions(getDriver()).moveToElement(uiRoot, 0, 0).click().perform(); + // uiRoot.click(); + checkTooltipNotPresent(); + + selectAndType(closeTimeout, "3000"); + moveMouseToTopLeft(uiRoot); + testBenchElement(shortTooltip).showTooltip(); + waitForElementToBePresent(tooltipBy); + WebElement tooltip2 = getTooltip(); + Assert.assertEquals("This is a short tooltip", tooltip2.getText()); + + uiRoot.click(); + // assert that tooltip is present + getTooltip(); + selectAndType(closeTimeout, "0"); + testBenchElement(longTooltip).showTooltip(); + waitForElementToBePresent(tooltipBy); + Assert.assertEquals(500, getTooltip().getSize().getWidth()); + + uiRoot.click(); + selectAndType(maxWidth, "100"); + moveMouseToTopLeft(uiRoot); + testBenchElement(longTooltip).showTooltip(); + Assert.assertEquals(100, getTooltip().getSize().getWidth()); + } + + private WebElement getTooltip() { + return getDriver().findElement(tooltipBy); + } + + private void checkTooltipNotPresent() { + try { + WebElement tooltip = getTooltip(); + if (!"".equals(tooltip.getText()) + || tooltip.getLocation().getX() > -999) { + Assert.fail("Found tooltip that shouldn't be visible: " + + tooltip.getText() + " at " + tooltip.getLocation()); + } + } catch (NoSuchElementException e) { + Assert.fail("Tooltip element was removed completely, causing extra events to accessibility tools"); + } + } + + private void selectAndType(WebElement element, String value) { + // select and replace text + element.clear(); + // if null representation not set as "", need to move cursor to end and + // remove text "null" + // element.sendKeys("" + Keys.BACK_SPACE + Keys.BACK_SPACE + // + Keys.BACK_SPACE + Keys.BACK_SPACE); + element.sendKeys(value + Keys.ENTER); + } + + private void moveMouseToTopLeft(WebElement element) { + new Actions(getDriver()).moveToElement(element, 0, 0).perform(); + } + +} \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/tb3/TooltipTest.java b/uitest/src/com/vaadin/tests/tb3/TooltipTest.java new file mode 100644 index 0000000000..86ac8c1f12 --- /dev/null +++ b/uitest/src/com/vaadin/tests/tb3/TooltipTest.java @@ -0,0 +1,94 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.tests.tb3; + +import java.util.NoSuchElementException; + +import org.junit.Assert; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; + +import com.vaadin.testbench.By; + +/** + * Base class for TestBench 3+ tests that use tooltips. This class contains + * utility methods for testing tooltip use. + * + * @author Vaadin Ltd + */ +public abstract class TooltipTest extends MultiBrowserTest { + + protected void clearTooltip() throws Exception { + moveToRoot(); + Thread.sleep(500); + checkTooltipNotPresent(); + } + + protected void checkTooltip(String locator, String value) throws Exception { + checkTooltip(By.vaadin(locator), value); + } + + protected void checkTooltip(org.openqa.selenium.By by, String value) + throws Exception { + checkTooltip(getDriver().findElement(by), value); + } + + protected void checkTooltip(WebElement element, String value) + throws Exception { + testBenchElement(element).showTooltip(); + if (null != value) { + checkTooltip(value); + } else { + checkTooltipNotPresent(); + } + } + + protected void checkTooltip(String value) throws Exception { + WebElement tooltip = getTooltip(); + Assert.assertEquals(value, tooltip.getText()); + Assert.assertTrue("Tooltip should be in viewport", tooltip + .getLocation().getX() >= 0); + } + + protected void moveToRoot() { + WebElement uiRoot = getDriver().findElement(By.vaadin("Root")); + moveMouseToTopLeft(uiRoot); + } + + protected WebElement getTooltip() throws InterruptedException { + org.openqa.selenium.By tooltipBy = By.vaadin("Root/VTooltip[0]"); + return getDriver().findElement(tooltipBy); + } + + protected void checkTooltipNotPresent() throws Exception { + try { + WebElement tooltip = getTooltip(); + if (!"".equals(tooltip.getText()) + || tooltip.getLocation().getX() > -999) { + Assert.fail("Found tooltip that shouldn't be visible: " + + tooltip.getText() + " at " + tooltip.getLocation()); + } + } catch (NoSuchElementException e) { + Assert.fail("Tooltip element was removed completely, causing extra events to accessibility tools"); + } + } + + protected void moveMouseToTopLeft(WebElement element) { + new Actions(getDriver()).moveToElement(element, 0, 0).perform(); + } + +}