Change-Id: I0ac58945d70676135d982f5b829b065b96b1ffa4tags/7.4.6
import com.vaadin.ui.Button.ClickEvent; | import com.vaadin.ui.Button.ClickEvent; | ||||
import com.vaadin.ui.HasComponents; | import com.vaadin.ui.HasComponents; | ||||
import com.vaadin.ui.JavaScriptFunction; | import com.vaadin.ui.JavaScriptFunction; | ||||
import elemental.json.JsonArray; | import elemental.json.JsonArray; | ||||
import elemental.json.JsonObject; | |||||
import elemental.json.JsonValue; | |||||
public class BasicJavaScriptComponent extends AbstractTestUI { | public class BasicJavaScriptComponent extends AbstractTestUI { | ||||
} | } | ||||
}); | }); | ||||
addFunction("sendDifferentTypeOfData", new JavaScriptFunction() { | |||||
@Override | |||||
public void call(JsonArray arguments) { | |||||
for (int i = 0; i < arguments.length(); i++) { | |||||
JsonValue arg = arguments.get(i); | |||||
if (arg instanceof JsonObject) { | |||||
JsonObject o = (JsonObject) arg; | |||||
log.log("Argument[" + i + "] type: " | |||||
+ arg.getClass().getName()); | |||||
for (String key : o.keys()) { | |||||
JsonValue v = o.get(key); | |||||
log.log("Argument[" + i + "][" + key | |||||
+ "] type: " + v.getClass().getName() | |||||
+ ", value: " + v.asString()); | |||||
} | |||||
} else { | |||||
log.log("Argument[" + i + "] type: " | |||||
+ arg.getClass().getName() + ", value: " | |||||
+ arg.asString()); | |||||
} | |||||
} | |||||
} | |||||
}); | |||||
getRpcProxy(TestRpc.class).sendRpc("RPC message"); | getRpcProxy(TestRpc.class).sendRpc("RPC message"); | ||||
callFunction("messageToClient", "Callback message"); | callFunction("messageToClient", "Callback message"); | ||||
} | } | ||||
} | } | ||||
private final Log log = new Log(5); | |||||
private final Log log = new Log(15); | |||||
@Override | @Override | ||||
protected void setup(VaadinRequest request) { | protected void setup(VaadinRequest request) { |
parentIds.push(connectorId); | parentIds.push(connectorId); | ||||
connectorId = this.getParentId(connectorId); | connectorId = this.getParentId(connectorId); | ||||
} | } | ||||
this.sendDifferentTypeOfData(new Date(123), "a string", 556, true ,{"aString": "value1","anInt":556,"aBoolean":false,"aDate":new Date(111)}); | |||||
this.reportParentIds(parentIds); | this.reportParentIds(parentIds); | ||||
this.onStateChange = function() { | this.onStateChange = function() { | ||||
var e = this.getElement(); | var e = this.getElement(); | ||||
package com.vaadin.tests.components.javascriptcomponent; | |||||
import org.junit.Assert; | |||||
import org.junit.Test; | |||||
import org.openqa.selenium.By; | |||||
import com.vaadin.testbench.elements.AbstractComponentElement; | |||||
import com.vaadin.testbench.elements.AbstractJavaScriptComponentElement; | |||||
import com.vaadin.testbench.elements.ButtonElement; | |||||
import com.vaadin.testbench.parallel.BrowserUtil; | |||||
import com.vaadin.tests.tb3.MultiBrowserTest; | |||||
public class BasicJavaScriptComponentTest extends MultiBrowserTest { | |||||
@Test | |||||
public void javascriptCommunication() throws Exception { | |||||
openTestURL(); | |||||
int idx = 0; | |||||
Assert.assertEquals( | |||||
"12. Got callback message: Callback message processed", | |||||
getLogRow(idx++)); | |||||
Assert.assertEquals("11. Got RPC message: RPC message processed", | |||||
getLogRow(idx++)); | |||||
Assert.assertEquals("10. Parent ids checked", getLogRow(idx++)); | |||||
// Data types in JS functions | |||||
String expected = "1970-01-01T00:00:00.111Z"; | |||||
if (BrowserUtil.isIE8(getDesiredCapabilities())) { | |||||
expected = "1970-01-01T00:00:00Z"; | |||||
} | |||||
Assert.assertEquals( | |||||
"9. Argument[4][aDate] type: elemental.json.impl.JreJsonString, value: " | |||||
+ expected, getLogRow(idx++)); | |||||
Assert.assertEquals( | |||||
"8. Argument[4][aBoolean] type: elemental.json.impl.JreJsonBoolean, value: false", | |||||
getLogRow(idx++)); | |||||
Assert.assertEquals( | |||||
"7. Argument[4][anInt] type: elemental.json.impl.JreJsonNumber, value: 556", | |||||
getLogRow(idx++)); | |||||
Assert.assertEquals( | |||||
"6. Argument[4][aString] type: elemental.json.impl.JreJsonString, value: value1", | |||||
getLogRow(idx++)); | |||||
Assert.assertEquals( | |||||
"5. Argument[4] type: elemental.json.impl.JreJsonObject", | |||||
getLogRow(idx++)); | |||||
Assert.assertEquals( | |||||
"4. Argument[3] type: elemental.json.impl.JreJsonBoolean, value: true", | |||||
getLogRow(idx++)); | |||||
Assert.assertEquals( | |||||
"3. Argument[2] type: elemental.json.impl.JreJsonNumber, value: 556", | |||||
getLogRow(idx++)); | |||||
Assert.assertEquals( | |||||
"2. Argument[1] type: elemental.json.impl.JreJsonString, value: a string", | |||||
getLogRow(idx++)); | |||||
expected = "1970-01-01T00:00:00.123Z"; | |||||
if (BrowserUtil.isIE8(getDesiredCapabilities())) { | |||||
expected = "1970-01-01T00:00:00Z"; | |||||
} | |||||
Assert.assertEquals( | |||||
"1. Argument[0] type: elemental.json.impl.JreJsonString, value: " | |||||
+ expected, getLogRow(idx++)); | |||||
// Component attributes | |||||
AbstractJavaScriptComponentElement jsComponent = $( | |||||
AbstractJavaScriptComponentElement.class).first(); | |||||
Assert.assertEquals("Component caption", getCaption(jsComponent)); | |||||
// app://APP/connector/[uiid]/[cid]/[key]/[filename] | |||||
Assert.assertTrue(getChildText(jsComponent, 0).matches( | |||||
"4. Url: .*/run/APP/connector/0/\\d+/test")); | |||||
Assert.assertEquals("3. State message: Second state message", | |||||
getChildText(jsComponent, 1)); | |||||
Assert.assertEquals("2. State message: First state message", | |||||
getChildText(jsComponent, 2)); | |||||
// Can't/shouldn't check parent class name as we used to because it | |||||
// relies on parent state change events being fired before child state | |||||
// change events and this is not guaranteed | |||||
// Modifications | |||||
ButtonElement button = $(ButtonElement.class).first(); | |||||
button.click(); | |||||
Assert.assertEquals("Remove component\nDon't mess with me", | |||||
button.getText()); | |||||
} | |||||
private String getCaption(AbstractComponentElement c) { | |||||
return c.findElement(By.xpath("../div[@class='v-caption']")).getText(); | |||||
} | |||||
private String getChildText(AbstractComponentElement e, int index) { | |||||
return e.findElement(By.xpath("(./div)[" + (index + 1) + "]")) | |||||
.getText(); | |||||
} | |||||
} |
@Widgetset("com.vaadin.tests.widgetset.TestingWidgetSet") | @Widgetset("com.vaadin.tests.widgetset.TestingWidgetSet") | ||||
public class SerializerTest extends AbstractTestUI { | public class SerializerTest extends AbstractTestUI { | ||||
private Log log = new Log(50); | |||||
private Log log = new Log(80); | |||||
@Override | @Override | ||||
protected void setup(VaadinRequest request) { | protected void setup(VaadinRequest request) { | ||||
log.setNumberLogRows(false); | log.setNumberLogRows(false); | ||||
addComponent(log); | addComponent(log); | ||||
SerializerTestRpc rpc = testExtension | |||||
final SerializerTestRpc rpc = testExtension | |||||
.getRpcProxy(SerializerTestRpc.class); | .getRpcProxy(SerializerTestRpc.class); | ||||
SerializerTestState state = testExtension.getState(); | SerializerTestState state = testExtension.getState(); | ||||
rpc.sendDate(new Date(1)); | rpc.sendDate(new Date(1)); | ||||
rpc.sendDate(new Date(2013 - 1900, 5 - 1, 31, 11, 12, 13)); | rpc.sendDate(new Date(2013 - 1900, 5 - 1, 31, 11, 12, 13)); | ||||
rpc.sendDateArray(new Date[] { new Date(2013 - 1900, 1, 1), | |||||
new Date(2012 - 1900, 1, 1) }); | |||||
state.jsonNull = Json.createNull(); | state.jsonNull = Json.createNull(); | ||||
state.jsonString = Json.create("a string"); | state.jsonString = Json.create("a string"); | ||||
state.jsonBoolean = Json.create(false); | state.jsonBoolean = Json.create(false); | ||||
rpc.sendJson(Json.create(true), Json.createNull(), Json.create("JSON")); | rpc.sendJson(Json.create(true), Json.createNull(), Json.create("JSON")); | ||||
state.dateArray = new Date[] { new Date(1), new Date(2) }; | |||||
state.date1 = new Date(1); | state.date1 = new Date(1); | ||||
state.date2 = new Date(2013 - 1900, 5 - 1, 31, 11, 12, 13); | state.date2 = new Date(2013 - 1900, 5 - 1, 31, 11, 12, 13); | ||||
log.log("sendDate: " + format.format(date)); | log.log("sendDate: " + format.format(date)); | ||||
} | } | ||||
@Override | |||||
public void sendDateArray(Date[] dateArray) { | |||||
DateFormat format = DateFormat.getDateTimeInstance( | |||||
DateFormat.LONG, DateFormat.FULL, | |||||
new Locale("en", "fi")); | |||||
format.setTimeZone(TimeZone.getTimeZone("UTC")); | |||||
String dates = ""; | |||||
for (Date date : dateArray) { | |||||
dates += " " + format.format(date); | |||||
} | |||||
log.log("sendDateArray: " + dates); | |||||
} | |||||
@Override | @Override | ||||
public void sendJson(JsonValue value1, JsonValue value2, | public void sendJson(JsonValue value1, JsonValue value2, | ||||
JsonString string) { | JsonString string) { |
Assert.assertEquals( | Assert.assertEquals( | ||||
"sendJson: {\"b\":false,\"s\":\"JSON\"}, null, \"value\"", | "sendJson: {\"b\":false,\"s\":\"JSON\"}, null, \"value\"", | ||||
getLogRow(logRow++)); | getLogRow(logRow++)); | ||||
Assert.assertEquals( | |||||
"sendDateArray: January 31, 2013 10:00:00 PM UTC January 31, 2012 10:00:00 PM UTC", | |||||
getLogRow(logRow++)); | |||||
Assert.assertEquals("sendDate: May 31, 2013 8:12:13 AM UTC", | Assert.assertEquals("sendDate: May 31, 2013 8:12:13 AM UTC", | ||||
getLogRow(logRow++)); | getLogRow(logRow++)); | ||||
Assert.assertEquals("sendDate: January 1, 1970 12:00:00 AM UTC", | Assert.assertEquals("sendDate: January 1, 1970 12:00:00 AM UTC", | ||||
"sendBoolean: false, false, [false, false, true, false, true, true]", | "sendBoolean: false, false, [false, false, true, false, true, true]", | ||||
getLogRow(logRow++)); | getLogRow(logRow++)); | ||||
Assert.assertEquals("sendBeanSubclass: 43", getLogRow(logRow++)); | Assert.assertEquals("sendBeanSubclass: 43", getLogRow(logRow++)); | ||||
Assert.assertEquals( | |||||
"state.dateArray: Thu Jan 01 02:00:00 GMT+200 1970 Thu Jan 01 02:00:00 GMT+200 1970", | |||||
getLogRow(logRow++)); | |||||
Assert.assertEquals("state.date2: Fri May 31 11:12:13 GMT+300 2013", | |||||
getLogRow(logRow++)); | |||||
Assert.assertEquals("state.date1: Thu Jan 01 02:00:00 GMT+200 1970", | |||||
getLogRow(logRow++)); | |||||
Assert.assertEquals("state.jsonBoolean: false", getLogRow(logRow++)); | Assert.assertEquals("state.jsonBoolean: false", getLogRow(logRow++)); | ||||
Assert.assertEquals("state.jsonString: a string", getLogRow(logRow++)); | Assert.assertEquals("state.jsonString: a string", getLogRow(logRow++)); | ||||
Assert.assertEquals("state.jsonNull: NULL", getLogRow(logRow++)); | Assert.assertEquals("state.jsonNull: NULL", getLogRow(logRow++)); |
rpc.sendDate(date); | rpc.sendDate(date); | ||||
} | } | ||||
@Override | |||||
public void sendDateArray(Date[] date) { | |||||
rpc.sendDateArray(date); | |||||
} | |||||
@Override | @Override | ||||
public void sendJson(JsonValue value1, JsonValue value2, | public void sendJson(JsonValue value1, JsonValue value2, | ||||
JsonString string) { | JsonString string) { | ||||
+ ((JsonString) getState().jsonString).getString()); | + ((JsonString) getState().jsonString).getString()); | ||||
rpc.log("state.jsonBoolean: " + getState().jsonBoolean.getBoolean()); | rpc.log("state.jsonBoolean: " + getState().jsonBoolean.getBoolean()); | ||||
rpc.log("state.date1: " + getState().date1); | |||||
rpc.log("state.date2: " + getState().date2); | |||||
String arrStr = ""; | |||||
for (Date d : getState().dateArray) { | |||||
arrStr += d + " "; | |||||
} | |||||
rpc.log("state.dateArray: " + arrStr); | |||||
/* | /* | ||||
* TODO public double doubleValue; public Double DoubleValue; public | * TODO public double doubleValue; public Double DoubleValue; public | ||||
* double[] doubleArray; ; | * double[] doubleArray; ; |
public void sendDate(Date date); | public void sendDate(Date date); | ||||
public void sendDateArray(Date[] dates); | |||||
public void sendJson(JsonValue value1, JsonValue value2, JsonString string); | public void sendJson(JsonValue value1, JsonValue value2, JsonString string); | ||||
public void log(String string); | public void log(String string); | ||||
} | } |
public Date date1; | public Date date1; | ||||
public Date date2; | public Date date2; | ||||
public Date[] dateArray; | |||||
public BeanWithAbstractSuperclass beanWithAbstractSuperclass; | public BeanWithAbstractSuperclass beanWithAbstractSuperclass; | ||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |||||
<head profile="http://selenium-ide.openqa.org/profiles/test-case"> | |||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |||||
<link rel="selenium.base" href="" /> | |||||
<title>New Test</title> | |||||
</head> | |||||
<body> | |||||
<table cellpadding="1" cellspacing="1" border="1"> | |||||
<thead> | |||||
<tr><td rowspan="1" colspan="3">New Test</td></tr> | |||||
</thead><tbody> | |||||
<tr> | |||||
<td>open</td> | |||||
<td>/run/com.vaadin.tests.components.javascriptcomponent.BasicJavaScriptComponent?restartApplication</td> | |||||
<td></td> | |||||
</tr> | |||||
<tr> | |||||
<td>assertText</td> | |||||
<td>vaadin=runcomvaadintestscomponentsjavascriptcomponentBasicJavaScriptComponent::/VVerticalLayout[0]/VVerticalLayout[0]/VVerticalLayout[0]/VLabel[0]</td> | |||||
<td>3. Got callback message: Callback message processed</td> | |||||
</tr> | |||||
<tr> | |||||
<td>assertText</td> | |||||
<td>vaadin=runcomvaadintestscomponentsjavascriptcomponentBasicJavaScriptComponent::/VVerticalLayout[0]/VVerticalLayout[0]/VVerticalLayout[0]/VLabel[1]</td> | |||||
<td>2. Got RPC message: RPC message processed</td> | |||||
</tr> | |||||
<tr> | |||||
<td>assertText</td> | |||||
<td>vaadin=runcomvaadintestscomponentsjavascriptcomponentBasicJavaScriptComponent::/VVerticalLayout[0]/VVerticalLayout[0]/VVerticalLayout[0]/VLabel[2]</td> | |||||
<td>1. Parent ids checked</td> | |||||
</tr> | |||||
<tr> | |||||
<td>assertText</td> | |||||
<td>vaadin=runcomvaadintestscomponentsjavascriptcomponentBasicJavaScriptComponent::/VVerticalLayout[0]/VVerticalLayout[0]/domChild[1]/domChild[0]/domChild[0]</td> | |||||
<td>Component caption</td> | |||||
</tr> | |||||
<tr> | |||||
<td>assertText</td> | |||||
<td>vaadin=runcomvaadintestscomponentsjavascriptcomponentBasicJavaScriptComponent::/VVerticalLayout[0]/VVerticalLayout[0]/JavaScriptWidget[0]/domChild[0]</td> | |||||
<td>4. Url: */run/APP/connector/0/10/test</td> | |||||
</tr> | |||||
<tr> | |||||
<td>assertText</td> | |||||
<td>vaadin=runcomvaadintestscomponentsjavascriptcomponentBasicJavaScriptComponent::/VVerticalLayout[0]/VVerticalLayout[0]/JavaScriptWidget[0]/domChild[1]</td> | |||||
<td>3. State message: Second state message</td> | |||||
</tr> | |||||
<tr> | |||||
<td>assertText</td> | |||||
<td>vaadin=runcomvaadintestscomponentsjavascriptcomponentBasicJavaScriptComponent::/VVerticalLayout[0]/VVerticalLayout[0]/JavaScriptWidget[0]/domChild[2]</td> | |||||
<td>2. State message: First state message</td> | |||||
</tr> | |||||
<tr> | |||||
<td>assertText</td> | |||||
<td>vaadin=runcomvaadintestscomponentsjavascriptcomponentBasicJavaScriptComponent::/VVerticalLayout[0]/VVerticalLayout[0]/JavaScriptWidget[0]/domChild[3]</td> | |||||
<td>1. Parent element className: v-verticallayout v-layout v-vertical v-widget v-has-width</td> | |||||
</tr> | |||||
<tr> | |||||
<td>click</td> | |||||
<td>vaadin=runcomvaadintestscomponentsjavascriptcomponentBasicJavaScriptComponent::PID_SRemoveButton/domChild[0]/domChild[0]</td> | |||||
<td></td> | |||||
</tr> | |||||
<tr> | |||||
<td>assertText</td> | |||||
<td>vaadin=runcomvaadintestscomponentsjavascriptcomponentBasicJavaScriptComponent::PID_SRemoveButton</td> | |||||
<td>Remove componentDon't mess with me</td> | |||||
</tr> | |||||
</tbody></table> | |||||
</body> | |||||
</html> |