author Myk Melez <myk@mozilla.org>
Tue, 14 Aug 2012 15:27:26 -0700
changeset 102403 07b53bdc212ac3876cea6c2c7906e3106985043e
parent 102104 cefb4ae9e7d5aa7622df9577c1914b3592c4654d
child 106739 d1b17b12a472efd78831ef9f6b9f331c96c438b0
permissions -rw-r--r--
bug 770770: refactor webapp runtime test harness to reduce complexity/special-casing; r=adw

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

Runs the Mochitest test harness.

from __future__ import with_statement
from datetime import datetime
import optparse
import os
import os.path
import sys
import time

SCRIPT_DIR = os.path.abspath(os.path.realpath(os.path.dirname(sys.argv[0])))
sys.path.insert(0, SCRIPT_DIR);

import shutil
from urllib import quote_plus as encodeURIComponent
import urllib2
import commands
from automation import Automation
from automationutils import *
import tempfile

VMWARE_RECORDING_HELPER_BASENAME = "vmwarerecordinghelper"


class MochitestOptions(optparse.OptionParser):
  """Parses Mochitest commandline options."""
  def __init__(self, automation, scriptdir, **kwargs):
    self._automation = automation
    optparse.OptionParser.__init__(self, **kwargs)
    defaults = {}

    # we want to pass down everything from self._automation.__all__
    addCommonOptions(self, defaults=dict(zip(self._automation.__all__, 
             [getattr(self._automation, x) for x in self._automation.__all__])))

                    action = "store_true", dest = "closeWhenDone",
                    help = "close the application when tests are done running")
    defaults["closeWhenDone"] = False

                    action = "store", type = "string", dest = "app",
                    help = "absolute path to application, overriding default")
    defaults["app"] = os.path.join(scriptdir, self._automation.DEFAULT_APP)

                    action = "store", type = "string", dest = "utilityPath",
                    help = "absolute path to directory containing utility programs (xpcshell, ssltunnel, certutil)")
    defaults["utilityPath"] = self._automation.DIST_BIN

                    action = "store", type = "string", dest = "certPath",
                    help = "absolute path to directory containing certificate store to use testing profile")
    defaults["certPath"] = self._automation.CERTS_SRC_DIR

                    action = "store", type = "string",
                    dest = "logFile", metavar = "FILE",
                    help = "file to which logging occurs")
    defaults["logFile"] = ""

                    action = "store_true", dest = "autorun",
                    help = "start running tests when the application starts")
    defaults["autorun"] = False
                    type = "int", dest = "timeout",
                    help = "per-test timeout in seconds")
    defaults["timeout"] = None

                    type = "int", dest = "totalChunks",
                    help = "how many chunks to split the tests up into")
    defaults["totalChunks"] = None

                    type = "int", dest = "thisChunk",
                    help = "which chunk to run")
    defaults["thisChunk"] = None

                    type = "int", dest = "chunkByDir",
                    help = "group tests together in the same chunk that are in the same top chunkByDir directories")
    defaults["chunkByDir"] = 0

                    dest = "shuffle",
                    action = "store_true",
                    help = "randomize test order")
    defaults["shuffle"] = False

    LEVEL_STRING = ", ".join(LOG_LEVELS)

                    action = "store", type = "choice", dest = "consoleLevel",
                    choices = LOG_LEVELS, metavar = "LEVEL",
                    help = "one of %s to determine the level of console "
                           "logging" % LEVEL_STRING)
    defaults["consoleLevel"] = None

                    action = "store", type = "choice", dest = "fileLevel",
                    choices = LOG_LEVELS, metavar = "LEVEL",
                    help = "one of %s to determine the level of file "
                           "logging if a file has been specified, defaulting "
                           "to INFO" % LEVEL_STRING)
    defaults["fileLevel"] = "INFO"

                    action = "store_true", dest = "chrome",
                    help = "run chrome Mochitests")
    defaults["chrome"] = False

                    action = "store_true", dest = "ipcplugins",
                    help = "run ipcplugins Mochitests")
    defaults["ipcplugins"] = False

                    action = "store", type = "string", dest = "testPath",
                    help = "start in the given directory's tests")
    defaults["testPath"] = ""

                    action = "store_true", dest = "browserChrome",
                    help = "run browser chrome Mochitests")
    defaults["browserChrome"] = False

                    action = "store_true", dest = "webapprtContent",
                    help = "run WebappRT content tests")
    defaults["webapprtContent"] = False

                    action = "store_true", dest = "webapprtChrome",
                    help = "run WebappRT chrome tests")
    defaults["webapprtChrome"] = False

                    action = "store_true", dest = "a11y",
                    help = "run accessibility Mochitests");
    defaults["a11y"] = False

                    action = "append", type = "string",
                    dest = "environment", metavar = "NAME=VALUE",
                    help = "sets the given variable in the application's "
    defaults["environment"] = []

                    action = "append", type = "string",
                    dest = "extensionsToExclude",
                    help = "excludes the given extension from being installed "
                           "in the test profile")
    defaults["extensionsToExclude"] = []

                    action = "append", type = "string",
                    dest = "browserArgs", metavar = "ARG",
                    help = "provides an argument to the test application")
    defaults["browserArgs"] = []

                    action = "store", type = "int",
                    dest = "leakThreshold", metavar = "THRESHOLD",
                    help = "fail if the number of bytes leaked through "
                           "refcounted objects (or bytes in classes with "
                           "MOZ_COUNT_CTOR and MOZ_COUNT_DTOR) is greater "
                           "than the given number")
    defaults["leakThreshold"] = 0

                    action = "store_true", dest = "fatalAssertions",
                    help = "abort testing whenever an assertion is hit "
                           "(requires a debug build to be effective)")
    defaults["fatalAssertions"] = False

                    action = "append", dest = "extraProfileFiles",
                    help = "copy specified files/dirs to testing profile")
    defaults["extraProfileFiles"] = []

                    action = "append", dest = "extensionsToInstall",
                    help = "install the specified extension in the testing profile."
                           "The extension file's name should be <id>.xpi where <id> is"
                           "the extension's id as indicated in its install.rdf."
                           "An optional path can be specified too.")
    defaults["extensionsToInstall"] = []

    self.add_option("--profile-path", action = "store",
                    type = "string", dest = "profilePath",
                    help = "Directory where the profile will be stored."
                           "This directory will be deleted after the tests are finished")
    defaults["profilePath"] = tempfile.mkdtemp()

    self.add_option("--testing-modules-dir", action = "store",
                    type = "string", dest = "testingModulesDir",
                    help = "Directory where testing-only JS modules are "
    defaults["testingModulesDir"] = None

                    action = "store_true", dest = "vmwareRecording",
                    help = "enables recording while the application is running "
                           "inside a VMware Workstation 7.0 or later VM")
    defaults["vmwareRecording"] = False

                    action = "store", type = "int",
                    dest = "repeat", metavar = "REPEAT",
                    help = "repeats the test or set of tests the given number of times, ie: repeat=1 will run the test twice.")                   
    defaults["repeat"] = 0

                    action = "store", type="string", dest = "runOnlyTests",
                    help = "JSON list of tests that we only want to run, cannot be specified with --exclude-tests. [DEPRECATED- please use --test-manifest]")
    defaults["runOnlyTests"] = None

                    action = "store", type="string", dest = "excludeTests",
                    help = "JSON list of tests that we want to not run, cannot be specified with --run-only-tests. [DEPRECATED- please use --test-manifest]")
    defaults["excludeTests"] = None

                    action = "store", type="string", dest = "testManifest",
                    help = "JSON list of tests to specify 'runtests' and 'excludetests'.")
    defaults["testManifest"] = None

                    action = "store", type="string", dest = "failureFile",
                    help = "Filename of the output file where we can store a .json list of failures to be run in the future with --run-only-tests.")
    defaults["failureFile"] = None

    # -h, --help are automatically handled by OptionParser


    usage = """\
Usage instructions for runtests.py.
All arguments are optional.
If --chrome is specified, chrome tests will be run instead of web content tests.
If --browser-chrome is specified, browser-chrome tests will be run instead of web content tests.
See <http://mochikit.com/doc/html/MochiKit/Logging.html> for details on the logging levels."""

  def verifyOptions(self, options, mochitest):
    """ verify correct options and cleanup paths """

    if options.totalChunks is not None and options.thisChunk is None:
      self.error("thisChunk must be specified when totalChunks is specified")

    if options.totalChunks:
      if not 1 <= options.thisChunk <= options.totalChunks:
        self.error("thisChunk must be between 1 and totalChunks")

    if options.xrePath is None:
      # default xrePath to the app path if not provided
      # but only if an app path was explicitly provided
      if options.app != self.defaults['app']:
        options.xrePath = os.path.dirname(options.app)
        # otherwise default to dist/bin
        options.xrePath = self._automation.DIST_BIN

    # allow relative paths
    options.xrePath = mochitest.getFullPath(options.xrePath)

    options.profilePath = mochitest.getFullPath(options.profilePath)

    options.app = mochitest.getFullPath(options.app)
    if not os.path.exists(options.app):
      msg = """\
      Error: Path %(app)s doesn't exist.
      Are you executing $objdir/_tests/testing/mochitest/runtests.py?"""
      print msg % {"app": options.app}
      return None

    options.utilityPath = mochitest.getFullPath(options.utilityPath)
    options.certPath = mochitest.getFullPath(options.certPath)
    if options.symbolsPath and not isURL(options.symbolsPath):
      options.symbolsPath = mochitest.getFullPath(options.symbolsPath)

    options.webServer = self._automation.DEFAULT_WEB_SERVER
    options.httpPort = self._automation.DEFAULT_HTTP_PORT
    options.sslPort = self._automation.DEFAULT_SSL_PORT
    options.webSocketPort = self._automation.DEFAULT_WEBSOCKET_PORT

    if options.vmwareRecording:
      if not self._automation.IS_WIN32:
        self.error("use-vmware-recording is only supported on Windows.")
      mochitest.vmwareHelperPath = os.path.join(
        options.utilityPath, VMWARE_RECORDING_HELPER_BASENAME + ".dll")
      if not os.path.exists(mochitest.vmwareHelperPath):
        self.error("%s not found, cannot automate VMware recording." %

    if options.runOnlyTests != None and options.excludeTests != None:
      self.error("We can only support --run-only-tests OR --exclude-tests, not both.  Please consider using --test-manifest instead.")

    if options.testManifest != None and (options.runOnlyTests != None or options.excludeTests != None):
      self.error("Please use --test-manifest only and not --run-only-tests or --exclude-tests.")
    if options.runOnlyTests:
      if not os.path.exists(os.path.abspath(options.runOnlyTests)):
        self.error("unable to find --run-only-tests file '%s'" % options.runOnlyTests);
      options.testManifest = options.runOnlyTests
      options.runOnly = True
    if options.excludeTests:
      if not os.path.exists(os.path.abspath(options.excludeTests)):
        self.error("unable to find --exclude-tests file '%s'" % options.excludeTests);
      options.testManifest = options.excludeTests
      options.runOnly = False

    if options.webapprtContent and options.webapprtChrome:
      self.error("Only one of --webapprt-content and --webapprt-chrome may be given.")

    # Try to guess the testing modules directory.
    # This somewhat grotesque hack allows the buildbot machines to find the
    # modules directory without having to configure the buildbot hosts. This
    # code should never be executed in local runs because the build system
    # should always set the flag that populates this variable. If buildbot ever
    # passes this argument, this code can be deleted.
    if options.testingModulesDir is None:
      possible = os.path.join(os.getcwd(), os.path.pardir, 'modules')

      if os.path.isdir(possible):
        options.testingModulesDir = possible

    # Even if buildbot is updated, we still want this, as the path we pass in
    # to the app must be absolute and have proper slashes.
    if options.testingModulesDir is not None:
      options.testingModulesDir = os.path.normpath(options.testingModulesDir)

      if not os.path.isabs(options.testingModulesDir):
        options.testingModulesDir = os.path.abspath(testingModulesDir)

      if not os.path.isdir(options.testingModulesDir):
        self.error('--testing-modules-dir not a directory: %s' %

      options.testingModulesDir = options.testingModulesDir.replace('\\', '/')
      if options.testingModulesDir[-1] != '/':
        options.testingModulesDir += '/'

    return options


class MochitestServer:
  "Web server used to serve Mochitests, for closer fidelity to the real web."

  def __init__(self, automation, options):
    self._automation = automation
    self._closeWhenDone = options.closeWhenDone
    self._utilityPath = options.utilityPath
    self._xrePath = options.xrePath
    self._profileDir = options.profilePath
    self.webServer = options.webServer
    self.httpPort = options.httpPort
    self.shutdownURL = "http://%(server)s:%(port)s/server/shutdown" % { "server" : self.webServer, "port" : self.httpPort }
    self.testPrefix = "'webapprt_'" if options.webapprtContent else "undefined"

  def start(self):
    "Run the Mochitest server, returning the process ID of the server."
    env = self._automation.environment(xrePath = self._xrePath)
    env["XPCOM_DEBUG_BREAK"] = "warn"
    if self._automation.IS_WIN32:
      env["PATH"] = env["PATH"] + ";" + self._xrePath

    args = ["-g", self._xrePath,
            "-v", "170",
            "-f", "./" + "httpd.js",
            "-e", "const _PROFILE_PATH = '%(profile)s';const _SERVER_PORT = '%(port)s'; const _SERVER_ADDR = '%(server)s'; const _TEST_PREFIX = %(testPrefix)s;" %
                   {"profile" : self._profileDir.replace('\\', '\\\\'), "port" : self.httpPort, "server" : self.webServer, "testPrefix" : self.testPrefix },
            "-f", "./" + "server.js"]

    xpcshell = os.path.join(self._utilityPath,
                            "xpcshell" + self._automation.BIN_SUFFIX)
    self._process = self._automation.Process([xpcshell] + args, env = env)
    pid = self._process.pid
    if pid < 0:
      print "Error starting server."
    self._automation.log.info("INFO | runtests.py | Server pid: %d", pid)

  def ensureReady(self, timeout):
    assert timeout >= 0

    aliveFile = os.path.join(self._profileDir, "server_alive.txt")
    i = 0
    while i < timeout:
      if os.path.exists(aliveFile):
      i += 1
      print "Timed out while waiting for server startup."

  def stop(self):
      with urllib2.urlopen(self.shutdownURL) as c:

      rtncode = self._process.poll()
      if rtncode is None:

class WebSocketServer(object):
  "Class which encapsulates the mod_pywebsocket server"

  def __init__(self, automation, options, scriptdir, debuggerInfo=None):
    self.port = options.webSocketPort
    self._automation = automation
    self._scriptdir = scriptdir
    self.debuggerInfo = debuggerInfo

  def start(self):
    # Invoke pywebsocket through a wrapper which adds special SIGINT handling.
    # If we're in an interactive debugger, the wrapper causes the server to
    # ignore SIGINT so the server doesn't capture a ctrl+c meant for the
    # debugger.
    # If we're not in an interactive debugger, the wrapper causes the server to
    # die silently upon receiving a SIGINT.
    scriptPath = 'pywebsocket_wrapper.py'
    script = os.path.join(self._scriptdir, scriptPath)

    cmd = [sys.executable, script]
    if self.debuggerInfo and self.debuggerInfo['interactive']:
        cmd += ['--interactive']
    cmd += ['-p', str(self.port), '-w', self._scriptdir, '-l',      \
           os.path.join(self._scriptdir, "websock.log"),            \
           '--log-level=debug', '--allow-handlers-outside-root-dir']

    self._process = self._automation.Process(cmd)
    pid = self._process.pid
    if pid < 0:
      print "Error starting websocket server."
    self._automation.log.info("INFO | runtests.py | Websocket server pid: %d", pid)

  def stop(self):

class Mochitest(object):
  # Path to the test script on the server
  TEST_PATH = "tests"
  CHROME_PATH = "redirect.html"
  PLAIN_LOOP_PATH = "plain-loop.html"
  urlOpts = []
  runSSLTunnel = True
  vmwareHelper = None

  oldcwd = os.getcwd()

  def __init__(self, automation):
    self.automation = automation

    # Max time in seconds to wait for server startup before tests will fail -- if
    # this seems big, it's mostly for debug machines where cold startup
    # (particularly after a build) takes forever.
    if self.automation.IS_DEBUG_BUILD:

    self.SCRIPT_DIRECTORY = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))

  def getFullPath(self, path):
    " Get an absolute path relative to self.oldcwd."
    return os.path.normpath(os.path.join(self.oldcwd, os.path.expanduser(path)))

  def buildTestPath(self, options):
    """ Build the url path to the specific test harness and test file or directory """
    testHost = "http://mochi.test:8888"
    testURL = ("/").join([testHost, self.TEST_PATH, options.testPath])
    if os.path.isfile(os.path.join(self.oldcwd, os.path.dirname(__file__), self.TEST_PATH, options.testPath)) and options.repeat > 0:
       testURL = ("/").join([testHost, self.PLAIN_LOOP_PATH])
    if options.chrome or options.a11y:
       testURL = ("/").join([testHost, self.CHROME_PATH])
    elif options.browserChrome:
      testURL = "about:blank"
    elif options.ipcplugins:
      testURL = ("/").join([testHost, self.TEST_PATH, "dom/plugins/test"])
    return testURL

  def startWebSocketServer(self, options, debuggerInfo):
    """ Launch the websocket server """
    if options.webServer != '':

    self.wsserver = WebSocketServer(self.automation, options,
                                    self.SCRIPT_DIRECTORY, debuggerInfo)

  def stopWebSocketServer(self, options):
    if options.webServer != '':


  def startWebServer(self, options):
    if options.webServer != '':

    """ Create the webserver and start it up """
    self.server = MochitestServer(self.automation, options)

    # If we're lucky, the server has fully started by now, and all paths are
    # ready, etc.  However, xpcshell cold start times suck, at least for debug
    # builds.  We'll try to connect to the server for awhile, and if we fail,
    # we'll try to kill the server and exit with an error.

  def stopWebServer(self, options):
    """ Server's no longer needed, and perhaps more importantly, anything it might
        spew to console shouldn't disrupt the leak information table we print next.
    if options.webServer != '':


  def getLogFilePath(self, logFile):
    """ return the log file path relative to the device we are testing on, in most cases 
        it will be the full path on the local system
    return self.getFullPath(logFile)

  def buildProfile(self, options):
    """ create the profile and add optional chrome bits and files if requested """
    self.automation.initializeProfile(options.profilePath, options.extraPrefs, useServerLocations = True)
    manifest = self.addChromeToProfile(options)
    return manifest

  def buildBrowserEnv(self, options):
    """ build the environment variables for the specific test and operating system """
    browserEnv = self.automation.environment(xrePath = options.xrePath)

    # These variables are necessary for correct application startup; change
    # via the commandline at your own risk.
    browserEnv["XPCOM_DEBUG_BREAK"] = "stack"

    for v in options.environment:
      ix = v.find("=")
      if ix <= 0:
        print "Error: syntax error in --setenv=" + v
        return None
      browserEnv[v[:ix]] = v[ix + 1:]

    browserEnv["XPCOM_MEM_BLOAT_LOG"] = self.leak_report_file

    if options.fatalAssertions:
      browserEnv["XPCOM_DEBUG_BREAK"] = "stack-and-abort"

    return browserEnv

  def buildURLOptions(self, options, env):
    """ Add test control options from the command line to the url 

        URL parameters to test URL:

        autorun -- kick off tests automatically
        closeWhenDone -- closes the browser after the tests
        hideResultsTable -- hides the table of individual test results
        logFile -- logs test run to an absolute path
        totalChunks -- how many chunks to split tests into
        thisChunk -- which chunk to run
        timeout -- per-test timeout in seconds
        repeat -- How many times to repeat the test, ie: repeat=1 will run the test twice.
    # allow relative paths for logFile
    if options.logFile:
      options.logFile = self.getLogFilePath(options.logFile)
    if options.browserChrome or options.chrome or options.a11y or options.webapprtChrome:
      if options.autorun:
      if options.timeout:
        self.urlOpts.append("timeout=%d" % options.timeout)
      if options.closeWhenDone:
      if options.logFile:
        self.urlOpts.append("logFile=" + encodeURIComponent(options.logFile))
        self.urlOpts.append("fileLevel=" + encodeURIComponent(options.fileLevel))
      if options.consoleLevel:
        self.urlOpts.append("consoleLevel=" + encodeURIComponent(options.consoleLevel))
      if options.totalChunks:
        self.urlOpts.append("totalChunks=%d" % options.totalChunks)
        self.urlOpts.append("thisChunk=%d" % options.thisChunk)
      if options.chunkByDir:
        self.urlOpts.append("chunkByDir=%d" % options.chunkByDir)
      if options.shuffle:
      if "MOZ_HIDE_RESULTS_TABLE" in env and env["MOZ_HIDE_RESULTS_TABLE"] == "1":
      if options.repeat:
        self.urlOpts.append("repeat=%d" % options.repeat)
      if os.path.isfile(os.path.join(self.oldcwd, os.path.dirname(__file__), self.TEST_PATH, options.testPath)) and options.repeat > 0:
        self.urlOpts.append("testname=%s" % ("/").join([self.TEST_PATH, options.testPath]))
      if options.testManifest:
        self.urlOpts.append("testManifest=%s" % options.testManifest)
        if hasattr(options, 'runOnly') and options.runOnly:
      if options.failureFile:
        self.urlOpts.append("failureFile=%s" % self.getFullPath(options.failureFile))

  def cleanup(self, manifest, options):
    """ remove temporary files and profile """

  def startVMwareRecording(self, options):
    """ starts recording inside VMware VM using the recording helper dll """
    from ctypes import cdll
    self.vmwareHelper = cdll.LoadLibrary(self.vmwareHelperPath)
    if self.vmwareHelper is None:
      self.automation.log.warning("WARNING | runtests.py | Failed to load "
                                  "VMware recording helper")
    self.automation.log.info("INFO | runtests.py | Starting VMware recording.")
    except Exception, e:
      self.automation.log.warning("WARNING | runtests.py | Failed to start "
                                  "VMware recording: (%s)" % str(e))
      self.vmwareHelper = None

  def stopVMwareRecording(self):
    """ stops recording inside VMware VM using the recording helper dll """
    if self.vmwareHelper is not None:
      self.automation.log.info("INFO | runtests.py | Stopping VMware "
      except Exception, e:
        self.automation.log.warning("WARNING | runtests.py | Failed to stop "
                                    "VMware recording: (%s)" % str(e))
      self.vmwareHelper = None

  def runTests(self, options):
    """ Prepare, configure, run tests and cleanup """
    debuggerInfo = getDebuggerInfo(self.oldcwd, options.debugger, options.debuggerArgs,

    self.leak_report_file = os.path.join(options.profilePath, "runtests_leaks.log")

    browserEnv = self.buildBrowserEnv(options)
    if browserEnv is None:
      return 1

    manifest = self.buildProfile(options)
    if manifest is None:
      return 1

    self.startWebSocketServer(options, debuggerInfo)

    testURL = self.buildTestPath(options)
    self.buildURLOptions(options, browserEnv)
    if len(self.urlOpts) > 0:
      testURL += "?" + "&".join(self.urlOpts)

    if options.webapprtContent:
      options.browserArgs.extend(('-test-mode', testURL))
      testURL = None

    # Remove the leak detection file so it can't "leak" to the tests run.
    # The file is not there if leak logging was not enabled in the application build.
    if os.path.exists(self.leak_report_file):

    # then again to actually run mochitest
    if options.timeout:
      timeout = options.timeout + 30
    elif not options.autorun:
      timeout = None
      timeout = 330.0 # default JS harness timeout is 300 seconds

    if options.vmwareRecording:

    self.automation.log.info("INFO | runtests.py | Running tests: start.\n")
      status = self.automation.runApp(testURL, browserEnv, options.app,
                                  options.profilePath, options.browserArgs,
                                  runSSLTunnel = self.runSSLTunnel,
                                  utilityPath = options.utilityPath,
                                  xrePath = options.xrePath,
                                  timeout = timeout)
    except KeyboardInterrupt:
      self.automation.log.info("INFO | runtests.py | Received keyboard interrupt.\n");
      status = -1
      self.automation.log.exception("INFO | runtests.py | Received unexpected exception while running application\n")
      status = 1

    if options.vmwareRecording:

    processLeakLog(self.leak_report_file, options.leakThreshold)

    self.automation.log.info("\nINFO | runtests.py | Running tests: end.")

    if manifest is not None:
      self.cleanup(manifest, options)
    return status

  def makeTestConfig(self, options):
    "Creates a test configuration file for customizing test execution."
    def jsonString(val):
      if isinstance(val, bool):
        if val:
          return "true"
        return "false"
      elif val is None:
        return '""'
      elif isinstance(val, basestring):
        return '"%s"' % (val.replace('\\', '\\\\'))
      elif isinstance(val, int):
        return '%s' % (val)
      elif isinstance(val, list):
        content = '['
        first = True
        for item in val:
          if first:
            first = False
            content += ", "
          content += jsonString(item)
        content += ']'
        return content
        print "unknown type: %s: %s" % (opt, val)

    options.logFile = options.logFile.replace("\\", "\\\\")
    options.testPath = options.testPath.replace("\\", "\\\\")
    testRoot = 'chrome'
    if (options.browserChrome):
      testRoot = 'browser'
    elif (options.a11y):
      testRoot = 'a11y'
    elif (options.webapprtChrome):
      testRoot = 'webapprtChrome'

    if "MOZ_HIDE_RESULTS_TABLE" in os.environ and os.environ["MOZ_HIDE_RESULTS_TABLE"] == "1":
      options.hideResultsTable = True

    #TODO: when we upgrade to python 2.6, just use json.dumps(options.__dict__)
    content = "{"
    content += '"testRoot": "%s", ' % (testRoot) 
    first = True
    for opt in options.__dict__.keys():
      val = options.__dict__[opt]
      if first:
        first = False
        content += ", "

      content += '"' + opt + '": '
      content += jsonString(val)
    content += "}"

    with open(os.path.join(options.profilePath, "testConfig.js"), "w") as config:

  def addChromeToProfile(self, options):
    "Adds MochiKit chrome tests to the profile."

    # Create (empty) chrome directory.
    chromedir = os.path.join(options.profilePath, "chrome")

    # Write userChrome.css.
    chrome = """
@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); /* set default namespace to XUL */
toolbarpalette {
  background-color: rgb(235, 235, 235) !important;
toolbar#nav-bar {
  background-image: none !important;
    with open(os.path.join(options.profilePath, "userChrome.css"), "a") as chromeFile:

    # Call copyTestsJarToProfile(), Write tests.manifest.
    manifest = os.path.join(options.profilePath, "tests.manifest")
    with open(manifest, "w") as manifestFile:
      if self.copyTestsJarToProfile(options):
        # Register tests.jar.
        manifestFile.write("content mochitests jar:tests.jar!/content/\n");
        # Register chrome directory.
        chrometestDir = os.path.abspath(".") + "/"
        if self.automation.IS_WIN32:
          chrometestDir = "file:///" + chrometestDir.replace("\\", "/")
        manifestFile.write("content mochitests %s contentaccessible=yes\n" % chrometestDir)

      if options.testingModulesDir is not None:
        manifestFile.write("resource testing-common file:///%s\n" %

    # Call installChromeJar().
    jarDir = "mochijar"
    if not os.path.isdir(os.path.join(self.SCRIPT_DIRECTORY, jarDir)):
      self.automation.log.warning("TEST-UNEXPECTED-FAIL | invalid setup: missing mochikit extension")
      return None

    # Support Firefox (browser), B2G (shell), SeaMonkey (navigator), and Webapp
    # Runtime (webapp).
    chrome = ""
    if options.browserChrome or options.chrome or options.a11y or options.webapprtChrome:
      chrome += """
overlay chrome://browser/content/browser.xul chrome://mochikit/content/browser-test-overlay.xul
overlay chrome://browser/content/shell.xul chrome://mochikit/content/browser-test-overlay.xul
overlay chrome://navigator/content/navigator.xul chrome://mochikit/content/browser-test-overlay.xul
overlay chrome://webapprt/content/webapp.xul chrome://mochikit/content/browser-test-overlay.xul

    self.installChromeJar(jarDir, chrome, options)
    return manifest

  def installChromeJar(self, jarDirName, chrome, options):
      copy mochijar directory to profile as an extension so we have chrome://mochikit for all harness code
    self.automation.installExtension(os.path.join(self.SCRIPT_DIRECTORY, jarDirName), \
                                     options.profilePath, "mochikit@mozilla.org")

    # Write chrome.manifest.
    with open(os.path.join(options.profilePath, "extensions", "staged", "mochikit@mozilla.org", "chrome.manifest"), "a") as mfile:

  def copyTestsJarToProfile(self, options):
    """ copy tests.jar to the profile directory so we can auto register it in the .xul harness """
    testsJarFile = os.path.join(self.SCRIPT_DIRECTORY, "tests.jar")
    if not os.path.isfile(testsJarFile):
      return False

    shutil.copy2(testsJarFile, options.profilePath)
    return True

  def copyExtraFilesToProfile(self, options):
    "Copy extra files or dirs specified on the command line to the testing profile."
    for f in options.extraProfileFiles:
      abspath = self.getFullPath(f)
      if os.path.isfile(abspath):
        shutil.copy2(abspath, options.profilePath)
      elif os.path.isdir(abspath):
        dest = os.path.join(options.profilePath, os.path.basename(abspath))
        shutil.copytree(abspath, dest)
        self.automation.log.warning("WARNING | runtests.py | Failed to copy %s to profile", abspath)

  def installExtensionFromPath(self, options, path, extensionID = None):
    extensionPath = self.getFullPath(path)

    self.automation.log.info("INFO | runtests.py | Installing extension at %s to %s." %
                            (extensionPath, options.profilePath))
    self.automation.installExtension(extensionPath, options.profilePath,

  def installExtensionsToProfile(self, options):
    "Install special testing extensions, application distributed extensions, and specified on the command line ones to testing profile."
    extensionDirs = [
      # Extensions distributed with the test harness.
      os.path.normpath(os.path.join(self.SCRIPT_DIRECTORY, "extensions")),
      # Extensions distributed with the application.
      os.path.join(options.app[ : options.app.rfind(os.sep)], "distribution", "extensions")

    for extensionDir in extensionDirs:
      if os.path.isdir(extensionDir):
        for dirEntry in os.listdir(extensionDir):
          if dirEntry not in options.extensionsToExclude:
            path = os.path.join(extensionDir, dirEntry)
            if os.path.isdir(path) or (os.path.isfile(path) and path.endswith(".xpi")):
              self.installExtensionFromPath(options, path)

    # Install custom extensions passed on the command line.
    for path in options.extensionsToInstall:
      self.installExtensionFromPath(options, path)

def main():
  automation = Automation()
  mochitest = Mochitest(automation)
  parser = MochitestOptions(automation, mochitest.SCRIPT_DIRECTORY)
  options, args = parser.parse_args()

  options = parser.verifyOptions(options, mochitest)
  if options == None:

  options.utilityPath = mochitest.getFullPath(options.utilityPath)
  options.certPath = mochitest.getFullPath(options.certPath)
  if options.symbolsPath and not isURL(options.symbolsPath):
    options.symbolsPath = mochitest.getFullPath(options.symbolsPath)


if __name__ == "__main__":