Workaround for package-private framework tests in runtest.

Checks if the test to run is a framework test based on path, and if so
disables the davlik verifier by writing to /data/local.prop and
rebooting device.

Change-Id: Ibf94fb921662b5ddf6136dd12360d5de83a3a284
diff --git a/testrunner/adb_interface.py b/testrunner/adb_interface.py
index ea9188c..451d046 100755
--- a/testrunner/adb_interface.py
+++ b/testrunner/adb_interface.py
@@ -148,6 +148,19 @@
       return False
     return True
 
+  def EnableAdbRoot(self):
+    """Enable adb root on device."""
+    output = self.SendCommand("root")
+    if "adbd is already running as root" in output:
+      return True
+    elif "restarting adbd as root" in output:
+      # device will disappear from adb, wait for it to come back
+      self.SendCommand("wait-for-device")
+      return True
+    else:
+      logger.Log("Unrecognized output from adb root: %s" % output)
+      return False
+
   def StartInstrumentationForPackage(
       self, package_name, runner_name, timeout_time=60*10,
       no_window_animation=False, instrumentation_args={}):
diff --git a/testrunner/runtest.py b/testrunner/runtest.py
index 5bea81a..4a86a63 100755
--- a/testrunner/runtest.py
+++ b/testrunner/runtest.py
@@ -70,6 +70,8 @@
   # default value for make -jX
   _DEFAULT_JOBS = 4
 
+  _DALVIK_VERIFIER_OFF_PROP = "dalvik.vm.dexopt-flags = v=n"
+
   def __init__(self):
     # disable logging of timestamp
     self._root_path = android_build.GetTop()
@@ -227,6 +229,10 @@
     for test_suite in tests:
       self._AddBuildTarget(test_suite, target_set, extra_args_set)
 
+    if not self._options.preview:
+      self._adb.EnableAdbRoot()
+    else:
+      logger.Log("adb root")
     rebuild_libcore = False
     if target_set:
       if self._options.coverage:
@@ -258,6 +264,8 @@
           os.chdir(self._root_path)
           run_command.RunCommand(cmd, return_output=False)
           os.chdir(old_dir)
+      # turn off dalvik verifier if necessary
+      self._TurnOffVerifier(tests)
       target_build_string = " ".join(list(target_set))
       extra_args_string = " ".join(list(extra_args_set))
       # mmm cannot be used from python, so perform a similar operation using
@@ -328,6 +336,36 @@
         return True
     return False
 
+  def _TurnOffVerifier(self, test_list):
+    """Turn off the dalvik verifier if needed by given tests.
+
+    If one or more tests needs dalvik verifier off, and it is not already off,
+    turns off verifier and reboots device to allow change to take effect.
+    """
+    # hack to check if these are framework/base tests. If so, turn off verifier
+    # to allow framework tests to access package-private framework api
+    framework_test = False
+    for test in test_list:
+      if os.path.commonprefix([test.GetBuildPath(), "frameworks/base"]):
+        framework_test = True
+    if framework_test:
+      # check if verifier is off already - to avoid the reboot if not
+      # necessary
+      output = self._adb.SendShellCommand("cat /data/local.prop")
+      if not self._DALVIK_VERIFIER_OFF_PROP in output:
+        if self._options.preview:
+          logger.Log("adb shell \"echo %s >> /data/local.prop\""
+                     % self._DALVIK_VERIFIER_OFF_PROP)
+          logger.Log("adb reboot")
+          logger.Log("adb wait-for-device")
+        else:
+          logger.Log("Turning off dalvik verifier and rebooting")
+          self._adb.SendShellCommand("\"echo %s >> /data/local.prop\""
+                                     % self._DALVIK_VERIFIER_OFF_PROP)
+          self._adb.SendCommand("reboot")
+          self._adb.SendCommand("wait-for-device", timeout_time=60,
+                                retry_count=3)
+
   def RunTests(self):
     """Main entry method - executes the tests according to command line args."""
     try: