From 64a029571955ee52b0c416b597fb718bc0a66c46 Mon Sep 17 00:00:00 2001 From: Matti Hosio Date: Thu, 11 Dec 2014 09:05:19 +0200 Subject: Declarative support for TextField, PasswordField and TextArea (#7749) Change-Id: I41d04c55c65820f0270742468e94f47099783950 --- server/src/com/vaadin/ui/PasswordField.java | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'server/src/com/vaadin/ui/PasswordField.java') diff --git a/server/src/com/vaadin/ui/PasswordField.java b/server/src/com/vaadin/ui/PasswordField.java index 107e40c149..b842fc5569 100644 --- a/server/src/com/vaadin/ui/PasswordField.java +++ b/server/src/com/vaadin/ui/PasswordField.java @@ -15,7 +15,12 @@ */ package com.vaadin.ui; +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; + import com.vaadin.data.Property; +import com.vaadin.ui.declarative.DesignAttributeHandler; +import com.vaadin.ui.declarative.DesignContext; /** * A field that is used to enter secret text information like passwords. The @@ -76,4 +81,40 @@ public class PasswordField extends AbstractTextField { this(); setCaption(caption); } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.AbstractField#synchronizeFromDesign(org.jsoup.nodes.Element + * , com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeFromDesign(Element design, + DesignContext designContext) { + super.synchronizeFromDesign(design, designContext); + AbstractTextField def = designContext.getDefaultInstance(this + .getClass()); + Attributes attr = design.attributes(); + String value = DesignAttributeHandler.readAttribute("value", attr, + def.getValue(), String.class); + setValue(value); + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.ui.AbstractTextField#synchronizeToDesign(org.jsoup.nodes.Element + * , com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void synchronizeToDesign(Element design, DesignContext designContext) { + super.synchronizeToDesign(design, designContext); + AbstractTextField def = designContext.getDefaultInstance(this + .getClass()); + Attributes attr = design.attributes(); + DesignAttributeHandler.writeAttribute("value", attr, getValue(), + def.getValue(), String.class); + } } -- cgit v1.2.3 From 3b75f2b14833f78c00d0032cbd76b09fca058100 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Sun, 14 Dec 2014 21:34:01 +0200 Subject: SynchronizeFrom/ToDesign -> read/writeDesign (#7749) Change-Id: Ie5e420ac5d62a2aa3206051888e6dd0f8f30df11 --- server/src/com/vaadin/server/ClientConnector.java | 4 +- server/src/com/vaadin/ui/AbsoluteLayout.java | 19 +- server/src/com/vaadin/ui/AbstractComponent.java | 14 +- server/src/com/vaadin/ui/AbstractField.java | 17 +- .../src/com/vaadin/ui/AbstractOrderedLayout.java | 17 +- .../ui/AbstractSingleComponentContainer.java | 17 +- server/src/com/vaadin/ui/AbstractSplitPanel.java | 14 +- server/src/com/vaadin/ui/AbstractTextField.java | 17 +- server/src/com/vaadin/ui/Button.java | 17 +- server/src/com/vaadin/ui/Component.java | 5 +- server/src/com/vaadin/ui/CssLayout.java | 17 +- server/src/com/vaadin/ui/Label.java | 17 +- server/src/com/vaadin/ui/Panel.java | 9 +- server/src/com/vaadin/ui/PasswordField.java | 17 +- server/src/com/vaadin/ui/TabSheet.java | 17 +- server/src/com/vaadin/ui/TextArea.java | 17 +- server/src/com/vaadin/ui/TextField.java | 17 +- .../com/vaadin/ui/declarative/DesignContext.java | 25 +- .../component/absolutelayout/TestReadDesign.java | 110 +++++++++ .../absolutelayout/TestSynchronizeFromDesign.java | 110 --------- .../absolutelayout/TestSynchronizeToDesign.java | 100 -------- .../component/absolutelayout/TestWriteDesign.java | 100 ++++++++ .../abstractcomponent/TestReadDesign.java | 256 ++++++++++++++++++++ .../TestSynchronizeFromDesign.java | 256 -------------------- .../abstractcomponent/TestSynchronizeToDesign.java | 266 --------------------- .../abstractcomponent/TestWriteDesign.java | 266 +++++++++++++++++++++ .../component/abstractfield/TestReadDesign.java | 71 ++++++ .../abstractfield/TestSynchronizeFromDesign.java | 71 ------ .../abstractfield/TestSynchronizeToDesign.java | 77 ------ .../component/abstractfield/TestWriteDesign.java | 77 ++++++ .../abstractorderedlayout/TestReadDesign.java | 119 +++++++++ .../TestSynchronizeFromDesign.java | 119 --------- .../TestSynchronizeToDesign.java | 148 ------------ .../abstractorderedlayout/TestWriteDesign.java | 148 ++++++++++++ .../abstractsplitpanel/TestReadDesign.java | 173 ++++++++++++++ .../TestSynchronizeFromDesign.java | 173 -------------- .../TestSynchronizeToDesign.java | 151 ------------ .../abstractsplitpanel/TestWriteDesign.java | 151 ++++++++++++ .../abstracttextfield/TestReadDesign.java | 73 ++++++ .../TestSynchronizeFromDesign.java | 73 ------ .../abstracttextfield/TestSynchronizeToDesign.java | 73 ------ .../abstracttextfield/TestWriteDesign.java | 73 ++++++ .../server/component/button/TestReadDesign.java | 129 ++++++++++ .../button/TestSynchronizeFromDesign.java | 129 ---------- .../component/button/TestSynchronizeToDesign.java | 103 -------- .../server/component/button/TestWriteDesign.java | 103 ++++++++ .../server/component/csslayout/TestReadDesign.java | 74 ++++++ .../csslayout/TestSynchronizeFromDesign.java | 74 ------ .../csslayout/TestSynchronizeToDesign.java | 78 ------ .../component/csslayout/TestWriteDesign.java | 78 ++++++ .../server/component/label/TestReadDesign.java | 102 ++++++++ .../component/label/TestSynchronizeFromDesign.java | 102 -------- .../component/label/TestSynchronizeToDesign.java | 126 ---------- .../server/component/label/TestWriteDesign.java | 126 ++++++++++ .../server/component/panel/TestReadDesign.java | 98 ++++++++ .../component/panel/TestSynchronizeFromDesign.java | 98 -------- .../component/panel/TestSynchronizeToDesign.java | 71 ------ .../server/component/panel/TestWriteDesign.java | 71 ++++++ .../server/component/tabsheet/TestReadDesign.java | 136 +++++++++++ .../tabsheet/TestSynchronizeFromDesign.java | 137 ----------- .../tabsheet/TestSynchronizeToDesign.java | 109 --------- .../server/component/tabsheet/TestWriteDesign.java | 109 +++++++++ .../server/component/textarea/TestReadDesign.java | 59 +++++ .../textarea/TestSynchronizeFromDesign.java | 59 ----- .../textarea/TestSynchronizeToDesign.java | 60 ----- .../server/component/textarea/TestWriteDesign.java | 60 +++++ .../server/component/textfield/TestReadDesign.java | 59 +++++ .../textfield/TestSynchronizeFromDesign.java | 59 ----- .../textfield/TestSynchronizeToDesign.java | 59 ----- .../component/textfield/TestWriteDesign.java | 59 +++++ 70 files changed, 2997 insertions(+), 3041 deletions(-) create mode 100644 server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractfield/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractfield/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractfield/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractfield/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/button/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/button/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/csslayout/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/csslayout/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/label/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/label/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/panel/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/panel/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/panel/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/panel/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/tabsheet/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/tabsheet/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/tabsheet/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/tabsheet/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/textarea/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/textarea/TestWriteDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/textfield/TestReadDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeFromDesign.java delete mode 100644 server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeToDesign.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/textfield/TestWriteDesign.java (limited to 'server/src/com/vaadin/ui/PasswordField.java') diff --git a/server/src/com/vaadin/server/ClientConnector.java b/server/src/com/vaadin/server/ClientConnector.java index 50ce2754cb..b784aa5d35 100644 --- a/server/src/com/vaadin/server/ClientConnector.java +++ b/server/src/com/vaadin/server/ClientConnector.java @@ -26,6 +26,7 @@ import com.vaadin.shared.Connector; import com.vaadin.shared.communication.SharedState; import com.vaadin.ui.UI; import com.vaadin.util.ReflectTools; + import elemental.json.JsonObject; /** @@ -277,9 +278,8 @@ public interface ClientConnector extends Connector { * . * * @return a JSON object with the encoded connector state - * if the state can not be encoded */ - public JsonObject encodeState() ; + public JsonObject encodeState(); /** * Handle a request directed to this connector. This can be used by diff --git a/server/src/com/vaadin/ui/AbsoluteLayout.java b/server/src/com/vaadin/ui/AbsoluteLayout.java index 7af22ba5c6..3e1e7bfbdb 100644 --- a/server/src/com/vaadin/ui/AbsoluteLayout.java +++ b/server/src/com/vaadin/ui/AbsoluteLayout.java @@ -677,15 +677,13 @@ public class AbsoluteLayout extends AbstractLayout implements /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes - * .Node, com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Node, + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { + public void readDesign(Element design, DesignContext designContext) { // process default attributes - super.synchronizeFromDesign(design, designContext); + super.readDesign(design, designContext); // remove current children removeAllComponents(); // handle children @@ -716,19 +714,18 @@ public class AbsoluteLayout extends AbstractLayout implements /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Node, + * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Node, * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { - super.synchronizeToDesign(design, designContext); + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); // handle children Element designElement = design; for (Component child : this) { Element childNode = designContext.createNode(child); designElement.appendChild(childNode); - child.synchronizeToDesign(childNode, designContext); + child.writeDesign(childNode, designContext); // handle position ComponentPosition position = getPosition(child); writePositionAttribute(childNode, ATTR_TOP, position.getTopUnits() diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java index 808b70386f..3c68fde9b2 100644 --- a/server/src/com/vaadin/ui/AbstractComponent.java +++ b/server/src/com/vaadin/ui/AbstractComponent.java @@ -914,13 +914,11 @@ public abstract class AbstractComponent extends AbstractClientConnector /* * (non-Javadoc) * - * @see - * com.vaadin.ui.Component#synchronizeFromDesign(org.jsoup.nodes.Element, + * @see com.vaadin.ui.Component#readDesign(org.jsoup.nodes.Element, * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { + public void readDesign(Element design, DesignContext designContext) { Attributes attr = design.attributes(); AbstractComponent def = designContext.getDefaultInstance(this .getClass()); @@ -1190,8 +1188,8 @@ public abstract class AbstractComponent extends AbstractClientConnector /** * Returns a collection of attributes that should not be handled by the * basic implementation of the {@link synhronizeFromDesign} and - * {@link synchronizeToDesign} methods. Typically these are handled in a - * custom way in the overridden versions of the above methods + * {@link writeDesign} methods. Typically these are handled in a custom way + * in the overridden versions of the above methods * * @return the collection of attributes that are not handled by the basic * implementation @@ -1208,11 +1206,11 @@ public abstract class AbstractComponent extends AbstractClientConnector /* * (non-Javadoc) * - * @see com.vaadin.ui.Component#synchronizeToDesign(org.jsoup.nodes.Element, + * @see com.vaadin.ui.Component#writeDesign(org.jsoup.nodes.Element, * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { + public void writeDesign(Element design, DesignContext designContext) { // clear element contents DesignAttributeHandler.clearElement(design); AbstractComponent def = designContext.getDefaultInstance(this diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java index fc3129d1ba..ba518000d6 100644 --- a/server/src/com/vaadin/ui/AbstractField.java +++ b/server/src/com/vaadin/ui/AbstractField.java @@ -1761,14 +1761,12 @@ public abstract class AbstractField extends AbstractComponent implements /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes - * .Element, com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Element, + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { - super.synchronizeFromDesign(design, designContext); + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); AbstractField def = designContext.getDefaultInstance(this.getClass()); Attributes attr = design.attributes(); boolean readOnly = DesignAttributeHandler.readAttribute("readonly", @@ -1798,13 +1796,12 @@ public abstract class AbstractField extends AbstractComponent implements /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Element + * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Element * , com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { - super.synchronizeToDesign(design, designContext); + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); AbstractField def = designContext.getDefaultInstance(this.getClass()); Attributes attr = design.attributes(); // handle readonly diff --git a/server/src/com/vaadin/ui/AbstractOrderedLayout.java b/server/src/com/vaadin/ui/AbstractOrderedLayout.java index d2a302cff5..e99641a9ab 100644 --- a/server/src/com/vaadin/ui/AbstractOrderedLayout.java +++ b/server/src/com/vaadin/ui/AbstractOrderedLayout.java @@ -470,15 +470,13 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes - * .Element, com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Element, + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { + public void readDesign(Element design, DesignContext designContext) { // process default attributes - super.synchronizeFromDesign(design, designContext); + super.readDesign(design, designContext); // remove current children removeAllComponents(); // handle margin @@ -535,14 +533,13 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Element + * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Element * , com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { + public void writeDesign(Element design, DesignContext designContext) { // synchronize default attributes - super.synchronizeToDesign(design, designContext); + super.writeDesign(design, designContext); // handle margin AbstractOrderedLayout def = designContext.getDefaultInstance(this .getClass()); diff --git a/server/src/com/vaadin/ui/AbstractSingleComponentContainer.java b/server/src/com/vaadin/ui/AbstractSingleComponentContainer.java index 6c8d4fcde2..330fcfeae1 100644 --- a/server/src/com/vaadin/ui/AbstractSingleComponentContainer.java +++ b/server/src/com/vaadin/ui/AbstractSingleComponentContainer.java @@ -281,15 +281,13 @@ public abstract class AbstractSingleComponentContainer extends /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes - * .Element, com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Element, + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { + public void readDesign(Element design, DesignContext designContext) { // process default attributes - super.synchronizeFromDesign(design, designContext); + super.readDesign(design, designContext); // handle child element, checking that the design specifies at most one // child int childCount = design.children().size(); @@ -309,14 +307,13 @@ public abstract class AbstractSingleComponentContainer extends /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Element + * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Element * , com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { + public void writeDesign(Element design, DesignContext designContext) { // synchronize default attributes (also clears children and attributes) - super.synchronizeToDesign(design, designContext); + super.writeDesign(design, designContext); // handle child component Component child = getContent(); if (child != null) { diff --git a/server/src/com/vaadin/ui/AbstractSplitPanel.java b/server/src/com/vaadin/ui/AbstractSplitPanel.java index 1400bcf092..66d01084d1 100644 --- a/server/src/com/vaadin/ui/AbstractSplitPanel.java +++ b/server/src/com/vaadin/ui/AbstractSplitPanel.java @@ -557,15 +557,13 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer { /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes - * .Element, com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Element, + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { + public void readDesign(Element design, DesignContext designContext) { // handle default attributes - super.synchronizeFromDesign(design, designContext); + super.readDesign(design, designContext); // handle custom attributes, use default values if no explicit value // set AbstractSplitPanel def = designContext.getDefaultInstance(this @@ -634,9 +632,9 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer { } @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { + public void writeDesign(Element design, DesignContext designContext) { // handle default attributes (also clears children and attributes) - super.synchronizeToDesign(design, designContext); + super.writeDesign(design, designContext); // handle custom attributes (write only if a value is not the // default value) AbstractSplitPanel def = designContext.getDefaultInstance(this diff --git a/server/src/com/vaadin/ui/AbstractTextField.java b/server/src/com/vaadin/ui/AbstractTextField.java index 2de8555b26..79492ecf5e 100644 --- a/server/src/com/vaadin/ui/AbstractTextField.java +++ b/server/src/com/vaadin/ui/AbstractTextField.java @@ -766,14 +766,12 @@ public abstract class AbstractTextField extends AbstractField implements /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractField#synchronizeFromDesign(org.jsoup.nodes.Element - * , com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractField#readDesign(org.jsoup.nodes.Element , + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { - super.synchronizeFromDesign(design, designContext); + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); AbstractTextField def = designContext.getDefaultInstance(this .getClass()); Attributes attr = design.attributes(); @@ -800,13 +798,12 @@ public abstract class AbstractTextField extends AbstractField implements /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractField#synchronizeToDesign(org.jsoup.nodes.Element, + * @see com.vaadin.ui.AbstractField#writeDesign(org.jsoup.nodes.Element, * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { - super.synchronizeToDesign(design, designContext); + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); AbstractTextField def = designContext.getDefaultInstance(this .getClass()); Attributes attr = design.attributes(); diff --git a/server/src/com/vaadin/ui/Button.java b/server/src/com/vaadin/ui/Button.java index 9a754c24d9..2d0a0cf8da 100644 --- a/server/src/com/vaadin/ui/Button.java +++ b/server/src/com/vaadin/ui/Button.java @@ -669,14 +669,12 @@ public class Button extends AbstractComponent implements /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes - * .Element, com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Element, + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { - super.synchronizeFromDesign(design, designContext); + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); Button def = designContext.getDefaultInstance(this.getClass()); Attributes attr = design.attributes(); String content = design.html(); @@ -718,13 +716,12 @@ public class Button extends AbstractComponent implements /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Element + * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Element * , com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { - super.synchronizeToDesign(design, designContext); + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); Attributes attr = design.attributes(); Button def = designContext.getDefaultInstance(this.getClass()); String content = getCaption(); diff --git a/server/src/com/vaadin/ui/Component.java b/server/src/com/vaadin/ui/Component.java index 024c3eb433..78fb643ba9 100644 --- a/server/src/com/vaadin/ui/Component.java +++ b/server/src/com/vaadin/ui/Component.java @@ -744,8 +744,7 @@ public interface Component extends ClientConnector, Sizeable, Serializable { * @param designContext * The DesignContext instance used for parsing the design */ - public void synchronizeFromDesign(Element design, - DesignContext designContext); + public void readDesign(Element design, DesignContext designContext); /** * Update the given design based on the component state. The component is @@ -761,7 +760,7 @@ public interface Component extends ClientConnector, Sizeable, Serializable { * The design as HTML to update with the current state * @param designContext */ - public void synchronizeToDesign(Element design, DesignContext designContext); + public void writeDesign(Element design, DesignContext designContext); /* Component event framework */ diff --git a/server/src/com/vaadin/ui/CssLayout.java b/server/src/com/vaadin/ui/CssLayout.java index 0b4e9cda7b..5c7471522d 100644 --- a/server/src/com/vaadin/ui/CssLayout.java +++ b/server/src/com/vaadin/ui/CssLayout.java @@ -364,15 +364,13 @@ public class CssLayout extends AbstractLayout implements LayoutClickNotifier { /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes - * .Element, com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Element, + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { + public void readDesign(Element design, DesignContext designContext) { // process default attributes - super.synchronizeFromDesign(design, designContext); + super.readDesign(design, designContext); // remove current children removeAllComponents(); // handle children @@ -385,14 +383,13 @@ public class CssLayout extends AbstractLayout implements LayoutClickNotifier { /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Element + * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Element * , com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { + public void writeDesign(Element design, DesignContext designContext) { // synchronize default attributes - super.synchronizeToDesign(design, designContext); + super.writeDesign(design, designContext); // handle children Element designElement = design; for (Component child : this) { diff --git a/server/src/com/vaadin/ui/Label.java b/server/src/com/vaadin/ui/Label.java index f882940fe6..fc18c374b4 100644 --- a/server/src/com/vaadin/ui/Label.java +++ b/server/src/com/vaadin/ui/Label.java @@ -578,14 +578,12 @@ public class Label extends AbstractComponent implements Property, /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes - * .Element, com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Element, + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { - super.synchronizeFromDesign(design, designContext); + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); String innerHtml = design.html(); if (innerHtml != null && !"".equals(innerHtml)) { setValue(innerHtml); @@ -613,13 +611,12 @@ public class Label extends AbstractComponent implements Property, /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Element + * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Element * , com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { - super.synchronizeToDesign(design, designContext); + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); String content = getValue(); if (content != null) { design.html(getValue()); diff --git a/server/src/com/vaadin/ui/Panel.java b/server/src/com/vaadin/ui/Panel.java index 46ad801a6d..5112a732ac 100644 --- a/server/src/com/vaadin/ui/Panel.java +++ b/server/src/com/vaadin/ui/Panel.java @@ -345,9 +345,8 @@ public class Panel extends AbstractSingleComponentContainer implements } @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { - super.synchronizeFromDesign(design, designContext); + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); // handle tabindex Panel def = designContext.getDefaultInstance(this.getClass()); int tabIndex = DesignAttributeHandler.readAttribute("tabindex", @@ -364,8 +363,8 @@ public class Panel extends AbstractSingleComponentContainer implements } @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { - super.synchronizeToDesign(design, designContext); + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); // handle tabindex Panel def = designContext.getDefaultInstance(this.getClass()); DesignAttributeHandler.writeAttribute("tabindex", design.attributes(), diff --git a/server/src/com/vaadin/ui/PasswordField.java b/server/src/com/vaadin/ui/PasswordField.java index b842fc5569..5f27eb59c9 100644 --- a/server/src/com/vaadin/ui/PasswordField.java +++ b/server/src/com/vaadin/ui/PasswordField.java @@ -85,14 +85,12 @@ public class PasswordField extends AbstractTextField { /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractField#synchronizeFromDesign(org.jsoup.nodes.Element - * , com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractField#readDesign(org.jsoup.nodes.Element , + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { - super.synchronizeFromDesign(design, designContext); + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); AbstractTextField def = designContext.getDefaultInstance(this .getClass()); Attributes attr = design.attributes(); @@ -104,13 +102,12 @@ public class PasswordField extends AbstractTextField { /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractTextField#synchronizeToDesign(org.jsoup.nodes.Element + * @see com.vaadin.ui.AbstractTextField#writeDesign(org.jsoup.nodes.Element * , com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { - super.synchronizeToDesign(design, designContext); + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); AbstractTextField def = designContext.getDefaultInstance(this .getClass()); Attributes attr = design.attributes(); diff --git a/server/src/com/vaadin/ui/TabSheet.java b/server/src/com/vaadin/ui/TabSheet.java index 121ba89e06..94bcb80279 100644 --- a/server/src/com/vaadin/ui/TabSheet.java +++ b/server/src/com/vaadin/ui/TabSheet.java @@ -1458,14 +1458,12 @@ public class TabSheet extends AbstractComponentContainer implements Focusable, /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes - * .Element, com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Element, + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { - super.synchronizeFromDesign(design, designContext); + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); Attributes attr = design.attributes(); TabSheet def = designContext.getDefaultInstance(this.getClass()); // handle tab index @@ -1594,13 +1592,12 @@ public class TabSheet extends AbstractComponentContainer implements Focusable, /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Element + * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Element * , com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { - super.synchronizeToDesign(design, designContext); + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); TabSheet def = designContext.getDefaultInstance(this.getClass()); Attributes attr = design.attributes(); // handle tab index diff --git a/server/src/com/vaadin/ui/TextArea.java b/server/src/com/vaadin/ui/TextArea.java index 70b9268228..c8103f9c5b 100644 --- a/server/src/com/vaadin/ui/TextArea.java +++ b/server/src/com/vaadin/ui/TextArea.java @@ -139,27 +139,24 @@ public class TextArea extends AbstractTextField { /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractField#synchronizeFromDesign(org.jsoup.nodes.Element - * , com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractField#readDesign(org.jsoup.nodes.Element , + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { - super.synchronizeFromDesign(design, designContext); + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); setValue(design.html()); } /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractTextField#synchronizeToDesign(org.jsoup.nodes.Element + * @see com.vaadin.ui.AbstractTextField#writeDesign(org.jsoup.nodes.Element * , com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { - super.synchronizeToDesign(design, designContext); + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); design.html(getValue()); } } diff --git a/server/src/com/vaadin/ui/TextField.java b/server/src/com/vaadin/ui/TextField.java index bb855c7b90..03bbfd2d8f 100644 --- a/server/src/com/vaadin/ui/TextField.java +++ b/server/src/com/vaadin/ui/TextField.java @@ -107,14 +107,12 @@ public class TextField extends AbstractTextField { /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractField#synchronizeFromDesign(org.jsoup.nodes.Element - * , com.vaadin.ui.declarative.DesignContext) + * @see com.vaadin.ui.AbstractField#readDesign(org.jsoup.nodes.Element , + * com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeFromDesign(Element design, - DesignContext designContext) { - super.synchronizeFromDesign(design, designContext); + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); AbstractTextField def = designContext.getDefaultInstance(this .getClass()); Attributes attr = design.attributes(); @@ -126,13 +124,12 @@ public class TextField extends AbstractTextField { /* * (non-Javadoc) * - * @see - * com.vaadin.ui.AbstractTextField#synchronizeToDesign(org.jsoup.nodes.Element + * @see com.vaadin.ui.AbstractTextField#writeDesign(org.jsoup.nodes.Element * , com.vaadin.ui.declarative.DesignContext) */ @Override - public void synchronizeToDesign(Element design, DesignContext designContext) { - super.synchronizeToDesign(design, designContext); + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); AbstractTextField def = designContext.getDefaultInstance(this .getClass()); Attributes attr = design.attributes(); diff --git a/server/src/com/vaadin/ui/declarative/DesignContext.java b/server/src/com/vaadin/ui/declarative/DesignContext.java index e84faa31ca..b196c84b02 100644 --- a/server/src/com/vaadin/ui/declarative/DesignContext.java +++ b/server/src/com/vaadin/ui/declarative/DesignContext.java @@ -331,9 +331,9 @@ public class DesignContext implements Serializable { /** * Creates an html tree node corresponding to the given element. Also - * initializes its attributes by calling synchronizeToDesign. As a result of - * the synchronizeToDesign() call, this method creates the entire subtree - * rooted at the returned Node. + * initializes its attributes by calling writeDesign. As a result of the + * writeDesign() call, this method creates the entire subtree rooted at the + * returned Node. * * @param childComponent * The component with state that is synchronized in to the node @@ -353,9 +353,9 @@ public class DesignContext implements Serializable { String className = classNameToElementName(componentClass .getSimpleName()); Element newElement = doc.createElement(prefix + className); - childComponent.synchronizeToDesign(newElement, this); + childComponent.writeDesign(newElement, this); // Handle the local id. Global id and caption should have been taken - // care of by synchronizeToDesign. + // care of by writeDesign. String localId = componentToLocalId.get(childComponent); if (localId != null) { newElement.attr(LOCAL_ID_ATTRIBUTE, localId); @@ -391,8 +391,8 @@ public class DesignContext implements Serializable { /** * Creates a Component corresponding to the given html node. Also calls - * synchronizeFromDesign() for the created node, in effect creating the - * entire component hierarchy rooted at the returned component. + * readDesign() for the created node, in effect creating the entire + * component hierarchy rooted at the returned component. * * @param componentDesign * The html tree node containing the description of the component @@ -408,11 +408,10 @@ public class DesignContext implements Serializable { } /** - * Calls synchronizeFromDesign() for the given component and passes the - * given component design as a parameter. This creates the entire component - * hierarchy rooted at the given component. Also registers the componentid, - * localId and caption of the given component and all its children to the - * context + * Calls readDesign() for the given component and passes the given component + * design as a parameter. This creates the entire component hierarchy rooted + * at the given component. Also registers the componentid, localId and + * caption of the given component and all its children to the context * * * @param component @@ -422,7 +421,7 @@ public class DesignContext implements Serializable { */ public void synchronizeAndRegister(Component component, Element componentDesign) { - component.synchronizeFromDesign(componentDesign, this); + component.readDesign(componentDesign, this); // Get the ids and the caption of the component and store them in the // maps of this design context. org.jsoup.nodes.Attributes attributes = componentDesign.attributes(); diff --git a/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestReadDesign.java new file mode 100644 index 0000000000..5f9674e554 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestReadDesign.java @@ -0,0 +1,110 @@ +/* + * Copyright 2000-2014 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.server.component.absolutelayout; + +import java.util.Iterator; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.server.Sizeable; +import com.vaadin.ui.AbsoluteLayout; +import com.vaadin.ui.AbsoluteLayout.ComponentPosition; +import com.vaadin.ui.Component; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for reading AbsoluteLayout from design + * + * @since + * @author Vaadin Ltd + */ +public class TestReadDesign extends TestCase { + + private AbsoluteLayout root; + + @Override + public void setUp() throws Exception { + super.setUp(); + root = createLayout(); + } + + public void testAttributes() { + assertEquals("test-layout", root.getCaption()); + Iterator children = root.iterator(); + assertEquals("test-label", children.next().getCaption()); + assertEquals("test-button", children.next().getCaption()); + } + + public void testTopLeftPosition() { + ComponentPosition position = root.getPosition(root.iterator().next()); + assertEquals(Sizeable.Unit.PIXELS, position.getTopUnits()); + assertEquals(100.0f, position.getTopValue()); + assertEquals(Sizeable.Unit.PERCENTAGE, position.getLeftUnits()); + assertEquals(50.0f, position.getLeftValue()); + } + + public void testBottomRightPosition() { + Iterator children = root.iterator(); + children.next(); + ComponentPosition position = root.getPosition(children.next()); + assertEquals(Sizeable.Unit.PIXELS, position.getBottomUnits()); + assertEquals(100.0f, position.getBottomValue()); + assertEquals(Sizeable.Unit.PERCENTAGE, position.getRightUnits()); + assertEquals(50.0f, position.getRightValue()); + } + + public void testZIndex() { + ComponentPosition position = root.getPosition(root.iterator().next()); + assertEquals(2, position.getZIndex()); + } + + private AbsoluteLayout createLayout() { + DesignContext ctx = new DesignContext(); + Element design = createDesign(); + Component child = ctx.createChild(design); + return (AbsoluteLayout) child; + } + + private Element createDesign() { + + Attributes rootAttributes = new Attributes(); + rootAttributes.put("caption", "test-layout"); + Element node = new Element(Tag.valueOf("v-absolute-layout"), "", + rootAttributes); + + Attributes firstChildAttributes = new Attributes(); + firstChildAttributes.put("caption", "test-label"); + firstChildAttributes.put(":top", "100px"); + firstChildAttributes.put(":left", "50%"); + firstChildAttributes.put(":z-index", "2"); + Element firstChild = new Element(Tag.valueOf("v-label"), "", + firstChildAttributes); + node.appendChild(firstChild); + + Attributes secondChildAttributes = new Attributes(); + secondChildAttributes.put(":bottom", "100px"); + secondChildAttributes.put(":right", "50%"); + Element secondChild = new Element(Tag.valueOf("v-button"), "", + secondChildAttributes); + secondChild.html("test-button"); + node.appendChild(secondChild); + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeFromDesign.java deleted file mode 100644 index ce054007a6..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.absolutelayout; - -import java.util.Iterator; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.server.Sizeable; -import com.vaadin.ui.AbsoluteLayout; -import com.vaadin.ui.AbsoluteLayout.ComponentPosition; -import com.vaadin.ui.Component; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for reading AbsoluteLayout from design - * - * @since - * @author Vaadin Ltd - */ -public class TestSynchronizeFromDesign extends TestCase { - - private AbsoluteLayout root; - - @Override - public void setUp() throws Exception { - super.setUp(); - root = createLayout(); - } - - public void testAttributes() { - assertEquals("test-layout", root.getCaption()); - Iterator children = root.iterator(); - assertEquals("test-label", children.next().getCaption()); - assertEquals("test-button", children.next().getCaption()); - } - - public void testTopLeftPosition() { - ComponentPosition position = root.getPosition(root.iterator().next()); - assertEquals(Sizeable.Unit.PIXELS, position.getTopUnits()); - assertEquals(100.0f, position.getTopValue()); - assertEquals(Sizeable.Unit.PERCENTAGE, position.getLeftUnits()); - assertEquals(50.0f, position.getLeftValue()); - } - - public void testBottomRightPosition() { - Iterator children = root.iterator(); - children.next(); - ComponentPosition position = root.getPosition(children.next()); - assertEquals(Sizeable.Unit.PIXELS, position.getBottomUnits()); - assertEquals(100.0f, position.getBottomValue()); - assertEquals(Sizeable.Unit.PERCENTAGE, position.getRightUnits()); - assertEquals(50.0f, position.getRightValue()); - } - - public void testZIndex() { - ComponentPosition position = root.getPosition(root.iterator().next()); - assertEquals(2, position.getZIndex()); - } - - private AbsoluteLayout createLayout() { - DesignContext ctx = new DesignContext(); - Element design = createDesign(); - Component child = ctx.createChild(design); - return (AbsoluteLayout) child; - } - - private Element createDesign() { - - Attributes rootAttributes = new Attributes(); - rootAttributes.put("caption", "test-layout"); - Element node = new Element(Tag.valueOf("v-absolute-layout"), "", - rootAttributes); - - Attributes firstChildAttributes = new Attributes(); - firstChildAttributes.put("caption", "test-label"); - firstChildAttributes.put(":top", "100px"); - firstChildAttributes.put(":left", "50%"); - firstChildAttributes.put(":z-index", "2"); - Element firstChild = new Element(Tag.valueOf("v-label"), "", - firstChildAttributes); - node.appendChild(firstChild); - - Attributes secondChildAttributes = new Attributes(); - secondChildAttributes.put(":bottom", "100px"); - secondChildAttributes.put(":right", "50%"); - Element secondChild = new Element(Tag.valueOf("v-button"), "", - secondChildAttributes); - secondChild.html("test-button"); - node.appendChild(secondChild); - return node; - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeToDesign.java deleted file mode 100644 index eb61d13684..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestSynchronizeToDesign.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.absolutelayout; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.AbsoluteLayout; -import com.vaadin.ui.Label; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for writing AbsoluteLayout to design - * - * @since - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - - public void testSynchronizeEmptyLayout() { - AbsoluteLayout layout = createTestLayout(); - layout.removeAllComponents(); - Element design = createDesign(); - layout.synchronizeToDesign(design, createDesignContext()); - assertEquals(0, design.childNodes().size()); - assertEquals("changed-caption", design.attr("caption")); - } - - public void testSynchronizeLayoutWithChildren() { - AbsoluteLayout layout = createTestLayout(); - Element design = createDesign(); - layout.synchronizeToDesign(design, createDesignContext()); - assertEquals(2, design.childNodes().size()); - assertEquals("v-label", ((Element) design.childNode(0)).tagName()); - assertEquals("v-label", ((Element) design.childNode(1)).tagName()); - } - - public void testSynchronizePosition() { - AbsoluteLayout layout = createTestLayout(); - Element design = createDesign(); - layout.synchronizeToDesign(design, createDesignContext()); - Attributes attributes = design.childNode(0).attributes(); - assertEquals("50px", attributes.get(":top")); - assertEquals("50%", attributes.get(":left")); - assertEquals("2", attributes.get(":z-index")); - attributes = design.childNode(1).attributes(); - assertEquals("50px", attributes.get(":bottom")); - assertEquals("50%", attributes.get(":right")); - } - - private AbsoluteLayout createTestLayout() { - AbsoluteLayout layout = new AbsoluteLayout(); - layout.setCaption("changed-caption"); - layout.addComponent(new Label("test-label"), - "top:50px;left:50%;z-index:2"); - layout.addComponent(new Label("test-label-2"), - "bottom:50px;right:50%;z-index:3"); - return layout; - } - - private Element createDesign() { - // make sure that the design node has old content that should be removed - Attributes rootAttributes = new Attributes(); - rootAttributes.put("caption", "test-layout"); - Element node = new Element(Tag.valueOf("v-absolute-layout"), "", - rootAttributes); - Attributes firstChildAttributes = new Attributes(); - firstChildAttributes.put("caption", "test-label"); - Element firstChild = new Element(Tag.valueOf("v-label"), "", - firstChildAttributes); - node.appendChild(firstChild); - - Attributes secondChildAttributes = new Attributes(); - secondChildAttributes.put("caption", "test-button"); - Element secondChild = new Element(Tag.valueOf("v-button"), "", - secondChildAttributes); - node.appendChild(secondChild); - return node; - } - - private DesignContext createDesignContext() { - return new DesignContext(); - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestWriteDesign.java new file mode 100644 index 0000000000..5f42b1fa81 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/absolutelayout/TestWriteDesign.java @@ -0,0 +1,100 @@ +/* + * Copyright 2000-2014 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.server.component.absolutelayout; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.AbsoluteLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing AbsoluteLayout to design + * + * @since + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + + public void testSynchronizeEmptyLayout() { + AbsoluteLayout layout = createTestLayout(); + layout.removeAllComponents(); + Element design = createDesign(); + layout.writeDesign(design, createDesignContext()); + assertEquals(0, design.childNodes().size()); + assertEquals("changed-caption", design.attr("caption")); + } + + public void testSynchronizeLayoutWithChildren() { + AbsoluteLayout layout = createTestLayout(); + Element design = createDesign(); + layout.writeDesign(design, createDesignContext()); + assertEquals(2, design.childNodes().size()); + assertEquals("v-label", ((Element) design.childNode(0)).tagName()); + assertEquals("v-label", ((Element) design.childNode(1)).tagName()); + } + + public void testSynchronizePosition() { + AbsoluteLayout layout = createTestLayout(); + Element design = createDesign(); + layout.writeDesign(design, createDesignContext()); + Attributes attributes = design.childNode(0).attributes(); + assertEquals("50px", attributes.get(":top")); + assertEquals("50%", attributes.get(":left")); + assertEquals("2", attributes.get(":z-index")); + attributes = design.childNode(1).attributes(); + assertEquals("50px", attributes.get(":bottom")); + assertEquals("50%", attributes.get(":right")); + } + + private AbsoluteLayout createTestLayout() { + AbsoluteLayout layout = new AbsoluteLayout(); + layout.setCaption("changed-caption"); + layout.addComponent(new Label("test-label"), + "top:50px;left:50%;z-index:2"); + layout.addComponent(new Label("test-label-2"), + "bottom:50px;right:50%;z-index:3"); + return layout; + } + + private Element createDesign() { + // make sure that the design node has old content that should be removed + Attributes rootAttributes = new Attributes(); + rootAttributes.put("caption", "test-layout"); + Element node = new Element(Tag.valueOf("v-absolute-layout"), "", + rootAttributes); + Attributes firstChildAttributes = new Attributes(); + firstChildAttributes.put("caption", "test-label"); + Element firstChild = new Element(Tag.valueOf("v-label"), "", + firstChildAttributes); + node.appendChild(firstChild); + + Attributes secondChildAttributes = new Attributes(); + secondChildAttributes.put("caption", "test-button"); + Element secondChild = new Element(Tag.valueOf("v-button"), "", + secondChildAttributes); + node.appendChild(secondChild); + return node; + } + + private DesignContext createDesignContext() { + return new DesignContext(); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestReadDesign.java new file mode 100644 index 0000000000..760f1ca422 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestReadDesign.java @@ -0,0 +1,256 @@ +/* + * Copyright 2000-2014 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.server.component.abstractcomponent; + +import java.lang.reflect.Field; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.server.ExternalResource; +import com.vaadin.server.FileResource; +import com.vaadin.server.Responsive; +import com.vaadin.server.ThemeResource; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.Label; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for reading the attributes of the AbstractComponent from design + * + * @author Vaadin Ltd + */ +public class TestReadDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testSynchronizeId() { + Element design = createDesign("id", "testId"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals("testId", component.getId()); + } + + public void testSynchronizePrimaryStyleName() { + Element design = createDesign("primary-style-name", "test-style"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals("test-style", component.getPrimaryStyleName()); + } + + public void testSynchronizeCaption() { + Element design = createDesign("caption", "test-caption"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals("test-caption", component.getCaption()); + } + + public void testSynchronizeLocale() { + Element design = createDesign("locale", "fi_FI"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals("fi", component.getLocale().getLanguage()); + assertEquals("FI", component.getLocale().getCountry()); + } + + public void testSynchronizeExternalIcon() { + Element design = createDesign("icon", "http://example.com/example.gif"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertTrue("Incorrect resource type returned", component.getIcon() + .getClass().isAssignableFrom(ExternalResource.class)); + assertEquals("http://example.com/example.gif", + ((ExternalResource) component.getIcon()).getURL()); + } + + public void testSynchronizeThemeIcon() { + Element design = createDesign("icon", "theme://example.gif"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertTrue("Incorrect resource type returned", component.getIcon() + .getClass().isAssignableFrom(ThemeResource.class)); + } + + public void testSynchronizeFileResource() { + Element design = createDesign("icon", "img/example.gif"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertTrue("Incorrect resource type returned", component.getIcon() + .getClass().isAssignableFrom(FileResource.class)); + } + + public void testSynchronizeImmediate() { + Element design = createDesign("immediate", "true"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals(true, component.isImmediate()); + assertEquals(Boolean.TRUE, getExplicitImmediate(component)); + // Synchronize with a design having no immediate attribute - + // explicitImmediate should then be null. + design = createDesign("description", "test-description"); + component.readDesign(design, ctx); + assertEquals(null, getExplicitImmediate(component)); + // Synchronize with a design having immediate = false + design = createDesign("immediate", "false"); + component.readDesign(design, ctx); + assertEquals(false, component.isImmediate()); + assertEquals(Boolean.FALSE, getExplicitImmediate(component)); + // Synchronize with a design having immediate = "" - should correspond + // to + // true. + design = createDesign("immediate", ""); + component.readDesign(design, ctx); + assertEquals(true, component.isImmediate()); + assertEquals(Boolean.TRUE, getExplicitImmediate(component)); + } + + public void testSynchronizeDescription() { + Element design = createDesign("description", "test-description"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals("test-description", component.getDescription()); + } + + public void testSynchronizeComponentError() { + Element design = createDesign("error", "
test-error
"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals("
test-error
", component.getComponentError() + .getFormattedHtmlMessage()); + } + + public void testSynchronizeSizeFull() { + Element design = createDesign("size-full", ""); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals(100, component.getWidth(), 0.1f); + assertEquals(100, component.getHeight(), 0.1f); + } + + public void testSynchronizeSizeAuto() { + Element design = createDesign("size-auto", ""); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals(-1, component.getWidth(), 0.1f); + assertEquals(-1, component.getHeight(), 0.1f); + } + + public void testSynchronizeHeightFull() { + Element design = createDesign("height-full", ""); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals(100, component.getHeight(), 0.1f); + } + + public void testSynchronizeHeightAuto() { + Element design = createDesign("height-auto", ""); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals(-1, component.getHeight(), 0.1f); + } + + public void testSynchronizeWidthFull() { + Element design = createDesign("width-full", ""); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals(100, component.getWidth(), 0.1f); + } + + public void testSynchronizeWidthAuto() { + Element design = createDesign("width-auto", ""); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals(-1, component.getWidth(), 0.1f); + } + + public void testSynchronizeWidth() { + Element design = createDesign("width", "12px"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals(12, component.getWidth(), 0.1f); + assertEquals(com.vaadin.server.Sizeable.Unit.PIXELS, + component.getWidthUnits()); + } + + public void testSynchronizeHeight() { + Element design = createDesign("height", "12px"); + AbstractComponent component = getComponent(); + component.readDesign(design, ctx); + assertEquals(12, component.getHeight(), 0.1f); + assertEquals(com.vaadin.server.Sizeable.Unit.PIXELS, + component.getHeightUnits()); + } + + public void testSynchronizeNotResponsive() { + AbstractComponent component = getComponent(); + Responsive.makeResponsive(component); + Element design = createDesign("responsive", "false"); + component.readDesign(design, ctx); + assertEquals("Component should not have extensions", 0, component + .getExtensions().size()); + } + + public void testSynchronizeResponsive() { + AbstractComponent component = getComponent(); + Element design = createDesign("responsive", ""); + component.readDesign(design, ctx); + assertEquals("Component should have one extension", 1, component + .getExtensions().size()); + assertTrue("Extension should be responsive", component.getExtensions() + .iterator().next() instanceof Responsive); + } + + public void testSynchronizeAlreadyResponsive() { + AbstractComponent component = getComponent(); + Responsive.makeResponsive(component); + Element design = createDesign("responsive", ""); + component.readDesign(design, ctx); + assertEquals("Component should have only one extension", 1, component + .getExtensions().size()); + } + + private AbstractComponent getComponent() { + return new Label(); + } + + private Element createDesign(String key, String value) { + Attributes attributes = new Attributes(); + attributes.put(key, value); + Element node = new Element(Tag.valueOf("v-label"), "", attributes); + return node; + } + + private Boolean getExplicitImmediate(AbstractComponent component) { + try { + Field immediate = AbstractComponent.class + .getDeclaredField("explicitImmediateValue"); + immediate.setAccessible(true); + return (Boolean) immediate.get(component); + } catch (Exception e) { + throw new RuntimeException( + "Getting the field explicitImmediateValue failed."); + } + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeFromDesign.java deleted file mode 100644 index df6fb47bf2..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.abstractcomponent; - -import java.lang.reflect.Field; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.server.ExternalResource; -import com.vaadin.server.FileResource; -import com.vaadin.server.Responsive; -import com.vaadin.server.ThemeResource; -import com.vaadin.ui.AbstractComponent; -import com.vaadin.ui.Label; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for reading the attributes of the AbstractComponent from design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeFromDesign extends TestCase { - - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - public void testSynchronizeId() { - Element design = createDesign("id", "testId"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals("testId", component.getId()); - } - - public void testSynchronizePrimaryStyleName() { - Element design = createDesign("primary-style-name", "test-style"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals("test-style", component.getPrimaryStyleName()); - } - - public void testSynchronizeCaption() { - Element design = createDesign("caption", "test-caption"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals("test-caption", component.getCaption()); - } - - public void testSynchronizeLocale() { - Element design = createDesign("locale", "fi_FI"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals("fi", component.getLocale().getLanguage()); - assertEquals("FI", component.getLocale().getCountry()); - } - - public void testSynchronizeExternalIcon() { - Element design = createDesign("icon", "http://example.com/example.gif"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertTrue("Incorrect resource type returned", component.getIcon() - .getClass().isAssignableFrom(ExternalResource.class)); - assertEquals("http://example.com/example.gif", - ((ExternalResource) component.getIcon()).getURL()); - } - - public void testSynchronizeThemeIcon() { - Element design = createDesign("icon", "theme://example.gif"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertTrue("Incorrect resource type returned", component.getIcon() - .getClass().isAssignableFrom(ThemeResource.class)); - } - - public void testSynchronizeFileResource() { - Element design = createDesign("icon", "img/example.gif"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertTrue("Incorrect resource type returned", component.getIcon() - .getClass().isAssignableFrom(FileResource.class)); - } - - public void testSynchronizeImmediate() { - Element design = createDesign("immediate", "true"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals(true, component.isImmediate()); - assertEquals(Boolean.TRUE, getExplicitImmediate(component)); - // Synchronize with a design having no immediate attribute - - // explicitImmediate should then be null. - design = createDesign("description", "test-description"); - component.synchronizeFromDesign(design, ctx); - assertEquals(null, getExplicitImmediate(component)); - // Synchronize with a design having immediate = false - design = createDesign("immediate", "false"); - component.synchronizeFromDesign(design, ctx); - assertEquals(false, component.isImmediate()); - assertEquals(Boolean.FALSE, getExplicitImmediate(component)); - // Synchronize with a design having immediate = "" - should correspond - // to - // true. - design = createDesign("immediate", ""); - component.synchronizeFromDesign(design, ctx); - assertEquals(true, component.isImmediate()); - assertEquals(Boolean.TRUE, getExplicitImmediate(component)); - } - - public void testSynchronizeDescription() { - Element design = createDesign("description", "test-description"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals("test-description", component.getDescription()); - } - - public void testSynchronizeComponentError() { - Element design = createDesign("error", "
test-error
"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals("
test-error
", component.getComponentError() - .getFormattedHtmlMessage()); - } - - public void testSynchronizeSizeFull() { - Element design = createDesign("size-full", ""); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals(100, component.getWidth(), 0.1f); - assertEquals(100, component.getHeight(), 0.1f); - } - - public void testSynchronizeSizeAuto() { - Element design = createDesign("size-auto", ""); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals(-1, component.getWidth(), 0.1f); - assertEquals(-1, component.getHeight(), 0.1f); - } - - public void testSynchronizeHeightFull() { - Element design = createDesign("height-full", ""); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals(100, component.getHeight(), 0.1f); - } - - public void testSynchronizeHeightAuto() { - Element design = createDesign("height-auto", ""); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals(-1, component.getHeight(), 0.1f); - } - - public void testSynchronizeWidthFull() { - Element design = createDesign("width-full", ""); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals(100, component.getWidth(), 0.1f); - } - - public void testSynchronizeWidthAuto() { - Element design = createDesign("width-auto", ""); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals(-1, component.getWidth(), 0.1f); - } - - public void testSynchronizeWidth() { - Element design = createDesign("width", "12px"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals(12, component.getWidth(), 0.1f); - assertEquals(com.vaadin.server.Sizeable.Unit.PIXELS, - component.getWidthUnits()); - } - - public void testSynchronizeHeight() { - Element design = createDesign("height", "12px"); - AbstractComponent component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals(12, component.getHeight(), 0.1f); - assertEquals(com.vaadin.server.Sizeable.Unit.PIXELS, - component.getHeightUnits()); - } - - public void testSynchronizeNotResponsive() { - AbstractComponent component = getComponent(); - Responsive.makeResponsive(component); - Element design = createDesign("responsive", "false"); - component.synchronizeFromDesign(design, ctx); - assertEquals("Component should not have extensions", 0, component - .getExtensions().size()); - } - - public void testSynchronizeResponsive() { - AbstractComponent component = getComponent(); - Element design = createDesign("responsive", ""); - component.synchronizeFromDesign(design, ctx); - assertEquals("Component should have one extension", 1, component - .getExtensions().size()); - assertTrue("Extension should be responsive", component.getExtensions() - .iterator().next() instanceof Responsive); - } - - public void testSynchronizeAlreadyResponsive() { - AbstractComponent component = getComponent(); - Responsive.makeResponsive(component); - Element design = createDesign("responsive", ""); - component.synchronizeFromDesign(design, ctx); - assertEquals("Component should have only one extension", 1, component - .getExtensions().size()); - } - - private AbstractComponent getComponent() { - return new Label(); - } - - private Element createDesign(String key, String value) { - Attributes attributes = new Attributes(); - attributes.put(key, value); - Element node = new Element(Tag.valueOf("v-label"), "", attributes); - return node; - } - - private Boolean getExplicitImmediate(AbstractComponent component) { - try { - Field immediate = AbstractComponent.class - .getDeclaredField("explicitImmediateValue"); - immediate.setAccessible(true); - return (Boolean) immediate.get(component); - } catch (Exception e) { - throw new RuntimeException( - "Getting the field explicitImmediateValue failed."); - } - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java deleted file mode 100644 index 349598ec8f..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestSynchronizeToDesign.java +++ /dev/null @@ -1,266 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.abstractcomponent; - -import java.io.File; -import java.util.Locale; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.server.AbstractErrorMessage.ContentMode; -import com.vaadin.server.ErrorMessage.ErrorLevel; -import com.vaadin.server.ExternalResource; -import com.vaadin.server.FileResource; -import com.vaadin.server.Responsive; -import com.vaadin.server.ThemeResource; -import com.vaadin.server.UserError; -import com.vaadin.ui.AbstractComponent; -import com.vaadin.ui.Button; -import com.vaadin.ui.HorizontalSplitPanel; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for writing the attributes of the AbstractComponent to design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - public void testSynchronizeId() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setId("testId"); - component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertEquals("testId", design.attr("id")); - } - - public void testSynchronizePrimaryStyleName() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setPrimaryStyleName("test-style"); - component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertEquals("test-style", design.attr("primary-style-name")); - } - - public void testSynchronizeCaption() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setCaption("test-caption"); - component.synchronizeToDesign(design, ctx); - // We only changed the caption, which is not - // an attribute. - assertEquals(0, design.attributes().size()); - assertEquals("test-caption", design.html()); - } - - public void testSynchronizeLocale() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setLocale(new Locale("fi", "FI")); - component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertEquals("fi_FI", design.attr("locale")); - } - - public void testSynchronizeExternalIcon() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component - .setIcon(new ExternalResource("http://example.com/example.gif")); - component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertEquals("http://example.com/example.gif", design.attr("icon")); - } - - public void testSynchronizeThemeIcon() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setIcon(new ThemeResource("example.gif")); - component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertEquals("theme://example.gif", design.attr("icon")); - } - - public void testSynchronizeFileResource() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setIcon(new FileResource(new File("img/example.gif"))); - component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertEquals("img/example.gif", design.attr("icon")); - } - - public void testSynchronizeImmediate() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - // no immediate attribute should be written before setting immediate to - // some value - component.synchronizeToDesign(design, ctx); - assertFalse(design.hasAttr("immediate")); - component.setImmediate(true); - component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertEquals("true", design.attr("immediate")); - } - - public void testSynchronizeDescription() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setDescription("test-description"); - component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertEquals("test-description", design.attr("description")); - } - - public void testSynchronizeComponentError() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setComponentError(new UserError("
test-error
", - ContentMode.HTML, ErrorLevel.ERROR)); - component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertEquals("
test-error
", design.attr("error")); - } - - public void testSynchronizeSizeFull() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setSizeFull(); - component.synchronizeToDesign(design, ctx); - // there should be only size full - assertEquals(1, design.attributes().size()); - assertEquals("true", design.attr("size-full")); - } - - public void testSynchronizeSizeAuto() { - Element design = createDesign(); - AbstractComponent component = getPanel(); - component.setSizeUndefined(); - component.synchronizeToDesign(design, ctx); - // there should be only size auto - assertEquals(1, design.attributes().size()); - assertEquals("true", design.attr("size-auto")); - } - - public void testSynchronizeHeightFull() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setHeight("100%"); - component.setWidth("20px"); - component.synchronizeToDesign(design, ctx); - assertEquals("true", design.attr("height-full")); - } - - public void testSynchronizeHeightAuto() { - Element design = createDesign(); - // we need to have default height of 100% -> use split panel - AbstractComponent component = getPanel(); - component.setHeight(null); - component.setWidth("20px"); - component.synchronizeToDesign(design, ctx); - assertEquals("true", design.attr("height-auto")); - } - - public void testSynchronizeWidthFull() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setHeight("20px"); - component.setWidth("100%"); - component.synchronizeToDesign(design, ctx); - assertEquals("true", design.attr("width-full")); - } - - public void testSynchronizeWidthAuto() { - Element design = createDesign(); - // need to get label, otherwise the default would be auto - AbstractComponent component = getPanel(); - component.setHeight("20px"); - component.setWidth(null); - component.synchronizeToDesign(design, ctx); - assertEquals("true", design.attr("width-auto")); - } - - public void testSynchronizeWidth() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setHeight("20px"); - component.setWidth("70%"); - component.synchronizeToDesign(design, ctx); - assertEquals("70%", design.attr("width")); - } - - public void testSynchronizeHeight() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - component.setHeight("20px"); - component.setWidth("70%"); - component.synchronizeToDesign(design, ctx); - assertEquals("20px", design.attr("height")); - } - - public void testSynchronizeResponsive() { - Element design = createDesign(); - AbstractComponent component = getComponent(); - Responsive.makeResponsive(component); - component.synchronizeToDesign(design, ctx); - assertTrue("Design attributes should have key 'responsive'", design - .attributes().hasKey("responsive")); - assertFalse("Responsive attribute should not be 'false'", - design.attr("responsive").equalsIgnoreCase("false")); - } - - private AbstractComponent getComponent() { - Button button = new Button(); - button.setHtmlContentAllowed(true); - return button; - } - - private AbstractComponent getPanel() { - return new HorizontalSplitPanel(); - } - - private Element createDesign() { - Attributes attr = new Attributes(); - attr.put("should_be_removed", "foo"); - Element node = new Element(Tag.valueOf("v-button"), "", attr); - Element child = new Element(Tag.valueOf("to-be-removed"), "foo", attr); - node.appendChild(child); - return node; - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestWriteDesign.java new file mode 100644 index 0000000000..44c0e0b063 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractcomponent/TestWriteDesign.java @@ -0,0 +1,266 @@ +/* + * Copyright 2000-2014 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.server.component.abstractcomponent; + +import java.io.File; +import java.util.Locale; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.server.AbstractErrorMessage.ContentMode; +import com.vaadin.server.ErrorMessage.ErrorLevel; +import com.vaadin.server.ExternalResource; +import com.vaadin.server.FileResource; +import com.vaadin.server.Responsive; +import com.vaadin.server.ThemeResource; +import com.vaadin.server.UserError; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.Button; +import com.vaadin.ui.HorizontalSplitPanel; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing the attributes of the AbstractComponent to design + * + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testSynchronizeId() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setId("testId"); + component.writeDesign(design, ctx); + // we only changed one of the attributes, others are at default values + assertEquals(1, design.attributes().size()); + assertEquals("testId", design.attr("id")); + } + + public void testSynchronizePrimaryStyleName() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setPrimaryStyleName("test-style"); + component.writeDesign(design, ctx); + // we only changed one of the attributes, others are at default values + assertEquals(1, design.attributes().size()); + assertEquals("test-style", design.attr("primary-style-name")); + } + + public void testSynchronizeCaption() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setCaption("test-caption"); + component.writeDesign(design, ctx); + // We only changed the caption, which is not + // an attribute. + assertEquals(0, design.attributes().size()); + assertEquals("test-caption", design.html()); + } + + public void testSynchronizeLocale() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setLocale(new Locale("fi", "FI")); + component.writeDesign(design, ctx); + // we only changed one of the attributes, others are at default values + assertEquals(1, design.attributes().size()); + assertEquals("fi_FI", design.attr("locale")); + } + + public void testSynchronizeExternalIcon() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component + .setIcon(new ExternalResource("http://example.com/example.gif")); + component.writeDesign(design, ctx); + // we only changed one of the attributes, others are at default values + assertEquals(1, design.attributes().size()); + assertEquals("http://example.com/example.gif", design.attr("icon")); + } + + public void testSynchronizeThemeIcon() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setIcon(new ThemeResource("example.gif")); + component.writeDesign(design, ctx); + // we only changed one of the attributes, others are at default values + assertEquals(1, design.attributes().size()); + assertEquals("theme://example.gif", design.attr("icon")); + } + + public void testSynchronizeFileResource() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setIcon(new FileResource(new File("img/example.gif"))); + component.writeDesign(design, ctx); + // we only changed one of the attributes, others are at default values + assertEquals(1, design.attributes().size()); + assertEquals("img/example.gif", design.attr("icon")); + } + + public void testSynchronizeImmediate() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + // no immediate attribute should be written before setting immediate to + // some value + component.writeDesign(design, ctx); + assertFalse(design.hasAttr("immediate")); + component.setImmediate(true); + component.writeDesign(design, ctx); + // we only changed one of the attributes, others are at default values + assertEquals(1, design.attributes().size()); + assertEquals("true", design.attr("immediate")); + } + + public void testSynchronizeDescription() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setDescription("test-description"); + component.writeDesign(design, ctx); + // we only changed one of the attributes, others are at default values + assertEquals(1, design.attributes().size()); + assertEquals("test-description", design.attr("description")); + } + + public void testSynchronizeComponentError() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setComponentError(new UserError("
test-error
", + ContentMode.HTML, ErrorLevel.ERROR)); + component.writeDesign(design, ctx); + // we only changed one of the attributes, others are at default values + assertEquals(1, design.attributes().size()); + assertEquals("
test-error
", design.attr("error")); + } + + public void testSynchronizeSizeFull() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setSizeFull(); + component.writeDesign(design, ctx); + // there should be only size full + assertEquals(1, design.attributes().size()); + assertEquals("true", design.attr("size-full")); + } + + public void testSynchronizeSizeAuto() { + Element design = createDesign(); + AbstractComponent component = getPanel(); + component.setSizeUndefined(); + component.writeDesign(design, ctx); + // there should be only size auto + assertEquals(1, design.attributes().size()); + assertEquals("true", design.attr("size-auto")); + } + + public void testSynchronizeHeightFull() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setHeight("100%"); + component.setWidth("20px"); + component.writeDesign(design, ctx); + assertEquals("true", design.attr("height-full")); + } + + public void testSynchronizeHeightAuto() { + Element design = createDesign(); + // we need to have default height of 100% -> use split panel + AbstractComponent component = getPanel(); + component.setHeight(null); + component.setWidth("20px"); + component.writeDesign(design, ctx); + assertEquals("true", design.attr("height-auto")); + } + + public void testSynchronizeWidthFull() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setHeight("20px"); + component.setWidth("100%"); + component.writeDesign(design, ctx); + assertEquals("true", design.attr("width-full")); + } + + public void testSynchronizeWidthAuto() { + Element design = createDesign(); + // need to get label, otherwise the default would be auto + AbstractComponent component = getPanel(); + component.setHeight("20px"); + component.setWidth(null); + component.writeDesign(design, ctx); + assertEquals("true", design.attr("width-auto")); + } + + public void testSynchronizeWidth() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setHeight("20px"); + component.setWidth("70%"); + component.writeDesign(design, ctx); + assertEquals("70%", design.attr("width")); + } + + public void testSynchronizeHeight() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + component.setHeight("20px"); + component.setWidth("70%"); + component.writeDesign(design, ctx); + assertEquals("20px", design.attr("height")); + } + + public void testSynchronizeResponsive() { + Element design = createDesign(); + AbstractComponent component = getComponent(); + Responsive.makeResponsive(component); + component.writeDesign(design, ctx); + assertTrue("Design attributes should have key 'responsive'", design + .attributes().hasKey("responsive")); + assertFalse("Responsive attribute should not be 'false'", + design.attr("responsive").equalsIgnoreCase("false")); + } + + private AbstractComponent getComponent() { + Button button = new Button(); + button.setHtmlContentAllowed(true); + return button; + } + + private AbstractComponent getPanel() { + return new HorizontalSplitPanel(); + } + + private Element createDesign() { + Attributes attr = new Attributes(); + attr.put("should_be_removed", "foo"); + Element node = new Element(Tag.valueOf("v-button"), "", attr); + Element child = new Element(Tag.valueOf("to-be-removed"), "foo", attr); + node.appendChild(child); + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestReadDesign.java new file mode 100644 index 0000000000..dd0e629199 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestReadDesign.java @@ -0,0 +1,71 @@ +/* + * Copyright 2000-2014 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.server.component.abstractfield; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.AbstractField; +import com.vaadin.ui.TextField; +import com.vaadin.ui.declarative.DesignContext; + +/** + * + * Test case for reading the attributes of the AbstractField from design + * + * @author Vaadin Ltd + */ +public class TestReadDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testSynchronizeReadOnly() { + Element design = createDesign("readonly", ""); + AbstractField component = getComponent(); + component.readDesign(design, ctx); + assertEquals(true, component.isReadOnly()); + design = createDesign("readonly", "false"); + component.readDesign(design, ctx); + assertEquals(false, component.isReadOnly()); + } + + public void testSynchronizeTabIndex() { + Element design = createDesign("tabindex", "2"); + AbstractField component = getComponent(); + component.readDesign(design, ctx); + assertEquals("Tab index must be 2", 2, component.getTabIndex()); + } + + private AbstractField getComponent() { + return new TextField(); + } + + private Element createDesign(String key, String value) { + Attributes attributes = new Attributes(); + attributes.put(key, value); + Element node = new Element(Tag.valueOf("v-text-field"), "", attributes); + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestSynchronizeFromDesign.java deleted file mode 100644 index 6a2c194978..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.abstractfield; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.AbstractField; -import com.vaadin.ui.TextField; -import com.vaadin.ui.declarative.DesignContext; - -/** - * - * Test case for reading the attributes of the AbstractField from design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeFromDesign extends TestCase { - - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - public void testSynchronizeReadOnly() { - Element design = createDesign("readonly", ""); - AbstractField component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals(true, component.isReadOnly()); - design = createDesign("readonly", "false"); - component.synchronizeFromDesign(design, ctx); - assertEquals(false, component.isReadOnly()); - } - - public void testSynchronizeTabIndex() { - Element design = createDesign("tabindex", "2"); - AbstractField component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals("Tab index must be 2", 2, component.getTabIndex()); - } - - private AbstractField getComponent() { - return new TextField(); - } - - private Element createDesign(String key, String value) { - Attributes attributes = new Attributes(); - attributes.put(key, value); - Element node = new Element(Tag.valueOf("v-text-field"), "", attributes); - return node; - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestSynchronizeToDesign.java deleted file mode 100644 index 26f64f9199..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestSynchronizeToDesign.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.abstractfield; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.data.util.ObjectProperty; -import com.vaadin.ui.AbstractField; -import com.vaadin.ui.TextField; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for writing the attributes of the AbstractField to design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - public void testSynchronizeReadOnly() { - Element design = createDesign(); - AbstractField component = getComponent(); - component.setReadOnly(true); - component.synchronizeToDesign(design, ctx); - // we only changed one of the attributes, others are at default values - assertEquals(1, design.attributes().size()); - assertTrue("Design must contain readonly", design.hasAttr("readonly")); - assertTrue("Readonly must be true", design.attr("readonly").equals("") - || design.attr("readonly").equals("true")); - } - - public void testSynchronizeModelReadOnly() { - Element design = createDesign(); - AbstractField component = getComponent(); - ObjectProperty property = new ObjectProperty("test"); - property.setReadOnly(true); - component.setPropertyDataSource(property); - component.synchronizeToDesign(design, ctx); - // make sure that property readonly is not written to design - assertFalse("Design must not contain readonly", - design.hasAttr("readonly")); - } - - private AbstractField getComponent() { - return new TextField(); - } - - private Element createDesign() { - Attributes attr = new Attributes(); - attr.put("should_be_removed", "foo"); - return new Element(Tag.valueOf("v-text-field"), "", attr); - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestWriteDesign.java new file mode 100644 index 0000000000..969713abe6 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractfield/TestWriteDesign.java @@ -0,0 +1,77 @@ +/* + * Copyright 2000-2014 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.server.component.abstractfield; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.data.util.ObjectProperty; +import com.vaadin.ui.AbstractField; +import com.vaadin.ui.TextField; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing the attributes of the AbstractField to design + * + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testSynchronizeReadOnly() { + Element design = createDesign(); + AbstractField component = getComponent(); + component.setReadOnly(true); + component.writeDesign(design, ctx); + // we only changed one of the attributes, others are at default values + assertEquals(1, design.attributes().size()); + assertTrue("Design must contain readonly", design.hasAttr("readonly")); + assertTrue("Readonly must be true", design.attr("readonly").equals("") + || design.attr("readonly").equals("true")); + } + + public void testSynchronizeModelReadOnly() { + Element design = createDesign(); + AbstractField component = getComponent(); + ObjectProperty property = new ObjectProperty("test"); + property.setReadOnly(true); + component.setPropertyDataSource(property); + component.writeDesign(design, ctx); + // make sure that property readonly is not written to design + assertFalse("Design must not contain readonly", + design.hasAttr("readonly")); + } + + private AbstractField getComponent() { + return new TextField(); + } + + private Element createDesign() { + Attributes attr = new Attributes(); + attr.put("should_be_removed", "foo"); + return new Element(Tag.valueOf("v-text-field"), "", attr); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestReadDesign.java new file mode 100644 index 0000000000..f52a04496a --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestReadDesign.java @@ -0,0 +1,119 @@ +/* + * Copyright 2000-2014 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.server.component.abstractorderedlayout; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.Alignment; +import com.vaadin.ui.Component; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case from reading AbstractOrdered layouts from design + * + * @since + * @author Vaadin Ltd + */ +public class TestReadDesign extends TestCase { + + public void testChildCount() { + VerticalLayout root = createLayout(0f, false); + assertEquals(2, root.getComponentCount()); + } + + public void testMargin() { + VerticalLayout root = createLayout(0f, true); + assertTrue(root.getMargin().getBitMask() != 0); + root = createLayout(0f, false); + assertTrue(root.getMargin().getBitMask() == 0); + } + + public void testAttributes() { + VerticalLayout root = createLayout(0f, false); + assertEquals("test-layout", root.getCaption()); + assertEquals("test-label", root.getComponent(0).getCaption()); + assertEquals("test-button", root.getComponent(1).getCaption()); + } + + public void testExpandRatio() { + VerticalLayout root = createLayout(1f, false); + assertEquals(1f, root.getExpandRatio(root.getComponent(0))); + assertEquals(1f, root.getExpandRatio(root.getComponent(1))); + + root = createLayout(0f, false); + assertEquals(0f, root.getExpandRatio(root.getComponent(0))); + assertEquals(0f, root.getExpandRatio(root.getComponent(1))); + } + + public void testAlignment() { + VerticalLayout root = createLayout(0f, false, ":top", ":left"); + assertEquals(Alignment.TOP_LEFT, + root.getComponentAlignment(root.getComponent(0))); + root = createLayout(0f, false, ":middle", ":center"); + assertEquals(Alignment.MIDDLE_CENTER, + root.getComponentAlignment(root.getComponent(0))); + root = createLayout(0f, false, ":bottom", ":right"); + assertEquals(Alignment.BOTTOM_RIGHT, + root.getComponentAlignment(root.getComponent(0))); + + } + + private VerticalLayout createLayout(float expandRatio, boolean margin, + String... alignments) { + DesignContext ctx = new DesignContext(); + Element design = createDesign(expandRatio, margin, alignments); + Component child = ctx.createChild(design); + return (VerticalLayout) child; + } + + private Element createDesign(float expandRatio, boolean margin, + String... alignments) { + + Attributes rootAttributes = new Attributes(); + rootAttributes.put("caption", "test-layout"); + if (margin) { + rootAttributes.put("margin", ""); + } + Element node = new Element(Tag.valueOf("v-vertical-layout"), "", + rootAttributes); + + Attributes firstChildAttributes = new Attributes(); + firstChildAttributes.put("caption", "test-label"); + firstChildAttributes.put(":expand", String.valueOf(expandRatio)); + for (String alignment : alignments) { + firstChildAttributes.put(alignment, ""); + } + Element firstChild = new Element(Tag.valueOf("v-label"), "", + firstChildAttributes); + node.appendChild(firstChild); + + Attributes secondChildAttributes = new Attributes(); + secondChildAttributes.put(":expand", String.valueOf(expandRatio)); + for (String alignment : alignments) { + secondChildAttributes.put(alignment, ""); + } + Element secondChild = new Element(Tag.valueOf("v-button"), "", + secondChildAttributes); + secondChild.html("test-button"); + node.appendChild(secondChild); + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeFromDesign.java deleted file mode 100644 index 2b3d4cbdac..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.abstractorderedlayout; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.Alignment; -import com.vaadin.ui.Component; -import com.vaadin.ui.VerticalLayout; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case from reading AbstractOrdered layouts from design - * - * @since - * @author Vaadin Ltd - */ -public class TestSynchronizeFromDesign extends TestCase { - - public void testChildCount() { - VerticalLayout root = createLayout(0f, false); - assertEquals(2, root.getComponentCount()); - } - - public void testMargin() { - VerticalLayout root = createLayout(0f, true); - assertTrue(root.getMargin().getBitMask() != 0); - root = createLayout(0f, false); - assertTrue(root.getMargin().getBitMask() == 0); - } - - public void testAttributes() { - VerticalLayout root = createLayout(0f, false); - assertEquals("test-layout", root.getCaption()); - assertEquals("test-label", root.getComponent(0).getCaption()); - assertEquals("test-button", root.getComponent(1).getCaption()); - } - - public void testExpandRatio() { - VerticalLayout root = createLayout(1f, false); - assertEquals(1f, root.getExpandRatio(root.getComponent(0))); - assertEquals(1f, root.getExpandRatio(root.getComponent(1))); - - root = createLayout(0f, false); - assertEquals(0f, root.getExpandRatio(root.getComponent(0))); - assertEquals(0f, root.getExpandRatio(root.getComponent(1))); - } - - public void testAlignment() { - VerticalLayout root = createLayout(0f, false, ":top", ":left"); - assertEquals(Alignment.TOP_LEFT, - root.getComponentAlignment(root.getComponent(0))); - root = createLayout(0f, false, ":middle", ":center"); - assertEquals(Alignment.MIDDLE_CENTER, - root.getComponentAlignment(root.getComponent(0))); - root = createLayout(0f, false, ":bottom", ":right"); - assertEquals(Alignment.BOTTOM_RIGHT, - root.getComponentAlignment(root.getComponent(0))); - - } - - private VerticalLayout createLayout(float expandRatio, boolean margin, - String... alignments) { - DesignContext ctx = new DesignContext(); - Element design = createDesign(expandRatio, margin, alignments); - Component child = ctx.createChild(design); - return (VerticalLayout) child; - } - - private Element createDesign(float expandRatio, boolean margin, - String... alignments) { - - Attributes rootAttributes = new Attributes(); - rootAttributes.put("caption", "test-layout"); - if (margin) { - rootAttributes.put("margin", ""); - } - Element node = new Element(Tag.valueOf("v-vertical-layout"), "", - rootAttributes); - - Attributes firstChildAttributes = new Attributes(); - firstChildAttributes.put("caption", "test-label"); - firstChildAttributes.put(":expand", String.valueOf(expandRatio)); - for (String alignment : alignments) { - firstChildAttributes.put(alignment, ""); - } - Element firstChild = new Element(Tag.valueOf("v-label"), "", - firstChildAttributes); - node.appendChild(firstChild); - - Attributes secondChildAttributes = new Attributes(); - secondChildAttributes.put(":expand", String.valueOf(expandRatio)); - for (String alignment : alignments) { - secondChildAttributes.put(alignment, ""); - } - Element secondChild = new Element(Tag.valueOf("v-button"), "", - secondChildAttributes); - secondChild.html("test-button"); - node.appendChild(secondChild); - return node; - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeToDesign.java deleted file mode 100644 index 23cf15cd13..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestSynchronizeToDesign.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.abstractorderedlayout; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.Alignment; -import com.vaadin.ui.Label; -import com.vaadin.ui.VerticalLayout; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for writing abstract ordered layout to design - * - * @since - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - - public void testSynchronizeMargin() { - VerticalLayout layout = new VerticalLayout(); - layout.setMargin(true); - Element design = createDesign(); - layout.synchronizeToDesign(design, createDesignContext()); - assertTrue("The margin must be written", design.hasAttr("margin")); - assertTrue("The margin must be empty or true", design.attr("margin") - .equals("") || design.attr("margin").equalsIgnoreCase("true")); - } - - public void testSynchronizeEmptyLayout() { - VerticalLayout layout = new VerticalLayout(); - layout.setCaption("changed-caption"); - Element design = createDesign(); - layout.synchronizeToDesign(design, createDesignContext()); - assertEquals(0, design.childNodes().size()); - assertEquals("changed-caption", design.attr("caption")); - } - - public void testSynchronizeLayoutWithChildren() { - VerticalLayout layout = new VerticalLayout(); - layout.addComponent(new Label("test-label")); - layout.getComponent(0).setCaption("test-caption"); - layout.addComponent(new Label("test-label-2")); - Element design = createDesign(); - layout.synchronizeToDesign(design, createDesignContext()); - assertEquals(2, design.childNodes().size()); - assertEquals("v-label", ((Element) design.childNode(0)).tagName()); - assertEquals("test-caption", design.childNode(0).attr("caption")); - } - - public void testSynchronizeUnitExpandRatio() { - VerticalLayout layout = new VerticalLayout(); - layout.addComponent(new Label("test-label")); - layout.setExpandRatio(layout.getComponent(0), 1.0f); - Element design = createDesign(); - layout.synchronizeToDesign(design, createDesignContext()); - assertTrue(design.childNode(0).hasAttr(":expand")); - assertEquals("", design.childNode(0).attr(":expand")); - } - - public void testSynchronizeArbitraryExpandRatio() { - VerticalLayout layout = new VerticalLayout(); - layout.addComponent(new Label("test-label")); - layout.setExpandRatio(layout.getComponent(0), 2.40f); - Element design = createDesign(); - layout.synchronizeToDesign(design, createDesignContext()); - assertTrue(design.childNode(0).hasAttr(":expand")); - assertEquals("2.4", design.childNode(0).attr(":expand")); - } - - public void testSynchronizeDefaultAlignment() { - Element design = createDesign(); - VerticalLayout layout = createLayoutWithAlignment(design, null); - layout.synchronizeToDesign(design, createDesignContext()); - assertFalse(design.childNode(0).hasAttr(":top")); - assertFalse(design.childNode(0).hasAttr(":left")); - } - - public void testSynchronizeMiddleCenter() { - Element design = createDesign(); - VerticalLayout layout = createLayoutWithAlignment(design, - Alignment.MIDDLE_CENTER); - layout.synchronizeToDesign(design, createDesignContext()); - assertTrue(design.childNode(0).hasAttr(":middle")); - assertTrue(design.childNode(0).hasAttr(":center")); - } - - public void testSynchronizeBottomRight() { - Element design = createDesign(); - VerticalLayout layout = createLayoutWithAlignment(design, - Alignment.BOTTOM_RIGHT); - layout.synchronizeToDesign(design, createDesignContext()); - assertTrue(design.childNode(0).hasAttr(":bottom")); - assertTrue(design.childNode(0).hasAttr(":right")); - } - - private VerticalLayout createLayoutWithAlignment(Element design, - Alignment alignment) { - VerticalLayout layout = new VerticalLayout(); - layout.addComponent(new Label("test-label")); - if (alignment != null) { - layout.setComponentAlignment(layout.getComponent(0), alignment); - } - layout.synchronizeToDesign(design, createDesignContext()); - return layout; - } - - private Element createDesign() { - // make sure that the design node has old content that should be removed - Attributes rootAttributes = new Attributes(); - rootAttributes.put("caption", "test-layout"); - Element node = new Element(Tag.valueOf("v-vertical-layout"), "", - rootAttributes); - Attributes firstChildAttributes = new Attributes(); - firstChildAttributes.put("caption", "test-label"); - Element firstChild = new Element(Tag.valueOf("v-label"), "", - firstChildAttributes); - node.appendChild(firstChild); - - Attributes secondChildAttributes = new Attributes(); - secondChildAttributes.put("caption", "test-button"); - Element secondChild = new Element(Tag.valueOf("v-button"), "", - secondChildAttributes); - node.appendChild(secondChild); - return node; - } - - private DesignContext createDesignContext() { - return new DesignContext(); - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestWriteDesign.java new file mode 100644 index 0000000000..302cf985bd --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/TestWriteDesign.java @@ -0,0 +1,148 @@ +/* + * Copyright 2000-2014 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.server.component.abstractorderedlayout; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.Alignment; +import com.vaadin.ui.Label; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing abstract ordered layout to design + * + * @since + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + + public void testSynchronizeMargin() { + VerticalLayout layout = new VerticalLayout(); + layout.setMargin(true); + Element design = createDesign(); + layout.writeDesign(design, createDesignContext()); + assertTrue("The margin must be written", design.hasAttr("margin")); + assertTrue("The margin must be empty or true", design.attr("margin") + .equals("") || design.attr("margin").equalsIgnoreCase("true")); + } + + public void testSynchronizeEmptyLayout() { + VerticalLayout layout = new VerticalLayout(); + layout.setCaption("changed-caption"); + Element design = createDesign(); + layout.writeDesign(design, createDesignContext()); + assertEquals(0, design.childNodes().size()); + assertEquals("changed-caption", design.attr("caption")); + } + + public void testSynchronizeLayoutWithChildren() { + VerticalLayout layout = new VerticalLayout(); + layout.addComponent(new Label("test-label")); + layout.getComponent(0).setCaption("test-caption"); + layout.addComponent(new Label("test-label-2")); + Element design = createDesign(); + layout.writeDesign(design, createDesignContext()); + assertEquals(2, design.childNodes().size()); + assertEquals("v-label", ((Element) design.childNode(0)).tagName()); + assertEquals("test-caption", design.childNode(0).attr("caption")); + } + + public void testSynchronizeUnitExpandRatio() { + VerticalLayout layout = new VerticalLayout(); + layout.addComponent(new Label("test-label")); + layout.setExpandRatio(layout.getComponent(0), 1.0f); + Element design = createDesign(); + layout.writeDesign(design, createDesignContext()); + assertTrue(design.childNode(0).hasAttr(":expand")); + assertEquals("", design.childNode(0).attr(":expand")); + } + + public void testSynchronizeArbitraryExpandRatio() { + VerticalLayout layout = new VerticalLayout(); + layout.addComponent(new Label("test-label")); + layout.setExpandRatio(layout.getComponent(0), 2.40f); + Element design = createDesign(); + layout.writeDesign(design, createDesignContext()); + assertTrue(design.childNode(0).hasAttr(":expand")); + assertEquals("2.4", design.childNode(0).attr(":expand")); + } + + public void testSynchronizeDefaultAlignment() { + Element design = createDesign(); + VerticalLayout layout = createLayoutWithAlignment(design, null); + layout.writeDesign(design, createDesignContext()); + assertFalse(design.childNode(0).hasAttr(":top")); + assertFalse(design.childNode(0).hasAttr(":left")); + } + + public void testSynchronizeMiddleCenter() { + Element design = createDesign(); + VerticalLayout layout = createLayoutWithAlignment(design, + Alignment.MIDDLE_CENTER); + layout.writeDesign(design, createDesignContext()); + assertTrue(design.childNode(0).hasAttr(":middle")); + assertTrue(design.childNode(0).hasAttr(":center")); + } + + public void testSynchronizeBottomRight() { + Element design = createDesign(); + VerticalLayout layout = createLayoutWithAlignment(design, + Alignment.BOTTOM_RIGHT); + layout.writeDesign(design, createDesignContext()); + assertTrue(design.childNode(0).hasAttr(":bottom")); + assertTrue(design.childNode(0).hasAttr(":right")); + } + + private VerticalLayout createLayoutWithAlignment(Element design, + Alignment alignment) { + VerticalLayout layout = new VerticalLayout(); + layout.addComponent(new Label("test-label")); + if (alignment != null) { + layout.setComponentAlignment(layout.getComponent(0), alignment); + } + layout.writeDesign(design, createDesignContext()); + return layout; + } + + private Element createDesign() { + // make sure that the design node has old content that should be removed + Attributes rootAttributes = new Attributes(); + rootAttributes.put("caption", "test-layout"); + Element node = new Element(Tag.valueOf("v-vertical-layout"), "", + rootAttributes); + Attributes firstChildAttributes = new Attributes(); + firstChildAttributes.put("caption", "test-label"); + Element firstChild = new Element(Tag.valueOf("v-label"), "", + firstChildAttributes); + node.appendChild(firstChild); + + Attributes secondChildAttributes = new Attributes(); + secondChildAttributes.put("caption", "test-button"); + Element secondChild = new Element(Tag.valueOf("v-button"), "", + secondChildAttributes); + node.appendChild(secondChild); + return node; + } + + private DesignContext createDesignContext() { + return new DesignContext(); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestReadDesign.java new file mode 100644 index 0000000000..c57f18cbf9 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestReadDesign.java @@ -0,0 +1,173 @@ +/* + * Copyright 2000-2014 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.server.component.abstractsplitpanel; + +import java.lang.reflect.Method; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.server.Sizeable.Unit; +import com.vaadin.shared.ui.splitpanel.AbstractSplitPanelState.SplitterState; +import com.vaadin.ui.AbstractSplitPanel; +import com.vaadin.ui.HorizontalSplitPanel; +import com.vaadin.ui.Table; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.VerticalSplitPanel; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Tests synchronizing the attributes and children of horizontal and vertical + * split panels from a design. + * + * @author Vaadin Ltd + */ +public class TestReadDesign extends TestCase { + DesignContext ctx; + + @Override + protected void setUp() { + ctx = new DesignContext(); + } + + public void testAttributes() throws Exception { + // Create a design with non-default attributes values. + Element design = createDesign(true, false, true, true); + HorizontalSplitPanel sp = new HorizontalSplitPanel(); + sp.readDesign(design, ctx); + // Check that the attributes are correctly parsed. + assertEquals(20.5f, sp.getSplitPosition()); + assertEquals(Unit.PERCENTAGE, sp.getSplitPositionUnit()); + assertEquals(20f, sp.getMinSplitPosition()); + assertEquals(Unit.PERCENTAGE, sp.getMinSplitPositionUnit()); + assertEquals(50f, sp.getMaxSplitPosition()); + assertEquals(Unit.PIXELS, sp.getMaxSplitPositionUnit()); + assertEquals(true, sp.isLocked()); + checkReversed(sp, true); + // check that the properties get the default values if the design + // does not have attributes corresponding to those properties + design = createDesign(true, true, true, true); + sp.readDesign(design, ctx); + HorizontalSplitPanel def = new HorizontalSplitPanel(); + assertEquals(def.getSplitPosition(), sp.getSplitPosition()); + assertEquals(def.getSplitPositionUnit(), sp.getSplitPositionUnit()); + assertEquals(def.getMinSplitPosition(), sp.getMinSplitPosition()); + assertEquals(def.getMinSplitPositionUnit(), + sp.getMinSplitPositionUnit()); + assertEquals(def.getMaxSplitPosition(), sp.getMaxSplitPosition()); + assertEquals(def.getMaxSplitPositionUnit(), + sp.getMaxSplitPositionUnit()); + assertEquals(def.isLocked(), sp.isLocked()); + checkReversed(sp, false); + } + + public void testWithNoChildren() { + Element design = createDesign(true, false, false, false); + HorizontalSplitPanel sp = new HorizontalSplitPanel(); + sp.readDesign(design, ctx); + assertEquals("Unexpected child count for the split panel.", 0, + sp.getComponentCount()); + } + + public void testWithFirstChild() { + Element design = createDesign(false, false, true, false); + VerticalSplitPanel sp = new VerticalSplitPanel(); + sp.readDesign(design, ctx); + assertEquals("Unexpected child count for the split panel.", 1, + sp.getComponentCount()); + Object obj = sp.getFirstComponent(); + assertEquals("Wrong component in split panel.", Table.class, + obj.getClass()); + } + + public void testWithSecondChild() { + Element design = createDesign(true, false, false, true); + HorizontalSplitPanel sp = new HorizontalSplitPanel(); + sp.readDesign(design, ctx); + assertEquals("Unexpected child count for the split panel.", 1, + sp.getComponentCount()); + Object obj = sp.getSecondComponent(); + assertEquals("Wrong component in split panel.", VerticalLayout.class, + obj.getClass()); + } + + public void testWithBothChildren() { + Element design = createDesign(false, false, true, true); + VerticalSplitPanel sp = new VerticalSplitPanel(); + sp.readDesign(design, ctx); + assertEquals("Unexpected child count for the split panel.", 2, + sp.getComponentCount()); + Object first = sp.getFirstComponent(); + Object second = sp.getSecondComponent(); + assertEquals("Wrong first component in split panel.", Table.class, + first.getClass()); + assertEquals("Wrong second component in split panel.", + VerticalLayout.class, second.getClass()); + } + + /* + * Creates an html tree node structure representing a split panel and its + * contents. The parameters are used for controlling whether the split panel + * is horizontal or vertical, whether attributes are set for the design and + * whether the split panel should have the first and the second child + * component. + */ + private Element createDesign(boolean horizontal, + boolean useDefaultAttributes, boolean hasFirstChild, + boolean hasSecondChild) { + Attributes attributes = new Attributes(); + if (!useDefaultAttributes) { + attributes.put("split-position", "20.5%"); + // The unitless number should correspond to 20% + attributes.put("min-split-position", "20"); + attributes.put("max-split-position", "50px"); + attributes.put("locked", ""); + attributes.put("reversed", ""); + } + String tagName = horizontal ? "v-horizontal-split-panel" + : "v-vertical-split-panel"; + Element element = new Element(Tag.valueOf(tagName), "", attributes); + // Create the children + if (hasFirstChild) { + Element child = new Element(Tag.valueOf("v-table"), ""); + element.appendChild(child); + } + if (hasSecondChild) { + Element child = new Element(Tag.valueOf("v-vertical-layout"), ""); + if (!hasFirstChild) { + child.attr(":second", ""); + } + element.appendChild(child); + } + return element; + } + + /* + * Checks the reversed property of a split panel. + */ + private void checkReversed(AbstractSplitPanel sp, boolean expected) + throws Exception { + Method getter = AbstractSplitPanel.class + .getDeclaredMethod("getSplitterState"); + getter.setAccessible(true); + SplitterState state = (SplitterState) getter.invoke(sp); + assertEquals("Wrong value for split panel property reversed.", + expected, state.positionReversed); + } +} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestSynchronizeFromDesign.java deleted file mode 100644 index b34e8fc3f0..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.abstractsplitpanel; - -import java.lang.reflect.Method; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.server.Sizeable.Unit; -import com.vaadin.shared.ui.splitpanel.AbstractSplitPanelState.SplitterState; -import com.vaadin.ui.AbstractSplitPanel; -import com.vaadin.ui.HorizontalSplitPanel; -import com.vaadin.ui.Table; -import com.vaadin.ui.VerticalLayout; -import com.vaadin.ui.VerticalSplitPanel; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Tests synchronizing the attributes and children of horizontal and vertical - * split panels from a design. - * - * @author Vaadin Ltd - */ -public class TestSynchronizeFromDesign extends TestCase { - DesignContext ctx; - - @Override - protected void setUp() { - ctx = new DesignContext(); - } - - public void testAttributes() throws Exception { - // Create a design with non-default attributes values. - Element design = createDesign(true, false, true, true); - HorizontalSplitPanel sp = new HorizontalSplitPanel(); - sp.synchronizeFromDesign(design, ctx); - // Check that the attributes are correctly parsed. - assertEquals(20.5f, sp.getSplitPosition()); - assertEquals(Unit.PERCENTAGE, sp.getSplitPositionUnit()); - assertEquals(20f, sp.getMinSplitPosition()); - assertEquals(Unit.PERCENTAGE, sp.getMinSplitPositionUnit()); - assertEquals(50f, sp.getMaxSplitPosition()); - assertEquals(Unit.PIXELS, sp.getMaxSplitPositionUnit()); - assertEquals(true, sp.isLocked()); - checkReversed(sp, true); - // check that the properties get the default values if the design - // does not have attributes corresponding to those properties - design = createDesign(true, true, true, true); - sp.synchronizeFromDesign(design, ctx); - HorizontalSplitPanel def = new HorizontalSplitPanel(); - assertEquals(def.getSplitPosition(), sp.getSplitPosition()); - assertEquals(def.getSplitPositionUnit(), sp.getSplitPositionUnit()); - assertEquals(def.getMinSplitPosition(), sp.getMinSplitPosition()); - assertEquals(def.getMinSplitPositionUnit(), - sp.getMinSplitPositionUnit()); - assertEquals(def.getMaxSplitPosition(), sp.getMaxSplitPosition()); - assertEquals(def.getMaxSplitPositionUnit(), - sp.getMaxSplitPositionUnit()); - assertEquals(def.isLocked(), sp.isLocked()); - checkReversed(sp, false); - } - - public void testWithNoChildren() { - Element design = createDesign(true, false, false, false); - HorizontalSplitPanel sp = new HorizontalSplitPanel(); - sp.synchronizeFromDesign(design, ctx); - assertEquals("Unexpected child count for the split panel.", 0, - sp.getComponentCount()); - } - - public void testWithFirstChild() { - Element design = createDesign(false, false, true, false); - VerticalSplitPanel sp = new VerticalSplitPanel(); - sp.synchronizeFromDesign(design, ctx); - assertEquals("Unexpected child count for the split panel.", 1, - sp.getComponentCount()); - Object obj = sp.getFirstComponent(); - assertEquals("Wrong component in split panel.", Table.class, - obj.getClass()); - } - - public void testWithSecondChild() { - Element design = createDesign(true, false, false, true); - HorizontalSplitPanel sp = new HorizontalSplitPanel(); - sp.synchronizeFromDesign(design, ctx); - assertEquals("Unexpected child count for the split panel.", 1, - sp.getComponentCount()); - Object obj = sp.getSecondComponent(); - assertEquals("Wrong component in split panel.", VerticalLayout.class, - obj.getClass()); - } - - public void testWithBothChildren() { - Element design = createDesign(false, false, true, true); - VerticalSplitPanel sp = new VerticalSplitPanel(); - sp.synchronizeFromDesign(design, ctx); - assertEquals("Unexpected child count for the split panel.", 2, - sp.getComponentCount()); - Object first = sp.getFirstComponent(); - Object second = sp.getSecondComponent(); - assertEquals("Wrong first component in split panel.", Table.class, - first.getClass()); - assertEquals("Wrong second component in split panel.", - VerticalLayout.class, second.getClass()); - } - - /* - * Creates an html tree node structure representing a split panel and its - * contents. The parameters are used for controlling whether the split panel - * is horizontal or vertical, whether attributes are set for the design and - * whether the split panel should have the first and the second child - * component. - */ - private Element createDesign(boolean horizontal, - boolean useDefaultAttributes, boolean hasFirstChild, - boolean hasSecondChild) { - Attributes attributes = new Attributes(); - if (!useDefaultAttributes) { - attributes.put("split-position", "20.5%"); - // The unitless number should correspond to 20% - attributes.put("min-split-position", "20"); - attributes.put("max-split-position", "50px"); - attributes.put("locked", ""); - attributes.put("reversed", ""); - } - String tagName = horizontal ? "v-horizontal-split-panel" - : "v-vertical-split-panel"; - Element element = new Element(Tag.valueOf(tagName), "", attributes); - // Create the children - if (hasFirstChild) { - Element child = new Element(Tag.valueOf("v-table"), ""); - element.appendChild(child); - } - if (hasSecondChild) { - Element child = new Element(Tag.valueOf("v-vertical-layout"), ""); - if (!hasFirstChild) { - child.attr(":second", ""); - } - element.appendChild(child); - } - return element; - } - - /* - * Checks the reversed property of a split panel. - */ - private void checkReversed(AbstractSplitPanel sp, boolean expected) - throws Exception { - Method getter = AbstractSplitPanel.class - .getDeclaredMethod("getSplitterState"); - getter.setAccessible(true); - SplitterState state = (SplitterState) getter.invoke(sp); - assertEquals("Wrong value for split panel property reversed.", - expected, state.positionReversed); - } -} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestSynchronizeToDesign.java deleted file mode 100644 index 3e342a0c89..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestSynchronizeToDesign.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.abstractsplitpanel; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Element; - -import com.vaadin.server.Sizeable.Unit; -import com.vaadin.ui.Button; -import com.vaadin.ui.HorizontalSplitPanel; -import com.vaadin.ui.Label; -import com.vaadin.ui.VerticalSplitPanel; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Tests synchronizing the properties and child components of split panels to a - * design. - * - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - private DesignContext ctx; - - @Override - public void setUp() { - ctx = new DesignContext(); - } - - public void testHorizontalWithDefaultValues() { - // no attributes or child elements should appear - HorizontalSplitPanel sp = new HorizontalSplitPanel(); - Element e = ctx.createNode(sp); - assertEquals("Wrong tag name.", "v-horizontal-split-panel", - e.nodeName()); - assertEquals("The split panel should not have attributes.", 0, e - .attributes().size()); - assertEquals("The split panel should not have children.", 0, e - .children().size()); - } - - public void testVerticalWithAttributes() { - // All defined attributes should be output in the tree node. No child - // components are present in this test. - VerticalSplitPanel sp = new VerticalSplitPanel(); - sp.setSplitPosition(27f, Unit.PIXELS, true); - sp.setMinSplitPosition(5.5f, Unit.PERCENTAGE); - sp.setMaxSplitPosition(95, Unit.PERCENTAGE); - sp.setLocked(true); - Element e = ctx.createNode(sp); - assertEquals("Wrong tag name.", "v-vertical-split-panel", e.nodeName()); - assertEquals("Unexpected number of attributes.", 5, e.attributes() - .size()); - assertEquals("Wrong split position.", "27px", e.attr("split-position")); - assertEquals("Wrong minimum split position.", "5.5%", - e.attr("min-split-position")); - assertEquals("Wrong maximum split position.", "95%", - e.attr("max-split-position")); - assertTrue("Unexpected value for locked: " + e.attr("locked"), - "true".equals(e.attr("locked")) || "".equals(e.attr("locked"))); - assertTrue( - "Unexpected value for reversed: " + e.attr("reversed"), - "true".equals(e.attr("reversed")) - || "".equals(e.attr("reversed"))); - } - - public void testHorizontalWithFirstChild() { - // The split panel contains only the first child. - HorizontalSplitPanel sp = new HorizontalSplitPanel(); - sp.setSplitPosition(25f); - sp.setFirstComponent(new Button("First slot")); - Element e = ctx.createNode(sp); - assertEquals("Wrong split position.", "25%", e.attr("split-position")); - assertEquals("Wrong number of child elements.", 1, e.children().size()); - Element eb = e.children().get(0); - assertEquals("Wrong tag name of first child element.", "v-button", - eb.nodeName()); - assertEquals("Wrong text in the button element.", "First slot", - eb.html()); - } - - public void testVerticalWithSecondChild() { - // The split panel contains only the second child. - VerticalSplitPanel sp = new VerticalSplitPanel(); - sp.setMinSplitPosition(25f, Unit.PIXELS); - sp.setSecondComponent(new Label("Second slot")); - Element e = ctx.createNode(sp); - assertEquals("Wrong minimum split position.", "25px", - e.attr("min-split-position")); - assertEquals("Wrong number of child elements.", 1, e.children().size()); - Element el = e.children().get(0); - assertEquals("Wrong tag name of child element.", "v-label", - el.nodeName()); - assertEquals("Wrong text in the label element.", "Second slot", - el.html()); - assertTrue("Missing attribute :second in the label element.", - el.hasAttr(":second")); - } - - public void testVerticalWithBothChildren() { - // The split panel has both child components. - VerticalSplitPanel sp = new VerticalSplitPanel(); - sp.setFirstComponent(new Button("First slot")); - sp.setSecondComponent(new Label("Second slot")); - Element e = ctx.createNode(sp); - assertEquals("Wrong number of child elements.", 2, e.children().size()); - Element eb = e.children().get(0); - assertEquals("Wrong tag name of first child element.", "v-button", - eb.nodeName()); - assertEquals("Wrong text in the button element.", "First slot", - eb.html()); - Element el = e.children().get(1); - assertEquals("Wrong tag name of second child element.", "v-label", - el.nodeName()); - assertEquals("Wrong text in the label element.", "Second slot", - el.html()); - assertFalse( - "There should be no :second attribute when a split panel has both children.", - el.hasAttr(":second")); - } - - public void testReSynchronize() { - // Test that old children and attributes are removed when an element is - // synchronized to a new component. - VerticalSplitPanel sp = new VerticalSplitPanel(); - sp.setMinSplitPosition(5.5f, Unit.PERCENTAGE); - sp.setMaxSplitPosition(95, Unit.PERCENTAGE); - sp.setFirstComponent(new Button("First slot")); - sp.setSecondComponent(new Label("Second slot")); - Element e = ctx.createNode(sp); - sp = new VerticalSplitPanel(); - sp.synchronizeToDesign(e, ctx); - assertTrue("There should be no attributes in the node.", e.attributes() - .size() == 0); - assertTrue("There should be no child elements.", - e.children().size() == 0); - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestWriteDesign.java new file mode 100644 index 0000000000..1027414c2d --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestWriteDesign.java @@ -0,0 +1,151 @@ +/* + * Copyright 2000-2014 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.server.component.abstractsplitpanel; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Element; + +import com.vaadin.server.Sizeable.Unit; +import com.vaadin.ui.Button; +import com.vaadin.ui.HorizontalSplitPanel; +import com.vaadin.ui.Label; +import com.vaadin.ui.VerticalSplitPanel; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Tests synchronizing the properties and child components of split panels to a + * design. + * + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + private DesignContext ctx; + + @Override + public void setUp() { + ctx = new DesignContext(); + } + + public void testHorizontalWithDefaultValues() { + // no attributes or child elements should appear + HorizontalSplitPanel sp = new HorizontalSplitPanel(); + Element e = ctx.createNode(sp); + assertEquals("Wrong tag name.", "v-horizontal-split-panel", + e.nodeName()); + assertEquals("The split panel should not have attributes.", 0, e + .attributes().size()); + assertEquals("The split panel should not have children.", 0, e + .children().size()); + } + + public void testVerticalWithAttributes() { + // All defined attributes should be output in the tree node. No child + // components are present in this test. + VerticalSplitPanel sp = new VerticalSplitPanel(); + sp.setSplitPosition(27f, Unit.PIXELS, true); + sp.setMinSplitPosition(5.5f, Unit.PERCENTAGE); + sp.setMaxSplitPosition(95, Unit.PERCENTAGE); + sp.setLocked(true); + Element e = ctx.createNode(sp); + assertEquals("Wrong tag name.", "v-vertical-split-panel", e.nodeName()); + assertEquals("Unexpected number of attributes.", 5, e.attributes() + .size()); + assertEquals("Wrong split position.", "27px", e.attr("split-position")); + assertEquals("Wrong minimum split position.", "5.5%", + e.attr("min-split-position")); + assertEquals("Wrong maximum split position.", "95%", + e.attr("max-split-position")); + assertTrue("Unexpected value for locked: " + e.attr("locked"), + "true".equals(e.attr("locked")) || "".equals(e.attr("locked"))); + assertTrue( + "Unexpected value for reversed: " + e.attr("reversed"), + "true".equals(e.attr("reversed")) + || "".equals(e.attr("reversed"))); + } + + public void testHorizontalWithFirstChild() { + // The split panel contains only the first child. + HorizontalSplitPanel sp = new HorizontalSplitPanel(); + sp.setSplitPosition(25f); + sp.setFirstComponent(new Button("First slot")); + Element e = ctx.createNode(sp); + assertEquals("Wrong split position.", "25%", e.attr("split-position")); + assertEquals("Wrong number of child elements.", 1, e.children().size()); + Element eb = e.children().get(0); + assertEquals("Wrong tag name of first child element.", "v-button", + eb.nodeName()); + assertEquals("Wrong text in the button element.", "First slot", + eb.html()); + } + + public void testVerticalWithSecondChild() { + // The split panel contains only the second child. + VerticalSplitPanel sp = new VerticalSplitPanel(); + sp.setMinSplitPosition(25f, Unit.PIXELS); + sp.setSecondComponent(new Label("Second slot")); + Element e = ctx.createNode(sp); + assertEquals("Wrong minimum split position.", "25px", + e.attr("min-split-position")); + assertEquals("Wrong number of child elements.", 1, e.children().size()); + Element el = e.children().get(0); + assertEquals("Wrong tag name of child element.", "v-label", + el.nodeName()); + assertEquals("Wrong text in the label element.", "Second slot", + el.html()); + assertTrue("Missing attribute :second in the label element.", + el.hasAttr(":second")); + } + + public void testVerticalWithBothChildren() { + // The split panel has both child components. + VerticalSplitPanel sp = new VerticalSplitPanel(); + sp.setFirstComponent(new Button("First slot")); + sp.setSecondComponent(new Label("Second slot")); + Element e = ctx.createNode(sp); + assertEquals("Wrong number of child elements.", 2, e.children().size()); + Element eb = e.children().get(0); + assertEquals("Wrong tag name of first child element.", "v-button", + eb.nodeName()); + assertEquals("Wrong text in the button element.", "First slot", + eb.html()); + Element el = e.children().get(1); + assertEquals("Wrong tag name of second child element.", "v-label", + el.nodeName()); + assertEquals("Wrong text in the label element.", "Second slot", + el.html()); + assertFalse( + "There should be no :second attribute when a split panel has both children.", + el.hasAttr(":second")); + } + + public void testReSynchronize() { + // Test that old children and attributes are removed when an element is + // synchronized to a new component. + VerticalSplitPanel sp = new VerticalSplitPanel(); + sp.setMinSplitPosition(5.5f, Unit.PERCENTAGE); + sp.setMaxSplitPosition(95, Unit.PERCENTAGE); + sp.setFirstComponent(new Button("First slot")); + sp.setSecondComponent(new Label("Second slot")); + Element e = ctx.createNode(sp); + sp = new VerticalSplitPanel(); + sp.writeDesign(e, ctx); + assertTrue("There should be no attributes in the node.", e.attributes() + .size() == 0); + assertTrue("There should be no child elements.", + e.children().size() == 0); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestReadDesign.java new file mode 100644 index 0000000000..3b4b6f1f03 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestReadDesign.java @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2014 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.server.component.abstracttextfield; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.AbstractTextField; +import com.vaadin.ui.AbstractTextField.TextChangeEventMode; +import com.vaadin.ui.TextField; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for reading the attributes of the AbstractTextField from design + */ +public class TestReadDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testAttributes() { + Element design = createDesign(); + AbstractTextField component = getComponent(); + component.readDesign(design, ctx); + assertEquals("this-is-null", component.getNullRepresentation()); + assertEquals(true, component.isNullSettingAllowed()); + assertEquals(5, component.getMaxLength()); + assertEquals(3, component.getColumns()); + assertEquals("input", component.getInputPrompt()); + assertEquals(TextChangeEventMode.EAGER, + component.getTextChangeEventMode()); + assertEquals(100, component.getTextChangeTimeout()); + } + + private AbstractTextField getComponent() { + return new TextField(); + } + + private Element createDesign() { + Attributes attributes = new Attributes(); + attributes.put("null-representation", "this-is-null"); + attributes.put("null-setting-allowed", "true"); + attributes.put("maxlength", "5"); + attributes.put("columns", "3"); + attributes.put("input-prompt", "input"); + attributes.put("text-change-event-mode", "eager"); + attributes.put("text-change-timeout", "100"); + Element node = new Element(Tag.valueOf("v-text-field"), "", attributes); + return node; + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeFromDesign.java deleted file mode 100644 index f7ba67f506..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.abstracttextfield; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.AbstractTextField; -import com.vaadin.ui.AbstractTextField.TextChangeEventMode; -import com.vaadin.ui.TextField; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for reading the attributes of the AbstractTextField from design - */ -public class TestSynchronizeFromDesign extends TestCase { - - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - public void testAttributes() { - Element design = createDesign(); - AbstractTextField component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals("this-is-null", component.getNullRepresentation()); - assertEquals(true, component.isNullSettingAllowed()); - assertEquals(5, component.getMaxLength()); - assertEquals(3, component.getColumns()); - assertEquals("input", component.getInputPrompt()); - assertEquals(TextChangeEventMode.EAGER, - component.getTextChangeEventMode()); - assertEquals(100, component.getTextChangeTimeout()); - } - - private AbstractTextField getComponent() { - return new TextField(); - } - - private Element createDesign() { - Attributes attributes = new Attributes(); - attributes.put("null-representation", "this-is-null"); - attributes.put("null-setting-allowed", "true"); - attributes.put("maxlength", "5"); - attributes.put("columns", "3"); - attributes.put("input-prompt", "input"); - attributes.put("text-change-event-mode", "eager"); - attributes.put("text-change-timeout", "100"); - Element node = new Element(Tag.valueOf("v-text-field"), "", attributes); - return node; - } - -} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeToDesign.java deleted file mode 100644 index b8f29b0ac9..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeToDesign.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.abstracttextfield; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.AbstractTextField; -import com.vaadin.ui.AbstractTextField.TextChangeEventMode; -import com.vaadin.ui.TextField; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for writing the attributes of the AbstractTextField to design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - public void testSynchronizetestAttributes() { - Element design = createDesign(); - AbstractTextField component = getComponent(); - component.setNullRepresentation("this-is-null"); - component.setNullSettingAllowed(true); - component.setMaxLength(5); - component.setColumns(3); - component.setInputPrompt("input"); - component.setTextChangeEventMode(TextChangeEventMode.EAGER); - component.setTextChangeTimeout(100); - component.synchronizeToDesign(design, ctx); - assertEquals("this-is-null", design.attr("null-representation")); - assertEquals("true", design.attr("null-setting-allowed")); - assertEquals("5", design.attr("maxlength")); - assertEquals("3", design.attr("columns")); - assertEquals("input", design.attr("input-prompt")); - assertEquals("EAGER", design.attr("text-change-event-mode")); - assertEquals("100", design.attr("text-change-timeout")); - } - - private AbstractTextField getComponent() { - return new TextField(); - } - - private Element createDesign() { - Attributes attr = new Attributes(); - return new Element(Tag.valueOf("v-text-field"), "", attr); - } - -} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestWriteDesign.java new file mode 100644 index 0000000000..be5c384d3b --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestWriteDesign.java @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2014 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.server.component.abstracttextfield; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.AbstractTextField; +import com.vaadin.ui.AbstractTextField.TextChangeEventMode; +import com.vaadin.ui.TextField; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing the attributes of the AbstractTextField to design + * + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testSynchronizetestAttributes() { + Element design = createDesign(); + AbstractTextField component = getComponent(); + component.setNullRepresentation("this-is-null"); + component.setNullSettingAllowed(true); + component.setMaxLength(5); + component.setColumns(3); + component.setInputPrompt("input"); + component.setTextChangeEventMode(TextChangeEventMode.EAGER); + component.setTextChangeTimeout(100); + component.writeDesign(design, ctx); + assertEquals("this-is-null", design.attr("null-representation")); + assertEquals("true", design.attr("null-setting-allowed")); + assertEquals("5", design.attr("maxlength")); + assertEquals("3", design.attr("columns")); + assertEquals("input", design.attr("input-prompt")); + assertEquals("EAGER", design.attr("text-change-event-mode")); + assertEquals("100", design.attr("text-change-timeout")); + } + + private AbstractTextField getComponent() { + return new TextField(); + } + + private Element createDesign() { + Attributes attr = new Attributes(); + return new Element(Tag.valueOf("v-text-field"), "", attr); + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/button/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/button/TestReadDesign.java new file mode 100644 index 0000000000..48b228a35a --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/button/TestReadDesign.java @@ -0,0 +1,129 @@ +/* + * Copyright 2000-2014 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.server.component.button; + +import java.lang.reflect.Field; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; +import org.junit.Test; + +import com.vaadin.event.ShortcutAction.KeyCode; +import com.vaadin.event.ShortcutAction.ModifierKey; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickShortcut; +import com.vaadin.ui.NativeButton; +import com.vaadin.ui.declarative.DesignContext; + +/** + * + * Test cases for reading the contents of a Button and a NativeButton from a + * design. + * + */ +public class TestReadDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + @Test + public void testWithContent() { + createAndTestButtons("Click", null); + } + + @Test + public void testWithHtmlCaption() { + createAndTestButtons("Click me", null); + } + + @Test + public void testWithContentAndCaption() { + createAndTestButtons("Click me", "caption"); + } + + @Test + public void testWithCaption() { + createAndTestButtons(null, "Click me"); + } + + @Test + public void testAttributes() throws IllegalArgumentException, + SecurityException, IllegalAccessException, NoSuchFieldException { + Attributes attributes = new Attributes(); + attributes.put("tabindex", "3"); + attributes.put("plain-text", ""); + attributes.put("icon-alt", "OK"); + attributes.put("click-shortcut", "ctrl-shift-o"); + Button button = (Button) ctx + .createChild(createButtonWithAttributes(attributes)); + assertEquals(3, button.getTabIndex()); + assertEquals(false, button.isHtmlContentAllowed()); + assertEquals("OK", button.getIconAlternateText()); + Field field = Button.class.getDeclaredField("clickShortcut"); + field.setAccessible(true); + ClickShortcut value = (ClickShortcut) field.get(button); + assertEquals(KeyCode.O, value.getKeyCode()); + assertEquals(ModifierKey.CTRL, value.getModifiers()[0]); + assertEquals(ModifierKey.SHIFT, value.getModifiers()[1]); + } + + /* + * Test both Button and NativeButton. Caption should always be ignored. If + * content is null, the created button should have empty content. + */ + private void createAndTestButtons(String content, String caption) { + Element e1 = createElement("v-button", content, caption); + Button b1 = (Button) ctx.createChild(e1); + Element e2 = createElement("v-native-button", content, caption); + NativeButton b2 = (NativeButton) ctx.createChild(e2); + if (content != null) { + assertEquals("The button has the wrong text content.", content, + b1.getCaption()); + assertEquals("The button has the wrong text content.", content, + b2.getCaption()); + } else { + assertTrue("The button has the wrong content.", + b1.getCaption() == null || "".equals(b1.getCaption())); + assertTrue("The button has the wrong content.", + b2.getCaption() == null || "".equals(b2.getCaption())); + } + } + + private Element createButtonWithAttributes(Attributes attributes) { + return new Element(Tag.valueOf("v-button"), "", attributes); + } + + private Element createElement(String elementName, String content, + String caption) { + Attributes attributes = new Attributes(); + if (caption != null) { + attributes.put("caption", caption); + } + Element node = new Element(Tag.valueOf(elementName), "", attributes); + if (content != null) { + node.html(content); + } + return node; + } +} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeFromDesign.java deleted file mode 100644 index 704192fb04..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.button; - -import java.lang.reflect.Field; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; -import org.junit.Test; - -import com.vaadin.event.ShortcutAction.KeyCode; -import com.vaadin.event.ShortcutAction.ModifierKey; -import com.vaadin.ui.Button; -import com.vaadin.ui.Button.ClickShortcut; -import com.vaadin.ui.NativeButton; -import com.vaadin.ui.declarative.DesignContext; - -/** - * - * Test cases for reading the contents of a Button and a NativeButton from a - * design. - * - */ -public class TestSynchronizeFromDesign extends TestCase { - - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - @Test - public void testWithContent() { - createAndTestButtons("Click", null); - } - - @Test - public void testWithHtmlCaption() { - createAndTestButtons("Click me", null); - } - - @Test - public void testWithContentAndCaption() { - createAndTestButtons("Click me", "caption"); - } - - @Test - public void testWithCaption() { - createAndTestButtons(null, "Click me"); - } - - @Test - public void testAttributes() throws IllegalArgumentException, - SecurityException, IllegalAccessException, NoSuchFieldException { - Attributes attributes = new Attributes(); - attributes.put("tabindex", "3"); - attributes.put("plain-text", ""); - attributes.put("icon-alt", "OK"); - attributes.put("click-shortcut", "ctrl-shift-o"); - Button button = (Button) ctx - .createChild(createButtonWithAttributes(attributes)); - assertEquals(3, button.getTabIndex()); - assertEquals(false, button.isHtmlContentAllowed()); - assertEquals("OK", button.getIconAlternateText()); - Field field = Button.class.getDeclaredField("clickShortcut"); - field.setAccessible(true); - ClickShortcut value = (ClickShortcut) field.get(button); - assertEquals(KeyCode.O, value.getKeyCode()); - assertEquals(ModifierKey.CTRL, value.getModifiers()[0]); - assertEquals(ModifierKey.SHIFT, value.getModifiers()[1]); - } - - /* - * Test both Button and NativeButton. Caption should always be ignored. If - * content is null, the created button should have empty content. - */ - private void createAndTestButtons(String content, String caption) { - Element e1 = createElement("v-button", content, caption); - Button b1 = (Button) ctx.createChild(e1); - Element e2 = createElement("v-native-button", content, caption); - NativeButton b2 = (NativeButton) ctx.createChild(e2); - if (content != null) { - assertEquals("The button has the wrong text content.", content, - b1.getCaption()); - assertEquals("The button has the wrong text content.", content, - b2.getCaption()); - } else { - assertTrue("The button has the wrong content.", - b1.getCaption() == null || "".equals(b1.getCaption())); - assertTrue("The button has the wrong content.", - b2.getCaption() == null || "".equals(b2.getCaption())); - } - } - - private Element createButtonWithAttributes(Attributes attributes) { - return new Element(Tag.valueOf("v-button"), "", attributes); - } - - private Element createElement(String elementName, String content, - String caption) { - Attributes attributes = new Attributes(); - if (caption != null) { - attributes.put("caption", caption); - } - Element node = new Element(Tag.valueOf(elementName), "", attributes); - if (content != null) { - node.html(content); - } - return node; - } -} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java deleted file mode 100644 index c96fd36d5d..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/button/TestSynchronizeToDesign.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.button; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; -import org.junit.Test; - -import com.vaadin.event.ShortcutAction.KeyCode; -import com.vaadin.event.ShortcutAction.ModifierKey; -import com.vaadin.ui.Button; -import com.vaadin.ui.NativeButton; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Tests generating html tree nodes corresponding to the contents of a Button - * and a NativeButton. - */ -public class TestSynchronizeToDesign extends TestCase { - - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - @Test - public void testWithTextContent() { - createAndTestButtons("Click me"); - } - - @Test - public void testWithHtmlContent() { - createAndTestButtons("Click"); - } - - @Test - public void testAttributes() { - Button button = new Button(); - button.setTabIndex(3); - button.setIconAlternateText("OK"); - button.setClickShortcut(KeyCode.O, ModifierKey.CTRL, ModifierKey.SHIFT); - Element e = new Element(Tag.valueOf("v-button"), "", new Attributes()); - button.synchronizeToDesign(e, ctx); - assertEquals("3", e.attr("tabindex")); - assertTrue("Button is plain text by default", e.hasAttr("plain-text")); - assertEquals("OK", e.attr("icon-alt")); - assertEquals("ctrl-shift-o", e.attr("click-shortcut")); - } - - @Test - public void testUpdateContentMode() { - Button button = new Button("OK"); - Element e = new Element(Tag.valueOf("v-button"), "", new Attributes()); - button.synchronizeToDesign(e, ctx); - assertTrue("Button is plain text by default", e.hasAttr("plain-text")); - - button.setHtmlContentAllowed(true); - button.synchronizeToDesign(e, ctx); - assertTrue("Button is updated to HTML", !e.hasAttr("plain-text")); - - } - - private void createAndTestButtons(String content) { - Button b1 = new Button(content); - // we need to set this on, since the plain-text attribute will appear - // otherwise - b1.setHtmlContentAllowed(true); - Element e1 = ctx.createNode(b1); - assertEquals("Wrong tag name for button.", "v-button", e1.tagName()); - assertEquals("Unexpected content in the v-button element.", content, - e1.html()); - assertTrue("The v-button element should not have attributes.", e1 - .attributes().size() == 0); - NativeButton b2 = new NativeButton(content); - b2.setHtmlContentAllowed(true); - Element e2 = ctx.createNode(b2); - assertEquals("Wrong tag name for button.", "v-native-button", - e2.tagName()); - assertEquals("Unexpected content in the v-button element.", content, - e2.html()); - assertTrue("The v-button element should not have attributes.", e2 - .attributes().size() == 0); - } -} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/button/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/button/TestWriteDesign.java new file mode 100644 index 0000000000..a8cf83522c --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/button/TestWriteDesign.java @@ -0,0 +1,103 @@ +/* + * Copyright 2000-2014 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.server.component.button; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; +import org.junit.Test; + +import com.vaadin.event.ShortcutAction.KeyCode; +import com.vaadin.event.ShortcutAction.ModifierKey; +import com.vaadin.ui.Button; +import com.vaadin.ui.NativeButton; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Tests generating html tree nodes corresponding to the contents of a Button + * and a NativeButton. + */ +public class TestWriteDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + @Test + public void testWithTextContent() { + createAndTestButtons("Click me"); + } + + @Test + public void testWithHtmlContent() { + createAndTestButtons("Click"); + } + + @Test + public void testAttributes() { + Button button = new Button(); + button.setTabIndex(3); + button.setIconAlternateText("OK"); + button.setClickShortcut(KeyCode.O, ModifierKey.CTRL, ModifierKey.SHIFT); + Element e = new Element(Tag.valueOf("v-button"), "", new Attributes()); + button.writeDesign(e, ctx); + assertEquals("3", e.attr("tabindex")); + assertTrue("Button is plain text by default", e.hasAttr("plain-text")); + assertEquals("OK", e.attr("icon-alt")); + assertEquals("ctrl-shift-o", e.attr("click-shortcut")); + } + + @Test + public void testUpdateContentMode() { + Button button = new Button("OK"); + Element e = new Element(Tag.valueOf("v-button"), "", new Attributes()); + button.writeDesign(e, ctx); + assertTrue("Button is plain text by default", e.hasAttr("plain-text")); + + button.setHtmlContentAllowed(true); + button.writeDesign(e, ctx); + assertTrue("Button is updated to HTML", !e.hasAttr("plain-text")); + + } + + private void createAndTestButtons(String content) { + Button b1 = new Button(content); + // we need to set this on, since the plain-text attribute will appear + // otherwise + b1.setHtmlContentAllowed(true); + Element e1 = ctx.createNode(b1); + assertEquals("Wrong tag name for button.", "v-button", e1.tagName()); + assertEquals("Unexpected content in the v-button element.", content, + e1.html()); + assertTrue("The v-button element should not have attributes.", e1 + .attributes().size() == 0); + NativeButton b2 = new NativeButton(content); + b2.setHtmlContentAllowed(true); + Element e2 = ctx.createNode(b2); + assertEquals("Wrong tag name for button.", "v-native-button", + e2.tagName()); + assertEquals("Unexpected content in the v-button element.", content, + e2.html()); + assertTrue("The v-button element should not have attributes.", e2 + .attributes().size() == 0); + } +} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/csslayout/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/csslayout/TestReadDesign.java new file mode 100644 index 0000000000..51f763bf14 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/csslayout/TestReadDesign.java @@ -0,0 +1,74 @@ +/* + * Copyright 2000-2014 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.server.component.csslayout; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.Component; +import com.vaadin.ui.CssLayout; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for reading CssLayout from design + * + * @author Vaadin Ltd + */ +public class TestReadDesign extends TestCase { + + public void testChildCount() { + CssLayout root = createLayout(); + assertEquals(2, root.getComponentCount()); + } + + public void testAttributes() { + CssLayout root = createLayout(); + assertEquals("test-layout", root.getCaption()); + assertEquals("test-label", root.getComponent(0).getCaption()); + assertEquals("test-button", root.getComponent(1).getCaption()); + } + + private CssLayout createLayout() { + DesignContext ctx = new DesignContext(); + Element design = createDesign(); + Component child = ctx.createChild(design); + return (CssLayout) child; + } + + private Element createDesign() { + + Attributes rootAttributes = new Attributes(); + rootAttributes.put("caption", "test-layout"); + Element node = new Element(Tag.valueOf("v-css-layout"), "", + rootAttributes); + + Attributes firstChildAttributes = new Attributes(); + firstChildAttributes.put("caption", "test-label"); + Element firstChild = new Element(Tag.valueOf("v-label"), "", + firstChildAttributes); + node.appendChild(firstChild); + + Attributes secondChildAttributes = new Attributes(); + Element secondChild = new Element(Tag.valueOf("v-button"), "", + secondChildAttributes); + secondChild.html("test-button"); + node.appendChild(secondChild); + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeFromDesign.java deleted file mode 100644 index e9694f0170..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.csslayout; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.Component; -import com.vaadin.ui.CssLayout; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for reading CssLayout from design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeFromDesign extends TestCase { - - public void testChildCount() { - CssLayout root = createLayout(); - assertEquals(2, root.getComponentCount()); - } - - public void testAttributes() { - CssLayout root = createLayout(); - assertEquals("test-layout", root.getCaption()); - assertEquals("test-label", root.getComponent(0).getCaption()); - assertEquals("test-button", root.getComponent(1).getCaption()); - } - - private CssLayout createLayout() { - DesignContext ctx = new DesignContext(); - Element design = createDesign(); - Component child = ctx.createChild(design); - return (CssLayout) child; - } - - private Element createDesign() { - - Attributes rootAttributes = new Attributes(); - rootAttributes.put("caption", "test-layout"); - Element node = new Element(Tag.valueOf("v-css-layout"), "", - rootAttributes); - - Attributes firstChildAttributes = new Attributes(); - firstChildAttributes.put("caption", "test-label"); - Element firstChild = new Element(Tag.valueOf("v-label"), "", - firstChildAttributes); - node.appendChild(firstChild); - - Attributes secondChildAttributes = new Attributes(); - Element secondChild = new Element(Tag.valueOf("v-button"), "", - secondChildAttributes); - secondChild.html("test-button"); - node.appendChild(secondChild); - return node; - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeToDesign.java deleted file mode 100644 index 2dfd560c8c..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeToDesign.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.csslayout; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.CssLayout; -import com.vaadin.ui.Label; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for writing CssLayout to design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - - public void testSynchronizeEmptyLayout() { - CssLayout layout = new CssLayout(); - layout.setCaption("changed-caption"); - Element design = createDesign(); - layout.synchronizeToDesign(design, createDesignContext()); - assertEquals(0, design.childNodes().size()); - assertEquals("changed-caption", design.attr("caption")); - } - - public void testSynchronizeLayoutWithChildren() { - CssLayout layout = new CssLayout(); - layout.addComponent(new Label("test-label")); - layout.getComponent(0).setCaption("test-caption"); - layout.addComponent(new Label("test-label-2")); - Element design = createDesign(); - layout.synchronizeToDesign(design, createDesignContext()); - assertEquals(2, design.childNodes().size()); - assertEquals("v-label", ((Element) design.childNode(0)).tagName()); - assertEquals("test-caption", design.childNode(0).attr("caption")); - } - - private Element createDesign() { - // make sure that the design node has old content that should be removed - Attributes rootAttributes = new Attributes(); - rootAttributes.put("caption", "test-layout"); - Element node = new Element(Tag.valueOf("v-vertical-layout"), "", - rootAttributes); - Attributes firstChildAttributes = new Attributes(); - firstChildAttributes.put("caption", "test-label"); - Element firstChild = new Element(Tag.valueOf("v-label"), "", - firstChildAttributes); - node.appendChild(firstChild); - - Element secondChild = new Element(Tag.valueOf("v-button"), "", - new Attributes()); - secondChild.html("test-button"); - node.appendChild(secondChild); - return node; - } - - private DesignContext createDesignContext() { - return new DesignContext(); - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/csslayout/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/csslayout/TestWriteDesign.java new file mode 100644 index 0000000000..d660eb77ec --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/csslayout/TestWriteDesign.java @@ -0,0 +1,78 @@ +/* + * Copyright 2000-2014 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.server.component.csslayout; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.CssLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing CssLayout to design + * + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + + public void testSynchronizeEmptyLayout() { + CssLayout layout = new CssLayout(); + layout.setCaption("changed-caption"); + Element design = createDesign(); + layout.writeDesign(design, createDesignContext()); + assertEquals(0, design.childNodes().size()); + assertEquals("changed-caption", design.attr("caption")); + } + + public void testSynchronizeLayoutWithChildren() { + CssLayout layout = new CssLayout(); + layout.addComponent(new Label("test-label")); + layout.getComponent(0).setCaption("test-caption"); + layout.addComponent(new Label("test-label-2")); + Element design = createDesign(); + layout.writeDesign(design, createDesignContext()); + assertEquals(2, design.childNodes().size()); + assertEquals("v-label", ((Element) design.childNode(0)).tagName()); + assertEquals("test-caption", design.childNode(0).attr("caption")); + } + + private Element createDesign() { + // make sure that the design node has old content that should be removed + Attributes rootAttributes = new Attributes(); + rootAttributes.put("caption", "test-layout"); + Element node = new Element(Tag.valueOf("v-vertical-layout"), "", + rootAttributes); + Attributes firstChildAttributes = new Attributes(); + firstChildAttributes.put("caption", "test-label"); + Element firstChild = new Element(Tag.valueOf("v-label"), "", + firstChildAttributes); + node.appendChild(firstChild); + + Element secondChild = new Element(Tag.valueOf("v-button"), "", + new Attributes()); + secondChild.html("test-button"); + node.appendChild(secondChild); + return node; + } + + private DesignContext createDesignContext() { + return new DesignContext(); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/label/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/label/TestReadDesign.java new file mode 100644 index 0000000000..1a02ed98cc --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/label/TestReadDesign.java @@ -0,0 +1,102 @@ +/* + * Copyright 2000-2014 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.server.component.label; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; +import org.junit.Test; + +import com.vaadin.ui.Label; +import com.vaadin.ui.declarative.DesignContext; + +/** + * + * Test case for reading the contents of a Label from a design. + * + */ +public class TestReadDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + @Test + public void testWithContent() { + createAndTestLabel("A label", null); + } + + @Test + public void testWithHtmlContent() { + createAndTestLabel("A label", null); + } + + @Test + public void testWithContentAndCaption() { + createAndTestLabel("A label", "This is a label"); + } + + @Test + public void testWithCaption() { + createAndTestLabel(null, "This is a label"); + } + + @Test + public void testWithoutContentAndCaption() { + createAndTestLabel(null, null); + } + + /* + * Test creating a Label. A Label can have both caption and content. + */ + private void createAndTestLabel(String content, String caption) { + Element e = createElement("v-label", content, caption); + Label l = (Label) ctx.createChild(e); + if (content != null) { + assertEquals("The label has wrong text content.", content, + l.getValue()); + } else { + assertTrue("The label has wrong text content.", + l.getValue() == null || "".equals(l.getValue())); + } + if (caption != null) { + assertEquals("The label has wrong caption.", caption, + l.getCaption()); + } else { + assertTrue("The label has wrong caption.", l.getCaption() == null + || "".equals(l.getCaption())); + } + } + + private Element createElement(String elementName, String content, + String caption) { + Attributes attributes = new Attributes(); + if (caption != null) { + attributes.put("caption", caption); + } + Element node = new Element(Tag.valueOf(elementName), "", attributes); + if (content != null) { + node.html(content); + } + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeFromDesign.java deleted file mode 100644 index 168c44cb90..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.label; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; -import org.junit.Test; - -import com.vaadin.ui.Label; -import com.vaadin.ui.declarative.DesignContext; - -/** - * - * Test case for reading the contents of a Label from a design. - * - */ -public class TestSynchronizeFromDesign extends TestCase { - - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - @Test - public void testWithContent() { - createAndTestLabel("A label", null); - } - - @Test - public void testWithHtmlContent() { - createAndTestLabel("A label", null); - } - - @Test - public void testWithContentAndCaption() { - createAndTestLabel("A label", "This is a label"); - } - - @Test - public void testWithCaption() { - createAndTestLabel(null, "This is a label"); - } - - @Test - public void testWithoutContentAndCaption() { - createAndTestLabel(null, null); - } - - /* - * Test creating a Label. A Label can have both caption and content. - */ - private void createAndTestLabel(String content, String caption) { - Element e = createElement("v-label", content, caption); - Label l = (Label) ctx.createChild(e); - if (content != null) { - assertEquals("The label has wrong text content.", content, - l.getValue()); - } else { - assertTrue("The label has wrong text content.", - l.getValue() == null || "".equals(l.getValue())); - } - if (caption != null) { - assertEquals("The label has wrong caption.", caption, - l.getCaption()); - } else { - assertTrue("The label has wrong caption.", l.getCaption() == null - || "".equals(l.getCaption())); - } - } - - private Element createElement(String elementName, String content, - String caption) { - Attributes attributes = new Attributes(); - if (caption != null) { - attributes.put("caption", caption); - } - Element node = new Element(Tag.valueOf(elementName), "", attributes); - if (content != null) { - node.html(content); - } - return node; - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java deleted file mode 100644 index 869546ac3d..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/label/TestSynchronizeToDesign.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.label; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; -import org.junit.Test; - -import com.vaadin.shared.ui.label.ContentMode; -import com.vaadin.ui.Label; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Tests generating an html tree node corresponding to a Label. - */ -public class TestSynchronizeToDesign extends TestCase { - - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - @Test - public void testWithContent() { - createAndTestLabel("A label", null); - } - - @Test - public void testWithHtmlContent() { - createAndTestLabel("A label", null); - } - - @Test - public void testWithCaption() { - createAndTestLabel(null, "Label caption"); - } - - @Test - public void testWithContentAndCaption() { - createAndTestLabel("A label", "Label caption"); - } - - @Test - public void testContentModeText() { - Label l = new Label("plain text label"); - Element e = new Element(Tag.valueOf("v-label"), "", new Attributes()); - l.synchronizeToDesign(e, ctx); - assertTrue("Label should be marked as plain text", - e.hasAttr("plain-text")); - } - - @Test - public void testContentModeHtml() { - Label l = new Label("html label"); - l.setContentMode(ContentMode.HTML); - - Element e = new Element(Tag.valueOf("v-label"), "", new Attributes()); - l.synchronizeToDesign(e, ctx); - assertFalse("Label should not be marked as plain text", - e.hasAttr("plain-text")); - } - - @Test - public void testChangeContentMode() { - Label l = new Label("html label"); - l.setContentMode(ContentMode.HTML); - - Element e = new Element(Tag.valueOf("v-label"), "", new Attributes()); - l.synchronizeToDesign(e, ctx); - - assertFalse("Label should not be marked as plain text", - e.hasAttr("plain-text")); - l.setContentMode(ContentMode.TEXT); - l.synchronizeToDesign(e, ctx); - - assertTrue("Label should be marked as plain text", - e.hasAttr("plain-text")); - } - - @Test - public void testWithoutContentAndCaption() { - createAndTestLabel(null, null); - } - - private void createAndTestLabel(String content, String caption) { - Label l = new Label(content); - if (caption != null) { - l.setCaption(caption); - } - Element e = ctx.createNode(l); - assertEquals("Wrong tag name for label.", "v-label", e.tagName()); - if (content != null) { - assertEquals("Unexpected content in the v-label element.", content, - e.html()); - } else { - assertTrue("Unexpected content in the v-label element.", - e.html() == null || "".equals(e.html())); - } - if (caption != null) { - assertEquals("Wrong caption in the v-label element.", caption, - e.attr("caption")); - } else { - assertTrue("Unexpected caption in the v-label element.", - e.attr("caption") == null || "".equals(e.attr("caption"))); - } - } -} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/label/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/label/TestWriteDesign.java new file mode 100644 index 0000000000..fab7c1d8eb --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/label/TestWriteDesign.java @@ -0,0 +1,126 @@ +/* + * Copyright 2000-2014 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.server.component.label; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; +import org.junit.Test; + +import com.vaadin.shared.ui.label.ContentMode; +import com.vaadin.ui.Label; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Tests generating an html tree node corresponding to a Label. + */ +public class TestWriteDesign extends TestCase { + + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + @Test + public void testWithContent() { + createAndTestLabel("A label", null); + } + + @Test + public void testWithHtmlContent() { + createAndTestLabel("A label", null); + } + + @Test + public void testWithCaption() { + createAndTestLabel(null, "Label caption"); + } + + @Test + public void testWithContentAndCaption() { + createAndTestLabel("A label", "Label caption"); + } + + @Test + public void testContentModeText() { + Label l = new Label("plain text label"); + Element e = new Element(Tag.valueOf("v-label"), "", new Attributes()); + l.writeDesign(e, ctx); + assertTrue("Label should be marked as plain text", + e.hasAttr("plain-text")); + } + + @Test + public void testContentModeHtml() { + Label l = new Label("html label"); + l.setContentMode(ContentMode.HTML); + + Element e = new Element(Tag.valueOf("v-label"), "", new Attributes()); + l.writeDesign(e, ctx); + assertFalse("Label should not be marked as plain text", + e.hasAttr("plain-text")); + } + + @Test + public void testChangeContentMode() { + Label l = new Label("html label"); + l.setContentMode(ContentMode.HTML); + + Element e = new Element(Tag.valueOf("v-label"), "", new Attributes()); + l.writeDesign(e, ctx); + + assertFalse("Label should not be marked as plain text", + e.hasAttr("plain-text")); + l.setContentMode(ContentMode.TEXT); + l.writeDesign(e, ctx); + + assertTrue("Label should be marked as plain text", + e.hasAttr("plain-text")); + } + + @Test + public void testWithoutContentAndCaption() { + createAndTestLabel(null, null); + } + + private void createAndTestLabel(String content, String caption) { + Label l = new Label(content); + if (caption != null) { + l.setCaption(caption); + } + Element e = ctx.createNode(l); + assertEquals("Wrong tag name for label.", "v-label", e.tagName()); + if (content != null) { + assertEquals("Unexpected content in the v-label element.", content, + e.html()); + } else { + assertTrue("Unexpected content in the v-label element.", + e.html() == null || "".equals(e.html())); + } + if (caption != null) { + assertEquals("Wrong caption in the v-label element.", caption, + e.attr("caption")); + } else { + assertTrue("Unexpected caption in the v-label element.", + e.attr("caption") == null || "".equals(e.attr("caption"))); + } + } +} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/panel/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/panel/TestReadDesign.java new file mode 100644 index 0000000000..3eb52309a8 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/panel/TestReadDesign.java @@ -0,0 +1,98 @@ +/* + * Copyright 2000-2014 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.server.component.panel; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.Panel; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.ui.declarative.DesignException; + +/** + * Test case for reading the attributes of a Panel from design. + * + * @author Vaadin Ltd + */ +public class TestReadDesign extends TestCase { + DesignContext ctx; + + @Override + public void setUp() { + ctx = new DesignContext(); + } + + public void testAttributes() { + Element design = createDesign(); + Panel panel = new Panel(); + panel.readDesign(design, ctx); + assertEquals("A panel", panel.getCaption()); + assertEquals(2, panel.getTabIndex()); + assertEquals(10, panel.getScrollLeft()); + assertEquals(20, panel.getScrollTop()); + assertEquals(200f, panel.getWidth()); + assertEquals(150f, panel.getHeight()); + } + + public void testChild() { + Element design = createDesign(); + Panel panel = new Panel(); + panel.readDesign(design, ctx); + VerticalLayout vLayout = (VerticalLayout) panel.getContent(); + assertEquals(300f, vLayout.getWidth()); + assertEquals(400f, vLayout.getHeight()); + } + + public void testWithMoreThanOneChild() { + Element design = createDesign(); + // Add a new child to the panel element. An exception should be + // thrown when parsing the design. + Element newChild = new Element(Tag.valueOf("v-horizontal-layout"), ""); + design.appendChild(newChild); + Panel panel = new Panel(); + try { + panel.readDesign(design, ctx); + fail("Parsing a design containing a Panel with more than one child component should have failed."); + } catch (DesignException e) { + // Nothing needs to be done, this is the expected case. + } + } + + /* + * Creates an html document that can be parsed into a valid component + * hierarchy. + */ + private Element createDesign() { + // Create a node defining a Panel + Element panelElement = new Element(Tag.valueOf("v-panel"), ""); + panelElement.attr("caption", "A panel"); + panelElement.attr("tabindex", "2"); + panelElement.attr("scroll-left", "10"); + panelElement.attr("scroll-top", "20"); + panelElement.attr("width", "200px"); + panelElement.attr("height", "150px"); + // Add some content to the panel + Element layoutElement = new Element(Tag.valueOf("v-vertical-layout"), + ""); + layoutElement.attr("width", "300px"); + layoutElement.attr("height", "400px"); + panelElement.appendChild(layoutElement); + return panelElement; + } +} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/panel/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/panel/TestSynchronizeFromDesign.java deleted file mode 100644 index bc4a597a4e..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/panel/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.panel; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.Panel; -import com.vaadin.ui.VerticalLayout; -import com.vaadin.ui.declarative.DesignContext; -import com.vaadin.ui.declarative.DesignException; - -/** - * Test case for reading the attributes of a Panel from design. - * - * @author Vaadin Ltd - */ -public class TestSynchronizeFromDesign extends TestCase { - DesignContext ctx; - - @Override - public void setUp() { - ctx = new DesignContext(); - } - - public void testAttributes() { - Element design = createDesign(); - Panel panel = new Panel(); - panel.synchronizeFromDesign(design, ctx); - assertEquals("A panel", panel.getCaption()); - assertEquals(2, panel.getTabIndex()); - assertEquals(10, panel.getScrollLeft()); - assertEquals(20, panel.getScrollTop()); - assertEquals(200f, panel.getWidth()); - assertEquals(150f, panel.getHeight()); - } - - public void testChild() { - Element design = createDesign(); - Panel panel = new Panel(); - panel.synchronizeFromDesign(design, ctx); - VerticalLayout vLayout = (VerticalLayout) panel.getContent(); - assertEquals(300f, vLayout.getWidth()); - assertEquals(400f, vLayout.getHeight()); - } - - public void testWithMoreThanOneChild() { - Element design = createDesign(); - // Add a new child to the panel element. An exception should be - // thrown when parsing the design. - Element newChild = new Element(Tag.valueOf("v-horizontal-layout"), ""); - design.appendChild(newChild); - Panel panel = new Panel(); - try { - panel.synchronizeFromDesign(design, ctx); - fail("Parsing a design containing a Panel with more than one child component should have failed."); - } catch (DesignException e) { - // Nothing needs to be done, this is the expected case. - } - } - - /* - * Creates an html document that can be parsed into a valid component - * hierarchy. - */ - private Element createDesign() { - // Create a node defining a Panel - Element panelElement = new Element(Tag.valueOf("v-panel"), ""); - panelElement.attr("caption", "A panel"); - panelElement.attr("tabindex", "2"); - panelElement.attr("scroll-left", "10"); - panelElement.attr("scroll-top", "20"); - panelElement.attr("width", "200px"); - panelElement.attr("height", "150px"); - // Add some content to the panel - Element layoutElement = new Element(Tag.valueOf("v-vertical-layout"), - ""); - layoutElement.attr("width", "300px"); - layoutElement.attr("height", "400px"); - panelElement.appendChild(layoutElement); - return panelElement; - } -} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/server/component/panel/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/panel/TestSynchronizeToDesign.java deleted file mode 100644 index 56ff155612..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/panel/TestSynchronizeToDesign.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.panel; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.Panel; -import com.vaadin.ui.VerticalLayout; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for writing the attributes and the child element of a Panel to a - * design. - * - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - Element panelElement; - - @Override - public void setUp() { - // create a component hierarchy - Panel panel = new Panel("A panel"); - panel.setId("panelId"); - panel.setHeight("250px"); - panel.setScrollTop(50); - panel.setTabIndex(4); - VerticalLayout vLayout = new VerticalLayout(); - vLayout.setWidth("500px"); - panel.setContent(vLayout); - // synchronize to design - DesignContext ctx = new DesignContext(); - panelElement = new Element(Tag.valueOf("div"), ""); - panel.synchronizeToDesign(panelElement, ctx); - } - - public void testAttributes() { - // should have caption, id, height, scroll top and tab index - assertEquals(5, panelElement.attributes().size()); - // check the values of the attributes - assertEquals("A panel", panelElement.attr("caption")); - assertEquals("panelId", panelElement.attr("id")); - assertEquals("250px", panelElement.attr("height")); - assertEquals("50", panelElement.attr("scroll-top")); - assertEquals("4", panelElement.attr("tabindex")); - } - - public void testChild() { - // the panel element should have exactly one child, a v-vertical-layout - assertEquals(1, panelElement.childNodes().size()); - Element vLayoutElement = panelElement.child(0); - assertEquals("v-vertical-layout", vLayoutElement.nodeName()); - assertEquals("500px", vLayoutElement.attr("width")); - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/panel/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/panel/TestWriteDesign.java new file mode 100644 index 0000000000..f81193d511 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/panel/TestWriteDesign.java @@ -0,0 +1,71 @@ +/* + * Copyright 2000-2014 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.server.component.panel; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.Panel; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing the attributes and the child element of a Panel to a + * design. + * + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + Element panelElement; + + @Override + public void setUp() { + // create a component hierarchy + Panel panel = new Panel("A panel"); + panel.setId("panelId"); + panel.setHeight("250px"); + panel.setScrollTop(50); + panel.setTabIndex(4); + VerticalLayout vLayout = new VerticalLayout(); + vLayout.setWidth("500px"); + panel.setContent(vLayout); + // synchronize to design + DesignContext ctx = new DesignContext(); + panelElement = new Element(Tag.valueOf("div"), ""); + panel.writeDesign(panelElement, ctx); + } + + public void testAttributes() { + // should have caption, id, height, scroll top and tab index + assertEquals(5, panelElement.attributes().size()); + // check the values of the attributes + assertEquals("A panel", panelElement.attr("caption")); + assertEquals("panelId", panelElement.attr("id")); + assertEquals("250px", panelElement.attr("height")); + assertEquals("50", panelElement.attr("scroll-top")); + assertEquals("4", panelElement.attr("tabindex")); + } + + public void testChild() { + // the panel element should have exactly one child, a v-vertical-layout + assertEquals(1, panelElement.childNodes().size()); + Element vLayoutElement = panelElement.child(0); + assertEquals("v-vertical-layout", vLayoutElement.nodeName()); + assertEquals("500px", vLayoutElement.attr("width")); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestReadDesign.java new file mode 100644 index 0000000000..6a34d6584a --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestReadDesign.java @@ -0,0 +1,136 @@ +/* + * Copyright 2000-2014 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.server.component.tabsheet; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.server.ExternalResource; +import com.vaadin.ui.Label; +import com.vaadin.ui.TabSheet; +import com.vaadin.ui.TabSheet.Tab; +import com.vaadin.ui.TextField; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case from reading TabSheet from design + * + * @since + * @author Vaadin Ltd + */ +public class TestReadDesign extends TestCase { + + private TabSheet sheet; + + @Override + protected void setUp() throws Exception { + super.setUp(); + sheet = createTabSheet(); + } + + public void testChildCount() { + assertEquals(1, sheet.getComponentCount()); + } + + public void testTabIndex() { + assertEquals(5, sheet.getTabIndex()); + } + + public void testTabAttributes() { + Tab tab = sheet.getTab(0); + assertEquals("test-caption", tab.getCaption()); + assertEquals(false, tab.isVisible()); + assertEquals(false, tab.isClosable()); + assertEquals(false, tab.isEnabled()); + assertEquals("http://www.vaadin.com/test.png", + ((ExternalResource) tab.getIcon()).getURL()); + assertEquals("OK", tab.getIconAlternateText()); + assertEquals("test-desc", tab.getDescription()); + assertEquals("test-style", tab.getStyleName()); + assertEquals("test-id", tab.getId()); + } + + public void testSelectedComponent() { + TabSheet tabSheet = new TabSheet(); + tabSheet.readDesign(createFirstTabSelectedDesign(), new DesignContext()); + assertEquals(tabSheet.getTab(0).getComponent(), + tabSheet.getSelectedTab()); + } + + public void testTabContent() { + assertTrue("The child for the tabsheet should be textfield", sheet + .getTab(0).getComponent() instanceof TextField); + } + + private TabSheet createTabSheet() { + TabSheet tabSheet = new TabSheet(); + // add some tabs that should be cleared on sync + tabSheet.addComponent(new Label("tab1")); + tabSheet.addComponent(new Label("tab2")); + DesignContext ctx = new DesignContext(); + Element design = createDesign(); + tabSheet.readDesign(design, ctx); + return tabSheet; + } + + private Element createDesign() { + // create root design + Attributes rootAttributes = new Attributes(); + rootAttributes.put("tabindex", "5"); + Element node = new Element(Tag.valueOf("v-tab-sheet"), "", + rootAttributes); + // create tab design + Attributes tabAttributes = new Attributes(); + tabAttributes.put("caption", "test-caption"); + tabAttributes.put("visible", "false"); + tabAttributes.put("closable", "false"); + tabAttributes.put("enabled", "false"); + tabAttributes.put("icon", "http://www.vaadin.com/test.png"); + tabAttributes.put("icon-alt", "OK"); + tabAttributes.put("description", "test-desc"); + tabAttributes.put("style-name", "test-style"); + tabAttributes.put("id", "test-id"); + Element tab = new Element(Tag.valueOf("tab"), "", tabAttributes); + // add child component to tab + tab.appendChild(new Element(Tag.valueOf("v-text-field"), "", + new Attributes())); + // add tab to root design + node.appendChild(tab); + return node; + } + + private Element createFirstTabSelectedDesign() { + // create root design + Attributes rootAttributes = new Attributes(); + Element node = new Element(Tag.valueOf("v-tab-sheet"), "", + rootAttributes); + // create tab design + Attributes tabAttributes = new Attributes(); + tabAttributes.put("selected", ""); + tabAttributes.put("caption", "test-caption"); + Element tab = new Element(Tag.valueOf("tab"), "", tabAttributes); + // add child component to tab + tab.appendChild(new Element(Tag.valueOf("v-text-field"), "", + new Attributes())); + // add tab to root design + node.appendChild(tab); + return node; + + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestSynchronizeFromDesign.java deleted file mode 100644 index 4c0a2863f0..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.tabsheet; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.server.ExternalResource; -import com.vaadin.ui.Label; -import com.vaadin.ui.TabSheet; -import com.vaadin.ui.TabSheet.Tab; -import com.vaadin.ui.TextField; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case from reading TabSheet from design - * - * @since - * @author Vaadin Ltd - */ -public class TestSynchronizeFromDesign extends TestCase { - - private TabSheet sheet; - - @Override - protected void setUp() throws Exception { - super.setUp(); - sheet = createTabSheet(); - } - - public void testChildCount() { - assertEquals(1, sheet.getComponentCount()); - } - - public void testTabIndex() { - assertEquals(5, sheet.getTabIndex()); - } - - public void testTabAttributes() { - Tab tab = sheet.getTab(0); - assertEquals("test-caption", tab.getCaption()); - assertEquals(false, tab.isVisible()); - assertEquals(false, tab.isClosable()); - assertEquals(false, tab.isEnabled()); - assertEquals("http://www.vaadin.com/test.png", - ((ExternalResource) tab.getIcon()).getURL()); - assertEquals("OK", tab.getIconAlternateText()); - assertEquals("test-desc", tab.getDescription()); - assertEquals("test-style", tab.getStyleName()); - assertEquals("test-id", tab.getId()); - } - - public void testSelectedComponent() { - TabSheet tabSheet = new TabSheet(); - tabSheet.synchronizeFromDesign(createFirstTabSelectedDesign(), - new DesignContext()); - assertEquals(tabSheet.getTab(0).getComponent(), - tabSheet.getSelectedTab()); - } - - public void testTabContent() { - assertTrue("The child for the tabsheet should be textfield", sheet - .getTab(0).getComponent() instanceof TextField); - } - - private TabSheet createTabSheet() { - TabSheet tabSheet = new TabSheet(); - // add some tabs that should be cleared on sync - tabSheet.addComponent(new Label("tab1")); - tabSheet.addComponent(new Label("tab2")); - DesignContext ctx = new DesignContext(); - Element design = createDesign(); - tabSheet.synchronizeFromDesign(design, ctx); - return tabSheet; - } - - private Element createDesign() { - // create root design - Attributes rootAttributes = new Attributes(); - rootAttributes.put("tabindex", "5"); - Element node = new Element(Tag.valueOf("v-tab-sheet"), "", - rootAttributes); - // create tab design - Attributes tabAttributes = new Attributes(); - tabAttributes.put("caption", "test-caption"); - tabAttributes.put("visible", "false"); - tabAttributes.put("closable", "false"); - tabAttributes.put("enabled", "false"); - tabAttributes.put("icon", "http://www.vaadin.com/test.png"); - tabAttributes.put("icon-alt", "OK"); - tabAttributes.put("description", "test-desc"); - tabAttributes.put("style-name", "test-style"); - tabAttributes.put("id", "test-id"); - Element tab = new Element(Tag.valueOf("tab"), "", tabAttributes); - // add child component to tab - tab.appendChild(new Element(Tag.valueOf("v-text-field"), "", - new Attributes())); - // add tab to root design - node.appendChild(tab); - return node; - } - - private Element createFirstTabSelectedDesign() { - // create root design - Attributes rootAttributes = new Attributes(); - Element node = new Element(Tag.valueOf("v-tab-sheet"), "", - rootAttributes); - // create tab design - Attributes tabAttributes = new Attributes(); - tabAttributes.put("selected", ""); - tabAttributes.put("caption", "test-caption"); - Element tab = new Element(Tag.valueOf("tab"), "", tabAttributes); - // add child component to tab - tab.appendChild(new Element(Tag.valueOf("v-text-field"), "", - new Attributes())); - // add tab to root design - node.appendChild(tab); - return node; - - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestSynchronizeToDesign.java deleted file mode 100644 index c33a1da4d7..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestSynchronizeToDesign.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.tabsheet; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.server.ExternalResource; -import com.vaadin.ui.TabSheet; -import com.vaadin.ui.TabSheet.Tab; -import com.vaadin.ui.TextField; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for writing TabSheet to design - * - * @since - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - - private TabSheet sheet; - private Element design; - - @Override - protected void setUp() throws Exception { - super.setUp(); - sheet = createTabSheet(); - design = createDesign(); - sheet.synchronizeToDesign(design, createDesignContext()); - } - - public void testOnlyOneTab() { - assertEquals("There should be only one child", 1, design.children() - .size()); - } - - public void testAttributes() { - Element tabDesign = design.child(0); - assertEquals("5", design.attr("tabindex")); - assertEquals("test-caption", tabDesign.attr("caption")); - assertEquals("false", tabDesign.attr("visible")); - assertTrue(tabDesign.hasAttr("closable")); - assertTrue(tabDesign.attr("closable").equals("true") - || tabDesign.attr("closable").equals("")); - assertEquals("false", tabDesign.attr("enabled")); - assertEquals("http://www.vaadin.com/test.png", tabDesign.attr("icon")); - assertEquals("OK", tabDesign.attr("icon-alt")); - assertEquals("test-desc", tabDesign.attr("description")); - assertEquals("test-style", tabDesign.attr("style-name")); - assertEquals("test-id", tabDesign.attr("id")); - } - - public void testContent() { - Element tabDesign = design.child(0); - Element content = tabDesign.child(0); - assertEquals("Tab must have only one child", 1, tabDesign.children() - .size()); - assertEquals("v-text-field", content.tagName()); - } - - private Element createDesign() { - // make sure that the design node has old content that should be removed - Element node = new Element(Tag.valueOf("v-tab-sheet"), "", - new Attributes()); - node.appendChild(new Element(Tag.valueOf("tab"), "", new Attributes())); - node.appendChild(new Element(Tag.valueOf("tab"), "", new Attributes())); - node.appendChild(new Element(Tag.valueOf("tab"), "", new Attributes())); - return node; - } - - private DesignContext createDesignContext() { - return new DesignContext(); - } - - private TabSheet createTabSheet() { - TabSheet sheet = new TabSheet(); - sheet.setTabIndex(5); - sheet.addTab(new TextField()); - Tab tab = sheet.getTab(0); - tab.setCaption("test-caption"); - tab.setVisible(false); - tab.setClosable(true); - tab.setEnabled(false); - tab.setIcon(new ExternalResource("http://www.vaadin.com/test.png")); - tab.setIconAlternateText("OK"); - tab.setDescription("test-desc"); - tab.setStyleName("test-style"); - tab.setId("test-id"); - return sheet; - } - -} diff --git a/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestWriteDesign.java new file mode 100644 index 0000000000..a46b33e2d6 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/tabsheet/TestWriteDesign.java @@ -0,0 +1,109 @@ +/* + * Copyright 2000-2014 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.server.component.tabsheet; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.server.ExternalResource; +import com.vaadin.ui.TabSheet; +import com.vaadin.ui.TabSheet.Tab; +import com.vaadin.ui.TextField; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing TabSheet to design + * + * @since + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + + private TabSheet sheet; + private Element design; + + @Override + protected void setUp() throws Exception { + super.setUp(); + sheet = createTabSheet(); + design = createDesign(); + sheet.writeDesign(design, createDesignContext()); + } + + public void testOnlyOneTab() { + assertEquals("There should be only one child", 1, design.children() + .size()); + } + + public void testAttributes() { + Element tabDesign = design.child(0); + assertEquals("5", design.attr("tabindex")); + assertEquals("test-caption", tabDesign.attr("caption")); + assertEquals("false", tabDesign.attr("visible")); + assertTrue(tabDesign.hasAttr("closable")); + assertTrue(tabDesign.attr("closable").equals("true") + || tabDesign.attr("closable").equals("")); + assertEquals("false", tabDesign.attr("enabled")); + assertEquals("http://www.vaadin.com/test.png", tabDesign.attr("icon")); + assertEquals("OK", tabDesign.attr("icon-alt")); + assertEquals("test-desc", tabDesign.attr("description")); + assertEquals("test-style", tabDesign.attr("style-name")); + assertEquals("test-id", tabDesign.attr("id")); + } + + public void testContent() { + Element tabDesign = design.child(0); + Element content = tabDesign.child(0); + assertEquals("Tab must have only one child", 1, tabDesign.children() + .size()); + assertEquals("v-text-field", content.tagName()); + } + + private Element createDesign() { + // make sure that the design node has old content that should be removed + Element node = new Element(Tag.valueOf("v-tab-sheet"), "", + new Attributes()); + node.appendChild(new Element(Tag.valueOf("tab"), "", new Attributes())); + node.appendChild(new Element(Tag.valueOf("tab"), "", new Attributes())); + node.appendChild(new Element(Tag.valueOf("tab"), "", new Attributes())); + return node; + } + + private DesignContext createDesignContext() { + return new DesignContext(); + } + + private TabSheet createTabSheet() { + TabSheet sheet = new TabSheet(); + sheet.setTabIndex(5); + sheet.addTab(new TextField()); + Tab tab = sheet.getTab(0); + tab.setCaption("test-caption"); + tab.setVisible(false); + tab.setClosable(true); + tab.setEnabled(false); + tab.setIcon(new ExternalResource("http://www.vaadin.com/test.png")); + tab.setIconAlternateText("OK"); + tab.setDescription("test-desc"); + tab.setStyleName("test-style"); + tab.setId("test-id"); + return sheet; + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/textarea/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/textarea/TestReadDesign.java new file mode 100644 index 0000000000..a31367008a --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/textarea/TestReadDesign.java @@ -0,0 +1,59 @@ +/* + * Copyright 2000-2014 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.server.component.textarea; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.AbstractTextField; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for reading the value of the TextField from design + * + * @author Vaadin Ltd + */ +public class TestReadDesign extends TestCase { + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testValue() { + Element design = createDesign(); + AbstractTextField component = getComponent(); + component.readDesign(design, ctx); + assertEquals("test value", component.getValue()); + } + + private AbstractTextField getComponent() { + return new TextArea(); + } + + private Element createDesign() { + Attributes attributes = new Attributes(); + Element node = new Element(Tag.valueOf("v-text-area"), "", attributes); + node.html("test value"); + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeFromDesign.java deleted file mode 100644 index 787eac0a03..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.textarea; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.AbstractTextField; -import com.vaadin.ui.TextArea; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for reading the value of the TextField from design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeFromDesign extends TestCase { - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - public void testValue() { - Element design = createDesign(); - AbstractTextField component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals("test value", component.getValue()); - } - - private AbstractTextField getComponent() { - return new TextArea(); - } - - private Element createDesign() { - Attributes attributes = new Attributes(); - Element node = new Element(Tag.valueOf("v-text-area"), "", attributes); - node.html("test value"); - return node; - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeToDesign.java deleted file mode 100644 index a316b02ddc..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/textarea/TestSynchronizeToDesign.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.textarea; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.AbstractTextField; -import com.vaadin.ui.TextArea; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for writing the value of the TextField to design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - public void testSynchronizeValue() { - Element design = createDesign(); - AbstractTextField component = getComponent(); - component.setValue("test value"); - component.synchronizeToDesign(design, ctx); - assertEquals("test value", design.html()); - assertFalse(design.hasAttr("value")); - } - - private AbstractTextField getComponent() { - return new TextArea(); - } - - private Element createDesign() { - Attributes attr = new Attributes(); - return new Element(Tag.valueOf("v-text-area"), "", attr); - } - -} diff --git a/server/tests/src/com/vaadin/tests/server/component/textarea/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/textarea/TestWriteDesign.java new file mode 100644 index 0000000000..01b0095fe9 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/textarea/TestWriteDesign.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2014 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.server.component.textarea; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.AbstractTextField; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing the value of the TextField to design + * + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testSynchronizeValue() { + Element design = createDesign(); + AbstractTextField component = getComponent(); + component.setValue("test value"); + component.writeDesign(design, ctx); + assertEquals("test value", design.html()); + assertFalse(design.hasAttr("value")); + } + + private AbstractTextField getComponent() { + return new TextArea(); + } + + private Element createDesign() { + Attributes attr = new Attributes(); + return new Element(Tag.valueOf("v-text-area"), "", attr); + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/textfield/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/textfield/TestReadDesign.java new file mode 100644 index 0000000000..62cb1a53bc --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/textfield/TestReadDesign.java @@ -0,0 +1,59 @@ +/* + * Copyright 2000-2014 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.server.component.textfield; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.AbstractTextField; +import com.vaadin.ui.TextField; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for reading the value of the TextField from design + * + * @author Vaadin Ltd + */ +public class TestReadDesign extends TestCase { + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testValue() { + Element design = createDesign(); + AbstractTextField component = getComponent(); + component.readDesign(design, ctx); + assertEquals("test value", component.getValue()); + } + + private AbstractTextField getComponent() { + return new TextField(); + } + + private Element createDesign() { + Attributes attributes = new Attributes(); + attributes.put("value", "test value"); + Element node = new Element(Tag.valueOf("v-text-field"), "", attributes); + return node; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeFromDesign.java deleted file mode 100644 index 2df0ab4855..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeFromDesign.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.textfield; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.AbstractTextField; -import com.vaadin.ui.TextField; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for reading the value of the TextField from design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeFromDesign extends TestCase { - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - public void testValue() { - Element design = createDesign(); - AbstractTextField component = getComponent(); - component.synchronizeFromDesign(design, ctx); - assertEquals("test value", component.getValue()); - } - - private AbstractTextField getComponent() { - return new TextField(); - } - - private Element createDesign() { - Attributes attributes = new Attributes(); - attributes.put("value", "test value"); - Element node = new Element(Tag.valueOf("v-text-field"), "", attributes); - return node; - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeToDesign.java deleted file mode 100644 index 58d361e683..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/textfield/TestSynchronizeToDesign.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2000-2014 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.server.component.textfield; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Attributes; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Tag; - -import com.vaadin.ui.AbstractTextField; -import com.vaadin.ui.TextField; -import com.vaadin.ui.declarative.DesignContext; - -/** - * Test case for writing the value of the TextField to design - * - * @author Vaadin Ltd - */ -public class TestSynchronizeToDesign extends TestCase { - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = new DesignContext(); - } - - public void testSynchronizeValue() { - Element design = createDesign(); - AbstractTextField component = getComponent(); - component.setValue("test value"); - component.synchronizeToDesign(design, ctx); - assertEquals("test value", design.attr("value")); - } - - private AbstractTextField getComponent() { - return new TextField(); - } - - private Element createDesign() { - Attributes attr = new Attributes(); - return new Element(Tag.valueOf("v-text-field"), "", attr); - } - -} diff --git a/server/tests/src/com/vaadin/tests/server/component/textfield/TestWriteDesign.java b/server/tests/src/com/vaadin/tests/server/component/textfield/TestWriteDesign.java new file mode 100644 index 0000000000..479c94f6a8 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/textfield/TestWriteDesign.java @@ -0,0 +1,59 @@ +/* + * Copyright 2000-2014 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.server.component.textfield; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.AbstractTextField; +import com.vaadin.ui.TextField; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Test case for writing the value of the TextField to design + * + * @author Vaadin Ltd + */ +public class TestWriteDesign extends TestCase { + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = new DesignContext(); + } + + public void testSynchronizeValue() { + Element design = createDesign(); + AbstractTextField component = getComponent(); + component.setValue("test value"); + component.writeDesign(design, ctx); + assertEquals("test value", design.attr("value")); + } + + private AbstractTextField getComponent() { + return new TextField(); + } + + private Element createDesign() { + Attributes attr = new Attributes(); + return new Element(Tag.valueOf("v-text-field"), "", attr); + } + +} -- cgit v1.2.3 From 8eafe7aee31b3e2f80c87b39c4e42e260a86a5eb Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 15 Dec 2014 08:41:45 +0200 Subject: Do not create default instances while reading designs (#7749) Change-Id: I26cb6d8d43200d10ebf8c0ac22c538b4272f5ecd --- common.xml | 4 ++ server/src/com/vaadin/ui/AbstractComponent.java | 21 ++---- server/src/com/vaadin/ui/AbstractField.java | 15 ++-- .../src/com/vaadin/ui/AbstractOrderedLayout.java | 12 +--- server/src/com/vaadin/ui/AbstractSplitPanel.java | 28 +++----- server/src/com/vaadin/ui/AbstractTextField.java | 9 ++- server/src/com/vaadin/ui/Button.java | 18 +++-- server/src/com/vaadin/ui/Panel.java | 3 +- server/src/com/vaadin/ui/PasswordField.java | 9 ++- server/src/com/vaadin/ui/TabSheet.java | 79 ++++++++++++++-------- server/src/com/vaadin/ui/TextField.java | 9 ++- .../ui/declarative/DesignAttributeHandler.java | 23 +++---- .../tests/design/designroot/DesignRootTest.java | 3 - .../abstractsplitpanel/TestReadDesign.java | 15 ---- 14 files changed, 114 insertions(+), 134 deletions(-) (limited to 'server/src/com/vaadin/ui/PasswordField.java') diff --git a/common.xml b/common.xml index 6e28ab6a60..edd941c5e3 100644 --- a/common.xml +++ b/common.xml @@ -386,6 +386,10 @@ + + + + diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java index 3c68fde9b2..0b9f8bd244 100644 --- a/server/src/com/vaadin/ui/AbstractComponent.java +++ b/server/src/com/vaadin/ui/AbstractComponent.java @@ -920,11 +920,13 @@ public abstract class AbstractComponent extends AbstractClientConnector @Override public void readDesign(Element design, DesignContext designContext) { Attributes attr = design.attributes(); - AbstractComponent def = designContext.getDefaultInstance(this - .getClass()); // handle default attributes for (String attribute : getDefaultAttributes()) { - DesignAttributeHandler.readAttribute(this, attribute, attr, def); + if (!design.hasAttr(attribute)) { + continue; + } + + DesignAttributeHandler.readAttribute(this, attribute, attr); } // handle immediate if (attr.hasKey("immediate")) { @@ -941,14 +943,12 @@ public abstract class AbstractComponent extends AbstractClientConnector setLocale(null); } // handle width and height - readSize(attr, def); + readSize(attr); // handle component error if (attr.hasKey("error")) { UserError error = new UserError(attr.get("error"), ContentMode.HTML, ErrorLevel.ERROR); setComponentError(error); - } else { - setComponentError(def.getComponentError()); } // handle responsive setResponsive(attr.hasKey("responsive") @@ -1045,8 +1045,7 @@ public abstract class AbstractComponent extends AbstractClientConnector * @param defaultInstance * instance of the class that has default sizing. */ - private void readSize(Attributes attributes, - AbstractComponent defaultInstance) { + private void readSize(Attributes attributes) { // read width if (attributes.hasKey("width-auto") || attributes.hasKey("size-auto")) { this.setWidth(null); @@ -1055,9 +1054,6 @@ public abstract class AbstractComponent extends AbstractClientConnector this.setWidth("100%"); } else if (attributes.hasKey("width")) { this.setWidth(attributes.get("width")); - } else { - this.setWidth(defaultInstance.getWidth(), - defaultInstance.getWidthUnits()); } // read height @@ -1068,9 +1064,6 @@ public abstract class AbstractComponent extends AbstractClientConnector this.setHeight("100%"); } else if (attributes.hasKey("height")) { this.setHeight(attributes.get("height")); - } else { - this.setHeight(defaultInstance.getHeight(), - defaultInstance.getHeightUnits()); } } diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java index ba518000d6..c3e2036936 100644 --- a/server/src/com/vaadin/ui/AbstractField.java +++ b/server/src/com/vaadin/ui/AbstractField.java @@ -1767,15 +1767,16 @@ public abstract class AbstractField extends AbstractComponent implements @Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); - AbstractField def = designContext.getDefaultInstance(this.getClass()); Attributes attr = design.attributes(); - boolean readOnly = DesignAttributeHandler.readAttribute("readonly", - attr, def.isReadOnly(), Boolean.class); - setReadOnly(readOnly); + if (design.hasAttr("readonly")) { + setReadOnly(DesignAttributeHandler.readAttribute("readonly", attr, + Boolean.class)); + } // tabindex - int tabIndex = DesignAttributeHandler.readAttribute("tabindex", attr, - def.getTabIndex(), Integer.class); - setTabIndex(tabIndex); + if (design.hasAttr("tabindex")) { + setTabIndex(DesignAttributeHandler.readAttribute("tabindex", attr, + Integer.class)); + } } /* diff --git a/server/src/com/vaadin/ui/AbstractOrderedLayout.java b/server/src/com/vaadin/ui/AbstractOrderedLayout.java index e99641a9ab..cd5609f091 100644 --- a/server/src/com/vaadin/ui/AbstractOrderedLayout.java +++ b/server/src/com/vaadin/ui/AbstractOrderedLayout.java @@ -477,18 +477,10 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements public void readDesign(Element design, DesignContext designContext) { // process default attributes super.readDesign(design, designContext); - // remove current children - removeAllComponents(); // handle margin - AbstractOrderedLayout def = designContext.getDefaultInstance(this - .getClass()); if (design.hasAttr("margin")) { - String value = design.attr("margin"); - setMargin(value.isEmpty() || value.equalsIgnoreCase("true")); - - } else { - // we currently support only on-off margins - setMargin(def.getMargin().getBitMask() != 0); + setMargin(DesignAttributeHandler.readAttribute("margin", + design.attributes(), Boolean.class)); } // handle children for (Element childComponent : design.children()) { diff --git a/server/src/com/vaadin/ui/AbstractSplitPanel.java b/server/src/com/vaadin/ui/AbstractSplitPanel.java index 66d01084d1..9c33665c1c 100644 --- a/server/src/com/vaadin/ui/AbstractSplitPanel.java +++ b/server/src/com/vaadin/ui/AbstractSplitPanel.java @@ -566,41 +566,31 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer { super.readDesign(design, designContext); // handle custom attributes, use default values if no explicit value // set - AbstractSplitPanel def = designContext.getDefaultInstance(this - .getClass()); // There is no setter for reversed, so it will be handled using // setSplitPosition. - boolean reversed = DesignAttributeHandler.readAttribute("reversed", - design.attributes(), def.getSplitterState().positionReversed, - Boolean.class); + boolean reversed = false; + if (design.hasAttr("reversed")) { + reversed = DesignAttributeHandler.readAttribute("reversed", + design.attributes(), Boolean.class); + setSplitPosition(getSplitPosition(), reversed); + } if (design.hasAttr("split-position")) { SizeWithUnit splitPosition = SizeWithUnit.parseStringSize( - design.attr("split-position"), def.getSplitPositionUnit()); + design.attr("split-position"), Unit.PERCENTAGE); setSplitPosition(splitPosition.getSize(), splitPosition.getUnit(), reversed); - } else { // default value for split position - setSplitPosition(def.getSplitPosition(), - def.getSplitPositionUnit(), reversed); } if (design.hasAttr("min-split-position")) { SizeWithUnit minSplitPosition = SizeWithUnit.parseStringSize( - design.attr("min-split-position"), - def.getMinSplitPositionUnit()); + design.attr("min-split-position"), Unit.PERCENTAGE); setMinSplitPosition(minSplitPosition.getSize(), minSplitPosition.getUnit()); - } else { // default value for min-split-position - setMinSplitPosition(def.getMinSplitPosition(), - def.getMinSplitPositionUnit()); } if (design.hasAttr("max-split-position")) { SizeWithUnit maxSplitPosition = SizeWithUnit.parseStringSize( - design.attr("max-split-position"), - def.getMaxSplitPositionUnit()); + design.attr("max-split-position"), Unit.PERCENTAGE); setMaxSplitPosition(maxSplitPosition.getSize(), maxSplitPosition.getUnit()); - } else { // default value for max-split-position - setMaxSplitPosition(def.getMaxSplitPosition(), - def.getMaxSplitPositionUnit()); } // remove current children removeAllComponents(); diff --git a/server/src/com/vaadin/ui/AbstractTextField.java b/server/src/com/vaadin/ui/AbstractTextField.java index 79492ecf5e..8bca74936a 100644 --- a/server/src/com/vaadin/ui/AbstractTextField.java +++ b/server/src/com/vaadin/ui/AbstractTextField.java @@ -772,12 +772,11 @@ public abstract class AbstractTextField extends AbstractField implements @Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); - AbstractTextField def = designContext.getDefaultInstance(this - .getClass()); Attributes attr = design.attributes(); - int maxLength = DesignAttributeHandler.readAttribute("maxlength", attr, - def.getMaxLength(), Integer.class); - setMaxLength(maxLength); + if (attr.hasKey("maxlength")) { + setMaxLength(DesignAttributeHandler.readAttribute("maxlength", + attr, Integer.class)); + } } /* diff --git a/server/src/com/vaadin/ui/Button.java b/server/src/com/vaadin/ui/Button.java index 2d0a0cf8da..9b50b5eebc 100644 --- a/server/src/com/vaadin/ui/Button.java +++ b/server/src/com/vaadin/ui/Button.java @@ -675,22 +675,26 @@ public class Button extends AbstractComponent implements @Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); - Button def = designContext.getDefaultInstance(this.getClass()); Attributes attr = design.attributes(); String content = design.html(); setCaption(content); // tabindex - setTabIndex(DesignAttributeHandler.readAttribute("tabindex", attr, - def.getTabIndex(), Integer.class)); + if (attr.hasKey("tabindex")) { + setTabIndex(DesignAttributeHandler.readAttribute("tabindex", attr, + Integer.class)); + } // plain-text (default is html) - setHtmlContentAllowed(!DesignAttributeHandler.readAttribute( - DESIGN_ATTR_PLAIN_TEXT, attr, false, Boolean.class)); + Boolean plain = DesignAttributeHandler.readAttribute( + DESIGN_ATTR_PLAIN_TEXT, attr, Boolean.class); + if (plain == null || !plain) { + setHtmlContentAllowed(true); + } setIconAlternateText(DesignAttributeHandler.readAttribute("icon-alt", - attr, def.getIconAlternateText(), String.class)); + attr, String.class)); // click-shortcut removeClickShortcut(); ShortcutAction action = DesignAttributeHandler.readAttribute( - "click-shortcut", attr, null, ShortcutAction.class); + "click-shortcut", attr, ShortcutAction.class); if (action != null) { setClickShortcut(action.getKeyCode(), action.getModifiers()); } diff --git a/server/src/com/vaadin/ui/Panel.java b/server/src/com/vaadin/ui/Panel.java index 5112a732ac..b0242cc32d 100644 --- a/server/src/com/vaadin/ui/Panel.java +++ b/server/src/com/vaadin/ui/Panel.java @@ -348,9 +348,8 @@ public class Panel extends AbstractSingleComponentContainer implements public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); // handle tabindex - Panel def = designContext.getDefaultInstance(this.getClass()); int tabIndex = DesignAttributeHandler.readAttribute("tabindex", - design.attributes(), def.getTabIndex(), Integer.class); + design.attributes(), Integer.class); setTabIndex(tabIndex); } diff --git a/server/src/com/vaadin/ui/PasswordField.java b/server/src/com/vaadin/ui/PasswordField.java index 5f27eb59c9..974a1c2c10 100644 --- a/server/src/com/vaadin/ui/PasswordField.java +++ b/server/src/com/vaadin/ui/PasswordField.java @@ -91,12 +91,11 @@ public class PasswordField extends AbstractTextField { @Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); - AbstractTextField def = designContext.getDefaultInstance(this - .getClass()); Attributes attr = design.attributes(); - String value = DesignAttributeHandler.readAttribute("value", attr, - def.getValue(), String.class); - setValue(value); + if (attr.hasKey("value")) { + setValue(DesignAttributeHandler.readAttribute("value", attr, + String.class)); + } } /* diff --git a/server/src/com/vaadin/ui/TabSheet.java b/server/src/com/vaadin/ui/TabSheet.java index 94bcb80279..330acacfd0 100644 --- a/server/src/com/vaadin/ui/TabSheet.java +++ b/server/src/com/vaadin/ui/TabSheet.java @@ -1464,12 +1464,8 @@ public class TabSheet extends AbstractComponentContainer implements Focusable, @Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); - Attributes attr = design.attributes(); - TabSheet def = designContext.getDefaultInstance(this.getClass()); // handle tab index - int tabIndex = DesignAttributeHandler.readAttribute("tabindex", attr, - def.getTabIndex(), Integer.class); - setTabIndex(tabIndex); + readTabIndex(design); // clear old tabs removeAllComponents(); // create new tabs @@ -1482,6 +1478,15 @@ public class TabSheet extends AbstractComponentContainer implements Focusable, } } + private void readTabIndex(Element design) { + // Could be in AbstractComponent as if (this implements Focusable) + if (design.hasAttr("tabindex")) { + setTabIndex(DesignAttributeHandler.readAttribute("tabindex", + design.attributes(), Integer.class)); + } + + } + /** * Reads the given tab element from design * @@ -1503,28 +1508,48 @@ public class TabSheet extends AbstractComponentContainer implements Focusable, Element content = tabElement.child(0); Component child = designContext.createChild(content); Tab tab = this.addTab(child); - tab.setVisible(DesignAttributeHandler.readAttribute("visible", attr, - tab.isVisible(), Boolean.class)); - tab.setClosable(DesignAttributeHandler.readAttribute("closable", attr, - tab.isClosable(), Boolean.class)); - tab.setCaption(DesignAttributeHandler.readAttribute("caption", attr, - tab.getCaption(), String.class)); - tab.setEnabled(DesignAttributeHandler.readAttribute("enabled", attr, - tab.isEnabled(), Boolean.class)); - tab.setIcon(DesignAttributeHandler.readAttribute("icon", attr, - tab.getIcon(), Resource.class)); - tab.setIconAlternateText(DesignAttributeHandler.readAttribute( - "icon-alt", attr, tab.getIconAlternateText(), String.class)); - tab.setDescription(DesignAttributeHandler.readAttribute("description", - attr, tab.getDescription(), String.class)); - tab.setStyleName(DesignAttributeHandler.readAttribute("style-name", - attr, tab.getStyleName(), String.class)); - tab.setId(DesignAttributeHandler.readAttribute("id", attr, tab.getId(), - String.class)); - boolean selected = DesignAttributeHandler.readAttribute("selected", - attr, false, Boolean.class); - if (selected) { - this.setSelectedTab(tab.getComponent()); + if (attr.hasKey("visible")) { + tab.setVisible(DesignAttributeHandler.readAttribute("visible", + attr, Boolean.class)); + } + if (attr.hasKey("closable")) { + tab.setClosable(DesignAttributeHandler.readAttribute("closable", + attr, Boolean.class)); + } + if (attr.hasKey("caption")) { + tab.setCaption(DesignAttributeHandler.readAttribute("caption", + attr, String.class)); + } + if (attr.hasKey("enabled")) { + tab.setEnabled(DesignAttributeHandler.readAttribute("enabled", + attr, Boolean.class)); + } + if (attr.hasKey("icon")) { + tab.setIcon(DesignAttributeHandler.readAttribute("icon", attr, + Resource.class)); + } + if (attr.hasKey("icon-alt")) { + tab.setIconAlternateText(DesignAttributeHandler.readAttribute( + "icon-alt", attr, String.class)); + } + if (attr.hasKey("description")) { + tab.setDescription(DesignAttributeHandler.readAttribute( + "description", attr, String.class)); + } + if (attr.hasKey("style-name")) { + tab.setStyleName(DesignAttributeHandler.readAttribute("style-name", + attr, String.class)); + } + if (attr.hasKey("id")) { + tab.setId(DesignAttributeHandler.readAttribute("id", attr, + String.class)); + } + if (attr.hasKey("selected")) { + boolean selected = DesignAttributeHandler.readAttribute("selected", + attr, Boolean.class); + if (selected) { + this.setSelectedTab(tab.getComponent()); + } } } diff --git a/server/src/com/vaadin/ui/TextField.java b/server/src/com/vaadin/ui/TextField.java index 03bbfd2d8f..cd919ded9c 100644 --- a/server/src/com/vaadin/ui/TextField.java +++ b/server/src/com/vaadin/ui/TextField.java @@ -113,12 +113,11 @@ public class TextField extends AbstractTextField { @Override public void readDesign(Element design, DesignContext designContext) { super.readDesign(design, designContext); - AbstractTextField def = designContext.getDefaultInstance(this - .getClass()); Attributes attr = design.attributes(); - String value = DesignAttributeHandler.readAttribute("value", attr, - def.getValue(), String.class); - setValue(value); + if (attr.hasKey("value")) { + setValue(DesignAttributeHandler.readAttribute("value", attr, + String.class)); + } } /* diff --git a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java index b410fd0001..0a58fc549f 100644 --- a/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -99,16 +99,12 @@ public class DesignAttributeHandler implements Serializable { * the attribute map. If the attributes does not contain the * requested attribute, the value is retrieved from the * defaultInstance - * @param defaultInstance - * the default instance of the class for fetching the default - * values * @return true on success */ public static boolean readAttribute(Component component, String attribute, - Attributes attributes, Component defaultInstance) { + Attributes attributes) { String value = null; - if (component == null || attribute == null || attributes == null - || defaultInstance == null) { + if (component == null || attribute == null || attributes == null) { throw new IllegalArgumentException( "Parameters with null value not allowed"); } @@ -129,13 +125,10 @@ public class DesignAttributeHandler implements Serializable { setter.invoke(component, param); success = true; } else { - // otherwise find the getter for the attribute - Method getter = findGetterForAttribute(component.getClass(), - attribute); - // read the default value from defaults - Object defaultValue = getter.invoke(defaultInstance); - setter.invoke(component, defaultValue); - success = true; + getLogger().log( + Level.WARNING, + "Attribute value for " + attribute + + " is null, this should not happen"); } } catch (Exception e) { getLogger().log(Level.WARNING, @@ -256,13 +249,13 @@ public class DesignAttributeHandler implements Serializable { */ @SuppressWarnings("unchecked") public static T readAttribute(String attribute, Attributes attributes, - T defaultValue, Class outputType) { + Class outputType) { if (!isSupported(outputType)) { throw new IllegalArgumentException("output type: " + outputType.getName() + " not supported"); } if (!attributes.hasKey(attribute)) { - return defaultValue; + return null; } else { try { String value = attributes.get(attribute); diff --git a/server/tests/src/com/vaadin/tests/design/designroot/DesignRootTest.java b/server/tests/src/com/vaadin/tests/design/designroot/DesignRootTest.java index 0d390ba184..300d293e66 100644 --- a/server/tests/src/com/vaadin/tests/design/designroot/DesignRootTest.java +++ b/server/tests/src/com/vaadin/tests/design/designroot/DesignRootTest.java @@ -16,11 +16,8 @@ package com.vaadin.tests.design.designroot; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; -// This test will not pass until default instance creation is changed. Will leave it ignored for now. -@Ignore public class DesignRootTest { @Test public void designAnnotationWithoutFilename() { diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestReadDesign.java b/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestReadDesign.java index c57f18cbf9..db30f05c5f 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestReadDesign.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractsplitpanel/TestReadDesign.java @@ -60,21 +60,6 @@ public class TestReadDesign extends TestCase { assertEquals(Unit.PIXELS, sp.getMaxSplitPositionUnit()); assertEquals(true, sp.isLocked()); checkReversed(sp, true); - // check that the properties get the default values if the design - // does not have attributes corresponding to those properties - design = createDesign(true, true, true, true); - sp.readDesign(design, ctx); - HorizontalSplitPanel def = new HorizontalSplitPanel(); - assertEquals(def.getSplitPosition(), sp.getSplitPosition()); - assertEquals(def.getSplitPositionUnit(), sp.getSplitPositionUnit()); - assertEquals(def.getMinSplitPosition(), sp.getMinSplitPosition()); - assertEquals(def.getMinSplitPositionUnit(), - sp.getMinSplitPositionUnit()); - assertEquals(def.getMaxSplitPosition(), sp.getMaxSplitPosition()); - assertEquals(def.getMaxSplitPositionUnit(), - sp.getMaxSplitPositionUnit()); - assertEquals(def.isLocked(), sp.isLocked()); - checkReversed(sp, false); } public void testWithNoChildren() { -- cgit v1.2.3 From 353206974a542d0aadc278dea4adeff226fb3a9e Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 15 Dec 2014 23:02:22 +0200 Subject: Fix writing issues (#7749) * A root component with @DesignRoot must always use its superclass for default values. Otherwise the written design will be empty as the design is read in the constructor. * A component which is not the root must not write its component tree if the component tree is generated in the constructor. This is a simplification which should be good enough for most cases (can't add children in constructor and also using addComponent - in this case the component added with addComponent will not be written). * Test cases for nested templates Change-Id: I3a384d1d8654b9865a3a790ebeb055a300a62135 --- server/src/com/vaadin/ui/AbstractComponent.java | 3 +- server/src/com/vaadin/ui/AbstractField.java | 2 +- .../src/com/vaadin/ui/AbstractOrderedLayout.java | 8 +- server/src/com/vaadin/ui/AbstractSplitPanel.java | 4 +- server/src/com/vaadin/ui/AbstractTextField.java | 4 +- server/src/com/vaadin/ui/Button.java | 2 +- server/src/com/vaadin/ui/Panel.java | 2 +- server/src/com/vaadin/ui/PasswordField.java | 4 +- server/src/com/vaadin/ui/TabSheet.java | 2 +- server/src/com/vaadin/ui/TextField.java | 4 +- .../com/vaadin/ui/declarative/DesignContext.java | 63 ++++++++++++--- .../vaadin/tests/design/nested/MyChildDesign.java | 40 ++++++++++ .../nested/MyChildDesignCustomComponent.java | 25 ++++++ .../vaadin/tests/design/nested/MyDesignRoot.java | 38 +++++++++ .../tests/design/nested/MyExtendedChildDesign.java | 25 ++++++ .../design/nested/TestReadNestedTemplates.java | 62 +++++++++++++++ .../design/nested/TestWriteNestedTemplates.java | 89 ++++++++++++++++++++++ .../vaadin/tests/design/nested/mychilddesign.html | 12 +++ .../vaadin/tests/design/nested/mydesignroot.html | 10 +++ 19 files changed, 374 insertions(+), 25 deletions(-) create mode 100644 server/tests/src/com/vaadin/tests/design/nested/MyChildDesign.java create mode 100644 server/tests/src/com/vaadin/tests/design/nested/MyChildDesignCustomComponent.java create mode 100644 server/tests/src/com/vaadin/tests/design/nested/MyDesignRoot.java create mode 100644 server/tests/src/com/vaadin/tests/design/nested/MyExtendedChildDesign.java create mode 100644 server/tests/src/com/vaadin/tests/design/nested/TestReadNestedTemplates.java create mode 100644 server/tests/src/com/vaadin/tests/design/nested/TestWriteNestedTemplates.java create mode 100644 server/tests/src/com/vaadin/tests/design/nested/mychilddesign.html create mode 100644 server/tests/src/com/vaadin/tests/design/nested/mydesignroot.html (limited to 'server/src/com/vaadin/ui/PasswordField.java') diff --git a/server/src/com/vaadin/ui/AbstractComponent.java b/server/src/com/vaadin/ui/AbstractComponent.java index 0b9f8bd244..83833b75ce 100644 --- a/server/src/com/vaadin/ui/AbstractComponent.java +++ b/server/src/com/vaadin/ui/AbstractComponent.java @@ -1206,8 +1206,7 @@ public abstract class AbstractComponent extends AbstractClientConnector public void writeDesign(Element design, DesignContext designContext) { // clear element contents DesignAttributeHandler.clearElement(design); - AbstractComponent def = designContext.getDefaultInstance(this - .getClass()); + AbstractComponent def = designContext.getDefaultInstance(this); Attributes attr = design.attributes(); // handle default attributes for (String attribute : getDefaultAttributes()) { diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java index c3e2036936..e40bdbde68 100644 --- a/server/src/com/vaadin/ui/AbstractField.java +++ b/server/src/com/vaadin/ui/AbstractField.java @@ -1803,7 +1803,7 @@ public abstract class AbstractField extends AbstractComponent implements @Override public void writeDesign(Element design, DesignContext designContext) { super.writeDesign(design, designContext); - AbstractField def = designContext.getDefaultInstance(this.getClass()); + AbstractField def = designContext.getDefaultInstance(this); Attributes attr = design.attributes(); // handle readonly DesignAttributeHandler.writeAttribute("readonly", attr, diff --git a/server/src/com/vaadin/ui/AbstractOrderedLayout.java b/server/src/com/vaadin/ui/AbstractOrderedLayout.java index cd5609f091..b105d53fce 100644 --- a/server/src/com/vaadin/ui/AbstractOrderedLayout.java +++ b/server/src/com/vaadin/ui/AbstractOrderedLayout.java @@ -533,13 +533,17 @@ public abstract class AbstractOrderedLayout extends AbstractLayout implements // synchronize default attributes super.writeDesign(design, designContext); // handle margin - AbstractOrderedLayout def = designContext.getDefaultInstance(this - .getClass()); + AbstractOrderedLayout def = (AbstractOrderedLayout) designContext + .getDefaultInstance(this); if (getMargin().getBitMask() != def.getMargin().getBitMask()) { design.attr("margin", ""); } // handle children Element designElement = design; + if (!designContext.shouldWriteChildren(this, def)) { + return; + } + for (Component child : this) { Element childNode = designContext.createNode(child); designElement.appendChild(childNode); diff --git a/server/src/com/vaadin/ui/AbstractSplitPanel.java b/server/src/com/vaadin/ui/AbstractSplitPanel.java index 9c33665c1c..4c85311075 100644 --- a/server/src/com/vaadin/ui/AbstractSplitPanel.java +++ b/server/src/com/vaadin/ui/AbstractSplitPanel.java @@ -627,8 +627,8 @@ public abstract class AbstractSplitPanel extends AbstractComponentContainer { super.writeDesign(design, designContext); // handle custom attributes (write only if a value is not the // default value) - AbstractSplitPanel def = designContext.getDefaultInstance(this - .getClass()); + AbstractSplitPanel def = (AbstractSplitPanel) designContext + .getDefaultInstance(this); if (getSplitPosition() != def.getSplitPosition() || !def.getSplitPositionUnit().equals(getSplitPositionUnit())) { String splitPositionString = asString(getSplitPosition()) diff --git a/server/src/com/vaadin/ui/AbstractTextField.java b/server/src/com/vaadin/ui/AbstractTextField.java index 8bca74936a..adeb1cb69f 100644 --- a/server/src/com/vaadin/ui/AbstractTextField.java +++ b/server/src/com/vaadin/ui/AbstractTextField.java @@ -803,8 +803,8 @@ public abstract class AbstractTextField extends AbstractField implements @Override public void writeDesign(Element design, DesignContext designContext) { super.writeDesign(design, designContext); - AbstractTextField def = designContext.getDefaultInstance(this - .getClass()); + AbstractTextField def = (AbstractTextField) designContext + .getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("maxlength", attr, getMaxLength(), def.getMaxLength(), Integer.class); diff --git a/server/src/com/vaadin/ui/Button.java b/server/src/com/vaadin/ui/Button.java index 9b50b5eebc..7063568f26 100644 --- a/server/src/com/vaadin/ui/Button.java +++ b/server/src/com/vaadin/ui/Button.java @@ -727,7 +727,7 @@ public class Button extends AbstractComponent implements public void writeDesign(Element design, DesignContext designContext) { super.writeDesign(design, designContext); Attributes attr = design.attributes(); - Button def = designContext.getDefaultInstance(this.getClass()); + Button def = (Button) designContext.getDefaultInstance(this); String content = getCaption(); if (content != null) { design.html(content); diff --git a/server/src/com/vaadin/ui/Panel.java b/server/src/com/vaadin/ui/Panel.java index b0242cc32d..0c2a3f580b 100644 --- a/server/src/com/vaadin/ui/Panel.java +++ b/server/src/com/vaadin/ui/Panel.java @@ -365,7 +365,7 @@ public class Panel extends AbstractSingleComponentContainer implements public void writeDesign(Element design, DesignContext designContext) { super.writeDesign(design, designContext); // handle tabindex - Panel def = designContext.getDefaultInstance(this.getClass()); + Panel def = (Panel) designContext.getDefaultInstance(this); DesignAttributeHandler.writeAttribute("tabindex", design.attributes(), getTabIndex(), def.getTabIndex(), Integer.class); } diff --git a/server/src/com/vaadin/ui/PasswordField.java b/server/src/com/vaadin/ui/PasswordField.java index 974a1c2c10..1894804775 100644 --- a/server/src/com/vaadin/ui/PasswordField.java +++ b/server/src/com/vaadin/ui/PasswordField.java @@ -107,8 +107,8 @@ public class PasswordField extends AbstractTextField { @Override public void writeDesign(Element design, DesignContext designContext) { super.writeDesign(design, designContext); - AbstractTextField def = designContext.getDefaultInstance(this - .getClass()); + AbstractTextField def = (AbstractTextField) designContext + .getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("value", attr, getValue(), def.getValue(), String.class); diff --git a/server/src/com/vaadin/ui/TabSheet.java b/server/src/com/vaadin/ui/TabSheet.java index 330acacfd0..a264692acb 100644 --- a/server/src/com/vaadin/ui/TabSheet.java +++ b/server/src/com/vaadin/ui/TabSheet.java @@ -1623,7 +1623,7 @@ public class TabSheet extends AbstractComponentContainer implements Focusable, @Override public void writeDesign(Element design, DesignContext designContext) { super.writeDesign(design, designContext); - TabSheet def = designContext.getDefaultInstance(this.getClass()); + TabSheet def = (TabSheet) designContext.getDefaultInstance(this); Attributes attr = design.attributes(); // handle tab index DesignAttributeHandler.writeAttribute("tabindex", attr, getTabIndex(), diff --git a/server/src/com/vaadin/ui/TextField.java b/server/src/com/vaadin/ui/TextField.java index cd919ded9c..563810c76a 100644 --- a/server/src/com/vaadin/ui/TextField.java +++ b/server/src/com/vaadin/ui/TextField.java @@ -129,8 +129,8 @@ public class TextField extends AbstractTextField { @Override public void writeDesign(Element design, DesignContext designContext) { super.writeDesign(design, designContext); - AbstractTextField def = designContext.getDefaultInstance(this - .getClass()); + AbstractTextField def = (AbstractTextField) designContext + .getDefaultInstance(this); Attributes attr = design.attributes(); DesignAttributeHandler.writeAttribute("value", attr, getValue(), def.getValue(), String.class); diff --git a/server/src/com/vaadin/ui/declarative/DesignContext.java b/server/src/com/vaadin/ui/declarative/DesignContext.java index b196c84b02..214040b54d 100644 --- a/server/src/com/vaadin/ui/declarative/DesignContext.java +++ b/server/src/com/vaadin/ui/declarative/DesignContext.java @@ -28,7 +28,10 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; +import com.vaadin.annotations.DesignRoot; +import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Component; +import com.vaadin.ui.HasComponents; /** * This class contains contextual information that is collected when a component @@ -42,8 +45,8 @@ import com.vaadin.ui.Component; public class DesignContext implements Serializable { // cache for object instances - private static Map, Object> instanceCache = Collections - .synchronizedMap(new HashMap, Object>()); + private static Map, Component> instanceCache = Collections + .synchronizedMap(new HashMap, Component>()); // The root component of the component hierarchy private Component rootComponent = null; @@ -254,22 +257,38 @@ public class DesignContext implements Serializable { * Returns the default instance for the given class. The instance must not * be modified by the caller. * - * @param instanceClass + * @param abstractComponent * @return the default instance for the given class. The return value must * not be modified by the caller */ - public T getDefaultInstance(Class instanceClass) { - T instance = (T) instanceCache.get(instanceClass); + public T getDefaultInstance(AbstractComponent abstractComponent) { + // If the root is a @DesignRoot component, it can't use itself as a + // reference or the written design will be empty + + // If the root component in some other way initializes itself in the + // constructor + if (getRootComponent() == abstractComponent + && abstractComponent.getClass().isAnnotationPresent( + DesignRoot.class)) { + return (T) getDefaultInstance((Class) abstractComponent + .getClass().getSuperclass()); + } + return (T) getDefaultInstance(abstractComponent.getClass()); + } + + private Component getDefaultInstance( + Class componentClass) { + Component instance = instanceCache.get(componentClass); if (instance == null) { try { - instance = instanceClass.newInstance(); - instanceCache.put(instanceClass, instance); + instance = componentClass.newInstance(); + instanceCache.put(componentClass, instance); } catch (InstantiationException e) { throw new RuntimeException("Could not instantiate " - + instanceClass.getName()); + + componentClass.getName()); } catch (IllegalAccessException e) { throw new RuntimeException("Could not instantiate " - + instanceClass.getName()); + + componentClass.getName()); } } return instance; @@ -663,4 +682,30 @@ public class DesignContext implements Serializable { } } + /** + * Helper method for component write implementors to determine whether their + * children should be written out or not + * + * @param c + * The component being written + * @param defaultC + * The default instance for the component + * @return + */ + public boolean shouldWriteChildren(Component c, Component defaultC) { + if (c == getRootComponent()) { + // The root component should always write its children - otherwise + // the result is empty + return true; + } + + if (defaultC instanceof HasComponents + && ((HasComponents) defaultC).iterator().hasNext()) { + // Easy version which assumes that this is a custom component if the + // constructor adds children + return false; + } + + return true; + } } diff --git a/server/tests/src/com/vaadin/tests/design/nested/MyChildDesign.java b/server/tests/src/com/vaadin/tests/design/nested/MyChildDesign.java new file mode 100644 index 0000000000..e85c0aca5f --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/nested/MyChildDesign.java @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2014 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.design.nested; + +import org.junit.Ignore; + +import com.vaadin.annotations.DesignRoot; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.declarative.Design; + +/** + * Child design component + * + * @author Vaadin Ltd + */ +@Ignore +@DesignRoot("mychilddesign.html") +public class MyChildDesign extends HorizontalLayout { + public Label childLabel; + public MyChildDesignCustomComponent childCustomComponent; + + public MyChildDesign() { + Design.read(this); + childLabel.setDescription("added in constructor"); + } +} diff --git a/server/tests/src/com/vaadin/tests/design/nested/MyChildDesignCustomComponent.java b/server/tests/src/com/vaadin/tests/design/nested/MyChildDesignCustomComponent.java new file mode 100644 index 0000000000..94e34baea2 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/nested/MyChildDesignCustomComponent.java @@ -0,0 +1,25 @@ +/* + * Copyright 2000-2014 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.design.nested; + +import org.junit.Ignore; + +import com.vaadin.ui.Button; + +@Ignore +public class MyChildDesignCustomComponent extends Button { + +} diff --git a/server/tests/src/com/vaadin/tests/design/nested/MyDesignRoot.java b/server/tests/src/com/vaadin/tests/design/nested/MyDesignRoot.java new file mode 100644 index 0000000000..5727322ce3 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/nested/MyDesignRoot.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2014 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.design.nested; + +import org.junit.Ignore; + +import com.vaadin.annotations.DesignRoot; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.declarative.Design; + +/** + * Root design component + * + * @author Vaadin Ltd + */ +@Ignore +@DesignRoot("mydesignroot.html") +public class MyDesignRoot extends VerticalLayout { + // should be assigned automatically + public MyExtendedChildDesign childDesign; + + public MyDesignRoot() { + Design.read(this); + } +} diff --git a/server/tests/src/com/vaadin/tests/design/nested/MyExtendedChildDesign.java b/server/tests/src/com/vaadin/tests/design/nested/MyExtendedChildDesign.java new file mode 100644 index 0000000000..2012e4ec14 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/nested/MyExtendedChildDesign.java @@ -0,0 +1,25 @@ +/* + * Copyright 2000-2014 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.design.nested; + +import org.junit.Ignore; + +@Ignore +public class MyExtendedChildDesign extends MyChildDesign { + public MyExtendedChildDesign() { + super(); + } +} diff --git a/server/tests/src/com/vaadin/tests/design/nested/TestReadNestedTemplates.java b/server/tests/src/com/vaadin/tests/design/nested/TestReadNestedTemplates.java new file mode 100644 index 0000000000..c1483adc8a --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/nested/TestReadNestedTemplates.java @@ -0,0 +1,62 @@ +/* + * Copyright 2000-2014 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.design.nested; + +import junit.framework.TestCase; + +/** + * Test case for reading nested templates + * + * @since + * @author Vaadin Ltd + */ +public class TestReadNestedTemplates extends TestCase { + + private MyDesignRoot root; + + @Override + protected void setUp() throws Exception { + super.setUp(); + root = new MyDesignRoot(); + } + + public void testChildren() { + assertEquals("The root layout must contain one child", 1, + root.getComponentCount()); + assertTrue(root.iterator().next() instanceof MyExtendedChildDesign); + } + + public void testGrandChildren() { + assertEquals("The root layout must have two grandchildren", 2, + root.childDesign.getComponentCount()); + } + + public void testRootComponentFields() { + assertNotNull("The child component must not be null", root.childDesign); + } + + public void testChildComponentFields() { + assertNotNull("Grandchildren must not be null", + root.childDesign.childLabel); + assertNotNull("Grandchildren must not be null", + root.childDesign.childCustomComponent); + assertEquals("child label caption must be read", "test content", + root.childDesign.childLabel.getValue()); + assertEquals("child custom component caption must be read", + "custom content", + root.childDesign.childCustomComponent.getCaption()); + } +} diff --git a/server/tests/src/com/vaadin/tests/design/nested/TestWriteNestedTemplates.java b/server/tests/src/com/vaadin/tests/design/nested/TestWriteNestedTemplates.java new file mode 100644 index 0000000000..7fe63baa73 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/nested/TestWriteNestedTemplates.java @@ -0,0 +1,89 @@ +/* + * Copyright 2000-2014 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.design.nested; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; + +import com.vaadin.ui.declarative.DesignContext; + +/** + * + * Test case for writing nested templates + * + * @author Vaadin Ltd + */ +public class TestWriteNestedTemplates extends TestCase { + + private MyDesignRoot root; + private Element design; + + @Override + protected void setUp() throws Exception { + super.setUp(); + root = new MyDesignRoot(); + design = createDesign(); + DesignContext designContext = new DesignContext(); + designContext.setRootComponent(root); + root.writeDesign(design, designContext); + } + + public void testChildRendered() { + assertEquals("Root layout must have one child", 1, design.children() + .size()); + assertEquals("com_vaadin_tests_design_nested-my-extended-child-design", + design.child(0).tagName()); + } + + public void testRootCaptionWritten() { + assertTrue("Root layout caption must be written", + design.hasAttr("caption")); + assertEquals("Root layout caption must be written", "root caption", + design.attr("caption")); + } + + public void testChildCaptionWritten() { + assertTrue("Child design caption must be written", design.child(0) + .hasAttr("caption")); + assertEquals("Child design caption must be written", "child caption", + design.child(0).attr("caption")); + } + + // The default caption is read from child template + public void testDefaultCaptionShouldNotBeWritten() { + design = createDesign(); + root.childDesign.setCaption("Default caption for child design"); + DesignContext designContext = new DesignContext(); + designContext.setRootComponent(root); + root.writeDesign(design, designContext); + assertFalse("Default caption must not be written", design.child(0) + .hasAttr("caption")); + } + + public void testChildDesignChildrenNotWrittenInRootTemplate() { + assertEquals( + "Children of the child template must not be written to root template", + 0, design.child(0).children().size()); + } + + private Element createDesign() { + return new Element(Tag.valueOf("v-vertical-layout"), "", + new Attributes()); + } +} diff --git a/server/tests/src/com/vaadin/tests/design/nested/mychilddesign.html b/server/tests/src/com/vaadin/tests/design/nested/mychilddesign.html new file mode 100644 index 0000000000..c636033d2d --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/nested/mychilddesign.html @@ -0,0 +1,12 @@ + + + + + + + + test content + + custom content + + \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/design/nested/mydesignroot.html b/server/tests/src/com/vaadin/tests/design/nested/mydesignroot.html new file mode 100644 index 0000000000..55ac27c194 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/nested/mydesignroot.html @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3