Change-Id: I7e1992fe8fc88bd0d34a4b7d3b945bc4ef68fd7etags/7.5.0.beta1
@@ -31,38 +31,30 @@ public class AbsoluteLayoutDeclarativeTest extends | |||
DeclarativeTestBase<AbsoluteLayout> { | |||
@Test | |||
public void testPlainTextRead() { | |||
testRead(getDesign(), getExpected()); | |||
} | |||
@Test | |||
public void testPlainTextWrite() { | |||
testWrite(getDesign(), getExpected()); | |||
} | |||
protected String getDesign() { | |||
return "<v-absolute-layout>" | |||
public void testAbsoluteLayoutFeatures() { | |||
String design = "<v-absolute-layout caption=\"test-layout\">" | |||
+ "<v-button :top='100px' :left='0px' :z-index=21>OK</v-button>" | |||
+ "<v-button :bottom='0px' :right='0px'>Cancel</v-button>" | |||
+ "</v-absolute-layout>"; | |||
} | |||
protected AbsoluteLayout getExpected() { | |||
AbsoluteLayout c = new AbsoluteLayout(); | |||
AbsoluteLayout layout = new AbsoluteLayout(); | |||
layout.setCaption("test-layout"); | |||
Button b1 = new Button("OK"); | |||
b1.setCaptionAsHtml(true); | |||
Button b2 = new Button("Cancel"); | |||
b2.setCaptionAsHtml(true); | |||
layout.addComponent(b1, "top: 100px; left: 0px; z-index: 21"); | |||
layout.addComponent(b2, "bottom: 0px; right: 0px;"); | |||
c.addComponent(b1, "top: 100px; left: 0px; z-index: 21"); | |||
c.addComponent(b2, "bottom: 0px; right: 0px;"); | |||
System.out.println(c.getComponentCount()); | |||
return c; | |||
}; | |||
testWrite(design, layout); | |||
testRead(design, layout); | |||
} | |||
@Test | |||
public void testEmpty() { | |||
testRead("<v-absolute-layout/>", new AbsoluteLayout()); | |||
String design = "<v-absolute-layout/>"; | |||
AbsoluteLayout layout = new AbsoluteLayout(); | |||
testRead(design, layout); | |||
testWrite(design, layout); | |||
} | |||
} |
@@ -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 ReadDesignTest extends TestCase { | |||
private AbsoluteLayout root; | |||
@Override | |||
public void setUp() throws Exception { | |||
super.setUp(); | |||
root = createLayout(); | |||
} | |||
public void testAttributes() { | |||
assertEquals("test-layout", root.getCaption()); | |||
Iterator<Component> 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<Component> 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.readDesign(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; | |||
} | |||
} |
@@ -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 WriteDesignTest 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(); | |||
} | |||
} |
@@ -0,0 +1,241 @@ | |||
/* | |||
* 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.ByteArrayInputStream; | |||
import java.io.File; | |||
import java.lang.reflect.Field; | |||
import java.nio.charset.Charset; | |||
import java.util.Locale; | |||
import org.jsoup.nodes.Attributes; | |||
import org.jsoup.nodes.Element; | |||
import org.jsoup.parser.Tag; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
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.shared.ui.label.ContentMode; | |||
import com.vaadin.tests.design.DeclarativeTestBase; | |||
import com.vaadin.ui.AbstractComponent; | |||
import com.vaadin.ui.Button; | |||
import com.vaadin.ui.HorizontalSplitPanel; | |||
import com.vaadin.ui.Label; | |||
import com.vaadin.ui.declarative.Design; | |||
import com.vaadin.ui.declarative.DesignContext; | |||
/** | |||
* Test cases for reading and writing the properties of AbstractComponent. | |||
* | |||
* @since | |||
* @author Vaadin Ltd | |||
*/ | |||
public class AbstractComponentDeclarativeTest extends | |||
DeclarativeTestBase<AbstractComponent> { | |||
private AbstractComponent component; | |||
@Before | |||
public void setUp() { | |||
Label l = new Label(); | |||
l.setContentMode(ContentMode.HTML); | |||
component = l; | |||
} | |||
@Test | |||
public void testEmptyDesign() { | |||
String design = "<v-label>"; | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testProperties() { | |||
String design = "<v-label id=\"testId\" primary-style-name=\"test-style\" " | |||
+ "caption=\"test-caption\" locale=\"fi_FI\" description=\"test-description\" " | |||
+ "error=\"<div>test-error</div>\" immediate=\"true\"/>"; | |||
component.setId("testId"); | |||
component.setPrimaryStyleName("test-style"); | |||
component.setCaption("test-caption"); | |||
component.setLocale(new Locale("fi", "FI")); | |||
component.setDescription("test-description"); | |||
component.setComponentError(new UserError("<div>test-error</div>", | |||
com.vaadin.server.AbstractErrorMessage.ContentMode.HTML, | |||
ErrorLevel.ERROR)); | |||
component.setImmediate(true); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testReadImmediate() { | |||
// Additional tests for the immediate property, including | |||
// explicit immediate values | |||
String[] design = { "<v-label/>", "<v-label immediate=\"false\"/>", | |||
"<v-label immediate=\"true\"/>", "<v-label immediate=\"\"/>" }; | |||
Boolean[] explicitImmediate = { null, Boolean.FALSE, Boolean.TRUE, | |||
Boolean.TRUE }; | |||
boolean[] immediate = { false, false, true, true }; | |||
for (int i = 0; i < design.length; i++) { | |||
component = (AbstractComponent) Design | |||
.read(new ByteArrayInputStream(design[i].getBytes(Charset | |||
.forName("UTF-8")))); | |||
assertEquals(immediate[i], component.isImmediate()); | |||
assertEquals(explicitImmediate[i], getExplicitImmediate(component)); | |||
} | |||
} | |||
@Test | |||
public void testExternalIcon() { | |||
String design = "<v-label icon=\"http://example.com/example.gif\"/>"; | |||
component | |||
.setIcon(new ExternalResource("http://example.com/example.gif")); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testThemeIcon() { | |||
String design = "<v-label icon=\"theme://example.gif\"/>"; | |||
component.setIcon(new ThemeResource("example.gif")); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testFileResourceIcon() { | |||
String design = "<v-label icon=\"img/example.gif\"/>"; | |||
component.setIcon(new FileResource(new File("img/example.gif"))); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testWidthAndHeight() { | |||
String design = "<v-label width=\"70%\" height=\"12px\"/>"; | |||
component.setWidth("70%"); | |||
component.setHeight("12px"); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testSizeFull() { | |||
String design = "<v-label size-full=\"true\"/>"; | |||
component.setSizeFull(); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testSizeAuto() { | |||
String design = "<v-label size-auto=\"true\"/>"; | |||
component.setSizeUndefined(); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testHeightFull() { | |||
String design = "<v-label height-full=\"true\"/ width=\"20px\"/>"; | |||
component.setHeight("100%"); | |||
component.setWidth("20px"); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testHeightAuto() { | |||
String design = "<v-horizontal-split-panel height-auto=\"true\"/ width=\"20px\" >"; | |||
// we need to have default height of 100% -> use split panel | |||
AbstractComponent component = new HorizontalSplitPanel(); | |||
component.setHeight(null); | |||
component.setWidth("20px"); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testWidthFull() { | |||
String design = "<v-button width-full=\"true\"/ height=\"20px\">Foo</button>"; | |||
AbstractComponent component = new Button(); | |||
component.setCaptionAsHtml(true); | |||
component.setCaption("Foo"); | |||
component.setHeight("20px"); | |||
component.setWidth("100%"); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testWidthAuto() { | |||
String design = "<v-label height=\"20px\"/ width-auto=\"true\"/>"; | |||
component.setCaptionAsHtml(false); | |||
component.setHeight("20px"); | |||
component.setWidth(null); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testResponsive() { | |||
String design = "<v-label responsive =\"true\"/>"; | |||
Responsive.makeResponsive(component); | |||
testRead(design, component); | |||
testWrite(design, component); | |||
} | |||
@Test | |||
public void testResponsiveFalse() { | |||
String design = "<v-label responsive =\"false\"/>"; | |||
// Only test read as the attribute responsive=false would not be written | |||
testRead(design, component); | |||
} | |||
@Test | |||
public void testReadAlreadyResponsive() { | |||
AbstractComponent component = new Label(); | |||
Responsive.makeResponsive(component); | |||
Element design = createDesign("responsive", ""); | |||
component.readDesign(design, new DesignContext()); | |||
assertEquals("Component should have only one extension", 1, component | |||
.getExtensions().size()); | |||
} | |||
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."); | |||
} | |||
} | |||
} |
@@ -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 ReadDesignTest 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 = getComponent(); | |||
component.readDesign(design, ctx); | |||
// 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", "<div>test-error</div>"); | |||
AbstractComponent component = getComponent(); | |||
component.readDesign(design, ctx); | |||
assertEquals("<div>test-error</div>", 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."); | |||
} | |||
} | |||
} |
@@ -1,281 +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.TabSheet; | |||
import com.vaadin.ui.declarative.DesignContext; | |||
/** | |||
* Test case for writing the attributes of the AbstractComponent to design | |||
* | |||
* @author Vaadin Ltd | |||
*/ | |||
public class WriteDesignTest 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 testSynchronizeImmediateByDefault() { | |||
Element design = createDesign(); | |||
TabSheet byDefaultImmediate = new TabSheet(); | |||
// no immediate attribute should be written before setting immediate to | |||
// false | |||
byDefaultImmediate.writeDesign(design, ctx); | |||
assertFalse(design.hasAttr("immediate")); | |||
byDefaultImmediate.setImmediate(false); | |||
byDefaultImmediate.writeDesign(design, ctx); | |||
// we only changed one of the attributes, others are at default values | |||
assertEquals(1, design.attributes().size()); | |||
assertEquals("false", 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("<div>test-error</div>", | |||
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("<div>test-error</div>", 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; | |||
} | |||
} |