Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / renderer / chrome_content_renderer_client_browsertest.cc
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.
4
5 #include "chrome/renderer/chrome_content_renderer_client.h"
6
7 #include <string>
8 #include <vector>
9
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"
30
31 using testing::_;
32 using testing::SetArgPointee;
33
34 typedef ChromeRenderViewTest InstantProcessNavigationTest;
35
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   CommandLine::ForCurrentProcess()->AppendSwitch(switches::kInstantProcess);
41   bool unused;
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));
46 }
47
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")));
61   bool unused;
62   EXPECT_TRUE(client->ShouldFork(
63       GetMainFrame(), GURL("http://example.com/newtab"), "GET", false, false,
64       &unused));
65   EXPECT_TRUE(client->ShouldFork(
66       GetMainFrame(), GURL("http://example.com/search?q=foo"), "GET", false,
67       false, &unused));
68   EXPECT_FALSE(client->ShouldFork(
69       GetMainFrame(), GURL("http://example.com/"), "GET", false, false,
70       &unused));
71 }
72
73 namespace {
74
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 {
78  public:
79   explicit ScopedMockPluginInfoFilter(
80       content::MockRenderThread* mock_render_thread)
81       : sink_(mock_render_thread->sink()), sender_(mock_render_thread) {
82     sink_.AddFilter(this);
83   }
84   ~ScopedMockPluginInfoFilter() override { sink_.RemoveFilter(this); }
85
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)
90     IPC_END_MESSAGE_MAP()
91     return true;
92   }
93
94   bool Send(IPC::Message* message) override { return sender_->Send(message); }
95
96   MOCK_METHOD5(OnGetPluginInfo,
97                void(int render_frame_id,
98                     const GURL& url,
99                     const GURL& top_origin_url,
100                     const std::string& mime_type,
101                     ChromeViewHostMsg_GetPluginInfo_Output* output));
102
103  private:
104   IPC::TestSink& sink_;
105   IPC::Sender* sender_;
106   DISALLOW_COPY_AND_ASSIGN(ScopedMockPluginInfoFilter);
107 };
108
109 }  // namespace
110
111 class CreatePluginPlaceholderTest : public ChromeRenderViewTest {
112  protected:
113   void SetUp() override {
114     ChromeRenderViewTest::SetUp();
115     base::CommandLine::ForCurrentProcess()->AppendSwitch(
116         switches::kEnablePluginPlaceholderShadowDom);
117   }
118
119   content::RenderFrame* GetMainRenderFrame() const {
120     return view_->GetMainRenderFrame();
121   }
122
123   int GetRoutingID() const { return GetMainRenderFrame()->GetRoutingID(); }
124 };
125
126 TEST_F(CreatePluginPlaceholderTest, MissingPlugin) {
127   GURL url("http://www.example.com/example.swf");
128   std::string mime_type("application/x-shockwave-flash");
129
130   blink::WebPluginParams params;
131   params.url = url;
132   params.mimeType = base::ASCIIToUTF16(mime_type);
133
134   ChromeViewHostMsg_GetPluginInfo_Output output;
135   output.status.value = ChromeViewHostMsg_GetPluginInfo_Status::kNotFound;
136
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));
141 #endif
142
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());
149 }
150
151 TEST_F(CreatePluginPlaceholderTest, PluginFound) {
152   GURL url("http://www.example.com/example.swf");
153   std::string mime_type(content::kFlashPluginSwfMimeType);
154
155   blink::WebPluginParams params;
156   params.url = url;
157   params.mimeType = base::ASCIIToUTF16(mime_type);
158
159   ChromeViewHostMsg_GetPluginInfo_Output output;
160   output.status.value = ChromeViewHostMsg_GetPluginInfo_Status::kAllowed;
161
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));
166 #endif
167
168   scoped_ptr<blink::WebPluginPlaceholder> placeholder =
169       content_renderer_client_->CreatePluginPlaceholder(
170           GetMainRenderFrame(), GetMainFrame(), params);
171   EXPECT_EQ(nullptr, placeholder);
172 }