1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package cgeo.geocaching.test;
import butterknife.ButterKnife;
import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.InstrumentationInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
public class CgeoTestsActivity extends Activity {
private static final String TAG = CgeoTestsActivity.class.getName();
private static final int TIMEOUT = 600 * 1000;
private TextView logView;
private LogcatAsyncTask logCatTask;
private BottomAwareScrollView scrollView;
private class LogcatAsyncTask extends AsyncTask<Integer, String, Void> {
// TestRunner and silence others
private static final String CMD = "logcat -v brief TestRunner:I cgeo:I *:S";
private BufferedReader mReader;
private Process mProc;
public LogcatAsyncTask() {
try {
mProc = Runtime.getRuntime().exec(CMD);
mReader = new BufferedReader(new InputStreamReader(
mProc.getInputStream()));
} catch (Exception e) {
Log.e(TAG, "Creating proc", e);
}
}
@Override
protected void onProgressUpdate(String... values) {
final String line = values[0];
final boolean isAtBottom = scrollView.isAtBottom();
if (!TextUtils.isEmpty(line)) {
logView.append(Html.fromHtml("<font color=\"" + color(line) + "\">" + line + "</font><br/>"));
if (isAtBottom) {
scrollView.scrollTo(0, logView.getBottom());
}
}
}
private String color(String line) {
switch (line.charAt(0)) {
case 'E':
return "red";
case 'W':
return "#FFA500";
case 'D':
return "blue";
default:
return "white";
}
}
@Override
protected Void doInBackground(Integer... params) {
final long timeout = System.currentTimeMillis() + params[0];
try {
do {
Thread.sleep(50);
publishProgress(mReader.readLine());
} while (System.currentTimeMillis() < timeout);
} catch (InterruptedException e) {
publishProgress("ERROR: " + e);
} catch (IOException e) {
publishProgress("ERROR: " + e);
} finally {
publishProgress("END");
mProc.destroy();
}
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cgeo_tests_activity);
logView = ButterKnife.findById(this, R.id.logOutput);
scrollView = ButterKnife.findById(this, R.id.scrollView);
}
@Override
protected void onDestroy() {
if (logCatTask != null) {
logCatTask.cancel(true);
}
super.onDestroy();
}
private InstrumentationInfo getInstrumentationInfo(final String packageName) {
final List<InstrumentationInfo> list =
getPackageManager()
.queryInstrumentation(packageName, 0);
return (!list.isEmpty()) ? list.get(0) : null;
}
/**
* @param v
* referenced from XML layout
*/
public void runTests(final View v) {
final Button button = ButterKnife.findById(this, R.id.buttonRun);
button.setEnabled(false);
try {
runTestsInternally();
} finally {
// button.setEnabled(true);
}
}
private void runTestsInternally() {
final String pn = getPackageName().replaceFirst(".test$", "");
final InstrumentationInfo info = getInstrumentationInfo(pn);
if (info == null) {
Toast.makeText(this,
"Cannot find instrumentation for " + pn, Toast.LENGTH_SHORT)
.show();
return;
}
final ComponentName cn = new ComponentName(info.packageName,
info.name);
if (startInstrumentation(cn, null, null)) {
logCatTask = new LogcatAsyncTask();
logCatTask.execute(TIMEOUT);
}
else {
Toast.makeText(this,
"Cannot run instrumentation for " + pn, Toast.LENGTH_SHORT)
.show();
}
}
}
|