1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.mobile.tests;
18
19 import java.io.UnsupportedEncodingException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.http.ProtocolVersion;
24 import org.apache.http.entity.StringEntity;
25 import org.apache.http.message.BasicHttpResponse;
26 import org.exoplatform.model.ExoAccount;
27 import org.exoplatform.singleton.AccountSetting;
28 import org.exoplatform.singleton.ServerSettingHelper;
29 import org.exoplatform.utils.ServerConfigurationUtils;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.robolectric.annotation.Config;
33 import org.robolectric.shadows.ShadowLog;
34 import org.robolectric.shadows.httpclient.FakeHttpLayer.RequestMatcherBuilder;
35 import org.robolectric.shadows.httpclient.RequestMatcher;
36 import org.robolectric.util.ActivityController;
37
38 import android.app.Activity;
39 import android.content.Context;
40 import android.content.Intent;
41 import android.content.SharedPreferences;
42 import android.os.Build;
43 import android.os.Bundle;
44 import android.util.Log;
45
46
47
48
49
50 @Config(sdk=Build.VERSION_CODES.LOLLIPOP)
51 public abstract class ExoActivityTestUtils<A extends Activity> {
52
53 final String TAG_TEST = "eXo____Test____";
54
55 final String TEST_SERVER_NAME = "testserver";
56
57 final String TEST_SERVER_URL = "http://www.test.com";
58
59 final String TEST_HTTPS_SERVER_URL = "https://www.test.com";
60
61 final String TEST_WRONG_SERVER_URL = "test&%$'{}";
62
63 final String TEST_USER_NAME = "testuser";
64
65 final String TEST_USER_PWD = "testpwd";
66
67 final String TEST_USER_IDENTITY = "testidentityid";
68
69 final int REQ_SOCIAL_VERSION_LATEST = 0;
70
71 final String RESP_SOCIAL_VERSION_LATEST = "{\"version\":\"v1-alpha3\"}";
72
73 final int REQ_SOCIAL_IDENTITY = 1;
74
75 final int REQ_SOCIAL_IDENTITY_2 = 2;
76
77 final String RESP_SOCIAL_IDENTITY = "{" + "\"id\":\"" + TEST_USER_IDENTITY + "\","
78 + "\"providerId\":\"organization\"," + "\"remoteId\":\"" + TEST_USER_NAME + "\"," + "\"profile\":{\"avatarUrl\":\"\","
79 + "\"fullName\":\"" + TEST_USER_NAME + "\"}" + "}";
80
81 final int REQ_SOCIAL_NEWS = 3;
82
83 final String RESP_SOCIAL_NEWS =
84
85 "{\"activities\":[{\"body\":\"\", \"appId\":\"\", \"identityId\":\"7ba7ff1a0a2106c63a820dfdda487a4a\", \"totalNumberOfComments\":1, \"templateParams\":{\"author\":\"fdrouet\", \"contenLink\":\"repository/collaboration/sites/intranet/web contents/Contributions/jmxtrans-addon\", \"contentName\":\"jmxtrans-addon\", \"dateCreated\":\"2014-04-17T11:57:18\", \"docSummary\":\"\", \"docTitle\":\"JMXTrans addon\", \"docTypeLabel\":\"Add-on\", \"docVersion\":\"0\", \"id\":\"6f1e77f8c06313bc4af5b62c1775b63d\", \"imagePath\":\"\", \"isSystemComment\":\"true\", \"lastModified\":\"\", \"message\":\"SocialIntegration.messages.emptyContent\", \"mimeType\":\"\", \"repository\":\"repository\", \"state\":\"draft\", \"systemComment\":\"\", \"workspace\":\"collaboration\"}, \"liked\":false, \"lastUpdated\":1397728639490, \"postedTime\":1397728639167, \"type\":\"contents:spaces\", \"posterIdentity\":{\"id\":\"7ba7ff1a0a2106c63a820dfdda487a4a\", \"providerId\":\"organization\", \"remoteId\":\"fdrouet\", \"profile\":{\"avatarUrl\":\"http://community.exoplatform.com:80/rest/jcr/repository/social/production/soc%3Aproviders/soc%3Aorganization/soc%3Afdrouet/soc%3Aprofile/soc%3Aavatar/?upd=1372169292623\", \"fullName\":\"Frederic DROUET\"} }, \"activityStream\":{\"title\":\"\", \"permaLink\":\"http://community.exoplatform.com:80/portal/intranet/activities/fdrouet\", \"prettyId\":\"fdrouet\", \"faviconUrl\":\"\", \"fullName\":\"Frederic DROUET\", \"type\":\"organization\"}, \"id\":\"6f1e78dac06313bc6eb98680ce104064\", \"title\":\"JMXTrans addon\", \"priority\":0.0, \"createdAt\":\"Thu Apr 17 11:57:19 +0200 2014\", \"likedByIdentities\":[], \"totalNumberOfLikes\":0, \"titleId\":\"\", \"comments\":[] } ] }";
86
87 final int REQ_SOCIAL_MY_CONNECTIONS = 4;
88
89 final String RESP_SOCIAL_MY_CONNECTIONS =
90
91 "{\"activities\":[{\"body\":\"\",\"appId\":\"\",\"identityId\":\"51998d9b0a2106c60330eb14726dc376\",\"totalNumberOfComments\":1,\"templateParams\":{},\"liked\":false,\"lastUpdated\":1396514460377,\"postedTime\":1396514164727,\"type\":\"DEFAULT_ACTIVITY\",\"posterIdentity\":{\"id\":\"51998d9b0a2106c60330eb14726dc376\",\"providerId\":\"organization\",\"remoteId\":\"patrice_lamarque\",\"profile\":{\"avatarUrl\":\"http://community.exoplatform.com:80/rest/jcr/repository/social/production/soc%3Aproviders/soc%3Aorganization/soc%3Apatrice_lamarque/soc%3Aprofile/soc%3Aavatar/?upd=1371464305999\",\"fullName\":\"Patrice Lamarque\"}},\"activityStream\":{\"title\":\"\",\"permaLink\":\"http://community.exoplatform.com:80/portal/intranet/activities/patrice_lamarque\",\"prettyId\":\"patrice_lamarque\",\"faviconUrl\":\"\",\"fullName\":\"Patrice Lamarque\",\"type\":\"organization\"},\"id\":\"26bb0ff9c06313bc307d3e126720561e\",\"title\":\"2 very cool add-ons have been added to the add-ons center today : <ul><li><a href=\"http://community.exoplatform.com/portal/intranet/addon-detail?content-id=/repository/collaboration/sites/intranet/web%20contents/Contributions/exo-atemis-extension\">Atemis</a></li><li><a href=\"http://community.exoplatform.com/portal/intranet/addon-detail?content-id=/repository/collaboration/sites/intranet/web%20contents/Contributions/video-wiki-macro\">Video Wiki Macro</a></li></ul>Try them out!\",\"priority\":0.0,\"createdAt\":\"Thu Apr 3 10:36:04 +0200 2014\",\"likedByIdentities\":[],\"totalNumberOfLikes\":8,\"titleId\":\"\",\"comments\":[]}]}";
92
93 final int REQ_SOCIAL_ALL_UPDATES = 5;
94
95 final String RESP_SOCIAL_ALL_UPDATES =
96
97 "{\"activities\":[{\"body\":\"\",\"appId\":\"\",\"identityId\":\"f07184510a2106c6201ac1956140d95f\",\"totalNumberOfComments\":2,\"templateParams\":{},\"liked\":false,\"lastUpdated\":1398099158541,\"postedTime\":1398013281970,\"type\":\"DEFAULT_ACTIVITY\",\"posterIdentity\":{\"id\":\"7fe04a04c06313bc6121d1235b281f23\",\"providerId\":\"organization\",\"remoteId\":\"ilkay_aydemir10\",\"profile\":{\"avatarUrl\":\"http://community.exoplatform.com:80/rest/jcr/repository/social/production/soc%3Aproviders/soc%3Aorganization/soc%3Ailkay_aydemir10/soc%3Aprofile/soc%3Aavatar/?upd=1398009779213\",\"fullName\":\"ilkay Aydemir\"}},\"activityStream\":{\"title\":\"\",\"permaLink\":\"http://community.exoplatform.com:80/portal/intranet/activities/jmazziotta\",\"prettyId\":\"jmazziotta\",\"faviconUrl\":\"\",\"fullName\":\"Julie Mazziotta\",\"type\":\"organization\"},\"id\":\"8015c6b9c06313bc7bddbab957acc8b0\",\"title\":\"Are you the admin site?\",\"priority\":0.0,\"createdAt\":\"Sun Apr 20 19:01:21 +0200 2014\",\"likedByIdentities\":[],\"totalNumberOfLikes\":0,\"titleId\":\"\",\"comments\":[]},{\"body\":\"\",\"appId\":\"\",\"identityId\":\"d3c28a300a2106c658573c3c030bf9da\",\"totalNumberOfComments\":2,\"templateParams\":{},\"liked\":false,\"lastUpdated\":1398080445273,\"postedTime\":1397828057340,\"type\":\"DEFAULT_ACTIVITY\",\"posterIdentity\":{\"id\":\"51998d9b0a2106c60330eb14726dc376\",\"providerId\":\"organization\",\"remoteId\":\"patrice_lamarque\",\"profile\":{\"avatarUrl\":\"http://community.exoplatform.com:80/rest/jcr/repository/social/production/soc%3Aproviders/soc%3Aorganization/soc%3Apatrice_lamarque/soc%3Aprofile/soc%3Aavatar/?upd=1371464305999\",\"fullName\":\"Patrice Lamarque\"}},\"activityStream\":{\"title\":\"\",\"permaLink\":\"http://community.exoplatform.com:80/portal/g/:spaces:translations/translations\",\"prettyId\":\"translations\",\"faviconUrl\":\"\",\"fullName\":\"Translations\",\"type\":\"space\"},\"id\":\"750b7901c06313bc6477591b2d591ed4\",\"title\":\"<a href='/portal/intranet/profile/nguyenbaoan'>An Bao Nguyen</a> <a href='/portal/intranet/profile/tglenat'>Tristan Glenat</a> Shouldn't we move eXo Mobile Translations to <a href='http://translate.exoplatform.org/project/exo-platform/' target='_blank'>http://translate.exoplatform.org/project/exo-platform/</a> now ? It will make them easier to find for potential contributors.\",\"priority\":0.0,\"createdAt\":\"Fri Apr 18 15:34:17 +0200 2014\",\"likedByIdentities\":[],\"totalNumberOfLikes\":3,\"titleId\":\"\",\"comments\":[]}]}";
98
99 final int REQ_PLATFORM_INFO = 6;
100
101 final String RESP_PLATFORM_INFO = "{\"duration\":\"UNLIMITED\",\"platformEdition\":\"ENTERPRISE\",\"buildNumber\":null,\"productCode\":\"CWI-team-09LC0xLDA2L\",\"unlockKey\":\"aaabbbccc\",\"nbUsers\":null,\"dateOfKeyGeneration\":null,\"platformVersion\":\"4.0.4\",\"isMobileCompliant\":\"true\",\"platformBuildNumber\":\"20131225\",\"platformRevision\":\"aaabbbccc\",\"userHomeNodePath\":\"/Users/t___/te___/tes___/testuser\",\"runningProfile\":\"all\",\"currentRepoName\":\"repository\",\"defaultWorkSpaceName\":\"collaboration\"}";
102
103 final int REQ_JCR_USER = 7;
104
105 final int REQ_JCR_USER_2 = 8;
106
107 final String RESP_JCR_USER = "{}";
108
109 final int REQ_PLATFORM_INFO_USER_2 = 9;
110
111 final String RESP_PLATFORM_INFO_USER_2 = "{\"duration\":\"UNLIMITED\",\"platformEdition\":\"ENTERPRISE\",\"buildNumber\":null,\"productCode\":\"CWI-team-09LC0xLDA2L\",\"unlockKey\":\"aaabbbccc\",\"nbUsers\":null,\"dateOfKeyGeneration\":null,\"platformVersion\":\"4.0.4\",\"isMobileCompliant\":\"true\",\"platformBuildNumber\":\"20131225\",\"platformRevision\":\"aaabbbccc\",\"userHomeNodePath\":\"/Users/t___/te___/tes___/testuser_2\",\"runningProfile\":\"all\",\"currentRepoName\":\"repository\",\"defaultWorkSpaceName\":\"collaboration\"}";
112
113 ActivityController<A> controller;
114
115 A activity;
116
117 @Before
118 public abstract void setup();
119
120 @After
121 public void teardown() {
122 controller.destroy();
123 }
124
125 public void create() {
126 activity = controller.create().start().resume().visible().get();
127 }
128
129 public void createWithBundle(Bundle b) {
130
131
132 activity = controller.create(b).start().resume().visible().get();
133 }
134
135 public void createWithIntent(Intent i) {
136 activity = controller.withIntent(i)
137 .create()
138 .start()
139 .resume()
140 .visible()
141 .get();
142 }
143
144 public void createAndAttach() {
145 activity = controller.attach().create().start().resume().visible().get();
146 }
147
148 public void createWithContext(Context ctx) {
149 activity = controller.withBaseContext(ctx).create().start().resume().visible().get();
150 }
151
152
153
154 public void enableLog() {
155 ShadowLog.stream = System.out;
156 }
157
158 public void disableLog() {
159 ShadowLog.stream = null;
160 }
161
162
163
164
165
166
167 public ExoAccount getServerWithDefaultValues() {
168 ExoAccount srv = new ExoAccount();
169 srv.accountName = TEST_SERVER_NAME;
170 srv.serverUrl = TEST_SERVER_URL;
171 srv.username = TEST_USER_NAME;
172 srv.password = TEST_USER_PWD;
173 return srv;
174 }
175
176
177
178
179
180
181
182 public void setLanguageInPreferences(Context c, String lang) {
183 SharedPreferences.Editor prefs = c.getSharedPreferences("exo_preference", 0).edit();
184 prefs.putString("exo_prf_localize", lang);
185 prefs.commit();
186 }
187
188
189
190
191
192
193
194 public void setDefaultServerInPreferences(Context c, ExoAccount server) {
195 ArrayList<ExoAccount> serversList = new ArrayList<ExoAccount>(1);
196 serversList.add(server);
197
198 ServerConfigurationUtils.generateXmlFileWithServerList(c, serversList, "ServerList.xml", "");
199 ServerSettingHelper.getInstance().setServerInfoList(serversList);
200
201 SharedPreferences.Editor prefs = c.getSharedPreferences("exo_preference", 0).edit();
202 prefs.putString("exo_prf_domain_index", "0");
203 prefs.commit();
204 }
205
206 public void selectAccountAtIndex(Context c, int idx) {
207 SharedPreferences.Editor prefs = c.getSharedPreferences("exo_preference", 0).edit();
208 prefs.putString("exo_prf_domain_index", String.valueOf(idx));
209 prefs.commit();
210 }
211
212
213
214
215
216
217 public void deleteAllAccounts(Context c) {
218 ServerConfigurationUtils.generateXmlFileWithServerList(c, new ArrayList<ExoAccount>(), "ServerList.xml", "");
219 ServerSettingHelper.getInstance().setServerInfoList(new ArrayList<ExoAccount>());
220
221 SharedPreferences.Editor prefs = c.getSharedPreferences("exo_preference", 0).edit();
222 prefs.remove("exo_prf_domain_index");
223 prefs.commit();
224 }
225
226
227
228
229
230
231
232 public void addServersInPreferences(Context c, ArrayList<ExoAccount> servers) {
233 addServersInPreferences(c, servers, 0);
234 }
235
236
237
238
239
240
241
242
243
244 public void addServersInPreferences(Context c, ArrayList<ExoAccount> servers, int posDefault) {
245 if (servers != null && servers.size() > 0) {
246 Log.i(TAG_TEST, "Saving " + servers.size() + " accounts.");
247 ServerConfigurationUtils.generateXmlFileWithServerList(c, servers, "ServerList.xml", "");
248 ServerSettingHelper.getInstance().setServerInfoList(servers);
249 if (posDefault >= 0 && posDefault < servers.size()) {
250 SharedPreferences.Editor prefs = c.getSharedPreferences("exo_preference", 0).edit();
251 prefs.putString("exo_prf_domain_index", String.valueOf(posDefault));
252 prefs.commit();
253 Log.i(TAG_TEST, "Account selected: " + posDefault);
254 AccountSetting.getInstance().setDomainIndex(String.valueOf(posDefault));
255 }
256 }
257 }
258
259 public List<ExoAccount> getAccounts(Context c) {
260 return ServerConfigurationUtils.getServerListFromFile(c, "ServerList.xml");
261 }
262
263 public ExoAccount getCurrentAccount(Context c) {
264 List<ExoAccount> list = getAccounts(c);
265 SharedPreferences pref = c.getSharedPreferences("exo_preference", 0);
266 int current = Integer.parseInt(pref.getString("exo_prf_domain_index", "-1"));
267 return list.get(current);
268 }
269
270
271
272
273
274
275
276
277
278 public ArrayList<ExoAccount> createXAccountsWithRMAndAL(int x, boolean rm, boolean al) {
279 ArrayList<ExoAccount> accounts = new ArrayList<ExoAccount>(x);
280
281 for (int i = 1; i <= x; i++) {
282 ExoAccount acc = getServerWithDefaultValues();
283 acc.accountName = TEST_SERVER_NAME + " " + i;
284 acc.username = TEST_USER_NAME + "_" + i;
285 acc.isRememberEnabled = rm;
286 acc.isAutoLoginEnabled = al;
287 accounts.add(acc);
288 }
289
290 return accounts;
291 }
292
293
294
295
296
297
298
299 public ArrayList<ExoAccount> createXAccounts(int x) {
300 return createXAccountsWithRMAndAL(x, true, true);
301 }
302
303
304
305
306
307
308
309
310 public RequestMatcher getMatcherForRequest(int req) {
311 RequestMatcherBuilder m = new RequestMatcherBuilder();
312 switch (req) {
313 case REQ_SOCIAL_VERSION_LATEST:
314 m.path("rest/api/social/version/latest.json");
315 break;
316
317 case REQ_SOCIAL_IDENTITY:
318 m.path("rest/private/api/social/v1-alpha3/portal/identity/organization/" + TEST_USER_NAME + ".json");
319 break;
320
321 case REQ_SOCIAL_IDENTITY_2:
322 m.path("rest/private/api/social/v1-alpha3/portal/identity/" + TEST_USER_IDENTITY + ".json");
323 break;
324
325 case REQ_SOCIAL_NEWS:
326 m.path("rest/private/api/social/v1-alpha3/portal/activity_stream/feed.json");
327 m.param("limit", "10");
328 break;
329
330 case REQ_SOCIAL_ALL_UPDATES:
331 m.path("rest/private/api/social/v1-alpha3/portal/activity_stream/feed.json");
332 m.param("limit", "50");
333 break;
334
335 case REQ_SOCIAL_MY_CONNECTIONS:
336 m.path("rest/private/api/social/v1-alpha3/portal/activity_stream/connections.json");
337 break;
338
339 case REQ_PLATFORM_INFO:
340 m.path("rest/private/platform/info");
341 break;
342
343 case REQ_JCR_USER:
344 m.path("rest/private/jcr/repository/collaboration/Users/t___/te___/tes___/testuser");
345 break;
346
347 case REQ_JCR_USER_2:
348 m.path("rest/private/jcr/repository/collaboration/Users/t___/te___/tes___/testuser_2");
349 break;
350
351 case REQ_PLATFORM_INFO_USER_2:
352 m.path("rest/private/platform/info");
353 break;
354 }
355 return m;
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
370 public BasicHttpResponse getResponseOKForRequest(int req) {
371 BasicHttpResponse resp = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
372 try {
373
374 switch (req) {
375 case REQ_SOCIAL_VERSION_LATEST:
376 resp.setEntity(new StringEntity(RESP_SOCIAL_VERSION_LATEST));
377 break;
378
379 case REQ_SOCIAL_IDENTITY:
380 case REQ_SOCIAL_IDENTITY_2:
381 resp.setEntity(new StringEntity(RESP_SOCIAL_IDENTITY));
382 break;
383
384 case REQ_SOCIAL_NEWS:
385 resp.setEntity(new StringEntity(RESP_SOCIAL_NEWS));
386 break;
387
388 case REQ_SOCIAL_ALL_UPDATES:
389 resp.setEntity(new StringEntity(RESP_SOCIAL_ALL_UPDATES));
390 break;
391
392 case REQ_SOCIAL_MY_CONNECTIONS:
393 resp.setEntity(new StringEntity(RESP_SOCIAL_MY_CONNECTIONS));
394 break;
395
396 case REQ_PLATFORM_INFO:
397 resp.setEntity(new StringEntity(RESP_PLATFORM_INFO));
398 break;
399 case REQ_PLATFORM_INFO_USER_2:
400 resp.setEntity(new StringEntity(RESP_PLATFORM_INFO_USER_2));
401 break;
402
403 case REQ_JCR_USER:
404 case REQ_JCR_USER_2:
405 resp.setEntity(new StringEntity(RESP_JCR_USER));
406 break;
407 }
408
409 } catch (UnsupportedEncodingException e) {
410 e.printStackTrace();
411 }
412
413 return resp;
414 }
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429 public BasicHttpResponse getResponseFailedWithStatus(int wantedStatus) {
430 int statusCode = 404;
431 if (wantedStatus >= 400 && wantedStatus < 600)
432 statusCode = wantedStatus;
433 return new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), statusCode, "ERROR");
434 }
435
436 }