Home | History | Annotate | Download | only in net_internals
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 /**
      6  * This view displays the progress and results from the "connection tester".
      7  *
      8  *   - Has an input box to specify the URL.
      9  *   - Has a button to start running the tests.
     10  *   - Shows the set of experiments that have been run so far, and their
     11  *     result.
     12  */
     13 var TestView = (function() {
     14   'use strict';
     15 
     16   // We inherit from DivView.
     17   var superClass = DivView;
     18 
     19   /**
     20    * @constructor
     21    */
     22   function TestView() {
     23     assertFirstConstructorCall(TestView);
     24 
     25     // Call superclass's constructor.
     26     superClass.call(this, TestView.MAIN_BOX_ID);
     27 
     28     this.urlInput_ = $(TestView.URL_INPUT_ID);
     29     this.summaryDiv_ = $(TestView.SUMMARY_DIV_ID);
     30 
     31     var form = $(TestView.FORM_ID);
     32     form.addEventListener('submit', this.onSubmitForm_.bind(this), false);
     33 
     34     // Register to test information as it's received.
     35     g_browser.addConnectionTestsObserver(this);
     36   }
     37 
     38   TestView.TAB_ID = 'tab-handle-tests';
     39   TestView.TAB_NAME = 'Tests';
     40   TestView.TAB_HASH = '#tests';
     41 
     42   // IDs for special HTML elements in test_view.html
     43   TestView.MAIN_BOX_ID = 'test-view-tab-content';
     44   TestView.FORM_ID = 'test-view-connection-tests-form';
     45   TestView.URL_INPUT_ID = 'test-view-url-input';
     46   TestView.SUMMARY_DIV_ID = 'test-view-summary';
     47   // Needed by tests.
     48   TestView.SUBMIT_BUTTON_ID = 'test-view-connection-tests-submit';
     49 
     50 
     51   cr.addSingletonGetter(TestView);
     52 
     53   TestView.prototype = {
     54     // Inherit the superclass's methods.
     55     __proto__: superClass.prototype,
     56 
     57     onSubmitForm_: function(event) {
     58       g_browser.sendStartConnectionTests(this.urlInput_.value);
     59       event.preventDefault();
     60     },
     61 
     62     /**
     63      * Callback for when the connection tests have begun.
     64      */
     65     onStartedConnectionTestSuite: function() {
     66       this.summaryDiv_.innerHTML = '';
     67 
     68       var p = addNode(this.summaryDiv_, 'p');
     69       addTextNode(p, 'Started connection test suite suite on ');
     70       timeutil.addNodeWithDate(p, new Date());
     71 
     72       // Add a table that will hold the individual test results.
     73       var table = addNode(this.summaryDiv_, 'table');
     74       table.className = 'styled-table';
     75       var thead = addNode(table, 'thead');
     76       thead.innerHTML = '<tr><th>Result</th><th>Experiment</th>' +
     77                         '<th>Error</th><th>Time (ms)</th></tr>';
     78 
     79       this.tbody_ = addNode(table, 'tbody');
     80     },
     81 
     82     /**
     83      * Callback for when an individual test in the suite has begun.
     84      */
     85     onStartedConnectionTestExperiment: function(experiment) {
     86       var tr = addNode(this.tbody_, 'tr');
     87 
     88       var passFailCell = addNode(tr, 'td');
     89 
     90       var experimentCell = addNode(tr, 'td');
     91 
     92       var resultCell = addNode(tr, 'td');
     93       addTextNode(resultCell, '?');
     94 
     95       var dtCell = addNode(tr, 'td');
     96       addTextNode(dtCell, '?');
     97 
     98       // We will fill in result cells with actual values (to replace the
     99       // placeholder '?') once the test has completed. For now we just
    100       // save references to these cells.
    101       this.currentExperimentRow_ = {
    102         experimentCell: experimentCell,
    103         dtCell: dtCell,
    104         resultCell: resultCell,
    105         passFailCell: passFailCell,
    106         startTime: timeutil.getCurrentTime()
    107       };
    108 
    109       addTextNode(experimentCell, 'Fetch ' + experiment.url);
    110 
    111       if (experiment.proxy_settings_experiment ||
    112           experiment.host_resolver_experiment) {
    113         var ul = addNode(experimentCell, 'ul');
    114 
    115         if (experiment.proxy_settings_experiment) {
    116           var li = addNode(ul, 'li');
    117           addTextNode(li, experiment.proxy_settings_experiment);
    118         }
    119 
    120         if (experiment.host_resolver_experiment) {
    121           var li = addNode(ul, 'li');
    122           addTextNode(li, experiment.host_resolver_experiment);
    123         }
    124       }
    125     },
    126 
    127     /**
    128      * Callback for when an individual test in the suite has finished.
    129      */
    130     onCompletedConnectionTestExperiment: function(experiment, result) {
    131       var r = this.currentExperimentRow_;
    132 
    133       var endTime = timeutil.getCurrentTime();
    134 
    135       r.dtCell.innerHTML = '';
    136       addTextNode(r.dtCell, (endTime - r.startTime));
    137 
    138       r.resultCell.innerHTML = '';
    139 
    140       if (result == 0) {
    141         r.passFailCell.style.color = 'green';
    142         addTextNode(r.passFailCell, 'PASS');
    143       } else {
    144         addTextNode(r.resultCell,
    145                     netErrorToString(result) + ' (' + result + ')');
    146         r.passFailCell.style.color = 'red';
    147         addTextNode(r.passFailCell, 'FAIL');
    148       }
    149 
    150       this.currentExperimentRow_ = null;
    151     },
    152 
    153     /**
    154      * Callback for when the last test in the suite has finished.
    155      */
    156     onCompletedConnectionTestSuite: function() {
    157       var p = addNode(this.summaryDiv_, 'p');
    158       addTextNode(p, 'Completed connection test suite suite');
    159     }
    160   };
    161 
    162   return TestView;
    163 })();
    164