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

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

Issue 1409163006: Migrating tests to use EmbeddedTestServer (/chrome/browser misc) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix rebase bug. Created 5 years, 1 month 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/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 23 matching lines...) Expand all
34 #include "content/public/browser/navigation_controller.h" 34 #include "content/public/browser/navigation_controller.h"
35 #include "content/public/browser/navigation_entry.h" 35 #include "content/public/browser/navigation_entry.h"
36 #include "content/public/browser/notification_service.h" 36 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/render_process_host.h" 37 #include "content/public/browser/render_process_host.h"
38 #include "content/public/browser/render_view_host.h" 38 #include "content/public/browser/render_view_host.h"
39 #include "content/public/browser/render_widget_host.h" 39 #include "content/public/browser/render_widget_host.h"
40 #include "content/public/browser/web_contents.h" 40 #include "content/public/browser/web_contents.h"
41 #include "content/public/test/browser_test_utils.h" 41 #include "content/public/test/browser_test_utils.h"
42 #include "content/public/test/test_utils.h" 42 #include "content/public/test/test_utils.h"
43 #include "net/base/load_flags.h" 43 #include "net/base/load_flags.h"
44 #include "net/test/embedded_test_server/embedded_test_server.h"
44 #include "net/url_request/url_request.h" 45 #include "net/url_request/url_request.h"
45 #include "net/url_request/url_request_filter.h" 46 #include "net/url_request/url_request_filter.h"
46 #include "net/url_request/url_request_interceptor.h" 47 #include "net/url_request/url_request_interceptor.h"
47 #include "third_party/WebKit/public/web/WebContextMenuData.h" 48 #include "third_party/WebKit/public/web/WebContextMenuData.h"
48 #include "third_party/WebKit/public/web/WebInputEvent.h" 49 #include "third_party/WebKit/public/web/WebInputEvent.h"
49 50
50 #if defined(OS_CHROMEOS) 51 #if defined(OS_CHROMEOS)
51 #include "chrome/browser/chromeos/profiles/profile_helper.h" 52 #include "chrome/browser/chromeos/profiles/profile_helper.h"
52 #include "components/signin/core/account_id/account_id.h" 53 #include "components/signin/core/account_id/account_id.h"
53 #include "components/user_manager/user_manager.h" 54 #include "components/user_manager/user_manager.h"
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 234
234 // Verify that it's the correct tab. 235 // Verify that it's the correct tab.
235 EXPECT_EQ(GURL("about:blank"), tab->GetURL()); 236 EXPECT_EQ(GURL("about:blank"), tab->GetURL());
236 } 237 }
237 238
238 // Verify that "Open Link in New Tab" doesn't send URL fragment as referrer. 239 // Verify that "Open Link in New Tab" doesn't send URL fragment as referrer.
239 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenInNewTabReferrer) { 240 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenInNewTabReferrer) {
240 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( 241 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer(
241 content::NotificationService::AllSources()); 242 content::NotificationService::AllSources());
242 243
243 ASSERT_TRUE(test_server()->Start()); 244 ASSERT_TRUE(embedded_test_server()->Start());
244 GURL echoheader(test_server()->GetURL("echoheader?Referer")); 245 GURL echoheader(embedded_test_server()->GetURL("/echoheader?Referer"));
245 246
246 // Go to a |page| with a link to echoheader URL. 247 // Go to a |page| with a link to echoheader URL.
247 GURL page("data:text/html,<a href='" + echoheader.spec() + "'>link</a>"); 248 GURL page("data:text/html,<a href='" + echoheader.spec() + "'>link</a>");
248 ui_test_utils::NavigateToURL(browser(), page); 249 ui_test_utils::NavigateToURL(browser(), page);
249 250
250 // Set up referrer URL with fragment. 251 // Set up referrer URL with fragment.
251 const GURL kReferrerWithFragment("http://foo.com/test#fragment"); 252 const GURL kReferrerWithFragment("http://foo.com/test#fragment");
252 const std::string kCorrectReferrer("http://foo.com/test"); 253 const std::string kCorrectReferrer("http://foo.com/test");
253 254
254 // Set up menu with link URL. 255 // Set up menu with link URL.
(...skipping 29 matching lines...) Expand all
284 "window.domAutomationController.send(window.document.referrer);", 285 "window.domAutomationController.send(window.document.referrer);",
285 &page_referrer)); 286 &page_referrer));
286 ASSERT_EQ(kCorrectReferrer, page_referrer); 287 ASSERT_EQ(kCorrectReferrer, page_referrer);
287 } 288 }
288 289
289 // Verify that "Open Link in Incognito Window " doesn't send referrer URL. 290 // Verify that "Open Link in Incognito Window " doesn't send referrer URL.
290 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenIncognitoNoneReferrer) { 291 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenIncognitoNoneReferrer) {
291 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( 292 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer(
292 content::NotificationService::AllSources()); 293 content::NotificationService::AllSources());
293 294
294 ASSERT_TRUE(test_server()->Start()); 295 ASSERT_TRUE(embedded_test_server()->Start());
295 GURL echoheader(test_server()->GetURL("echoheader?Referer")); 296 GURL echoheader(embedded_test_server()->GetURL("/echoheader?Referer"));
296 297
297 // Go to a |page| with a link to echoheader URL. 298 // Go to a |page| with a link to echoheader URL.
298 GURL page("data:text/html,<a href='" + echoheader.spec() + "'>link</a>"); 299 GURL page("data:text/html,<a href='" + echoheader.spec() + "'>link</a>");
299 ui_test_utils::NavigateToURL(browser(), page); 300 ui_test_utils::NavigateToURL(browser(), page);
300 301
301 // Set up referrer URL with fragment. 302 // Set up referrer URL with fragment.
302 const GURL kReferrerWithFragment("http://foo.com/test#fragment"); 303 const GURL kReferrerWithFragment("http://foo.com/test#fragment");
303 const std::string kNoneReferrer("None"); 304 const std::string kNoneReferrer("None");
304 const std::string kEmptyReferrer(""); 305 const std::string kEmptyReferrer("");
305 306
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 DISALLOW_COPY_AND_ASSIGN(ThumbnailResponseWatcher); 596 DISALLOW_COPY_AND_ASSIGN(ThumbnailResponseWatcher);
596 }; 597 };
597 598
598 // Maintains image search test state. In particular, note that |menu_observer_| 599 // Maintains image search test state. In particular, note that |menu_observer_|
599 // must live until the right-click completes asynchronously. 600 // must live until the right-click completes asynchronously.
600 class SearchByImageBrowserTest : public InProcessBrowserTest { 601 class SearchByImageBrowserTest : public InProcessBrowserTest {
601 protected: 602 protected:
602 void SetupAndLoadImagePage(const std::string& image_path) { 603 void SetupAndLoadImagePage(const std::string& image_path) {
603 // The test server must start first, so that we know the port that the test 604 // The test server must start first, so that we know the port that the test
604 // server is using. 605 // server is using.
605 ASSERT_TRUE(test_server()->Start()); 606 ASSERT_TRUE(embedded_test_server()->Start());
606 SetupImageSearchEngine(); 607 SetupImageSearchEngine();
607 608
608 // Go to a page with an image in it. The test server doesn't serve the image 609 // Go to a page with an image in it. The test server doesn't serve the image
609 // with the right MIME type, so use a data URL to make a page containing it. 610 // with the right MIME type, so use a data URL to make a page containing it.
610 GURL image_url(test_server()->GetURL(image_path)); 611 GURL image_url(embedded_test_server()->GetURL(image_path));
611 GURL page("data:text/html,<img src='" + image_url.spec() + "'>"); 612 GURL page("data:text/html,<img src='" + image_url.spec() + "'>");
612 ui_test_utils::NavigateToURL(browser(), page); 613 ui_test_utils::NavigateToURL(browser(), page);
613 } 614 }
614 615
615 void AttemptImageSearch() { 616 void AttemptImageSearch() {
616 // Right-click where the image should be. 617 // Right-click where the image should be.
617 // |menu_observer_| will cause the search-by-image menu item to be clicked. 618 // |menu_observer_| will cause the search-by-image menu item to be clicked.
618 menu_observer_.reset(new ContextMenuNotificationObserver( 619 menu_observer_.reset(new ContextMenuNotificationObserver(
619 IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE)); 620 IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE));
620 content::WebContents* tab = 621 content::WebContents* tab =
621 browser()->tab_strip_model()->GetActiveWebContents(); 622 browser()->tab_strip_model()->GetActiveWebContents();
622 content::SimulateMouseClickAt(tab, 0, blink::WebMouseEvent::ButtonRight, 623 content::SimulateMouseClickAt(tab, 0, blink::WebMouseEvent::ButtonRight,
623 gfx::Point(15, 15)); 624 gfx::Point(15, 15));
624 } 625 }
625 626
626 GURL GetImageSearchURL() { 627 GURL GetImageSearchURL() {
627 static const char kImageSearchURL[] = "imagesearch"; 628 static const char kImageSearchURL[] = "/imagesearch";
628 return test_server()->GetURL(kImageSearchURL); 629 return embedded_test_server()->GetURL(kImageSearchURL);
629 } 630 }
630 631
631 private: 632 private:
632 void SetupImageSearchEngine() { 633 void SetupImageSearchEngine() {
633 static const char kShortName[] = "test"; 634 static const char kShortName[] = "test";
634 static const char kSearchURL[] = "search?q={searchTerms}"; 635 static const char kSearchURL[] = "/search?q={searchTerms}";
635 static const char kImageSearchPostParams[] = 636 static const char kImageSearchPostParams[] =
636 "thumb={google:imageThumbnail}"; 637 "thumb={google:imageThumbnail}";
637 638
638 TemplateURLService* model = 639 TemplateURLService* model =
639 TemplateURLServiceFactory::GetForProfile(browser()->profile()); 640 TemplateURLServiceFactory::GetForProfile(browser()->profile());
640 ASSERT_TRUE(model); 641 ASSERT_TRUE(model);
641 search_test_utils::WaitForTemplateURLServiceToLoad(model); 642 search_test_utils::WaitForTemplateURLServiceToLoad(model);
642 ASSERT_TRUE(model->loaded()); 643 ASSERT_TRUE(model->loaded());
643 644
644 TemplateURLData data; 645 TemplateURLData data;
645 data.SetShortName(base::ASCIIToUTF16(kShortName)); 646 data.SetShortName(base::ASCIIToUTF16(kShortName));
646 data.SetKeyword(data.short_name()); 647 data.SetKeyword(data.short_name());
647 data.SetURL(test_server()->GetURL(kSearchURL).spec()); 648 data.SetURL(embedded_test_server()->GetURL(kSearchURL).spec());
648 data.image_url = GetImageSearchURL().spec(); 649 data.image_url = GetImageSearchURL().spec();
649 data.image_url_post_params = kImageSearchPostParams; 650 data.image_url_post_params = kImageSearchPostParams;
650 651
651 // The model takes ownership of |template_url|. 652 // The model takes ownership of |template_url|.
652 TemplateURL* template_url = new TemplateURL(data); 653 TemplateURL* template_url = new TemplateURL(data);
653 ASSERT_TRUE(model->Add(template_url)); 654 ASSERT_TRUE(model->Add(template_url));
654 model->SetUserSelectedDefaultSearchProvider(template_url); 655 model->SetUserSelectedDefaultSearchProvider(template_url);
655 } 656 }
656 657
657 void TearDownInProcessBrowserTestFixture() override { 658 void TearDownInProcessBrowserTestFixture() override {
658 menu_observer_.reset(); 659 menu_observer_.reset();
659 } 660 }
660 661
661 scoped_ptr<ContextMenuNotificationObserver> menu_observer_; 662 scoped_ptr<ContextMenuNotificationObserver> menu_observer_;
662 }; 663 };
663 664
664 IN_PROC_BROWSER_TEST_F(SearchByImageBrowserTest, ImageSearchWithValidImage) { 665 IN_PROC_BROWSER_TEST_F(SearchByImageBrowserTest, ImageSearchWithValidImage) {
665 static const char kValidImage[] = "files/image_search/valid.png"; 666 static const char kValidImage[] = "/image_search/valid.png";
666 SetupAndLoadImagePage(kValidImage); 667 SetupAndLoadImagePage(kValidImage);
667 668
668 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( 669 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer(
669 content::NotificationService::AllSources()); 670 content::NotificationService::AllSources());
670 AttemptImageSearch(); 671 AttemptImageSearch();
671 672
672 // The browser should open a new tab for an image search. 673 // The browser should open a new tab for an image search.
673 tab_observer.Wait(); 674 tab_observer.Wait();
674 content::WebContents* new_tab = tab_observer.GetTab(); 675 content::WebContents* new_tab = tab_observer.GetTab();
675 content::WaitForLoadStop(new_tab); 676 content::WaitForLoadStop(new_tab);
676 EXPECT_EQ(GetImageSearchURL(), new_tab->GetURL()); 677 EXPECT_EQ(GetImageSearchURL(), new_tab->GetURL());
677 } 678 }
678 679
679 IN_PROC_BROWSER_TEST_F(SearchByImageBrowserTest, ImageSearchWithCorruptImage) { 680 IN_PROC_BROWSER_TEST_F(SearchByImageBrowserTest, ImageSearchWithCorruptImage) {
680 static const char kCorruptImage[] = "files/image_search/corrupt.png"; 681 static const char kCorruptImage[] = "/image_search/corrupt.png";
681 SetupAndLoadImagePage(kCorruptImage); 682 SetupAndLoadImagePage(kCorruptImage);
682 683
683 content::WebContents* tab = 684 content::WebContents* tab =
684 browser()->tab_strip_model()->GetActiveWebContents(); 685 browser()->tab_strip_model()->GetActiveWebContents();
685 ThumbnailResponseWatcher watcher(tab->GetRenderProcessHost()); 686 ThumbnailResponseWatcher watcher(tab->GetRenderProcessHost());
686 AttemptImageSearch(); 687 AttemptImageSearch();
687 688
688 // The browser should receive a response from the renderer, because the 689 // The browser should receive a response from the renderer, because the
689 // renderer should not crash. 690 // renderer should not crash.
690 EXPECT_EQ(ThumbnailResponseWatcher::THUMBNAIL_RECEIVED, watcher.Wait()); 691 EXPECT_EQ(ThumbnailResponseWatcher::THUMBNAIL_RECEIVED, watcher.Wait());
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 // This prevents any risk of flake if any test doesn't wait for a request 754 // This prevents any risk of flake if any test doesn't wait for a request
754 // it sent. Mutable so it can be accessed from a const function. 755 // it sent. Mutable so it can be accessed from a const function.
755 mutable base::WeakPtrFactory<LoadImageRequestInterceptor> weak_factory_; 756 mutable base::WeakPtrFactory<LoadImageRequestInterceptor> weak_factory_;
756 757
757 DISALLOW_COPY_AND_ASSIGN(LoadImageRequestInterceptor); 758 DISALLOW_COPY_AND_ASSIGN(LoadImageRequestInterceptor);
758 }; 759 };
759 760
760 class LoadImageBrowserTest : public InProcessBrowserTest { 761 class LoadImageBrowserTest : public InProcessBrowserTest {
761 protected: 762 protected:
762 void SetupAndLoadImagePage(const std::string& image_path) { 763 void SetupAndLoadImagePage(const std::string& image_path) {
764 ASSERT_TRUE(embedded_test_server()->Start());
763 // Go to a page with an image in it. The test server doesn't serve the image 765 // Go to a page with an image in it. The test server doesn't serve the image
764 // with the right MIME type, so use a data URL to make a page containing it. 766 // with the right MIME type, so use a data URL to make a page containing it.
765 GURL image_url(test_server()->GetURL(image_path)); 767 GURL image_url(embedded_test_server()->GetURL(image_path));
766 GURL page("data:text/html,<img src='" + image_url.spec() + "'>"); 768 GURL page("data:text/html,<img src='" + image_url.spec() + "'>");
767 ui_test_utils::NavigateToURL(browser(), page); 769 ui_test_utils::NavigateToURL(browser(), page);
768 } 770 }
769 771
770 void AddLoadImageInterceptor(const std::string& image_path) { 772 void AddLoadImageInterceptor(const std::string& image_path) {
771 interceptor_ = new LoadImageRequestInterceptor(); 773 interceptor_ = new LoadImageRequestInterceptor();
772 scoped_ptr<net::URLRequestInterceptor> owned_interceptor(interceptor_); 774 scoped_ptr<net::URLRequestInterceptor> owned_interceptor(interceptor_);
773 content::BrowserThread::PostTask( 775 content::BrowserThread::PostTask(
774 content::BrowserThread::IO, FROM_HERE, 776 content::BrowserThread::IO, FROM_HERE,
775 base::Bind(&LoadImageBrowserTest::AddInterceptorForURL, 777 base::Bind(&LoadImageBrowserTest::AddInterceptorForURL,
776 base::Unretained(this), 778 base::Unretained(this),
777 GURL(test_server()->GetURL(image_path).spec()), 779 GURL(embedded_test_server()->GetURL(image_path).spec()),
mmenke 2015/11/17 21:54:12 Erm...how did this test work before, without setti
svaldez 2015/11/17 22:18:54 I believe because this test doesn't actually check
778 base::Passed(&owned_interceptor))); 780 base::Passed(&owned_interceptor)));
779 } 781 }
780 782
781 void AttemptLoadImage() { 783 void AttemptLoadImage() {
782 // Right-click where the image should be. 784 // Right-click where the image should be.
783 // |menu_observer_| will cause the "Load image" menu item to be clicked. 785 // |menu_observer_| will cause the "Load image" menu item to be clicked.
784 menu_observer_.reset(new ContextMenuNotificationObserver( 786 menu_observer_.reset(new ContextMenuNotificationObserver(
785 IDC_CONTENT_CONTEXT_LOAD_ORIGINAL_IMAGE)); 787 IDC_CONTENT_CONTEXT_LOAD_ORIGINAL_IMAGE));
786 content::WebContents* tab = 788 content::WebContents* tab =
787 browser()->tab_strip_model()->GetActiveWebContents(); 789 browser()->tab_strip_model()->GetActiveWebContents();
788 content::SimulateMouseClickAt(tab, 0, blink::WebMouseEvent::ButtonRight, 790 content::SimulateMouseClickAt(tab, 0, blink::WebMouseEvent::ButtonRight,
789 gfx::Point(15, 15)); 791 gfx::Point(15, 15));
790 } 792 }
791 793
792 void AddInterceptorForURL( 794 void AddInterceptorForURL(
793 const GURL& url, scoped_ptr<net::URLRequestInterceptor> handler) { 795 const GURL& url, scoped_ptr<net::URLRequestInterceptor> handler) {
794 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 796 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
795 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 797 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
796 url, handler.Pass()); 798 url, handler.Pass());
797 } 799 }
798 800
799 LoadImageRequestInterceptor* interceptor_; 801 LoadImageRequestInterceptor* interceptor_;
800 802
801 private: 803 private:
802 scoped_ptr<ContextMenuNotificationObserver> menu_observer_; 804 scoped_ptr<ContextMenuNotificationObserver> menu_observer_;
803 }; 805 };
804 806
805 IN_PROC_BROWSER_TEST_F(LoadImageBrowserTest, LoadImage) { 807 IN_PROC_BROWSER_TEST_F(LoadImageBrowserTest, LoadImage) {
806 static const char kValidImage[] = "files/load_image/image.png"; 808 static const char kValidImage[] = "/load_image/image.png";
807 SetupAndLoadImagePage(kValidImage); 809 SetupAndLoadImagePage(kValidImage);
808 AddLoadImageInterceptor(kValidImage); 810 AddLoadImageInterceptor(kValidImage);
809 AttemptLoadImage(); 811 AttemptLoadImage();
810 interceptor_->WaitForRequests(1); 812 interceptor_->WaitForRequests(1);
811 EXPECT_EQ(1, interceptor_->num_requests()); 813 EXPECT_EQ(1, interceptor_->num_requests());
812 } 814 }
813 815
814 } // namespace 816 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698