      1 if (Widgets === undefined) {
      2   var Widgets = {};
      3 }
      4
      5 Widgets.ResultListWidget = function (options) {
      6   pockets.ui.ListWidget.call(this, options);
      7
      8 };
      9 Widgets.ResultListWidget.inheritsFrom(pockets.ui.ListWidget);
     10
     11 Widgets.ResultListWidget.prototype.addSpecResult = function(spec) {
     12   this.addItemToList(this.formatResult(spec));
     13 };
     14
     15 Widgets.ResultListWidget.prototype.formatResult = function (spec) {
     16   var result = spec.results;
     17   return {
     18     spec: spec,
     19     cssClass: result.skipped ? 'pending' : result.passed() ? 'passed' : 'failed',
     20     specDescription: result.description
     21   };
     22 };
     23
     24 pockets.dompp = function(parentNode, o) {
     25   var domPrettyPrinter = new pockets.DomPrettyPrinter();
     26   domPrettyPrinter.format(o);
     27   parentNode.appendChild(domPrettyPrinter.dom);
     28 };
     29
     30 Widgets.ResultListWidget.prototype.renderExpectationResult = function(parentNode, expectationResult, number) {
     31   if (!pockets.test.domPrettyPrint) {
     32     parentNode.innerHTML = expectationResult.message;
     33   } else {
     34     var details = expectationResult.details || {};
     35
     36     parentNode.innerText = 'Expectation ' + number + ': ' + details.matcherName + '() failed:';
     37
     38     if (details.hasOwnProperty('expected') && details.hasOwnProperty('actual')) {
     39       var expectedDiv;
     40       var actualDiv;
     41       parentNode.appendChild(
     42         pockets.dom.create('table', {className: 'failed-expectation-summary', width: '100%'},
     43           pockets.dom.create('tr', {},
     44             pockets.dom.create('th', {}, 'Expected:'),
     45             pockets.dom.create('th', {}, 'Actual:')),
     46           pockets.dom.create('tr', {},
     47             expectedDiv = pockets.dom.create('td', {className: 'expected', width: '50%'}),
     48             actualDiv = pockets.dom.create('td', {className: 'actual', width: '50%'}))));
     49
     50       pockets.dompp(expectedDiv, details.expected);
     51       pockets.dompp(actualDiv, details.actual);
     52     } else {
     53       parentNode.innerHTML = expectationResult.message;
     54     }
     55   }
     56 };
     57
     58 Widgets.ResultListWidget.prototype.onItemRendered = function(listWidget, itemModel, itemNode) {
     59   var results = itemModel.spec.results;
     60
     61   if (results.failedCount > 0) {
     62     var messagesDiv = itemNode.querySelector('.messages');
     63
     64     messagesDiv.appendChild(pockets.dom.create('span', {}, results.passedCount + ' passed, ' + results.failedCount + ' failed.'));
     65     messagesDiv.appendChild(pockets.dom.create('br'));
     66     messagesDiv.appendChild(pockets.dom.create('br'));
     67
     68     var failureMessagesDiv = pockets.dom.create('ul', {className: 'failureMessages'});
     69     var expectationNumber = 1;
     70     var resultItems = results.getItems();
     71     for (var i = 0; i < resultItems.length; i++) {
     72       var expectationResult = resultItems[i];
     73
     74       if (expectationResult.type == 'ExpectationResult') {
     75         if (!expectationResult.passed) {
     76           var failureMessageDiv = pockets.dom.create('li');
     77           this.renderExpectationResult(failureMessageDiv, expectationResult, expectationNumber);
     78           failureMessagesDiv.appendChild(failureMessageDiv);
     79         }
     80         expectationNumber++;
     81       } else {
     82         var messageDiv = pockets.dom.create('li', {}, expectationResult.text);
     83         messagesDiv.appendChild(messageDiv);
     84       }
     85     }
     86     messagesDiv.appendChild(failureMessagesDiv);
     87   }
     88
     89   Mojo.Event.listen(itemNode, Mojo.Event.tap, function() {
     90     document.location = TestAssistant.focusUrl({description: itemModel.spec.description});
     91   }.bind(this));
     92 };
     93
     94
     95
     96 Widgets.TestListWidget = function (options, templatePath) {
     97   options.listTemplate = templatePath !== undefined ? templatePath + 'test/suite' : 'test/suite';
     98   options.itemTemplate = templatePath !== undefined ? templatePath + 'test/result' : 'test/result';
     99
    100   Widgets.ResultListWidget.call(this, options);
    101
    102 };
    103 Widgets.TestListWidget.inheritsFrom(Widgets.ResultListWidget);
    104
    105 Widgets.TestListWidget.prototype.addSpecResult = function(spec) {
    106   this.addItemToList(this.formatResult(spec));
    107 };
    108
    109
    110
    111 Widgets.SuiteListWidget = function(options, templatePath) {
    112   options.listTemplate = templatePath !== undefined ? templatePath + 'test/suites' : 'test/suites';
    113   options.itemTemplate = templatePath !== undefined ? templatePath + 'test/suiteItem' : 'test/suiteItem';
    114
    115   Widgets.ResultListWidget.call(this, options, templatePath);
    116 };
    117 Widgets.SuiteListWidget.inheritsFrom(Widgets.ResultListWidget);
    118
    119 Widgets.SuiteListWidget.prototype.addSuite = function(suite) {
    120   var results = suite.getResults();
    121   this.addItemToList({
    122     suite: results,
    123     cssClass: results.passed() ? "passed" : "failed",
    124     suiteName: suite.getFullName(),
    125     passedCount: results.passedCount,
    126     totalCount: results.totalCount
    127   });
    128 };
    129
    130 Widgets.SuiteListWidget.prototype.onItemRendered = function(listWidget, itemModel, itemNode) {
    131   Mojo.Event.listen(itemNode, Mojo.Event.tap, function() {
    132     document.location = TestAssistant.focusUrl({suite: itemModel.suite.description});
    133   }.bind(this));
    134 };
    135