DXR will be turned off on Tuesday, December 29th. It will redirect to Searchfox.
See the announcement on Discourse.

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 (c68fe15a81fc)

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
#!/usr/bin/env python
# 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
# 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 __future__ import absolute_import

import base64
import base64
import cgi
from datetime import datetime
import os
import json


from .. import base

from collections import defaultdict
import six


html = None
raw = None

base_path = os.path.split(__file__)[0]
base_path = os.path.split(__file__)[0]


def do_defered_imports():
    global html
    global raw

    from .xmlgen import html, raw
    from .xmlgen import html, raw


class HTMLFormatter(base.BaseFormatter):
    """Formatter that produces a simple HTML-formatted report."""


    def __init__(self):
        do_defered_imports()
        self.suite_name = None
        self.result_rows = []
        self.test_count = defaultdict(int)
        self.test_count = defaultdict(int)
        self.start_times = {}
        self.suite_times = {"start": None,
                            "end": None}
                            "end": None}
        self.head = None
        self.env = {}

    def suite_start(self, data):
        self.suite_times["start"] = data["time"]
        self.suite_times["start"] = data["time"]
        self.suite_name = data["source"]
        with open(os.path.join(base_path, "style.css")) as f:
            self.head = html.head(
                html.meta(charset="utf-8"),
                html.title(data["source"]),
                html.title(data["source"]),
                html.style(raw(f.read())))

        date_format = "%d %b %Y %H:%M:%S"
        version_info = data.get("version_info")
        if version_info:
        if version_info:
            self.env["Device identifier"] = version_info.get("device_id")
            self.env["Device firmware (base)"] = version_info.get("device_firmware_version_base")
            self.env["Device firmware (date)"] = (
            self.env["Device firmware (date)"] = (
                datetime.utcfromtimestamp(int(version_info.get("device_firmware_date")))
                .strftime(date_format) if
                "device_firmware_date" in version_info else None)
            self.env["Device firmware (incremental)"] = version_info.get(
                "device_firmware_version_incremental")
                "device_firmware_version_incremental")
            self.env["Device firmware (release)"] = version_info.get(
                "device_firmware_version_release")
            self.env["Gaia date"] = (
                datetime.utcfromtimestamp(int(version_info.get("gaia_date")))
                .strftime(date_format) if
                .strftime(date_format) if
                "gaia_date" in version_info else None)
            self.env["Gecko version"] = version_info.get("application_version")
            self.env["Gecko build"] = version_info.get("application_buildid")


            if version_info.get("application_changeset"):
                self.env["Gecko revision"] = version_info.get("application_changeset")
                if version_info.get("application_repository"):
                    self.env["Gecko revision"] = html.a(
                        version_info.get("application_changeset"),
                        version_info.get("application_changeset"),
                        href="/rev/".join([version_info.get("application_repository"),
                                           version_info.get("application_changeset")]),
                        target="_blank")

            if version_info.get("gaia_changeset"):
            if version_info.get("gaia_changeset"):
                self.env["Gaia revision"] = html.a(
                    version_info.get("gaia_changeset")[:12],
                    href="https://github.com/mozilla-b2g/gaia/commit/%s" % version_info.get(
                        "gaia_changeset"),
                    target="_blank")

        device_info = data.get("device_info")
        if device_info:
            self.env["Device uptime"] = device_info.get("uptime")
            self.env["Device uptime"] = device_info.get("uptime")
            self.env["Device memory"] = device_info.get("memtotal")
            self.env["Device serial"] = device_info.get("id")

    def suite_end(self, data):
        self.suite_times["end"] = data["time"]
        self.suite_times["end"] = data["time"]
        return self.generate_html()

    def test_start(self, data):
        self.start_times[data["test"]] = data["time"]

    def test_end(self, data):
    def test_end(self, data):
        self.make_result_html(data)

    def make_result_html(self, data):
    def make_result_html(self, data):
        tc_time = (data["time"] - self.start_times.pop(data["test"])) / 1000.
        additional_html = []
        debug = data.get("extra", {})
        # Add support for log exported from wptrunner. The structure of
        # reftest_screenshots is listed in wptrunner/executors/base.py.
        # reftest_screenshots is listed in wptrunner/executors/base.py.
        if debug.get('reftest_screenshots'):
            log_data = debug.get("reftest_screenshots", {})
            debug = {
                'image1': 'data:image/png;base64,' + log_data[0].get("screenshot", {}),
                'image2': 'data:image/png;base64,' + log_data[2].get("screenshot", {}),
                'image2': 'data:image/png;base64,' + log_data[2].get("screenshot", {}),
                'differences': "Not Implemented",
            }

        links_html = []


        status = status_name = data["status"]
        expected = data.get("expected", status)
        known_intermittent = data.get("known_intermittent", [])

        if status != expected and status not in known_intermittent:
            status_name = "UNEXPECTED_" + status
            status_name = "UNEXPECTED_" + status
        elif status in known_intermittent:
            status_name = "KNOWN_INTERMITTENT"
        elif status not in ("PASS", "SKIP"):
            status_name = "EXPECTED_" + status


        self.test_count[status_name] += 1

        if status in ['SKIP', 'FAIL', 'PRECONDITION_FAILED', 'ERROR']:
            if debug.get('differences'):
                images = [
                images = [
                    ('image1', 'Image 1 (test)'),
                    ('image2', 'Image 2 (reference)')
                ]
                for title, description in images:
                    screenshot = '%s' % debug[title]
                    additional_html.append(html.div(
                        html.a(html.img(src=screenshot), href="#"),
                        html.a(html.img(src=screenshot), href="#"),
                        html.br(),
                        html.a(description),
                        class_='screenshot'))

            if debug.get('screenshot'):
            if debug.get('screenshot'):
                screenshot = '%s' % debug['screenshot']
                screenshot = 'data:image/png;base64,' + screenshot

                additional_html.append(html.div(
                    html.a(html.img(src=screenshot), href="#"),
                    html.a(html.img(src=screenshot), href="#"),
                    class_='screenshot'))

            for name, content in debug.items():
            for name, content in debug.items():
                if name in ['screenshot', 'image1', 'image2']:
                    if not content.startswith('data:image/png;base64,'):
                        href = 'data:image/png;base64,%s' % content
                    else:
                    else:
                        href = content
                else:
                    if not isinstance(content, (six.text_type, six.binary_type)):
                        # All types must be json serializable
                        content = json.dumps(content)
                        content = json.dumps(content)
                        # Decode to text type if JSON output is byte string
                        if not isinstance(content, six.text_type):
                            content = content.decode('utf-8')
                            content = content.decode('utf-8')
                    # Encode base64 to avoid that some browsers (such as Firefox, Opera)
                    # Encode base64 to avoid that some browsers (such as Firefox, Opera)
                    # treats '#' as the start of another link if it is contained in the data URL.
                    if isinstance(content, six.text_type):
                        is_known_utf8 = True
                        is_known_utf8 = True
                        content_bytes = six.text_type(content).encode('utf-8',
                                                                      'xmlcharrefreplace')
                    else:
                        is_known_utf8 = False
                        is_known_utf8 = False
                        content_bytes = content

                    meta = ["text/html"]
                    if is_known_utf8:
                        meta.append("charset=utf-8")
                        meta.append("charset=utf-8")

                    # base64 is ascii only, which means we don't have to care about encoding
                    # in the case where we don't know the encoding of the input
                    b64_bytes = base64.b64encode(content_bytes)
                    b64_text = b64_bytes.decode()
                    b64_text = b64_bytes.decode()
                    href = "data:%s;base64,%s" % (";".join(meta), b64_text)
                links_html.append(html.a(
                    name.title(),
                    class_=name,
                    href=href,
                    href=href,
                    target='_blank'))
                links_html.append(' ')

            log = html.div(class_='log')
            log = html.div(class_='log')
            output = data.get('stack', '').splitlines()
            output.extend(data.get('message', '').splitlines())
            for line in output:
                separator = line.startswith(' ' * 10)
                if separator:
                if separator:
                    log.append(line[:80])
                else:
                    if line.lower().find("error") != -1 or line.lower().find("exception") != -1:
                        log.append(html.span(raw(cgi.escape(line)), class_='error'))
                    else:
                    else:
                        log.append(raw(cgi.escape(line)))
                log.append(html.br())
            additional_html.append(log)

        self.result_rows.append(
        self.result_rows.append(
            html.tr([html.td(status_name, class_='col-result'),
                     html.td(data['test'], class_='col-name'),
                     html.td('%.2f' % tc_time, class_='col-duration'),
                     html.td('%.2f' % tc_time, class_='col-duration'),
                     html.td(links_html, class_='col-links'),
                     html.td(additional_html, class_='debug')],
                    class_=status_name.lower() + ' results-table-row'))

    def generate_html(self):
    def generate_html(self):
        generated = datetime.utcnow()
        with open(os.path.join(base_path, "main.js")) as main_f:
            doc = html.html(
                self.head,
                html.body(
                html.body(
                    html.script(raw(main_f.read())),
                    html.p('Report generated on %s at %s' % (
                        generated.strftime('%d-%b-%Y'),
                        generated.strftime('%H:%M:%S'))),
                    html.h2('Environment'),
                    html.h2('Environment'),
                    html.table(
                        [html.tr(html.td(k), html.td(v))
                         for k, v in sorted(self.env.items()) if v],
                        id='environment'),


                    html.h2('Summary'),
                    html.p('%i tests ran in %.1f seconds.' % (sum(six.itervalues(self.test_count)),
                                                              (self.suite_times["end"] -
                                                               self.suite_times["start"]) / 1000.),
                           html.br(),
                           html.br(),
                           html.span('%i passed' % self.test_count["PASS"], class_='pass'), ', ',
                           html.span('%i skipped' % self.test_count["SKIP"], class_='skip'), ', ',
                           html.span('%i failed' % self.test_count[
                                     "UNEXPECTED_FAIL"], class_='fail'), ', ',
                           html.span('%i errors' % self.test_count[
                           html.span('%i errors' % self.test_count[
                                     "UNEXPECTED_ERROR"], class_='error'), '.',
                           html.br(),
                           html.span('%i expected failures' % self.test_count["EXPECTED_FAIL"],
                                     class_='expected_fail'), ', ',
                                     class_='expected_fail'), ', ',
                           html.span('%i unexpected passes' % self.test_count["UNEXPECTED_PASS"],
                                     class_='unexpected_pass'), ', ',
                           html.span('%i known intermittent results' %
                                     self.test_count["KNOWN_INTERMITTENT"],
                                     class_='known_intermittent'), '.'),
                                     class_='known_intermittent'), '.'),
                    html.h2('Results'),
                    html.table([html.thead(
                        html.tr([
                            html.th('Result', class_='sortable', col='result'),
                            html.th('Test', class_='sortable', col='name'),
                            html.th('Test', class_='sortable', col='name'),
                            html.th('Duration', class_='sortable numeric', col='duration'),
                            html.th('Links')]), id='results-table-head'),
                        html.tbody(self.result_rows,
                                   id='results-table-body')], id='results-table')))


        return u"<!DOCTYPE html>\n" + doc.unicode(indent=2)