diff options
author | Artur Signell <artur@vaadin.com> | 2016-08-18 22:56:01 +0300 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2016-08-20 00:22:07 +0300 |
commit | cfe379885275aae74b88c22ec4dd1bd3a8a094bd (patch) | |
tree | cb406af26735c0a4f100a4c1deda07032f633bf5 /compatibility-server | |
parent | f23880749d9c2dca3ef4fd31b21fcc94b5220b42 (diff) | |
download | vaadin-framework-cfe379885275aae74b88c22ec4dd1bd3a8a094bd.tar.gz vaadin-framework-cfe379885275aae74b88c22ec4dd1bd3a8a094bd.zip |
Move TextArea to compatibility package
Change-Id: I16b6566340e3ce32a4f94b7554e2f6f583e20486
Diffstat (limited to 'compatibility-server')
5 files changed, 410 insertions, 0 deletions
diff --git a/compatibility-server/src/main/java/com/vaadin/ui/TextArea.java b/compatibility-server/src/main/java/com/vaadin/ui/TextArea.java new file mode 100644 index 0000000000..056667a696 --- /dev/null +++ b/compatibility-server/src/main/java/com/vaadin/ui/TextArea.java @@ -0,0 +1,171 @@ +/* + * Copyright 2000-2016 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; + +import org.jsoup.nodes.Element; + +import com.vaadin.data.Property; +import com.vaadin.shared.ui.textarea.TextAreaState; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.ui.declarative.DesignFormatter; +import com.vaadin.v7.ui.LegacyAbstractTextField; + +/** + * A text field that supports multi line editing. + */ +public class TextArea extends LegacyAbstractTextField { + + /** + * Constructs an empty TextArea. + */ + public TextArea() { + setValue(""); + } + + /** + * Constructs an empty TextArea with given caption. + * + * @param caption + * the caption for the field. + */ + public TextArea(String caption) { + this(); + setCaption(caption); + } + + /** + * Constructs a TextArea with given property data source. + * + * @param dataSource + * the data source for the field + */ + public TextArea(Property dataSource) { + this(); + setPropertyDataSource(dataSource); + } + + /** + * Constructs a TextArea with given caption and property data source. + * + * @param caption + * the caption for the field + * @param dataSource + * the data source for the field + */ + public TextArea(String caption, Property dataSource) { + this(dataSource); + setCaption(caption); + } + + /** + * Constructs a TextArea with given caption and value. + * + * @param caption + * the caption for the field + * @param value + * the value for the field + */ + public TextArea(String caption, String value) { + this(caption); + setValue(value); + + } + + @Override + protected TextAreaState getState() { + return (TextAreaState) super.getState(); + } + + @Override + protected TextAreaState getState(boolean markAsDirty) { + return (TextAreaState) super.getState(markAsDirty); + } + + /** + * Sets the number of rows in the text area. + * + * @param rows + * the number of rows for this text area. + */ + public void setRows(int rows) { + if (rows < 0) { + rows = 0; + } + getState().rows = rows; + } + + /** + * Gets the number of rows in the text area. + * + * @return number of explicitly set rows. + */ + public int getRows() { + return getState(false).rows; + } + + /** + * Sets the text area's word-wrap mode on or off. + * + * @param wordwrap + * the boolean value specifying if the text area should be in + * word-wrap mode. + */ + public void setWordwrap(boolean wordwrap) { + getState().wordwrap = wordwrap; + } + + /** + * Tests if the text area is in word-wrap mode. + * + * @return <code>true</code> if the component is in word-wrap mode, + * <code>false</code> if not. + */ + public boolean isWordwrap() { + return getState(false).wordwrap; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.AbstractField#readDesign(org.jsoup.nodes.Element , + * com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); + setValue(DesignFormatter.decodeFromTextNode(design.html()), false, + true); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.ui.AbstractTextField#writeDesign(org.jsoup.nodes.Element + * , com.vaadin.ui.declarative.DesignContext) + */ + @Override + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); + design.html(DesignFormatter.encodeForTextNode(getValue())); + } + + @Override + public void clear() { + setValue(""); + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/design/WriteLegacyDesignTest.java b/compatibility-server/src/test/java/com/vaadin/tests/design/WriteLegacyDesignTest.java new file mode 100644 index 0000000000..eb3841b094 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/design/WriteLegacyDesignTest.java @@ -0,0 +1,99 @@ +/* + * Copyright 2000-2016 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 static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Properties; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.nodes.Node; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.Constants; +import com.vaadin.server.DefaultDeploymentConfiguration; +import com.vaadin.server.DeploymentConfiguration; +import com.vaadin.server.VaadinService; +import com.vaadin.server.VaadinServletService; +import com.vaadin.ui.declarative.Design; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.util.CurrentInstance; + +/** + * Parse and write a legacy design (using the "v-" prefix). + */ +public class WriteLegacyDesignTest { + + // The context is used for accessing the created component hierarchy. + private DesignContext ctx; + + @Before + public void setUp() throws Exception { + Properties properties = new Properties(); + properties.put(Constants.SERVLET_PARAMETER_LEGACY_DESIGN_PREFIX, + "true"); + final DeploymentConfiguration configuration = new DefaultDeploymentConfiguration( + WriteLegacyDesignTest.class, properties); + + VaadinService service = new VaadinServletService(null, configuration); + + CurrentInstance.set(VaadinService.class, service); + + ctx = Design.read( + getClass().getResourceAsStream("testFile-legacy.html"), null); + } + + @After + public void tearDown() { + CurrentInstance.set(VaadinService.class, null); + } + + private ByteArrayOutputStream serializeDesign(DesignContext context) + throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Design.write(context, out); + + return out; + } + + @Test + public void designIsSerializedWithCorrectPrefixesAndPackageNames() + throws IOException { + ByteArrayOutputStream out = serializeDesign(ctx); + + Document doc = Jsoup.parse(out.toString("UTF-8")); + for (Node child : doc.body().childNodes()) { + checkNode(child); + } + } + + private void checkNode(Node node) { + if (node instanceof Element) { + assertTrue("Wrong design element prefix", + node.nodeName().startsWith("v-")); + for (Node child : node.childNodes()) { + checkNode(child); + } + } + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/textarea/TextAreaDeclarativeTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/textarea/TextAreaDeclarativeTest.java new file mode 100644 index 0000000000..cbfa33320f --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/textarea/TextAreaDeclarativeTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2000-2016 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 java.io.IOException; + +import org.jsoup.nodes.Element; +import org.jsoup.parser.Tag; +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.declarative.DesignContext; + +/** + * Tests declarative support for implementations of {@link TextArea}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class TextAreaDeclarativeTest extends DeclarativeTestBase<TextArea> { + + @Test + public void testTextArea() { + String design = "<vaadin-text-area rows=6 wordwrap=false>Hello World!</vaadin-text-area>"; + TextArea ta = new TextArea(); + ta.setRows(6); + ta.setWordwrap(false); + ta.setValue("Hello World!"); + testRead(design, ta); + testWrite(design, ta); + } + + @Test + public void testHtmlEntities() throws IOException { + String design = "<vaadin-text-area>& Test</vaadin-text-area>"; + TextArea read = read(design); + Assert.assertEquals("& Test", read.getValue()); + + read.setValue("& Test"); + + DesignContext dc = new DesignContext(); + Element root = new Element(Tag.valueOf("vaadin-text-area"), ""); + read.writeDesign(root, dc); + + Assert.assertEquals("&amp; Test", root.html()); + } + + @Test + public void testReadOnlyValue() { + String design = "<vaadin-text-area readonly rows=6 wordwrap=false>Hello World!</vaadin-text-area>"; + TextArea ta = new TextArea(); + ta.setRows(6); + ta.setWordwrap(false); + ta.setValue("Hello World!"); + ta.setReadOnly(true); + testRead(design, ta); + testWrite(design, ta); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/ui/TextAreaTest.java b/compatibility-server/src/test/java/com/vaadin/ui/TextAreaTest.java new file mode 100644 index 0000000000..07bce554df --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/ui/TextAreaTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2016 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; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.util.ObjectProperty; + +public class TextAreaTest { + @Test + public void initiallyEmpty() { + TextArea tf = new TextArea(); + Assert.assertTrue(tf.isEmpty()); + } + + @Test + public void emptyAfterClearUsingPDS() { + TextArea tf = new TextArea(new ObjectProperty<String>("foo")); + Assert.assertFalse(tf.isEmpty()); + tf.clear(); + Assert.assertTrue(tf.isEmpty()); + } + + @Test + public void emptyAfterClear() { + TextArea tf = new TextArea(); + tf.setValue("foobar"); + Assert.assertFalse(tf.isEmpty()); + tf.clear(); + Assert.assertTrue(tf.isEmpty()); + } + +} diff --git a/compatibility-server/src/test/resources/com/vaadin/tests/design/testFile-legacy.html b/compatibility-server/src/test/resources/com/vaadin/tests/design/testFile-legacy.html new file mode 100644 index 0000000000..79ae1e9eaf --- /dev/null +++ b/compatibility-server/src/test/resources/com/vaadin/tests/design/testFile-legacy.html @@ -0,0 +1,19 @@ +<!DOCTYPE html> +<html> + <head> + <meta name="package-mapping" content="my:com.addon.mypackage"/> + </head> + <body> + <v-vertical-layout width="500px"> + <v-horizontal-layout> + <v-label plain-text caption="FooBar"></v-label> + <v-native-button _id=firstButton>Native click me</v-native-button> + <v-native-button id = secondButton _id="localID">Another button</v-native-button> + <v-native-button>Yet another button</v-native-button> + <v-button plain-text width = "150px">Click me</v-button> + </v-horizontal-layout> + <v-text-field caption = "Text input"/> + <v-text-area caption = "Text area" height="200px" width="300px"/> + </v-vertical-layout> + </body> +</html>
\ No newline at end of file |