EMMA Coverage Report (generated Sun Aug 25 06:27:34 PDT 2013)
[all classes][com.example.instrumentation]

COVERAGE SUMMARY FOR SOURCE FILE [EmmaInstrumentation.java]

nameclass, %method, %block, %line, %
EmmaInstrumentation.java100% (2/2)75%  (9/12)46%  (113/245)50%  (30.9/62)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class EmmaInstrumentation100% (1/1)67%  (6/9)43%  (97/225)46%  (24.9/54)
generateCoverageReport (): void 0%   (0/1)0%   (0/84)0%   (0/19)
reportEmmaError (Exception): void 0%   (0/1)0%   (0/5)0%   (0/2)
reportEmmaError (String, Exception): void 0%   (0/1)0%   (0/27)0%   (0/4)
onActivityFinished (): void 100% (1/1)47%  (7/15)40%  (2/5)
getCoverageFilePath (): String 100% (1/1)62%  (5/8)67%  (2/3)
getBooleanArgument (Bundle, String): boolean 100% (1/1)92%  (12/13)96%  (1.9/2)
EmmaInstrumentation (): void 100% (1/1)100% (11/11)100% (4/4)
onCreate (Bundle): void 100% (1/1)100% (45/45)100% (9/9)
onStart (): void 100% (1/1)100% (17/17)100% (6/6)
     
class EmmaInstrumentation$InstrumentedActivity100% (1/1)100% (3/3)80%  (16/20)75%  (6/8)
finish (): void 100% (1/1)69%  (9/13)60%  (3/5)
EmmaInstrumentation$InstrumentedActivity (): void 100% (1/1)100% (3/3)100% (1/1)
setFinishListener (FinishListener): void 100% (1/1)100% (4/4)100% (2/2)

1package com.example.instrumentation;
2 
3import java.lang.reflect.InvocationTargetException;
4import java.lang.reflect.Method;
5 
6import com.example.i2at.tc.TemperatureConverterActivity;
7//import com.vladium.emma.rt.RT;
8 
9import android.app.Activity;
10import android.app.Instrumentation;
11import android.content.Intent;
12import android.os.Bundle;
13import android.os.Looper;
14import android.util.Log;
15 
16public class EmmaInstrumentation extends Instrumentation implements FinishListener {
17 
18    private static final String TAG = "EmmaInstrumentation";
19 
20    private static final boolean LOGD = true;
21 
22    private static final String DEFAULT_COVERAGE_FILE_PATH = "/mnt/sdcard/coverage.ec";
23 
24    private final Bundle mResults = new Bundle();
25 
26    private Intent mIntent;
27 
28    private boolean mCoverage = true;
29 
30    private String mCoverageFilePath;
31 
32    /**
33     * Extends the AUT to provide the necessary behavior to invoke the
34     * {@link FinishListener} that may have been provided using
35     * {@link #setFinishListener(FinishListener)}.
36     * 
37     * It's important to note that the original Activity has not been modified.
38     * Also, the Activity must be declared in the
39     * <code>AndroidManifest.xml</code> because it is started by an intent in
40     * {@link EmmaInstrumentation#onStart()}. This turns more difficult to use
41     * other methods like using template classes. This latter method could be
42     * viable, but all Activity methods should be re-written to invoke the
43     * template parameter class corresponding methods.
44     * 
45     * @author diego
46     * 
47     */
48    public static class InstrumentedActivity extends
49    TemperatureConverterActivity {
50        private FinishListener mListener;
51 
52        public void setFinishListener(FinishListener listener) {
53            mListener = listener;
54        }
55 
56        @Override
57        public void finish() {
58            if (LOGD)
59                Log.d(TAG + ".InstrumentedActivity", "finish()");
60            super.finish();
61            if (mListener != null) {
62                mListener.onActivityFinished();
63            }
64        }
65 
66    }
67 
68    /**
69     * Constructor
70     */
71    public EmmaInstrumentation() {
72 
73    }
74 
75    @Override
76    public void onCreate(Bundle arguments) {
77        if (LOGD)
78            Log.d(TAG, "onCreate(" + arguments + ")");
79        super.onCreate(arguments);
80 
81        if (arguments != null) {
82            mCoverage = getBooleanArgument(arguments, "coverage");
83            mCoverageFilePath = arguments.getString("coverageFile");
84        }
85 
86        mIntent = new Intent(getTargetContext(), InstrumentedActivity.class);
87        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
88        start();
89    }
90 
91    @Override
92    public void onStart() {
93        if (LOGD)
94            Log.d(TAG, "onStart()");
95        super.onStart();
96 
97        Looper.prepare();
98        InstrumentedActivity activity = (InstrumentedActivity) startActivitySync(mIntent);
99        activity.setFinishListener(this);
100    }
101 
102    private boolean getBooleanArgument(Bundle arguments, String tag) {
103        String tagString = arguments.getString(tag);
104        return tagString != null && Boolean.parseBoolean(tagString);
105    }
106 
107    private void generateCoverageReport() {
108        if (LOGD)
109            Log.d(TAG, "generateCoverageReport()");
110 
111        java.io.File coverageFile = new java.io.File(getCoverageFilePath());
112 
113        // We may use this if we want to avoid reflection and we include
114        // emma.jar
115        // RT.dumpCoverageData(coverageFile, false, false);
116 
117        // Use reflection to call emma dump coverage method, to avoid
118        // always statically compiling against emma jar
119        try {
120            Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
121            Method dumpCoverageMethod = emmaRTClass.getMethod(
122                    "dumpCoverageData", coverageFile.getClass(), boolean.class,
123                    boolean.class);
124            dumpCoverageMethod.invoke(null, coverageFile, false, false);
125        } catch (ClassNotFoundException e) {
126            reportEmmaError("Is emma jar on classpath?", e);
127        } catch (SecurityException e) {
128            reportEmmaError(e);
129        } catch (NoSuchMethodException e) {
130            reportEmmaError(e);
131        } catch (IllegalArgumentException e) {
132            reportEmmaError(e);
133        } catch (IllegalAccessException e) {
134            reportEmmaError(e);
135        } catch (InvocationTargetException e) {
136            reportEmmaError(e);
137        }
138    }
139 
140    private String getCoverageFilePath() {
141        if (mCoverageFilePath == null) {
142            return DEFAULT_COVERAGE_FILE_PATH;
143        } else {
144            return mCoverageFilePath;
145        }
146    }
147 
148    private void reportEmmaError(Exception e) {
149        reportEmmaError("", e);
150    }
151 
152    private void reportEmmaError(String hint, Exception e) {
153        String msg = "Failed to generate emma coverage. " + hint;
154        Log.e(TAG, msg, e);
155        mResults.putString(Instrumentation.REPORT_KEY_STREAMRESULT, "\nError: "
156                + msg);
157    }
158 
159    /* (non-Javadoc)
160     * @see com.example.instrumentation.FinishListener#onActivityFinished()
161     */
162    @Override
163    public void onActivityFinished() {
164        if (LOGD)
165            Log.d(TAG, "onActivityFinished()");
166        if (mCoverage) {
167            generateCoverageReport();
168        }
169        finish(Activity.RESULT_OK, mResults);
170    }
171 
172}

[all classes][com.example.instrumentation]
EMMA 2.0.5312 (C) Vladimir Roubtsov