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

Side by Side Diff: content/renderer/render_view_browsertest.cc

Issue 138513002: Plumb network stack information about existence of cached copy (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Incorporated Matt's latest comments. Created 6 years, 10 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/basictypes.h" 5 #include "base/basictypes.h"
6 6
7 #include "base/bind.h"
8 #include "base/callback.h"
7 #include "base/memory/shared_memory.h" 9 #include "base/memory/shared_memory.h"
8 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
10 #include "base/win/windows_version.h" 12 #include "base/win/windows_version.h"
11 #include "content/common/ssl_status_serialization.h" 13 #include "content/common/ssl_status_serialization.h"
12 #include "content/common/view_messages.h" 14 #include "content/common/view_messages.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/native_web_keyboard_event.h" 17 #include "content/public/browser/native_web_keyboard_event.h"
18 #include "content/public/browser/render_frame_host.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_ui_controller_factory.h" 21 #include "content/public/browser/web_ui_controller_factory.h"
15 #include "content/public/common/bindings_policy.h" 22 #include "content/public/common/bindings_policy.h"
23 #include "content/public/common/content_switches.h"
16 #include "content/public/common/page_zoom.h" 24 #include "content/public/common/page_zoom.h"
17 #include "content/public/common/url_constants.h" 25 #include "content/public/common/url_constants.h"
18 #include "content/public/common/url_utils.h" 26 #include "content/public/common/url_utils.h"
19 #include "content/public/renderer/document_state.h" 27 #include "content/public/renderer/document_state.h"
20 #include "content/public/renderer/history_item_serialization.h" 28 #include "content/public/renderer/history_item_serialization.h"
21 #include "content/public/renderer/navigation_state.h" 29 #include "content/public/renderer/navigation_state.h"
30 #include "content/public/test/browser_test_utils.h"
22 #include "content/public/test/render_view_test.h" 31 #include "content/public/test/render_view_test.h"
32 #include "content/public/test/test_utils.h"
23 #include "content/renderer/render_view_impl.h" 33 #include "content/renderer/render_view_impl.h"
34 #include "content/shell/browser/shell.h"
35 #include "content/shell/browser/shell_browser_context.h"
24 #include "content/shell/browser/shell_content_browser_client.h" 36 #include "content/shell/browser/shell_content_browser_client.h"
25 #include "content/shell/common/shell_content_client.h" 37 #include "content/shell/common/shell_content_client.h"
38 #include "content/shell/renderer/shell_content_renderer_client.h"
39 #include "content/test/content_browser_test.h"
40 #include "content/test/content_browser_test_utils.h"
26 #include "content/test/mock_keyboard.h" 41 #include "content/test/mock_keyboard.h"
27 #include "net/base/net_errors.h" 42 #include "net/base/net_errors.h"
28 #include "net/cert/cert_status_flags.h" 43 #include "net/cert/cert_status_flags.h"
44 #include "net/disk_cache/disk_cache.h"
45 #include "net/http/failing_http_transaction_factory.h"
46 #include "net/http/http_cache.h"
47 #include "net/url_request/url_request_context.h"
48 #include "net/url_request/url_request_context_getter.h"
29 #include "testing/gtest/include/gtest/gtest.h" 49 #include "testing/gtest/include/gtest/gtest.h"
30 #include "third_party/WebKit/public/platform/WebData.h" 50 #include "third_party/WebKit/public/platform/WebData.h"
31 #include "third_party/WebKit/public/platform/WebHTTPBody.h" 51 #include "third_party/WebKit/public/platform/WebHTTPBody.h"
32 #include "third_party/WebKit/public/platform/WebString.h" 52 #include "third_party/WebKit/public/platform/WebString.h"
33 #include "third_party/WebKit/public/platform/WebURLError.h" 53 #include "third_party/WebKit/public/platform/WebURLError.h"
34 #include "third_party/WebKit/public/platform/WebURLResponse.h" 54 #include "third_party/WebKit/public/platform/WebURLResponse.h"
35 #include "third_party/WebKit/public/web/WebDataSource.h" 55 #include "third_party/WebKit/public/web/WebDataSource.h"
36 #include "third_party/WebKit/public/web/WebFrame.h" 56 #include "third_party/WebKit/public/web/WebFrame.h"
37 #include "third_party/WebKit/public/web/WebHistoryItem.h" 57 #include "third_party/WebKit/public/web/WebHistoryItem.h"
38 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" 58 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 virtual bool UseWebUIForURL(BrowserContext* browser_context, 130 virtual bool UseWebUIForURL(BrowserContext* browser_context,
111 const GURL& url) const OVERRIDE { 131 const GURL& url) const OVERRIDE {
112 return HasWebUIScheme(url); 132 return HasWebUIScheme(url);
113 } 133 }
114 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context, 134 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context,
115 const GURL& url) const OVERRIDE { 135 const GURL& url) const OVERRIDE {
116 return HasWebUIScheme(url); 136 return HasWebUIScheme(url);
117 } 137 }
118 }; 138 };
119 139
120 } // namespace
121
122 class RenderViewImplTest : public RenderViewTest { 140 class RenderViewImplTest : public RenderViewTest {
123 public: 141 public:
124 RenderViewImplTest() { 142 RenderViewImplTest() {
125 // Attach a pseudo keyboard device to this object. 143 // Attach a pseudo keyboard device to this object.
126 mock_keyboard_.reset(new MockKeyboard()); 144 mock_keyboard_.reset(new MockKeyboard());
127 } 145 }
128 146
129 virtual ~RenderViewImplTest() {} 147 virtual ~RenderViewImplTest() {}
130 148
131 virtual void SetUp() OVERRIDE { 149 virtual void SetUp() OVERRIDE {
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 #else 315 #else
298 NOTIMPLEMENTED(); 316 NOTIMPLEMENTED();
299 return L'\0'; 317 return L'\0';
300 #endif 318 #endif
301 } 319 }
302 320
303 private: 321 private:
304 scoped_ptr<MockKeyboard> mock_keyboard_; 322 scoped_ptr<MockKeyboard> mock_keyboard_;
305 }; 323 };
306 324
325 class TestShellContentRendererClient : public ShellContentRendererClient {
326 public:
327 TestShellContentRendererClient()
328 : latest_error_valid_(false),
329 latest_error_reason_(0),
330 latest_error_stale_copy_in_cache_(false) {}
331
332 virtual void GetNavigationErrorStrings(
333 content::RenderView* render_view,
334 blink::WebFrame* frame,
335 const blink::WebURLRequest& failed_request,
336 const blink::WebURLError& error,
337 std::string* error_html,
338 base::string16* error_description) OVERRIDE {
339 if (error_html)
340 *error_html = "A suffusion of yellow.";
341 latest_error_valid_ = true;
342 latest_error_reason_ = error.reason;
343 latest_error_stale_copy_in_cache_ = error.staleCopyInCache;
344 }
345
346 bool GetLatestError(int* error_code, bool* stale_cache_entry_present) {
347 if (latest_error_valid_) {
348 *error_code = latest_error_reason_;
349 *stale_cache_entry_present = latest_error_stale_copy_in_cache_;
350 }
351 return latest_error_valid_;
352 }
353
354 private:
355 bool latest_error_valid_;
356 int latest_error_reason_;
357 bool latest_error_stale_copy_in_cache_;
358 };
359
360 // Must be called on IO thread.
361 void InterceptNetworkTransactions(net::URLRequestContextGetter* getter,
362 net::Error error) {
363 DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::IO));
364 net::HttpCache* cache(
365 getter->GetURLRequestContext()->http_transaction_factory()->GetCache());
366 DCHECK(cache);
367 scoped_ptr<net::FailingHttpTransactionFactory> factory(
368 new net::FailingHttpTransactionFactory(cache->GetSession(), error));
369 // Throw away old version; since this is a browser test, there is no
370 // need to restore the old state.
371 cache->SetHttpNetworkTransactionFactoryForTesting(
372 factory.PassAs<net::HttpTransactionFactory>());
373 }
374
375 void CallOnUIThreadValidatingReturn(const base::Closure& callback,
376 int rv) {
377 DCHECK_EQ(net::OK, rv);
378 BrowserThread::PostTask(
379 BrowserThread::UI, FROM_HERE, callback);
380 }
381
382 // Must be called on IO thread. The callback will be called on
383 // completion of cache clearing on the UI thread.
384 void BackendClearCache(scoped_ptr<disk_cache::Backend*> backend,
385 const base::Closure& callback,
386 int rv) {
387 DCHECK(*backend);
388 DCHECK_EQ(net::OK, rv);
389 (*backend)->DoomAllEntries(
390 base::Bind(&CallOnUIThreadValidatingReturn, callback));
391 }
392
393 // Must be called on IO thread. The callback will be called on
394 // completion of cache clearing on the UI thread.
395 void ClearCache(net::URLRequestContextGetter* getter,
396 const base::Closure& callback) {
397 DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::IO));
398 net::HttpCache* cache(
399 getter->GetURLRequestContext()->http_transaction_factory()->GetCache());
400 DCHECK(cache);
401 scoped_ptr<disk_cache::Backend*> backend(new disk_cache::Backend*);
402 *backend = NULL;
403 disk_cache::Backend** backend_ptr = backend.get();
404
405 net::CompletionCallback backend_callback(
406 base::Bind(&BackendClearCache, base::Passed(backend.Pass()), callback));
407
408 // backend_ptr is valid until all copies of backend_callback go out
409 // of scope.
410 if (net::OK == cache->GetBackend(backend_ptr, backend_callback)) {
411 // The call completed synchronously, so it is our job to call the callback.
mmenke 2014/01/29 22:18:24 nit: --our. :)
Randy Smith (Not in Mondays) 2014/01/29 22:50:45 Done.
412 backend_callback.Run(net::OK);
413 }
414 }
415
416 } // namespace
417
307 // Test that we get form state change notifications when input fields change. 418 // Test that we get form state change notifications when input fields change.
308 TEST_F(RenderViewImplTest, DISABLED_OnNavStateChanged) { 419 TEST_F(RenderViewImplTest, DISABLED_OnNavStateChanged) {
309 // Don't want any delay for form state sync changes. This will still post a 420 // Don't want any delay for form state sync changes. This will still post a
310 // message so updates will get coalesced, but as soon as we spin the message 421 // message so updates will get coalesced, but as soon as we spin the message
311 // loop, it will generate an update. 422 // loop, it will generate an update.
312 view()->set_send_content_state_immediately(true); 423 view()->set_send_content_state_immediately(true);
313 424
314 LoadHTML("<input type=\"text\" id=\"elt_text\"></input>"); 425 LoadHTML("<input type=\"text\" id=\"elt_text\"></input>");
315 426
316 // We should NOT have gotten a form state change notification yet. 427 // We should NOT have gotten a form state change notification yet.
(...skipping 1916 matching lines...) Expand 10 before | Expand all | Expand 10 after
2233 2344
2234 view()->webview()->clearFocusedNode(); 2345 view()->webview()->clearFocusedNode();
2235 const IPC::Message* msg3 = render_thread_->sink().GetFirstMessageMatching( 2346 const IPC::Message* msg3 = render_thread_->sink().GetFirstMessageMatching(
2236 ViewHostMsg_FocusedNodeChanged::ID); 2347 ViewHostMsg_FocusedNodeChanged::ID);
2237 EXPECT_TRUE(msg3); 2348 EXPECT_TRUE(msg3);
2238 ViewHostMsg_FocusedNodeChanged::Read(msg3, &params); 2349 ViewHostMsg_FocusedNodeChanged::Read(msg3, &params);
2239 EXPECT_FALSE(params.a); 2350 EXPECT_FALSE(params.a);
2240 render_thread_->sink().ClearMessages(); 2351 render_thread_->sink().ClearMessages();
2241 } 2352 }
2242 2353
2354 // For actual, browser side tests targetting code in RenderView
2355 // or RenderViewImpl.
2356 class RenderViewBrowserTest : public ContentBrowserTest {
2357 public:
2358 RenderViewBrowserTest() : renderer_client_(NULL) {}
2359
2360 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
2361 // This method is needed to allow interaction with in-process renderer
2362 // and use of a test ContentRendererClient.
2363 command_line->AppendSwitch(switches::kSingleProcess);
2364 }
2365
2366 virtual void SetUp() OVERRIDE {
2367 // Override setting of renderer client.
2368 renderer_client_ = new TestShellContentRendererClient();
2369 SetContentRendererClient(
2370 scoped_ptr<ContentRendererClient>(renderer_client_).Pass());
2371
2372 ContentBrowserTest::SetUp();
2373 }
2374
2375 // Navigates to the given URL and waits for |num_navigations| to occur, and
2376 // the title to change to |expected_title|.
2377 void NavigateToURLAndWaitForTitle(const GURL& url,
2378 const std::string& expected_title,
2379 int num_navigations) {
2380 content::TitleWatcher title_watcher(
2381 shell()->web_contents(), base::ASCIIToUTF16(expected_title));
2382
2383 content::NavigateToURLBlockUntilNavigationsComplete(
2384 shell(), url, num_navigations);
2385
2386 EXPECT_EQ(base::ASCIIToUTF16(expected_title),
2387 title_watcher.WaitAndGetTitle());
2388 }
2389
2390 // Returns true if there is a valid error stored; in this case
2391 // |*error_code| and |*stale_cache_entry_present| will be updated
2392 // appropriately.
2393 // Must be called after the renderer thread is created.
2394 bool GetLatestErrorFromRendererClient(
2395 int* error_code, bool* stale_cache_entry_present) {
2396 bool result = false;
2397
2398 PostTaskToInProcessRendererAndWait(
2399 base::Bind(&RenderViewBrowserTest::GetLatestErrorFromRendererClient0,
2400 renderer_client_, &result, error_code,
2401 stale_cache_entry_present));
2402 return result;
2403 }
2404
2405 private:
2406 // Must be run on renderer thread.
2407 static void GetLatestErrorFromRendererClient0(
2408 TestShellContentRendererClient* renderer_client,
2409 bool* result, int* error_code, bool* stale_cache_entry_present) {
2410 *result = renderer_client->GetLatestError(
2411 error_code, stale_cache_entry_present);
2412 }
2413
2414 // Actually owned by our superclass, so safe to keep a bare pointer.
2415 TestShellContentRendererClient* renderer_client_;
2416 };
2417
2418 IN_PROC_BROWSER_TEST_F(RenderViewBrowserTest, ConfirmCacheInformationPlumbed) {
2419 ASSERT_TRUE(test_server()->Start());
2420
2421 // Load URL with "nocache" set, to create stale cache.
2422 GURL test_url(test_server()->GetURL("files/nocache.html"));
2423 NavigateToURLAndWaitForTitle(test_url, "Nocache Test Page", 1);
2424
2425 // Reload same URL after forcing an error from the the network layer;
2426 // confirm that the error page is told the cached copy exists.
2427 int renderer_id =
2428 shell()->web_contents()->GetMainFrame()->GetProcess()->GetID();
2429 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter =
2430 ShellContentBrowserClient::Get()->browser_context()->
2431 GetRequestContextForRenderProcess(renderer_id);
2432 BrowserThread::PostTask(
2433 BrowserThread::IO, FROM_HERE,
2434 base::Bind(&InterceptNetworkTransactions, url_request_context_getter,
2435 net::ERR_FAILED));
2436
2437 // An error results in one completed navigation.
2438 content::NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 1);
2439 int error_code = net::OK;
2440 bool stale_cache_entry_present = false;
2441 ASSERT_TRUE(GetLatestErrorFromRendererClient(
2442 &error_code, &stale_cache_entry_present));
2443 EXPECT_EQ(net::ERR_FAILED, error_code);
2444 EXPECT_TRUE(stale_cache_entry_present);
2445
2446 // Clear the cache and repeat; confirm lack of entry in cache reported.
2447 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
2448 BrowserThread::PostTask(
2449 BrowserThread::IO, FROM_HERE,
2450 base::Bind(&ClearCache, url_request_context_getter,
2451 runner->QuitClosure()));
2452 runner->Run();
2453
2454 content::NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 1);
2455
2456 error_code = net::OK;
2457 stale_cache_entry_present = true;
2458 ASSERT_TRUE(GetLatestErrorFromRendererClient(
2459 &error_code, &stale_cache_entry_present));
2460 EXPECT_EQ(net::ERR_FAILED, error_code);
2461 EXPECT_FALSE(stale_cache_entry_present);
2462 }
2463
2243 } // namespace content 2464 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698