lundi 25 juin 2018

Unit test an Android activity

Im making an application in android studio. And for this project i have to make multiple unit tests. But i have no idea what i can unit test this application. Im really struggeling with unit testing so i would appreciate if someone could help me, or come up with some unit tests i can implement in my project. Its an app with a listview that retrieves data from a webservice.

Code:

 public class MainActivity extends AppCompatActivity {

ListView listView;
ArrayAdapter<String> adapter;
String[] data;
String[] waarde;
String[] hoog;
String[] laag;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = findViewById(R.id.listview);

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());

    getData();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(MainActivity.this, Showdata.class);
        intent.putExtra("teeltbed", listView.getItemAtPosition(position).toString());
        startActivity(intent);
        }
    });

}

private void getData()
{
    String getData = null;
    String dbResult = "empty";
    dbConnect database = new dbConnect(this);

    try{
        String query = "SELECT * FROM Lamp";
        getData = "?query=" + URLEncoder.encode(query, "UTF-8");
        //data += "&username=" + URLEncoder.encode(userName, "UTF-8");
        String link = "http://10.247.240.53/kas/lampen.php";
        dbResult = database.execute(link).get();
    }
    catch (Exception e){

    }

    try{
        JSONObject jsonObject = new JSONObject(dbResult);
        JSONArray array = jsonObject.getJSONArray("Lamp");
        waarde = new String[array.length()];
        data = new String[array.length()];
        hoog = new String[array.length()];
        laag = new String[array.length()];
        for (int i = 0; i < array.length(); i++)
        {
            jsonObject = array.getJSONObject(i);
            data[i] = jsonObject.getString("teeltbed");
            waarde[i] = "A: " + jsonObject.getString("waarde") + " %";
            hoog[i] = "H: " + jsonObject.getString("hoog") + " W/m2";
            laag[i] = "L: " + jsonObject.getString("laag") + " W/m2";

        }

        listView.setAdapter(new dataListAdapter(data, waarde, hoog, laag));
    }

    catch (Exception e)
    {
        e.printStackTrace();
    }
}

class dataListAdapter extends BaseAdapter{
    String[] data, waarde;

    dataListAdapter(){
        data = null;
        waarde = null;
    }

    public dataListAdapter(String[] sdata, String[]swaarde, String[]shoog, String[]slaag)
    {
        data = sdata;
        waarde = swaarde;
        hoog = shoog;
        laag = slaag;

    }

    public int getCount()
    {
        return data.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView (int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = getLayoutInflater();
        View row;
        row = inflater.inflate(R.layout.layout_list, parent, false);
        TextView t1, t2, t3, t4;
        t1 = (TextView) row.findViewById(R.id.list_item);
        t2 = (TextView) row.findViewById(R.id.list_item2);
        t3 = (TextView) row.findViewById(R.id.list_item3);
        t4 = (TextView) row.findViewById(R.id.list_item4);
        t1.setText(data[position]);
        t2.setText(waarde[position]);
        t3.setText(hoog[position]);
        t4.setText(laag[position]);
        return (row);
    }
}

class Sproeier
{
 public int TeeltBed;
 public String Stand;
 public double Actueel;
 public double Hoog;
 public double Laag;

 public void Sproeier()
 {
     int teeltbed;
     double hoog;
     double laag;
 }
}

class Lamp {

}

public static class ScadaWebservice
{

}

class Kas
{

    public void kas()
    {

    }
}

}

Aucun commentaire:

Enregistrer un commentaire