Created
April 17, 2012 10:39
-
-
Save charroch/2405203 to your computer and use it in GitHub Desktop.
SpecInstrumentationRunner on android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.scalatest.tools | |
import org.scalatest._ | |
import dalvik.system.DexFile | |
import java.io.File | |
import android.os.{Looper, Bundle} | |
import android.test.{AndroidTestCase, InstrumentationTestCase} | |
import scala.collection.JavaConversions._ | |
import android.app.{Activity, Instrumentation} | |
class SpecRunner extends SpecRunnerComponent with DefaultInstrumentationReporter | |
abstract class SpecRunnerComponent extends Instrumentation with InstrumentationReporter { | |
override def onCreate(arguments: Bundle) { | |
super.onCreate(arguments); | |
start() | |
} | |
override def onStart() { | |
Looper.prepare() | |
val dexFile = new DexFile(new File(getContext.getApplicationInfo.publicSourceDir)); | |
dexFile.entries() | |
.withFilter(isSpec) | |
.withFilter(isSuite) | |
.map(asSuite) | |
.map(injectContext) | |
.map(injectInstrumentation) | |
.foreach(run) | |
finish(Activity.RESULT_OK, new Bundle()) | |
} | |
override def onException(obj: Object, e: Throwable) = { | |
println("TEST ecetion " + e) | |
super.onException(obj, e) | |
} | |
def run(s: Suite) { | |
s.run(None, this.reporter, new Stopper {}, Filter(), Map(), None, new Tracker) | |
} | |
def isSuite(klass: String) = { | |
classOf[Suite].isAssignableFrom(getContext.getClassLoader.loadClass(klass)) | |
} | |
def asSuite(klass: String): Suite = { | |
getContext.getClassLoader.loadClass(klass).newInstance().asInstanceOf[Suite] | |
} | |
def isSpec(klass: String): Boolean = { | |
klass.endsWith("Spec") || klass.endsWith("Specs") | |
} | |
def injectInstrumentation(s: Suite) = { | |
if (classOf[InstrumentationTestCase].isAssignableFrom(s.getClass)) { | |
s.asInstanceOf[InstrumentationTestCase].injectInsrumentation(this) | |
} | |
s | |
} | |
def injectContext(s: Suite) = { | |
if (classOf[AndroidTestCase].isAssignableFrom(s.getClass)) { | |
s.asInstanceOf[AndroidTestCase].setContext(this.getTargetContext) | |
// | |
// s.asInstanceOf[ { | |
// def setTestContext(c: Context): Unit | |
// }].setTestContext(this.getTargetContext) | |
} | |
s | |
} | |
} | |
trait InstrumentationReporter { | |
i: Instrumentation => | |
def reporter: Reporter | |
} | |
trait DefaultInstrumentationReporter extends InstrumentationReporter { | |
i: Instrumentation => | |
def reporter = new SimpleInstrumentationReporter(i) | |
} | |
class SimpleInstrumentationReporter(inst: Instrumentation) extends StringReporter(false, false, false, false) { | |
final val ansiReset = "\033[0m" | |
protected def printPossiblyInColor(text: String, ansiColor: String) = { | |
val mTestResult = new Bundle(); | |
mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, ansiColor + text + ansiReset + '\n') | |
inst.sendStatus(0, mTestResult) | |
} | |
def dispose() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment