public void testStreamArray() 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);
    writer.appendPayload(
        WidgetUtil.getId(shell),
        IProtocolConstants.PAYLOAD_CONSTRUCT,
        "key",
        new Integer[] {new Integer(1), new Integer(2)});
    writer.appendPayload(
        WidgetUtil.getId(shell), IProtocolConstants.PAYLOAD_CONSTRUCT, "key2", new Boolean(true));

    writer.finish();
    String actual = stringWriter.getBuffer().toString();
    JSONObject message = new JSONObject(actual);
    JSONArray widgetArray = message.getJSONArray(IProtocolConstants.MESSAGE_WIDGETS);
    JSONObject widgetObject = widgetArray.getJSONObject(0);
    JSONObject payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
    JSONArray array = payload.getJSONArray("key");
    assertEquals(1, array.getInt(0));
    assertEquals(2, array.getInt(1));
    assertTrue(payload.getBoolean("key2"));
  }
  public void testStreamWithBlock() 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);
    writer.appendPayload(
        WidgetUtil.getId(shell), IProtocolConstants.PAYLOAD_CONSTRUCT, "key", "value");
    writer.addConstructPayload(
        WidgetUtil.getId(button),
        WidgetUtil.getId(button.getParent()),
        button.getClass().getName(),
        new String[] {"PUSH"},
        null);
    writer.appendPayload(
        WidgetUtil.getId(button), IProtocolConstants.PAYLOAD_SYNCHRONIZE, "key", "value");
    writer.appendPayload(
        WidgetUtil.getId(button), IProtocolConstants.PAYLOAD_SYNCHRONIZE, "key2", "value");

    writer.finish();
    String actual = stringWriter.getBuffer().toString();
    JSONObject message = new JSONObject(actual);
    JSONArray widgetArray = message.getJSONArray(IProtocolConstants.MESSAGE_WIDGETS);
    JSONObject widgetObject = widgetArray.getJSONObject(1);
    JSONObject payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
    String parentId = payload.getString(IProtocolConstants.KEY_PARENT_ID);
    assertEquals(WidgetUtil.getId(shell), parentId);
    widgetObject = widgetArray.getJSONObject(2);
    payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
    assertEquals("value", payload.getString("key"));
    assertEquals("value", payload.getString("key2"));
  }
 public void testMessageWithFireEvent() 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);
   writer.addFireEventPayload(WidgetUtil.getId(button), "selection");
   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_FIRE_EVENT, type);
   String actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID);
   assertEquals(widgetId, actualId);
   JSONObject payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
   String actualEvent = payload.getString(IProtocolConstants.KEY_EVENT);
   assertEquals("selection", actualEvent);
   writer.addFireEventPayload(WidgetUtil.getId(button), "focus");
   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_FIRE_EVENT, type);
   actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID);
   assertEquals(widgetId, actualId);
   payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
   actualEvent = payload.getString(IProtocolConstants.KEY_EVENT);
   assertEquals("focus", actualEvent);
 }
 public void testMessageWithDestroy() 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);
   writer.addDestroyPayload(WidgetUtil.getId(button));
   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_DESTROY, type);
   String actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID);
   assertEquals(widgetId, actualId);
   Object payload = widgetObject.get(IProtocolConstants.WIDGETS_PAYLOAD);
   assertEquals(JSONObject.NULL, payload);
   writer.addDestroyPayload(WidgetUtil.getId(shell));
   writer.finish();
   String shellId = WidgetUtil.getId(shell);
   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_DESTROY, type);
   actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID);
   assertEquals(shellId, actualId);
   payload = widgetObject.get(IProtocolConstants.WIDGETS_PAYLOAD);
   assertEquals(JSONObject.NULL, payload);
 }
 public void testEmptyMessage() throws IOException {
   StringWriter stringWriter = new StringWriter();
   PrintWriter printWriter = new PrintWriter(stringWriter);
   ProtocolMessageWriter writer = new JsonMessageWriter(printWriter);
   writer.finish();
   String actual = stringWriter.getBuffer().toString();
   try {
     JSONObject jsonObject = new JSONObject(actual);
     JSONObject meta = jsonObject.getJSONObject(IProtocolConstants.MESSAGE_META);
     int counter = meta.getInt(IProtocolConstants.META_REQUEST_COUNTER);
     assertEquals(getRequestCounter(), counter);
     JSONArray widgets = jsonObject.getJSONArray(IProtocolConstants.MESSAGE_WIDGETS);
     assertEquals(0, widgets.length());
   } catch (JSONException e) {
     fail(e.getMessage());
   }
 }
 public void testMessageWithExecuteScript() 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);
   String script = "var c = 4; c++;";
   String scriptType = "text/javascript";
   writer.addExecuteScript(WidgetUtil.getId(button), scriptType, script);
   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_EXECUTE_SCRIPT, type);
   String actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID);
   assertEquals(widgetId, actualId);
   JSONObject payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
   String actualType = payload.getString(IProtocolConstants.KEY_SCRIPT_TYPE);
   assertEquals(scriptType, actualType);
   String actualScript = payload.getString(IProtocolConstants.KEY_SCRIPT);
   assertEquals(script, actualScript);
   scriptType = "text/vb";
   script = "really bad VB;";
   writer.addExecuteScript(WidgetUtil.getId(shell), scriptType, script);
   writer.finish();
   widgetId = WidgetUtil.getId(shell);
   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_EXECUTE_SCRIPT, type);
   actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID);
   assertEquals(widgetId, actualId);
   payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
   actualType = payload.getString(IProtocolConstants.KEY_SCRIPT_TYPE);
   assertEquals(scriptType, actualType);
   actualScript = payload.getString(IProtocolConstants.KEY_SCRIPT);
   assertEquals(script, actualScript);
 }
 public void testMessageWithExecute() throws IOException, JSONException {
   StringWriter stringWriter = new StringWriter();
   PrintWriter printWriter = new PrintWriter(stringWriter);
   ProtocolMessageWriter writer = new JsonMessageWriter(printWriter);
   String methodName = "methodName";
   Display display = new Display();
   Shell shell = new Shell(display);
   writer.addExecutePayload(WidgetUtil.getId(shell), methodName, new Object[] {"a", "b"});
   String widgetId = WidgetUtil.getId(shell);
   String actual = stringWriter.getBuffer().toString();
   JSONObject message = new JSONObject(actual + "]}");
   JSONArray widgets = message.getJSONArray(IProtocolConstants.MESSAGE_WIDGETS);
   JSONObject widgetObject = widgets.getJSONObject(0);
   String actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID);
   assertEquals(widgetId, actualId);
   String type = widgetObject.getString(IProtocolConstants.WIDGETS_TYPE);
   assertEquals(IProtocolConstants.PAYLOAD_EXECUTE, type);
   JSONObject payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
   String actualMethod = payload.getString(IProtocolConstants.KEY_METHODNAME);
   assertEquals(methodName, actualMethod);
   JSONArray actualParams = payload.getJSONArray(IProtocolConstants.KEY_PARAMETER_LIST);
   assertEquals("a", actualParams.getString(0));
   assertEquals("b", actualParams.getString(1));
   Object[] array = new Object[] {new Integer(5), "b", new Boolean(false)};
   writer.addExecutePayload(WidgetUtil.getId(shell), methodName, array);
   writer.finish();
   actual = stringWriter.getBuffer().toString();
   message = new JSONObject(actual);
   JSONArray widgetArray = message.getJSONArray(IProtocolConstants.MESSAGE_WIDGETS);
   widgetObject = widgetArray.getJSONObject(1);
   type = widgetObject.getString(IProtocolConstants.WIDGETS_TYPE);
   assertEquals(IProtocolConstants.PAYLOAD_EXECUTE, type);
   actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID);
   assertEquals(widgetId, actualId);
   payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
   actualMethod = payload.getString(IProtocolConstants.KEY_METHODNAME);
   assertEquals(methodName, actualMethod);
   JSONArray params = payload.getJSONArray(IProtocolConstants.KEY_PARAMETER_LIST);
   assertEquals(5, params.getInt(0));
   assertEquals("b", params.getString(1));
   assertFalse(params.getBoolean(2));
 }
 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"));
 }
 public void testMessageWithMixedPayloads() 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);
   createShellPayload(shell, writer);
   createButtonPayload(button, writer);
   writer.finish();
   String shellId = WidgetUtil.getId(shell);
   String buttonId = WidgetUtil.getId(button);
   String actual = stringWriter.getBuffer().toString();
   JSONObject message = new JSONObject(actual);
   checkShellConstruct(shell, message);
   checkShellSynchronize(shellId, message);
   checkShellListen(shellId, message);
   checkButtonConstruct(button, shellId, buttonId, message);
   checkButtonExecute(buttonId, message);
 }
 public void testMessageWithConstruct() 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);
   writer.addConstructPayload(
       WidgetUtil.getId(shell),
       DisplayUtil.getId(display),
       "org.Text",
       new String[] {"TRIM"},
       new Object[] {"a", "b"});
   String parentId = "";
   if (shell.getParent() != null) {
     parentId = WidgetUtil.getId(shell.getParent());
   } else {
     parentId = DisplayUtil.getId(display);
   }
   String widgetId = WidgetUtil.getId(shell);
   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_CONSTRUCT, type);
   String actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID);
   assertEquals(widgetId, actualId);
   JSONObject payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
   String actualParentId = payload.getString(IProtocolConstants.KEY_PARENT_ID);
   assertEquals(parentId, actualParentId);
   String actualType = payload.getString(IProtocolConstants.KEY_WIDGET_TYPE);
   assertEquals("org.Text", actualType);
   JSONArray actualStyle = payload.getJSONArray(IProtocolConstants.KEY_WIDGET_STYLE);
   assertEquals("TRIM", actualStyle.getString(0));
   JSONArray actualParams = payload.getJSONArray(IProtocolConstants.KEY_PARAMETER_LIST);
   assertEquals("a", actualParams.getString(0));
   assertEquals("b", actualParams.getString(1));
   writer.addConstructPayload(
       WidgetUtil.getId(button),
       WidgetUtil.getId(shell),
       "org.Shell",
       new String[] {"PUSH", "BORDER"},
       new Object[] {"a", new Boolean(true)});
   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_CONSTRUCT, type);
   actualId = widgetObject.getString(IProtocolConstants.WIDGETS_ID);
   assertEquals(WidgetUtil.getId(button), actualId);
   payload = widgetObject.getJSONObject(IProtocolConstants.WIDGETS_PAYLOAD);
   JSONArray styles = payload.getJSONArray(IProtocolConstants.KEY_WIDGET_STYLE);
   assertEquals("PUSH", styles.getString(0));
   assertEquals("BORDER", styles.getString(1));
   String actualParent = payload.getString(IProtocolConstants.KEY_PARENT_ID);
   assertEquals(WidgetUtil.getId(shell), actualParent);
   JSONArray params = payload.getJSONArray(IProtocolConstants.KEY_PARAMETER_LIST);
   assertTrue(params.getBoolean(1));
 }