8eeada6f0f1406bc6fa9d41c8be0665ea53191f8
[platform/framework/web/crosswalk.git] / src / chrome / test / data / webui / net_internals / test_view.js
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 // Include test fixture.
6 GEN_INCLUDE(['net_internals_test.js']);
7
8 // Anonymous namespace
9 (function() {
10
11 /**
12  * Navigates to the test tab, runs the test suite once, using a URL
13  * either passed to the constructor or passed in from the previous task, and
14  * expects to see |expectedResult| from the first experiment.  If
15  * |expectedResult| indicates failure, all experiments are expected to fail.
16  * Otherwise, results of all experiments but the first are ignored.
17  * Checks the order of events and several fields of the events received.
18  *
19  * Note that it is not safe for one RunTestSuiteTasks to run right after
20  * another.  Otherwise, the second Task would receive the final observer
21  * message from the first task.
22  *
23  * @param {int} expectedResult Expected result of first test.
24  * @param {string} url URL to test.  If null, will use parameter passed to
25  *     start instead.
26  * @extends {NetInternalsTest.Task}
27  * @constructor
28  */
29 function RunTestSuiteTask(expectedResult, url) {
30   NetInternalsTest.Task.call(this);
31   // Complete asynchronously, so two RunTestSuiteTask can be run in a row.
32   this.setCompleteAsync(true);
33
34   this.url_ = url;
35   this.expectedResult_ = expectedResult;
36   this.seenStartSuite_ = false;
37   this.seenStartExperiment_ = false;
38   this.experimentsRun_ = 0;
39 }
40
41 RunTestSuiteTask.prototype = {
42   __proto__: NetInternalsTest.Task.prototype,
43
44   /**
45    * Switches to the test view, and simulates entering |url| and submitting the
46    * form.  Also adds the task as a ConnectionTestObserver.
47    * @param {string} url URL to run the test suite on.
48    */
49   start: function(url) {
50     NetInternalsTest.switchToView('tests');
51     if (this.url_ === null) {
52       assertEquals('string', typeof(url));
53       this.url_ = url;
54     }
55
56     g_browser.addConnectionTestsObserver(this);
57
58     $(TestView.URL_INPUT_ID).value = this.url_;
59     $(TestView.SUBMIT_BUTTON_ID).click();
60   },
61
62   /**
63    * Checks that the table was created/cleared, and that no experiment is
64    * currently running.
65    */
66   onStartedConnectionTestSuite: function() {
67     if (this.isDone())
68       return;
69     expectFalse(this.seenStartSuite_, 'Suite started more than once.');
70     checkTestTableRows(0);
71     expectEquals(this.experimentsRun_, 0);
72     expectFalse(this.seenStartSuite_);
73
74     this.seenStartSuite_ = true;
75   },
76
77   /**
78    * Checks that the table has one row per started experiment, and the events
79    * occur in the proper order.
80    * @param {object} experiment Experiment that was just started.
81    */
82   onStartedConnectionTestExperiment: function(experiment) {
83     if (this.isDone())
84       return;
85     console.log('Experiment: ' + this.experimentsRun_);
86     expectEquals(this.url_, experiment.url, 'Test run on wrong URL');
87     expectTrue(this.seenStartSuite_, 'Experiment started before suite.');
88     expectFalse(this.seenStartExperiment_,
89                 'Two experiments running at once.');
90     checkTestTableRows(this.experimentsRun_ + 1);
91
92     this.seenStartExperiment_ = true;
93   },
94
95   /**
96    * Checks that the table has one row per started experiment, and the events
97    * occur in the proper order.
98    * @param {object} experiment Experiment that finished.
99    * @param {number} result Code indicating success or reason for failure.
100    */
101   onCompletedConnectionTestExperiment: function(experiment, result) {
102     if (this.isDone())
103       return;
104     expectEquals(this.url_, experiment.url, 'Test run on wrong URL');
105     // Can only rely on the error code of the first test.
106     if (this.experimentsRun_ == 0)
107       expectEquals(this.expectedResult_, result);
108     // If the first expected result is an error, all tests should return some
109     // error.
110     if (this.expectedResult_ < 0)
111       expectLT(result, 0);
112
113     expectTrue(this.seenStartExperiment_,
114                'Experiment stopped without starting.');
115     checkTestTableRows(this.experimentsRun_ + 1);
116
117     this.seenStartExperiment_ = false;
118     ++this.experimentsRun_;
119   },
120
121   /**
122    * Checks that we've received all 12 sets of results, and either runs the
123    * next test iteration, or ends the test, depending on the total number of
124    * iterations.
125    */
126   onCompletedConnectionTestSuite: function() {
127     if (this.isDone())
128       return;
129     expectTrue(this.seenStartSuite_, 'Suite stopped without being started.');
130     expectFalse(this.seenStartExperiment_,
131                 'Suite stopped while experiment was still running.');
132     expectEquals(12, this.experimentsRun_,
133                  'Incorrect number of experiments run.');
134     checkTestTableRows(this.experimentsRun_);
135
136     this.onTaskDone();
137   }
138 };
139
140 /**
141  * Checks that there are |expected| rows in the test table.
142  * @param {number} expectedRows Expected number of rows in the table.
143  */
144 function checkTestTableRows(expectedRows) {
145   NetInternalsTest.checkTbodyRows(TestView.SUMMARY_DIV_ID, expectedRows);
146 }
147
148 /**
149  * Runs the test suite twice, expecting a passing result the first time.  Checks
150  * the first result, the order of events that occur, and the number of rows in
151  * the table.  Uses the TestServer.
152  */
153 TEST_F('NetInternalsTest', 'netInternalsTestViewPassTwice', function() {
154   var taskQueue = new NetInternalsTest.TaskQueue(true);
155   taskQueue.addTask(
156       new NetInternalsTest.GetTestServerURLTask('files/title1.html'));
157   // 0 indicates success, the null means we'll use the URL resulting from the
158   // previous task.
159   taskQueue.addTask(new RunTestSuiteTask(0, null));
160
161   taskQueue.addTask(
162       new NetInternalsTest.GetTestServerURLTask('files/title2.html'));
163   // 0 indicates success, the null means we'll use the URL resulting from the
164   // previous task.
165   taskQueue.addTask(new RunTestSuiteTask(0, null));
166   taskQueue.run();
167 });
168
169 /**
170  * Runs the test suite twice.  Checks the exact error code of the first result,
171  * the order of events that occur, and the number of rows in the HTML table.
172  * Does not use the TestServer.
173  */
174 TEST_F('NetInternalsTest', 'netInternalsTestViewFailTwice', function() {
175   var taskQueue = new NetInternalsTest.TaskQueue(true);
176   taskQueue.addTask(new RunTestSuiteTask(NetError.UNSAFE_PORT,
177                                          'http://127.0.0.1:7/'));
178   taskQueue.addTask(new RunTestSuiteTask(NetError.UNSAFE_PORT,
179                                          'http://127.0.0.1:7/'));
180   taskQueue.run();
181 });
182
183 })();  // Anonymous namespace