package test;

import android.content.Context;
import android.os.Handler;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.concurrent.CountDownLatch;

import il.org.hasadna.opentrain.Reporter;
import il.org.hasadna.opentrain.Scanner;

/**
 * Created by Noam.m on 6/2/2014.
 */
public class TestTrainTrip extends AndroidTestCase {

    private CountDownLatch signal = new CountDownLatch(1);
    private Handler handler = new Handler();
    private Scanner mScanner;
    private Reporter mReporter;

    private MockLocationScanner.OnDoneListener onDoneListener=new MockLocationScanner.OnDoneListener() {
        @Override
        public void onDone() {
            signal.countDown();
        }
    };

    protected void setUp() throws Exception {
        super.setUp();
        Context context = getContext().getApplicationContext();
        mReporter = new Reporter(context);
        MockLocationScanner mockLocationScanner = new MockLocationScanner(context);
        mockLocationScanner.setLocationListener(onDoneListener);
        MockWifiScanner mockWifiScanner = new MockWifiScanner(context);
        mScanner = new Scanner(context, mockWifiScanner, mockLocationScanner);
        mockWifiScanner.setLocationScanner(mockLocationScanner);
        mScanner.startScanning();
    }

    @Override
    protected void tearDown() throws Exception {
        mScanner.stopScanning();
        mReporter.shutdown();
        super.tearDown();
    }

    @SmallTest
    public void testTrainTrip1() {
        try {
            signal.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}

@@ -59,16 +59,101 @@
 oid.
-os.Handl
+content.SharedPreferences;%0Aimport android.os.Build;%0Aimport android.preference.PreferenceManag
 er;%0A
@@ -173,22 +173,26 @@
 d.test.A
-ndroid
+pplication
 TestCase
@@ -245,24 +245,194 @@
 SmallTest;%0A%0A
+import org.json.JSONArray;%0Aimport org.json.JSONObject;%0A%0Aimport java.io.BufferedReader;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.net.URL;%0A
 import java.
@@ -546,16 +546,77 @@
 Scanner;
+%0Aimport il.org.hasadna.opentrain.application.MainApplication;
 %0A%0A/**%0A *
@@ -691,22 +691,43 @@
 ds A
-ndroidTestCase
+pplicationTestCase%3CMainApplication%3E
  %7B%0A%0A
@@ -789,53 +789,8 @@
 1);%0A
-    private Handler handler = new Handler();%0A
     
@@ -846,16 +846,47 @@
 eporter;
+%0A    private String testerName;
 %0A%0A    pr
@@ -940,17 +940,19 @@
 Listener
-=
+ = 
 new Mock
@@ -1031,24 +1031,53 @@
  onDone() %7B%0A
+            getServerTrip();%0A
             
@@ -1114,16 +1114,90 @@
     %7D;%0A%0A
+    public TestTrainTrip() %7B%0A        super(MainApplication.class);%0A    %7D%0A%0A
     prot
@@ -1317,24 +1317,49 @@
 nContext();%0A
+        setTesterName();%0A
         mRep
@@ -1758,42 +1758,8 @@
 r);%0A
-        mScanner.startScanning();%0A
     
@@ -1984,16 +1984,54 @@
   try %7B%0A
+            mScanner.startScanning();%0A
         
@@ -2143,11 +2143,1428 @@
 %0A    %7D%0A%0A
+    private void setTesterName() %7B%0A        testerName = %22test_10%22;%0A        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);%0A        SharedPreferences.Editor editor = sharedPreferences.edit();%0A        editor.putString(%22prefUsername%22, testerName);%0A        if (Build.VERSION.SDK_INT %3E= 9) %7B%0A            editor.apply();%0A        %7D else %7B%0A            editor.commit();%0A        %7D%0A    %7D%0A%0A    private void getServerTrip() %7B%0A        try %7B%0A            String url = %22http://opentrain.hasadna.org.il/api/1/devices/%22 + testerName + %22/reports/%22;%0A            URL serverUrl = new URL(url);%0A            InputStream inputStream = serverUrl.openStream();%0A            BufferedReader in = new BufferedReader(%0A                    new InputStreamReader(inputStream));%0A            StringBuilder responseString = new StringBuilder();%0A            String line = %22%22;%0A            while ((line = in.readLine()) != null) %7B%0A                responseString.append(line);%0A            %7D%0A            JSONObject jsonObject = new JSONObject(responseString.toString());%0A            JSONArray jsonArray = jsonObject.getJSONArray(%22objects%22);%0A            int size = jsonArray.length();%0A            for (int i = 0; i %3C size; i++) %7B%0A                JSONObject object = jsonArray.getJSONObject(i);%0A                if (object != null) %7B%0A%0A                %7D%0A            %7D%0A        %7D catch (Exception e) %7B%0A%0A        %7D%0A    %7D%0A%0A
 %0A%7D%0A

test trip