aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2013-09-15 18:07:20 +0200
committerBananeweizen <bananeweizen@gmx.de>2013-09-15 18:07:42 +0200
commit693b9f4bdeb275c7aa9e12f3f24ebee9aad53be6 (patch)
tree3acdca769ccd59db1f49bc52a7fa1c038c4f79f9 /tests
parentec5c569f35730fa608a1ea83d46e4e60f1985310 (diff)
downloadcgeo-693b9f4bdeb275c7aa9e12f3f24ebee9aad53be6.zip
cgeo-693b9f4bdeb275c7aa9e12f3f24ebee9aad53be6.tar.gz
cgeo-693b9f4bdeb275c7aa9e12f3f24ebee9aad53be6.tar.bz2
refactoring: tests can now be run as app
* run unit tests app as normal android app (it has a launcher now) * hit "run tests" button * enjoy the log flying by
Diffstat (limited to 'tests')
-rw-r--r--tests/AndroidManifest.xml8
-rw-r--r--tests/res/layout/cgeo_tests_activity.xml54
-rw-r--r--tests/res/values/strings.xml2
-rw-r--r--tests/src/cgeo/geocaching/test/CgeoTestsActivity.java140
4 files changed, 204 insertions, 0 deletions
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 19029aa..77367ae 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -21,6 +21,14 @@
android:icon="@drawable/icon"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
+
+ <activity android:name="CgeoTestsActivity" >
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
</application>
</manifest> \ No newline at end of file
diff --git a/tests/res/layout/cgeo_tests_activity.xml b/tests/res/layout/cgeo_tests_activity.xml
new file mode 100644
index 0000000..ba87829
--- /dev/null
+++ b/tests/res/layout/cgeo_tests_activity.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:id="@+id/RelativeLayout1"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:layout_marginLeft="6dp"
+ android:layout_marginRight="6dp"
+ android:orientation="vertical" >
+
+ <TextView
+ android:id="@+id/headline"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_alignParentLeft="true"
+ android:layout_alignParentTop="true"
+ android:layout_gravity="center_vertical"
+ android:gravity="center_vertical"
+ android:lines="1"
+ android:singleLine="true"
+ android:text="@string/logcat"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <ScrollView
+ android:id="@+id/scrollView"
+ android:layout_width="match_parent"
+ android:layout_height="fill_parent"
+ android:layout_above="@+id/buttonRun"
+ android:layout_alignParentLeft="true"
+ android:layout_below="@+id/headline" >
+
+ <TextView
+ android:id="@+id/logOutput"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:textSize="8sp"
+ tools:ignore="SmallSp" />
+ </ScrollView>
+
+ <Button
+ android:id="@+id/buttonRun"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_alignParentBottom="true"
+ android:layout_alignParentLeft="false"
+ android:onClick="runTests"
+ android:text="@string/run_tests" />
+
+ <requestFocus
+ android:layout_alignTop="@+id/buttonRun"
+ android:layout_centerHorizontal="true"
+ android:layout_marginTop="18dp" />
+
+</RelativeLayout> \ No newline at end of file
diff --git a/tests/res/values/strings.xml b/tests/res/values/strings.xml
index 6f4b38a..1230358 100644
--- a/tests/res/values/strings.xml
+++ b/tests/res/values/strings.xml
@@ -2,5 +2,7 @@
<resources>
<string name="app_name">c:geo tests</string>
+ <string name="logcat">Logcat:</string>
+ <string name="run_tests">Run tests…</string>
</resources> \ No newline at end of file
diff --git a/tests/src/cgeo/geocaching/test/CgeoTestsActivity.java b/tests/src/cgeo/geocaching/test/CgeoTestsActivity.java
new file mode 100644
index 0000000..8b86f35
--- /dev/null
+++ b/tests/src/cgeo/geocaching/test/CgeoTestsActivity.java
@@ -0,0 +1,140 @@
+package cgeo.geocaching.test;
+
+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.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 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];
+ if (!TextUtils.isEmpty(line)) {
+ logView.append(Html.fromHtml("<font color=\"" + color(line) + "\">" + line + "</font><br/>"));
+ }
+ }
+
+ 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 (Exception 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 = (TextView) findViewById(R.id.logOutput);
+ }
+
+ @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 = (Button) findViewById(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();
+ }
+ }
+}