OLD | NEW |
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 Loading... |
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 Loading... |
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 bool ShouldSuppressErrorPage(RenderFrame* render_frame, |
| 333 const GURL& url) OVERRIDE { |
| 334 return url == GURL("http://example.com/suppress"); |
| 335 } |
| 336 |
| 337 virtual void GetNavigationErrorStrings( |
| 338 content::RenderView* render_view, |
| 339 blink::WebFrame* frame, |
| 340 const blink::WebURLRequest& failed_request, |
| 341 const blink::WebURLError& error, |
| 342 std::string* error_html, |
| 343 base::string16* error_description) OVERRIDE { |
| 344 if (error_html) |
| 345 *error_html = "A suffusion of yellow."; |
| 346 latest_error_valid_ = true; |
| 347 latest_error_reason_ = error.reason; |
| 348 latest_error_stale_copy_in_cache_ = error.staleCopyInCache; |
| 349 } |
| 350 |
| 351 bool GetLatestError(int* error_code, bool* stale_cache_entry_present) { |
| 352 if (latest_error_valid_) { |
| 353 *error_code = latest_error_reason_; |
| 354 *stale_cache_entry_present = latest_error_stale_copy_in_cache_; |
| 355 } |
| 356 return latest_error_valid_; |
| 357 } |
| 358 |
| 359 private: |
| 360 bool latest_error_valid_; |
| 361 int latest_error_reason_; |
| 362 bool latest_error_stale_copy_in_cache_; |
| 363 }; |
| 364 |
| 365 |
| 366 // For actual, browser side tests targetting code in RenderView |
| 367 // or RenderViewImpl. |
| 368 class RenderViewBrowserTest : public ContentBrowserTest { |
| 369 public: |
| 370 RenderViewBrowserTest() : renderer_client_(NULL) {} |
| 371 |
| 372 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 373 // To allow interaction with in-process renderer and use |
| 374 // of a test ContentRendererClient. |
| 375 command_line->AppendSwitch(switches::kSingleProcess); |
| 376 } |
| 377 |
| 378 virtual void SetUp() OVERRIDE { |
| 379 // Override setting of renderer client. |
| 380 renderer_client_ = new TestShellContentRendererClient(); |
| 381 SetContentRendererClient( |
| 382 scoped_ptr<ContentRendererClient>(renderer_client_).Pass()); |
| 383 |
| 384 ContentBrowserTest::SetUp(); |
| 385 } |
| 386 |
| 387 // Navigates to the given URL and waits for |num_navigations| to occur, and |
| 388 // the title to change to |expected_title|. |
| 389 void NavigateToURLAndWaitForTitle(const GURL& url, |
| 390 const std::string& expected_title, |
| 391 int num_navigations) { |
| 392 content::TitleWatcher title_watcher( |
| 393 shell()->web_contents(), base::ASCIIToUTF16(expected_title)); |
| 394 |
| 395 content::NavigateToURLBlockUntilNavigationsComplete( |
| 396 shell(), url, num_navigations); |
| 397 |
| 398 EXPECT_EQ(base::ASCIIToUTF16(expected_title), |
| 399 title_watcher.WaitAndGetTitle()); |
| 400 } |
| 401 |
| 402 // Returns true if there is a valid error stored; in this case |
| 403 // |*error| will be set to that value. |
| 404 // Must be called after the renderer thread is created. |
| 405 bool GetLatestErrorFromRendererClient( |
| 406 int* error_code, bool* stale_cache_entry_present) { |
| 407 bool result = false; |
| 408 |
| 409 PostTaskToInProcessRendererAndWait( |
| 410 base::Bind(&RenderViewBrowserTest::GetLatestErrorFromRendererClient0, |
| 411 renderer_client_, &result, error_code, |
| 412 stale_cache_entry_present)); |
| 413 return result; |
| 414 } |
| 415 |
| 416 private: |
| 417 // Must be run on renderer thread. |
| 418 static void GetLatestErrorFromRendererClient0( |
| 419 TestShellContentRendererClient* renderer_client, |
| 420 bool* result, int* error_code, bool* stale_cache_entry_present) { |
| 421 *result = renderer_client->GetLatestError( |
| 422 error_code, stale_cache_entry_present); |
| 423 } |
| 424 |
| 425 // Actually owned by our superclass, so safe to keep a bare pointer. |
| 426 TestShellContentRendererClient* renderer_client_; |
| 427 }; |
| 428 |
| 429 // Must be called on IO thread. |
| 430 void InterceptNetworkTransactions(net::URLRequestContextGetter* getter, |
| 431 net::Error error) { |
| 432 DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 433 net::HttpCache* cache( |
| 434 getter->GetURLRequestContext()->http_transaction_factory()->GetCache()); |
| 435 DCHECK(cache); |
| 436 scoped_ptr<net::FailingHttpTransactionFactory> factory( |
| 437 new net::FailingHttpTransactionFactory(cache->GetSession(), error)); |
| 438 // Throw away old version; since we're a browser test, we don't |
| 439 // need to restore the old state. |
| 440 cache->SetHttpNetworkTransactionFactoryForTesting( |
| 441 factory.PassAs<net::HttpTransactionFactory>()); |
| 442 } |
| 443 |
| 444 void CallOnUIThreadValidatingReturn(const base::Closure& callback, |
| 445 int rv) { |
| 446 DCHECK_EQ(net::OK, rv); |
| 447 BrowserThread::PostTask( |
| 448 BrowserThread::UI, FROM_HERE, callback); |
| 449 } |
| 450 |
| 451 // Must be called on IO thread. The callback will be called on |
| 452 // completion of cache clearing on the UI thread. |
| 453 void BackendClearCache(disk_cache::Backend** backend, |
| 454 const base::Closure& callback, |
| 455 int rv) { |
| 456 DCHECK(*backend); |
| 457 DCHECK_EQ(net::OK, rv); |
| 458 (*backend)->DoomAllEntries( |
| 459 base::Bind(&CallOnUIThreadValidatingReturn, callback)); |
| 460 // Not deleting the backend, just the spot allocated for storing |
| 461 // the pointer. |
| 462 delete backend; |
| 463 return; |
| 464 } |
| 465 |
| 466 // Must be called on IO thread. The callback will be called on |
| 467 // completion of cache clearing on the UI thread. |
| 468 void ClearCache(net::URLRequestContextGetter* getter, |
| 469 const base::Closure& callback) { |
| 470 DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 471 net::HttpCache* cache( |
| 472 getter->GetURLRequestContext()->http_transaction_factory()->GetCache()); |
| 473 DCHECK(cache); |
| 474 disk_cache::Backend** backend = new disk_cache::Backend*; |
| 475 *backend = NULL; |
| 476 if (net::OK == cache->GetBackend( |
| 477 backend, base::Bind(&BackendClearCache, backend, callback))) { |
| 478 // Completed synchronously, so our job to call the callback. |
| 479 BackendClearCache(backend, callback, net::OK); |
| 480 // *backend has been deleted. |
| 481 } |
| 482 } |
| 483 |
| 484 } // namespace |
| 485 |
307 // Test that we get form state change notifications when input fields change. | 486 // Test that we get form state change notifications when input fields change. |
308 TEST_F(RenderViewImplTest, DISABLED_OnNavStateChanged) { | 487 TEST_F(RenderViewImplTest, DISABLED_OnNavStateChanged) { |
309 // Don't want any delay for form state sync changes. This will still post a | 488 // 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 | 489 // message so updates will get coalesced, but as soon as we spin the message |
311 // loop, it will generate an update. | 490 // loop, it will generate an update. |
312 view()->set_send_content_state_immediately(true); | 491 view()->set_send_content_state_immediately(true); |
313 | 492 |
314 LoadHTML("<input type=\"text\" id=\"elt_text\"></input>"); | 493 LoadHTML("<input type=\"text\" id=\"elt_text\"></input>"); |
315 | 494 |
316 // We should NOT have gotten a form state change notification yet. | 495 // We should NOT have gotten a form state change notification yet. |
(...skipping 1916 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2233 | 2412 |
2234 view()->webview()->clearFocusedNode(); | 2413 view()->webview()->clearFocusedNode(); |
2235 const IPC::Message* msg3 = render_thread_->sink().GetFirstMessageMatching( | 2414 const IPC::Message* msg3 = render_thread_->sink().GetFirstMessageMatching( |
2236 ViewHostMsg_FocusedNodeChanged::ID); | 2415 ViewHostMsg_FocusedNodeChanged::ID); |
2237 EXPECT_TRUE(msg3); | 2416 EXPECT_TRUE(msg3); |
2238 ViewHostMsg_FocusedNodeChanged::Read(msg3, ¶ms); | 2417 ViewHostMsg_FocusedNodeChanged::Read(msg3, ¶ms); |
2239 EXPECT_FALSE(params.a); | 2418 EXPECT_FALSE(params.a); |
2240 render_thread_->sink().ClearMessages(); | 2419 render_thread_->sink().ClearMessages(); |
2241 } | 2420 } |
2242 | 2421 |
| 2422 IN_PROC_BROWSER_TEST_F(RenderViewBrowserTest, ConfirmCacheInformationPlumbed) { |
| 2423 ASSERT_TRUE(test_server()->Start()); |
| 2424 |
| 2425 // Load cache with entry with "nocache" set, to create stale |
| 2426 // cache. |
| 2427 GURL test_url(test_server()->GetURL("files/nocache.html")); |
| 2428 NavigateToURLAndWaitForTitle(test_url, "Nocache Test Page", 1); |
| 2429 |
| 2430 // Reload same URL after forcing an error from the the network layer; |
| 2431 // confirm that the error page is told the cached copy exists. |
| 2432 int renderer_id = |
| 2433 shell()->web_contents()->GetMainFrame()->GetProcess()->GetID(); |
| 2434 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter = |
| 2435 ShellContentBrowserClient::Get()->browser_context()-> |
| 2436 GetRequestContextForRenderProcess(renderer_id); |
| 2437 BrowserThread::PostTask( |
| 2438 BrowserThread::IO, FROM_HERE, |
| 2439 base::Bind(&InterceptNetworkTransactions, url_request_context_getter, |
| 2440 net::ERR_FAILED)); |
| 2441 |
| 2442 // An error results in one completed navigation. |
| 2443 content::NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 1); |
| 2444 int error_code = net::OK; |
| 2445 bool stale_cache_entry_present = false; |
| 2446 ASSERT_TRUE(GetLatestErrorFromRendererClient( |
| 2447 &error_code, &stale_cache_entry_present)); |
| 2448 ASSERT_EQ(net::ERR_FAILED, error_code); |
| 2449 ASSERT_TRUE(stale_cache_entry_present); |
| 2450 |
| 2451 // Clear the cache and repeat; confirm lack of entry in cache reported. |
| 2452 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; |
| 2453 BrowserThread::PostTask( |
| 2454 BrowserThread::IO, FROM_HERE, |
| 2455 base::Bind(&ClearCache, url_request_context_getter, |
| 2456 runner->QuitClosure())); |
| 2457 runner->Run(); |
| 2458 |
| 2459 content::NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 1); |
| 2460 |
| 2461 error_code = net::OK; |
| 2462 stale_cache_entry_present = true; |
| 2463 ASSERT_TRUE(GetLatestErrorFromRendererClient( |
| 2464 &error_code, &stale_cache_entry_present)); |
| 2465 ASSERT_EQ(net::ERR_FAILED, error_code); |
| 2466 ASSERT_FALSE(stale_cache_entry_present); |
| 2467 } |
| 2468 |
2243 } // namespace content | 2469 } // namespace content |
OLD | NEW |