From 2789f701678106e9f5141b096706ace0d6880780 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Fri, 12 Dec 2014 11:01:55 +0200 Subject: Rename LayoutHandler to Design (#7749) Change-Id: I86fcf9062804c7eb71d460d2003a3f5eb8d775eb --- server/src/com/vaadin/ui/declarative/Design.java | 261 +++++++++++++++++++++ .../com/vaadin/ui/declarative/LayoutHandler.java | 261 --------------------- .../vaadin/tests/design/InvalidLayoutTemplate.java | 58 +++++ .../com/vaadin/tests/design/LayoutTemplate.java | 52 ++++ .../design/ParseAllSupportedComponentsTest.java | 46 ++++ .../com/vaadin/tests/design/ParseLayoutTest.java | 215 +++++++++++++++++ .../src/com/vaadin/tests/design/TestLocale.java | 150 ++++++++++++ .../com/vaadin/tests/design/all-components.html | 103 ++++++++ .../src/com/vaadin/tests/design/testFile.html | 19 ++ .../tests/layoutparser/InvalidLayoutTemplate.java | 58 ----- .../vaadin/tests/layoutparser/LayoutTemplate.java | 52 ---- .../ParseAllSupportedComponentsTest.java | 46 ---- .../vaadin/tests/layoutparser/ParseLayoutTest.java | 216 ----------------- .../com/vaadin/tests/layoutparser/TestLocale.java | 150 ------------ .../vaadin/tests/layoutparser/all-components.html | 103 -------- .../com/vaadin/tests/layoutparser/testFile.html | 19 -- 16 files changed, 904 insertions(+), 905 deletions(-) create mode 100644 server/src/com/vaadin/ui/declarative/Design.java delete mode 100644 server/src/com/vaadin/ui/declarative/LayoutHandler.java create mode 100644 server/tests/src/com/vaadin/tests/design/InvalidLayoutTemplate.java create mode 100644 server/tests/src/com/vaadin/tests/design/LayoutTemplate.java create mode 100644 server/tests/src/com/vaadin/tests/design/ParseAllSupportedComponentsTest.java create mode 100644 server/tests/src/com/vaadin/tests/design/ParseLayoutTest.java create mode 100644 server/tests/src/com/vaadin/tests/design/TestLocale.java create mode 100644 server/tests/src/com/vaadin/tests/design/all-components.html create mode 100644 server/tests/src/com/vaadin/tests/design/testFile.html delete mode 100644 server/tests/src/com/vaadin/tests/layoutparser/InvalidLayoutTemplate.java delete mode 100644 server/tests/src/com/vaadin/tests/layoutparser/LayoutTemplate.java delete mode 100644 server/tests/src/com/vaadin/tests/layoutparser/ParseAllSupportedComponentsTest.java delete mode 100644 server/tests/src/com/vaadin/tests/layoutparser/ParseLayoutTest.java delete mode 100644 server/tests/src/com/vaadin/tests/layoutparser/TestLocale.java delete mode 100644 server/tests/src/com/vaadin/tests/layoutparser/all-components.html delete mode 100644 server/tests/src/com/vaadin/tests/layoutparser/testFile.html diff --git a/server/src/com/vaadin/ui/declarative/Design.java b/server/src/com/vaadin/ui/declarative/Design.java new file mode 100644 index 0000000000..89e992181b --- /dev/null +++ b/server/src/com/vaadin/ui/declarative/Design.java @@ -0,0 +1,261 @@ +/* + * 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.ui.declarative; + +import java.beans.IntrospectionException; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; +import java.util.Collection; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.DocumentType; +import org.jsoup.nodes.Element; +import org.jsoup.nodes.Node; +import org.jsoup.parser.Parser; +import org.jsoup.select.Elements; + +import com.vaadin.ui.Component; +import com.vaadin.ui.declarative.DesignContext.ComponentCreatedEvent; +import com.vaadin.ui.declarative.DesignContext.ComponentCreationListener; + +/** + * Design is used for parsing a component hierarchy from an html file and, + * conversely, for generating an html tree representation corresponding to a + * given component hierarchy. For both parsing and tree generation the component + * hierarchy must contain a single root. + * + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class Design implements Serializable { + /** + * Constructs a component hierarchy from the design specified as an html + * document. The component hierarchy must contain exactly one top-level + * Component. The component should be located under , but also invalid + * html containing the hierarchy without , and tags is + * accepted. + * + * @param html + * the html document describing the component design + * @return the DesignContext created while traversing the tree. The + * top-level component of the created component hierarchy can be + * accessed using result.getRootComponent(), where result is the + * object returned by this method. + * @throws IOException + */ + public static DesignContext parse(InputStream html) { + Document doc; + try { + doc = Jsoup.parse(html, "UTF-8", "", Parser.htmlParser()); + } catch (IOException e) { + throw new DesignException("The html document cannot be parsed."); + } + return parse(doc, null); + } + + /** + * Constructs a component hierarchy from the design specified as an html + * document. The component hierarchy must contain exactly one top-level + * Component. The component should be located under , but also invalid + * html containing the hierarchy without , and tags is + * accepted. You can optionally pass instance for the root component with + * some uninitialized instance fields. The fields will be automatically + * populated when parsing the design based on the component ids, local ids, + * and captions of the components in the design. + * + * @param html + * the html document describing the component design + * @param rootInstance + * the root instance with fields to be mapped to components in + * the design + * @return the DesignContext created while traversing the tree. The + * top-level component of the created component hierarchy can be + * accessed using result.getRootComponent(), where result is the + * object returned by this method. + * @throws IOException + */ + public static DesignContext parse(InputStream html, Component rootInstance) { + Document doc; + try { + doc = Jsoup.parse(html, "UTF-8", "", Parser.htmlParser()); + } catch (IOException e) { + throw new DesignException("The html document cannot be parsed."); + } + return parse(doc, rootInstance); + } + + /** + * Constructs a component hierarchy from the design specified as an html + * document given as a string. The component hierarchy must contain exactly + * one top-level Component. The component should be located under , + * but also invalid html containing the hierarchy without , and + * tags is accepted. + * + * @param html + * the html document describing the component design + * @return the DesignContext created while traversing the tree. The + * top-level component of the created component hierarchy can be + * accessed using result.getRootComponent(), where result is the + * object returned by this method. + * @throws IOException + */ + public static DesignContext parse(String html) { + Document doc = Jsoup.parse(html); + return parse(doc, null); + } + + /** + * Constructs a component hierarchy from the design specified as an html + * document given as a string. The component hierarchy must contain exactly + * one top-level Component. The component should be located under , + * but also invalid html containing the hierarchy without , and + * tags is accepted. You can optionally pass instance for the root + * component with some uninitialized instance fields. The fields will be + * automatically populated when parsing the design based on the component + * ids, local ids, and captions of the components in the design. + * + * @param html + * the html document describing the component design + * @param rootInstance + * the root instance with fields to be mapped to components in + * the design + * @return the DesignContext created while traversing the tree. The + * top-level component of the created component hierarchy can be + * accessed using result.getRootComponent(), where result is the + * object returned by this method. + * @throws IOException + */ + public static DesignContext parse(String html, Component rootInstance) { + Document doc = Jsoup.parse(html); + return parse(doc, rootInstance); + } + + /** + * Constructs a component hierarchy from the design specified as an html + * tree. If componentRoot is not null, the component instances created + * during synchronizing the design are assigned to its member fields based + * on their id, localId, and caption + * + * @param doc + * the html tree + * @param componentRoot + * optional component root instance with some member fields. The + * type must match the type of the root element in the design. + * The member fields whose type is assignable from + * {@link Component} are set when parsing the component tree + * + */ + private static DesignContext parse(Document doc, Component componentRoot) { + DesignContext designContext = new DesignContext(doc); + designContext.getPrefixes(doc); + // No special handling for a document without a body element - should be + // taken care of by jsoup. + Element root = doc.body(); + Elements children = root.children(); + if (children.size() != 1) { + throw new DesignException( + "The first level of a component hierarchy should contain exactly one root component, but found " + + children.size()); + } + Element element = children.first(); + if (componentRoot != null) { + // user has specified root instance that may have member fields that + // should be bound + FieldBinder binder = null; + try { + binder = new FieldBinder(componentRoot); + } catch (IntrospectionException e) { + throw new DesignException( + "Could not bind fields of the root component", e); + } + final FieldBinder fBinder = binder; + // create listener for component creations that binds the created + // components to the componentRoot instance fields + ComponentCreationListener creationListener = new ComponentCreationListener() { + @Override + public void componentCreated(ComponentCreatedEvent event) { + fBinder.bindField(event.getComponent(), event.getLocalId()); + } + }; + designContext.addComponentCreationListener(creationListener); + // create subtree + designContext.synchronizeAndRegister(componentRoot, element); + // make sure that all the member fields are bound + Collection unboundFields = binder.getUnboundFields(); + if (!unboundFields.isEmpty()) { + throw new DesignException( + "Found unbound fields from component root " + + unboundFields); + } + // no need to listen anymore + designContext.removeComponentCreationListener(creationListener); + } else { + // createChild creates the entire component hierarchy + componentRoot = designContext.createChild(element); + } + designContext.setComponentRoot(componentRoot); + return designContext; + } + + /** + * Generates an html tree representation representing the component + * hierarchy having the given root. The hierarchy is stored under in + * the tree. The generated tree corresponds to a valid html document. + * + * + * @param root + * the root of the component hierarchy + * @return an html tree representation of the component hierarchy + */ + public static Document createHtml(DesignContext designContext) { + // Create the html tree skeleton. + Document doc = new Document(""); + DocumentType docType = new DocumentType("html", "", "", ""); + doc.appendChild(docType); + Element html = doc.createElement("html"); + doc.appendChild(html); + html.appendChild(doc.createElement("head")); + Element body = doc.createElement("body"); + html.appendChild(body); + designContext.storePrefixes(doc); + + // Append the design under in the html tree. createNode + // creates the entire component hierarchy rooted at the + // given root node. + Component root = designContext.getComponentRoot(); + Node rootNode = designContext.createNode(root); + body.appendChild(rootNode); + return doc; + } + + /** + * Generates an html file corresponding to the component hierarchy with the + * given root. + * + * @param writer + * @param root + * @throws IOException + */ + public static void createHtml(BufferedWriter writer, DesignContext ctx) + throws IOException { + String docAsString = createHtml(ctx).toString(); + writer.write(docAsString); + } +} diff --git a/server/src/com/vaadin/ui/declarative/LayoutHandler.java b/server/src/com/vaadin/ui/declarative/LayoutHandler.java deleted file mode 100644 index b3ef2f15c7..0000000000 --- a/server/src/com/vaadin/ui/declarative/LayoutHandler.java +++ /dev/null @@ -1,261 +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.ui.declarative; - -import java.beans.IntrospectionException; -import java.io.BufferedWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.Serializable; -import java.util.Collection; - -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.DocumentType; -import org.jsoup.nodes.Element; -import org.jsoup.nodes.Node; -import org.jsoup.parser.Parser; -import org.jsoup.select.Elements; - -import com.vaadin.ui.Component; -import com.vaadin.ui.declarative.DesignContext.ComponentCreatedEvent; -import com.vaadin.ui.declarative.DesignContext.ComponentCreationListener; - -/** - * LayoutHandler is used for parsing a component hierarchy from an html file - * and, conversely, for generating an html tree representation corresponding to - * a given component hierarchy. For both parsing and tree generation the - * component hierarchy must contain a single root. - * - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class LayoutHandler implements Serializable { - /** - * Constructs a component hierarchy from the design specified as an html - * document. The component hierarchy must contain exactly one top-level - * Component. The component should be located under , but also invalid - * html containing the hierarchy without , and tags is - * accepted. - * - * @param html - * the html document describing the component design - * @return the DesignContext created while traversing the tree. The - * top-level component of the created component hierarchy can be - * accessed using result.getRootComponent(), where result is the - * object returned by this method. - * @throws IOException - */ - public static DesignContext parse(InputStream html) { - Document doc; - try { - doc = Jsoup.parse(html, "UTF-8", "", Parser.htmlParser()); - } catch (IOException e) { - throw new DesignException("The html document cannot be parsed."); - } - return parse(doc, null); - } - - /** - * Constructs a component hierarchy from the design specified as an html - * document. The component hierarchy must contain exactly one top-level - * Component. The component should be located under , but also invalid - * html containing the hierarchy without , and tags is - * accepted. You can optionally pass instance for the root component with - * some uninitialized instance fields. The fields will be automatically - * populated when parsing the design based on the component ids, local ids, - * and captions of the components in the design. - * - * @param html - * the html document describing the component design - * @param rootInstance - * the root instance with fields to be mapped to components in - * the design - * @return the DesignContext created while traversing the tree. The - * top-level component of the created component hierarchy can be - * accessed using result.getRootComponent(), where result is the - * object returned by this method. - * @throws IOException - */ - public static DesignContext parse(InputStream html, Component rootInstance) { - Document doc; - try { - doc = Jsoup.parse(html, "UTF-8", "", Parser.htmlParser()); - } catch (IOException e) { - throw new DesignException("The html document cannot be parsed."); - } - return parse(doc, rootInstance); - } - - /** - * Constructs a component hierarchy from the design specified as an html - * document given as a string. The component hierarchy must contain exactly - * one top-level Component. The component should be located under , - * but also invalid html containing the hierarchy without , and - * tags is accepted. - * - * @param html - * the html document describing the component design - * @return the DesignContext created while traversing the tree. The - * top-level component of the created component hierarchy can be - * accessed using result.getRootComponent(), where result is the - * object returned by this method. - * @throws IOException - */ - public static DesignContext parse(String html) { - Document doc = Jsoup.parse(html); - return parse(doc, null); - } - - /** - * Constructs a component hierarchy from the design specified as an html - * document given as a string. The component hierarchy must contain exactly - * one top-level Component. The component should be located under , - * but also invalid html containing the hierarchy without , and - * tags is accepted. You can optionally pass instance for the root - * component with some uninitialized instance fields. The fields will be - * automatically populated when parsing the design based on the component - * ids, local ids, and captions of the components in the design. - * - * @param html - * the html document describing the component design - * @param rootInstance - * the root instance with fields to be mapped to components in - * the design - * @return the DesignContext created while traversing the tree. The - * top-level component of the created component hierarchy can be - * accessed using result.getRootComponent(), where result is the - * object returned by this method. - * @throws IOException - */ - public static DesignContext parse(String html, Component rootInstance) { - Document doc = Jsoup.parse(html); - return parse(doc, rootInstance); - } - - /** - * Constructs a component hierarchy from the design specified as an html - * tree. If componentRoot is not null, the component instances created - * during synchronizing the design are assigned to its member fields based - * on their id, localId, and caption - * - * @param doc - * the html tree - * @param componentRoot - * optional component root instance with some member fields. The - * type must match the type of the root element in the design. - * The member fields whose type is assignable from - * {@link Component} are set when parsing the component tree - * - */ - private static DesignContext parse(Document doc, Component componentRoot) { - DesignContext designContext = new DesignContext(doc); - designContext.getPrefixes(doc); - // No special handling for a document without a body element - should be - // taken care of by jsoup. - Element root = doc.body(); - Elements children = root.children(); - if (children.size() != 1) { - throw new DesignException( - "The first level of a component hierarchy should contain exactly one root component, but found " - + children.size()); - } - Element element = children.first(); - if (componentRoot != null) { - // user has specified root instance that may have member fields that - // should be bound - FieldBinder binder = null; - try { - binder = new FieldBinder(componentRoot); - } catch (IntrospectionException e) { - throw new DesignException( - "Could not bind fields of the root component", e); - } - final FieldBinder fBinder = binder; - // create listener for component creations that binds the created - // components to the componentRoot instance fields - ComponentCreationListener creationListener = new ComponentCreationListener() { - @Override - public void componentCreated(ComponentCreatedEvent event) { - fBinder.bindField(event.getComponent(), event.getLocalId()); - } - }; - designContext.addComponentCreationListener(creationListener); - // create subtree - designContext.synchronizeAndRegister(componentRoot, element); - // make sure that all the member fields are bound - Collection unboundFields = binder.getUnboundFields(); - if (!unboundFields.isEmpty()) { - throw new DesignException( - "Found unbound fields from component root " - + unboundFields); - } - // no need to listen anymore - designContext.removeComponentCreationListener(creationListener); - } else { - // createChild creates the entire component hierarchy - componentRoot = designContext.createChild(element); - } - designContext.setComponentRoot(componentRoot); - return designContext; - } - - /** - * Generates an html tree representation representing the component - * hierarchy having the given root. The hierarchy is stored under in - * the tree. The generated tree corresponds to a valid html document. - * - * - * @param root - * the root of the component hierarchy - * @return an html tree representation of the component hierarchy - */ - public static Document createHtml(DesignContext designContext) { - // Create the html tree skeleton. - Document doc = new Document(""); - DocumentType docType = new DocumentType("html", "", "", ""); - doc.appendChild(docType); - Element html = doc.createElement("html"); - doc.appendChild(html); - html.appendChild(doc.createElement("head")); - Element body = doc.createElement("body"); - html.appendChild(body); - designContext.storePrefixes(doc); - - // Append the design under in the html tree. createNode - // creates the entire component hierarchy rooted at the - // given root node. - Component root = designContext.getComponentRoot(); - Node rootNode = designContext.createNode(root); - body.appendChild(rootNode); - return doc; - } - - /** - * Generates an html file corresponding to the component hierarchy with the - * given root. - * - * @param writer - * @param root - * @throws IOException - */ - public static void createHtml(BufferedWriter writer, DesignContext ctx) - throws IOException { - String docAsString = createHtml(ctx).toString(); - writer.write(docAsString); - } -} diff --git a/server/tests/src/com/vaadin/tests/design/InvalidLayoutTemplate.java b/server/tests/src/com/vaadin/tests/design/InvalidLayoutTemplate.java new file mode 100644 index 0000000000..bdfa4b47cc --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/InvalidLayoutTemplate.java @@ -0,0 +1,58 @@ +/* + * 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; + +import org.junit.Ignore; + +import com.vaadin.ui.Button; +import com.vaadin.ui.NativeButton; +import com.vaadin.ui.TextField; +import com.vaadin.ui.VerticalLayout; + +/** + * + * @since + * @author Vaadin Ltd + */ +@Ignore +public class InvalidLayoutTemplate extends VerticalLayout { + private NativeButton firstButton; + private NativeButton secondButton; + private NativeButton yetanotherbutton; // generated based on caption + private Button clickme; // generated based on caption + private TextField shouldNotBeMapped; + + public NativeButton getFirstButton() { + return firstButton; + } + + public NativeButton getSecondButton() { + return secondButton; + } + + public NativeButton getYetanotherbutton() { + return yetanotherbutton; + } + + public Button getClickme() { + return clickme; + } + + public TextField getShouldNotBeMapped() { + return shouldNotBeMapped; + } + +} diff --git a/server/tests/src/com/vaadin/tests/design/LayoutTemplate.java b/server/tests/src/com/vaadin/tests/design/LayoutTemplate.java new file mode 100644 index 0000000000..95f30fb728 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/LayoutTemplate.java @@ -0,0 +1,52 @@ +/* + * 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; + +import org.junit.Ignore; + +import com.vaadin.ui.Button; +import com.vaadin.ui.NativeButton; +import com.vaadin.ui.VerticalLayout; + +/** + * Template to be populated in the tests + * + * @since + * @author Vaadin Ltd + */ +@Ignore +public class LayoutTemplate extends VerticalLayout { + private NativeButton firstButton; // assigned based on local id + private NativeButton secondButton; // assigned based on id + private NativeButton yetanotherbutton; // assigned based on caption + private Button clickme; // assigned based on caption + + public NativeButton getFirstButton() { + return firstButton; + } + + public NativeButton getSecondButton() { + return secondButton; + } + + public NativeButton getYetanotherbutton() { + return yetanotherbutton; + } + + public Button getClickme() { + return clickme; + } +} diff --git a/server/tests/src/com/vaadin/tests/design/ParseAllSupportedComponentsTest.java b/server/tests/src/com/vaadin/tests/design/ParseAllSupportedComponentsTest.java new file mode 100644 index 0000000000..ff13522b2f --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/ParseAllSupportedComponentsTest.java @@ -0,0 +1,46 @@ +/* + * 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; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +import junit.framework.TestCase; + +import com.vaadin.ui.declarative.Design; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Just top level test case that contains all synchronizable components + * + * @author Vaadin Ltd + */ +public class ParseAllSupportedComponentsTest extends TestCase { + + public void testParsing() { + try { + DesignContext ctx = Design + .parse(new FileInputStream( + "server/tests/src/com/vaadin/tests/design/all-components.html")); + assertNotNull("The returned design context can not be null", ctx); + assertNotNull("the component root can not be null", + ctx.getComponentRoot()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + fail("Template parsing threw exception"); + } + } +} diff --git a/server/tests/src/com/vaadin/tests/design/ParseLayoutTest.java b/server/tests/src/com/vaadin/tests/design/ParseLayoutTest.java new file mode 100644 index 0000000000..07a0b218f7 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/ParseLayoutTest.java @@ -0,0 +1,215 @@ +/* + * 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; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.nodes.Node; +import org.junit.Test; + +import com.vaadin.ui.Button; +import com.vaadin.ui.Component; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.NativeButton; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.TextField; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.declarative.Design; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.ui.declarative.DesignException; + +/** + * A test for checking that parsing a layout preserves the IDs and the mapping + * from prefixes to package names (for example ) + * + * @since + * @author Vaadin Ltd + */ +public class ParseLayoutTest extends TestCase { + // The context is used for accessing the created component hierarchy. + private DesignContext ctx; + + @Override + protected void setUp() throws Exception { + super.setUp(); + ctx = Design.parse(new FileInputStream( + "server/tests/src/com/vaadin/tests/design/testFile.html")); + } + + /* + * Checks the component hierarchy created by parsing a design. Also checks + * that components can be found by id and caption. + */ + @Test + public void testGettingByIDAndCaption() throws FileNotFoundException { + findElements(ctx); + checkHierarchy(ctx); + } + + /* + * Check that captions, ids and package mappings are preserved when an html + * tree is generated from a DesignContext containing the component root of + * the component hierarchy. Done by writing the design to a string and then + * reading it back, not using the original context information after reading + * the written design. The mapping from prefixes to package names is checked + * directly from the html tree. + */ + @Test + public void testThatSerializationPreservesProperties() throws IOException { + Document doc = Design.createHtml(ctx); + DesignContext newContext = Design.parse(doc.toString()); + // Check that the elements can still be found by id and caption + findElements(newContext); + checkHierarchy(newContext); + // Check the mapping from prefixes to package names using the html tree + String[] expectedPrefixes = { "my" }; + String[] expectedPackageNames = { "com.addon.mypackage" }; + int index = 0; + Element head = doc.head(); + for (Node child : head.childNodes()) { + if ("meta".equals(child.nodeName())) { + String name = child.attributes().get("name"); + if ("package-mapping".equals(name)) { + String content = child.attributes().get("content"); + String[] parts = content.split(":"); + assertEquals("Unexpected prefix.", expectedPrefixes[index], + parts[0]); + assertEquals("Unexpected package name.", + expectedPackageNames[index], parts[1]); + index++; + } + } + } + assertEquals("Unexpected number of prefix - package name pairs.", 1, + index); + } + + /* + * Check that the field binding works if root instance with member fields is + * passed to the LayoutHandler + * + * @throws IOException + */ + public void testFieldBinding() throws IOException { + LayoutTemplate template = new LayoutTemplate(); + InputStream htmlFile = new FileInputStream( + "server/tests/src/com/vaadin/tests/design/testFile.html"); + Design.parse(htmlFile, template); + assertNotNull(template.getFirstButton()); + assertNotNull(template.getSecondButton()); + assertNotNull(template.getYetanotherbutton()); + assertNotNull(template.getClickme()); + assertEquals("Native click me", template.getFirstButton().getCaption()); + } + + /* + * Check that the field binding fails if some of the fields in the root + * instance were not bound + * + * @throws IOException + */ + public void testUnboundFields() throws IOException { + InvalidLayoutTemplate template = new InvalidLayoutTemplate(); + InputStream htmlFile = new FileInputStream( + "server/tests/src/com/vaadin/tests/design/testFile.html"); + try { + Design.parse(htmlFile, template); + // we are expecting an exception + fail(); + } catch (DesignException e) { + // expected + } + } + + /* + * Checks that the correct components occur in the correct order in the + * component hierarchy rooted at context.getComponentRoot(). + */ + private void checkHierarchy(DesignContext context) { + Component root = context.getComponentRoot(); + VerticalLayout vlayout = (VerticalLayout) root; + int numComponents = vlayout.getComponentCount(); + assertEquals("Wrong number of child components", 3, numComponents); + + // Check the contents of the horizontal layout + HorizontalLayout hlayout = (HorizontalLayout) vlayout.getComponent(0); + int numHLComponents = hlayout.getComponentCount(); + assertEquals(5, numHLComponents); + Label label = (Label) hlayout.getComponent(0); + assertEquals("Wrong caption.", "FooBar", label.getCaption()); + NativeButton nb = (NativeButton) hlayout.getComponent(1); + assertEquals("Wrong caption.", "Native click me", nb.getCaption()); + nb = (NativeButton) hlayout.getComponent(2); + assertEquals("Wrong caption.", "Another button", nb.getCaption()); + nb = (NativeButton) hlayout.getComponent(3); + assertEquals("Wrong caption.", "Yet another button", nb.getCaption()); + Button b = (Button) hlayout.getComponent(4); + assertEquals("Wrong caption.", "Click me", b.getCaption()); + assertEquals("Wrong width.", 150f, b.getWidth()); + + // Check the remaining two components of the vertical layout + TextField tf = (TextField) vlayout.getComponent(1); + assertEquals("Wrong caption.", "Text input", tf.getCaption()); + TextArea ta = (TextArea) vlayout.getComponent(2); + assertEquals("Wrong caption.", "Text area", ta.getCaption()); + assertEquals("Wrong width.", 300f, ta.getWidth()); + assertEquals("Wrong height.", 200f, ta.getHeight()); + } + + /* + * Checks that the correct elements are found using a local id, a global id + * or a caption. + */ + private void findElements(DesignContext designContext) { + NativeButton firstButton = (NativeButton) designContext + .getComponentByLocalId("firstButton"); + NativeButton firstButton_2 = (NativeButton) designContext + .getComponentByCaption("Native click me"); + NativeButton secondButton = (NativeButton) designContext + .getComponentById("secondButton"); + NativeButton secondButton_2 = (NativeButton) designContext + .getComponentByLocalId("localID"); + NativeButton secondButton_3 = (NativeButton) designContext + .getComponentByCaption("Another button"); + NativeButton thirdButton = (NativeButton) designContext + .getComponentByCaption("Yet another button"); + // Check that the first button was found using both identifiers. + assertEquals("The found buttons should be identical but they are not.", + firstButton, firstButton_2); + assertTrue("The found button element is incorrect.", firstButton + .getCaption().equals("Native click me")); + // Check that the second button was found using all three identifiers. + assertEquals("The found buttons should be identical but they are not.", + secondButton, secondButton_2); + assertEquals("The found buttons should be identical but they are not.", + secondButton_2, secondButton_3); + assertTrue("The found button is incorrect.", secondButton.getCaption() + .equals("Another button")); + // Check that the third button was found by caption. + assertTrue("The found button is incorrect.", thirdButton.getCaption() + .equals("Yet another button")); + } + +} diff --git a/server/tests/src/com/vaadin/tests/design/TestLocale.java b/server/tests/src/com/vaadin/tests/design/TestLocale.java new file mode 100644 index 0000000000..8b46c9feff --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/TestLocale.java @@ -0,0 +1,150 @@ +/* + * 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; + +import java.util.Locale; + +import junit.framework.TestCase; + +import org.jsoup.nodes.Document; +import org.jsoup.nodes.DocumentType; +import org.jsoup.nodes.Element; + +import com.vaadin.ui.Button; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.ui.declarative.Design; + +/** + * Tests the handling of the locale property in parsing and html generation. + * + * @since + * @author Vaadin Ltd + */ +public class TestLocale extends TestCase { + DesignContext ctx; + + @Override + public void setUp() { + ctx = new DesignContext(); + } + + /* + * Checks that when the html corresponding to a component hierarchy is + * constructed, the result only contains locale attributes for a component + * if its locale differs from that of its parent. + */ + public void testHtmlGeneration() { + // create a component hierarchy + VerticalLayout vLayout = new VerticalLayout(); + vLayout.setLocale(Locale.US); + HorizontalLayout hLayout = new HorizontalLayout(); + hLayout.setLocale(Locale.ITALY); + vLayout.addComponent(hLayout); + Button b1 = new Button(); + b1.setLocale(Locale.ITALY); + Button b2 = new Button(); + b2.setLocale(Locale.US); + hLayout.addComponent(b1); + hLayout.addComponent(b2); + HorizontalLayout hlayout2 = new HorizontalLayout(); + hlayout2.setLocale(Locale.US); + vLayout.addComponent(hlayout2); + Label l = new Label(); + l.setLocale(Locale.US); + hlayout2.addComponent(l); + Label l2 = new Label(); + l2.setLocale(Locale.CANADA); + hlayout2.addComponent(l2); + ctx.setComponentRoot(vLayout); + // create the html tree corresponding to the component hierarchy + Document doc = Design.createHtml(ctx); + // check the created html + Element body = doc.body(); + Element evLayout = body.child(0); + assertEquals("Wrong locale information.", "en_US", + evLayout.attr("locale")); + Element ehLayout = evLayout.child(0); + assertEquals("Wrong locale information.", "it_IT", + ehLayout.attr("locale")); + Element eb1 = ehLayout.child(0); + assertTrue( + "The element should not have a locale specification, found locale " + + eb1.attr("locale"), "".equals(eb1.attr("locale"))); + Element eb2 = ehLayout.child(1); + assertEquals("Wrong locale information.", "en_US", eb2.attr("locale")); + Element ehLayout2 = evLayout.child(1); + assertTrue( + "The element should not have a locale specification, found locale " + + ehLayout2.attr("locale"), + "".equals(ehLayout2.attr("locale"))); + Element el1 = ehLayout2.child(0); + assertTrue( + "The element should not have a locale specification, found locale " + + el1.attr("locale"), "".equals(el1.attr("locale"))); + Element el2 = ehLayout2.child(1); + assertEquals("Wrong locale information.", "en_CA", el2.attr("locale")); + } + + /* + * Checks that the locale of a component is set when the html element + * corresponding to the component specifies a locale. + */ + public void testParsing() { + // create an html document + Document doc = new Document(""); + DocumentType docType = new DocumentType("html", "", "", ""); + doc.appendChild(docType); + Element html = doc.createElement("html"); + doc.appendChild(html); + html.appendChild(doc.createElement("head")); + Element body = doc.createElement("body"); + html.appendChild(body); + Element evLayout = doc.createElement("v-vertical-layout"); + evLayout.attr("locale", "en_US"); + body.appendChild(evLayout); + Element ehLayout = doc.createElement("v-horizontal-layout"); + evLayout.appendChild(ehLayout); + Element eb1 = doc.createElement("v-button"); + eb1.attr("locale", "en_US"); + ehLayout.appendChild(eb1); + Element eb2 = doc.createElement("v-button"); + eb2.attr("locale", "en_GB"); + ehLayout.appendChild(eb2); + Element eb3 = doc.createElement("v-button"); + ehLayout.appendChild(eb3); + + // parse the created document and check the constructed component + // hierarchy + VerticalLayout vLayout = (VerticalLayout) Design.parse( + doc.toString()).getComponentRoot(); + assertEquals("Wrong locale.", new Locale("en", "US"), + vLayout.getLocale()); + HorizontalLayout hLayout = (HorizontalLayout) vLayout.getComponent(0); + assertEquals("The element should have the same locale as its parent.", + vLayout.getLocale(), hLayout.getLocale()); + Button b1 = (Button) hLayout.getComponent(0); + assertEquals("Wrong locale.", new Locale("en", "US"), b1.getLocale()); + Button b2 = (Button) hLayout.getComponent(1); + assertEquals("Wrong locale.", new Locale("en", "GB"), b2.getLocale()); + Button b3 = (Button) hLayout.getComponent(2); + assertEquals( + "The component should have the same locale as its parent.", + hLayout.getLocale(), b3.getLocale()); + } +} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/design/all-components.html b/server/tests/src/com/vaadin/tests/design/all-components.html new file mode 100644 index 0000000000..71aeeeaa41 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/all-components.html @@ -0,0 +1,103 @@ + + + + + + + + + + + + + OK + Cancel + + + + + OK + + + + + + OK + + + + + + OK + + + + + + OK + + + + + + + + + + + + + + + + test value + + OK + + OK + + + + + + + + + + In disabled tab - can’t be shown by default + + + + + + + + + + + + + + + In disabled tab - can’t be shown by default + + + + + + + + + First slot + + + Second slot + + + First slot + Second slot + + + + + diff --git a/server/tests/src/com/vaadin/tests/design/testFile.html b/server/tests/src/com/vaadin/tests/design/testFile.html new file mode 100644 index 0000000000..79ae1e9eaf --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/testFile.html @@ -0,0 +1,19 @@ + + + + + + + + + + Native click me + Another button + Yet another button + Click me + + + + + + \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/layoutparser/InvalidLayoutTemplate.java b/server/tests/src/com/vaadin/tests/layoutparser/InvalidLayoutTemplate.java deleted file mode 100644 index a595bac4fc..0000000000 --- a/server/tests/src/com/vaadin/tests/layoutparser/InvalidLayoutTemplate.java +++ /dev/null @@ -1,58 +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.layoutparser; - -import org.junit.Ignore; - -import com.vaadin.ui.Button; -import com.vaadin.ui.NativeButton; -import com.vaadin.ui.TextField; -import com.vaadin.ui.VerticalLayout; - -/** - * - * @since - * @author Vaadin Ltd - */ -@Ignore -public class InvalidLayoutTemplate extends VerticalLayout { - private NativeButton firstButton; - private NativeButton secondButton; - private NativeButton yetanotherbutton; // generated based on caption - private Button clickme; // generated based on caption - private TextField shouldNotBeMapped; - - public NativeButton getFirstButton() { - return firstButton; - } - - public NativeButton getSecondButton() { - return secondButton; - } - - public NativeButton getYetanotherbutton() { - return yetanotherbutton; - } - - public Button getClickme() { - return clickme; - } - - public TextField getShouldNotBeMapped() { - return shouldNotBeMapped; - } - -} diff --git a/server/tests/src/com/vaadin/tests/layoutparser/LayoutTemplate.java b/server/tests/src/com/vaadin/tests/layoutparser/LayoutTemplate.java deleted file mode 100644 index 533379e298..0000000000 --- a/server/tests/src/com/vaadin/tests/layoutparser/LayoutTemplate.java +++ /dev/null @@ -1,52 +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.layoutparser; - -import org.junit.Ignore; - -import com.vaadin.ui.Button; -import com.vaadin.ui.NativeButton; -import com.vaadin.ui.VerticalLayout; - -/** - * Template to be populated in the tests - * - * @since - * @author Vaadin Ltd - */ -@Ignore -public class LayoutTemplate extends VerticalLayout { - private NativeButton firstButton; // assigned based on local id - private NativeButton secondButton; // assigned based on id - private NativeButton yetanotherbutton; // assigned based on caption - private Button clickme; // assigned based on caption - - public NativeButton getFirstButton() { - return firstButton; - } - - public NativeButton getSecondButton() { - return secondButton; - } - - public NativeButton getYetanotherbutton() { - return yetanotherbutton; - } - - public Button getClickme() { - return clickme; - } -} diff --git a/server/tests/src/com/vaadin/tests/layoutparser/ParseAllSupportedComponentsTest.java b/server/tests/src/com/vaadin/tests/layoutparser/ParseAllSupportedComponentsTest.java deleted file mode 100644 index c3d1b36320..0000000000 --- a/server/tests/src/com/vaadin/tests/layoutparser/ParseAllSupportedComponentsTest.java +++ /dev/null @@ -1,46 +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.layoutparser; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; - -import junit.framework.TestCase; - -import com.vaadin.ui.declarative.DesignContext; -import com.vaadin.ui.declarative.LayoutHandler; - -/** - * Just top level test case that contains all synchronizable components - * - * @author Vaadin Ltd - */ -public class ParseAllSupportedComponentsTest extends TestCase { - - public void testParsing() { - try { - DesignContext ctx = LayoutHandler - .parse(new FileInputStream( - "server/tests/src/com/vaadin/tests/layoutparser/all-components.html")); - assertNotNull("The returned design context can not be null", ctx); - assertNotNull("the component root can not be null", - ctx.getComponentRoot()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - fail("Template parsing threw exception"); - } - } -} diff --git a/server/tests/src/com/vaadin/tests/layoutparser/ParseLayoutTest.java b/server/tests/src/com/vaadin/tests/layoutparser/ParseLayoutTest.java deleted file mode 100644 index 0c7ac50601..0000000000 --- a/server/tests/src/com/vaadin/tests/layoutparser/ParseLayoutTest.java +++ /dev/null @@ -1,216 +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.layoutparser; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.nodes.Node; -import org.junit.Test; - -import com.vaadin.ui.Button; -import com.vaadin.ui.Component; -import com.vaadin.ui.HorizontalLayout; -import com.vaadin.ui.Label; -import com.vaadin.ui.NativeButton; -import com.vaadin.ui.TextArea; -import com.vaadin.ui.TextField; -import com.vaadin.ui.VerticalLayout; -import com.vaadin.ui.declarative.DesignContext; -import com.vaadin.ui.declarative.DesignException; -import com.vaadin.ui.declarative.LayoutHandler; - -/** - * A test for checking that parsing a layout preserves the IDs and the mapping - * from prefixes to package names (for example ) - * - * @since - * @author Vaadin Ltd - */ -public class ParseLayoutTest extends TestCase { - // The context is used for accessing the created component hierarchy. - private DesignContext ctx; - - @Override - protected void setUp() throws Exception { - super.setUp(); - ctx = LayoutHandler - .parse(new FileInputStream( - "server/tests/src/com/vaadin/tests/layoutparser/testFile.html")); - } - - /* - * Checks the component hierarchy created by parsing a design. Also checks - * that components can be found by id and caption. - */ - @Test - public void testGettingByIDAndCaption() throws FileNotFoundException { - findElements(ctx); - checkHierarchy(ctx); - } - - /* - * Check that captions, ids and package mappings are preserved when an html - * tree is generated from a DesignContext containing the component root of - * the component hierarchy. Done by writing the design to a string and then - * reading it back, not using the original context information after reading - * the written design. The mapping from prefixes to package names is checked - * directly from the html tree. - */ - @Test - public void testThatSerializationPreservesProperties() throws IOException { - Document doc = LayoutHandler.createHtml(ctx); - DesignContext newContext = LayoutHandler.parse(doc.toString()); - // Check that the elements can still be found by id and caption - findElements(newContext); - checkHierarchy(newContext); - // Check the mapping from prefixes to package names using the html tree - String[] expectedPrefixes = { "my" }; - String[] expectedPackageNames = { "com.addon.mypackage" }; - int index = 0; - Element head = doc.head(); - for (Node child : head.childNodes()) { - if ("meta".equals(child.nodeName())) { - String name = child.attributes().get("name"); - if ("package-mapping".equals(name)) { - String content = child.attributes().get("content"); - String[] parts = content.split(":"); - assertEquals("Unexpected prefix.", expectedPrefixes[index], - parts[0]); - assertEquals("Unexpected package name.", - expectedPackageNames[index], parts[1]); - index++; - } - } - } - assertEquals("Unexpected number of prefix - package name pairs.", 1, - index); - } - - /* - * Check that the field binding works if root instance with member fields is - * passed to the LayoutHandler - * - * @throws IOException - */ - public void testFieldBinding() throws IOException { - LayoutTemplate template = new LayoutTemplate(); - InputStream htmlFile = new FileInputStream( - "server/tests/src/com/vaadin/tests/layoutparser/testFile.html"); - LayoutHandler.parse(htmlFile, template); - assertNotNull(template.getFirstButton()); - assertNotNull(template.getSecondButton()); - assertNotNull(template.getYetanotherbutton()); - assertNotNull(template.getClickme()); - assertEquals("Native click me", template.getFirstButton().getCaption()); - } - - /* - * Check that the field binding fails if some of the fields in the root - * instance were not bound - * - * @throws IOException - */ - public void testUnboundFields() throws IOException { - InvalidLayoutTemplate template = new InvalidLayoutTemplate(); - InputStream htmlFile = new FileInputStream( - "server/tests/src/com/vaadin/tests/layoutparser/testFile.html"); - try { - LayoutHandler.parse(htmlFile, template); - // we are expecting an exception - fail(); - } catch (DesignException e) { - // expected - } - } - - /* - * Checks that the correct components occur in the correct order in the - * component hierarchy rooted at context.getComponentRoot(). - */ - private void checkHierarchy(DesignContext context) { - Component root = context.getComponentRoot(); - VerticalLayout vlayout = (VerticalLayout) root; - int numComponents = vlayout.getComponentCount(); - assertEquals("Wrong number of child components", 3, numComponents); - - // Check the contents of the horizontal layout - HorizontalLayout hlayout = (HorizontalLayout) vlayout.getComponent(0); - int numHLComponents = hlayout.getComponentCount(); - assertEquals(5, numHLComponents); - Label label = (Label) hlayout.getComponent(0); - assertEquals("Wrong caption.", "FooBar", label.getCaption()); - NativeButton nb = (NativeButton) hlayout.getComponent(1); - assertEquals("Wrong caption.", "Native click me", nb.getCaption()); - nb = (NativeButton) hlayout.getComponent(2); - assertEquals("Wrong caption.", "Another button", nb.getCaption()); - nb = (NativeButton) hlayout.getComponent(3); - assertEquals("Wrong caption.", "Yet another button", nb.getCaption()); - Button b = (Button) hlayout.getComponent(4); - assertEquals("Wrong caption.", "Click me", b.getCaption()); - assertEquals("Wrong width.", 150f, b.getWidth()); - - // Check the remaining two components of the vertical layout - TextField tf = (TextField) vlayout.getComponent(1); - assertEquals("Wrong caption.", "Text input", tf.getCaption()); - TextArea ta = (TextArea) vlayout.getComponent(2); - assertEquals("Wrong caption.", "Text area", ta.getCaption()); - assertEquals("Wrong width.", 300f, ta.getWidth()); - assertEquals("Wrong height.", 200f, ta.getHeight()); - } - - /* - * Checks that the correct elements are found using a local id, a global id - * or a caption. - */ - private void findElements(DesignContext designContext) { - NativeButton firstButton = (NativeButton) designContext - .getComponentByLocalId("firstButton"); - NativeButton firstButton_2 = (NativeButton) designContext - .getComponentByCaption("Native click me"); - NativeButton secondButton = (NativeButton) designContext - .getComponentById("secondButton"); - NativeButton secondButton_2 = (NativeButton) designContext - .getComponentByLocalId("localID"); - NativeButton secondButton_3 = (NativeButton) designContext - .getComponentByCaption("Another button"); - NativeButton thirdButton = (NativeButton) designContext - .getComponentByCaption("Yet another button"); - // Check that the first button was found using both identifiers. - assertEquals("The found buttons should be identical but they are not.", - firstButton, firstButton_2); - assertTrue("The found button element is incorrect.", firstButton - .getCaption().equals("Native click me")); - // Check that the second button was found using all three identifiers. - assertEquals("The found buttons should be identical but they are not.", - secondButton, secondButton_2); - assertEquals("The found buttons should be identical but they are not.", - secondButton_2, secondButton_3); - assertTrue("The found button is incorrect.", secondButton.getCaption() - .equals("Another button")); - // Check that the third button was found by caption. - assertTrue("The found button is incorrect.", thirdButton.getCaption() - .equals("Yet another button")); - } - -} diff --git a/server/tests/src/com/vaadin/tests/layoutparser/TestLocale.java b/server/tests/src/com/vaadin/tests/layoutparser/TestLocale.java deleted file mode 100644 index eea533243c..0000000000 --- a/server/tests/src/com/vaadin/tests/layoutparser/TestLocale.java +++ /dev/null @@ -1,150 +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.layoutparser; - -import java.util.Locale; - -import junit.framework.TestCase; - -import org.jsoup.nodes.Document; -import org.jsoup.nodes.DocumentType; -import org.jsoup.nodes.Element; - -import com.vaadin.ui.Button; -import com.vaadin.ui.HorizontalLayout; -import com.vaadin.ui.Label; -import com.vaadin.ui.VerticalLayout; -import com.vaadin.ui.declarative.DesignContext; -import com.vaadin.ui.declarative.LayoutHandler; - -/** - * Tests the handling of the locale property in parsing and html generation. - * - * @since - * @author Vaadin Ltd - */ -public class TestLocale extends TestCase { - DesignContext ctx; - - @Override - public void setUp() { - ctx = new DesignContext(); - } - - /* - * Checks that when the html corresponding to a component hierarchy is - * constructed, the result only contains locale attributes for a component - * if its locale differs from that of its parent. - */ - public void testHtmlGeneration() { - // create a component hierarchy - VerticalLayout vLayout = new VerticalLayout(); - vLayout.setLocale(Locale.US); - HorizontalLayout hLayout = new HorizontalLayout(); - hLayout.setLocale(Locale.ITALY); - vLayout.addComponent(hLayout); - Button b1 = new Button(); - b1.setLocale(Locale.ITALY); - Button b2 = new Button(); - b2.setLocale(Locale.US); - hLayout.addComponent(b1); - hLayout.addComponent(b2); - HorizontalLayout hlayout2 = new HorizontalLayout(); - hlayout2.setLocale(Locale.US); - vLayout.addComponent(hlayout2); - Label l = new Label(); - l.setLocale(Locale.US); - hlayout2.addComponent(l); - Label l2 = new Label(); - l2.setLocale(Locale.CANADA); - hlayout2.addComponent(l2); - ctx.setComponentRoot(vLayout); - // create the html tree corresponding to the component hierarchy - Document doc = LayoutHandler.createHtml(ctx); - // check the created html - Element body = doc.body(); - Element evLayout = body.child(0); - assertEquals("Wrong locale information.", "en_US", - evLayout.attr("locale")); - Element ehLayout = evLayout.child(0); - assertEquals("Wrong locale information.", "it_IT", - ehLayout.attr("locale")); - Element eb1 = ehLayout.child(0); - assertTrue( - "The element should not have a locale specification, found locale " - + eb1.attr("locale"), "".equals(eb1.attr("locale"))); - Element eb2 = ehLayout.child(1); - assertEquals("Wrong locale information.", "en_US", eb2.attr("locale")); - Element ehLayout2 = evLayout.child(1); - assertTrue( - "The element should not have a locale specification, found locale " - + ehLayout2.attr("locale"), - "".equals(ehLayout2.attr("locale"))); - Element el1 = ehLayout2.child(0); - assertTrue( - "The element should not have a locale specification, found locale " - + el1.attr("locale"), "".equals(el1.attr("locale"))); - Element el2 = ehLayout2.child(1); - assertEquals("Wrong locale information.", "en_CA", el2.attr("locale")); - } - - /* - * Checks that the locale of a component is set when the html element - * corresponding to the component specifies a locale. - */ - public void testParsing() { - // create an html document - Document doc = new Document(""); - DocumentType docType = new DocumentType("html", "", "", ""); - doc.appendChild(docType); - Element html = doc.createElement("html"); - doc.appendChild(html); - html.appendChild(doc.createElement("head")); - Element body = doc.createElement("body"); - html.appendChild(body); - Element evLayout = doc.createElement("v-vertical-layout"); - evLayout.attr("locale", "en_US"); - body.appendChild(evLayout); - Element ehLayout = doc.createElement("v-horizontal-layout"); - evLayout.appendChild(ehLayout); - Element eb1 = doc.createElement("v-button"); - eb1.attr("locale", "en_US"); - ehLayout.appendChild(eb1); - Element eb2 = doc.createElement("v-button"); - eb2.attr("locale", "en_GB"); - ehLayout.appendChild(eb2); - Element eb3 = doc.createElement("v-button"); - ehLayout.appendChild(eb3); - - // parse the created document and check the constructed component - // hierarchy - VerticalLayout vLayout = (VerticalLayout) LayoutHandler.parse( - doc.toString()).getComponentRoot(); - assertEquals("Wrong locale.", new Locale("en", "US"), - vLayout.getLocale()); - HorizontalLayout hLayout = (HorizontalLayout) vLayout.getComponent(0); - assertEquals("The element should have the same locale as its parent.", - vLayout.getLocale(), hLayout.getLocale()); - Button b1 = (Button) hLayout.getComponent(0); - assertEquals("Wrong locale.", new Locale("en", "US"), b1.getLocale()); - Button b2 = (Button) hLayout.getComponent(1); - assertEquals("Wrong locale.", new Locale("en", "GB"), b2.getLocale()); - Button b3 = (Button) hLayout.getComponent(2); - assertEquals( - "The component should have the same locale as its parent.", - hLayout.getLocale(), b3.getLocale()); - } -} \ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/layoutparser/all-components.html b/server/tests/src/com/vaadin/tests/layoutparser/all-components.html deleted file mode 100644 index 71aeeeaa41..0000000000 --- a/server/tests/src/com/vaadin/tests/layoutparser/all-components.html +++ /dev/null @@ -1,103 +0,0 @@ - - - - - - - - - - - - - OK - Cancel - - - - - OK - - - - - - OK - - - - - - OK - - - - - - OK - - - - - - - - - - - - - - - - test value - - OK - - OK - - - - - - - - - - In disabled tab - can’t be shown by default - - - - - - - - - - - - - - - In disabled tab - can’t be shown by default - - - - - - - - - First slot - - - Second slot - - - First slot - Second slot - - - - - diff --git a/server/tests/src/com/vaadin/tests/layoutparser/testFile.html b/server/tests/src/com/vaadin/tests/layoutparser/testFile.html deleted file mode 100644 index 79ae1e9eaf..0000000000 --- a/server/tests/src/com/vaadin/tests/layoutparser/testFile.html +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - Native click me - Another button - Yet another button - Click me - - - - - - \ No newline at end of file -- cgit v1.2.3