Do not define POSIX.
[chromium-blink-merge.git] / chrome / renderer / chrome_content_renderer_client_browsertest.cc
blob14b25267c4d26a149fd2b506a735c784963912ad
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"
7 #include <string>
8 #include <vector>
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"
29 #include "url/gurl.h"
31 using testing::_;
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
38 // necessary.
39 TEST_F(InstantProcessNavigationTest, ForkForNavigationsFromInstantProcess) {
40 base::CommandLine::ForCurrentProcess()->AppendSwitch(
41 switches::kInstantProcess);
42 bool unused;
43 ChromeContentRendererClient* client =
44 static_cast<ChromeContentRendererClient*>(content_renderer_client_.get());
45 EXPECT_TRUE(client->ShouldFork(
46 GetMainFrame(), GURL("http://foo"), "GET", false, false, &unused));
49 // Tests that renderer-initiated navigations from a non-Instant render process
50 // to potentially Instant URLs get bounced back to the browser to be rebucketed
51 // into an Instant renderer if necessary.
52 TEST_F(InstantProcessNavigationTest, ForkForNavigationsToSearchURLs) {
53 ChromeContentRendererClient* client =
54 static_cast<ChromeContentRendererClient*>(content_renderer_client_.get());
55 chrome_render_thread_->set_io_message_loop_proxy(
56 base::MessageLoopProxy::current());
57 client->RenderThreadStarted();
58 std::vector<GURL> search_urls;
59 search_urls.push_back(GURL("http://example.com/search"));
60 chrome_render_thread_->Send(new ChromeViewMsg_SetSearchURLs(
61 search_urls, GURL("http://example.com/newtab")));
62 bool unused;
63 EXPECT_TRUE(client->ShouldFork(
64 GetMainFrame(), GURL("http://example.com/newtab"), "GET", false, false,
65 &unused));
66 EXPECT_TRUE(client->ShouldFork(
67 GetMainFrame(), GURL("http://example.com/search?q=foo"), "GET", false,
68 false, &unused));
69 EXPECT_FALSE(client->ShouldFork(
70 GetMainFrame(), GURL("http://example.com/"), "GET", false, false,
71 &unused));
74 namespace {
76 // Intercepts plugin info IPCs for a mock render thread within its scope,
77 // and allows tests to mock the response to each request.
78 class ScopedMockPluginInfoFilter : public IPC::Listener, public IPC::Sender {
79 public:
80 explicit ScopedMockPluginInfoFilter(
81 content::MockRenderThread* mock_render_thread)
82 : sink_(mock_render_thread->sink()), sender_(mock_render_thread) {
83 sink_.AddFilter(this);
85 ~ScopedMockPluginInfoFilter() override { sink_.RemoveFilter(this); }
87 bool OnMessageReceived(const IPC::Message& message) override {
88 IPC_BEGIN_MESSAGE_MAP(ScopedMockPluginInfoFilter, message)
89 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_GetPluginInfo, OnGetPluginInfo)
90 IPC_MESSAGE_UNHANDLED(return false)
91 IPC_END_MESSAGE_MAP()
92 return true;
95 bool Send(IPC::Message* message) override { return sender_->Send(message); }
97 MOCK_METHOD5(OnGetPluginInfo,
98 void(int render_frame_id,
99 const GURL& url,
100 const GURL& top_origin_url,
101 const std::string& mime_type,
102 ChromeViewHostMsg_GetPluginInfo_Output* output));
104 private:
105 IPC::TestSink& sink_;
106 IPC::Sender* sender_;
107 DISALLOW_COPY_AND_ASSIGN(ScopedMockPluginInfoFilter);
110 } // namespace
112 class CreatePluginPlaceholderTest : public ChromeRenderViewTest {
113 protected:
114 void SetUp() override {
115 ChromeRenderViewTest::SetUp();
116 base::CommandLine::ForCurrentProcess()->AppendSwitch(
117 switches::kEnablePluginPlaceholderShadowDom);
120 content::RenderFrame* GetMainRenderFrame() const {
121 return view_->GetMainRenderFrame();
124 int GetRoutingID() const { return GetMainRenderFrame()->GetRoutingID(); }
127 TEST_F(CreatePluginPlaceholderTest, MissingPlugin) {
128 GURL url("http://www.example.com/example.swf");
129 std::string mime_type("application/x-shockwave-flash");
131 blink::WebPluginParams params;
132 params.url = url;
133 params.mimeType = base::ASCIIToUTF16(mime_type);
135 ChromeViewHostMsg_GetPluginInfo_Output output;
136 output.status = ChromeViewHostMsg_GetPluginInfo_Status::kNotFound;
138 ScopedMockPluginInfoFilter filter(render_thread_.get());
139 #if defined(ENABLE_PLUGINS)
140 EXPECT_CALL(filter, OnGetPluginInfo(GetRoutingID(), url, _, mime_type, _))
141 .WillOnce(SetArgPointee<4>(output));
142 #endif
144 scoped_ptr<blink::WebPluginPlaceholder> placeholder =
145 content_renderer_client_->CreatePluginPlaceholder(
146 GetMainRenderFrame(), GetMainFrame(), params);
147 ASSERT_NE(nullptr, placeholder);
148 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_SUPPORTED),
149 placeholder->message());
152 TEST_F(CreatePluginPlaceholderTest, PluginFound) {
153 GURL url("http://www.example.com/example.swf");
154 std::string mime_type(content::kFlashPluginSwfMimeType);
156 blink::WebPluginParams params;
157 params.url = url;
158 params.mimeType = base::ASCIIToUTF16(mime_type);
160 ChromeViewHostMsg_GetPluginInfo_Output output;
161 output.status = ChromeViewHostMsg_GetPluginInfo_Status::kAllowed;
163 ScopedMockPluginInfoFilter filter(render_thread_.get());
164 #if defined(ENABLE_PLUGINS)
165 EXPECT_CALL(filter, OnGetPluginInfo(GetRoutingID(), url, _, mime_type, _))
166 .WillOnce(SetArgPointee<4>(output));
167 #endif
169 scoped_ptr<blink::WebPluginPlaceholder> placeholder =
170 content_renderer_client_->CreatePluginPlaceholder(
171 GetMainRenderFrame(), GetMainFrame(), params);
172 EXPECT_EQ(nullptr, placeholder);