diff options
author | Artur Signell <artur@vaadin.com> | 2014-12-14 21:22:14 +0200 |
---|---|---|
committer | Matti Hosio <mhosio@vaadin.com> | 2014-12-15 13:25:37 +0200 |
commit | 5d5e8c4c05b9ab765cebcae10f96144c2f570681 (patch) | |
tree | 9ae64d7568900afa7c3a8810b9bb639aebab3c5e | |
parent | 898e28d6aebc2b484f59f528a609bd59e5dfa4f5 (diff) | |
download | vaadin-framework-5d5e8c4c05b9ab765cebcae10f96144c2f570681.tar.gz vaadin-framework-5d5e8c4c05b9ab765cebcae10f96144c2f570681.zip |
Tests for declarative support (#7749)
Change-Id: I4dee7d3583a80f2f6a7334792ca064c725320bf0
4 files changed, 211 insertions, 0 deletions
diff --git a/uitest/src/com/vaadin/tests/declarative/DeclarativeEditor.java b/uitest/src/com/vaadin/tests/declarative/DeclarativeEditor.java new file mode 100644 index 0000000000..17942ee201 --- /dev/null +++ b/uitest/src/com/vaadin/tests/declarative/DeclarativeEditor.java @@ -0,0 +1,133 @@ +/* + * 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.declarative; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import org.apache.tools.ant.filters.StringInputStream; + +import com.vaadin.annotations.Theme; +import com.vaadin.data.Property.ReadOnlyException; +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; +import com.vaadin.data.Property.ValueChangeNotifier; +import com.vaadin.event.FieldEvents.TextChangeEvent; +import com.vaadin.event.FieldEvents.TextChangeListener; +import com.vaadin.server.VaadinRequest; +import com.vaadin.ui.Component; +import com.vaadin.ui.HasComponents; +import com.vaadin.ui.HorizontalSplitPanel; +import com.vaadin.ui.Panel; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.UI; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.declarative.Design; +import com.vaadin.ui.declarative.DesignContext; + +@Theme("valo") +public class DeclarativeEditor extends UI { + + private VerticalLayout treeHolder; + private TextArea editor; + private DesignContext dc; + private boolean disableEvents = false; + private HorizontalSplitPanel main; + + @Override + protected void init(VaadinRequest request) { + main = new HorizontalSplitPanel(); + editor = new TextArea(); + editor.setSizeFull(); + try { + editor.setValue(IOUtils.toString(getClass().getResourceAsStream( + "DeclarativeEditorInitial.html"))); + } catch (ReadOnlyException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + editor.addTextChangeListener(new TextChangeListener() { + + @Override + public void textChange(TextChangeEvent event) { + editor.setComponentError(null); + updateTree(event.getText()); + } + }); + + Panel editorPanel = new Panel(editor); + editorPanel.setSizeFull(); + treeHolder = new VerticalLayout(); + treeHolder.setSizeFull(); + + main.addComponents(editorPanel, treeHolder); + main.setSizeFull(); + + setContent(main); + updateTree(editor.getValue()); + } + + protected void updateTree(String string) { + if (disableEvents) { + return; + } + + dc = Design.read(new StringInputStream(string), null); + treeHolder.removeAllComponents(); + treeHolder.addComponent(dc.getRootComponent()); + + addValueChangeListeners(dc.getRootComponent()); + } + + protected void updateCode() { + if (disableEvents) { + return; + } + + ByteArrayOutputStream o = new ByteArrayOutputStream(); + try { + Design.write(treeHolder.getComponent(0), o); + disableEvents = true; + editor.setValue(o.toString("UTF-8")); + disableEvents = false; + } catch (IOException e1) { + e1.printStackTrace(); + } + + } + + private void addValueChangeListeners(Component component) { + if (component instanceof ValueChangeNotifier) { + ((ValueChangeNotifier) component) + .addValueChangeListener(new ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + updateCode(); + } + }); + } + + if (component instanceof HasComponents) { + for (Component c : ((HasComponents) component)) { + addValueChangeListeners(c); + } + } + + } + +} diff --git a/uitest/src/com/vaadin/tests/declarative/DeclarativeEditorInitial.html b/uitest/src/com/vaadin/tests/declarative/DeclarativeEditorInitial.html new file mode 100644 index 0000000000..17f7d6aa59 --- /dev/null +++ b/uitest/src/com/vaadin/tests/declarative/DeclarativeEditorInitial.html @@ -0,0 +1,23 @@ +<v-vertical-layout size-full margin spacing> + <v-horizontal-layout spacing margin width-full> + <v-label size-auto>POTUS Database</v-label> + <v-button :expand>Add new</v-button> + </v-horizontal-layout> + <v-table _id="potusList" :expand selectable size-full /> + <v-vertical-layout _id="form" spacing margin> + <v-horizontal-layout spacing> + <v-text-field caption="First Name" width="300px" /> + <v-text-field caption="Last Name" width="300px" /> + </v-horizontal-layout> + <v-horizontal-layout spacing> + <v-combo-box caption="Party" width="300px" /> + <v-popup-date-field caption="Took Office" /> + <v-popup-date-field caption="Left Office" /> + </v-horizontal-layout> + <v-horizontal-layout spacing width-full> + <v-button style-name="primary">Save</v-button> + <v-button>Revert</v-button> + <v-button :expand :right>Delete</v-button> + </v-horizontal-layout> + </v-vertical-layout> +</v-vertical-layout>
\ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/declarative/PotusCrud.html b/uitest/src/com/vaadin/tests/declarative/PotusCrud.html new file mode 100644 index 0000000000..1a73699b29 --- /dev/null +++ b/uitest/src/com/vaadin/tests/declarative/PotusCrud.html @@ -0,0 +1,23 @@ +<v-vertical-layout size-full margin="true" spacing> + <v-horizontal-layout spacing margin width-full> + <v-label size-auto>POTUS Database</v-label> + <v-button :expand>Add new</v-button> + </v-horizontal-layout> + <v-table _id="potusList" :expand selectable size-full/> + <v-vertical-layout _id="form" spacing margin> + <v-horizontal-layout spacing> + <v-text-field caption="First Name" width="300px" /> + <v-text-field caption="Last Name" width="300px" /> + </v-horizontal-layout> + <v-horizontal-layout spacing> + <v-combo-box caption="Party" width="300px" /> + <v-popup-date-field caption="Took Office" /> + <v-popup-date-field caption="Left Office" /> + </v-horizontal-layout> + <v-horizontal-layout spacing width-full> + <v-button style-name="primary">Save</v-button> + <v-button>Revert</v-button> + <v-button style-name="danger" :expand :right>Delete</v-button> + </v-horizontal-layout> + </v-vertical-layout> +</v-vertical-layout> diff --git a/uitest/src/com/vaadin/tests/declarative/PotusCrud.java b/uitest/src/com/vaadin/tests/declarative/PotusCrud.java new file mode 100644 index 0000000000..3fc924fea4 --- /dev/null +++ b/uitest/src/com/vaadin/tests/declarative/PotusCrud.java @@ -0,0 +1,32 @@ +/* + * 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.declarative; + +import com.vaadin.annotations.Theme; +import com.vaadin.server.VaadinRequest; +import com.vaadin.ui.UI; +import com.vaadin.ui.declarative.Design; + +@Theme("valo") +public class PotusCrud extends UI { + + @Override + protected void init(VaadinRequest request) { + setContent(Design + .read(getClass().getResourceAsStream("PotusCrud.html"))); + } + +} |