From: Johannes Dahlström Date: Wed, 31 Aug 2016 14:33:21 +0000 (+0300) Subject: Refactor AbstractComponentTest to extend AbstractTestUI X-Git-Tag: 8.0.0.alpha1~22 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c4a38e2502a6f7ce1cd0487fa95b0b3661de5518;p=vaadin-framework.git Refactor AbstractComponentTest to extend AbstractTestUI Change-Id: Ie614e7a7bf069ab4035cfcd35c3bc98007adfd4b --- diff --git a/uitest/src/main/java/com/vaadin/tests/Components.java b/uitest/src/main/java/com/vaadin/tests/Components.java deleted file mode 100644 index eea70f6ad1..0000000000 --- a/uitest/src/main/java/com/vaadin/tests/Components.java +++ /dev/null @@ -1,270 +0,0 @@ -package com.vaadin.tests; - -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import com.vaadin.server.ExternalResource; -import com.vaadin.server.LegacyApplication; -import com.vaadin.server.Sizeable; -import com.vaadin.shared.MouseEventDetails.MouseButton; -import com.vaadin.shared.ui.label.ContentMode; -import com.vaadin.tests.components.AbstractComponentTest; -import com.vaadin.ui.AbstractComponent; -import com.vaadin.ui.Component; -import com.vaadin.ui.Embedded; -import com.vaadin.ui.HorizontalSplitPanel; -import com.vaadin.ui.Label; -import com.vaadin.ui.LegacyWindow; -import com.vaadin.ui.VerticalLayout; -import com.vaadin.v7.data.Item; -import com.vaadin.v7.data.util.DefaultItemSorter; -import com.vaadin.v7.data.util.HierarchicalContainer; -import com.vaadin.v7.event.ItemClickEvent; -import com.vaadin.v7.event.ItemClickEvent.ItemClickListener; -import com.vaadin.v7.ui.Tree; -import com.vaadin.v7.ui.Tree.ItemStyleGenerator; - -public class Components extends LegacyApplication { - - private static final Object CAPTION = "c"; - private Map, String> tests = new HashMap<>(); - private Tree naviTree; - private HorizontalSplitPanel sp; - private LegacyWindow mainWindow; - private final Embedded applicationEmbedder = new Embedded(); - private String baseUrl; - private List> componentsWithoutTests = new ArrayList<>(); - - { - for (Class c : VaadinClasses.getBasicComponentTests()) { - String testClass = c.getSimpleName(); - tests.put((Class) c, testClass); - } - - List> componentsWithoutTest = VaadinClasses - .getComponents(); - Set availableTests = new HashSet<>(); - for (String testName : tests.values()) { - availableTests.add(testName); - } - - for (Class component : componentsWithoutTest) { - String baseName = component.getSimpleName(); - if (availableTests.contains(baseName + "es")) { - continue; - } - if (availableTests.contains(baseName + "es2")) { - continue; - } - if (availableTests.contains(baseName + "s2")) { - continue; - } - if (availableTests.contains(baseName + "s")) { - continue; - } - if (availableTests.contains(baseName + "Test")) { - continue; - } - - componentsWithoutTests.add(component); - } - - } - - class MissingTest extends AbstractComponentTest { - @Override - protected Class getTestClass() { - return null; - } - } - - @Override - public void init() { - mainWindow = new LegacyWindow(); - setTheme("tests-components"); - mainWindow.getContent().setSizeFull(); - setMainWindow(mainWindow); - sp = new HorizontalSplitPanel(); - sp.setSizeFull(); - VerticalLayout naviLayout = new VerticalLayout(); - naviLayout.addComponent(new Label( - "Click to open a test case.
Right click to open test in a new window

", - ContentMode.HTML)); - naviLayout.addComponent(createMenu()); - naviLayout.addComponent(createMissingTestsList()); - - sp.setFirstComponent(naviLayout); - sp.setSplitPosition(250, Sizeable.UNITS_PIXELS); - VerticalLayout embeddingLayout = new VerticalLayout(); - embeddingLayout.setSizeFull(); - embeddingLayout.addComponent(new Label( - "Do not use the embedded version for creating automated tests. Open the test in a new window before recording.
", - ContentMode.HTML)); - applicationEmbedder.setSizeFull(); - embeddingLayout.addComponent(applicationEmbedder); - embeddingLayout.setExpandRatio(applicationEmbedder, 1); - sp.setSecondComponent(embeddingLayout); - mainWindow.addComponent(sp); - - applicationEmbedder.setType(Embedded.TYPE_BROWSER); - baseUrl = getURL().toString().replace(getClass().getName(), "") - .replaceAll("//$", "/"); - } - - private Component createMissingTestsList() { - String missingTests = ""; - for (Class component : componentsWithoutTests) { - String cls = "missing"; - if (component.getAnnotation(Deprecated.class) != null) { - cls = "missing-deprecated"; - } - missingTests += "" - + component.getSimpleName() + "
"; - } - return new Label( - "Components without a test:
" + missingTests, - ContentMode.HTML); - } - - private Component createMenu() { - naviTree = new Tree(); - naviTree.setItemStyleGenerator(new ItemStyleGenerator() { - - @Override - public String getStyle(Tree source, Object itemId) { - Class cls = (Class) itemId; - if (!isAbstract(cls)) { - return "blue"; - } - return null; - } - }); - HierarchicalContainer hc = new HierarchicalContainer(); - naviTree.setContainerDataSource(hc); - DefaultItemSorter sorter = new DefaultItemSorter() { - @SuppressWarnings("rawtypes") - @Override - public int compare(Object o1, Object o2) { - if (o1 instanceof Class && o2 instanceof Class && o1 != null - && o2 != null) { - Class c1 = (Class) o1; - Class c2 = (Class) o2; - boolean a1 = isAbstract(c1); - boolean a2 = isAbstract(c2); - - if (a1 && !a2) { - return 1; - } else if (!a1 && a2) { - return -1; - } - - } - return super.compare(o1, o2); - } - }; - hc.setItemSorter(sorter); - naviTree.addContainerProperty(CAPTION, String.class, ""); - naviTree.setItemCaptionPropertyId(CAPTION); - for (Class cls : tests.keySet()) { - addTreeItem(cls); - } - hc.sort(new Object[] { CAPTION }, new boolean[] { true }); - naviTree.setSelectable(false); - for (Object o : naviTree.rootItemIds()) { - expandAndSetChildrenAllowed(o); - } - - naviTree.addListener(new ItemClickListener() { - - @Override - public void itemClick(ItemClickEvent event) { - Class cls = (Class) event.getItemId(); - if (!isAbstract(cls)) { - String url = baseUrl + cls.getName() - + "?restartApplication"; - if (event.getButton() == MouseButton.LEFT) { - openEmbedded(url); - naviTree.setValue(event.getItemId()); - } else if (event.getButton() == MouseButton.RIGHT) { - openInNewTab(url); - } - } - } - - }); - return naviTree; - } - - protected void openInNewTab(String url) { - getMainWindow().open(new ExternalResource(url), "_blank"); - } - - protected void openEmbedded(String url) { - applicationEmbedder.setSource(new ExternalResource(url)); - } - - private void expandAndSetChildrenAllowed(Object o) { - Collection children = naviTree.getChildren(o); - if (children == null || children.size() == 0) { - naviTree.setChildrenAllowed(o, false); - } else { - naviTree.expandItem(o); - for (Object c : children) { - expandAndSetChildrenAllowed(c); - } - } - - } - - protected boolean isAbstract(Class cls) { - return Modifier.isAbstract(cls.getModifiers()); - } - - @SuppressWarnings("unchecked") - private void addTreeItem(Class cls) { - String name = tests.get(cls); - if (name == null) { - name = cls.getSimpleName(); - } - - Class superClass = (Class) cls - .getSuperclass(); - - // This cast is needed only to make compilation through Ant work .. - if (((Class) cls) != AbstractComponentTest.class) { - addTreeItem(superClass); - } - if (naviTree.containsId(cls)) { - return; - } - - Item i = naviTree.addItem(cls); - i.getItemProperty(CAPTION).setValue(name); - naviTree.setParent(cls, superClass); - } - - protected Component createTestComponent( - Class cls) { - try { - AbstractComponentTest t = cls.newInstance(); - t.init(); - Component c = t.getMainWindow().getContent(); - t.getMainWindow().setContent(null); - return c; - } catch (InstantiationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - } - -} diff --git a/uitest/src/main/java/com/vaadin/tests/components/AbstractComponentTest.java b/uitest/src/main/java/com/vaadin/tests/components/AbstractComponentTest.java index 2c51bd132d..1e0f54bebc 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/AbstractComponentTest.java +++ b/uitest/src/main/java/com/vaadin/tests/components/AbstractComponentTest.java @@ -9,23 +9,24 @@ import java.util.Locale; import java.util.Map; import java.util.Set; +import com.vaadin.annotations.Theme; import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; import com.vaadin.event.FieldEvents.BlurNotifier; import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.event.FieldEvents.FocusListener; import com.vaadin.event.FieldEvents.FocusNotifier; -import com.vaadin.server.DefaultErrorHandler; import com.vaadin.server.Resource; import com.vaadin.server.ThemeResource; +import com.vaadin.server.VaadinRequest; import com.vaadin.tests.util.Log; import com.vaadin.tests.util.LoremIpsum; import com.vaadin.ui.AbstractComponent; -import com.vaadin.ui.Component.Focusable; import com.vaadin.ui.MenuBar; import com.vaadin.ui.MenuBar.MenuItem; import com.vaadin.ui.themes.BaseTheme; +@Theme("tests-components") public abstract class AbstractComponentTest extends AbstractComponentTestCase implements FocusListener, BlurListener { @@ -81,15 +82,14 @@ public abstract class AbstractComponentTest extends protected static final String CATEGORY_DECORATIONS = "Decorations"; @Override - protected final void setup() { - setTheme("tests-components"); + protected final void setup(VaadinRequest request) { // Create menu here so it appears before the components addComponent(createMainMenu()); getLayout().setSizeFull(); createLog(); - super.setup(); + super.setup(request); // Create menu actions and trigger default actions createActions(); @@ -747,16 +747,6 @@ public abstract class AbstractComponentTest extends super.doCommand(commandName, command, value, data); } - @Override - public void error(com.vaadin.server.ErrorEvent event) { - final Throwable throwable = DefaultErrorHandler - .findRelevantThrowable(event.getThrowable()); - - log.log("Exception occured, " + throwable.getClass().getName() + ": " - + throwable.getMessage()); - throwable.printStackTrace(); - } - @Override public void focus(FocusEvent event) { log(event.getClass().getSimpleName()); diff --git a/uitest/src/main/java/com/vaadin/tests/components/AbstractComponentTestCase.java b/uitest/src/main/java/com/vaadin/tests/components/AbstractComponentTestCase.java index bdb1ff3f83..c52e332b94 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/AbstractComponentTestCase.java +++ b/uitest/src/main/java/com/vaadin/tests/components/AbstractComponentTestCase.java @@ -9,12 +9,13 @@ import com.vaadin.data.HasRequired; import com.vaadin.server.Resource; import com.vaadin.server.ThemeResource; import com.vaadin.server.UserError; +import com.vaadin.server.VaadinRequest; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Layout.SpacingHandler; import com.vaadin.v7.ui.Field; public abstract class AbstractComponentTestCase - extends TestBase { + extends AbstractTestUI { protected static final ThemeResource ICON_16_HELP_PNG_CACHEABLE = cacheableThemeResource( "../runo/icons/16/help.png"); @@ -52,7 +53,7 @@ public abstract class AbstractComponentTestCase abstract protected void initializeComponents(); @Override - protected void setup() { + protected void setup(VaadinRequest request) { ((SpacingHandler) getLayout()).setSpacing(true); // Create Components @@ -77,136 +78,66 @@ public abstract class AbstractComponentTestCase public void execute(T c, VALUETYPE value, Object data); } - /* COMMANDS */ - - protected Command widthCommand = new Command() { - - @Override - public void execute(T t, String value, Object data) { - t.setWidth(value); - } - }; - protected Command heightCommand = new Command() { - - @Override - public void execute(T t, String value, Object data) { - t.setHeight(value); - } - }; - - protected Command enabledCommand = new Command() { - - @Override - public void execute(T c, Boolean enabled, Object data) { - c.setEnabled(enabled); - } - }; - - protected Command immediateCommand = new Command() { - - @Override - public void execute(T c, Boolean immediate, Object data) { - c.setImmediate(immediate); - } - }; - - protected Command errorIndicatorCommand = new Command() { - - @Override - public void execute(T c, Boolean enabled, Object data) { - if (enabled) { - c.setComponentError(new UserError(errorMessage)); - } else { - c.setComponentError(null); - - } - } - }; private String errorMessage = null; - protected Command errorMessageCommand = new Command() { - - @Override - public void execute(T c, String value, Object data) { - errorMessage = value; - if (c.getComponentError() != null) { - errorIndicatorCommand.execute(c, true, null); - } - - } - - }; + /* COMMANDS */ - // TODO Move to AbstractFieldTestCase - protected Command requiredCommand = new Command() { - - @Override - public void execute(T c, Boolean enabled, Object data) { - if (c instanceof HasRequired) { - ((HasRequired) c).setRequired(enabled); - } else { - throw new IllegalArgumentException(c.getClass().getName() - + " is not a field and cannot be set to required"); - } - } - }; - protected Command requiredErrorMessageCommand = new Command() { + protected Command widthCommand = (t, value, data) -> t.setWidth( + value); + protected Command heightCommand = (t, value, data) -> t + .setHeight(value); - @Override - public void execute(T c, String value, Object data) { - ((Field) c).setRequiredError(value); - } + protected Command enabledCommand = (c, enabled, data) -> c + .setEnabled(enabled); - }; + protected Command immediateCommand = (c, immediate, data) -> c + .setImmediate(immediate); - protected Command descriptionCommand = new Command() { - @Override - public void execute(T c, String value, Object data) { - c.setDescription(value); + protected Command errorIndicatorCommand = (c, enabled, + data) -> { + if (enabled) { + c.setComponentError(new UserError(errorMessage)); + } else { + c.setComponentError(null); } }; - protected Command readonlyCommand = new Command() { - - @Override - public void execute(T c, Boolean enabled, Object data) { - c.setReadOnly(enabled); + protected Command errorMessageCommand = (c, value, data) -> { + errorMessage = value; + if (c.getComponentError() != null) { + errorIndicatorCommand.execute(c, true, null); } - }; - - protected Command visibleCommand = new Command() { - @Override - public void execute(T c, Boolean enabled, Object data) { - c.setVisible(enabled); - } }; - protected Command iconCommand = new Command() { - - @Override - public void execute(T c, Resource value, Object data) { - c.setIcon(value); + // TODO Move to AbstractFieldTestCase + protected Command requiredCommand = (c, enabled, data) -> { + if (c instanceof HasRequired) { + ((HasRequired) c).setRequired(enabled); + } else { + throw new IllegalArgumentException(c.getClass().getName() + + " is not a field and cannot be set to required"); } - }; - protected Command captionCommand = new Command() { + protected Command requiredErrorMessageCommand = (c, value, + data) -> ((Field) c).setRequiredError(value); - @Override - public void execute(T c, String value, Object data) { - c.setCaption(value); - } + protected Command descriptionCommand = (c, value, data) -> c + .setDescription(value); - }; + protected Command readonlyCommand = (c, enabled, data) -> c + .setReadOnly(enabled); - protected Command localeCommand = new Command() { + protected Command visibleCommand = (c, enabled, data) -> c + .setVisible(enabled); - @Override - public void execute(T c, Locale value, Object data) { - c.setLocale(value); - } + protected Command iconCommand = (c, value, data) -> c.setIcon( + value); + protected Command captionCommand = (c, value, data) -> c + .setCaption(value); - }; + protected Command localeCommand = (c, value, data) -> c + .setLocale(value); protected void doCommand(Command command, VALUET value) { @@ -230,23 +161,14 @@ public abstract class AbstractComponentTestCase doCommand(command, value, data); } - protected Command styleNameCommand = new Command() { - @Override - public void execute(T c, String value, Object data) { - c.setStyleName(value); - } - }; + protected Command styleNameCommand = (c, value, data) -> c + .setStyleName(value); - protected Command primaryStyleNameCommand = new Command() { - @Override - public void execute(T c, String value, Object data) { - c.setPrimaryStyleName(value); - } - }; + protected Command primaryStyleNameCommand = (c, value, data) -> c + .setPrimaryStyleName(value); @Override - protected String getDescription() { + protected String getTestDescription() { return "Generic test case for " + getTestClass().getSimpleName(); } - } diff --git a/uitest/src/main/java/com/vaadin/tests/components/ComponentTestCase.java b/uitest/src/main/java/com/vaadin/tests/components/ComponentTestCase.java index 2786eb8558..86a743001b 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/ComponentTestCase.java +++ b/uitest/src/main/java/com/vaadin/tests/components/ComponentTestCase.java @@ -4,16 +4,14 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; +import com.vaadin.server.VaadinRequest; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; -import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.CheckBox; import com.vaadin.ui.Component; import com.vaadin.ui.HorizontalLayout; import com.vaadin.v7.data.Item; -import com.vaadin.v7.data.Property; -import com.vaadin.v7.data.Property.ValueChangeEvent; import com.vaadin.v7.ui.Field; import com.vaadin.v7.ui.NativeSelect; @@ -25,12 +23,12 @@ public abstract class ComponentTestCase private HorizontalLayout actionLayout; @Override - protected final void setup() { + protected final void setup(VaadinRequest request) { // Create action layout so it appears before the components actionLayout = createActionLayout(); addComponent(actionLayout); - super.setup(); + super.setup(request); // Create actions and add to layout populateActionLayout(); @@ -129,14 +127,11 @@ public abstract class ComponentTestCase Button button = new Button(caption); button.setData(Boolean.FALSE); - button.addListener(new Button.ClickListener() { - @Override - public void buttonClick(ClickEvent event) { - Button b = event.getButton(); - boolean state = (Boolean) b.getData(); - b.setData(!state); - doCommand(command, state); - } + button.addClickListener(event -> { + Button b = event.getButton(); + boolean state = (Boolean) b.getData(); + b.setData(!state); + doCommand(command, state); }); button.setId("buttonaction-" + caption); @@ -156,17 +151,12 @@ public abstract class ComponentTestCase select.addContainerProperty(VALUE, Object.class, ""); select.setItemCaptionPropertyId(CAPTION); select.setNullSelectionAllowed(false); - select.addListener(new Property.ValueChangeListener() { - - @Override - public void valueChange(ValueChangeEvent event) { - Object itemId = event.getProperty().getValue(); - Item item = select.getItem(itemId); - @SuppressWarnings("unchecked") - TYPE value = (TYPE) item.getItemProperty(VALUE).getValue(); - doCommand(command, value); - - } + select.addValueChangeListener(event -> { + Object itemId = event.getProperty().getValue(); + Item item = select.getItem(itemId); + @SuppressWarnings("unchecked") + TYPE value = (TYPE) item.getItemProperty(VALUE).getValue(); + doCommand(command, value); }); for (String itemCaption : options.keySet()) { @@ -177,7 +167,6 @@ public abstract class ComponentTestCase if (itemCaption.equals(initialValue)) { select.setValue(itemId); } - } select.setId("selectaction-" + caption); diff --git a/uitest/src/main/java/com/vaadin/tests/components/TouchDevicesTooltip.java b/uitest/src/main/java/com/vaadin/tests/components/TouchDevicesTooltip.java index 8db90bf332..24ba6c3a20 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/TouchDevicesTooltip.java +++ b/uitest/src/main/java/com/vaadin/tests/components/TouchDevicesTooltip.java @@ -62,7 +62,7 @@ public class TouchDevicesTooltip extends AbstractTestUI { } @Override - public String getDescription() { + protected String getTestDescription() { return "Unable to dismiss a tooltip on touch devices"; } -} \ No newline at end of file +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/button/ButtonHtml.java b/uitest/src/main/java/com/vaadin/tests/components/button/ButtonHtml.java index 1c3070f38b..0b1ab42667 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/button/ButtonHtml.java +++ b/uitest/src/main/java/com/vaadin/tests/components/button/ButtonHtml.java @@ -4,6 +4,12 @@ import com.vaadin.tests.components.TestBase; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; +/* + * NOTE This class is arbitrarily picked to represent a legacy application in + * MultipleServletConfigurationTest and the corresponding "Embed App 1" servlet + * configuration. The test will break if this class is refactored to extend UI + * instead of LegacyApplication. Just a friendly warning. + */ public class ButtonHtml extends TestBase { @Override diff --git a/uitest/src/main/java/com/vaadin/tests/components/button/Buttons.java b/uitest/src/main/java/com/vaadin/tests/components/button/Buttons.java index a6c4e326fe..2913c64a98 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/button/Buttons.java +++ b/uitest/src/main/java/com/vaadin/tests/components/button/Buttons.java @@ -64,7 +64,7 @@ public class Buttons extends ComponentTestCase