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

Side by Side Diff: chrome/browser/renderer_context_menu/render_view_context_menu_browsertest.cc

Issue 1105263004: "Load image" context menu item to reload a LoFi image. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: All comments and move Chrome-Proxy logic outside of content/ Created 5 years, 7 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <string> 5 #include <string>
6 6
7 #include "base/bind.h"
7 #include "base/command_line.h" 8 #include "base/command_line.h"
8 #include "base/macros.h" 9 #include "base/macros.h"
9 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string16.h" 12 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/app/chrome_command_ids.h" 14 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" 16 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
16 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert est_util.h" 17 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert est_util.h"
(...skipping 10 matching lines...) Expand all
27 #include "content/public/browser/browser_message_filter.h" 28 #include "content/public/browser/browser_message_filter.h"
28 #include "content/public/browser/browser_thread.h" 29 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/navigation_controller.h" 30 #include "content/public/browser/navigation_controller.h"
30 #include "content/public/browser/navigation_entry.h" 31 #include "content/public/browser/navigation_entry.h"
31 #include "content/public/browser/notification_service.h" 32 #include "content/public/browser/notification_service.h"
32 #include "content/public/browser/render_process_host.h" 33 #include "content/public/browser/render_process_host.h"
33 #include "content/public/browser/render_view_host.h" 34 #include "content/public/browser/render_view_host.h"
34 #include "content/public/browser/web_contents.h" 35 #include "content/public/browser/web_contents.h"
35 #include "content/public/test/browser_test_utils.h" 36 #include "content/public/test/browser_test_utils.h"
36 #include "content/public/test/test_utils.h" 37 #include "content/public/test/test_utils.h"
38 #include "net/base/load_flags.h"
39 #include "net/url_request/url_request.h"
40 #include "net/url_request/url_request_filter.h"
41 #include "net/url_request/url_request_interceptor.h"
37 #include "third_party/WebKit/public/web/WebContextMenuData.h" 42 #include "third_party/WebKit/public/web/WebContextMenuData.h"
38 #include "third_party/WebKit/public/web/WebInputEvent.h" 43 #include "third_party/WebKit/public/web/WebInputEvent.h"
39 44
40 using content::WebContents; 45 using content::WebContents;
41 46
42 namespace { 47 namespace {
43 48
44 class ContextMenuBrowserTest : public InProcessBrowserTest { 49 class ContextMenuBrowserTest : public InProcessBrowserTest {
45 public: 50 public:
46 ContextMenuBrowserTest() {} 51 ContextMenuBrowserTest() {}
47 52
48 TestRenderViewContextMenu* CreateContextMenu( 53 content::ContextMenuParams CreateContextMenuParams(
49 const GURL& unfiltered_url, 54 const GURL& unfiltered_url,
50 const GURL& url, 55 const GURL& url,
51 blink::WebContextMenuData::MediaType media_type) { 56 blink::WebContextMenuData::MediaType media_type) {
52 content::ContextMenuParams params; 57 content::ContextMenuParams params;
53 params.media_type = media_type; 58 params.media_type = media_type;
54 params.unfiltered_link_url = unfiltered_url; 59 params.unfiltered_link_url = unfiltered_url;
55 params.link_url = url; 60 params.link_url = url;
56 params.src_url = url; 61 params.src_url = url;
57 WebContents* web_contents = 62 WebContents* web_contents =
58 browser()->tab_strip_model()->GetActiveWebContents(); 63 browser()->tab_strip_model()->GetActiveWebContents();
59 params.page_url = web_contents->GetController().GetActiveEntry()->GetURL(); 64 params.page_url = web_contents->GetController().GetActiveEntry()->GetURL();
60 #if defined(OS_MACOSX) 65 #if defined(OS_MACOSX)
61 params.writing_direction_default = 0; 66 params.writing_direction_default = 0;
62 params.writing_direction_left_to_right = 0; 67 params.writing_direction_left_to_right = 0;
63 params.writing_direction_right_to_left = 0; 68 params.writing_direction_right_to_left = 0;
64 #endif // OS_MACOSX 69 #endif // OS_MACOSX
70 return params;
71 }
72
73 TestRenderViewContextMenu* CreateContextMenu(
74 content::ContextMenuParams params) {
65 TestRenderViewContextMenu* menu = new TestRenderViewContextMenu( 75 TestRenderViewContextMenu* menu = new TestRenderViewContextMenu(
66 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), 76 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
67 params); 77 params);
68 menu->Init(); 78 menu->Init();
69 return menu; 79 return menu;
70 } 80 }
71 81
72 TestRenderViewContextMenu* CreateContextMenuMediaTypeNone( 82 TestRenderViewContextMenu* CreateContextMenuMediaTypeNone(
73 const GURL& unfiltered_url, 83 const GURL& unfiltered_url,
74 const GURL& url) { 84 const GURL& url) {
75 return CreateContextMenu(unfiltered_url, url, 85 return CreateContextMenu(CreateContextMenuParams(
76 blink::WebContextMenuData::MediaTypeNone); 86 unfiltered_url, url, blink::WebContextMenuData::MediaTypeNone));
77 } 87 }
78 }; 88 };
79 89
80 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, 90 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest,
81 OpenEntryPresentForNormalURLs) { 91 OpenEntryPresentForNormalURLs) {
82 scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenuMediaTypeNone( 92 scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenuMediaTypeNone(
83 GURL("http://www.google.com/"), GURL("http://www.google.com/"))); 93 GURL("http://www.google.com/"), GURL("http://www.google.com/")));
84 94
85 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB)); 95 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
86 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW)); 96 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 TestRenderViewContextMenu menu(tab->GetMainFrame(), context_menu_params); 311 TestRenderViewContextMenu menu(tab->GetMainFrame(), context_menu_params);
302 menu.Init(); 312 menu.Init();
303 313
304 // The item shouldn't be enabled in the menu. 314 // The item shouldn't be enabled in the menu.
305 EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_CONTENT_CONTEXT_VIEWPAGEINFO)); 315 EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_CONTENT_CONTEXT_VIEWPAGEINFO));
306 316
307 // Ensure that viewing page info doesn't crash even if you can get to it. 317 // Ensure that viewing page info doesn't crash even if you can get to it.
308 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_VIEWPAGEINFO, 0); 318 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_VIEWPAGEINFO, 0);
309 } 319 }
310 320
321 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, DataSaverLoadImage) {
322 content::ContextMenuParams params = CreateContextMenuParams(
323 GURL(), GURL("http://url.com/image.png"),
324 blink::WebContextMenuData::MediaTypeImage);
325 params.image_was_fetched_lo_fi = true;
326
327 scoped_ptr<TestRenderViewContextMenu> menu(CreateContextMenu(params));
328
329 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_RELOAD_ORIGINAL_IMAGE));
330 }
331
311 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, DataSaverOpenOrigImageInNewTab) { 332 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, DataSaverOpenOrigImageInNewTab) {
312 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 333 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
313 command_line->AppendSwitch( 334 command_line->AppendSwitch(
314 data_reduction_proxy::switches::kEnableDataReductionProxy); 335 data_reduction_proxy::switches::kEnableDataReductionProxy);
315 336
316 scoped_ptr<TestRenderViewContextMenu> menu( 337 scoped_ptr<TestRenderViewContextMenu> menu(
317 CreateContextMenu(GURL(), GURL("http://url.com/image.png"), 338 CreateContextMenu(CreateContextMenuParams(
318 blink::WebContextMenuData::MediaTypeImage)); 339 GURL(), GURL("http://url.com/image.png"),
340 blink::WebContextMenuData::MediaTypeImage)));
319 341
320 ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB)); 342 ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB));
321 ASSERT_TRUE(menu->IsItemPresent( 343 ASSERT_TRUE(menu->IsItemPresent(
322 IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB)); 344 IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB));
323 } 345 }
324 346
325 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, 347 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest,
326 DataSaverHttpsOpenImageInNewTab) { 348 DataSaverHttpsOpenImageInNewTab) {
327 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 349 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
328 command_line->AppendSwitch( 350 command_line->AppendSwitch(
329 data_reduction_proxy::switches::kEnableDataReductionProxy); 351 data_reduction_proxy::switches::kEnableDataReductionProxy);
330 352
331 scoped_ptr<TestRenderViewContextMenu> menu( 353 scoped_ptr<TestRenderViewContextMenu> menu(
332 CreateContextMenu(GURL(), GURL("https://url.com/image.png"), 354 CreateContextMenu(CreateContextMenuParams(
333 blink::WebContextMenuData::MediaTypeImage)); 355 GURL(), GURL("https://url.com/image.png"),
356 blink::WebContextMenuData::MediaTypeImage)));
334 357
335 ASSERT_FALSE( 358 ASSERT_FALSE(
336 menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB)); 359 menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB));
337 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB)); 360 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB));
338 } 361 }
339 362
340 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenImageInNewTab) { 363 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenImageInNewTab) {
341 scoped_ptr<TestRenderViewContextMenu> menu( 364 scoped_ptr<TestRenderViewContextMenu> menu(
342 CreateContextMenu(GURL(), GURL("http://url.com/image.png"), 365 CreateContextMenu(CreateContextMenuParams(
343 blink::WebContextMenuData::MediaTypeImage)); 366 GURL(), GURL("http://url.com/image.png"),
367 blink::WebContextMenuData::MediaTypeImage)));
344 ASSERT_FALSE( 368 ASSERT_FALSE(
345 menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB)); 369 menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB));
346 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB)); 370 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB));
347 } 371 }
348 372
349 class ThumbnailResponseWatcher : public content::NotificationObserver { 373 class ThumbnailResponseWatcher : public content::NotificationObserver {
350 public: 374 public:
351 enum QuitReason { 375 enum QuitReason {
352 STILL_RUNNING = 0, 376 STILL_RUNNING = 0,
353 THUMBNAIL_RECEIVED, 377 THUMBNAIL_RECEIVED,
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 content::WebContents* tab = 542 content::WebContents* tab =
519 browser()->tab_strip_model()->GetActiveWebContents(); 543 browser()->tab_strip_model()->GetActiveWebContents();
520 ThumbnailResponseWatcher watcher(tab->GetRenderProcessHost()); 544 ThumbnailResponseWatcher watcher(tab->GetRenderProcessHost());
521 AttemptImageSearch(); 545 AttemptImageSearch();
522 546
523 // The browser should receive a response from the renderer, because the 547 // The browser should receive a response from the renderer, because the
524 // renderer should not crash. 548 // renderer should not crash.
525 EXPECT_EQ(ThumbnailResponseWatcher::THUMBNAIL_RECEIVED, watcher.Wait()); 549 EXPECT_EQ(ThumbnailResponseWatcher::THUMBNAIL_RECEIVED, watcher.Wait());
526 } 550 }
527 551
552 class LoadImageRequestInterceptor : public net::URLRequestInterceptor {
553 public:
554 LoadImageRequestInterceptor() : num_requests_(0),
555 requests_to_wait_for_(-1),
556 weak_factory_(this) {
557 }
558
559 ~LoadImageRequestInterceptor() override {}
560
561 // net::URLRequestInterceptor implementation
562 net::URLRequestJob* MaybeInterceptRequest(
563 net::URLRequest* request,
564 net::NetworkDelegate* network_delegate) const override {
565 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
566 EXPECT_TRUE(request->load_flags() & net::LOAD_BYPASS_CACHE);
567 content::BrowserThread::PostTask(
568 content::BrowserThread::UI, FROM_HERE,
569 base::Bind(&LoadImageRequestInterceptor::RequestCreated,
570 weak_factory_.GetWeakPtr()));
571 return nullptr;
572 }
573
574 void WaitForRequests(int requests_to_wait_for) {
575 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
576 DCHECK_EQ(-1, requests_to_wait_for_);
577 DCHECK(!run_loop_);
578
579 if (num_requests_ >= requests_to_wait_for)
580 return;
581
582 requests_to_wait_for_ = requests_to_wait_for;
583 run_loop_.reset(new base::RunLoop());
584 run_loop_->Run();
585 run_loop_.reset();
586 requests_to_wait_for_ = -1;
587 EXPECT_EQ(num_requests_, requests_to_wait_for);
588 }
589
590 // It is up to the caller to wait until all relevant requests has been
591 // created, either through calling WaitForRequests or some other manner,
592 // before calling this method.
593 int num_requests() const {
594 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
595 return num_requests_;
596 }
597
598 private:
599 void RequestCreated() {
600 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
601
602 num_requests_++;
603 if (num_requests_ == requests_to_wait_for_)
604 run_loop_->Quit();
605 }
606
607 // These are only used on the UI thread.
608 int num_requests_;
609 int requests_to_wait_for_;
610 scoped_ptr<base::RunLoop> run_loop_;
611
612 // This prevents any risk of flake if any test doesn't wait for a request
613 // it sent. Mutable so it can be accessed from a const function.
614 mutable base::WeakPtrFactory<LoadImageRequestInterceptor> weak_factory_;
615
616 DISALLOW_COPY_AND_ASSIGN(LoadImageRequestInterceptor);
617 };
618
619 class LoadImageBrowserTest : public InProcessBrowserTest {
620 protected:
621 void SetupAndLoadImagePage(const std::string& image_path) {
622 // Go to a page with an image in it. The test server doesn't serve the image
623 // with the right MIME type, so use a data URL to make a page containing it.
624 GURL image_url(test_server()->GetURL(image_path));
625 GURL page("data:text/html,<img src='" + image_url.spec() + "'>");
626 ui_test_utils::NavigateToURL(browser(), page);
627 }
628
629 void AddLoadImageInterceptor(const std::string& image_path) {
630 interceptor_ = new LoadImageRequestInterceptor();
631 scoped_ptr<net::URLRequestInterceptor> owned_interceptor(interceptor_);
632 content::BrowserThread::PostTask(
633 content::BrowserThread::IO, FROM_HERE,
634 base::Bind(&LoadImageBrowserTest::AddInterceptorForURL,
635 base::Unretained(this),
636 GURL(test_server()->GetURL(image_path).spec()),
637 base::Passed(&owned_interceptor)));
638 }
639
640 void AttemptImageLoad() {
641 // Right-click where the image should be.
642 // |menu_observer_| will cause the load-image menu item to be clicked.
643 menu_observer_.reset(new ContextMenuNotificationObserver(
644 IDC_CONTENT_CONTEXT_RELOAD_ORIGINAL_IMAGE));
645 content::WebContents* tab =
646 browser()->tab_strip_model()->GetActiveWebContents();
647 content::SimulateMouseClickAt(tab, 0, blink::WebMouseEvent::ButtonRight,
648 gfx::Point(15, 15));
649 }
650
651 void AddInterceptorForURL(
652 const GURL& url, scoped_ptr<net::URLRequestInterceptor> handler) {
653 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
654 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
655 url, handler.Pass());
656 }
657
658 LoadImageRequestInterceptor* interceptor_;
659
660 private:
661 scoped_ptr<ContextMenuNotificationObserver> menu_observer_;
662 };
663
664 IN_PROC_BROWSER_TEST_F(LoadImageBrowserTest, LoadImage) {
665 static const char kValidImage[] = "files/load_image/image.png";
666 SetupAndLoadImagePage(kValidImage);
667 AddLoadImageInterceptor(kValidImage);
668 AttemptImageLoad();
669 interceptor_->WaitForRequests(1);
670 EXPECT_EQ(1, interceptor_->num_requests());
671 }
672
528 } // namespace 673 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698