Search in sources :

Example 1 with WebComponent

use of org.sablo.WebComponent in project servoy-client by Servoy.

the class EventCallTest method callButtonWithUUIDName.

@Test
public void callButtonWithUUIDName() {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("test");
    Assert.assertNotNull(form);
    Collection<WebComponent> components = form.getFormUI().getComponents();
    String beanName = null;
    for (WebComponent webComponent : components) {
        String rawName = ((WebFormComponent) webComponent).getFormElement().getRawName();
        if (rawName != null && rawName.equals(uuid)) {
            beanName = webComponent.getName();
            break;
        }
    }
    Assert.assertNotNull(beanName);
    // fake incoming request for button click
    endpoint.incoming("{\"service\":\"formService\",\"methodname\":\"executeEvent\",\"args\":{\"formname\":\"test\",\"beanname\":\"" + beanName + "\",\"event\":\"onActionMethodID\",\"args\":[{\"type\":\"event\",\"eventName\":\"onActionMethodID\",\"modifiers\":0,\"timestamp\":1430912492641,\"x\":362,\"y\":207}],\"changes\":{}},\"cmsgid\":2}", true);
    Object object = form.getFormScope().get("testVar2");
    Assert.assertEquals(uuid, object);
}
Also used : WebComponent(org.sablo.WebComponent) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Test(org.junit.Test)

Example 2 with WebComponent

use of org.sablo.WebComponent in project servoy-client by Servoy.

the class PropertySetTest method setColorProperty.

@Test
public void setColorProperty() throws Exception {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("test");
    Assert.assertNotNull(form);
    Collection<WebComponent> components = form.getFormUI().getComponents();
    Assert.assertEquals(1, components.size());
    WebComponent comp = components.iterator().next();
    Assert.assertEquals(Color.BLACK, comp.getProperty("background"));
    JSONObject json = new JSONObject();
    json.put("formname", "test");
    json.put("beanname", comp.getName());
    JSONObject changes = new JSONObject();
    changes.put("background", "#0000FF");
    json.put("changes", changes);
    FormServiceHandler.INSTANCE.executeMethod("dataPush", json);
    // should be changed.
    Assert.assertEquals(Color.BLUE, comp.getProperty("background"));
    changes.put("background", "#FF0000");
    JSONObject oldvalues = new JSONObject();
    oldvalues.put("background", "#0000FF");
    json.put("oldvalues", oldvalues);
    FormServiceHandler.INSTANCE.executeMethod("dataPush", json);
    // should be changed, old value was really the old value.
    Assert.assertEquals(Color.RED, comp.getProperty("background"));
    changes.put("background", "#00FF00");
    // should not be changed, still RED
    FormServiceHandler.INSTANCE.executeMethod("dataPush", json);
    Assert.assertEquals(Color.RED, comp.getProperty("background"));
}
Also used : WebComponent(org.sablo.WebComponent) JSONObject(org.json.JSONObject) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Test(org.junit.Test)

Example 3 with WebComponent

use of org.sablo.WebComponent in project servoy-client by Servoy.

the class CustomArrayAndObjectPropertyRhinoTest method testCustomTypeJavaBasedRhinoChanges.

@Test
public void testCustomTypeJavaBasedRhinoChanges() throws JSONException {
    Context.enter();
    try {
        WebComponent component = new WebComponent("mycomponent", "testComponentName");
        BrowserConverterContext allowingBrowserConverterContext = new BrowserConverterContext(component, PushToServerEnum.allow);
        PropertyDescription arrayTPD = component.getSpecification().getProperty("arrayT");
        // just some initial checks and setting a java value
        assertNull(component.getProperty("arrayT"));
        List<Map<String, Object>> javaV = new ArrayList<>();
        HashMap<String, Object> hm = new HashMap<String, Object>();
        javaV.add(hm);
        hm.put("text", "Just some text");
        component.setProperty("arrayT", javaV);
        // set should turn it into a change-aware list
        Object wrapped = component.getProperty("arrayT");
        assertTrue(wrapped instanceof ChangeAwareList<?, ?>);
        ChangeAwareList<Map<String, Object>, Map<String, Object>> cal = (ChangeAwareList) wrapped;
        assertEquals("Just some text", cal.get(0).get("text"));
        assertTrue(cal.get(0) instanceof ChangeAwareMap<?, ?>);
        ChangeAwareMap cam = ((ChangeAwareMap<?, ?>) cal.get(0));
        Changes chMap = cam.getChanges();
        ChangeAwareList<Map<String, Object>, Map<String, Object>>.Changes chList = cal.getChanges();
        // TODO I guess this kind of reference changes should be treated in the BaseWebObject directly when we have separate methods for changesToJSON and fullToJSON
        // so for now the change aware things do not report as being changed...
        assertTrue(!chList.mustSendAll());
        assertTrue(!chMap.mustSendAll());
        // still the component has to see them as changed!
        TypedData<Map<String, Object>> changes = component.getAndClearChanges();
        assertTrue(changes.content.get("arrayT") != null);
        assertTrue(changes.contentType.getProperty("arrayT").getType() instanceof CustomJSONArrayType);
        Object arrayCh = changes.content.get("arrayT");
        assertTrue(arrayCh != null);
        assertTrue(changes.contentType.getProperty("arrayT").getType() instanceof CustomJSONArrayType);
        Object mapCh = ((ChangeAwareList) arrayCh).get(0);
        assertTrue(mapCh != null);
        assertTrue(((CustomJSONPropertyType<?>) changes.contentType.getProperty("arrayT").getType()).getCustomJSONTypeDefinition().getType() instanceof CustomJSONObjectType);
        JSONUtils.writeDataWithConversions(changes.content, changes.contentType, allowingBrowserConverterContext);
        // ok now that we called component.getChanges() no changes should be present any more
        assertTrue(!chList.mustSendAll());
        assertTrue(!chMap.mustSendAll());
        assertEquals(0, component.getAndClearChanges().content.size());
        assertFalse(chList.getGranularUpdatesKeeper().hasChanges());
        // check changing java => change reflected in Rhino
        ScriptableObject topLevel = new ScriptableObject() {

            @Override
            public String getClassName() {
                return "test_top_level_scope";
            }
        };
        Scriptable rhinoVal = (Scriptable) NGConversions.INSTANCE.convertSabloComponentToRhinoValue(component.getProperty("arrayT"), arrayTPD, component, topLevel);
        assertEquals("Just some text", ((Scriptable) rhinoVal.get(0, rhinoVal)).get("text", rhinoVal));
        cam.put("text", "Just some text 2");
        assertEquals(1, chMap.getKeysChangedByRef().size());
        assertEquals("text", chMap.getKeysChangedByRef().iterator().next());
        ArrayOperation[] opSeq = chList.getGranularUpdatesKeeper().getEquivalentSequenceOfOperations();
        assertEquals(1, opSeq.length);
        assertGranularOpIs(0, 0, ArrayOperation.CHANGE, ChangeAwareList.GRANULAR_UPDATE_OP, opSeq[0]);
        assertTrue(!chList.mustSendAll());
        assertTrue(!chMap.mustSendAll());
        assertEquals("Just some text 2", ((Scriptable) rhinoVal.get(0, rhinoVal)).get("text", rhinoVal));
        cam.put("active", new ArrayList());
        assertTrue(!chList.mustSendAll());
        assertTrue(!chMap.mustSendAll());
        assertEquals(2, chMap.getKeysChangedByRef().size());
        assertTrue(chMap.getKeysChangedByRef().contains("text"));
        assertTrue(chMap.getKeysChangedByRef().contains("active"));
        cam.remove("active");
        assertTrue(!chList.mustSendAll());
        assertTrue(chMap.mustSendAll());
        cal.add(new HashMap<String, Object>());
        ChangeAwareMap cam1 = ((ChangeAwareMap<?, ?>) cal.get(1));
        assertFalse(chList.mustSendAll());
        opSeq = chList.getGranularUpdatesKeeper().getEquivalentSequenceOfOperations();
        assertEquals(2, opSeq.length);
        assertGranularOpIs(0, 0, ArrayOperation.CHANGE, ChangeAwareList.GRANULAR_UPDATE_OP, opSeq[0]);
        assertGranularOpIs(1, 1, ArrayOperation.INSERT, null, opSeq[1]);
        assertTrue(chMap.mustSendAll());
        // ok clear changes
        changes = component.getAndClearChanges();
        JSONUtils.writeDataWithConversions(changes.content, changes.contentType, allowingBrowserConverterContext);
        assertEquals(1, changes.content.size());
        assertEquals(0, component.getAndClearChanges().content.size());
        assertTrue(!chList.mustSendAll());
        assertTrue(!chMap.mustSendAll());
        assertFalse(chList.getGranularUpdatesKeeper().hasChanges());
        assertEquals(0, chMap.getKeysChangedByRef().size());
        assertEquals(0, chMap.getKeysWithUpdates().size());
        // assign some native values
        Scriptable oneO = Context.getCurrentContext().newObject(topLevel);
        Scriptable activeA1 = Context.getCurrentContext().newArray(topLevel, 0);
        Scriptable activeA1Obj = Context.getCurrentContext().newObject(topLevel);
        rhinoVal.put(0, rhinoVal, oneO);
        // same as 'cam' but in it's Rhino representation
        Scriptable oneOScriptable = (Scriptable) rhinoVal.get(0, rhinoVal);
        assertTrue(!chList.mustSendAll());
        opSeq = chList.getGranularUpdatesKeeper().getEquivalentSequenceOfOperations();
        assertEquals(1, opSeq.length);
        assertGranularOpIs(0, 0, ArrayOperation.CHANGE, null, opSeq[0]);
        cam = ((ChangeAwareMap<?, ?>) cal.get(0));
        chMap = cam.getChanges();
        activeA1Obj.put("field", activeA1Obj, 11);
        activeA1.put(0, activeA1, activeA1Obj);
        oneOScriptable.put("active", oneOScriptable, activeA1);
        assertEquals(11, ((Map) ((List) ((Map) cal.get(0)).get("active")).get(0)).get("field"));
        ((Map) ((List) ((Map) cal.get(0)).get("active")).get(0)).put("percent", 0.22);
        assertEquals(1, chMap.getKeysChangedByRef().size());
        assertEquals(0, chMap.getKeysWithUpdates().size());
        assertTrue(chMap.getKeysChangedByRef().contains("active"));
        assertTrue(!chList.mustSendAll());
        opSeq = chList.getGranularUpdatesKeeper().getEquivalentSequenceOfOperations();
        assertEquals(1, opSeq.length);
        assertGranularOpIs(0, 0, ArrayOperation.CHANGE, null, opSeq[0]);
        // now change the native values using initial ref to see if it changed in java; this is no longer supported after case SVY-11027
        // activeA1Obj.put("field", activeA1Obj, 98);
        // assertEquals(98, ((Map)((List)((Map)cal.get(0)).get("active")).get(0)).get("field"));
        // activeA1.put(1, activeA1, activeA2Obj);
        // activeA2Obj.put("field", activeA2Obj, 45);
        // assertEquals(45, ((Map)((List)((Map)cal.get(0)).get("active")).get(1)).get("field"));
        changes = component.getAndClearChanges();
        String msg = JSONUtils.writeChangesWithConversions(changes.content, changes.contentType, allowingBrowserConverterContext);
        JSONAssert.assertEquals("{\"arrayT\":{\"vEr\":3,\"g\":[{\"op\":[0,0,0],\"d\":[{\"rt\":\"mycomponent.mytype007\",\"vEr\":5,\"v\":{\"active\":{\"vEr\":2,\"v\":[{\"rt\":\"mycomponent.activeType\",\"vEr\":2,\"v\":{\"field\":11,\"percent\":0.22}}],\"svy_types\":{\"0\":\"JSON_obj\"}}},\"svy_types\":{\"active\":\"JSON_arr\"}}],\"svy_types\":{\"0\":\"JSON_obj\"}}]},\"svy_types\":{\"arrayT\":\"JSON_arr\"}}", msg, JSONCompareMode.NON_EXTENSIBLE);
        ((Map) ((List) ((Map) cal.get(0)).get("active")).get(0)).put("percent", 0.33);
        assertEquals(0, chMap.getKeysChangedByRef().size());
        assertEquals(1, chMap.getKeysWithUpdates().size());
        changes = component.getAndClearChanges();
        msg = JSONUtils.writeChangesWithConversions(changes.content, changes.contentType, allowingBrowserConverterContext);
        JSONAssert.assertEquals("{\"arrayT\":{\"vEr\":3,\"g\":[{\"op\":[0,0,0],\"d\":[{\"rt\":\"mycomponent.mytype007\",\"vEr\":5,\"u\":[{\"k\":\"active\",\"v\":{\"vEr\":2,\"g\":[{\"op\":[0,0,0],\"d\":[{\"rt\":\"mycomponent.activeType\",\"vEr\":2,\"u\":[{\"k\":\"percent\",\"v\":0.33}]}],\"svy_types\":{\"0\":\"JSON_obj\"}}]}}],\"svy_types\":{\"0\":{\"v\":\"JSON_arr\"}}}],\"svy_types\":{\"0\":\"JSON_obj\"}}]},\"svy_types\":{\"arrayT\":\"JSON_arr\"}}", msg, JSONCompareMode.NON_EXTENSIBLE);
        ((List) ((Map) cal.get(0)).get("active")).add(new HashMap<String, Object>());
        ((Map) ((List) ((Map) cal.get(0)).get("active")).get(1)).put("percent", 0.99);
        component.getAndClearChanges();
        // now simulate another request cycle that makes some change to the property from javascript
        rhinoVal = (Scriptable) NGConversions.INSTANCE.convertSabloComponentToRhinoValue(component.getProperty("arrayT"), arrayTPD, component, topLevel);
        Scriptable v = ((Scriptable) rhinoVal.get(0, rhinoVal));
        v = (Scriptable) v.get("active", v);
        v = (Scriptable) v.get(1, v);
        assertEquals(0.99, v.get("percent", v));
        v.put("percent", v, 0.56);
        assertEquals(0.56, ((Map) ((List) ((Map) cal.get(0)).get("active")).get(1)).get("percent"));
        assertTrue(!chMap.mustSendAll());
        assertTrue(!chList.mustSendAll());
        opSeq = chList.getGranularUpdatesKeeper().getEquivalentSequenceOfOperations();
        assertEquals(1, opSeq.length);
        assertGranularOpIs(0, 0, ArrayOperation.CHANGE, ChangeAwareList.GRANULAR_UPDATE_OP, opSeq[0]);
        assertEquals(1, chMap.getKeysWithUpdates().size());
        assertEquals("active", chMap.getKeysWithUpdates().iterator().next());
    } finally {
        Context.exit();
    }
}
Also used : ArrayOperation(org.sablo.specification.property.ArrayOperation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WebComponent(org.sablo.WebComponent) CustomJSONObjectType(org.sablo.specification.property.CustomJSONObjectType) CustomJSONArrayType(org.sablo.specification.property.CustomJSONArrayType) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) ChangeAwareMap(org.sablo.specification.property.ChangeAwareMap) ArrayList(java.util.ArrayList) List(java.util.List) ChangeAwareList(org.sablo.specification.property.ChangeAwareList) Changes(org.sablo.specification.property.ChangeAwareMap.Changes) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) ChangeAwareList(org.sablo.specification.property.ChangeAwareList) PropertyDescription(org.sablo.specification.PropertyDescription) ScriptableObject(org.mozilla.javascript.ScriptableObject) HashMap(java.util.HashMap) Map(java.util.Map) ChangeAwareMap(org.sablo.specification.property.ChangeAwareMap) Test(org.junit.Test)

Example 4 with WebComponent

use of org.sablo.WebComponent in project servoy-client by Servoy.

the class CustomArrayAndObjectPropertyRhinoTest method seeThatNormalArraysAndCustomObjectsKeepNullsAsExpected.

@Test
public void seeThatNormalArraysAndCustomObjectsKeepNullsAsExpected() throws JSONException {
    WebComponent component = new WebComponent("mycomponent", "testComponentName");
    PropertyDescription normalArrayPD = component.getSpecification().getProperty("normalArray");
    checkNormalArraysAndCustomObjectsWithNullValues(component, normalArrayPD);
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) WebComponent(org.sablo.WebComponent) Test(org.junit.Test)

Example 5 with WebComponent

use of org.sablo.WebComponent in project servoy-client by Servoy.

the class CustomArrayAndObjectPropertyRhinoTest method seeThatNormalArraysAndCustomObjectsWithIrrelevantConfigKeepNullsAsExpected.

@Test
public void seeThatNormalArraysAndCustomObjectsWithIrrelevantConfigKeepNullsAsExpected() throws JSONException {
    WebComponent component = new WebComponent("mycomponent", "testComponentName");
    PropertyDescription normalArrayPD = component.getSpecification().getProperty("normalArrayWithConfig");
    checkNormalArraysAndCustomObjectsWithNullValues(component, normalArrayPD);
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) WebComponent(org.sablo.WebComponent) Test(org.junit.Test)

Aggregations

WebComponent (org.sablo.WebComponent)20 ArrayList (java.util.ArrayList)7 PropertyDescription (org.sablo.specification.PropertyDescription)7 Test (org.junit.Test)6 RuntimeWebComponent (com.servoy.j2db.server.ngclient.component.RuntimeWebComponent)5 JSONObject (org.json.JSONObject)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)3 IFormElement (com.servoy.j2db.persistence.IFormElement)2 IWebFormController (com.servoy.j2db.server.ngclient.IWebFormController)2 ReadonlySabloValue (com.servoy.j2db.server.ngclient.property.types.ReadonlySabloValue)2 TreeMap (java.util.TreeMap)2 ScriptableObject (org.mozilla.javascript.ScriptableObject)2 WebObjectFunctionDefinition (org.sablo.specification.WebObjectFunctionDefinition)2 ChangeAwareList (org.sablo.specification.property.ChangeAwareList)2 ChangeAwareMap (org.sablo.specification.property.ChangeAwareMap)2 BasicFormController (com.servoy.j2db.BasicFormController)1 IFormController (com.servoy.j2db.IFormController)1