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

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: Fix test_view.js 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 // Each test involves calls a single Javascript function and then waits for the
20 // title to be changed to "Test Passed" or "Test Failed" when done.
21 class NetInternalsTest : public WebUIBrowserTest {
22 public:
23 NetInternalsTest();
24 virtual ~NetInternalsTest();
25
26 // InProcessBrowserTest overrides.
27 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
28 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
29 virtual void SetUpOnMainThread() OVERRIDE;
30
31
32 // Runs the specified Javascript test function with the specified arguments
33 // and waits for the title to change to "Test Passed" or "Test Failed".
34 void RunTestAndWaitForTitle(const std::string& function_name,
35 const ListValue& function_arguments);
36
37 // Same as above, with constant number of arguments for easy use. Will also
38 // free arguments.
39 void RunTestAndWaitForTitle(const std::string& function_name);
40 void RunTestAndWaitForTitle(const std::string& function_name, Value* arg);
41 void RunTestAndWaitForTitle(const std::string& function_name,
42 Value* arg1, Value* arg2);
43 void RunTestAndWaitForTitle(const std::string& function_name,
44 Value* arg1, Value* arg2, Value* arg3);
45
46 void set_expect_success_(bool value) { expect_success_ = value; }
47
48 private:
49 // True if the test is expected to pass. Default value is true. Set to false
50 // on some tests of the test infrastructure.
51 bool expect_success_;
52 };
53
54 NetInternalsTest::NetInternalsTest() : expect_success_(true) {
55 }
56
57 NetInternalsTest::~NetInternalsTest() {
58 }
59
60 void NetInternalsTest::SetUpCommandLine(CommandLine* command_line) {
61 WebUIBrowserTest::SetUpCommandLine(command_line);
62 // Needed to test the prerender view.
63 command_line->AppendSwitchASCII(switches::kPrerender,
64 switches::kPrerenderSwitchValueEnabled);
65 }
66
67 void NetInternalsTest::SetUpInProcessBrowserTestFixture() {
68 WebUIBrowserTest::SetUpInProcessBrowserTestFixture();
69
70 // Add Javascript files needed for the tests. Must be added after
71 // WebUIBrowserTest adds its files.
72 AddLibrary(FilePath(FILE_PATH_LITERAL("net_internals/framework.js")));
73
74 AddLibrary(FilePath(FILE_PATH_LITERAL("net_internals/log_view_painter.js")));
75 AddLibrary(FilePath(FILE_PATH_LITERAL("net_internals/prerender_view.js")));
76 AddLibrary(FilePath(FILE_PATH_LITERAL("net_internals/test_view.js")));
77 }
78
79 void NetInternalsTest::SetUpOnMainThread() {
80 // Navigate to the net-internals page.
81 ui_test_utils::NavigateToURL(browser(),
82 GURL(chrome::kChromeUINetInternalsURL));
83 }
84
85 void NetInternalsTest::RunTestAndWaitForTitle(
86 const std::string& function_name,
87 const ListValue& function_arguments) {
88 ui_test_utils::TitleWatcher title_watcher(browser()->GetTabContentsAt(0),
89 ASCIIToUTF16("Test Passed"),
90 ASCIIToUTF16("Test Failed"));
91
92 ConstValueVector arguments;
93 StringValue function_name_arg(function_name);
94 arguments.push_back(&function_name_arg);
95 arguments.push_back(&function_arguments);
96
97 ASSERT_TRUE(RunJavascriptFunction("runNetInternalsTest", arguments));
98 ASSERT_EQ(expect_success_, title_watcher.Wait());
99 }
100
101 void NetInternalsTest::RunTestAndWaitForTitle(
102 const std::string& function_name) {
103 ListValue test_arguments;
104 RunTestAndWaitForTitle(function_name, test_arguments);
105 }
106
107 void NetInternalsTest::RunTestAndWaitForTitle(const std::string& function_name,
108 Value* arg) {
109 ListValue test_arguments;
110 test_arguments.Append(arg);
111 RunTestAndWaitForTitle(function_name, test_arguments);
112 }
113
114 void NetInternalsTest::RunTestAndWaitForTitle(const std::string& function_name,
115 Value* arg1, Value* arg2) {
116 ListValue test_arguments;
117 test_arguments.Append(arg1);
118 test_arguments.Append(arg2);
119 RunTestAndWaitForTitle(function_name, test_arguments);
120 }
121
122 void NetInternalsTest::RunTestAndWaitForTitle(const std::string& function_name,
123 Value* arg1, Value* arg2,
124 Value* arg3) {
125 ListValue test_arguments;
126 test_arguments.Append(arg1);
127 test_arguments.Append(arg2);
128 test_arguments.Append(arg3);
129 RunTestAndWaitForTitle(function_name, test_arguments);
130 }
131
132 ////////////////////////////////////////////////////////////////////////////////
133 // framework.js
134 ////////////////////////////////////////////////////////////////////////////////
135
136 // Checks testDone.
137 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDone) {
138 RunTestAndWaitForTitle("NetInternalsDone");
139 }
140
141 // Checks a failed expect statement.
142 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsExpectFail) {
143 set_expect_success_(false);
144 RunTestAndWaitForTitle("NetInternalsExpectFail");
145 }
146
147 // Checks a failed assert statement.
148 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsAssertFail) {
149 set_expect_success_(false);
150 RunTestAndWaitForTitle("NetInternalsAssertFail");
151 }
152
153 // Checks that testDone works when called by an observer in response to an
154 // event.
155 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsObserverDone) {
156 RunTestAndWaitForTitle("NetInternalsObserverDone");
157 }
158
159 // Checks that a failed expect works when called by an observer in response
160 // to an event.
161 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsObserverExpectFail) {
162 set_expect_success_(false);
163 RunTestAndWaitForTitle("NetInternalsObserverExpectFail");
164 }
165
166 // Checks that a failed assertion works when called by an observer in response
167 // to an event.
168 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsObserverAssertFail) {
169 set_expect_success_(false);
170 RunTestAndWaitForTitle("NetInternalsObserverAssertFail");
171 }
172
173 ////////////////////////////////////////////////////////////////////////////////
174 // test_view.js
175 ////////////////////////////////////////////////////////////////////////////////
176
177 // Runs the test suite twice, expecting a passing result each time. Checks
178 // the result, the order of events that occur, and the number of rows in the
179 // table.
180 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsTestViewPassTwice) {
181 ASSERT_TRUE(test_server()->Start());
182 RunTestAndWaitForTitle(
183 "NetInternalsTestView",
184 // URL that results in success.
185 Value::CreateStringValue(
186 test_server()->GetURL("files/title1.html").spec()),
187 // Resulting error code.
188 Value::CreateIntegerValue(net::OK),
189 // Number of times to run the test.
190 Value::CreateIntegerValue(2));
191 }
192
193 // Runs the test suite twice, expecting a failing result each time. Checks
194 // the result, the order of events that occur, and the number of rows in the
195 // table.
196 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsTestViewFailTwice) {
197 RunTestAndWaitForTitle(
198 "NetInternalsTestView",
199 // URL that results in an error, due to the port.
200 Value::CreateStringValue("http://127.0.0.1:7/"),
201 // Resulting error code.
202 Value::CreateIntegerValue(net::ERR_UNSAFE_PORT),
203 // Number of times to run the test.
204 Value::CreateIntegerValue(2));
205 }
206
207 ////////////////////////////////////////////////////////////////////////////////
208 // prerender_view.js
209 ////////////////////////////////////////////////////////////////////////////////
210
211 // Prerender two pages and check PrerenderView behavior. The first is expected
212 // to fail, the second is expected to succeed.
213 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsPrerenderView) {
214 ASSERT_TRUE(test_server()->Start());
215 RunTestAndWaitForTitle(
216 "NetInternalsPrerenderView",
217 // URL that can't be prerendered, since it triggers a download.
218 Value::CreateStringValue(
219 test_server()->GetURL("files/download-test1.lib").spec()),
220 // URL that can be prerendered.
221 Value::CreateStringValue(
222 test_server()->GetURL("files/title1.html").spec()));
223 }
224
225 ////////////////////////////////////////////////////////////////////////////////
226 // log_view_painter.js
227 ////////////////////////////////////////////////////////////////////////////////
228
229 // Check that we correctly remove cookies and login information.
230 IN_PROC_BROWSER_TEST_F(NetInternalsTest,
231 NetInternalsLogViewPainterStripInfo) {
232 RunTestAndWaitForTitle("NetInternalsLogViewPainterStripInfo");
233 }
234
235 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698