| 1 | package kdk.android.simplydo.EmmaInstrument; |
| 2 | |
| 3 | import java.lang.reflect.InvocationTargetException; |
| 4 | import java.lang.reflect.Method; |
| 5 | |
| 6 | import android.app.Activity; |
| 7 | import android.app.Instrumentation; |
| 8 | import android.content.Intent; |
| 9 | import android.os.Bundle; |
| 10 | import android.os.Looper; |
| 11 | import android.util.Log; |
| 12 | |
| 13 | public class EmmaInstrumentation extends Instrumentation implements |
| 14 | FinishListener { |
| 15 | public static String TAG = "EmmaInstrumentation:"; |
| 16 | private static final String DEFAULT_COVERAGE_FILE_PATH = "/mnt/sdcard/coverage.ec"; |
| 17 | |
| 18 | private final Bundle mResults = new Bundle(); |
| 19 | |
| 20 | private Intent mIntent; |
| 21 | private static final boolean LOGD = true; |
| 22 | |
| 23 | private boolean mCoverage = true; |
| 24 | |
| 25 | private String mCoverageFilePath; |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * Constructor |
| 30 | */ |
| 31 | public EmmaInstrumentation() { |
| 32 | |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public void onCreate(Bundle arguments) { |
| 37 | Log.d(TAG, "onCreate(" + arguments + ")"); |
| 38 | super.onCreate(arguments); |
| 39 | |
| 40 | if (arguments != null) { |
| 41 | mCoverage = getBooleanArgument(arguments, "coverage"); |
| 42 | mCoverageFilePath = arguments.getString("coverageFile"); |
| 43 | } |
| 44 | |
| 45 | mIntent = new Intent(getTargetContext(), InstrumentedActivity.class); |
| 46 | mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 47 | start(); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void onStart() { |
| 52 | if (LOGD) |
| 53 | Log.d(TAG, "onStart()"); |
| 54 | super.onStart(); |
| 55 | |
| 56 | Looper.prepare(); |
| 57 | InstrumentedActivity activity = (InstrumentedActivity) startActivitySync(mIntent); |
| 58 | activity.setFinishListener(this); |
| 59 | } |
| 60 | |
| 61 | private boolean getBooleanArgument(Bundle arguments, String tag) { |
| 62 | String tagString = arguments.getString(tag); |
| 63 | return tagString != null && Boolean.parseBoolean(tagString); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | private void generateCoverageReport() { |
| 68 | if (LOGD) |
| 69 | Log.d(TAG, "generateCoverageReport()"); |
| 70 | |
| 71 | java.io.File coverageFile = new java.io.File(getCoverageFilePath()); |
| 72 | |
| 73 | // We may use this if we want to avoid refecltion and we include |
| 74 | // emma.jar |
| 75 | // RT.dumpCoverageData(coverageFile, false, false); |
| 76 | |
| 77 | // Use reflection to call emma dump coverage method, to avoid |
| 78 | // always statically compiling against emma jar |
| 79 | try { |
| 80 | Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT"); |
| 81 | Method dumpCoverageMethod = emmaRTClass.getMethod( |
| 82 | "dumpCoverageData", coverageFile.getClass(), boolean.class, |
| 83 | boolean.class); |
| 84 | dumpCoverageMethod.invoke(null, coverageFile, true, false); |
| 85 | } catch (ClassNotFoundException e) { |
| 86 | reportEmmaError("Emma.jar not in the class path?", e); |
| 87 | } catch (SecurityException e) { |
| 88 | reportEmmaError(e); |
| 89 | } catch (NoSuchMethodException e) { |
| 90 | reportEmmaError(e); |
| 91 | } catch (IllegalArgumentException e) { |
| 92 | reportEmmaError(e); |
| 93 | } catch (IllegalAccessException e) { |
| 94 | reportEmmaError(e); |
| 95 | } catch (InvocationTargetException e) { |
| 96 | reportEmmaError(e); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private String getCoverageFilePath() { |
| 101 | if (mCoverageFilePath == null) { |
| 102 | return DEFAULT_COVERAGE_FILE_PATH; |
| 103 | } else { |
| 104 | return mCoverageFilePath; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private boolean setCoverageFilePath(String filePath){ |
| 109 | if(filePath != null && filePath.length() > 0) { |
| 110 | mCoverageFilePath = filePath; |
| 111 | return true; |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | private void reportEmmaError(Exception e) { |
| 117 | reportEmmaError("", e); |
| 118 | } |
| 119 | |
| 120 | private void reportEmmaError(String hint, Exception e) { |
| 121 | String msg = "Failed to generate emma coverage. " + hint; |
| 122 | Log.e(TAG, msg, e); |
| 123 | mResults.putString(Instrumentation.REPORT_KEY_STREAMRESULT, "\nError: " |
| 124 | + msg); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onActivityFinished() { |
| 129 | if (LOGD) |
| 130 | Log.d(TAG, "onActivityFinished()"); |
| 131 | if (mCoverage) { |
| 132 | generateCoverageReport(); |
| 133 | } |
| 134 | finish(Activity.RESULT_OK, mResults); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void dumpIntermediateCoverage(String filePath){ |
| 139 | // TODO Auto-generated method stub |
| 140 | if(LOGD){ |
| 141 | Log.d(TAG,"Intermidate Dump Called with file name :"+ filePath); |
| 142 | } |
| 143 | if(mCoverage){ |
| 144 | if(!setCoverageFilePath(filePath)){ |
| 145 | if(LOGD){ |
| 146 | Log.d(TAG,"Unable to set the given file path:"+filePath+" as dump target."); |
| 147 | } |
| 148 | } |
| 149 | generateCoverageReport(); |
| 150 | setCoverageFilePath(DEFAULT_COVERAGE_FILE_PATH); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } |