OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/memory/singleton.h" | |
6 #include "base/run_loop.h" | |
7 #include "base/utf_string_conversions.h" | |
8 #include "content/browser/browser_plugin/browser_plugin_guest.h" | |
9 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" | |
10 #include "content/browser/browser_plugin/test_browser_plugin_embedder.h" | |
11 #include "content/browser/browser_plugin/test_browser_plugin_guest.h" | |
12 #include "content/browser/renderer_host/render_view_host_impl.h" | |
13 #include "content/browser/web_contents/web_contents_impl.h" | |
14 #include "content/common/view_messages.h" | |
15 #include "content/public/browser/notification_service.h" | |
16 #include "content/public/browser/notification_types.h" | |
17 #include "content/public/test/test_utils.h" | |
18 #include "content/shell/shell.h" | |
19 #include "content/test/content_browser_test_utils.h" | |
20 #include "content/test/content_browser_test.h" | |
21 #include "ipc/ipc_message.h" | |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | |
23 | |
24 using WebKit::WebInputEvent; | |
25 using WebKit::WebMouseEvent; | |
26 using content::BrowserPluginEmbedder; | |
27 using content::BrowserPluginGuest; | |
28 using content::BrowserPluginHostFactory; | |
29 | |
30 namespace content { | |
31 | |
32 // Test factory for creating test instances of BrowserPluginEmbedder and | |
33 // BrowserPluginGuest. | |
34 class TestBrowserPluginHostFactory : public BrowserPluginHostFactory { | |
35 public: | |
36 virtual BrowserPluginGuest* CreateBrowserPluginGuest( | |
37 int instance_id, | |
38 WebContentsImpl* web_contents, | |
39 RenderViewHost* render_view_host) OVERRIDE { | |
40 return new TestBrowserPluginGuest(instance_id, | |
41 web_contents, | |
42 render_view_host); | |
43 } | |
44 | |
45 // Also keeps track of number of instances created. | |
46 virtual BrowserPluginEmbedder* CreateBrowserPluginEmbedder( | |
47 WebContentsImpl* web_contents, | |
48 RenderViewHost* render_view_host) OVERRIDE { | |
49 embedder_instance_count_++; | |
50 if (message_loop_runner_) | |
51 message_loop_runner_->Quit(); | |
52 | |
53 return new TestBrowserPluginEmbedder(web_contents, render_view_host); | |
54 } | |
55 | |
56 // Singleton getter. | |
57 static TestBrowserPluginHostFactory* GetInstance() { | |
58 return Singleton<TestBrowserPluginHostFactory>::get(); | |
59 } | |
60 | |
61 // Waits for at least one embedder to be created in the test. | |
62 void WaitForEmbedderCreation() { | |
63 // Check if already have created instance. | |
64 if (embedder_instance_count_ > 0) | |
65 return; | |
66 // Wait otherwise. | |
67 message_loop_runner_ = new MessageLoopRunner(); | |
68 message_loop_runner_->Run(); | |
69 } | |
70 | |
71 private: | |
72 // For Singleton. | |
73 friend struct DefaultSingletonTraits<TestBrowserPluginHostFactory>; | |
74 | |
75 TestBrowserPluginHostFactory() : embedder_instance_count_(0) {} | |
76 virtual ~TestBrowserPluginHostFactory() {} | |
77 | |
78 scoped_refptr<MessageLoopRunner> message_loop_runner_; | |
79 int embedder_instance_count_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(TestBrowserPluginHostFactory); | |
82 }; | |
83 | |
84 class BrowserPluginHostTest : public ContentBrowserTest { | |
85 public: | |
86 BrowserPluginHostTest() {} | |
87 | |
88 virtual void SetUp() OVERRIDE { | |
89 // Override factory to create tests instances of BrowserPlugin*. | |
90 content::BrowserPluginEmbedder::set_factory_for_testing( | |
91 TestBrowserPluginHostFactory::GetInstance()); | |
92 content::BrowserPluginGuest::set_factory_for_testing( | |
93 TestBrowserPluginHostFactory::GetInstance()); | |
94 | |
95 ContentBrowserTest::SetUp(); | |
96 } | |
97 virtual void TearDown() OVERRIDE { | |
98 content::BrowserPluginEmbedder::set_factory_for_testing(NULL); | |
99 content::BrowserPluginGuest::set_factory_for_testing(NULL); | |
100 | |
101 ContentBrowserTest::TearDown(); | |
102 } | |
103 }; | |
104 | |
105 // This test loads a guest that has infinite loop, therefore it hangs the guest | |
106 // and eventually gets killed. | |
awong
2012/09/06 19:55:26
Doesn't this guarantee a 5 second wait in the brow
lazyboy
2012/09/07 19:33:19
Unfortunately yes. I will be looking at test_timeo
lazyboy
2012/09/08 02:12:22
I am not totally clear on the point of using a par
| |
107 IN_PROC_BROWSER_TEST_F(BrowserPluginHostTest, NavigateGuest) { | |
108 ASSERT_TRUE(test_server()->Start()); | |
109 GURL test_url(test_server()->GetURL( | |
110 "files/browser_plugin_infinite_loop.html")); | |
111 NavigateToURL(shell(), test_url); | |
112 | |
113 WebContentsImpl* embedder_web_contents = static_cast<WebContentsImpl*>( | |
114 shell()->web_contents()); | |
115 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( | |
116 embedder_web_contents->GetRenderViewHost()); | |
117 | |
118 test_url = test_server()->GetURL( | |
119 "files/browser_plugin_infinite_loop_child.html"); | |
120 rvh->ExecuteJavascriptAndGetValue(string16(), ASCIIToUTF16( | |
121 StringPrintf("SetSrc('%s');", test_url.spec().c_str()))); | |
122 | |
123 // Wait to make sure embedder is created/attached to WebContents. | |
124 TestBrowserPluginHostFactory::GetInstance()->WaitForEmbedderCreation(); | |
125 | |
126 TestBrowserPluginEmbedder* test_embedder = | |
127 static_cast<TestBrowserPluginEmbedder*>( | |
128 embedder_web_contents->GetBrowserPluginEmbedder()); | |
129 ASSERT_TRUE(test_embedder); | |
130 test_embedder->WaitForGuestAdded(); | |
131 | |
132 // Verify that we have exactly one guest. | |
133 const ContainerInstanceMap& instance_map = test_embedder-> | |
134 guests_for_testing(); | |
135 EXPECT_EQ(1u, instance_map.size()); | |
136 | |
137 TestBrowserPluginGuest* test_guest = static_cast<TestBrowserPluginGuest*>( | |
138 instance_map.begin()->second); | |
139 | |
140 // Wait for the guest to send an UpdateRectMsg, meaning it is ready. | |
141 test_guest->WaitForUpdateRectMsg(); | |
142 | |
143 WebKit::WebMouseEvent mouse_event; | |
144 mouse_event.type = WebInputEvent::MouseDown; | |
145 mouse_event.button = WebMouseEvent::ButtonMiddle; | |
146 mouse_event.x = 35; | |
147 mouse_event.y = 35; | |
148 mouse_event.globalX = 35; | |
149 mouse_event.globalY = 35; | |
150 | |
151 IPC::Message* input_message = new ViewMsg_HandleInputEvent( | |
152 embedder_web_contents->GetRenderViewHost()->GetRoutingID()); | |
153 input_message->WriteData(reinterpret_cast<const char*>(&mouse_event), | |
154 sizeof(WebKit::WebMouseEvent)); | |
155 embedder_web_contents->GetRenderViewHost()->Send(input_message); | |
156 | |
157 // Expect the guest to crash. | |
158 test_guest->WaitForCrashed(); | |
159 } | |
160 | |
161 } // namespace content | |
OLD | NEW |