| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 """Brings in Chrome Android's android_commands module, which itself is a | |
| 5 thin(ish) wrapper around adb.""" | |
| 6 import os | |
| 7 import sys | |
| 8 | |
| 9 # This is currently a thin wrapper around Chrome Android's | |
| 10 # build scripts, located in chrome/build/android. This file exists mainly to | |
| 11 # deal with locating the module. | |
| 12 | |
| 13 # Get build/android scripts into our path. | |
| 14 sys.path.append( | |
| 15 os.path.abspath( | |
| 16 os.path.join(os.path.dirname(__file__), | |
| 17 '../../../build/android'))) | |
| 18 try: | |
| 19 from pylib import android_commands # pylint: disable=F0401 | |
| 20 from pylib import cmd_helper # pylint: disable=F0401 | |
| 21 from pylib import forwarder # pylint: disable=F0401 | |
| 22 from pylib import ports # pylint: disable=F0401 | |
| 23 from pylib import valgrind_tools # pylint: disable=F0401 | |
| 24 except Exception: | |
| 25 android_commands = None | |
| 26 | |
| 27 | |
| 28 def IsAndroidSupported(): | |
| 29 return android_commands != None | |
| 30 | |
| 31 | |
| 32 def GetAttachedDevices(): | |
| 33 """Returns a list of attached, online android devices. | |
| 34 | |
| 35 If a preferred device has been set with ANDROID_SERIAL, it will be first in | |
| 36 the returned list.""" | |
| 37 return android_commands.GetAttachedDevices() | |
| 38 | |
| 39 | |
| 40 def AllocateTestServerPort(): | |
| 41 return ports.AllocateTestServerPort() | |
| 42 | |
| 43 | |
| 44 def ResetTestServerPortAllocation(): | |
| 45 return ports.ResetTestServerPortAllocation() | |
| 46 | |
| 47 | |
| 48 def GetOutDirectory(): | |
| 49 return cmd_helper.OutDirectory.get() | |
| 50 | |
| 51 | |
| 52 class AdbCommands(object): | |
| 53 """A thin wrapper around ADB""" | |
| 54 | |
| 55 def __init__(self, device): | |
| 56 self._adb = android_commands.AndroidCommands(device) | |
| 57 | |
| 58 def Adb(self): | |
| 59 return self._adb | |
| 60 | |
| 61 def Forward(self, local, remote): | |
| 62 ret = self._adb.Adb().SendCommand('forward %s %s' % (local, remote)) | |
| 63 assert ret == '' | |
| 64 | |
| 65 def RunShellCommand(self, command, timeout_time=20, log_result=False): | |
| 66 """Send a command to the adb shell and return the result. | |
| 67 | |
| 68 Args: | |
| 69 command: String containing the shell command to send. Must not include | |
| 70 the single quotes as we use them to escape the whole command. | |
| 71 timeout_time: Number of seconds to wait for command to respond before | |
| 72 retrying, used by AdbInterface.SendShellCommand. | |
| 73 log_result: Boolean to indicate whether we should log the result of the | |
| 74 shell command. | |
| 75 | |
| 76 Returns: | |
| 77 list containing the lines of output received from running the command | |
| 78 """ | |
| 79 return self._adb.RunShellCommand(command, timeout_time, log_result) | |
| 80 | |
| 81 def CloseApplication(self, package): | |
| 82 """Attempt to close down the application, using increasing violence. | |
| 83 | |
| 84 Args: | |
| 85 package: Name of the process to kill off, e.g. | |
| 86 com.google.android.apps.chrome | |
| 87 """ | |
| 88 self._adb.CloseApplication(package) | |
| 89 | |
| 90 def KillAll(self, process): | |
| 91 """Android version of killall, connected via adb. | |
| 92 | |
| 93 Args: | |
| 94 process: name of the process to kill off | |
| 95 | |
| 96 Returns: | |
| 97 the number of processess killed | |
| 98 """ | |
| 99 return self._adb.KillAll(process) | |
| 100 | |
| 101 def ExtractPid(self, process_name): | |
| 102 """Extracts Process Ids for a given process name from Android Shell. | |
| 103 | |
| 104 Args: | |
| 105 process_name: name of the process on the device. | |
| 106 | |
| 107 Returns: | |
| 108 List of all the process ids (as strings) that match the given name. | |
| 109 If the name of a process exactly matches the given name, the pid of | |
| 110 that process will be inserted to the front of the pid list. | |
| 111 """ | |
| 112 return self._adb.ExtractPid(process_name) | |
| 113 | |
| 114 def StartActivity(self, package, activity, wait_for_completion=False, | |
| 115 action='android.intent.action.VIEW', | |
| 116 category=None, data=None, | |
| 117 extras=None, trace_file_name=None): | |
| 118 """Starts |package|'s activity on the device. | |
| 119 | |
| 120 Args: | |
| 121 package: Name of package to start (e.g. 'com.google.android.apps.chrome'). | |
| 122 activity: Name of activity (e.g. '.Main' or | |
| 123 'com.google.android.apps.chrome.Main'). | |
| 124 wait_for_completion: wait for the activity to finish launching (-W flag). | |
| 125 action: string (e.g. 'android.intent.action.MAIN'). Default is VIEW. | |
| 126 category: string (e.g. 'android.intent.category.HOME') | |
| 127 data: Data string to pass to activity (e.g. 'http://www.example.com/'). | |
| 128 extras: Dict of extras to pass to activity. Values are significant. | |
| 129 trace_file_name: If used, turns on and saves the trace to this file name. | |
| 130 """ | |
| 131 return self._adb.StartActivity(package, activity, wait_for_completion, | |
| 132 action, | |
| 133 category, data, | |
| 134 extras, trace_file_name) | |
| 135 | |
| 136 def Push(self, local, remote): | |
| 137 return self._adb.Adb().Push(local, remote) | |
| 138 | |
| 139 def Pull(self, remote, local): | |
| 140 return self._adb.Adb().Pull(remote, local) | |
| 141 | |
| 142 def FileExistsOnDevice(self, file_name): | |
| 143 return self._adb.FileExistsOnDevice(file_name) | |
| 144 | |
| 145 def IsRootEnabled(self): | |
| 146 return self._adb.IsRootEnabled() | |
| 147 | |
| 148 def HasForwarder(buildtype=None): | |
| 149 if not buildtype: | |
| 150 return (HasForwarder(buildtype='Release') or | |
| 151 HasForwarder(buildtype='Debug')) | |
| 152 return (os.path.exists(os.path.join(GetOutDirectory(), buildtype, | |
| 153 'device_forwarder')) and | |
| 154 os.path.exists(os.path.join(GetOutDirectory(), buildtype, | |
| 155 'host_forwarder'))) | |
| 156 | |
| 157 class Forwarder(object): | |
| 158 def __init__(self, adb, *port_pairs): | |
| 159 assert HasForwarder() | |
| 160 tool = valgrind_tools.BaseTool() | |
| 161 self._host_port = port_pairs[0].local_port | |
| 162 | |
| 163 new_port_pairs = [(port_pair.local_port, port_pair.remote_port) | |
| 164 for port_pair in port_pairs] | |
| 165 | |
| 166 buildtype = 'Debug' | |
| 167 if HasForwarder('Release'): | |
| 168 buildtype = 'Release' | |
| 169 self._forwarder = forwarder.Forwarder(adb.Adb(), buildtype) | |
| 170 self._forwarder.Run(new_port_pairs, tool, '127.0.0.1') | |
| 171 | |
| 172 @property | |
| 173 def url(self): | |
| 174 assert self._forwarder | |
| 175 return 'http://localhost:%i' % self._host_port | |
| 176 | |
| 177 def Close(self): | |
| 178 if self._forwarder: | |
| 179 self._forwarder.Close() | |
| 180 self._forwarder = None | |
| OLD | NEW |