OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/file_path.h" | 6 #include "base/file_path.h" |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/prerender/prerender_final_status.h" |
8 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/webui/web_ui_browsertest.h" | 10 #include "chrome/browser/ui/webui/web_ui_browsertest.h" |
10 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
11 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
12 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
14 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 const string16 kPassTitle = ASCIIToUTF16("Test Passed"); | 20 const string16 kPassTitle = ASCIIToUTF16("Test Passed"); |
20 const string16 kFailTitle = ASCIIToUTF16("Test Failed"); | 21 const string16 kFailTitle = ASCIIToUTF16("Test Failed"); |
21 | 22 |
| 23 // Class to handle messages from the renderer needed by certain tests. |
| 24 class NetInternalsTestMessageHandler : public WebUIMessageHandler { |
| 25 public: |
| 26 NetInternalsTestMessageHandler(); |
| 27 |
| 28 void set_browser(Browser* browser) { |
| 29 ASSERT_TRUE(browser); |
| 30 browser_ = browser; |
| 31 } |
| 32 |
| 33 private: |
| 34 virtual void RegisterMessages() OVERRIDE; |
| 35 |
| 36 // Opens the given URL in a new tab. |
| 37 void OpenNewTab(const ListValue* list_value); |
| 38 |
| 39 Browser* browser_; |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(NetInternalsTestMessageHandler); |
| 42 }; |
| 43 |
| 44 NetInternalsTestMessageHandler::NetInternalsTestMessageHandler() |
| 45 : browser_(NULL) { |
| 46 } |
| 47 |
| 48 void NetInternalsTestMessageHandler::RegisterMessages() { |
| 49 web_ui_->RegisterMessageCallback("openNewTab", NewCallback( |
| 50 this, &NetInternalsTestMessageHandler::OpenNewTab)); |
| 51 } |
| 52 |
| 53 void NetInternalsTestMessageHandler::OpenNewTab(const ListValue* list_value) { |
| 54 std::string url; |
| 55 ASSERT_TRUE(list_value->GetString(0, &url)); |
| 56 ASSERT_TRUE(browser_); |
| 57 ui_test_utils::NavigateToURLWithDisposition( |
| 58 browser_, |
| 59 GURL(url), |
| 60 NEW_BACKGROUND_TAB, |
| 61 ui_test_utils::BROWSER_TEST_NONE); |
| 62 } |
| 63 |
22 // Each test involves calls a single Javascript function and then waits for the | 64 // 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. | 65 // title to be changed to "Test Passed" or "Test Failed" when done. |
24 class NetInternalsTest : public WebUIBrowserTest { | 66 class NetInternalsTest : public WebUIBrowserTest { |
25 public: | 67 public: |
26 NetInternalsTest(); | 68 NetInternalsTest(); |
27 virtual ~NetInternalsTest(); | 69 virtual ~NetInternalsTest(); |
28 | 70 |
29 // InProcessBrowserTest overrides. | 71 // InProcessBrowserTest overrides. |
30 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE; | 72 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE; |
31 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; | 73 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; |
32 virtual void SetUpOnMainThread() OVERRIDE; | 74 virtual void SetUpOnMainThread() OVERRIDE; |
33 | 75 |
34 // Runs the specified Javascript test function with the specified arguments | 76 // Runs the specified Javascript test function with the specified arguments |
35 // and waits for the title to change to "Test Passed" or "Test Failed". | 77 // and waits for the title to change to "Test Passed" or "Test Failed". |
36 void RunTestAndWaitForTitle(const std::string& function_name, | 78 void RunTestAndWaitForTitle(const std::string& function_name, |
37 const ListValue& function_arguments); | 79 const ListValue& function_arguments); |
38 | 80 |
39 // Same as above, with constant number of arguments for easy use. Will also | 81 // Same as above, with constant number of arguments for easy use. Will also |
40 // free arguments. | 82 // free arguments. |
41 void RunTestAndWaitForTitle(const std::string& function_name); | 83 void RunTestAndWaitForTitle(const std::string& function_name); |
42 void RunTestAndWaitForTitle(const std::string& function_name, Value* arg); | 84 void RunTestAndWaitForTitle(const std::string& function_name, Value* arg); |
43 void RunTestAndWaitForTitle(const std::string& function_name, | 85 void RunTestAndWaitForTitle(const std::string& function_name, |
44 Value* arg1, Value* arg2); | 86 Value* arg1, Value* arg2); |
45 void RunTestAndWaitForTitle(const std::string& function_name, | 87 void RunTestAndWaitForTitle(const std::string& function_name, |
46 Value* arg1, Value* arg2, Value* arg3); | 88 Value* arg1, Value* arg2, Value* arg3); |
47 | 89 |
48 void set_expected_title_(const string16& value) { expected_title_ = value; } | 90 void set_expected_title_(const string16& value) { expected_title_ = value; } |
49 | 91 |
50 private: | 92 private: |
| 93 virtual WebUIMessageHandler* GetMockMessageHandler() OVERRIDE { |
| 94 message_handler_.set_browser(browser()); |
| 95 return &message_handler_; |
| 96 } |
| 97 |
| 98 NetInternalsTestMessageHandler message_handler_; |
| 99 |
51 // The expected title at the end of the test. Defaults to kPassTitle. | 100 // The expected title at the end of the test. Defaults to kPassTitle. |
52 string16 expected_title_; | 101 string16 expected_title_; |
53 | 102 |
54 DISALLOW_COPY_AND_ASSIGN(NetInternalsTest); | 103 DISALLOW_COPY_AND_ASSIGN(NetInternalsTest); |
55 }; | 104 }; |
56 | 105 |
57 NetInternalsTest::NetInternalsTest() : expected_title_(kPassTitle) { | 106 NetInternalsTest::NetInternalsTest() : expected_title_(kPassTitle) { |
58 } | 107 } |
59 | 108 |
60 NetInternalsTest::~NetInternalsTest() { | 109 NetInternalsTest::~NetInternalsTest() { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 Value* arg1, Value* arg2, | 177 Value* arg1, Value* arg2, |
129 Value* arg3) { | 178 Value* arg3) { |
130 ListValue test_arguments; | 179 ListValue test_arguments; |
131 test_arguments.Append(arg1); | 180 test_arguments.Append(arg1); |
132 test_arguments.Append(arg2); | 181 test_arguments.Append(arg2); |
133 test_arguments.Append(arg3); | 182 test_arguments.Append(arg3); |
134 RunTestAndWaitForTitle(function_name, test_arguments); | 183 RunTestAndWaitForTitle(function_name, test_arguments); |
135 } | 184 } |
136 | 185 |
137 //////////////////////////////////////////////////////////////////////////////// | 186 //////////////////////////////////////////////////////////////////////////////// |
138 // framework.js | 187 // net_internals_ui.js |
139 //////////////////////////////////////////////////////////////////////////////// | 188 //////////////////////////////////////////////////////////////////////////////// |
140 | 189 |
141 // Checks testDone. | 190 // Checks testDone. |
142 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDone) { | 191 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDone) { |
143 RunTestAndWaitForTitle("NetInternalsDone"); | 192 RunTestAndWaitForTitle("NetInternalsDone"); |
144 } | 193 } |
145 | 194 |
146 // Checks a failed expect statement. | 195 // Checks a failed expect statement. |
147 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsExpectFail) { | 196 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsExpectFail) { |
148 set_expected_title_(kFailTitle); | 197 set_expected_title_(kFailTitle); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 // Resulting error code of the first test. | 255 // Resulting error code of the first test. |
207 Value::CreateIntegerValue(net::ERR_UNSAFE_PORT), | 256 Value::CreateIntegerValue(net::ERR_UNSAFE_PORT), |
208 // Number of times to run the test suite. | 257 // Number of times to run the test suite. |
209 Value::CreateIntegerValue(2)); | 258 Value::CreateIntegerValue(2)); |
210 } | 259 } |
211 | 260 |
212 //////////////////////////////////////////////////////////////////////////////// | 261 //////////////////////////////////////////////////////////////////////////////// |
213 // prerender_view.js | 262 // prerender_view.js |
214 //////////////////////////////////////////////////////////////////////////////// | 263 //////////////////////////////////////////////////////////////////////////////// |
215 | 264 |
216 // Prerender two pages and check PrerenderView behavior. The first is expected | 265 // Prerender a page and navigate to it, once prerendering starts. |
217 // to fail, the second is expected to succeed. | 266 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsPrerenderViewSucceed) { |
218 // Test flaky on XP due to http://crbug.com/91799. | 267 ASSERT_TRUE(test_server()->Start()); |
219 IN_PROC_BROWSER_TEST_F(NetInternalsTest, FLAKY_NetInternalsPrerenderView) { | 268 RunTestAndWaitForTitle( |
| 269 "NetInternalsPrerenderView", |
| 270 // URL that can be prerendered. |
| 271 Value::CreateStringValue( |
| 272 test_server()->GetURL("files/title1.html").spec()), |
| 273 Value::CreateBooleanValue(true), |
| 274 Value::CreateStringValue( |
| 275 prerender::NameFromFinalStatus(prerender::FINAL_STATUS_USED))); |
| 276 } |
| 277 |
| 278 // Prerender a page that is expected to fail. |
| 279 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsPrerenderViewFail) { |
220 ASSERT_TRUE(test_server()->Start()); | 280 ASSERT_TRUE(test_server()->Start()); |
221 RunTestAndWaitForTitle( | 281 RunTestAndWaitForTitle( |
222 "NetInternalsPrerenderView", | 282 "NetInternalsPrerenderView", |
223 // URL that can't be prerendered, since it triggers a download. | 283 // URL that can't be prerendered, since it triggers a download. |
224 Value::CreateStringValue( | 284 Value::CreateStringValue( |
225 test_server()->GetURL("files/download-test1.lib").spec()), | 285 test_server()->GetURL("files/download-test1.lib").spec()), |
226 // URL that can be prerendered. | 286 Value::CreateBooleanValue(false), |
227 Value::CreateStringValue( | 287 Value::CreateStringValue( |
228 test_server()->GetURL("files/title1.html").spec())); | 288 prerender::NameFromFinalStatus(prerender::FINAL_STATUS_DOWNLOAD))); |
229 } | 289 } |
230 | 290 |
231 //////////////////////////////////////////////////////////////////////////////// | 291 //////////////////////////////////////////////////////////////////////////////// |
232 // log_view_painter.js | 292 // log_view_painter.js |
233 //////////////////////////////////////////////////////////////////////////////// | 293 //////////////////////////////////////////////////////////////////////////////// |
234 | 294 |
235 // Check that we correctly remove cookies and login information. | 295 // Check that we correctly remove cookies and login information. |
236 IN_PROC_BROWSER_TEST_F(NetInternalsTest, | 296 IN_PROC_BROWSER_TEST_F(NetInternalsTest, |
237 NetInternalsLogViewPainterStripInfo) { | 297 NetInternalsLogViewPainterStripInfo) { |
238 RunTestAndWaitForTitle("NetInternalsLogViewPainterStripInfo"); | 298 RunTestAndWaitForTitle("NetInternalsLogViewPainterStripInfo"); |
239 } | 299 } |
240 | 300 |
241 } // namespace | 301 } // namespace |
OLD | NEW |