2010-03-13 21:57:06 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
# 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
# http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
# for the specific language governing rights and limitations under the
|
|
|
|
# License.
|
|
|
|
#
|
|
|
|
# The Original Code is mozilla.org code.
|
|
|
|
#
|
|
|
|
# The Initial Developer of the Original Code is The Mozilla Foundation
|
|
|
|
# Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
# the Initial Developer. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
# Joel Maher <joel.maher@gmail.com>
|
|
|
|
#
|
|
|
|
# Alternatively, the contents of this file may be used under the terms of
|
|
|
|
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
# in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
# of those above. If you wish to allow use of your version of this file only
|
|
|
|
# under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
# use your version of this file under the terms of the MPL, indicate your
|
|
|
|
# decision by deleting the provisions above and replace them with the notice
|
|
|
|
# and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
# the provisions above, a recipient may use your version of this file under
|
|
|
|
# the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
#
|
|
|
|
# ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-08-22 12:00:50 +04:00
|
|
|
import re, sys, os
|
2010-03-13 21:57:06 +03:00
|
|
|
import runxpcshelltests as xpcshell
|
|
|
|
from automationutils import *
|
2011-08-22 12:00:50 +04:00
|
|
|
import devicemanager, devicemanagerADB, devicemanagerSUT
|
2010-03-13 21:57:06 +03:00
|
|
|
|
2011-08-22 12:00:50 +04:00
|
|
|
# A specialization of XPCShellTests that runs tests on an Android device
|
|
|
|
# via devicemanager.
|
2010-03-13 21:57:06 +03:00
|
|
|
class XPCShellRemote(xpcshell.XPCShellTests, object):
|
|
|
|
|
2011-08-22 12:00:50 +04:00
|
|
|
def __init__(self, devmgr, options, args):
|
2010-03-13 21:57:06 +03:00
|
|
|
xpcshell.XPCShellTests.__init__(self)
|
2011-08-22 12:00:50 +04:00
|
|
|
self.options = options
|
|
|
|
self.device = devmgr
|
|
|
|
self.pathMapping = []
|
|
|
|
self.remoteTestRoot = self.device.getTestRoot("xpcshell")
|
|
|
|
# Terse directory names are used here ("b" for a binaries directory)
|
|
|
|
# to minimize the length of the command line used to execute
|
|
|
|
# xpcshell on the remote device. adb has a limit to the number
|
|
|
|
# of characters used in a shell command, and the xpcshell command
|
|
|
|
# line can be quite complex.
|
|
|
|
self.remoteBinDir = self.remoteJoin(self.remoteTestRoot, "b")
|
|
|
|
self.remoteScriptsDir = self.remoteTestRoot
|
|
|
|
self.remoteComponentsDir = self.remoteJoin(self.remoteTestRoot, "c")
|
|
|
|
self.profileDir = self.remoteJoin(self.remoteTestRoot, "p")
|
|
|
|
if options.setup:
|
|
|
|
self.setupUtilities()
|
|
|
|
self.setupTestDir()
|
|
|
|
self.remoteAPK = self.remoteJoin(self.remoteBinDir, os.path.basename(options.localAPK))
|
|
|
|
self.remoteDebugger = options.debugger
|
|
|
|
self.remoteDebuggerArgs = options.debuggerArgs
|
|
|
|
|
|
|
|
def remoteJoin(self, path1, path2):
|
|
|
|
joined = os.path.join(path1, path2)
|
|
|
|
joined = joined.replace('\\', '/')
|
|
|
|
return joined
|
|
|
|
|
|
|
|
def remoteForLocal(self, local):
|
|
|
|
for mapping in self.pathMapping:
|
|
|
|
if (os.path.abspath(mapping.local) == os.path.abspath(local)):
|
|
|
|
return mapping.remote
|
|
|
|
return local
|
|
|
|
|
|
|
|
def setupUtilities(self):
|
|
|
|
remotePrefDir = self.remoteJoin(self.remoteBinDir, "defaults/pref")
|
|
|
|
if (not self.device.dirExists(remotePrefDir)):
|
|
|
|
self.device.mkDirs(self.remoteJoin(remotePrefDir, "extra"))
|
|
|
|
if (not self.device.dirExists(self.remoteScriptsDir)):
|
|
|
|
self.device.mkDir(self.remoteScriptsDir)
|
|
|
|
if (not self.device.dirExists(self.remoteComponentsDir)):
|
|
|
|
self.device.mkDir(self.remoteComponentsDir)
|
|
|
|
|
|
|
|
local = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'head.js')
|
|
|
|
self.device.pushFile(local, self.remoteScriptsDir)
|
|
|
|
|
|
|
|
localBin = os.path.join(self.options.objdir, "dist/bin")
|
|
|
|
if not os.path.exists(localBin):
|
|
|
|
localBin = os.path.join(self.options.objdir, "bin")
|
|
|
|
if not os.path.exists(localBin):
|
|
|
|
print >>sys.stderr, "Error: could not find bin in objdir"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
local = os.path.join(localBin, "xpcshell")
|
|
|
|
self.device.pushFile(local, self.remoteBinDir)
|
|
|
|
|
|
|
|
local = os.path.join(localBin, "plugin-container")
|
|
|
|
self.device.pushFile(local, self.remoteBinDir)
|
|
|
|
|
|
|
|
local = os.path.join(localBin, "components/httpd.js")
|
|
|
|
self.device.pushFile(local, self.remoteComponentsDir)
|
|
|
|
|
|
|
|
local = os.path.join(localBin, "components/httpd.manifest")
|
|
|
|
self.device.pushFile(local, self.remoteComponentsDir)
|
|
|
|
|
|
|
|
local = os.path.join(localBin, "components/test_necko.xpt")
|
|
|
|
self.device.pushFile(local, self.remoteComponentsDir)
|
|
|
|
|
|
|
|
self.device.pushFile(self.options.localAPK, self.remoteBinDir)
|
|
|
|
|
|
|
|
localLib = os.path.join(self.options.objdir, "dist/fennec")
|
|
|
|
if not os.path.exists(localLib):
|
|
|
|
localLib = os.path.join(self.options.objdir, "fennec/lib")
|
|
|
|
if not os.path.exists(localLib):
|
|
|
|
print >>sys.stderr, "Error: could not find libs in objdir"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
for file in os.listdir(localLib):
|
|
|
|
if (file.endswith(".so")):
|
|
|
|
self.device.pushFile(os.path.join(localLib, file), self.remoteBinDir)
|
|
|
|
|
|
|
|
def setupTestDir(self):
|
|
|
|
xpcDir = os.path.join(self.options.objdir, "_tests/xpcshell")
|
|
|
|
self.device.pushDir(xpcDir, self.remoteScriptsDir)
|
|
|
|
|
|
|
|
def buildTestList(self):
|
|
|
|
xpcshell.XPCShellTests.buildTestList(self)
|
|
|
|
uniqueTestPaths = set([])
|
|
|
|
for test in self.alltests:
|
|
|
|
uniqueTestPaths.add(test['here'])
|
|
|
|
for testdir in uniqueTestPaths:
|
|
|
|
xpcDir = os.path.join(self.options.objdir, "_tests/xpcshell")
|
|
|
|
abbrevTestDir = os.path.relpath(testdir, xpcDir)
|
|
|
|
remoteScriptDir = self.remoteJoin(self.remoteScriptsDir, abbrevTestDir)
|
|
|
|
self.pathMapping.append(PathMapping(testdir, remoteScriptDir))
|
2010-03-13 21:57:06 +03:00
|
|
|
|
|
|
|
def buildXpcsCmd(self, testdir):
|
2011-08-22 12:00:50 +04:00
|
|
|
self.xpcsCmd = [
|
|
|
|
self.remoteJoin(self.remoteBinDir, "xpcshell"),
|
|
|
|
'-r', self.remoteJoin(self.remoteComponentsDir, 'httpd.manifest'),
|
|
|
|
'--greomni', self.remoteAPK,
|
|
|
|
'-j', '-s',
|
|
|
|
'-e', 'const _HTTPD_JS_PATH = "%s";' % self.remoteJoin(self.remoteComponentsDir, 'httpd.js'),
|
|
|
|
'-e', 'const _HEAD_JS_PATH = "%s";' % self.remoteJoin(self.remoteScriptsDir, 'head.js'),
|
|
|
|
'-f', self.remoteScriptsDir+'/head.js']
|
|
|
|
|
|
|
|
if self.remoteDebugger:
|
|
|
|
# for example, "/data/local/gdbserver" "localhost:12345"
|
|
|
|
self.xpcsCmd = [
|
|
|
|
self.remoteDebugger,
|
|
|
|
self.remoteDebuggerArgs,
|
|
|
|
self.xpcsCmd]
|
|
|
|
|
|
|
|
def getHeadFiles(self, test):
|
|
|
|
self.remoteHere = self.remoteForLocal(test['here'])
|
|
|
|
return [f.strip() for f in sorted(test['head'].split(' ')) if self.device.fileExists(self.remoteJoin(self.remoteHere, f))]
|
|
|
|
|
|
|
|
def getTailFiles(self, test):
|
|
|
|
return [f.strip() for f in sorted(test['tail'].split(' ')) if self.device.fileExists(self.remoteJoin(self.remoteHere, f))]
|
2010-03-13 21:57:06 +03:00
|
|
|
|
2011-08-22 12:00:50 +04:00
|
|
|
def buildCmdTestFile(self, name):
|
|
|
|
remoteDir = self.remoteForLocal(os.path.dirname(name))
|
|
|
|
if remoteDir == self.remoteHere:
|
|
|
|
remoteName = os.path.basename(name)
|
|
|
|
else:
|
|
|
|
remoteName = self.remoteJoin(remoteDir, os.path.basename(name))
|
|
|
|
return ['-e', 'const _TEST_FILE = ["%s"];' %
|
|
|
|
replaceBackSlashes(remoteName)]
|
2010-03-13 21:57:06 +03:00
|
|
|
|
|
|
|
def setupProfileDir(self):
|
2010-03-16 01:29:27 +03:00
|
|
|
self.device.removeDir(self.profileDir)
|
|
|
|
self.device.mkDir(self.profileDir)
|
2011-08-22 12:00:50 +04:00
|
|
|
if self.interactive or self.singleFile:
|
|
|
|
self.log.info("TEST-INFO | profile dir is %s" % self.profileDir)
|
2010-03-16 01:29:27 +03:00
|
|
|
return self.profileDir
|
2010-03-13 21:57:06 +03:00
|
|
|
|
|
|
|
def launchProcess(self, cmd, stdout, stderr, env, cwd):
|
2011-08-22 12:00:50 +04:00
|
|
|
# Some xpcshell arguments contain characters that are interpretted
|
|
|
|
# by the adb shell; enclose these arguments in quotes.
|
|
|
|
index = 0
|
|
|
|
for part in cmd:
|
|
|
|
if (part.find(" ")>=0 or part.find("(")>=0 or part.find(")")>=0 or part.find("\"")>=0):
|
|
|
|
part = '\''+part+'\''
|
|
|
|
cmd[index] = part
|
|
|
|
index = index + 1
|
|
|
|
|
|
|
|
xpcshell = self.remoteJoin(self.remoteBinDir, "xpcshell")
|
|
|
|
|
|
|
|
shellArgs = "cd "+self.remoteHere
|
|
|
|
shellArgs += "; LD_LIBRARY_PATH="+self.remoteBinDir
|
|
|
|
shellArgs += "; export CACHE_PATH="+self.remoteBinDir
|
2011-08-27 03:23:02 +04:00
|
|
|
if (self.device.getAppRoot()):
|
|
|
|
# xpcshell still runs without GRE_HOME; it may not be necessary
|
|
|
|
shellArgs += "; export GRE_HOME="+self.device.getAppRoot()
|
2011-08-22 12:00:50 +04:00
|
|
|
shellArgs += "; export XPCSHELL_TEST_PROFILE_DIR="+self.profileDir
|
|
|
|
shellArgs += "; "+xpcshell+" "
|
|
|
|
shellArgs += " ".join(cmd[1:])
|
|
|
|
|
|
|
|
if self.verbose:
|
|
|
|
self.log.info(shellArgs)
|
|
|
|
|
|
|
|
# If the adb version of devicemanager is used and the arguments passed
|
|
|
|
# to adb exceed ~1024 characters, the command may not execute.
|
|
|
|
if len(shellArgs) > 1000:
|
|
|
|
self.log.info("adb command length is excessive and may cause failure")
|
|
|
|
|
|
|
|
proc = self.device.runCmd(["shell", shellArgs])
|
2010-03-13 21:57:06 +03:00
|
|
|
return proc
|
|
|
|
|
|
|
|
def communicate(self, proc):
|
2011-08-22 12:00:50 +04:00
|
|
|
return proc.communicate()
|
2010-03-13 21:57:06 +03:00
|
|
|
|
|
|
|
def removeDir(self, dirname):
|
|
|
|
self.device.removeDir(dirname)
|
|
|
|
|
|
|
|
def getReturnCode(self, proc):
|
2011-08-22 12:00:50 +04:00
|
|
|
return proc.returncode
|
2010-03-13 21:57:06 +03:00
|
|
|
|
|
|
|
#TODO: consider creating a separate log dir. We don't have the test file structure,
|
|
|
|
# so we use filename.log. Would rather see ./logs/filename.log
|
2011-08-22 12:00:50 +04:00
|
|
|
def createLogFile(self, test, stdout, leakLogs):
|
2010-03-13 21:57:06 +03:00
|
|
|
try:
|
|
|
|
f = None
|
|
|
|
filename = test.replace('\\', '/').split('/')[-1] + ".log"
|
|
|
|
f = open(filename, "w")
|
|
|
|
f.write(stdout)
|
|
|
|
|
2011-08-22 12:00:50 +04:00
|
|
|
for leakLog in leakLogs:
|
|
|
|
if os.path.exists(leakLog):
|
|
|
|
leaks = open(leakLog, "r")
|
2010-03-13 21:57:06 +03:00
|
|
|
f.write(leaks.read())
|
|
|
|
leaks.close()
|
|
|
|
finally:
|
|
|
|
if f <> None:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
class RemoteXPCShellOptions(xpcshell.XPCShellOptions):
|
|
|
|
|
2011-08-22 12:00:50 +04:00
|
|
|
def __init__(self):
|
|
|
|
xpcshell.XPCShellOptions.__init__(self)
|
|
|
|
defaults = {}
|
|
|
|
|
|
|
|
self.add_option("--deviceIP", action="store",
|
|
|
|
type = "string", dest = "deviceIP",
|
|
|
|
help = "ip address of remote device to test")
|
|
|
|
defaults["deviceIP"] = None
|
|
|
|
|
|
|
|
self.add_option("--devicePort", action="store",
|
|
|
|
type = "string", dest = "devicePort",
|
|
|
|
help = "port of remote device to test")
|
|
|
|
defaults["devicePort"] = 20701
|
|
|
|
|
|
|
|
self.add_option("--dm_trans", action="store",
|
|
|
|
type = "string", dest = "dm_trans",
|
|
|
|
help = "the transport to use to communicate with device: [adb|sut]; default=sut")
|
|
|
|
defaults["dm_trans"] = "sut"
|
|
|
|
|
|
|
|
self.add_option("--objdir", action="store",
|
|
|
|
type = "string", dest = "objdir",
|
|
|
|
help = "local objdir, containing xpcshell binaries")
|
|
|
|
defaults["objdir"] = None
|
|
|
|
|
|
|
|
self.add_option("--apk", action="store",
|
|
|
|
type = "string", dest = "localAPK",
|
|
|
|
help = "local path to Fennec APK")
|
|
|
|
defaults["localAPK"] = None
|
|
|
|
|
|
|
|
self.add_option("--noSetup", action="store_false",
|
|
|
|
dest = "setup",
|
|
|
|
help = "do not copy any files to device (to be used only if device is already setup)")
|
|
|
|
defaults["setup"] = True
|
|
|
|
|
|
|
|
self.set_defaults(**defaults)
|
|
|
|
|
|
|
|
class PathMapping:
|
|
|
|
|
|
|
|
def __init__(self, localDir, remoteDir):
|
|
|
|
self.local = localDir
|
|
|
|
self.remote = remoteDir
|
2010-03-13 21:57:06 +03:00
|
|
|
|
|
|
|
def main():
|
|
|
|
|
2011-08-22 12:00:50 +04:00
|
|
|
dm_none = devicemanagerADB.DeviceManagerADB(None, None)
|
|
|
|
parser = RemoteXPCShellOptions()
|
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
|
|
|
if len(args) < 1 and options.manifest is None:
|
|
|
|
print >>sys.stderr, """Usage: %s <test dirs>
|
|
|
|
or: %s --manifest=test.manifest """ % (sys.argv[0], sys.argv[0])
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if (options.dm_trans == "adb"):
|
|
|
|
if (options.deviceIP):
|
|
|
|
dm = devicemanagerADB.DeviceManagerADB(options.deviceIP, options.devicePort)
|
|
|
|
else:
|
|
|
|
dm = dm_none
|
|
|
|
else:
|
|
|
|
dm = devicemanagerSUT.DeviceManagerSUT(options.deviceIP, options.devicePort)
|
|
|
|
if (options.deviceIP == None):
|
|
|
|
print "Error: you must provide a device IP to connect to via the --device option"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if options.interactive and not options.testPath:
|
|
|
|
print >>sys.stderr, "Error: You must specify a test filename in interactive mode!"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if not options.objdir:
|
|
|
|
print >>sys.stderr, "Error: You must specify an objdir"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if not options.localAPK:
|
|
|
|
for file in os.listdir(os.path.join(options.objdir, "dist")):
|
|
|
|
if (file.endswith(".apk") and file.startswith("fennec")):
|
|
|
|
options.localAPK = os.path.join(options.objdir, "dist")
|
|
|
|
options.localAPK = os.path.join(options.localAPK, file)
|
|
|
|
print >>sys.stderr, "using APK: " + options.localAPK
|
|
|
|
break
|
|
|
|
|
|
|
|
if not options.localAPK:
|
|
|
|
print >>sys.stderr, "Error: please specify an APK"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
xpcsh = XPCShellRemote(dm, options, args)
|
|
|
|
|
|
|
|
if not xpcsh.runTests(xpcshell='xpcshell',
|
|
|
|
testdirs=args[0:],
|
|
|
|
**options.__dict__):
|
|
|
|
sys.exit(1)
|
|
|
|
|
2010-03-13 21:57:06 +03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|