1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// Copyright (C) 2013 Google Inc. All rights reserved.
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)//
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// modification, are permitted provided that the following conditions are
5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// met:
6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)//
7926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)//     * Redistributions of source code must retain the above copyright
8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// notice, this list of conditions and the following disclaimer.
9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)//     * Redistributions in binary form must reproduce the above
10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer
11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// in the documentation and/or other materials provided with the
12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// distribution.
13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// contributors may be used to endorse or promote products derived from
15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// this software without specific prior written permission.
16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)//
17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
2953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)var defaultDashboardSpecificStateValues = {
3053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    builder: null,
3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    treemapfocus: '',
3253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)};
3353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)var DB_SPECIFIC_INVALIDATING_PARAMETERS = {
3553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    'testType': 'builder',
3653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    'group': 'builder'
3753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)};
3853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
395267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)var g_haveEverGeneratedPage = false;
405267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
4153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)function generatePage(historyInstance)
4253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
435267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    g_haveEverGeneratedPage = true;
4453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    $('header-container').innerHTML = ui.html.testTypeSwitcher();
4553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
4653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    g_isGeneratingPage = true;
4753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
4853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    var rawTree = g_resultsByBuilder[historyInstance.dashboardSpecificState.builder || currentBuilderGroup().defaultBuilder()];
495267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    g_webTree = convertToWebTreemapFormat('AllTests', rawTree);
5053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    appendTreemap($('map'), g_webTree);
5153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
5253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    if (historyInstance.dashboardSpecificState.treemapfocus)
5353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        focusPath(g_webTree, historyInstance.dashboardSpecificState.treemapfocus)
5453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
5553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    g_isGeneratingPage = false;
5653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
5753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
5853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)function handleValidHashParameter(historyInstance, key, value)
5953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
6053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    switch(key) {
6153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    case 'builder':
6253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        history.validateParameter(historyInstance.dashboardSpecificState, key, value,
6353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)            function() { return value in currentBuilders(); });
6453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return true;
6553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    case 'treemapfocus':
6753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        history.validateParameter(historyInstance.dashboardSpecificState, key, value,
6853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)            function() {
695267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)                return value.match(/^[\w./]+$/);
7053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)            });
7153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return true;
7253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
7353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    default:
7453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return false;
7553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    }
7653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
7753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
7853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)function handleQueryParameterChange(historyInstance, params)
7953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
8053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    for (var param in params) {
815267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        // When we're first loading the page, if there is a treemapfocus parameter,
825267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        // it will show up here. After we've generated the page, treemapfocus parameter
835267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        // changes should just be handled by the treemap code instead of calling through
845267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        // to generatePage.
855267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        if (!g_haveEverGeneratedPage || param != 'treemapfocus') {
8653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)            $('map').innerHTML = 'Loading...';
8753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)            return true;
8853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        }
8953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    }
9053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    return false;
9153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
9253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
9353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)var treemapConfig = {
9453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    defaultStateValues: defaultDashboardSpecificStateValues,
9553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    generatePage: generatePage,
9653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    handleValidHashParameter: handleValidHashParameter,
9753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    handleQueryParameterChange: handleQueryParameterChange,
9853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    invalidatingHashParameters: DB_SPECIFIC_INVALIDATING_PARAMETERS
9953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)};
10053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
10153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// FIXME(jparent): Eventually remove all usage of global history object.
10253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)var g_history = new history.History(treemapConfig);
10353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)g_history.parseCrossDashboardParameters();
10453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
10593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)var TEST_URL_BASE_PATH = "http://src.chromium.org/blink/trunk/";
106926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
107926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)function humanReadableTime(milliseconds)
108926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
109926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (milliseconds < 1000)
110926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return Math.floor(milliseconds) + 'ms';
111926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    else if (milliseconds < 60000)
112926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return (milliseconds / 1000).toPrecision(2) + 's';
113926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
114926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var minutes = Math.floor(milliseconds / 60000);
115926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var seconds = Math.floor((milliseconds - minutes * 60000) / 1000);
116926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return minutes + 'm' + seconds + 's';
117926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
118926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
119926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// This looks like:
120926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// { "data": {"$area": (sum of all timings)},
121926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)//   "name": (name of this node),
122926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)//   "children": [ (child nodes, in the same format as this) ] }
123926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// childCount is added just to be includes in the node's name
124926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)function convertToWebTreemapFormat(treename, tree, path)
125926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
126926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var total = 0;
127926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var childCount = 0;
128926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var children = [];
129926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    for (var name in tree) {
130926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var treeNode = tree[name];
131926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if (typeof treeNode == "number") {
132926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            var time = treeNode;
133926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            var node = {
134926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                "data": {"$area": time},
135926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                "name": name + " (" + humanReadableTime(time) + ")"
136926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            };
137926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            children.push(node);
138926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            total += time;
139926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            childCount++;
140926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        } else {
141926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            var newPath = path ? path + '/' + name : name;
142926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            var subtree = convertToWebTreemapFormat(name, treeNode, newPath);
143926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            children.push(subtree);
144926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            total += subtree["data"]["$area"];
145926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            childCount += subtree["childCount"];
146926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        }
147926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
148926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
149926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    children.sort(function(a, b) {
150926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        aTime = a.data["$area"]
151926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        bTime = b.data["$area"]
152926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return bTime - aTime;
153926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    });
154926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
155926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return {
156926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        "data": {"$area": total},
157926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        "name": treename + " (" + humanReadableTime(total) + " - " + childCount + " tests)",
158926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        "children": children,
159926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        "childCount": childCount,
160926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        "path": path
161926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    };
162926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
163926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
164926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)function listOfAllNonLeafNodes(tree, list)
165926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
166926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (!tree.children)
167926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return;
168926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
169926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (!list)
170926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        list = [];
171926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    list.push(tree);
172926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
173926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    tree.children.forEach(function(child) {
174926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        listOfAllNonLeafNodes(child, list);
175926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    });
176926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return list;
177926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
178926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
179926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)function reverseSortByAverage(list)
180926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
181926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    list.sort(function(a, b) {
182926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var avgA = a.data['$area'] / a.childCount;
183926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var avgB = b.data['$area'] / b.childCount;
184926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return avgB - avgA;
185926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    });
186926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
187926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
188926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)function showAverages()
189926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
190926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (!document.getElementById('map'))
191926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return;
192926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
193926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var table = document.createElement('table');
194926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    table.innerHTML = '<th>directory</th><th># tests</th><th>avg time / test</th>';
195926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
196926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var allNodes = listOfAllNonLeafNodes(g_webTree);
197926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    reverseSortByAverage(allNodes);
198926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    allNodes.forEach(function(node) {
199926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var average = node.data['$area'] / node.childCount;
200926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if (average > 100 && node.childCount != 1) {
201926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            var tr = document.createElement('tr');
202926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            tr.innerHTML = '<td></td><td>' + node.childCount + '</td><td>' + humanReadableTime(average) + '</td>';
203926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            tr.querySelector('td').innerText = node.path;
204926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            table.appendChild(tr);
205926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        }
206926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    });
207926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
208926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var map = document.getElementById('map');
209926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    map.parentNode.replaceChild(table, map);
210926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
211926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
212926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)var g_isGeneratingPage = false;
213926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)var g_webTree;
214926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
215926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)function focusPath(tree, path)
216926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
217926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var parts = decodeURIComponent(path).split('/');
218926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (extractName(tree) != parts[0]) {
219926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        console.error('Could not focus tree rooted at ' + parts[0]);
220926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return;
221926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
222926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
223926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    for (var i = 1; i < parts.length; i++) {
224926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var children = tree.children;
225926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        for (var j = 0; j < children.length; j++) {
226926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            var child = children[j];
227926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            if (extractName(child) == parts[i]) {
228926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                tree = child;
229926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                focus(tree);
230926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                break;
231926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            }
232926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        }
233926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if (j == children.length) {
234926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            console.error('Could not find tree at ' + parts[i]);
235926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            break;
236926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        }
237926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
238926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
239926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
240926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
241926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)function extractName(node)
242926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
243926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return node.name.split(' ')[0];
244926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
245926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
246926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)function fullName(node)
247926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
248926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var buffer = [extractName(node)];
249926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    while (node.parent) {
250926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        node = node.parent;
251926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        buffer.unshift(extractName(node));
252926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
253926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    return buffer.join('/');
254926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
255926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
256926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)function handleFocus(tree)
257926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){
258926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var currentlyFocusedNode = $('focused-leaf');
259926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (currentlyFocusedNode)
260926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        currentlyFocusedNode.id = '';
261926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
262926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (!tree.children)
263926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        tree.dom.id = 'focused-leaf';
264926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
265926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var name = fullName(tree);
266926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
267926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (!tree.children && !tree.extraDom && g_history.isLayoutTestResults()) {
268926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        tree.extraDom = document.createElement('pre');
269926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        tree.extraDom.className = 'extra-dom';
270926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        tree.dom.appendChild(tree.extraDom);
271926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
272926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        loader.request(TEST_URL_BASE_PATH + name,
273926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            function(xhr) {
274926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                tree.extraDom.onmousedown = function(e) {
275926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                    e.stopPropagation();
276926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                };
277926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                tree.extraDom.textContent = xhr.responseText;
278926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            },
279926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            function (xhr) {
280926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)                tree.extraDom.textContent = "Could not load test."
281926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        });
282926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
283926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
284926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // We don't want the focus calls during generatePage to try to modify the query state.
285926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if (!g_isGeneratingPage)
286926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        g_history.setQueryParameter('treemapfocus', name);
287926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
288926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
289926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)window.addEventListener('load', function() {
29053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    var resourceLoader = new loader.Loader();
291926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    resourceLoader.load();
292926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}, false);
293