acouchi 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/acouchi/solo.rb CHANGED
@@ -4,20 +4,19 @@ require "json"
4
4
  module Acouchi
5
5
  class Solo
6
6
  MENU = 82
7
+ LEFT = 21
8
+ RIGHT = 22
7
9
 
8
10
  def send_key key
9
- call_method("sendKey", [{:type => "int", :value => key}])
11
+ call_method("sendKey", [key])
10
12
  end
11
13
 
12
14
  def enter_text index, text
13
- call_method("enterText", [
14
- {:type => "int", :value => index},
15
- {:type => "java.lang.String", :value => text}
16
- ])
15
+ call_method("enterText", [index, text])
17
16
  end
18
17
 
19
18
  def clear_edit_text index
20
- call_method("clearEditText", [{:type => "int", :value => index}])
19
+ call_method("clearEditText", [index])
21
20
  end
22
21
 
23
22
  def has_text? text, options={}
@@ -26,12 +25,11 @@ module Acouchi
26
25
  :minimum_matches => 0,
27
26
  :must_be_visible => true
28
27
  }.merge(options)
29
-
30
28
  call_method("searchText", [
31
- {:type => "java.lang.String", :value => text},
32
- {:type => "int", :value => options[:minimum_matches]},
33
- {:type => "boolean", :value => options[:scroll]},
34
- {:type => "boolean", :value => options[:must_be_visible]}
29
+ text,
30
+ options[:minimum_matches],
31
+ options[:scroll],
32
+ options[:must_be_visible]
35
33
  ])
36
34
  end
37
35
 
@@ -52,11 +50,10 @@ module Acouchi
52
50
  :match => 1,
53
51
  :auto_scroll => true
54
52
  }.merge(options)
55
-
56
53
  call_method("clickOnText", [
57
- {:type => "java.lang.String", :value => text},
58
- {:type => "int", :value => options[:match]},
59
- {:type => "boolean", :value => options[:auto_scroll]}
54
+ text,
55
+ options[:match],
56
+ options[:auto_scroll]
60
57
  ])
61
58
  end
62
59
 
@@ -65,9 +62,7 @@ module Acouchi
65
62
  end
66
63
 
67
64
  def click_view id
68
- call_method("clickOnViewById", [
69
- {:type => "int", :value => id},
70
- ])
65
+ call_method("clickOnViewById", [id])
71
66
  end
72
67
 
73
68
  def scroll_up
@@ -79,15 +74,19 @@ module Acouchi
79
74
  end
80
75
 
81
76
  def scroll_up_list index=0
82
- call_method("scrollUpList", [
83
- {:type => "int", :value => index}
84
- ])
77
+ call_method("scrollUpList", [index])
85
78
  end
86
79
 
87
80
  def scroll_down_list index=0
88
- call_method("scrollDownList", [
89
- {:type => "int", :value => index}
90
- ])
81
+ call_method("scrollDownList", [index])
82
+ end
83
+
84
+ def scroll_to_left
85
+ call_method("scrollToSide", [LEFT])
86
+ end
87
+
88
+ def scroll_to_right
89
+ call_method("scrollToSide", [RIGHT])
91
90
  end
92
91
 
93
92
  private
@@ -1,3 +1,3 @@
1
1
  module Acouchi
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  package com.acouchi;
2
+
2
3
  import com.jayway.android.robotium.solo.Solo;
3
4
 
4
5
  import java.util.Properties;
@@ -9,24 +10,11 @@ import java.util.concurrent.locks.Condition;
9
10
  import java.util.concurrent.locks.Lock;
10
11
  import java.util.concurrent.locks.ReentrantLock;
11
12
 
12
- import android.app.Instrumentation;
13
- import android.app.Activity;
14
- import android.widget.TextView;
15
- import android.view.View;
16
- import java.lang.reflect.Method;
17
- import java.lang.reflect.InvocationTargetException;
18
- import org.json.JSONArray;
19
- import org.json.JSONObject;
20
- import org.json.JSONException;
21
- import java.util.ArrayList;
22
- import java.util.Map;
23
- import java.util.HashMap;
24
- import java.util.Collection;
13
+ import com.google.gson.Gson;
14
+
25
15
  import java.io.PrintWriter;
26
16
  import java.io.StringWriter;
27
17
  import java.io.Writer;
28
- import com.google.gson.Gson;
29
- import android.widget.Button;
30
18
 
31
19
  public class Acouchi extends NanoHTTPD
32
20
  {
@@ -71,205 +59,18 @@ public class Acouchi extends NanoHTTPD
71
59
  }
72
60
  else if (uri.startsWith("/execute_method"))
73
61
  {
74
- String methodName = uri.replace("/execute_method/", "");
75
62
  try {
76
- return ExecuteMethod(methodName, params.getProperty("parameters"));
63
+ String methodName = uri.replace("/execute_method/", "");
64
+ String[] parameters = new Gson().fromJson(params.getProperty("parameters"), String[].class);
65
+ String jsonResult = new MethodExecutor(solo, methodName, parameters).Execute();
66
+ return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, jsonResult);
77
67
  } catch (Throwable throwable) {
78
68
  return showException(throwable);
79
69
  }
80
70
  }
81
-
82
71
  return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, "Acouchi");
83
72
  }
84
73
 
85
- private NanoHTTPD.Response ExecuteMethod(String methodName, String json) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, JSONException, ClassNotFoundException
86
- {
87
- if (methodName.equals("getCurrentContent"))
88
- return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, getCurrentContentAsJson());
89
-
90
- JSONArray jsonArray = new JSONArray(json);
91
-
92
- if (methodName.equals("clickOnText")) {
93
- String text = jsonArray.getJSONObject(0).getString("value");
94
- int match = jsonArray.getJSONObject(1).getInt("value");
95
- boolean scroll = jsonArray.getJSONObject(2).getBoolean("value");
96
- try {
97
- solo.clickOnText(text, match, scroll);
98
- } catch (Exception exception) {
99
- return showException(exception);
100
- }
101
- return displayMethodResultAsJson(methodName, null);
102
- } else if (methodName.equals("scrollUpList")) {
103
- int index = jsonArray.getJSONObject(0).getInt("value");
104
- try {
105
- solo.scrollUpList(index);
106
- } catch (Exception exception) {
107
- return showException(exception);
108
- }
109
- return displayMethodResultAsJson(methodName, null);
110
- } else if (methodName.equals("scrollDownList")) {
111
- int index = jsonArray.getJSONObject(0).getInt("value");
112
- try {
113
- solo.scrollDownList(index);
114
- } catch (Exception exception) {
115
- return showException(exception);
116
- }
117
- return displayMethodResultAsJson(methodName, null);
118
- } else {
119
- Class[] parameterTypes = new Class[jsonArray.length()];
120
- Object[] parameters = new Object[jsonArray.length()];
121
-
122
- for (int i = 0; i < jsonArray.length(); i++) {
123
- JSONObject jsonObject = jsonArray.getJSONObject(i);
124
-
125
- parameterTypes[i] = getClassType(jsonObject.getString("type"));
126
- parameters[i] = getConvertedValue(jsonObject.getString("type"), jsonObject.getString("value"));
127
- }
128
- Object result = executeMethodOnSomeClass(methodName, parameterTypes, parameters);
129
- return displayMethodResultAsJson(methodName, result);
130
- }
131
- }
132
-
133
- private class MethodExecutor
134
- {
135
- private Solo solo;
136
- private Object[] parameters;
137
-
138
- public MethodExecutor(Solo solo, Object[] parameters)
139
- {
140
- this.solo = solo;
141
- this.parameters = parameters;
142
- }
143
- }
144
-
145
- private Object executeMethodOnSomeClass(String methodName, Class[] parameterTypes, Object[] parameters) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
146
- {
147
- try {
148
- Method method = solo.getClass().getMethod(methodName, parameterTypes);
149
- return method.invoke(solo, parameters);
150
- } catch (Exception exception) {
151
- }
152
-
153
- Method method = this.getClass().getMethod(methodName, parameterTypes);
154
- return method.invoke(this, parameters);
155
- }
156
-
157
- private NanoHTTPD.Response displayMethodResultAsJson(String methodName, Object result)
158
- {
159
- try {
160
- JSONObject object = new JSONObject();
161
-
162
- if (methodName.equals("getCurrentButtons"))
163
- return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, getButtonsAsJson((ArrayList<Button>)result));
164
-
165
- if (methodName.equals("getViews"))
166
- return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, getViewsAsJson((ArrayList<View>)result));
167
-
168
- if (methodName.equals("getView"))
169
- return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, getViewAsJson((View)result));
170
-
171
- if (result != null)
172
- object.put("result", result);
173
-
174
- return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, object.toString());
175
- } catch (JSONException e) {
176
- return showException(e);
177
- }
178
- }
179
-
180
- public void clickOnViewById(int id)
181
- {
182
- solo.clickOnView(solo.getView(id));
183
- }
184
-
185
- private String getViewsAsJson(ArrayList<View> views)
186
- {
187
- ArrayList<JsonView> jsonViews = new ArrayList<JsonView>();
188
- for(int index = 0; index < views.size(); index++)
189
- jsonViews.add(new JsonView(views.get(index)));
190
- JsonResult result = new JsonResult(jsonViews);
191
- return new Gson().toJson(result);
192
- }
193
-
194
- private String getViewAsJson(View view)
195
- {
196
- JsonResult result = new JsonResult(new JsonView(view));
197
- return new Gson().toJson(result);
198
- }
199
-
200
- private class JsonView
201
- {
202
- private int id;
203
- private String className;
204
-
205
- public JsonView(View view)
206
- {
207
- id = view.getId();
208
- className = view.getClass().toString();
209
- }
210
- }
211
-
212
- private String getButtonsAsJson(ArrayList<Button> buttons)
213
- {
214
- ArrayList<JsonButton> jsonButtons = new ArrayList<JsonButton>();
215
- for(int index = 0; index < buttons.size(); index++)
216
- jsonButtons.add(new JsonButton(buttons.get(index)));
217
- JsonResult result = new JsonResult(jsonButtons);
218
- return new Gson().toJson(result);
219
- }
220
-
221
- private String getCurrentContentAsJson()
222
- {
223
- ArrayList<TextView> currentTextViews = solo.getCurrentTextViews(null);
224
- ArrayList<String> content = new ArrayList<String>();
225
- for (TextView textView: currentTextViews)
226
- content.add(textView.getText().toString());
227
- JsonResult result = new JsonResult(content);
228
- return new Gson().toJson(result);
229
- }
230
-
231
- private class JsonResult
232
- {
233
- private Object result;
234
-
235
- public JsonResult(Object result)
236
- {
237
- this.result = result;
238
- }
239
- }
240
-
241
- private class JsonButton
242
- {
243
- private String text;
244
-
245
- public JsonButton(Button button)
246
- {
247
- text = button.getText().toString();
248
- }
249
- }
250
-
251
- private Class getClassType(String name) throws java.lang.ClassNotFoundException
252
- {
253
- if (name.equals("int")) return int.class;
254
- if (name.equals("long")) return long.class;
255
- if (name.equals("double")) return double.class;
256
- if (name.equals("boolean")) return boolean.class;
257
- return Class.forName(name);
258
- }
259
-
260
- private Object getConvertedValue(String name, String value)
261
- {
262
- if (name.equals("int")) return Integer.parseInt(value);
263
- if (name.equals("long")) return Long.parseLong(value);
264
- if (name.equals("double")) return Double.parseDouble(value);
265
- if (name.equals("boolean")) return Boolean.parseBoolean(value);
266
- if (name.equals("java.lang.Integer")) return Integer.parseInt(value);
267
- if (name.equals("java.lang.Long")) return Long.parseLong(value);
268
- if (name.equals("java.lang.Double")) return Double.parseDouble(value);
269
- if (name.equals("java.lang.Boolean")) return Boolean.parseBoolean(value);
270
- return value;
271
- }
272
-
273
74
  private NanoHTTPD.Response showException(Throwable throwable)
274
75
  {
275
76
  final Writer result = new StringWriter();
@@ -0,0 +1,13 @@
1
+ package com.acouchi;
2
+
3
+ import android.widget.Button;
4
+
5
+ public class JsonButton
6
+ {
7
+ private String text;
8
+
9
+ public JsonButton(Button button)
10
+ {
11
+ text = button.getText().toString();
12
+ }
13
+ }
@@ -0,0 +1,11 @@
1
+ package com.acouchi;
2
+
3
+ public class JsonResult
4
+ {
5
+ private Object result;
6
+
7
+ public JsonResult(Object result)
8
+ {
9
+ this.result = result;
10
+ }
11
+ }
@@ -0,0 +1,15 @@
1
+ package com.acouchi;
2
+
3
+ import android.view.View;
4
+
5
+ public class JsonView
6
+ {
7
+ private int id;
8
+ private String className;
9
+
10
+ public JsonView(View view)
11
+ {
12
+ id = view.getId();
13
+ className = view.getClass().toString();
14
+ }
15
+ }
@@ -0,0 +1,117 @@
1
+ package com.acouchi;
2
+
3
+ import com.jayway.android.robotium.solo.Solo;
4
+
5
+ import java.util.ArrayList;
6
+
7
+ import com.google.gson.Gson;
8
+
9
+ import com.acouchi.JsonResult;
10
+ import com.acouchi.JsonView;
11
+ import com.acouchi.JsonButton;
12
+
13
+ import android.view.View;
14
+ import android.widget.TextView;
15
+ import android.widget.Button;
16
+
17
+ public class MethodExecutor
18
+ {
19
+ private Solo solo;
20
+ private String methodName;
21
+ private String[] parameters;
22
+
23
+ public MethodExecutor(Solo solo, String methodName, String[] parameters)
24
+ {
25
+ this.solo = solo;
26
+ this.methodName = methodName;
27
+ this.parameters = parameters;
28
+ }
29
+
30
+ public String Execute()
31
+ {
32
+ Object result = null;
33
+
34
+ if (methodName.equals("getCurrentContent"))
35
+ result = getCurrentContent();
36
+
37
+ if (methodName.equals("enterText"))
38
+ solo.enterText(Integer.parseInt(parameters[0]), parameters[1]);
39
+
40
+ if (methodName.equals("clickOnText"))
41
+ solo.clickOnText(parameters[0], Integer.parseInt(parameters[1]), Boolean.parseBoolean(parameters[2]));
42
+
43
+ if (methodName.equals("scrollUpList"))
44
+ solo.scrollUpList(Integer.parseInt(parameters[0]));
45
+
46
+ if (methodName.equals("scrollDownList"))
47
+ solo.scrollDownList(Integer.parseInt(parameters[0]));
48
+
49
+ if (methodName.equals("sendKey"))
50
+ solo.sendKey(Integer.parseInt(parameters[0]));
51
+
52
+ if (methodName.equals("clearEditText"))
53
+ solo.clearEditText(Integer.parseInt(parameters[0]));
54
+
55
+ if (methodName.equals("searchText"))
56
+ result = solo.searchText(parameters[0], Integer.parseInt(parameters[1]), Boolean.parseBoolean(parameters[2]), Boolean.parseBoolean(parameters[3]));
57
+
58
+ if (methodName.equals("getCurrentButtons"))
59
+ result = getCurrentButtons();
60
+
61
+ if (methodName.equals("getCurrentContent"))
62
+ result = getCurrentContent();
63
+
64
+ if (methodName.equals("getViews"))
65
+ result = getCurrentViews();
66
+
67
+ if (methodName.equals("clickOnViewById"))
68
+ clickOnViewById(Integer.parseInt(parameters[0]));
69
+
70
+ if (methodName.equals("scrollUp"))
71
+ solo.scrollUp();
72
+
73
+ if (methodName.equals("scrollDown"))
74
+ solo.scrollDown();
75
+
76
+ if (methodName.equals("scrollToSide"))
77
+ solo.scrollToSide(Integer.parseInt(parameters[0]));
78
+
79
+ return methodResultAsJson(result);
80
+ }
81
+
82
+ public void clickOnViewById(int id)
83
+ {
84
+ solo.clickOnView(solo.getView(id));
85
+ }
86
+
87
+ private ArrayList<JsonButton> getCurrentButtons()
88
+ {
89
+ ArrayList<JsonButton> jsonButtons = new ArrayList<JsonButton>();
90
+ for(Button button: solo.getCurrentButtons())
91
+ jsonButtons.add(new JsonButton(button));
92
+ return jsonButtons;
93
+ }
94
+
95
+ private String methodResultAsJson(Object result)
96
+ {
97
+ JsonResult jsonResult = new JsonResult(result);
98
+ return new Gson().toJson(jsonResult);
99
+ }
100
+
101
+ private ArrayList<String> getCurrentContent()
102
+ {
103
+ ArrayList<TextView> currentTextViews = solo.getCurrentTextViews(null);
104
+ ArrayList<String> content = new ArrayList<String>();
105
+ for (TextView textView: currentTextViews)
106
+ content.add(textView.getText().toString());
107
+ return content;
108
+ }
109
+
110
+ private ArrayList<JsonView> getCurrentViews()
111
+ {
112
+ ArrayList<JsonView> jsonViews = new ArrayList<JsonView>();
113
+ for (View view: solo.getViews())
114
+ jsonViews.add(new JsonView(view));
115
+ return jsonViews;
116
+ }
117
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acouchi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-19 00:00:00.000000000 Z
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -106,6 +106,10 @@ files:
106
106
  - lib/acouchi/which.rb
107
107
  - not-yet-implemented.md
108
108
  - src/com/acouchi/Acouchi.java
109
+ - src/com/acouchi/JsonButton.java
110
+ - src/com/acouchi/JsonResult.java
111
+ - src/com/acouchi/JsonView.java
112
+ - src/com/acouchi/MethodExecutor.java
109
113
  - src/com/acouchi/NanoHTTPD.java
110
114
  - src/com/acouchi/TestCase.java
111
115
  homepage: https://github.com/AndrewVos/acouchi