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 "base/thread_task_runner_handle.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/render_messages.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "chrome/renderer/chrome_content_renderer_client.h"
17 #include "chrome/renderer/plugins/shadow_dom_plugin_placeholder.h"
18 #include "chrome/test/base/chrome_render_view_test.h"
19 #include "content/public/common/content_constants.h"
20 #include "content/public/renderer/render_frame.h"
21 #include "content/public/renderer/render_view.h"
22 #include "content/public/test/mock_render_thread.h"
23 #include "ipc/ipc_listener.h"
24 #include "ipc/ipc_sender.h"
25 #include "ipc/ipc_test_sink.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "third_party/WebKit/public/web/WebLocalFrame.h"
28 #include "third_party/WebKit/public/web/WebPluginParams.h"
29 #include "ui/base/l10n/l10n_util.h"
33 using testing::SetArgPointee
;
35 typedef ChromeRenderViewTest InstantProcessNavigationTest
;
37 // Tests that renderer-initiated navigations from an Instant render process get
38 // bounced back to the browser to be rebucketed into a non-Instant renderer if
40 TEST_F(InstantProcessNavigationTest
, ForkForNavigationsFromInstantProcess
) {
41 base::CommandLine::ForCurrentProcess()->AppendSwitch(
42 switches::kInstantProcess
);
44 ChromeContentRendererClient
* client
=
45 static_cast<ChromeContentRendererClient
*>(content_renderer_client_
.get());
46 EXPECT_TRUE(client
->ShouldFork(
47 GetMainFrame(), GURL("http://foo"), "GET", false, false, &unused
));
50 // Tests that renderer-initiated navigations from a non-Instant render process
51 // to potentially Instant URLs get bounced back to the browser to be rebucketed
52 // into an Instant renderer if necessary.
53 TEST_F(InstantProcessNavigationTest
, ForkForNavigationsToSearchURLs
) {
54 ChromeContentRendererClient
* client
=
55 static_cast<ChromeContentRendererClient
*>(content_renderer_client_
.get());
56 chrome_render_thread_
->set_io_message_loop_proxy(
57 base::ThreadTaskRunnerHandle::Get());
58 client
->RenderThreadStarted();
59 std::vector
<GURL
> search_urls
;
60 search_urls
.push_back(GURL("http://example.com/search"));
61 chrome_render_thread_
->Send(new ChromeViewMsg_SetSearchURLs(
62 search_urls
, GURL("http://example.com/newtab")));
64 EXPECT_TRUE(client
->ShouldFork(
65 GetMainFrame(), GURL("http://example.com/newtab"), "GET", false, false,
67 EXPECT_TRUE(client
->ShouldFork(
68 GetMainFrame(), GURL("http://example.com/search?q=foo"), "GET", false,
70 EXPECT_FALSE(client
->ShouldFork(
71 GetMainFrame(), GURL("http://example.com/"), "GET", false, false,
77 // Intercepts plugin info IPCs for a mock render thread within its scope,
78 // and allows tests to mock the response to each request.
79 class ScopedMockPluginInfoFilter
: public IPC::Listener
, public IPC::Sender
{
81 explicit ScopedMockPluginInfoFilter(
82 content::MockRenderThread
* mock_render_thread
)
83 : sink_(mock_render_thread
->sink()), sender_(mock_render_thread
) {
84 sink_
.AddFilter(this);
86 ~ScopedMockPluginInfoFilter() override
{ sink_
.RemoveFilter(this); }
88 bool OnMessageReceived(const IPC::Message
& message
) override
{
89 IPC_BEGIN_MESSAGE_MAP(ScopedMockPluginInfoFilter
, message
)
90 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_GetPluginInfo
, OnGetPluginInfo
)
91 IPC_MESSAGE_UNHANDLED(return false)
96 bool Send(IPC::Message
* message
) override
{ return sender_
->Send(message
); }
98 MOCK_METHOD5(OnGetPluginInfo
,
99 void(int render_frame_id
,
101 const GURL
& top_origin_url
,
102 const std::string
& mime_type
,
103 ChromeViewHostMsg_GetPluginInfo_Output
* output
));
106 IPC::TestSink
& sink_
;
107 IPC::Sender
* sender_
;
108 DISALLOW_COPY_AND_ASSIGN(ScopedMockPluginInfoFilter
);
113 class CreatePluginPlaceholderTest
: public ChromeRenderViewTest
{
115 void SetUp() override
{
116 ChromeRenderViewTest::SetUp();
117 base::CommandLine::ForCurrentProcess()->AppendSwitch(
118 switches::kEnablePluginPlaceholderShadowDom
);
121 content::RenderFrame
* GetMainRenderFrame() const {
122 return view_
->GetMainRenderFrame();
125 int GetRoutingID() const { return GetMainRenderFrame()->GetRoutingID(); }
128 TEST_F(CreatePluginPlaceholderTest
, MissingPlugin
) {
129 GURL
url("http://www.example.com/example.swf");
130 std::string
mime_type("application/x-shockwave-flash");
132 blink::WebPluginParams params
;
134 params
.mimeType
= base::ASCIIToUTF16(mime_type
);
136 ChromeViewHostMsg_GetPluginInfo_Output output
;
137 output
.status
= ChromeViewHostMsg_GetPluginInfo_Status::kNotFound
;
139 ScopedMockPluginInfoFilter
filter(render_thread_
.get());
140 #if defined(ENABLE_PLUGINS)
141 EXPECT_CALL(filter
, OnGetPluginInfo(GetRoutingID(), url
, _
, mime_type
, _
))
142 .WillOnce(SetArgPointee
<4>(output
));
145 scoped_ptr
<blink::WebPluginPlaceholder
> placeholder
=
146 content_renderer_client_
->CreatePluginPlaceholder(
147 GetMainRenderFrame(), GetMainFrame(), params
);
148 ASSERT_NE(nullptr, placeholder
);
149 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_SUPPORTED
),
150 placeholder
->message());
153 TEST_F(CreatePluginPlaceholderTest
, PluginFound
) {
154 GURL
url("http://www.example.com/example.swf");
155 std::string
mime_type(content::kFlashPluginSwfMimeType
);
157 blink::WebPluginParams params
;
159 params
.mimeType
= base::ASCIIToUTF16(mime_type
);
161 ChromeViewHostMsg_GetPluginInfo_Output output
;
162 output
.status
= ChromeViewHostMsg_GetPluginInfo_Status::kAllowed
;
164 ScopedMockPluginInfoFilter
filter(render_thread_
.get());
165 #if defined(ENABLE_PLUGINS)
166 EXPECT_CALL(filter
, OnGetPluginInfo(GetRoutingID(), url
, _
, mime_type
, _
))
167 .WillOnce(SetArgPointee
<4>(output
));
170 scoped_ptr
<blink::WebPluginPlaceholder
> placeholder
=
171 content_renderer_client_
->CreatePluginPlaceholder(
172 GetMainRenderFrame(), GetMainFrame(), params
);
173 EXPECT_EQ(nullptr, placeholder
);