Wait for emulator to boot before running tests

We should wait for the emulator to finish booting before running tests
to avoid flakes like:
https://travis-ci.org/catapult-project/perfetto/builds/287228461

Bug: 67765711
Change-Id: I033a864d441f9e12d1aa4284aff385f44d50473a
diff --git a/build/run_android_test b/build/run_android_test
index d9eb495..e991ed7 100755
--- a/build/run_android_test
+++ b/build/run_android_test
@@ -18,6 +18,7 @@
 import re
 import subprocess
 import sys
+import time
 
 
 """ Runs a test executable on Android.
@@ -37,6 +38,29 @@
   return subprocess.check_call(cmd)
 
 
+def GetProp(prop):
+  cmd = [ADB_PATH, 'shell', 'getprop', prop]
+  print '> adb ' + ' '.join(cmd)
+  output = subprocess.check_output(cmd)
+  lines = output.splitlines()
+  assert len(lines) == 1
+  print lines[0]
+  return lines[0]
+
+
+def BootCompleted():
+  return GetProp('sys.boot_completed') == '1'
+
+
+def ExponentialBackOff(f):
+  delay_seconds = 1
+  while True:
+    if f():
+      return
+    time.sleep(delay_seconds)
+    delay_seconds *= 2
+
+
 def Main():
   parser = argparse.ArgumentParser()
   parser.add_argument('--no-cleanup', '-n', action='store_true')
@@ -49,6 +73,8 @@
 
   print 'Waiting for device ...'
   AdbCall('wait-for-device')
+  ExponentialBackOff(BootCompleted)
+
   target_dir = '/data/local/tmp/' + args.test_name
   AdbCall('shell', 'rm -rf "%s"; mkdir -p "%s"' % (2 * (target_dir,)))
   AdbCall('push', test_bin, target_dir)