Home | History | Annotate | Download | only in assets
      1 #!/usr/bin/python
      2 
      3 """Run page cycler tests using Android instrumentation.
      4 
      5   First, you need to get an SD card or sdcard image that has page cycler tests.
      6 
      7   Usage:
      8     Run a single page cycler test:
      9       run_page_cycler.py "file:///sdcard/android/page_cycler/moz/start.html?auto=1\&iterations=10"
     10 """
     11 
     12 import logging
     13 import optparse
     14 import os
     15 import subprocess
     16 import sys
     17 import time
     18 
     19 
     20 
     21 def main(options, args):
     22   """Run the tests. Will call sys.exit when complete.
     23 
     24   """
     25 
     26   # Set up logging format.
     27   log_level = logging.INFO
     28   if options.verbose:
     29     log_level = logging.DEBUG
     30   logging.basicConfig(level=log_level,
     31                       format='%(message)s')
     32 
     33   # Include all tests if none are specified.
     34   if not args:
     35     print "need a URL, e.g. file:///sdcard/android/page_cycler/moz/start.html"
     36     sys.exit(1)
     37   else:
     38     path = ' '.join(args);
     39 
     40   adb_cmd = "adb ";
     41   if options.adb_options:
     42     adb_cmd += options.adb_options
     43 
     44   logging.info("Running the test ...")
     45 
     46   # Count crashed tests.
     47   crashed_tests = []
     48 
     49   timeout_ms = '0'
     50   if options.time_out_ms:
     51     timeout_ms = options.time_out_ms
     52 
     53   # Run test until it's done
     54 
     55   run_load_test_cmd_prefix = adb_cmd + " shell am instrument"
     56   run_load_test_cmd_postfix = " -w com.android.dumprendertree/.LayoutTestsAutoRunner"
     57 
     58   # Call LoadTestsAutoTest::runTest.
     59   run_load_test_cmd = run_load_test_cmd_prefix + " -e class com.android.dumprendertree.LoadTestsAutoTest#runPageCyclerTest -e path \"" + path + "\" -e timeout " + timeout_ms
     60 
     61   if options.drawtime:
     62     run_load_test_cmd += " -e drawtime true "
     63 
     64   if options.save_image:
     65     run_load_test_cmd += " -e saveimage \"%s\"" % options.save_image
     66 
     67   run_load_test_cmd += run_load_test_cmd_postfix
     68 
     69   (adb_output, adb_error) = subprocess.Popen(run_load_test_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
     70   fail_flag = False
     71   for line in adb_output.splitlines():
     72     line = line.strip()
     73     if line.find('INSTRUMENTATION_CODE') == 0:
     74       if not line[22:] == '-1':
     75         fail_flag = True
     76         break
     77     if (line.find('INSTRUMENTATION_FAILED') != -1 or
     78         line.find('Process crashed.') != -1):
     79       fail_flag = True
     80       break
     81   if fail_flag:
     82     logging.error("Error happened : " + adb_output)
     83     sys.exit(1)
     84 
     85   logging.info(adb_output);
     86   logging.info(adb_error);
     87   logging.info("Done\n");
     88 
     89   # Pull results from /sdcard/load_test_result.txt
     90   results_dir = options.results_directory
     91   if not os.path.exists(results_dir):
     92     os.makedirs(results_dir)
     93   if not os.path.isdir(results_dir):
     94     logging.error("Cannot create results dir: " + results_dir)
     95     sys.exit(1)
     96 
     97   result_file = "/sdcard/load_test_result.txt"
     98   shell_cmd_str = adb_cmd + " pull " + result_file + " " + results_dir
     99   (adb_output, err) = subprocess.Popen(
    100       shell_cmd_str, shell=True,
    101       stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    102   if not os.path.isfile(os.path.join(results_dir, "load_test_result.txt")):
    103     logging.error("Failed to pull result file.")
    104     logging.error("adb stdout:")
    105     logging.error(adb_output)
    106     logging.error("adb stderr:")
    107     logging.error(err)
    108   logging.info("Results are stored under: " + results_dir + "/load_test_result.txt\n")
    109 
    110 if '__main__' == __name__:
    111   option_parser = optparse.OptionParser()
    112   option_parser.add_option("-t", "--time-out-ms",
    113                            default=None,
    114                            help="set the timeout for each test")
    115   option_parser.add_option("-v", "--verbose", action="store_true",
    116                            default=False,
    117                            help="include debug-level logging")
    118   option_parser.add_option("-a", "--adb-options",
    119                            default=None,
    120                            help="pass options to adb, such as -d -e, etc");
    121   option_parser.add_option("-r", "--results-directory",
    122                            default="layout-test-results",
    123                            help="directory which results are stored.")
    124 
    125   option_parser.add_option("-d", "--drawtime", action="store_true",
    126                            default=False,
    127                            help="log draw time for each page rendered.")
    128 
    129   option_parser.add_option("-s", "--save-image",
    130                            default=None,
    131                            help="stores rendered page to a location on device.")
    132 
    133   options, args = option_parser.parse_args();
    134   main(options, args)
    135