def __init__(self, test_options, device, shard_index, test_pkg, ports_to_forward, additional_flags=None): """Create a new TestRunner. Args: test_options: An InstrumentationOptions object. device: Attached android device. shard_index: Shard index. test_pkg: A TestPackage object. ports_to_forward: A list of port numbers for which to set up forwarders. Can be optionally requested by a test case. additional_flags: A list of additional flags to add to the command line. """ super(TestRunner, self).__init__(device, test_options.tool, test_options.push_deps, test_options.cleanup_test_files) self._lighttp_port = constants.LIGHTTPD_RANDOM_PORT_FIRST + shard_index self.options = test_options self.test_pkg = test_pkg self.ports_to_forward = ports_to_forward self.coverage_dir = test_options.coverage_dir # Use the correct command line file for the package under test. cmdline_file = [a.cmdline_file for a in constants.PACKAGE_INFO.itervalues() if a.test_package == self.test_pkg.GetPackageName()] assert len(cmdline_file) < 2, 'Multiple packages have the same test package' if len(cmdline_file) and cmdline_file[0]: self.flags = flag_changer.FlagChanger(self.adb, cmdline_file[0]) else: self.flags = flag_changer.FlagChanger(self.adb) if additional_flags: self.flags.AddFlags(additional_flags)
def main(argv): option_parser = optparse.OptionParser() option_parser.add_option('-l', '--low', help='Simulate Activity#onLowMemory()', action='store_true') option_parser.add_option( '-t', '--trim', help=('Simulate Activity#onTrimMemory(...) with ' + ', '.join(ACTION_TRIM.keys())), type='string') option_parser.add_option('-b', '--browser', default=DEFAULT_BROWSER, help=('Which browser to use. One of ' + ', '.join(constants.PACKAGE_INFO.keys()) + ' [default: %default]'), type='string') (options, args) = option_parser.parse_args(argv) if len(args) > 1: print 'Unknown argument: ', args[1:] option_parser.print_help() sys.exit(1) if options.low and options.trim: option_parser.error('options --low and --trim are mutually exclusive') if not options.low and not options.trim: option_parser.print_help() sys.exit(1) action = None if options.low: action = ACTION_LOW elif options.trim in ACTION_TRIM.keys(): action = ACTION_TRIM[options.trim] if action is None: option_parser.print_help() sys.exit(1) if not options.browser in constants.PACKAGE_INFO.keys(): option_parser.error('Unknown browser option ' + options.browser) package_info = constants.PACKAGE_INFO[options.browser] package = package_info.package activity = package_info.activity adb = android_commands.AndroidCommands(device=None) adb.EnableAdbRoot() flags = flag_changer.FlagChanger(adb, package_info.cmdline_file) if ENABLE_TEST_INTENTS_FLAG not in flags.Get(): flags.AddFlags([ENABLE_TEST_INTENTS_FLAG]) adb.StartActivity(package, activity, action=action)
def individual_device_set_up(dev, host_device_tuples): dev.Install(self._test_instance.apk_under_test) dev.Install(self._test_instance.test_apk) external_storage = dev.GetExternalStoragePath() host_device_tuples = [ (h, substitute_external_storage(d, external_storage)) for h, d in host_device_tuples ] logging.info('instrumentation data deps:') for h, d in host_device_tuples: logging.info('%r -> %r', h, d) dev.PushChangedFiles(host_device_tuples) if self._test_instance.flags: if not self._test_instance.package_info: logging.error("Couldn't set flags: no package info") elif not self._test_instance.package_info.cmdline_file: logging.error("Couldn't set flags: no cmdline_file") else: self._flag_changers[str(dev)] = flag_changer.FlagChanger( dev, self._test_instance.package_info.cmdline_file) logging.debug('Attempting to set flags: %r', self._test_instance.flags) self._flag_changers[str(dev)].AddFlags( self._test_instance.flags)
def __init__(self, test_options, device, shard_index, test_pkg, additional_flags=None): """Create a new TestRunner. Args: test_options: An InstrumentationOptions object. device: Attached android device. shard_index: Shard index. test_pkg: A TestPackage object. additional_flags: A list of additional flags to add to the command line. """ super(TestRunner, self).__init__(device, test_options.tool) self._lighttp_port = constants.LIGHTTPD_RANDOM_PORT_FIRST + shard_index self._logcat_monitor = None self.coverage_device_file = None self.coverage_dir = test_options.coverage_dir self.coverage_host_file = None self.options = test_options self.test_pkg = test_pkg # Use the correct command line file for the package under test. cmdline_file = [a.cmdline_file for a in constants.PACKAGE_INFO.itervalues() if a.test_package == self.test_pkg.GetPackageName()] assert len(cmdline_file) < 2, 'Multiple packages have the same test package' if len(cmdline_file) and cmdline_file[0]: self.flags = flag_changer.FlagChanger(self.device, cmdline_file[0]) if additional_flags: self.flags.AddFlags(additional_flags) else: self.flags = None
def __init__(self, test_options, device, shard_index, test_pkg): """Create a new TestRunner. Args: test_options: A UIAutomatorOptions object. device: Attached android device. shard_index: Shard index. test_pkg: A TestPackage object. """ # Create an InstrumentationOptions object to pass to the super class instrumentation_options = instr_test_options.InstrumentationOptions( test_options.tool, test_options.cleanup_test_files, test_options.push_deps, test_options.annotations, test_options.exclude_annotations, test_options.test_filter, test_options.test_data, test_options.save_perf_json, test_options.screenshot_failures, wait_for_debugger=False, coverage_dir=None, test_apk=None, test_apk_path=None, test_apk_jar_path=None) super(TestRunner, self).__init__(instrumentation_options, device, shard_index, test_pkg) cmdline_file = constants.PACKAGE_INFO[ test_options.package].cmdline_file self.flags = None if cmdline_file: self.flags = flag_changer.FlagChanger(self.adb, cmdline_file) self._package = constants.PACKAGE_INFO[test_options.package].package self._activity = constants.PACKAGE_INFO[test_options.package].activity
def __init__(self, device, package_info, cold, url): self._device = device self._package_info = package_info self._cold = cold self._logcat_monitor = self._device.GetLogcatMonitor() self._url = url self._trace_file = None self._trace_finish_re = re.compile( r' Completed startup tracing to (.*)') self._flag_changer = flag_changer.FlagChanger( self._device, self._package_info.cmdline_file)
def _SetupTracing(self): # TODO(lizeb): Figure out how to clean up the command-line file when # _TearDownTracing() is not executed in StopTracing(). changer = flag_changer.FlagChanger(self._device, self._package_info.cmdline_file) changer.AddFlags(['--trace-startup']) self._device.ForceStop(self._package_info.package) if self._cold: self._device.EnableRoot() cache_control.CacheControl(self._device).DropRamCaches() self._device.StartActivity(intent.Intent( package=self._package_info.package, activity=self._package_info.activity, data=self._url, extras={'create_new_tab': True}), blocking=True)
def FlagChanger(device, command_line_path, new_flags): """Changes the flags in a context, restores them afterwards. Args: device: Device to target, from DeviceUtils. command_line_path: Full path to the command-line file. new_flags: Flags to add. """ # If we're logging requests from a local desktop chrome instance there is no # device. if not device: yield return changer = flag_changer.FlagChanger(device, command_line_path) changer.AddFlags(new_flags) try: yield finally: changer.Restore()
def _StartForwarder(self): """Sets up forwarding of device ports to the host, and configures chrome to use those ports. """ if not self._wpr_server: logging.warning('No host WPR server to forward to.') return print 'Starting device forwarder...' forwarder.Forwarder.Map([(0, self._host_http_port), (0, self._host_https_port)], self._device) device_http = forwarder.Forwarder.DevicePortForHostPort( self._host_http_port) device_https = forwarder.Forwarder.DevicePortForHostPort( self._host_https_port) self._flag_changer = flag_changer.FlagChanger( self._device, self._cmdline_file) self._flag_changer.AddFlags([ '--host-resolver-rules="MAP * 127.0.0.1,EXCLUDE localhost"', '--testing-fixed-http-port=%s' % device_http, '--testing-fixed-https-port=%s' % device_https])
devil_chromium.Initialize() package_info = constants.PACKAGE_INFO[options.browser] package = package_info.package activity = package_info.activity devices = device_utils.DeviceUtils.HealthyDevices() if not devices: raise device_errors.NoDevicesError() elif len(devices) > 1: logging.warning('Multiple devices attached. Using %s.', str(devices[0])) device = devices[0] try: device.EnableRoot() except device_errors.CommandFailedError as e: # Try to change the flags and start the activity anyway. # TODO(jbudorick) Handle this exception appropriately after interface # conversions are finished. logging.error(str(e)) flags = flag_changer.FlagChanger(device, package_info.cmdline_file) flags.AddFlags([ENABLE_TEST_INTENTS_FLAG]) device.StartActivity(intent.Intent(package=package, activity=activity, action=action)) if __name__ == '__main__': sys.exit(main(sys.argv))
def _SetChromeFlags(self, package_info): print 'Setting Chrome flags...' changer = flag_changer.FlagChanger( self._device, package_info.cmdline_file) changer.AddFlags(['--no-sandbox', '--disable-fre']) return changer
def main(argv): option_parser = optparse.OptionParser() option_parser.add_option('-l', '--low', help='Simulate Activity#onLowMemory()', action='store_true') option_parser.add_option( '-t', '--trim', help=('Simulate Activity#onTrimMemory(...) with ' + ', '.join(ACTION_TRIM.keys())), type='string') option_parser.add_option('-b', '--browser', default=DEFAULT_BROWSER, help=('Which browser to use. One of ' + ', '.join(constants.PACKAGE_INFO.keys()) + ' [default: %default]'), type='string') (options, args) = option_parser.parse_args(argv) if len(args) > 1: print 'Unknown argument: ', args[1:] option_parser.print_help() sys.exit(1) if options.low and options.trim: option_parser.error('options --low and --trim are mutually exclusive') if not options.low and not options.trim: option_parser.print_help() sys.exit(1) action = None if options.low: action = ACTION_LOW elif options.trim in ACTION_TRIM.keys(): action = ACTION_TRIM[options.trim] if action is None: option_parser.print_help() sys.exit(1) if not options.browser in constants.PACKAGE_INFO.keys(): option_parser.error('Unknown browser option ' + options.browser) package_info = constants.PACKAGE_INFO[options.browser] package = package_info.package activity = package_info.activity devices = device_utils.DeviceUtils.HealthyDevices() if not devices: raise device_errors.NoDevicesError() elif len(devices) > 1: logging.warning('Multiple devices attached. Using %s.', str(devices[0])) device = devices[0] try: device.EnableRoot() except device_errors.CommandFailedError as e: # Try to change the flags and start the activity anyway. # TODO(jbudorick) Handle this exception appropriately after interface # conversions are finished. logging.error(str(e)) flags = flag_changer.FlagChanger(device, package_info.cmdline_file) if ENABLE_TEST_INTENTS_FLAG not in flags.Get(): flags.AddFlags([ENABLE_TEST_INTENTS_FLAG]) device.StartActivity( intent.Intent(package=package, activity=activity, action=action))
def _CreateFlagChangerIfNeeded(self, device): if not str(device) in self._flag_changers: self._flag_changers[str(device)] = flag_changer.FlagChanger( device, self._test_instance.package_info.cmdline_file)
def _TearDownTracing(self): changer = flag_changer.FlagChanger(self._device, self._package_info.cmdline_file) changer.RemoveFlags(['--trace-startup'])