1 package com.android.rs.unittest;
2 
3 import com.android.rs.unittest.UnitTest;
4 
5 import android.app.ListActivity;
6 import android.content.Context;
7 import android.graphics.Color;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.ArrayAdapter;
12 import android.widget.TextView;
13 
14 import java.util.ArrayList;
15 import java.util.List;
16 
17 public abstract class RSListActivity extends ListActivity {
18     private static final String TAG = RSListActivity.class.getSimpleName();
19 
getUnitTests()20     protected abstract Iterable<Class<? extends UnitTest>> getUnitTests() throws Exception;
21 
logStartUnitTest(UnitTest test)22     protected abstract void logStartUnitTest(UnitTest test);
logEndUnitTest(UnitTest test)23     protected abstract void logEndUnitTest(UnitTest test);
24 
25     @Override
onCreate(Bundle bundle)26     public void onCreate(Bundle bundle) {
27         try {
28             super.onCreate(bundle);
29 
30             List<UnitTest> validUnitTests = new ArrayList<>();
31 
32             Iterable<Class<? extends UnitTest>> testClasses = getUnitTests();
33 
34             for (Class<? extends UnitTest> testClass : testClasses) {
35                 UnitTest test = testClass.getDeclaredConstructor(Context.class).newInstance(this);
36                 validUnitTests.add(test);
37             }
38 
39             ArrayAdapter<UnitTest> adapter = new ArrayAdapter<UnitTest>(this,
40                     android.R.layout.simple_list_item_1, validUnitTests) {
41                 @Override
42                 public View getView(int position, View convertView, ViewGroup parent) {
43                     View view = super.getView(position, convertView, parent);
44 
45                     TextView textView = (TextView) view.findViewById(android.R.id.text1);
46                     UnitTest test = getItem(position);
47                     String textSuffix = "";
48                     int textColor = Color.BLACK;
49                     switch (test.getResult()) {
50                         case UT_RUNNING:
51                             textSuffix = " - RUNNING";
52                             break;
53                         case UT_SUCCESS:
54                             textSuffix = " - PASS";
55                             textColor = Color.GREEN;
56                             break;
57                         case UT_FAIL:
58                             textSuffix = " - FAIL";
59                             textColor = Color.RED;
60                             break;
61                     }
62                     String text = String.format("%s%s", test.toString(), textSuffix);
63                     textView.setText(text);
64                     textView.setTextColor(textColor);
65 
66                     return view;
67                 }
68             };
69             setListAdapter(adapter);
70 
71             Runnable run = () -> {
72                 for (UnitTest unitTest : validUnitTests) {
73                     RSListActivity.this.logStartUnitTest(unitTest);
74                     unitTest.runTest();
75                     RSListActivity.this.logEndUnitTest(unitTest);
76                     RSListActivity.this.runOnUiThread(adapter::notifyDataSetChanged);
77                 }
78             };
79             new Thread(run).start();
80         } catch (Exception e) {
81             throw new RuntimeException(e);
82         }
83     }
84 }
85