@@ -189,9 +189,203 @@ new mode 100755
189189 public static float getDensity() {
190190 Context context = InstrumentationRegistry.getTargetContext().getApplicationContext();
191191 return context.getResources().getDisplayMetrics().density;
192- similarity index 100%
193- rename from node_modules/detox/android/detox/src/oldOkhttp/java/com/wix/detox/WebSocketClient.java
194- rename to node_modules/detox/android/detox/src/minReactNative44/java/com/wix/detox/WebSocketClient.java
192+ new file mode 100644
193+ --- /dev/null
194+ +++ b/node_modules/detox/android/detox/src/minReactNative44/java/com/wix/detox/WebSocketClient.java
195+ @@ -0,0 +1,193 @@
196+ + package com.wix.detox;
197+ +
198+ + import android.util.Log;
199+ +
200+ + import com.wix.detox.systeminfo.Environment;
201+ +
202+ + import org.json.JSONException;
203+ + import org.json.JSONObject;
204+ +
205+ + import java.io.BufferedReader;
206+ + import java.io.IOException;
207+ + import java.util.HashMap;
208+ + import java.util.Map;
209+ + import java.util.concurrent.TimeUnit;
210+ +
211+ + import okhttp3.OkHttpClient;
212+ + import okhttp3.Request;
213+ + import okhttp3.RequestBody;
214+ + import okhttp3.Response;
215+ + import okhttp3.ResponseBody;
216+ + import okhttp3.ws.WebSocket;
217+ + import okhttp3.ws.WebSocketCall;
218+ + import okhttp3.ws.WebSocketListener;
219+ + import okio.Buffer;
220+ +
221+ + import static okhttp3.ws.WebSocket.TEXT;
222+ +
223+ + /**
224+ + * Created by rotemm on 27/12/2016.
225+ + */
226+ +
227+ + public class WebSocketClient implements WebSocketListener {
228+ +
229+ + @Override
230+ + public void onOpen(WebSocket webSocket, Response response) {
231+ + Log.i(LOG_TAG, "At onOpen");
232+ + this.websocket = webSocket;
233+ + HashMap params = new HashMap();
234+ + params.put("sessionId", sessionId);
235+ + params.put("role", "testee");
236+ + sendAction("login", params, 0L);
237+ + actionHandler.onConnect();
238+ + }
239+ +
240+ + @Override
241+ + public void onFailure(IOException e, Response response) {
242+ + Log.e(LOG_TAG, "Detox Error: ", e);
243+ +
244+ + //OKHttp won't recover from failure if it got ConnectException,
245+ + // this is a workaround to make the websocket client try reconnecting when failed.
246+ + try {
247+ + Thread.sleep(1000);
248+ + } catch (InterruptedException e2) {
249+ + Log.d(LOG_TAG, "interrupted", e2);
250+ + }
251+ + Log.d(LOG_TAG, "Retrying...");
252+ + connectToServer(url, sessionId);
253+ + }
254+ +
255+ + @Override
256+ + public void onMessage(ResponseBody message) throws IOException {
257+ + Log.i(LOG_TAG, "At onMessage");
258+ + if (message.contentType() == WebSocket.TEXT) {
259+ + StringBuffer sb = new StringBuffer();
260+ + String line;
261+ + BufferedReader buffer = new BufferedReader(message.charStream());
262+ + while ((line = buffer.readLine()) != null) {
263+ + sb.append(line);
264+ + }
265+ + receiveAction(websocket, sb.toString());
266+ + }
267+ + message.close();
268+ + }
269+ +
270+ + @Override
271+ + public void onPong(Buffer payload) {
272+ + // empty
273+ + }
274+ +
275+ + private volatile boolean closing = false;
276+ +
277+ + @Override
278+ + public void onClose(int code, String reason) {
279+ + Log.i(LOG_TAG, "At onClose");
280+ + Log.d(LOG_TAG, "Detox Closed: " + code + " " + reason);
281+ + closing = true;
282+ + actionHandler.onClosed();
283+ + }
284+ +
285+ + public void close() {
286+ + if (closing) {
287+ + return;
288+ + }
289+ + closing = true;
290+ + try {
291+ + websocket.close(NORMAL_CLOSURE_STATUS, null);
292+ + } catch (IOException e) {
293+ + Log.i(LOG_TAG, "WS close", e);
294+ + } catch (IllegalStateException e) {
295+ + Log.i(LOG_TAG, "WS close", e);
296+ + }
297+ + }
298+ +
299+ + private static final String LOG_TAG = "WebSocketClient";
300+ +
301+ + private String url;
302+ + private String sessionId;
303+ + private OkHttpClient client;
304+ + private WebSocket websocket = null;
305+ + private ActionHandler actionHandler;
306+ +
307+ + private static final int NORMAL_CLOSURE_STATUS = 1000;
308+ +
309+ + public WebSocketClient(ActionHandler actionHandler) {
310+ + this.actionHandler = actionHandler;
311+ + }
312+ +
313+ + public void connectToServer(String sessionId) {
314+ +
315+ + connectToServer(Environment.getServerHost(), sessionId);
316+ + }
317+ +
318+ + public void connectToServer(String url, String sessionId) {
319+ + Log.i(LOG_TAG, "At connectToServer");
320+ + this.url = url;
321+ + this.sessionId = sessionId;
322+ +
323+ + client = new OkHttpClient.Builder().
324+ + retryOnConnectionFailure(true).
325+ + connectTimeout(1500, TimeUnit.MILLISECONDS).
326+ + readTimeout(0, TimeUnit.MILLISECONDS).build();
327+ +
328+ + Request request = new Request.Builder().url(url).build();
329+ +
330+ + WebSocketCall.create(client, request).enqueue(this);
331+ +
332+ + client.dispatcher().executorService().shutdown();
333+ + }
334+ +
335+ + public void sendAction(String type, Map params, Long messageId) {
336+ + Log.i(LOG_TAG, "At sendAction");
337+ + HashMap data = new HashMap();
338+ + data.put("type", type);
339+ + data.put("params", params);
340+ + data.put("messageId", messageId);
341+ +
342+ + JSONObject json = new JSONObject(data);
343+ + try {
344+ + websocket.sendMessage(RequestBody.create(TEXT, json.toString()));
345+ + } catch (IOException e) {
346+ + Log.e(LOG_TAG, "Error sending msg through WS", e);
347+ + }
348+ +
349+ + Log.d(LOG_TAG, "Detox Action Sent: " + type);
350+ +
351+ + }
352+ +
353+ + public void receiveAction(WebSocket webSocket, String json) {
354+ + Log.i(LOG_TAG, "At receiveAction");
355+ + try {
356+ + JSONObject object = new JSONObject(json);
357+ +
358+ + String type = (String) object.get("type");
359+ + if (type == null) {
360+ + Log.e(LOG_TAG, "Detox Error: receiveAction missing type");
361+ + return;
362+ + }
363+ +
364+ + Object params = object.get("params");
365+ + if (params != null && !(params instanceof JSONObject)) {
366+ + Log.d(LOG_TAG, "Detox Error: receiveAction invalid params");
367+ + }
368+ + long messageId = object.getLong("messageId");
369+ +
370+ + Log.d(LOG_TAG, "Detox Action Received: " + type);
371+ +
372+ + if (actionHandler != null) actionHandler.onAction(type, params.toString(), messageId);
373+ + } catch (JSONException e) {
374+ + Log.e(LOG_TAG, "Detox Error: receiveAction decode - " + e.toString());
375+ + }
376+ +
377+ + }
378+ +
379+ + /**
380+ + * These methods are called on an inner worker thread.
381+ + * @see <a href="https://medium.com/@jakewharton/listener-messages-are-called-on-a-background-thread-since-okhttp-is-agnostic-with-respect-to-5fdc5182e240">OkHTTP</a>
382+ + */
383+ + public interface ActionHandler {
384+ + void onAction(String type, String params, long messageId);
385+ + void onConnect();
386+ + void onClosed();
387+ + }
388+ + }
195389new file mode 100644
196390Binary files /dev/null and b/node_modules/detox/android/detox/src/minReactNative46/.DS_Store differ
197391new file mode 100644
@@ -200,9 +394,177 @@ new file mode 100644
200394Binary files /dev/null and b/node_modules/detox/android/detox/src/minReactNative46/java/com/.DS_Store differ
201395new file mode 100644
202396Binary files /dev/null and b/node_modules/detox/android/detox/src/minReactNative46/java/com/wix/.DS_Store differ
203- similarity index 100%
204- rename from node_modules/detox/android/detox/src/newOkhttp/java/com/wix/detox/WebSocketClient.java
205- rename to node_modules/detox/android/detox/src/minReactNative46/java/com/wix/detox/WebSocketClient.java
397+ new file mode 100644
398+ --- /dev/null
399+ +++ b/node_modules/detox/android/detox/src/minReactNative46/java/com/wix/detox/WebSocketClient.java
400+ @@ -0,0 +1,167 @@
401+ + package com.wix.detox;
402+ +
403+ + import android.util.Log;
404+ +
405+ + import com.wix.detox.systeminfo.Environment;
406+ +
407+ + import org.json.JSONException;
408+ + import org.json.JSONObject;
409+ +
410+ + import java.util.HashMap;
411+ + import java.util.Map;
412+ + import java.util.concurrent.TimeUnit;
413+ +
414+ + import okhttp3.OkHttpClient;
415+ + import okhttp3.Request;
416+ + import okhttp3.Response;
417+ + import okhttp3.WebSocket;
418+ + import okhttp3.WebSocketListener;
419+ + import okio.ByteString;
420+ +
421+ + /**
422+ + * Created by rotemm on 27/12/2016.
423+ + */
424+ +
425+ + public class WebSocketClient extends WebSocketListener {
426+ +
427+ + @Override
428+ + public void onOpen(WebSocket webSocket, Response response) {
429+ + Log.i(LOG_TAG, "At onOpen");
430+ + HashMap params = new HashMap();
431+ + params.put("sessionId", sessionId);
432+ + params.put("role", "testee");
433+ + sendAction("login", params, 0L);
434+ + actionHandler.onConnect();
435+ + }
436+ +
437+ + @Override
438+ + public void onFailure(WebSocket webSocket, Throwable t, Response response) {
439+ + Log.e(LOG_TAG, "Detox Error: ", t);
440+ +
441+ + //OKHttp won't recover from failure if it got ConnectException,
442+ + // this is a workaround to make the websocket client try reconnecting when failed.
443+ + try {
444+ + Thread.sleep(1000);
445+ + } catch (InterruptedException e2) {
446+ + Log.d(LOG_TAG, "interrupted", e2);
447+ + }
448+ + Log.d(LOG_TAG, "Retrying...");
449+ + connectToServer(url, sessionId);
450+ + }
451+ +
452+ + @Override
453+ + public void onMessage(WebSocket webSocket, String text) {
454+ + Log.i(LOG_TAG, "At onMessage");
455+ + receiveAction(websocket, text);
456+ + }
457+ +
458+ + @Override
459+ + public void onMessage(WebSocket webSocket, ByteString bytes) {
460+ + Log.e(LOG_TAG, "Unexpected binary ws message from detox server.");
461+ + }
462+ +
463+ + private volatile boolean closing = false;
464+ +
465+ + @Override
466+ + public void onClosed(WebSocket webSocket, int code, String reason) {
467+ + Log.d(LOG_TAG, "Detox WS Closed: " + code + " " + reason);
468+ + closing = true;
469+ + actionHandler.onClosed();
470+ + }
471+ +
472+ + @Override
473+ + public void onClosing(WebSocket webSocket, int code, String reason) {
474+ + Log.i(LOG_TAG, "At onClose");
475+ + closing = true;
476+ + websocket.close(NORMAL_CLOSURE_STATUS, null);
477+ + }
478+ +
479+ + public void close() {
480+ + if (closing) return;
481+ + closing = true;
482+ + websocket.close(NORMAL_CLOSURE_STATUS, null);
483+ + }
484+ +
485+ + private static final String LOG_TAG = "WebSocketClient";
486+ +
487+ + private String url;
488+ + private String sessionId;
489+ + private OkHttpClient client;
490+ + private WebSocket websocket = null;
491+ + private ActionHandler actionHandler;
492+ +
493+ + private static final int NORMAL_CLOSURE_STATUS = 1000;
494+ +
495+ + public WebSocketClient(ActionHandler actionHandler) {
496+ + this.actionHandler = actionHandler;
497+ + }
498+ +
499+ + public void connectToServer(String sessionId) {
500+ +
501+ + connectToServer(Environment.getServerHost(), sessionId);
502+ + }
503+ +
504+ + public void connectToServer(String url, String sessionId) {
505+ + Log.i(LOG_TAG, "At connectToServer");
506+ + this.url = url;
507+ + this.sessionId = sessionId;
508+ +
509+ + client = new OkHttpClient.Builder().
510+ + retryOnConnectionFailure(true).
511+ + connectTimeout(1500, TimeUnit.MILLISECONDS).
512+ + readTimeout(0, TimeUnit.MILLISECONDS).build();
513+ +
514+ + Request request = new Request.Builder().url(url).build();
515+ +
516+ + this.websocket = client.newWebSocket(request, this);
517+ +
518+ + client.dispatcher().executorService().shutdown();
519+ + }
520+ +
521+ + public void sendAction(String type, Map params, Long messageId) {
522+ + Log.i(LOG_TAG, "At sendAction");
523+ + HashMap data = new HashMap();
524+ + data.put("type", type);
525+ + data.put("params", params);
526+ + data.put("messageId", messageId);
527+ + JSONObject json = new JSONObject(data);
528+ +
529+ + websocket.send(json.toString());
530+ + Log.d(LOG_TAG, "Detox Action Sent: " + type);
531+ + }
532+ +
533+ + public void receiveAction(WebSocket webSocket, String json) {
534+ + Log.i(LOG_TAG, "At receiveAction");
535+ + try {
536+ + JSONObject object = new JSONObject(json);
537+ +
538+ + String type = (String) object.get("type");
539+ + if (type == null) {
540+ + Log.e(LOG_TAG, "Detox Error: receiveAction missing type");
541+ + return;
542+ + }
543+ +
544+ + Object params = object.get("params");
545+ + if (params != null && !(params instanceof JSONObject)) {
546+ + Log.d(LOG_TAG, "Detox Error: receiveAction invalid params");
547+ + }
548+ + long messageId = object.getLong("messageId");
549+ +
550+ + Log.d(LOG_TAG, "Detox Action Received: " + type);
551+ +
552+ + if (actionHandler != null) actionHandler.onAction(type, params.toString(), messageId);
553+ + } catch (JSONException e) {
554+ + Log.e(LOG_TAG, "Detox Error: receiveAction decode - " + e.toString());
555+ + }
556+ + }
557+ +
558+ + /**
559+ + * These methods are called on an inner worker thread.
560+ + * @see <a href="https://medium.com/@jakewharton/listener-messages-are-called-on-a-background-thread-since-okhttp-is-agnostic-with-respect-to-5fdc5182e240">OkHTTP</a>
561+ + */
562+ + public interface ActionHandler {
563+ + void onAction(String type, String params, long messageId);
564+ + void onConnect();
565+ + void onClosed();
566+ + }
567+ + }
206568new file mode 100644
207569Binary files /dev/null and b/node_modules/detox/android/gradle/.DS_Store differ
208570old mode 100644
0 commit comments