Search in sources :

Example 16 with IWebFormController

use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.

the class FoundsetTest method foundsetViewportBounds.

@Test
public void foundsetViewportBounds() throws JSONException {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("test");
    Assert.assertNotNull(form);
    WebFormComponent wc = form.getFormUI().getWebComponent("mycustombean");
    FoundsetTypeSabloValue rawPropertyValue = (FoundsetTypeSabloValue) wc.getRawPropertyValue("myfoundset");
    BrowserConverterContext allowBrowserConverterContext = new BrowserConverterContext(wc, PushToServerEnum.allow);
    FoundsetTypeViewport viewPort = rawPropertyValue.getViewPort();
    viewPort.setBounds(0, 2);
    StringWriter stringWriter = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(stringWriter);
    rawPropertyValue.toJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext);
    Assert.assertTrue(new JSONObject("{\"serverSize\":18,\"foundsetId\":2,\"sortColumns\":\"\",\"selectedRowIndexes\":[0],\"multiSelect\":false,\"hasMoreRows\":false,\"viewPort\":{\"startIndex\":0,\"size\":2,\"rows\":[{\"_svyRowId\":\"1.1;_0\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"1.2;_1\",\"lastname\":\"value4\",\"firstname\":\"value3\"}]}}").similar(new JSONObject(stringWriter.toString())));
    try {
        rawPropertyValue.getFoundset().setSort("test1 asc");
    } catch (ServoyException e) {
        e.printStackTrace();
    }
    stringWriter = new StringWriter();
    jsonWriter = new JSONWriter(stringWriter);
    rawPropertyValue.toJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext);
    Assert.assertTrue(new JSONObject("{\"serverSize\":18,\"foundsetId\":2,\"sortColumns\":\"firstname asc\",\"selectedRowIndexes\":[0],\"multiSelect\":false,\"hasMoreRows\":false,\"viewPort\":{\"startIndex\":0,\"size\":2,\"rows\":[{\"_svyRowId\":\"1.1;_0\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"1.2;_1\",\"lastname\":\"value4\",\"firstname\":\"value3\"}]}}").similar(new JSONObject(stringWriter.toString())));
    try {
        rawPropertyValue.getFoundset().setSort("test2 desc,pk asc,test1 asc");
    } catch (ServoyException e) {
        e.printStackTrace();
    }
    stringWriter = new StringWriter();
    jsonWriter = new JSONWriter(stringWriter);
    rawPropertyValue.toJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext);
    Assert.assertTrue(new JSONObject("{\"serverSize\":18,\"foundsetId\":2,\"sortColumns\":\"lastname desc\",\"selectedRowIndexes\":[0],\"multiSelect\":false,\"hasMoreRows\":false,\"viewPort\":{\"startIndex\":0,\"size\":2,\"rows\":[{\"_svyRowId\":\"1.1;_0\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"1.2;_1\",\"lastname\":\"value4\",\"firstname\":\"value3\"}]}}").similar(new JSONObject(stringWriter.toString())));
    try {
        rawPropertyValue.getFoundset().setSort("test2 desc,test1 asc");
    } catch (ServoyException e) {
        e.printStackTrace();
    }
    stringWriter = new StringWriter();
    jsonWriter = new JSONWriter(stringWriter);
    rawPropertyValue.toJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext);
    Assert.assertTrue(new JSONObject("{\"serverSize\":18,\"foundsetId\":2,\"sortColumns\":\"lastname desc,firstname asc\",\"selectedRowIndexes\":[0],\"multiSelect\":false,\"hasMoreRows\":false,\"viewPort\":{\"startIndex\":0,\"size\":2,\"rows\":[{\"_svyRowId\":\"1.1;_0\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"1.2;_1\",\"lastname\":\"value4\",\"firstname\":\"value3\"}]}}").similar(new JSONObject(stringWriter.toString())));
    try {
        rawPropertyValue.getFoundset().setSort("pk asc,test1 asc");
    } catch (ServoyException e) {
        e.printStackTrace();
    }
    stringWriter = new StringWriter();
    jsonWriter = new JSONWriter(stringWriter);
    rawPropertyValue.toJSON(jsonWriter, new DataConversion(), allowBrowserConverterContext);
    Assert.assertTrue(new JSONObject("{\"serverSize\":18,\"foundsetId\":2,\"sortColumns\":\"\",\"selectedRowIndexes\":[0],\"multiSelect\":false,\"hasMoreRows\":false,\"viewPort\":{\"startIndex\":0,\"size\":2,\"rows\":[{\"_svyRowId\":\"1.1;_0\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"1.2;_1\",\"lastname\":\"value4\",\"firstname\":\"value3\"}]}}").similar(new JSONObject(stringWriter.toString())));
    // changes to json
    // add changes test
    viewPort.setBounds(1, 1);
    StringWriter stringWriter2 = new StringWriter();
    JSONWriter jsonWriter2 = new JSONWriter(stringWriter2);
    rawPropertyValue.toJSON(jsonWriter2, new DataConversion(), allowBrowserConverterContext);
    Assert.assertTrue(new JSONObject("{\"serverSize\":18,\"foundsetId\":2,\"sortColumns\":\"\",\"selectedRowIndexes\":[0],\"multiSelect\":false,\"hasMoreRows\":false,\"viewPort\":{\"startIndex\":1,\"size\":1,\"rows\":[{\"_svyRowId\":\"1.2;_1\",\"lastname\":\"value4\",\"firstname\":\"value3\"}]}}").similar(new JSONObject(stringWriter2.toString())));
    viewPort.loadExtraRecords(-1);
    StringWriter stringWriter3 = new StringWriter();
    JSONWriter jsonWriter3 = new JSONWriter(stringWriter3);
    rawPropertyValue.changesToJSON(jsonWriter3, new DataConversion(), allowBrowserConverterContext);
    Assert.assertTrue(new JSONObject("{\"upd_viewPort\":{\"startIndex\":0,\"size\":2,\"upd_rows\":[{\"rows\":[{\"_svyRowId\":\"1.1;_0\",\"lastname\":\"value2\",\"firstname\":\"value1\"}],\"startIndex\":0,\"endIndex\":0,\"type\":1}]}}").similar(new JSONObject(stringWriter3.toString())));
    viewPort.loadExtraRecords(-1);
    stringWriter3 = new StringWriter();
    jsonWriter3 = new JSONWriter(stringWriter3);
    rawPropertyValue.changesToJSON(jsonWriter3, new DataConversion(), allowBrowserConverterContext);
    Assert.assertEquals(new JSONObject("{\"n\":true}").toString(), new JSONObject(stringWriter3.toString()).toString());
    viewPort.loadExtraRecords(16);
    stringWriter3 = new StringWriter();
    jsonWriter3 = new JSONWriter(stringWriter3);
    rawPropertyValue.changesToJSON(jsonWriter3, new DataConversion(), allowBrowserConverterContext);
    Assert.assertTrue(new JSONObject("{\"upd_viewPort\":{\"startIndex\":0,\"size\":18,\"upd_rows\":[{\"rows\":[{\"_svyRowId\":\"1.3;_2\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"1.4;_3\",\"lastname\":\"value4\",\"firstname\":\"value3\"},{\"_svyRowId\":\"1.5;_4\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"1.6;_5\",\"lastname\":\"value4\",\"firstname\":\"value3\"},{\"_svyRowId\":\"1.7;_6\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"1.8;_7\",\"lastname\":\"value4\",\"firstname\":\"value3\"},{\"_svyRowId\":\"1.9;_8\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"2.10;_9\",\"lastname\":\"value4\",\"firstname\":\"value3\"},{\"_svyRowId\":\"2.11;_10\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"2.12;_11\",\"lastname\":\"value4\",\"firstname\":\"value3\"},{\"_svyRowId\":\"2.13;_12\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"2.14;_13\",\"lastname\":\"value4\",\"firstname\":\"value3\"},{\"_svyRowId\":\"2.15;_14\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"2.16;_15\",\"lastname\":\"value4\",\"firstname\":\"value3\"},{\"_svyRowId\":\"2.17;_16\",\"lastname\":\"value2\",\"firstname\":\"value1\"},{\"_svyRowId\":\"2.18;_17\",\"lastname\":\"value4\",\"firstname\":\"value3\"}],\"startIndex\":2,\"endIndex\":17,\"type\":1}]}}").similar(new JSONObject(stringWriter3.toString())));
    viewPort.loadExtraRecords(1);
    stringWriter3 = new StringWriter();
    jsonWriter3 = new JSONWriter(stringWriter3);
    rawPropertyValue.changesToJSON(jsonWriter3, new DataConversion(), allowBrowserConverterContext);
    Assert.assertEquals("{\"n\":true}", stringWriter3.toString());
}
Also used : JSONWriter(org.json.JSONWriter) DataConversion(org.sablo.websocket.utils.DataConversion) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) ServoyException(com.servoy.j2db.util.ServoyException) Test(org.junit.Test)

Example 17 with IWebFormController

use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.

the class FoundsetTest method foundsetWithDynamicDataproviders.

@Test
public void foundsetWithDynamicDataproviders() throws JSONException {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("test");
    WebFormComponent webComponent = form.getFormUI().getWebComponent("mydynamiccustombean");
    FoundsetTypeSabloValue property = (FoundsetTypeSabloValue) webComponent.getProperty("myfoundset");
    JSONArray json = new JSONArray("[{" + FoundsetTypeSabloValue.PREFERRED_VIEWPORT_SIZE + ":1}]");
    property.browserUpdatesReceived(json, webComponent.getSpecification().getProperty("myfoundset"), new BrowserConverterContext(webComponent, PushToServerEnum.allow));
    Assert.assertNotNull(form);
    String full = NGUtils.formComponentPropertiesToString(form.getFormUI(), FullValueToJSONConverter.INSTANCE);
    JSONObject object = new JSONObject(full);
    JSONObject bean = object.getJSONObject("mydynamiccustombean");
    JSONObject foundset = bean.getJSONObject("myfoundset");
    Assert.assertEquals(12, foundset.getInt("serverSize"));
    JSONObject viewPort = foundset.getJSONObject("viewPort");
    Assert.assertEquals(0, viewPort.getInt("startIndex"));
    Assert.assertEquals(12, viewPort.getInt("size"));
    Assert.assertEquals(12, viewPort.getJSONArray("rows").length());
    // fake incomming request for view port change.
    endpoint.incoming("{\"methodname\":\"dataPush\",\"args\":{\"beanname\":\"mydynamiccustombean\",\"formname\":\"test\",\"changes\":{\"myfoundset\":[{\"newViewPort\":{\"startIndex\":0,\"size\":3}, \"id\": 4312}]}},\"service\":\"formService\"}", true);
    String changes = NGUtils.formChangesToString(((Container) form.getFormUI()), FullValueToJSONConverter.INSTANCE);
    object = new JSONObject(changes).getJSONObject("changes");
    bean = object.getJSONObject("mydynamiccustombean");
    foundset = bean.getJSONObject("myfoundset");
    Assert.assertEquals(12, foundset.getInt("serverSize"));
    viewPort = foundset.getJSONObject("viewPort");
    Assert.assertEquals(0, viewPort.getInt("startIndex"));
    Assert.assertEquals(3, viewPort.getInt("size"));
    JSONArray rows = viewPort.getJSONArray("rows");
    Assert.assertEquals(3, rows.length());
    JSONArray handledClientReqIds = foundset.getJSONArray("handledClientReqIds");
    Assert.assertEquals(1, handledClientReqIds.length());
    Assert.assertEquals(4312, handledClientReqIds.getJSONObject(0).getInt("id"));
    Assert.assertTrue(handledClientReqIds.getJSONObject(0).getBoolean("value"));
    JSONObject row0 = rows.getJSONObject(0);
    Assert.assertEquals("relatedvalue111", row0.getString("dp1"));
    Assert.assertEquals("relatedvalue112", row0.getString("dp2"));
    JSONObject row1 = rows.getJSONObject(1);
    Assert.assertEquals("relatedvalue121", row1.getString("dp1"));
    Assert.assertEquals("relatedvalue122", row1.getString("dp2"));
}
Also used : Container(org.sablo.Container) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) JSONArray(org.json.JSONArray) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Test(org.junit.Test)

Example 18 with IWebFormController

use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.

the class RelatedFormsTest method testRelatedFormsInTabPanels.

@Test
public void testRelatedFormsInTabPanels() throws RepositoryException {
    // f1 is the main
    // 
    // 3 forms on that main
    // 
    // f2 "relation2"
    // f3 "relation2.relation3"
    // f4 "relation2.relation3.relation4"
    Form f1 = solution.createNewForm(validator, null, "f1", null, false, new Dimension(600, 400));
    IWebFormController f1Controller = new TestFormController(f1, client);
    IDataAdapterList dataAdapterListF1 = f1Controller.getFormUI().getDataAdapterList();
    Form f2 = solution.createNewForm(validator, null, "f2", null, false, new Dimension(600, 400));
    IWebFormController f2Controller = new TestFormController(f2, client);
    IDataAdapterList dataAdapterListF2 = f2Controller.getFormUI().getDataAdapterList();
    Form f3 = solution.createNewForm(validator, null, "f3", null, false, new Dimension(600, 400));
    IWebFormController f3Controller = new TestFormController(f3, client);
    IDataAdapterList dataAdapterListF3 = f3Controller.getFormUI().getDataAdapterList();
    Form f4 = solution.createNewForm(validator, null, "f4", null, false, new Dimension(600, 400));
    IWebFormController f4Controller = new TestFormController(f4, client);
    IDataAdapterList dataAdapterListF4 = f4Controller.getFormUI().getDataAdapterList();
    TabPanel tabpanelF2 = f1.createNewTabPanel("tabpanelF2");
    tabpanelF2.createNewTab("tab1", "relation2", f2);
    TabPanel tabpanelF3 = f1.createNewTabPanel("tabpanelF3");
    tabpanelF3.createNewTab("tab1", "relation2.relation3", f3);
    TabPanel tabpanelF4 = f1.createNewTabPanel("tabpanelF4");
    tabpanelF4.createNewTab("tab1", "relation2.relation3.relation4", f4);
    dataAdapterListF1.addVisibleChildForm(f2Controller, "relation2", true);
    // relatedForms of f1 has now f2
    Assert.assertEquals(1, dataAdapterListF1.getRelatedForms().size());
    Assert.assertEquals("relation2", dataAdapterListF1.getRelatedForms().get(f2Controller));
    dataAdapterListF1.addVisibleChildForm(f3Controller, "relation2.relation3", true);
    // relatedForms of f1 has now f2 and f3
    Assert.assertEquals(2, dataAdapterListF1.getRelatedForms().size());
    Assert.assertEquals("relation2.relation3", dataAdapterListF1.getRelatedForms().get(f3Controller));
    // also f2 relatedForms has f3
    Assert.assertEquals(1, dataAdapterListF2.getRelatedForms().size());
    Assert.assertEquals("relation3", dataAdapterListF2.getRelatedForms().get(f3Controller));
    // and f3 relatedParentsForms has f2
    Assert.assertEquals(1, dataAdapterListF3.getParentRelatedForms().size());
    Assert.assertEquals(dataAdapterListF3.getParentRelatedForms().toString() + " should have:" + f2Controller, 0, dataAdapterListF3.getParentRelatedForms().indexOf(f2Controller));
    Assert.assertNotEquals(dataAdapterListF3.getParentRelatedForms().toString() + " should have:" + f2Controller, -1, dataAdapterListF3.getParentRelatedForms().indexOf(f2Controller));
    dataAdapterListF1.addVisibleChildForm(f4Controller, "relation2.relation3.relation4", true);
    // relatedForms of f1 has now f2 and f3 and f4
    Assert.assertEquals(3, dataAdapterListF1.getRelatedForms().size());
    Assert.assertEquals("relation2.relation3.relation4", dataAdapterListF1.getRelatedForms().get(f4Controller));
    // relatedForms of f2 has now f3 and f4
    Assert.assertEquals(2, dataAdapterListF2.getRelatedForms().size());
    Assert.assertEquals("relation3", dataAdapterListF2.getRelatedForms().get(f3Controller));
    Assert.assertEquals("relation3.relation4", dataAdapterListF2.getRelatedForms().get(f4Controller));
    // and f4 relatedParentsForms has f2 and f3
    Assert.assertEquals(2, dataAdapterListF4.getParentRelatedForms().size());
    Assert.assertTrue(dataAdapterListF4.getParentRelatedForms() + " should have " + f2Controller, dataAdapterListF4.getParentRelatedForms().indexOf(f2Controller) >= 0);
    Assert.assertTrue(dataAdapterListF4.getParentRelatedForms() + " should have " + f3Controller, dataAdapterListF4.getParentRelatedForms().indexOf(f3Controller) >= 0);
    dataAdapterListF1.removeVisibleChildForm(f4Controller, true);
    // relatedForms of f1 has now f2 and f3
    Assert.assertEquals(2, dataAdapterListF1.getRelatedForms().size());
    Assert.assertEquals("relation2", dataAdapterListF1.getRelatedForms().get(f2Controller));
    Assert.assertEquals("relation2.relation3", dataAdapterListF1.getRelatedForms().get(f3Controller));
    // relatedForms of f2 has now f3
    Assert.assertEquals(1, dataAdapterListF2.getRelatedForms().size());
    Assert.assertEquals("relation3", dataAdapterListF2.getRelatedForms().get(f3Controller));
    dataAdapterListF2.removeVisibleChildForm(f3Controller, true);
    // relatedForms of f1 has now f2 and f3
    Assert.assertEquals(2, dataAdapterListF1.getRelatedForms().size());
    Assert.assertEquals("relation2", dataAdapterListF1.getRelatedForms().get(f2Controller));
    Assert.assertEquals("relation2.relation3", dataAdapterListF1.getRelatedForms().get(f3Controller));
    // relatedForms of f2 has no related forms
    Assert.assertEquals(0, dataAdapterListF2.getRelatedForms().size());
}
Also used : TabPanel(com.servoy.j2db.persistence.TabPanel) Form(com.servoy.j2db.persistence.Form) IDataAdapterList(com.servoy.j2db.server.ngclient.IDataAdapterList) Dimension(java.awt.Dimension) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Test(org.junit.Test)

Example 19 with IWebFormController

use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.

the class RuntimeWebComponentTest method arrayPropDirectAccess.

@Test
public void arrayPropDirectAccess() throws Exception {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("testForm");
    Assert.assertNotNull(form);
    FormScope formScope = form.getFormScope();
    Context cx = Context.enter();
    try {
        // CHECK INITIAL DEFAULT VALUE FROM SPEC
        RhinoMapOrArrayWrapper stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.stringArray", "Evaluation Test Script", 1, null);
        Assert.assertArrayEquals(new String[] { "a", "b", "c" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        Boolean testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.stringArray.length === 3) && elements.testComponent.stringArray.every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
        // ADD ELEMENT TO EXISTING VALUE
        cx.evaluateString(formScope, "elements.testComponent.getStringArray().push(\"d\") ", "Evaluation Test Script", 1, null);
        // CHECK CHANGED VALUE
        stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.stringArray", "Evaluation Test Script", 1, null);
        Assert.assertArrayEquals(new String[] { "a", "b", "c", "d" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.stringArray.length === 4) && elements.testComponent.stringArray.every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\", \"d\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
        // ASSIGN DIFFERENT ARRAY BY REF TO PROPERTY
        cx.evaluateString(formScope, "elements.testComponent.stringArray = ['1', '2', '3']", "Evaluation Test Script", 1, null);
        // CHECK NEW VALUE
        stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.stringArray", "Evaluation Test Script", 1, null);
        Assert.assertArrayEquals(new String[] { "1", "2", "3" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.stringArray.length === 3) && elements.testComponent.stringArray.every(function(this_i, i) { return this_i == [\"1\", \"2\", \"3\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) FormScope(com.servoy.j2db.scripting.FormScope) Test(org.junit.Test)

Example 20 with IWebFormController

use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.

the class RuntimeWebComponentTest method arrayPropAccessThroughGetterAndSetter.

@Test
public void arrayPropAccessThroughGetterAndSetter() throws Exception {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("testForm");
    Assert.assertNotNull(form);
    FormScope formScope = form.getFormScope();
    Context cx = Context.enter();
    try {
        // CHECK INITIAL DEFAULT VALUE FROM SPEC
        RhinoMapOrArrayWrapper stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.getStringArray()", "Evaluation Test Script", 1, // this used to fail with an exception when RuntimeLegacyComponent gave null scope in getter code
        null);
        Assert.assertArrayEquals(new String[] { "a", "b", "c" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        Boolean testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.getStringArray().length === 3) && elements.testComponent.getStringArray().every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
        // ADD ELEMENT TO EXISTING VALUE
        cx.evaluateString(formScope, "elements.testComponent.getStringArray().push(\"d\") ", "Evaluation Test Script", 1, null);
        // CHECK CHANGED VALUE
        stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.getStringArray()", "Evaluation Test Script", 1, null);
        Assert.assertArrayEquals(new String[] { "a", "b", "c", "d" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.getStringArray().length === 4) && elements.testComponent.getStringArray().every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\", \"d\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
        // ASSIGN DIFFERENT ARRAY BY REF TO PROPERTY
        cx.evaluateString(formScope, "elements.testComponent.setStringArray(['1', '2', '3'])", "Evaluation Test Script", 1, null);
        // CHECK NEW VALUE
        stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.getStringArray()", "Evaluation Test Script", 1, null);
        Assert.assertArrayEquals(new String[] { "1", "2", "3" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.getStringArray().length === 3) && elements.testComponent.getStringArray().every(function(this_i, i) { return this_i == [\"1\", \"2\", \"3\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) FormScope(com.servoy.j2db.scripting.FormScope) Test(org.junit.Test)

Aggregations

IWebFormController (com.servoy.j2db.server.ngclient.IWebFormController)24 Test (org.junit.Test)21 JSONObject (org.json.JSONObject)12 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)10 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)9 StringWriter (java.io.StringWriter)9 JSONWriter (org.json.JSONWriter)9 BrowserConverterContext (org.sablo.specification.property.BrowserConverterContext)9 DataConversion (org.sablo.websocket.utils.DataConversion)8 JSONArray (org.json.JSONArray)4 Container (org.sablo.Container)4 FormScope (com.servoy.j2db.scripting.FormScope)3 Context (org.mozilla.javascript.Context)3 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)2 Form (com.servoy.j2db.persistence.Form)2 RhinoMapOrArrayWrapper (com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper)2 ServoyException (com.servoy.j2db.util.ServoyException)2 JSONException (org.json.JSONException)2 WebComponent (org.sablo.WebComponent)2 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)1