Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / render_frame_impl_browsertest.cc
blob7aeb278f97dfd2acde2f57d9d4a35694390d0c64
1 // Copyright 2015 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 "base/command_line.h"
6 #include "content/common/frame_messages.h"
7 #include "content/common/view_messages.h"
8 #include "content/public/common/content_switches.h"
9 #include "content/public/test/frame_load_waiter.h"
10 #include "content/public/test/render_view_test.h"
11 #include "content/renderer/render_frame_impl.h"
12 #include "content/renderer/render_view_impl.h"
13 #include "content/test/fake_compositor_dependencies.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/public/platform/WebURLRequest.h"
16 #include "third_party/WebKit/public/web/WebLocalFrame.h"
18 namespace {
19 const int32 kSubframeRouteId = 20;
20 const int32 kSubframeWidgetRouteId = 21;
21 const int32 kFrameProxyRouteId = 22;
22 const int32 kSubframeSurfaceId = 43;
23 } // namespace
25 namespace content {
27 // RenderFrameImplTest creates a RenderFrameImpl that is a child of the
28 // main frame, and has its own RenderWidget. This behaves like an out
29 // of process frame even though it is in the same process as its parent.
30 class RenderFrameImplTest : public RenderViewTest {
31 public:
32 ~RenderFrameImplTest() override {}
34 void SetUp() override {
35 RenderViewTest::SetUp();
37 FrameMsg_NewFrame_WidgetParams widget_params;
38 widget_params.routing_id = kSubframeWidgetRouteId;
39 widget_params.surface_id = kSubframeSurfaceId;
40 widget_params.hidden = false;
42 compositor_deps_.reset(new FakeCompositorDependencies);
44 base::CommandLine::ForCurrentProcess()->AppendSwitch(
45 switches::kSitePerProcess);
47 LoadHTML("Parent frame <iframe name='frame'></iframe>");
49 FrameReplicationState replicationState("frame");
51 RenderFrameImpl::FromWebFrame(
52 view_->GetMainRenderFrame()->GetWebFrame()->firstChild())
53 ->OnSwapOut(kFrameProxyRouteId, false, FrameReplicationState());
55 RenderFrameImpl::CreateFrame(kSubframeRouteId, kFrameProxyRouteId,
56 MSG_ROUTING_NONE, FrameReplicationState(),
57 compositor_deps_.get(), widget_params);
59 frame_ = RenderFrameImpl::FromRoutingID(kSubframeRouteId);
62 // Loads the given HTML into the frame as a data: URL and blocks until
63 // the navigation is committed.
64 void LoadHTMLInFrame(const char* html) {
65 std::string url_str = "data:text/html;charset=utf-8,";
66 url_str.append(html);
67 GURL url(url_str);
68 frame_->GetWebFrame()->loadRequest(blink::WebURLRequest(url));
69 // The load actually happens asynchronously, so we pump messages to process
70 // the pending continuation.
71 FrameLoadWaiter(frame_).Wait();
74 RenderFrameImpl* frame() { return frame_; }
76 content::RenderWidget* FrameWidget() { return frame_->render_widget_.get(); }
78 private:
79 RenderFrameImpl* frame_;
80 scoped_ptr<CompositorDependencies> compositor_deps_;
83 class RenderFrameTestObserver : public RenderFrameObserver {
84 public:
85 RenderFrameTestObserver(RenderFrame* render_frame)
86 : RenderFrameObserver(render_frame), visible_(false) {}
88 ~RenderFrameTestObserver() override {}
90 void WasShown() override { visible_ = true; }
91 void WasHidden() override { visible_ = false; }
93 bool visible() { return visible_; }
95 private:
96 bool visible_;
99 #if defined(OS_ANDROID)
100 // See https://crbug.com/472717
101 #define MAYBE_SubframeWidget DISABLED_SubframeWidget
102 #define MAYBE_FrameResize DISABLED_FrameResize
103 #define MAYBE_FrameWasShown DISABLED_FrameWasShown
104 #else
105 #define MAYBE_SubframeWidget SubframeWidget
106 #define MAYBE_FrameResize FrameResize
107 #define MAYBE_FrameWasShown FrameWasShown
108 #endif
110 // Verify that a frame with a RenderFrameProxy as a parent has its own
111 // RenderWidget.
112 TEST_F(RenderFrameImplTest, MAYBE_SubframeWidget) {
113 EXPECT_TRUE(FrameWidget());
114 EXPECT_NE(FrameWidget(), (content::RenderWidget*)view_);
117 // Verify a subframe RenderWidget properly processes its viewport being
118 // resized.
119 TEST_F(RenderFrameImplTest, MAYBE_FrameResize) {
120 ViewMsg_Resize_Params resize_params;
121 gfx::Size size(200, 200);
122 resize_params.screen_info = blink::WebScreenInfo();
123 resize_params.new_size = size;
124 resize_params.physical_backing_size = size;
125 resize_params.top_controls_height = 0.f;
126 resize_params.top_controls_shrink_blink_size = false;
127 resize_params.resizer_rect = gfx::Rect();
128 resize_params.is_fullscreen = false;
130 scoped_ptr<IPC::Message> resize_message(new ViewMsg_Resize(0, resize_params));
131 FrameWidget()->OnMessageReceived(*resize_message);
133 EXPECT_EQ(FrameWidget()->webwidget()->size(), blink::WebSize(size));
136 // Verify a subframe RenderWidget properly processes a WasShown message.
137 TEST_F(RenderFrameImplTest, MAYBE_FrameWasShown) {
138 RenderFrameTestObserver observer(frame());
140 scoped_ptr<IPC::Message> was_shown_message(
141 new ViewMsg_WasShown(0, true, ui::LatencyInfo()));
142 FrameWidget()->OnMessageReceived(*was_shown_message);
144 EXPECT_FALSE(FrameWidget()->is_hidden());
145 EXPECT_TRUE(observer.visible());
148 } // namespace