DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (dcc6d7a0dc00)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
# 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/.

from argparse import ArgumentParser

import json
import mozinfo
import moznetwork
import os
import random
import socket
import sys
import time
import traceback
import unittest
import warnings
import mozprofile


from manifestparser import TestManifest
from manifestparser.filters import tags
from marionette_driver.marionette import Marionette
from mixins.b2g import B2GTestResultMixin, get_b2g_pid, get_dm
from mozlog import get_default_logger
from moztest.adapters.unit import StructuredTestRunner, StructuredTestResult
from moztest.results import TestResultCollection, TestResult, relevant_line
import mozversion

import httpd


here = os.path.abspath(os.path.dirname(__file__))

def update_mozinfo(path=None):
    """walk up directories to find mozinfo.json and update the info"""

    path = path or here
    dirs = set()
    while path != os.path.expanduser('~'):
        if path in dirs:
            break
        dirs.add(path)
        path = os.path.split(path)[0]

    return mozinfo.find_and_update_from_json(*dirs)


class MarionetteTest(TestResult):

    @property
    def test_name(self):
        if self.test_class is not None:
            return '%s.py %s.%s' % (self.test_class.split('.')[0],
                                    self.test_class,
                                    self.name)
        else:
            return self.name

class MarionetteTestResult(StructuredTestResult, TestResultCollection):

    resultClass = MarionetteTest

    def __init__(self, *args, **kwargs):
        self.marionette = kwargs.pop('marionette')
        TestResultCollection.__init__(self, 'MarionetteTest')
        self.passed = 0
        self.testsRun = 0
        self.result_modifiers = [] # used by mixins to modify the result
        pid = kwargs.pop('b2g_pid')
        logcat_stdout = kwargs.pop('logcat_stdout')
        if pid:
            if B2GTestResultMixin not in self.__class__.__bases__:
                bases = [b for b in self.__class__.__bases__]
                bases.append(B2GTestResultMixin)
                self.__class__.__bases__ = tuple(bases)
            B2GTestResultMixin.__init__(self, b2g_pid=pid, logcat_stdout=logcat_stdout)
        StructuredTestResult.__init__(self, *args, **kwargs)

    @property
    def skipped(self):
        return [t for t in self if t.result == 'SKIPPED']

    @skipped.setter
    def skipped(self, value):
        pass

    @property
    def expectedFailures(self):
        return [t for t in self if t.result == 'KNOWN-FAIL']

    @expectedFailures.setter
    def expectedFailures(self, value):
        pass

    @property
    def unexpectedSuccesses(self):
        return [t for t in self if t.result == 'UNEXPECTED-PASS']

    @unexpectedSuccesses.setter
    def unexpectedSuccesses(self, value):
        pass

    @property
    def tests_passed(self):
        return [t for t in self if t.result == 'PASS']

    @property
    def errors(self):
        return [t for t in self if t.result == 'ERROR']

    @errors.setter
    def errors(self, value):
        pass

    @property
    def failures(self):
        return [t for t in self if t.result == 'UNEXPECTED-FAIL']

    @failures.setter
    def failures(self, value):
        pass

    @property
    def duration(self):
        if self.stop_time:
            return self.stop_time - self.start_time
        else:
            return 0

    def add_test_result(self, test, result_expected='PASS',
                        result_actual='PASS', output='', context=None, **kwargs):
        def get_class(test):
            return test.__class__.__module__ + '.' + test.__class__.__name__

        name = str(test).split()[0]
        test_class = get_class(test)
        if hasattr(test, 'jsFile'):
            name = os.path.basename(test.jsFile)
            test_class = None

        t = self.resultClass(name=name, test_class=test_class,
                       time_start=test.start_time, result_expected=result_expected,
                       context=context, **kwargs)
        # call any registered result modifiers
        for modifier in self.result_modifiers:
            result_expected, result_actual, output, context = modifier(t, result_expected, result_actual, output, context)
        t.finish(result_actual,
                 time_end=time.time() if test.start_time else 0,
                 reason=relevant_line(output),
                 output=output)
        self.append(t)

    def addError(self, test, err):
        self.add_test_result(test, output=self._exc_info_to_string(err, test), result_actual='ERROR')
        super(MarionetteTestResult, self).addError(test, err)

    def addFailure(self, test, err):
        self.add_test_result(test, output=self._exc_info_to_string(err, test), result_actual='UNEXPECTED-FAIL')
        super(MarionetteTestResult, self).addFailure(test, err)

    def addSuccess(self, test):
        self.passed += 1
        self.add_test_result(test, result_actual='PASS')
        super(MarionetteTestResult, self).addSuccess(test)

    def addExpectedFailure(self, test, err):
        """Called when an expected failure/error occured."""
        self.add_test_result(test, output=self._exc_info_to_string(err, test),
                             result_actual='KNOWN-FAIL')
        super(MarionetteTestResult, self).addExpectedFailure(test, err)

    def addUnexpectedSuccess(self, test):
        """Called when a test was expected to fail, but succeed."""
        self.add_test_result(test, result_actual='UNEXPECTED-PASS')
        super(MarionetteTestResult, self).addUnexpectedSuccess(test)

    def addSkip(self, test, reason):
        self.add_test_result(test, output=reason, result_actual='SKIPPED')
        super(MarionetteTestResult, self).addSkip(test, reason)

    def getInfo(self, test):
        return test.test_name

    def getDescription(self, test):
        doc_first_line = test.shortDescription()
        if self.descriptions and doc_first_line:
            return '\n'.join((str(test), doc_first_line))
        else:
            desc = str(test)
            if hasattr(test, 'jsFile'):
                desc = "%s, %s" % (test.jsFile, desc)
            return desc

    def printLogs(self, test):
        for testcase in test._tests:
            if hasattr(testcase, 'loglines') and testcase.loglines:
                # Don't dump loglines to the console if they only contain
                # TEST-START and TEST-END.
                skip_log = True
                for line in testcase.loglines:
                    str_line = ' '.join(line)
                    if not 'TEST-END' in str_line and not 'TEST-START' in str_line:
                        skip_log = False
                        break
                if skip_log:
                    return
                self.logger.info('START LOG:')
                for line in testcase.loglines:
                    self.logger.info(' '.join(line).encode('ascii', 'replace'))
                self.logger.info('END LOG:')

    def stopTest(self, *args, **kwargs):
        unittest._TextTestResult.stopTest(self, *args, **kwargs)
        if self.marionette.check_for_crash():
            # this tells unittest.TestSuite not to continue running tests
            self.shouldStop = True
            test = next((a for a in args if isinstance(a, unittest.TestCase)),
                        None)
            if test:
                self.addError(test, sys.exc_info())


class MarionetteTextTestRunner(StructuredTestRunner):

    resultclass = MarionetteTestResult

    def __init__(self, **kwargs):
        self.marionette = kwargs.pop('marionette')
        self.capabilities = kwargs.pop('capabilities')
        self.pre_run_functions = []
        self.b2g_pid = None
        self.logcat_stdout = kwargs.pop('logcat_stdout')

        if self.capabilities["device"] != "desktop" and self.capabilities["browserName"] == "B2G":
            def b2g_pre_run():
                dm_type = os.environ.get('DM_TRANS', 'adb')
                if dm_type == 'adb':
                    self.b2g_pid = get_b2g_pid(get_dm(self.marionette))
            self.pre_run_functions.append(b2g_pre_run)

        StructuredTestRunner.__init__(self, **kwargs)


    def _makeResult(self):
        return self.resultclass(self.stream,
                                self.descriptions,
                                self.verbosity,
                                marionette=self.marionette,
                                b2g_pid=self.b2g_pid,
                                logger=self.logger,
                                logcat_stdout=self.logcat_stdout,
                                result_callbacks=self.result_callbacks)

    def run(self, test):
        "Run the given test case or test suite."
        for pre_run_func in self.pre_run_functions:
            pre_run_func()

        result = super(MarionetteTextTestRunner, self).run(test)
        result.printLogs(test)
        return result


class BaseMarionetteArguments(ArgumentParser):
    socket_timeout_default = 360.0

    def __init__(self, **kwargs):
        ArgumentParser.__init__(self, **kwargs)

        def dir_path(path):
            path = os.path.abspath(os.path.expanduser(path))
            if not os.access(path, os.F_OK):
                os.makedirs(path)
            return path

        self.argument_containers = []
        self.add_argument('tests',
                          nargs='*',
                          default=[],
                          help='Tests to run.')
        self.add_argument('-v', '--verbose',
                        action='count',
                        help='Increase verbosity to include debug messages with -v, '
                            'and trace messages with -vv.')
        self.add_argument('--emulator',
                        choices=['x86', 'arm'],
                        help='if no --address is given, then the harness will launch a B2G emulator on which to run '
                             'emulator tests. if --address is given, then the harness assumes you are running an '
                             'emulator already, and will run the emulator tests using that emulator. you need to '
                             'specify which architecture to emulate for both cases')
        self.add_argument('--emulator-binary',
                        help='launch a specific emulator binary rather than launching from the B2G built emulator')
        self.add_argument('--emulator-img',
                        help='use a specific image file instead of a fresh one')
        self.add_argument('--emulator-res',
                        help='set a custom resolution for the emulator'
                             'Example: "480x800"')
        self.add_argument('--sdcard',
                        help='size of sdcard to create for the emulator')
        self.add_argument('--no-window',
                        action='store_true',
                        default=False,
                        help='when Marionette launches an emulator, start it with the -no-window argument')
        self.add_argument('--logcat-dir',
                        dest='logdir',
                        help='directory to store logcat dump files',
                        type=dir_path)
        self.add_argument('--logcat-stdout',
                        action='store_true',
                        default=False,
                        help='dump adb logcat to stdout')
        self.add_argument('--address',
                        help='host:port of running Gecko instance to connect to')
        self.add_argument('--device',
                        dest='device_serial',
                        help='serial ID of a device to use for adb / fastboot')
        self.add_argument('--adb-host',
                        help='host to use for adb connection')
        self.add_argument('--adb-port',
                        help='port to use for adb connection')
        self.add_argument('--type',
                        help="the type of test to run, can be a combination of values defined in the manifest file; "
                             "individual values are combined with '+' or '-' characters. for example: 'browser+b2g' "
                             "means the set of tests which are compatible with both browser and b2g; 'b2g-qemu' means "
                             "the set of tests which are compatible with b2g but do not require an emulator. this "
                             "argument is only used when loading tests from manifest files")
        self.add_argument('--homedir',
                        help='home directory of emulator files')
        self.add_argument('--app',
                        help='application to use')
        self.add_argument('--app-arg',
                        dest='app_args',
                        action='append',
                        default=[],
                        help='specify a command line argument to be passed onto the application')
        self.add_argument('--binary',
                        help='gecko executable to launch before running the test')
        self.add_argument('--profile',
                        help='profile to use when launching the gecko process. if not passed, then a profile will be '
                             'constructed and used',
                        type=dir_path)
        self.add_argument('--pref',
                        action='append',
                        dest='prefs_args',
                        help=(" A preference to set. Must be a key-value pair"
                              " separated by a ':'."))
        self.add_argument('--preferences',
                        action='append',
                        dest='prefs_files',
                        help=("read preferences from a JSON or INI file. For"
                              " INI, use 'file.ini:section' to specify a"
                              " particular section."))
        self.add_argument('--addon',
                        action='append',
                        help="addon to install; repeat for multiple addons.")
        self.add_argument('--repeat',
                        type=int,
                        default=0,
                        help='number of times to repeat the test(s)')
        self.add_argument('--testvars',
                        action='append',
                        help='path to a json file with any test data required')
        self.add_argument('--tree',
                        default='b2g',
                        help='the tree that the revision parameter refers to')
        self.add_argument('--symbols-path',
                        help='absolute path to directory containing breakpad symbols, or the url of a zip file containing symbols')
        self.add_argument('--timeout',
                        type=int,
                        help='if a --timeout value is given, it will set the default page load timeout, search timeout and script timeout to the given value. If not passed in, it will use the default values of 30000ms for page load, 0ms for search timeout and 10000ms for script timeout')
        self.add_argument('--startup-timeout',
                        type=int,
                        default=60,
                        help='the max number of seconds to wait for a Marionette connection after launching a binary')
        self.add_argument('--shuffle',
                        action='store_true',
                        default=False,
                        help='run tests in a random order')
        self.add_argument('--shuffle-seed',
                        type=int,
                        default=random.randint(0, sys.maxint),
                        help='Use given seed to shuffle tests')
        self.add_argument('--total-chunks',
                        type=int,
                        help='how many chunks to split the tests up into')
        self.add_argument('--this-chunk',
                        type=int,
                        help='which chunk to run')
        self.add_argument('--sources',
                        help='path to sources.xml (Firefox OS only)')
        self.add_argument('--server-root',
                        help='url to a webserver or path to a document root from which content '
                        'resources are served (default: {}).'.format(os.path.join(
                            os.path.dirname(here), 'www')))
        self.add_argument('--gecko-log',
                        help="Define the path to store log file. If the path is"
                             " a directory, the real log file will be created"
                             " given the format gecko-(timestamp).log. If it is"
                             " a file, if will be used directly. '-' may be passed"
                             " to write to stdout. Default: './gecko.log'")
        self.add_argument('--logger-name',
                        default='Marionette-based Tests',
                        help='Define the name to associate with the logger used')
        self.add_argument('--jsdebugger',
                        action='store_true',
                        default=False,
                        help='Enable the jsdebugger for marionette javascript.')
        self.add_argument('--pydebugger',
                        help='Enable python post-mortem debugger when a test fails.'
                             ' Pass in the debugger you want to use, eg pdb or ipdb.')
        self.add_argument('--socket-timeout',
                        default=self.socket_timeout_default,
                        help='Set the global timeout for marionette socket operations.')
        self.add_argument('--e10s',
                        action='store_true',
                        default=False,
                        help='Enable e10s when running marionette tests.')
        self.add_argument('--tag',
                        action='append', dest='test_tags',
                        default=None,
                        help="Filter out tests that don't have the given tag. Can be "
                             "used multiple times in which case the test must contain "
                             "at least one of the given tags.")
        self.add_argument('--workspace',
                          action='store',
                          default=None,
                          help="Path to directory for Marionette output. "
                               "(Default: .) (Default profile dest: TMP)",
                          type=dir_path)

    def register_argument_container(self, container):
        group = self.add_argument_group(container.name)

        for cli, kwargs in container.args:
            group.add_argument(*cli, **kwargs)

        self.argument_containers.append(container)

    def parse_args(self, args=None, values=None):
        args = ArgumentParser.parse_args(self, args, values)
        for container in self.argument_containers:
            if hasattr(container, 'parse_args_handler'):
                container.parse_args_handler(args)
        return args

    def _get_preferences(self, prefs_files, prefs_args):
        """
        return user defined profile preferences as a dict
        """
        # object that will hold the preferences
        prefs = mozprofile.prefs.Preferences()

        # add preferences files
        if prefs_files:
            for prefs_file in prefs_files:
                prefs.add_file(prefs_file)

        separator = ':'
        cli_prefs = []
        if prefs_args:
            for pref in prefs_args:
                if separator not in pref:
                    continue
                cli_prefs.append(pref.split(separator, 1))

        # string preferences
        prefs.add(cli_prefs, cast=True)

        return dict(prefs())

    def verify_usage(self, args):
        if not args.tests:
            print 'must specify one or more test files, manifests, or directories'
            sys.exit(1)

        for path in args.tests:
            if not os.path.exists(path):
                print '{0} does not exist'.format(path)
                sys.exit(1)

        if not args.emulator and not args.address and not args.binary:
            print 'must specify --binary, --emulator or --address'
            sys.exit(1)

        if args.emulator and args.binary:
            print 'can\'t specify both --emulator and --binary'
            sys.exit(1)

        # check for valid resolution string, strip whitespaces
        try:
            if args.emulator_res:
                dims = args.emulator_res.split('x')
                assert len(dims) == 2
                width = str(int(dims[0]))
                height = str(int(dims[1]))
                args.emulator_res = 'x'.join([width, height])
        except:
            raise ValueError('Invalid emulator resolution format. '
                             'Should be like "480x800".')

        if args.total_chunks is not None and args.this_chunk is None:
            self.error('You must specify which chunk to run.')

        if args.this_chunk is not None and args.total_chunks is None:
            self.error('You must specify how many chunks to split the tests into.')

        if args.total_chunks is not None:
            if not 1 <= args.total_chunks:
                self.error('Total chunks must be greater than 1.')
            if not 1 <= args.this_chunk <= args.total_chunks:
                self.error('Chunk to run must be between 1 and %s.' % args.total_chunks)

        if args.jsdebugger:
            args.app_args.append('-jsdebugger')
            args.socket_timeout = None

        args.prefs = self._get_preferences(args.prefs_files, args.prefs_args)

        if args.e10s:
            args.prefs.update({
                'browser.tabs.remote.autostart': True,
                'extensions.e10sBlocksEnabling': False
            })

        for container in self.argument_containers:
            if hasattr(container, 'verify_usage_handler'):
                container.verify_usage_handler(args)

        return args


class BaseMarionetteTestRunner(object):

    textrunnerclass = MarionetteTextTestRunner
    driverclass = Marionette

    def __init__(self, address=None, emulator=None, emulator_binary=None,
                 emulator_img=None, emulator_res='480x800', homedir=None,
                 app=None, app_args=None, binary=None, profile=None,
                 logger=None, no_window=False, logdir=None, logcat_stdout=False,
                 repeat=0, testvars=None, tree=None, type=None,
                 device_serial=None, symbols_path=None, timeout=None,
                 shuffle=False, shuffle_seed=random.randint(0, sys.maxint),
                 sdcard=None, this_chunk=1, total_chunks=1, sources=None,
                 server_root=None, gecko_log=None, result_callbacks=None,
                 adb_host=None, adb_port=None, prefs=None, test_tags=None,
                 socket_timeout=BaseMarionetteArguments.socket_timeout_default,
                 startup_timeout=None, addons=None, workspace=None,
                 verbose=0, **kwargs):
        self.address = address
        self.emulator = emulator
        self.emulator_binary = emulator_binary
        self.emulator_img = emulator_img
        self.emulator_res = emulator_res
        self.homedir = homedir
        self.app = app
        self.app_args = app_args or []
        self.bin = binary
        self.profile = profile
        self.addons = addons
        self.logger = logger
        self.no_window = no_window
        self.httpd = None
        self.marionette = None
        self.logdir = logdir
        self.logcat_stdout = logcat_stdout
        self.repeat = repeat
        self.test_kwargs = kwargs
        self.tree = tree
        self.type = type
        self.device_serial = device_serial
        self.symbols_path = symbols_path
        self.timeout = timeout
        self.socket_timeout = socket_timeout
        self._device = None
        self._capabilities = None
        self._appinfo = None
        self._appName = None
        self.shuffle = shuffle
        self.shuffle_seed = shuffle_seed
        self.sdcard = sdcard
        self.sources = sources
        self.server_root = server_root
        self.this_chunk = this_chunk
        self.total_chunks = total_chunks
        self.mixin_run_tests = []
        self.manifest_skipped_tests = []
        self.tests = []
        self.result_callbacks = result_callbacks or []
        self._adb_host = adb_host
        self._adb_port = adb_port
        self.prefs = prefs or {}
        self.test_tags = test_tags
        self.startup_timeout = startup_timeout
        self.workspace = workspace
        # If no workspace is set, default location for logcat and gecko.log is .
        # and default location for profile is TMP
        self.workspace_path = workspace or os.getcwd()
        self.verbose = verbose

        def gather_debug(test, status):
            rv = {}
            marionette = test._marionette_weakref()

            # In the event we're gathering debug without starting a session, skip marionette commands
            if marionette.session is not None:
                try:
                    with marionette.using_context(marionette.CONTEXT_CHROME):
                        rv['screenshot'] = marionette.screenshot()
                    with marionette.using_context(marionette.CONTEXT_CONTENT):
                        rv['source'] = marionette.page_source
                except Exception:
                    logger = get_default_logger()
                    logger.warning('Failed to gather test failure debug.', exc_info=True)
            return rv

        self.result_callbacks.append(gather_debug)

        def update(d, u):
            """ Update a dictionary that may contain nested dictionaries. """
            for k, v in u.iteritems():
                o = d.get(k, {})
                if isinstance(v, dict) and isinstance(o, dict):
                    d[k] = update(d.get(k, {}), v)
                else:
                    d[k] = u[k]
            return d

        self.testvars = {}
        if testvars is not None:
            for path in list(testvars):
                if not os.path.exists(path):
                    raise IOError('--testvars file %s does not exist' % path)
                try:
                    with open(path) as f:
                        self.testvars = update(self.testvars,
                                               json.loads(f.read()))
                except ValueError as e:
                    raise Exception("JSON file (%s) is not properly "
                                    "formatted: %s" % (os.path.abspath(path),
                                                       e.message))

        # set up test handlers
        self.test_handlers = []

        self.reset_test_stats()

        self.logger.info('Using workspace for temporary data: '
                         '"{}"'.format(self.workspace_path))

        if self.emulator and not self.logdir:
            self.logdir = os.path.join(self.workspace_path or '', 'logcat')
        if self.logdir and not os.access(self.logdir, os.F_OK):
                os.mkdir(self.logdir)

        if not gecko_log:
            self.gecko_log = os.path.join(self.workspace_path or '', 'gecko.log')
        else:
            self.gecko_log = gecko_log

        self.results = []

    @property
    def capabilities(self):
        if self._capabilities:
            return self._capabilities

        self.marionette.start_session()
        self._capabilities = self.marionette.session_capabilities
        self.marionette.delete_session()
        return self._capabilities

    @property
    def appinfo(self):
        if self._appinfo:
            return self._appinfo

        self.marionette.start_session()
        with self.marionette.using_context('chrome'):
            self._appinfo = self.marionette.execute_script("""
            try {
              return Services.appinfo;
            } catch (e) {
              return null;
            }""")
        self.marionette.delete_session()
        self._appinfo = self._appinfo or {}
        return self._appinfo

    @property
    def device(self):
        if self._device:
            return self._device

        self._device = self.capabilities.get('device')
        return self._device

    @property
    def appName(self):
        if self._appName:
            return self._appName

        self._appName = self.capabilities.get('browserName')
        return self._appName

    @property
    def bin(self):
        return self._bin

    @bin.setter
    def bin(self, path):
        """
        Set binary and reset parts of runner accordingly

        Intended use: to change binary between calls to run_tests
        """
        self._bin = path
        self.tests = []
        if hasattr(self, 'marionette') and self.marionette:
            self.marionette.cleanup()
            if self.marionette.instance:
                self.marionette.instance = None
        self.marionette = None

    def reset_test_stats(self):
        self.passed = 0
        self.failed = 0
        self.crashed = 0
        self.unexpected_successes = 0
        self.todo = 0
        self.skipped = 0
        self.failures = []

    def _build_kwargs(self):
        kwargs = {
            'device_serial': self.device_serial,
            'symbols_path': self.symbols_path,
            'timeout': self.timeout,
            'socket_timeout': self.socket_timeout,
            'adb_host': self._adb_host,
            'adb_port': self._adb_port,
            'prefs': self.prefs,
            'startup_timeout': self.startup_timeout,
            'verbose': self.verbose,
        }
        if self.bin:
            kwargs.update({
                'host': 'localhost',
                'port': 2828,
                'app': self.app,
                'app_args': self.app_args,
                'bin': self.bin,
                'profile': self.profile,
                'addons': self.addons,
                'gecko_log': self.gecko_log,
            })

        if self.emulator:
            kwargs.update({
                'homedir': self.homedir,
                'logdir': self.logdir,
            })

        if self.address:
            host, port = self.address.split(':')
            kwargs.update({
                'host': host,
                'port': int(port),
            })
            if self.emulator:
                kwargs['connectToRunningEmulator'] = True

            if not self.bin:
                try:
                    #establish a socket connection so we can vertify the data come back
                    connection = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                    connection.connect((host,int(port)))
                    connection.close()
                except Exception, e:
                    raise Exception("Connection attempt to %s:%s failed with error: %s" %(host,port,e))
        elif self.emulator:
            kwargs.update({
                'emulator': self.emulator,
                'emulator_binary': self.emulator_binary,
                'emulator_img': self.emulator_img,
                'emulator_res': self.emulator_res,
                'no_window': self.no_window,
                'sdcard': self.sdcard,
            })
        if self.workspace:
            kwargs['workspace'] = self.workspace_path
        return kwargs

    def start_marionette(self):
        self.marionette = self.driverclass(**self._build_kwargs())

    def launch_test_container(self):
        if self.marionette.session is None:
            self.marionette.start_session()
        self.marionette.set_context(self.marionette.CONTEXT_CONTENT)

        result = self.marionette.execute_async_script("""
if((navigator.mozSettings == undefined) || (navigator.mozSettings == null) || (navigator.mozApps == undefined) || (navigator.mozApps == null)) {
    marionetteScriptFinished(false);
    return;
}
let setReq = navigator.mozSettings.createLock().set({'lockscreen.enabled': false});
setReq.onsuccess = function() {
    let appName = 'Test Container';
    let activeApp = window.wrappedJSObject.Service.currentApp;

    // if the Test Container is already open then do nothing
    if(activeApp.name === appName){
        marionetteScriptFinished(true);
    }

    let appsReq = navigator.mozApps.mgmt.getAll();
    appsReq.onsuccess = function() {
        let apps = appsReq.result;
        for (let i = 0; i < apps.length; i++) {
            let app = apps[i];
            if (app.manifest.name === appName) {
                app.launch();
                window.addEventListener('appopen', function apploadtime(){
                    window.removeEventListener('appopen', apploadtime);
                    marionetteScriptFinished(true);
                });
                return;
            }
        }
        marionetteScriptFinished(false);
    }
    appsReq.onerror = function() {
        marionetteScriptFinished(false);
    }
}
setReq.onerror = function() {
    marionetteScriptFinished(false);
}""", script_timeout=60000)

        if not result:
            raise Exception("Could not launch test container app")

    def record_crash(self):
        crash = True
        try:
            crash = self.marionette.check_for_crash()
            self.crashed += int(crash)
        except Exception:
            traceback.print_exc()
        return crash

    def run_tests(self, tests):
        assert len(tests) > 0
        assert len(self.test_handlers) > 0
        self.reset_test_stats()
        self.start_time = time.time()

        need_external_ip = True
        if not self.marionette:
            self.start_marionette()
            # if we're working against a desktop version, we usually don't need
            # an external ip
            if self.capabilities['device'] == "desktop":
                need_external_ip = False
        self.logger.info('Initial Profile Destination is '
                         '"{}"'.format(self.marionette.profile_path))

        # Gaia sets server_root and that means we shouldn't spin up our own httpd
        if not self.httpd:
            if self.server_root is None or os.path.isdir(self.server_root):
                self.logger.info("starting httpd")
                self.start_httpd(need_external_ip)
                self.marionette.baseurl = self.httpd.get_url()
                self.logger.info("running httpd on %s" % self.marionette.baseurl)
            else:
                self.marionette.baseurl = self.server_root
                self.logger.info("using remote content from %s" % self.marionette.baseurl)

        device_info = None
        if self.capabilities['device'] != 'desktop' and self.capabilities['browserName'] == 'B2G':
            dm = get_dm(self.marionette)
            device_info = dm.getInfo()
            # Add Android version (SDK level) to mozinfo so that manifest entries
            # can be conditional on android_version.
            androidVersion = dm.shellCheckOutput(['getprop', 'ro.build.version.sdk'])
            self.logger.info(
                "Android sdk version '%s'; will use this to filter manifests" % androidVersion)
            mozinfo.info['android_version'] = androidVersion

        for test in tests:
            self.add_test(test)

        # ensure we have only tests files with names starting with 'test_'
        invalid_tests = \
            [t['filepath'] for t in self.tests
             if not os.path.basename(t['filepath']).startswith('test_')]
        if invalid_tests:
            raise Exception("Tests file names must starts with 'test_'."
                            " Invalid test names:\n  %s"
                            % '\n  '.join(invalid_tests))

        version_info = mozversion.get_version(binary=self.bin,
                                              sources=self.sources,
                                              dm_type=os.environ.get('DM_TRANS', 'adb'),
                                              device_serial=self.device_serial,
                                              adb_host=self.marionette.adb_host,
                                              adb_port=self.marionette.adb_port)

        self.logger.suite_start(self.tests,
                                version_info=version_info,
                                device_info=device_info)

        for test in self.manifest_skipped_tests:
            name = os.path.basename(test['path'])
            self.logger.test_start(name)
            self.logger.test_end(name,
                                 'SKIP',
                                 message=test['disabled'])
            self.todo += 1

        interrupted = None
        try:
            counter = self.repeat
            while counter >=0:
                round = self.repeat - counter
                if round > 0:
                    self.logger.info('\nREPEAT %d\n-------' % round)
                self.run_test_sets()
                counter -= 1
        except KeyboardInterrupt:
            # in case of KeyboardInterrupt during the test execution
            # we want to display current test results.
            # so we keep the exception to raise it later.
            interrupted = sys.exc_info()
        try:
            self._print_summary(tests)
        except:
            # raise only the exception if we were not interrupted
            if not interrupted:
                raise
        finally:
            # reraise previous interruption now
            if interrupted:
                raise interrupted[0], interrupted[1], interrupted[2]

    def _print_summary(self, tests):
        self.logger.info('\nSUMMARY\n-------')
        self.logger.info('passed: %d' % self.passed)
        if self.unexpected_successes == 0:
            self.logger.info('failed: %d' % self.failed)
        else:
            self.logger.info('failed: %d (unexpected sucesses: %d)' % (self.failed, self.unexpected_successes))
        if self.skipped == 0:
            self.logger.info('todo: %d' % self.todo)
        else:
            self.logger.info('todo: %d (skipped: %d)' % (self.todo, self.skipped))

        if self.failed > 0:
            self.logger.info('\nFAILED TESTS\n-------')
            for failed_test in self.failures:
                self.logger.info('%s' % failed_test[0])

        self.record_crash()
        self.end_time = time.time()
        self.elapsedtime = self.end_time - self.start_time

        if self.marionette.instance:
            self.marionette.instance.close()
            self.marionette.instance = None

        self.marionette.cleanup()

        for run_tests in self.mixin_run_tests:
            run_tests(tests)
        if self.shuffle:
            self.logger.info("Using seed where seed is:%d" % self.shuffle_seed)

        self.logger.suite_end()

    def start_httpd(self, need_external_ip):
        warnings.warn("start_httpd has been deprecated in favour of create_httpd",
            DeprecationWarning)
        self.httpd = self.create_httpd(need_external_ip)

    def create_httpd(self, need_external_ip):
        host = "127.0.0.1"
        if need_external_ip:
            host = moznetwork.get_ip()
        root = self.server_root or os.path.join(os.path.dirname(here), "www")
        rv = httpd.FixtureServer(root, host=host)
        rv.start()
        return rv

    def add_test(self, test, expected='pass', test_container=None):
        filepath = os.path.abspath(test)

        if os.path.isdir(filepath):
            for root, dirs, files in os.walk(filepath):
                for filename in files:
                    if (filename.startswith('test_') and
                        (filename.endswith('.py') or filename.endswith('.js'))):
                        filepath = os.path.join(root, filename)
                        self.add_test(filepath)
            return

        testargs = {}
        if self.type is not None:
            testtypes = self.type.replace('+', ' +').replace('-', ' -').split()
            for atype in testtypes:
                if atype.startswith('+'):
                    testargs.update({ atype[1:]: 'true' })
                elif atype.startswith('-'):
                    testargs.update({ atype[1:]: 'false' })
                else:
                    testargs.update({ atype: 'true' })

        testarg_b2g = bool(testargs.get('b2g'))

        file_ext = os.path.splitext(os.path.split(filepath)[-1])[1]

        if file_ext == '.ini':
            manifest = TestManifest()
            manifest.read(filepath)

            filters = []
            if self.test_tags:
                filters.append(tags(self.test_tags))
            e10s = self.appinfo.get('browserTabsRemoteAutostart', False)
            json_path = update_mozinfo(filepath)
            self.logger.info("mozinfo updated with the following: {}".format(None))
            manifest_tests = manifest.active_tests(exists=False,
                                                   disabled=True,
                                                   filters=filters,
                                                   device=self.device,
                                                   app=self.appName,
                                                   e10s=e10s,
                                                   **mozinfo.info)
            if len(manifest_tests) == 0:
                self.logger.error("no tests to run using specified "
                                  "combination of filters: {}".format(
                                       manifest.fmt_filters()))

            unfiltered_tests = []
            for test in manifest_tests:
                if test.get('disabled'):
                    self.manifest_skipped_tests.append(test)
                else:
                    unfiltered_tests.append(test)

            target_tests = manifest.get(tests=unfiltered_tests, **testargs)
            for test in unfiltered_tests:
                if test['path'] not in [x['path'] for x in target_tests]:
                    test.setdefault('disabled', 'filtered by type (%s)' % self.type)
                    self.manifest_skipped_tests.append(test)

            for i in target_tests:
                if not os.path.exists(i["path"]):
                    raise IOError("test file: %s does not exist" % i["path"])

                file_ext = os.path.splitext(os.path.split(i['path'])[-1])[-1]
                test_container = None
                if i.get('test_container') and testarg_b2g:
                    if i.get('test_container') == "true":
                        test_container = True
                    elif i.get('test_container') == "false":
                        test_container = False

                self.add_test(i["path"], i["expected"], test_container)
            return

        self.tests.append({'filepath': filepath, 'expected': expected, 'test_container': test_container})

    def run_test(self, filepath, expected, test_container):

        testloader = unittest.TestLoader()
        suite = unittest.TestSuite()
        self.test_kwargs['expected'] = expected
        self.test_kwargs['test_container'] = test_container
        mod_name = os.path.splitext(os.path.split(filepath)[-1])[0]
        for handler in self.test_handlers:
            if handler.match(os.path.basename(filepath)):
                handler.add_tests_to_suite(mod_name,
                                           filepath,
                                           suite,
                                           testloader,
                                           self.marionette,
                                           self.testvars,
                                           **self.test_kwargs)
                break

        if suite.countTestCases():
            runner = self.textrunnerclass(logger=self.logger,
                                          marionette=self.marionette,
                                          capabilities=self.capabilities,
                                          logcat_stdout=self.logcat_stdout,
                                          result_callbacks=self.result_callbacks)

            if test_container:
                self.launch_test_container()

            results = runner.run(suite)
            self.results.append(results)

            self.failed += len(results.failures) + len(results.errors)
            if hasattr(results, 'skipped'):
                self.skipped += len(results.skipped)
                self.todo += len(results.skipped)
            self.passed += results.passed
            for failure in results.failures + results.errors:
                self.failures.append((results.getInfo(failure), failure.output, 'TEST-UNEXPECTED-FAIL'))
            if hasattr(results, 'unexpectedSuccesses'):
                self.failed += len(results.unexpectedSuccesses)
                self.unexpected_successes += len(results.unexpectedSuccesses)
                for failure in results.unexpectedSuccesses:
                    self.failures.append((results.getInfo(failure), failure.output, 'TEST-UNEXPECTED-PASS'))
            if hasattr(results, 'expectedFailures'):
                self.todo += len(results.expectedFailures)

            self.mixin_run_tests = []
            for result in self.results:
                result.result_modifiers = []

    def run_test_set(self, tests):
        if self.shuffle:
            random.seed(self.shuffle_seed)
            random.shuffle(tests)

        for test in tests:
            self.run_test(test['filepath'], test['expected'], test['test_container'])
            if self.record_crash():
                break

    def run_test_sets(self):
        if len(self.tests) < 1:
            raise Exception('There are no tests to run.')
        elif self.total_chunks > len(self.tests):
            raise ValueError('Total number of chunks must be between 1 and %d.' % len(self.tests))
        if self.total_chunks > 1:
            chunks = [[] for i in range(self.total_chunks)]
            for i, test in enumerate(self.tests):
                target_chunk = i % self.total_chunks
                chunks[target_chunk].append(test)

            self.logger.info('Running chunk %d of %d (%d tests selected from a '
                             'total of %d)' % (self.this_chunk, self.total_chunks,
                                               len(chunks[self.this_chunk - 1]),
                                               len(self.tests)))
            self.tests = chunks[self.this_chunk - 1]

        self.run_test_set(self.tests)

    def cleanup(self):
        if self.httpd:
            self.httpd.stop()

        if self.marionette:
            self.marionette.cleanup()

    __del__ = cleanup