src/com/acouchi/Acouchi.java in acouchi-0.0.5 vs src/com/acouchi/Acouchi.java in acouchi-0.0.6
- old
+ new
@@ -1,34 +1,22 @@
package com.acouchi;
+
import com.jayway.android.robotium.solo.Solo;
import java.util.Properties;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
-import android.app.Instrumentation;
-import android.app.Activity;
-import android.widget.TextView;
-import android.view.View;
-import java.lang.reflect.Method;
-import java.lang.reflect.InvocationTargetException;
-import org.json.JSONArray;
-import org.json.JSONObject;
-import org.json.JSONException;
-import java.util.ArrayList;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.Collection;
+import com.google.gson.Gson;
+
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
-import com.google.gson.Gson;
-import android.widget.Button;
public class Acouchi extends NanoHTTPD
{
private Solo solo;
private boolean serverRunning = true;
@@ -69,206 +57,19 @@
}
return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, "");
}
else if (uri.startsWith("/execute_method"))
{
- String methodName = uri.replace("/execute_method/", "");
try {
- return ExecuteMethod(methodName, params.getProperty("parameters"));
+ String methodName = uri.replace("/execute_method/", "");
+ String[] parameters = new Gson().fromJson(params.getProperty("parameters"), String[].class);
+ String jsonResult = new MethodExecutor(solo, methodName, parameters).Execute();
+ return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, jsonResult);
} catch (Throwable throwable) {
return showException(throwable);
}
}
-
return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, "Acouchi");
- }
-
- private NanoHTTPD.Response ExecuteMethod(String methodName, String json) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, JSONException, ClassNotFoundException
- {
- if (methodName.equals("getCurrentContent"))
- return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, getCurrentContentAsJson());
-
- JSONArray jsonArray = new JSONArray(json);
-
- if (methodName.equals("clickOnText")) {
- String text = jsonArray.getJSONObject(0).getString("value");
- int match = jsonArray.getJSONObject(1).getInt("value");
- boolean scroll = jsonArray.getJSONObject(2).getBoolean("value");
- try {
- solo.clickOnText(text, match, scroll);
- } catch (Exception exception) {
- return showException(exception);
- }
- return displayMethodResultAsJson(methodName, null);
- } else if (methodName.equals("scrollUpList")) {
- int index = jsonArray.getJSONObject(0).getInt("value");
- try {
- solo.scrollUpList(index);
- } catch (Exception exception) {
- return showException(exception);
- }
- return displayMethodResultAsJson(methodName, null);
- } else if (methodName.equals("scrollDownList")) {
- int index = jsonArray.getJSONObject(0).getInt("value");
- try {
- solo.scrollDownList(index);
- } catch (Exception exception) {
- return showException(exception);
- }
- return displayMethodResultAsJson(methodName, null);
- } else {
- Class[] parameterTypes = new Class[jsonArray.length()];
- Object[] parameters = new Object[jsonArray.length()];
-
- for (int i = 0; i < jsonArray.length(); i++) {
- JSONObject jsonObject = jsonArray.getJSONObject(i);
-
- parameterTypes[i] = getClassType(jsonObject.getString("type"));
- parameters[i] = getConvertedValue(jsonObject.getString("type"), jsonObject.getString("value"));
- }
- Object result = executeMethodOnSomeClass(methodName, parameterTypes, parameters);
- return displayMethodResultAsJson(methodName, result);
- }
- }
-
- private class MethodExecutor
- {
- private Solo solo;
- private Object[] parameters;
-
- public MethodExecutor(Solo solo, Object[] parameters)
- {
- this.solo = solo;
- this.parameters = parameters;
- }
- }
-
- private Object executeMethodOnSomeClass(String methodName, Class[] parameterTypes, Object[] parameters) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
- {
- try {
- Method method = solo.getClass().getMethod(methodName, parameterTypes);
- return method.invoke(solo, parameters);
- } catch (Exception exception) {
- }
-
- Method method = this.getClass().getMethod(methodName, parameterTypes);
- return method.invoke(this, parameters);
- }
-
- private NanoHTTPD.Response displayMethodResultAsJson(String methodName, Object result)
- {
- try {
- JSONObject object = new JSONObject();
-
- if (methodName.equals("getCurrentButtons"))
- return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, getButtonsAsJson((ArrayList<Button>)result));
-
- if (methodName.equals("getViews"))
- return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, getViewsAsJson((ArrayList<View>)result));
-
- if (methodName.equals("getView"))
- return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, getViewAsJson((View)result));
-
- if (result != null)
- object.put("result", result);
-
- return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, object.toString());
- } catch (JSONException e) {
- return showException(e);
- }
- }
-
- public void clickOnViewById(int id)
- {
- solo.clickOnView(solo.getView(id));
- }
-
- private String getViewsAsJson(ArrayList<View> views)
- {
- ArrayList<JsonView> jsonViews = new ArrayList<JsonView>();
- for(int index = 0; index < views.size(); index++)
- jsonViews.add(new JsonView(views.get(index)));
- JsonResult result = new JsonResult(jsonViews);
- return new Gson().toJson(result);
- }
-
- private String getViewAsJson(View view)
- {
- JsonResult result = new JsonResult(new JsonView(view));
- return new Gson().toJson(result);
- }
-
- private class JsonView
- {
- private int id;
- private String className;
-
- public JsonView(View view)
- {
- id = view.getId();
- className = view.getClass().toString();
- }
- }
-
- private String getButtonsAsJson(ArrayList<Button> buttons)
- {
- ArrayList<JsonButton> jsonButtons = new ArrayList<JsonButton>();
- for(int index = 0; index < buttons.size(); index++)
- jsonButtons.add(new JsonButton(buttons.get(index)));
- JsonResult result = new JsonResult(jsonButtons);
- return new Gson().toJson(result);
- }
-
- private String getCurrentContentAsJson()
- {
- ArrayList<TextView> currentTextViews = solo.getCurrentTextViews(null);
- ArrayList<String> content = new ArrayList<String>();
- for (TextView textView: currentTextViews)
- content.add(textView.getText().toString());
- JsonResult result = new JsonResult(content);
- return new Gson().toJson(result);
- }
-
- private class JsonResult
- {
- private Object result;
-
- public JsonResult(Object result)
- {
- this.result = result;
- }
- }
-
- private class JsonButton
- {
- private String text;
-
- public JsonButton(Button button)
- {
- text = button.getText().toString();
- }
- }
-
- private Class getClassType(String name) throws java.lang.ClassNotFoundException
- {
- if (name.equals("int")) return int.class;
- if (name.equals("long")) return long.class;
- if (name.equals("double")) return double.class;
- if (name.equals("boolean")) return boolean.class;
- return Class.forName(name);
- }
-
- private Object getConvertedValue(String name, String value)
- {
- if (name.equals("int")) return Integer.parseInt(value);
- if (name.equals("long")) return Long.parseLong(value);
- if (name.equals("double")) return Double.parseDouble(value);
- if (name.equals("boolean")) return Boolean.parseBoolean(value);
- if (name.equals("java.lang.Integer")) return Integer.parseInt(value);
- if (name.equals("java.lang.Long")) return Long.parseLong(value);
- if (name.equals("java.lang.Double")) return Double.parseDouble(value);
- if (name.equals("java.lang.Boolean")) return Boolean.parseBoolean(value);
- return value;
}
private NanoHTTPD.Response showException(Throwable throwable)
{
final Writer result = new StringWriter();