1 // Copyright 2013 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.
5 #include "chrome/renderer/chrome_content_renderer_client.h"
10 #include "base/command_line.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/render_messages.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "chrome/renderer/chrome_content_renderer_client.h"
16 #include "chrome/renderer/plugins/shadow_dom_plugin_placeholder.h"
17 #include "chrome/test/base/chrome_render_view_test.h"
18 #include "content/public/common/content_constants.h"
19 #include "content/public/renderer/render_frame.h"
20 #include "content/public/renderer/render_view.h"
21 #include "content/public/test/mock_render_thread.h"
22 #include "ipc/ipc_listener.h"
23 #include "ipc/ipc_sender.h"
24 #include "ipc/ipc_test_sink.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "third_party/WebKit/public/web/WebLocalFrame.h"
27 #include "third_party/WebKit/public/web/WebPluginParams.h"
28 #include "ui/base/l10n/l10n_util.h"
32 using testing::SetArgPointee
;
34 typedef ChromeRenderViewTest InstantProcessNavigationTest
;
36 // Tests that renderer-initiated navigations from an Instant render process get
37 // bounced back to the browser to be rebucketed into a non-Instant renderer if
39 TEST_F(InstantProcessNavigationTest
, ForkForNavigationsFromInstantProcess
) {
40 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kInstantProcess
);
42 ChromeContentRendererClient
* client
=
43 static_cast<ChromeContentRendererClient
*>(content_renderer_client_
.get());
44 EXPECT_TRUE(client
->ShouldFork(
45 GetMainFrame(), GURL("http://foo"), "GET", false, false, &unused
));
48 // Tests that renderer-initiated navigations from a non-Instant render process
49 // to potentially Instant URLs get bounced back to the browser to be rebucketed
50 // into an Instant renderer if necessary.
51 TEST_F(InstantProcessNavigationTest
, ForkForNavigationsToSearchURLs
) {
52 ChromeContentRendererClient
* client
=
53 static_cast<ChromeContentRendererClient
*>(content_renderer_client_
.get());
54 chrome_render_thread_
->set_io_message_loop_proxy(
55 base::MessageLoopProxy::current());
56 client
->RenderThreadStarted();
57 std::vector
<GURL
> search_urls
;
58 search_urls
.push_back(GURL("http://example.com/search"));
59 chrome_render_thread_
->Send(new ChromeViewMsg_SetSearchURLs(
60 search_urls
, GURL("http://example.com/newtab")));
62 EXPECT_TRUE(client
->ShouldFork(
63 GetMainFrame(), GURL("http://example.com/newtab"), "GET", false, false,
65 EXPECT_TRUE(client
->ShouldFork(
66 GetMainFrame(), GURL("http://example.com/search?q=foo"), "GET", false,
68 EXPECT_FALSE(client
->ShouldFork(
69 GetMainFrame(), GURL("http://example.com/"), "GET", false, false,
75 // Intercepts plugin info IPCs for a mock render thread within its scope,
76 // and allows tests to mock the response to each request.
77 class ScopedMockPluginInfoFilter
: public IPC::Listener
, public IPC::Sender
{
79 explicit ScopedMockPluginInfoFilter(
80 content::MockRenderThread
* mock_render_thread
)
81 : sink_(mock_render_thread
->sink()), sender_(mock_render_thread
) {
82 sink_
.AddFilter(this);
84 ~ScopedMockPluginInfoFilter() override
{ sink_
.RemoveFilter(this); }
86 bool OnMessageReceived(const IPC::Message
& message
) override
{
87 IPC_BEGIN_MESSAGE_MAP(ScopedMockPluginInfoFilter
, message
)
88 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_GetPluginInfo
, OnGetPluginInfo
)
89 IPC_MESSAGE_UNHANDLED(return false)
94 bool Send(IPC::Message
* message
) override
{ return sender_
->Send(message
); }
96 MOCK_METHOD5(OnGetPluginInfo
,
97 void(int render_frame_id
,
99 const GURL
& top_origin_url
,
100 const std::string
& mime_type
,
101 ChromeViewHostMsg_GetPluginInfo_Output
* output
));
104 IPC::TestSink
& sink_
;
105 IPC::Sender
* sender_
;
106 DISALLOW_COPY_AND_ASSIGN(ScopedMockPluginInfoFilter
);
111 class CreatePluginPlaceholderTest
: public ChromeRenderViewTest
{
113 void SetUp() override
{
114 ChromeRenderViewTest::SetUp();
115 base::CommandLine::ForCurrentProcess()->AppendSwitch(
116 switches::kEnablePluginPlaceholderShadowDom
);
119 content::RenderFrame
* GetMainRenderFrame() const {
120 return view_
->GetMainRenderFrame();
123 int GetRoutingID() const { return GetMainRenderFrame()->GetRoutingID(); }
126 TEST_F(CreatePluginPlaceholderTest
, MissingPlugin
) {
127 GURL
url("http://www.example.com/example.swf");
128 std::string
mime_type("application/x-shockwave-flash");
130 blink::WebPluginParams params
;
132 params
.mimeType
= base::ASCIIToUTF16(mime_type
);
134 ChromeViewHostMsg_GetPluginInfo_Output output
;
135 output
.status
.value
= ChromeViewHostMsg_GetPluginInfo_Status::kNotFound
;
137 ScopedMockPluginInfoFilter
filter(render_thread_
.get());
138 #if defined(ENABLE_PLUGINS)
139 EXPECT_CALL(filter
, OnGetPluginInfo(GetRoutingID(), url
, _
, mime_type
, _
))
140 .WillOnce(SetArgPointee
<4>(output
));
143 scoped_ptr
<blink::WebPluginPlaceholder
> placeholder
=
144 content_renderer_client_
->CreatePluginPlaceholder(
145 GetMainRenderFrame(), GetMainFrame(), params
);
146 ASSERT_NE(nullptr, placeholder
);
147 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_SUPPORTED
),
148 placeholder
->message());
151 TEST_F(CreatePluginPlaceholderTest
, PluginFound
) {
152 GURL
url("http://www.example.com/example.swf");
153 std::string
mime_type(content::kFlashPluginSwfMimeType
);
155 blink::WebPluginParams params
;
157 params
.mimeType
= base::ASCIIToUTF16(mime_type
);
159 ChromeViewHostMsg_GetPluginInfo_Output output
;
160 output
.status
.value
= ChromeViewHostMsg_GetPluginInfo_Status::kAllowed
;
162 ScopedMockPluginInfoFilter
filter(render_thread_
.get());
163 #if defined(ENABLE_PLUGINS)
164 EXPECT_CALL(filter
, OnGetPluginInfo(GetRoutingID(), url
, _
, mime_type
, _
))
165 .WillOnce(SetArgPointee
<4>(output
));
168 scoped_ptr
<blink::WebPluginPlaceholder
> placeholder
=
169 content_renderer_client_
->CreatePluginPlaceholder(
170 GetMainRenderFrame(), GetMainFrame(), params
);
171 EXPECT_EQ(nullptr, placeholder
);