Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: chrome/browser/ui/webui/net_internals_ui_browsertest.cc

Issue 7553009: Add some browser tests for net-internals (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Response to Sheridan's comments Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "base/command_line.h"
6 #include "base/file_path.h"
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/webui/web_ui_browsertest.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "googleurl/src/gurl.h"
14 #include "net/base/net_errors.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace {
18
19 const string16 kPassTitle = ASCIIToUTF16("Test Passed");
20 const string16 kFailTitle = ASCIIToUTF16("Test Failed");
21
22 // Each test involves calls a single Javascript function and then waits for the
23 // title to be changed to "Test Passed" or "Test Failed" when done.
24 class NetInternalsTest : public WebUIBrowserTest {
25 public:
26 NetInternalsTest();
27 virtual ~NetInternalsTest();
28
29 // InProcessBrowserTest overrides.
30 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
31 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
32 virtual void SetUpOnMainThread() OVERRIDE;
33
34 // Runs the specified Javascript test function with the specified arguments
35 // and waits for the title to change to "Test Passed" or "Test Failed".
36 void RunTestAndWaitForTitle(const std::string& function_name,
37 const ListValue& function_arguments);
38
39 // Same as above, with constant number of arguments for easy use. Will also
40 // free arguments.
41 void RunTestAndWaitForTitle(const std::string& function_name);
42 void RunTestAndWaitForTitle(const std::string& function_name, Value* arg);
43 void RunTestAndWaitForTitle(const std::string& function_name,
44 Value* arg1, Value* arg2);
45 void RunTestAndWaitForTitle(const std::string& function_name,
46 Value* arg1, Value* arg2, Value* arg3);
47
48 void set_expected_title_(const string16& value) { expected_title_ = value; }
49
50 private:
51 // The expected title at the end of the test. Defaults to kPassTitle.
52 string16 expected_title_;
53
54 DISALLOW_COPY_AND_ASSIGN(NetInternalsTest);
55 };
56
57 NetInternalsTest::NetInternalsTest() : expected_title_(kPassTitle) {
58 }
59
60 NetInternalsTest::~NetInternalsTest() {
61 }
62
63 void NetInternalsTest::SetUpCommandLine(CommandLine* command_line) {
64 WebUIBrowserTest::SetUpCommandLine(command_line);
65 // Needed to test the prerender view.
66 command_line->AppendSwitchASCII(switches::kPrerender,
67 switches::kPrerenderSwitchValueEnabled);
68 }
69
70 void NetInternalsTest::SetUpInProcessBrowserTestFixture() {
71 // Adds libraries needed for testing, so much be first.
72 WebUIBrowserTest::SetUpInProcessBrowserTestFixture();
73
74 // Framework for net-internals tests.
75 AddLibrary(FilePath(FILE_PATH_LITERAL(
76 "net_internals/net_internals_test.js")));
77
78 // Add Javascript files needed for individual tests.
79 AddLibrary(FilePath(FILE_PATH_LITERAL("net_internals/log_view_painter.js")));
80 AddLibrary(FilePath(FILE_PATH_LITERAL("net_internals/prerender_view.js")));
81 AddLibrary(FilePath(FILE_PATH_LITERAL("net_internals/test_view.js")));
82 }
83
84 void NetInternalsTest::SetUpOnMainThread() {
85 // Navigate to chrome://net-internals.
86 ui_test_utils::NavigateToURL(browser(),
87 GURL(chrome::kChromeUINetInternalsURL));
88 }
89
90 void NetInternalsTest::RunTestAndWaitForTitle(
91 const std::string& function_name,
92 const ListValue& function_arguments) {
93 ui_test_utils::TitleWatcher title_watcher(browser()->GetTabContentsAt(0),
94 kPassTitle);
95 title_watcher.AlsoWaitForTitle(kFailTitle);
96
97 ConstValueVector arguments;
98 StringValue function_name_arg(function_name);
99 arguments.push_back(&function_name_arg);
100 arguments.push_back(&function_arguments);
101
102 ASSERT_TRUE(RunJavascriptFunction("netInternalsTest.runTest", arguments));
103 ASSERT_EQ(expected_title_, title_watcher.WaitAndGetTitle());
104 }
105
106 void NetInternalsTest::RunTestAndWaitForTitle(
107 const std::string& function_name) {
108 ListValue test_arguments;
109 RunTestAndWaitForTitle(function_name, test_arguments);
110 }
111
112 void NetInternalsTest::RunTestAndWaitForTitle(const std::string& function_name,
113 Value* arg) {
114 ListValue test_arguments;
115 test_arguments.Append(arg);
116 RunTestAndWaitForTitle(function_name, test_arguments);
117 }
118
119 void NetInternalsTest::RunTestAndWaitForTitle(const std::string& function_name,
120 Value* arg1, Value* arg2) {
121 ListValue test_arguments;
122 test_arguments.Append(arg1);
123 test_arguments.Append(arg2);
124 RunTestAndWaitForTitle(function_name, test_arguments);
125 }
126
127 void NetInternalsTest::RunTestAndWaitForTitle(const std::string& function_name,
128 Value* arg1, Value* arg2,
129 Value* arg3) {
130 ListValue test_arguments;
131 test_arguments.Append(arg1);
132 test_arguments.Append(arg2);
133 test_arguments.Append(arg3);
134 RunTestAndWaitForTitle(function_name, test_arguments);
135 }
136
137 ////////////////////////////////////////////////////////////////////////////////
138 // framework.js
139 ////////////////////////////////////////////////////////////////////////////////
140
141 // Checks testDone.
142 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDone) {
143 RunTestAndWaitForTitle("NetInternalsDone");
144 }
145
146 // Checks a failed expect statement.
147 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsExpectFail) {
148 set_expected_title_(kFailTitle);
149 RunTestAndWaitForTitle("NetInternalsExpectFail");
150 }
151
152 // Checks a failed assert statement.
153 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsAssertFail) {
154 set_expected_title_(kFailTitle);
155 RunTestAndWaitForTitle("NetInternalsAssertFail");
156 }
157
158 // Checks that testDone works when called by an observer in response to an
159 // event.
160 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsObserverDone) {
161 RunTestAndWaitForTitle("NetInternalsObserverDone");
162 }
163
164 // Checks that a failed expect works when called by an observer in response
165 // to an event.
166 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsObserverExpectFail) {
167 set_expected_title_(kFailTitle);
168 RunTestAndWaitForTitle("NetInternalsObserverExpectFail");
169 }
170
171 // Checks that a failed assertion works when called by an observer in response
172 // to an event.
173 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsObserverAssertFail) {
174 set_expected_title_(kFailTitle);
175 RunTestAndWaitForTitle("NetInternalsObserverAssertFail");
176 }
177
178 ////////////////////////////////////////////////////////////////////////////////
179 // test_view.js
180 ////////////////////////////////////////////////////////////////////////////////
181
182 // Runs the test suite twice, expecting a passing result the first time. Checks
183 // the first result, the order of events that occur, and the number of rows in
184 // the table.
185 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsTestViewPassTwice) {
186 ASSERT_TRUE(test_server()->Start());
187 RunTestAndWaitForTitle(
188 "NetInternalsTestView",
189 // URL that results in success.
190 Value::CreateStringValue(
191 test_server()->GetURL("files/title1.html").spec()),
192 // Resulting error code of the first test.
193 Value::CreateIntegerValue(net::OK),
194 // Number of times to run the test suite.
195 Value::CreateIntegerValue(2));
196 }
197
198 // Runs the test suite twice, expecting a failing result the first time. Checks
199 // the first result, the order of events that occur, and the number of rows in
200 // the table.
201 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsTestViewFailTwice) {
202 RunTestAndWaitForTitle(
203 "NetInternalsTestView",
204 // URL that results in an error, due to the port.
205 Value::CreateStringValue("http://127.0.0.1:7/"),
206 // Resulting error code of the first test.
207 Value::CreateIntegerValue(net::ERR_UNSAFE_PORT),
208 // Number of times to run the test suite.
209 Value::CreateIntegerValue(2));
210 }
211
212 ////////////////////////////////////////////////////////////////////////////////
213 // prerender_view.js
214 ////////////////////////////////////////////////////////////////////////////////
215
216 // Prerender two pages and check PrerenderView behavior. The first is expected
217 // to fail, the second is expected to succeed.
218 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsPrerenderView) {
219 ASSERT_TRUE(test_server()->Start());
220 RunTestAndWaitForTitle(
221 "NetInternalsPrerenderView",
222 // URL that can't be prerendered, since it triggers a download.
223 Value::CreateStringValue(
224 test_server()->GetURL("files/download-test1.lib").spec()),
225 // URL that can be prerendered.
226 Value::CreateStringValue(
227 test_server()->GetURL("files/title1.html").spec()));
228 }
229
230 ////////////////////////////////////////////////////////////////////////////////
231 // log_view_painter.js
232 ////////////////////////////////////////////////////////////////////////////////
233
234 // Check that we correctly remove cookies and login information.
235 IN_PROC_BROWSER_TEST_F(NetInternalsTest,
236 NetInternalsLogViewPainterStripInfo) {
237 RunTestAndWaitForTitle("NetInternalsLogViewPainterStripInfo");
238 }
239
240 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698