1/**
2 * Copyright (c) 2018 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you
5 * may not use this file except in compliance with the License. You may
6 * obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 * implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
16
17/**
18 * Display the log links in a modal window.
19 * @param linkList A list of [name, url] tuples representing log links.
20 */
21function showLinks(container, linkList) {
22  if (!linkList || linkList.length == 0) return;
23
24  var logCollection = $('<ul class="collection"></ul>');
25  var entries = linkList.reduce(function(acc, entry) {
26    if (!entry || entry.length == 0) return acc;
27    var link = '<a href="' + entry[1] + '"';
28    link += 'class="collection-item">' + entry[0] + '</li>';
29    return acc + link;
30  }, '');
31  logCollection.html(entries);
32
33  if (container.find('#info-modal').length == 0) {
34    var modal =
35        $('<div id="info-modal" class="modal modal-fixed-footer"></div>');
36    var content = $('<div class="modal-content"></div>');
37    content.append('<h4>Links</h4>');
38    content.append('<div class="info-container"></div>');
39    content.appendTo(modal);
40    var footer = $('<div class="modal-footer"></div>');
41    footer.append('<a class="btn-flat modal-close">Close</a></div>');
42    footer.appendTo(modal);
43    modal.appendTo(container);
44  }
45  var infoContainer = $('#info-modal>.modal-content>.info-container');
46  infoContainer.empty();
47  logCollection.appendTo(infoContainer);
48  $('#info-modal').modal({dismissible: true});
49  $('#info-modal').modal('open');
50}
51
52/**
53 * Get the nickname for a test case result.
54 *
55 * Removes the result prefix and suffix, extracting only the result name.
56 *
57 * @param testCaseResult The string name of a VtsReportMessage.TestCaseResult.
58 * @returns the string nickname of the result.
59 */
60function getNickname(testCaseResult) {
61  return testCaseResult.replace('TEST_CASE_RESULT_', '')
62      .replace('_RESULT', '')
63      .trim()
64      .toLowerCase();
65}
66
67/**
68 * Display test data in the body beneath a test run's metadata.
69 * @param container The jquery object in which to insert the test metadata.
70 * @param data The json object containing the columns to display.
71 * @param lineHeight The height of each list element.
72 */
73function displayTestDetails(container, data, lineHeight) {
74  var nCol = data.length;
75  var width = 's' + (12 / nCol);
76  test = container;
77  var maxLines = 0;
78  data.forEach(function(column, index) {
79    if (column.data == undefined || column.name == undefined) {
80      return;
81    }
82    var classes = 'col test-col grey lighten-5 ' + width;
83    if (index != nCol - 1) {
84      classes += ' bordered';
85    }
86    if (index == 0) {
87      classes += ' left-most';
88    }
89    if (index == nCol - 1) {
90      classes += ' right-most';
91    }
92    var colContainer = $('<div class="' + classes + '"></div>');
93    var col = $('<div class="test-case-container"></div>');
94    colContainer.appendTo(container);
95    var count = column.data.length;
96    var head = $('<h5 class="test-result-label white"></h5>')
97                   .text(getNickname(column.name))
98                   .appendTo(colContainer)
99                   .css('text-transform', 'capitalize');
100    $('<div class="indicator right center"></div>')
101        .text(count)
102        .addClass(column.name)
103        .appendTo(head);
104    col.appendTo(colContainer);
105    var list = $('<ul></ul>').appendTo(col);
106    column.data.forEach(function(testCase) {
107      $('<li></li>')
108          .text(testCase)
109          .addClass('test-case')
110          .css('font-size', lineHeight - 2)
111          .css('line-height', lineHeight + 'px')
112          .appendTo(list);
113    });
114    if (count > maxLines) {
115      maxLines = count;
116    }
117  });
118  var containers = container.find('.test-case-container');
119  containers.height(maxLines * lineHeight);
120}
121