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/memory/singleton.h" | 5 #include "base/memory/singleton.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
7 #include "base/test/test_timeouts.h" | 7 #include "base/test/test_timeouts.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "content/browser/browser_plugin/browser_plugin_guest.h" | 9 #include "content/browser/browser_plugin/browser_plugin_guest.h" |
10 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" | 10 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
25 #include "net/test/test_server.h" | 25 #include "net/test/test_server.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
27 | 27 |
28 using WebKit::WebInputEvent; | 28 using WebKit::WebInputEvent; |
29 using WebKit::WebMouseEvent; | 29 using WebKit::WebMouseEvent; |
30 using content::BrowserPluginEmbedder; | 30 using content::BrowserPluginEmbedder; |
31 using content::BrowserPluginGuest; | 31 using content::BrowserPluginGuest; |
32 using content::BrowserPluginHostFactory; | 32 using content::BrowserPluginHostFactory; |
33 | 33 |
34 namespace content { | 34 namespace { |
35 | 35 |
36 const char* kHTMLForGuest = | 36 const char* kHTMLForGuest = |
37 "data:text/html,<html><body>hello world</body></html>"; | 37 "data:text/html,<html><body>hello world</body></html>"; |
38 const char* kHTMLForGuestInfiniteLoop = | 38 const char* kHTMLForGuestInfiniteLoop = |
39 "data:text/html,<html><head><script type=\"text/javascript\">" | 39 "data:text/html,<html><head><script type=\"text/javascript\">" |
40 "function StartInfiniteLoop() {" | 40 "function StartInfiniteLoop() {" |
41 " setTimeout(function () {while (true) {} }, 0);" | 41 " setTimeout(function () {while (true) {} }, 0);" |
42 "}" | 42 "}" |
43 "</script></head><body></body></html>"; | 43 "</script></head><body></body></html>"; |
44 const char kHTMLForGuestTouchHandler[] = | 44 const char kHTMLForGuestTouchHandler[] = |
45 "data:text/html,<html><body><div id=\"touch\">With touch</div></body>" | 45 "data:text/html,<html><body><div id=\"touch\">With touch</div></body>" |
46 "<script type=\"text/javascript\">" | 46 "<script type=\"text/javascript\">" |
47 "function handler() {}" | 47 "function handler() {}" |
48 "function InstallTouchHandler() { " | 48 "function InstallTouchHandler() { " |
49 " document.getElementById(\"touch\").addEventListener(\"touchstart\", " | 49 " document.getElementById(\"touch\").addEventListener(\"touchstart\", " |
50 " handler);" | 50 " handler);" |
51 "}" | 51 "}" |
52 "function UninstallTouchHandler() { " | 52 "function UninstallTouchHandler() { " |
53 " document.getElementById(\"touch\").removeEventListener(\"touchstart\", " | 53 " document.getElementById(\"touch\").removeEventListener(\"touchstart\", " |
54 " handler);" | 54 " handler);" |
55 "}" | 55 "}" |
56 "</script></html>"; | 56 "</script></html>"; |
| 57 const char* kHTMLForGuestWithTitle = |
| 58 "data:text/html," |
| 59 "<html><head><title>%s</title></head>" |
| 60 "<body>hello world</body>" |
| 61 "</html>"; |
| 62 |
| 63 std::string GetHTMLForGuestWithTitle(const std::string& title) { |
| 64 return StringPrintf(kHTMLForGuestWithTitle, title.c_str()); |
| 65 } |
| 66 |
| 67 } // namespace |
| 68 |
| 69 namespace content { |
57 | 70 |
58 // Test factory for creating test instances of BrowserPluginEmbedder and | 71 // Test factory for creating test instances of BrowserPluginEmbedder and |
59 // BrowserPluginGuest. | 72 // BrowserPluginGuest. |
60 class TestBrowserPluginHostFactory : public BrowserPluginHostFactory { | 73 class TestBrowserPluginHostFactory : public BrowserPluginHostFactory { |
61 public: | 74 public: |
62 virtual BrowserPluginGuest* CreateBrowserPluginGuest( | 75 virtual BrowserPluginGuest* CreateBrowserPluginGuest( |
63 int instance_id, | 76 int instance_id, |
64 WebContentsImpl* web_contents, | 77 WebContentsImpl* web_contents, |
65 RenderViewHost* render_view_host) OVERRIDE { | 78 RenderViewHost* render_view_host) OVERRIDE { |
66 return new TestBrowserPluginGuest(instance_id, | 79 return new TestBrowserPluginGuest(instance_id, |
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 | 691 |
679 // Uninstalling the touch-handler in guest should cause the embedder to stop | 692 // Uninstalling the touch-handler in guest should cause the embedder to stop |
680 // listening for touch events. | 693 // listening for touch events. |
681 observer.ResetState(); | 694 observer.ResetState(); |
682 test_guest_web_contents->GetRenderViewHost()->ExecuteJavascriptAndGetValue( | 695 test_guest_web_contents->GetRenderViewHost()->ExecuteJavascriptAndGetValue( |
683 string16(), ASCIIToUTF16("UninstallTouchHandler();")); | 696 string16(), ASCIIToUTF16("UninstallTouchHandler();")); |
684 observer.WaitUntilMessageReceived(); | 697 observer.WaitUntilMessageReceived(); |
685 EXPECT_FALSE(rvh->has_touch_handler()); | 698 EXPECT_FALSE(rvh->has_touch_handler()); |
686 } | 699 } |
687 | 700 |
| 701 IN_PROC_BROWSER_TEST_F(BrowserPluginHostTest, Renavigate) { |
| 702 ASSERT_TRUE(test_server()->Start()); |
| 703 GURL test_url(test_server()->GetURL( |
| 704 "files/browser_plugin_embedder.html")); |
| 705 NavigateToURL(shell(), test_url); |
| 706 |
| 707 WebContentsImpl* embedder_web_contents = static_cast<WebContentsImpl*>( |
| 708 shell()->web_contents()); |
| 709 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( |
| 710 embedder_web_contents->GetRenderViewHost()); |
| 711 |
| 712 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16( |
| 713 StringPrintf("SetSrc('%s');", GetHTMLForGuestWithTitle("P1").c_str()))); |
| 714 |
| 715 // Wait to make sure embedder is created/attached to WebContents. |
| 716 TestBrowserPluginHostFactory::GetInstance()->WaitForEmbedderCreation(); |
| 717 |
| 718 TestBrowserPluginEmbedder* test_embedder = |
| 719 static_cast<TestBrowserPluginEmbedder*>( |
| 720 embedder_web_contents->GetBrowserPluginEmbedder()); |
| 721 ASSERT_TRUE(test_embedder); |
| 722 test_embedder->WaitForGuestAdded(); |
| 723 |
| 724 // Verify that we have exactly one guest. |
| 725 const BrowserPluginEmbedder::ContainerInstanceMap& instance_map = |
| 726 test_embedder->guest_web_contents_for_testing(); |
| 727 EXPECT_EQ(1u, instance_map.size()); |
| 728 |
| 729 WebContentsImpl* test_guest_web_contents = static_cast<WebContentsImpl*>( |
| 730 instance_map.begin()->second); |
| 731 TestBrowserPluginGuest* test_guest = static_cast<TestBrowserPluginGuest*>( |
| 732 test_guest_web_contents->GetBrowserPluginGuest()); |
| 733 |
| 734 // Wait for the guest to send an UpdateRectMsg, meaning it is ready. |
| 735 test_guest->WaitForUpdateRectMsg(); |
| 736 |
| 737 // Navigate to P2 and verify that the navigation occurred. |
| 738 { |
| 739 const string16 expected_title = ASCIIToUTF16("P2"); |
| 740 content::TitleWatcher title_watcher(test_guest_web_contents, |
| 741 expected_title); |
| 742 |
| 743 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16( |
| 744 StringPrintf("SetSrc('%s');", GetHTMLForGuestWithTitle("P2").c_str()))); |
| 745 |
| 746 string16 actual_title = title_watcher.WaitAndGetTitle(); |
| 747 EXPECT_EQ(expected_title, actual_title); |
| 748 } |
| 749 |
| 750 // Navigate to P3 and verify that the navigation occurred. |
| 751 { |
| 752 const string16 expected_title = ASCIIToUTF16("P3"); |
| 753 content::TitleWatcher title_watcher(test_guest_web_contents, |
| 754 expected_title); |
| 755 |
| 756 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16( |
| 757 StringPrintf("SetSrc('%s');", GetHTMLForGuestWithTitle("P3").c_str()))); |
| 758 |
| 759 string16 actual_title = title_watcher.WaitAndGetTitle(); |
| 760 EXPECT_EQ(expected_title, actual_title); |
| 761 } |
| 762 |
| 763 // Go back and verify that we're back at P2. |
| 764 { |
| 765 const string16 expected_title = ASCIIToUTF16("P2"); |
| 766 content::TitleWatcher title_watcher(test_guest_web_contents, |
| 767 expected_title); |
| 768 |
| 769 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16("Back();")); |
| 770 |
| 771 string16 actual_title = title_watcher.WaitAndGetTitle(); |
| 772 EXPECT_EQ(expected_title, actual_title); |
| 773 } |
| 774 |
| 775 // Go forward and verify that we're back at P3. |
| 776 { |
| 777 const string16 expected_title = ASCIIToUTF16("P3"); |
| 778 content::TitleWatcher title_watcher(test_guest_web_contents, |
| 779 expected_title); |
| 780 |
| 781 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16("Forward();")); |
| 782 |
| 783 string16 actual_title = title_watcher.WaitAndGetTitle(); |
| 784 EXPECT_EQ(expected_title, actual_title); |
| 785 } |
| 786 |
| 787 // Go back two entries and verify that we're back at P1. |
| 788 { |
| 789 const string16 expected_title = ASCIIToUTF16("P1"); |
| 790 content::TitleWatcher title_watcher(test_guest_web_contents, |
| 791 expected_title); |
| 792 |
| 793 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16("Go(-2);")); |
| 794 |
| 795 string16 actual_title = title_watcher.WaitAndGetTitle(); |
| 796 EXPECT_EQ(expected_title, actual_title); |
| 797 } |
| 798 } |
| 799 |
| 800 |
688 } // namespace content | 801 } // namespace content |
OLD | NEW |