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

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

Issue 8775067: Clear DNS cache and passive log collected on incognito close. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years 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
OLDNEW
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/bind.h" 5 #include "base/bind.h"
6 #include "base/bind_helpers.h" 6 #include "base/bind_helpers.h"
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "base/values.h" 10 #include "base/values.h"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 ASSERT_TRUE(ip_literal.empty()); 66 ASSERT_TRUE(ip_literal.empty());
67 } 67 }
68 68
69 // Add entry to the cache. 69 // Add entry to the cache.
70 cache->Set(net::HostCache::Key(hostname, net::ADDRESS_FAMILY_UNSPECIFIED, 0), 70 cache->Set(net::HostCache::Key(hostname, net::ADDRESS_FAMILY_UNSPECIFIED, 0),
71 net_error, 71 net_error,
72 address_list, 72 address_list,
73 expires); 73 expires);
74 } 74 }
75 75
76 // Class to handle messages from the renderer needed by certain tests.
77 class NetInternalsTestMessageHandler : public WebUIMessageHandler {
78 public:
79 NetInternalsTestMessageHandler();
80
81 void set_browser(Browser* browser) {
82 ASSERT_TRUE(browser);
83 browser_ = browser;
84 }
85
86 private:
87 virtual void RegisterMessages() OVERRIDE;
88
89 // Opens the given URL in a new background tab.
90 void OpenNewTab(const ListValue* list_value);
91
92 // Adds a new entry to the host cache. Takes in hostname, ip address,
93 // net error code, and expiration time (as number of days from now).
94 void AddCacheEntry(const ListValue* list_value);
95
96 // Navigates to the prerender in the background tab. This assumes that
97 // there is a "Click()" function in the background tab which will navigate
98 // there, and that the background tab exists at slot 1.
99 void NavigateToPrerender(const ListValue* list_value);
100
101 Browser* browser_;
102
103 DISALLOW_COPY_AND_ASSIGN(NetInternalsTestMessageHandler);
104 };
105
106 NetInternalsTestMessageHandler::NetInternalsTestMessageHandler()
107 : browser_(NULL) {
108 }
109
110 void NetInternalsTestMessageHandler::RegisterMessages() {
111 web_ui_->RegisterMessageCallback(
112 "openNewTab",
113 base::Bind(&NetInternalsTestMessageHandler::OpenNewTab,
114 base::Unretained(this)));
115 web_ui_->RegisterMessageCallback(
116 "addCacheEntry",
117 base::Bind(&NetInternalsTestMessageHandler::AddCacheEntry,
118 base::Unretained(this)));
119 web_ui_->RegisterMessageCallback("navigateToPrerender",
120 base::Bind(&NetInternalsTestMessageHandler::NavigateToPrerender,
121 base::Unretained(this)));
122 }
123
124 void NetInternalsTestMessageHandler::OpenNewTab(const ListValue* list_value) {
125 std::string url;
126 ASSERT_TRUE(list_value->GetString(0, &url));
127 ASSERT_TRUE(browser_);
128 ui_test_utils::NavigateToURLWithDisposition(
129 browser_,
130 GURL(url),
131 NEW_BACKGROUND_TAB,
132 ui_test_utils::BROWSER_TEST_NONE);
133 }
134
135 // Called on UI thread. Adds an entry to the cache for the specified hostname
136 // by posting a task to the IO thread. Takes the host name, ip address, net
137 // error code, and expiration time in days from now as parameters. If the error
138 // code indicates failure, the ip address must be an empty string.
139 void NetInternalsTestMessageHandler::AddCacheEntry(
140 const ListValue* list_value) {
141 std::string hostname;
142 std::string ip_literal;
143 double net_error;
144 double expire_days_from_now;
145 ASSERT_TRUE(list_value->GetString(0, &hostname));
146 ASSERT_TRUE(list_value->GetString(1, &ip_literal));
147 ASSERT_TRUE(list_value->GetDouble(2, &net_error));
148 ASSERT_TRUE(list_value->GetDouble(3, &expire_days_from_now));
149 ASSERT_TRUE(browser_);
150
151 BrowserThread::PostTask(
152 BrowserThread::IO, FROM_HERE,
153 base::Bind(&AddCacheEntryOnIOThread,
154 make_scoped_refptr(browser_->profile()->GetRequestContext()),
155 hostname,
156 ip_literal,
157 static_cast<int>(net_error),
158 static_cast<int>(expire_days_from_now)));
159 }
160
161 void NetInternalsTestMessageHandler::NavigateToPrerender(
162 const ListValue* list_value) {
163 RenderViewHost* host = browser_->GetTabContentsAt(1)->render_view_host();
164 host->ExecuteJavascriptInWebFrame(string16(), ASCIIToUTF16("Click()"));
165 }
166
167 class NetInternalsTest : public WebUIBrowserTest { 76 class NetInternalsTest : public WebUIBrowserTest {
168 public: 77 public:
169 NetInternalsTest(); 78 NetInternalsTest();
170 virtual ~NetInternalsTest(); 79 virtual ~NetInternalsTest();
171 80
172 // InProcessBrowserTest overrides. 81 // InProcessBrowserTest overrides.
173 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE; 82 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
174 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; 83 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
175 virtual void SetUpOnMainThread() OVERRIDE; 84 virtual void SetUpOnMainThread() OVERRIDE;
176 85
177 protected: 86 protected:
178 GURL CreatePrerenderLoaderUrl(const GURL& prerender_url) { 87 GURL CreatePrerenderLoaderUrl(const GURL& prerender_url) {
179 std::vector<net::TestServer::StringPair> replacement_text; 88 std::vector<net::TestServer::StringPair> replacement_text;
180 replacement_text.push_back( 89 replacement_text.push_back(
181 make_pair("REPLACE_WITH_PRERENDER_URL", prerender_url.spec())); 90 make_pair("REPLACE_WITH_PRERENDER_URL", prerender_url.spec()));
182 std::string replacement_path; 91 std::string replacement_path;
183 EXPECT_TRUE(net::TestServer::GetFilePathWithReplacements( 92 EXPECT_TRUE(net::TestServer::GetFilePathWithReplacements(
184 "files/prerender/prerender_loader.html", 93 "files/prerender/prerender_loader.html",
185 replacement_text, 94 replacement_text,
186 &replacement_path)); 95 &replacement_path));
187 GURL url_loader = test_server()->GetURL(replacement_path); 96 GURL url_loader = test_server()->GetURL(replacement_path);
188 return url_loader; 97 return url_loader;
189 } 98 }
190 99
191 private: 100 private:
101 // Class to handle messages from the renderer needed by certain tests.
102 class MessageHandler : public WebUIMessageHandler {
103 public:
104 explicit MessageHandler(NetInternalsTest* net_internals_test);
105
106 private:
107 virtual void RegisterMessages() OVERRIDE;
108
109 // Opens the given URL in a new background tab.
110 void OpenNewTab(const ListValue* list_value);
111
112 // Called on UI thread. Adds an entry to the cache for the specified
113 // hostname by posting a task to the IO thread. Takes the host name,
114 // ip address, net error code, and expiration time in days from now
115 // as parameters. If the error code indicates failure, the ip address
116 // must be an empty string.
117 void AddCacheEntry(const ListValue* list_value);
118
119 // Navigates to the prerender in the background tab. This assumes that
120 // there is a "Click()" function in the background tab which will navigate
121 // there, and that the background tab exists at slot 1.
122 void NavigateToPrerender(const ListValue* list_value);
123
124 // Creates an incognito browser.
125 void CreateIncognitoBrowser(const ListValue* list_value);
126
127 // Closes an incognito browser created with CreateIncognitoBrowser.
128 void CloseIncognitoBrowser(const ListValue* list_value);
129
130 Browser* browser() {
131 return net_internals_test_->browser();
132 }
133
134 NetInternalsTest* net_internals_test_;
135 Browser* incognito_browser_;
136
137 DISALLOW_COPY_AND_ASSIGN(MessageHandler);
138 };
139
192 virtual WebUIMessageHandler* GetMockMessageHandler() OVERRIDE { 140 virtual WebUIMessageHandler* GetMockMessageHandler() OVERRIDE {
193 message_handler_.set_browser(browser());
194 return &message_handler_; 141 return &message_handler_;
195 } 142 }
196 143
197 NetInternalsTestMessageHandler message_handler_; 144 MessageHandler message_handler_;
198 145
199 DISALLOW_COPY_AND_ASSIGN(NetInternalsTest); 146 DISALLOW_COPY_AND_ASSIGN(NetInternalsTest);
200 }; 147 };
201 148
202 NetInternalsTest::NetInternalsTest() { 149 NetInternalsTest::NetInternalsTest()
150 : ALLOW_THIS_IN_INITIALIZER_LIST(message_handler_(this)) {
203 } 151 }
204 152
205 NetInternalsTest::~NetInternalsTest() { 153 NetInternalsTest::~NetInternalsTest() {
206 } 154 }
207 155
208 void NetInternalsTest::SetUpCommandLine(CommandLine* command_line) { 156 void NetInternalsTest::SetUpCommandLine(CommandLine* command_line) {
209 WebUIBrowserTest::SetUpCommandLine(command_line); 157 WebUIBrowserTest::SetUpCommandLine(command_line);
210 // Needed to test the prerender view. 158 // Needed to test the prerender view.
211 command_line->AppendSwitchASCII(switches::kPrerenderMode, 159 command_line->AppendSwitchASCII(switches::kPrerenderMode,
212 switches::kPrerenderModeSwitchValueEnabled); 160 switches::kPrerenderModeSwitchValueEnabled);
(...skipping 24 matching lines...) Expand all
237 GURL(chrome::kChromeUINetInternalsURL)); 185 GURL(chrome::kChromeUINetInternalsURL));
238 // Increase the memory allowed in a prerendered page above normal settings, 186 // Increase the memory allowed in a prerendered page above normal settings,
239 // as debug builds use more memory and often go over the usual limit. 187 // as debug builds use more memory and often go over the usual limit.
240 Profile* profile = browser()->GetSelectedTabContentsWrapper()->profile(); 188 Profile* profile = browser()->GetSelectedTabContentsWrapper()->profile();
241 prerender::PrerenderManager* prerender_manager = 189 prerender::PrerenderManager* prerender_manager =
242 prerender::PrerenderManagerFactory::GetForProfile(profile); 190 prerender::PrerenderManagerFactory::GetForProfile(profile);
243 prerender_manager->mutable_config().max_bytes = 1000 * 1024 * 1024; 191 prerender_manager->mutable_config().max_bytes = 1000 * 1024 * 1024;
244 } 192 }
245 193
246 //////////////////////////////////////////////////////////////////////////////// 194 ////////////////////////////////////////////////////////////////////////////////
195 // NetInternalsTest::MessageHandler
196 ////////////////////////////////////////////////////////////////////////////////
197
198 NetInternalsTest::MessageHandler::MessageHandler(
199 NetInternalsTest* net_internals_test)
200 : net_internals_test_(net_internals_test),
201 incognito_browser_(NULL) {
202 }
203
204 void NetInternalsTest::MessageHandler::RegisterMessages() {
205 web_ui_->RegisterMessageCallback(
206 "openNewTab",
207 base::Bind(&NetInternalsTest::MessageHandler::OpenNewTab,
208 base::Unretained(this)));
209 web_ui_->RegisterMessageCallback(
210 "addCacheEntry",
211 base::Bind(&NetInternalsTest::MessageHandler::AddCacheEntry,
212 base::Unretained(this)));
213 web_ui_->RegisterMessageCallback("navigateToPrerender",
214 base::Bind(&NetInternalsTest::MessageHandler::NavigateToPrerender,
215 base::Unretained(this)));
216 web_ui_->RegisterMessageCallback("createIncognitoBrowser",
217 base::Bind(&NetInternalsTest::MessageHandler::CreateIncognitoBrowser,
218 base::Unretained(this)));
219 web_ui_->RegisterMessageCallback("closeIncognitoBrowser",
220 base::Bind(&NetInternalsTest::MessageHandler::CloseIncognitoBrowser,
221 base::Unretained(this)));
222 }
223
224 void NetInternalsTest::MessageHandler::OpenNewTab(
225 const ListValue* list_value) {
226 std::string url;
227 ASSERT_TRUE(list_value->GetString(0, &url));
228 ui_test_utils::NavigateToURLWithDisposition(
229 browser(),
230 GURL(url),
231 NEW_BACKGROUND_TAB,
232 ui_test_utils::BROWSER_TEST_NONE);
233 }
234
235 void NetInternalsTest::MessageHandler::AddCacheEntry(
236 const ListValue* list_value) {
237 std::string hostname;
238 std::string ip_literal;
239 double net_error;
240 double expire_days_from_now;
241 ASSERT_TRUE(list_value->GetString(0, &hostname));
242 ASSERT_TRUE(list_value->GetString(1, &ip_literal));
243 ASSERT_TRUE(list_value->GetDouble(2, &net_error));
244 ASSERT_TRUE(list_value->GetDouble(3, &expire_days_from_now));
245 ASSERT_TRUE(browser());
246
247 BrowserThread::PostTask(
248 BrowserThread::IO, FROM_HERE,
249 base::Bind(&AddCacheEntryOnIOThread,
250 make_scoped_refptr(browser()->profile()->GetRequestContext()),
251 hostname,
252 ip_literal,
253 static_cast<int>(net_error),
254 static_cast<int>(expire_days_from_now)));
255 }
256
257 void NetInternalsTest::MessageHandler::NavigateToPrerender(
258 const ListValue* list_value) {
259 RenderViewHost* host = browser()->GetTabContentsAt(1)->render_view_host();
260 host->ExecuteJavascriptInWebFrame(string16(), ASCIIToUTF16("Click()"));
261 }
262
263 void NetInternalsTest::MessageHandler::CreateIncognitoBrowser(
264 const ListValue* list_value) {
265 ASSERT_FALSE(incognito_browser_);
266 incognito_browser_ = net_internals_test_->CreateIncognitoBrowser();
267 }
268
269 void NetInternalsTest::MessageHandler::CloseIncognitoBrowser(
270 const ListValue* list_value) {
271 ASSERT_TRUE(incognito_browser_);
272 incognito_browser_->CloseAllTabs();
273 incognito_browser_ = NULL;
eroman 2011/12/05 21:10:49 What code is responsible for freeing the Browser*
mmenke 2011/12/05 22:28:00 Browser objects are automatically destroyed once t
274 }
275
276 ////////////////////////////////////////////////////////////////////////////////
247 // net_internals_ui.js 277 // net_internals_ui.js
248 //////////////////////////////////////////////////////////////////////////////// 278 ////////////////////////////////////////////////////////////////////////////////
249 279
250 // Checks testDone. 280 // Checks testDone.
251 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDone) { 281 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDone) {
252 EXPECT_TRUE(RunJavascriptAsyncTest("netInternalsDone")); 282 EXPECT_TRUE(RunJavascriptAsyncTest("netInternalsDone"));
253 } 283 }
254 284
255 // Checks a failed expect statement. 285 // Checks a failed expect statement.
256 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsExpectFail) { 286 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsExpectFail) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 // Adds an expired successful lookup to the DNS cache, then clears the cache. 391 // Adds an expired successful lookup to the DNS cache, then clears the cache.
362 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDnsViewExpired) { 392 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDnsViewExpired) {
363 EXPECT_TRUE(RunJavascriptAsyncTest("netInternalsDnsViewExpired")); 393 EXPECT_TRUE(RunJavascriptAsyncTest("netInternalsDnsViewExpired"));
364 } 394 }
365 395
366 // Adds two entries to the DNS cache, clears the cache, and then repeats. 396 // Adds two entries to the DNS cache, clears the cache, and then repeats.
367 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDnsViewAddTwoTwice) { 397 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDnsViewAddTwoTwice) {
368 EXPECT_TRUE(RunJavascriptAsyncTest("netInternalsDnsViewAddTwoTwice")); 398 EXPECT_TRUE(RunJavascriptAsyncTest("netInternalsDnsViewAddTwoTwice"));
369 } 399 }
370 400
401 // Makes sure that openning and then closing an incognito window clears the
402 // DNS cache. To keep things simple, we add a fake cache entry ourselves,
403 // rather than having the incognito browser create one.
404 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsDnsViewIncognitoClears) {
405 EXPECT_TRUE(RunJavascriptAsyncTest("netInternalsDnsViewIncognitoClears"));
406 }
407
371 //////////////////////////////////////////////////////////////////////////////// 408 ////////////////////////////////////////////////////////////////////////////////
372 // test_view.js 409 // test_view.js
373 //////////////////////////////////////////////////////////////////////////////// 410 ////////////////////////////////////////////////////////////////////////////////
374 411
375 // Runs the test suite twice, expecting a passing result the first time. Checks 412 // Runs the test suite twice, expecting a passing result the first time. Checks
376 // the first result, the order of events that occur, and the number of rows in 413 // the first result, the order of events that occur, and the number of rows in
377 // the table. 414 // the table.
378 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsTestViewPassTwice) { 415 IN_PROC_BROWSER_TEST_F(NetInternalsTest, NetInternalsTestViewPassTwice) {
379 ASSERT_TRUE(test_server()->Start()); 416 ASSERT_TRUE(test_server()->Start());
380 EXPECT_TRUE(RunJavascriptAsyncTest( 417 EXPECT_TRUE(RunJavascriptAsyncTest(
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 // log_view_painter.js 529 // log_view_painter.js
493 //////////////////////////////////////////////////////////////////////////////// 530 ////////////////////////////////////////////////////////////////////////////////
494 531
495 // Check that we correctly remove cookies and login information. 532 // Check that we correctly remove cookies and login information.
496 IN_PROC_BROWSER_TEST_F(NetInternalsTest, 533 IN_PROC_BROWSER_TEST_F(NetInternalsTest,
497 NetInternalsLogViewPainterStripInfo) { 534 NetInternalsLogViewPainterStripInfo) {
498 EXPECT_TRUE(RunJavascriptAsyncTest("netInternalsLogViewPainterStripInfo")); 535 EXPECT_TRUE(RunJavascriptAsyncTest("netInternalsLogViewPainterStripInfo"));
499 } 536 }
500 537
501 } // namespace 538 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698