private void addShellSynchronize(final Shell shell, final ProtocolMessageWriter writer) { Map properties = new HashMap(); for (int i = 0; i < 5; i++) { properties.put("key" + i, "value" + i); } writer.addSychronizePayload(WidgetUtil.getId(shell), properties); }
public void testMessageWithSynchronize() throws IOException, JSONException { StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); ProtocolMessageWriter writer = new JsonMessageWriter(printWriter); Display display = new Display(); Shell shell = new Shell(display); Button button = new Button(shell, SWT.PUSH); Map properties = new HashMap(); properties.put("text", "newText"); properties.put("image", "aUrl"); properties.put("fake", new Integer(1)); writer.addSychronizePayload(WidgetUtil.getId(button), properties); String widgetId = WidgetUtil.getId(button); String actual = stringWriter.getBuffer().toString(); JSONObject message = new JSONObject(actual + "]}"); JSONArray widgetArray = message.getJSONArray(IProtocolConstants.MESSAGE_WIDGETS); JSONObject widgetObject = widgetArray.getJSONObject(0); String type = widgetObject.getString(IProtocolConstants.WIDGETS_TYPE); assertEquals(IProtocolConstants.PAYLOAD_SYNCHRONIZE, type); String actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID); assertEquals(widgetId, actualId); JSONObject payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD); assertEquals("newText", payload.getString("text")); assertEquals("aUrl", payload.getString("image")); assertEquals(1, payload.getInt("fake")); properties.remove("image"); properties.put("state", new Boolean(true)); writer.addSychronizePayload(WidgetUtil.getId(button), properties); writer.finish(); actual = stringWriter.getBuffer().toString(); message = new JSONObject(actual); widgetArray = message.getJSONArray(IProtocolConstants.MESSAGE_WIDGETS); widgetObject = widgetArray.getJSONObject(1); type = widgetObject.getString(IProtocolConstants.WIDGETS_TYPE); assertEquals(IProtocolConstants.PAYLOAD_SYNCHRONIZE, type); actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID); assertEquals(widgetId, actualId); payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD); assertEquals("newText", payload.getString("text")); assertEquals(1, payload.getInt("fake")); assertTrue(payload.getBoolean("state")); }