Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / content / browser / frame_host / render_widget_host_view_child_frame_unittest.cc
blob1d344410a41bbdaa61d7c7c3d334581d57feb2fe
1 // Copyright 2014 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 "content/browser/frame_host/render_widget_host_view_child_frame.h"
7 #include "base/basictypes.h"
8 #include "base/message_loop/message_loop.h"
9 #include "cc/surfaces/surface.h"
10 #include "cc/surfaces/surface_factory.h"
11 #include "cc/surfaces/surface_manager.h"
12 #include "cc/surfaces/surface_sequence.h"
13 #include "content/browser/compositor/test/no_transport_image_transport_factory.h"
14 #include "content/browser/frame_host/cross_process_frame_connector.h"
15 #include "content/browser/gpu/compositor_util.h"
16 #include "content/browser/renderer_host/render_widget_host_delegate.h"
17 #include "content/browser/renderer_host/render_widget_host_impl.h"
18 #include "content/common/view_messages.h"
19 #include "content/public/browser/render_widget_host_view.h"
20 #include "content/public/test/mock_render_process_host.h"
21 #include "content/public/test/test_browser_context.h"
22 #include "content/test/test_render_view_host.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 namespace content {
26 namespace {
27 class MockRenderWidgetHostDelegate : public RenderWidgetHostDelegate {
28 public:
29 MockRenderWidgetHostDelegate() {}
30 ~MockRenderWidgetHostDelegate() override {}
31 private:
32 void Cut() override {}
33 void Copy() override {}
34 void Paste() override {}
35 void SelectAll() override {}
38 class MockCrossProcessFrameConnector : public CrossProcessFrameConnector {
39 public:
40 MockCrossProcessFrameConnector()
41 : CrossProcessFrameConnector(nullptr),
42 last_scale_factor_received_(0.f),
43 received_delegated_frame_(false) {}
44 ~MockCrossProcessFrameConnector() override {}
46 void ChildFrameCompositorFrameSwapped(
47 uint32 output_surface_id,
48 int host_id,
49 int route_id,
50 scoped_ptr<cc::CompositorFrame> frame) override {
51 received_delegated_frame_ = true;
52 last_frame_size_received_ =
53 frame->delegated_frame_data->render_pass_list.back()
54 ->output_rect.size();
55 last_scale_factor_received_ = frame->metadata.device_scale_factor;
58 void SetChildFrameSurface(const cc::SurfaceId& surface_id,
59 const gfx::Size& frame_size,
60 float scale_factor,
61 const cc::SurfaceSequence& sequence) override {
62 last_surface_id_received_ = surface_id;
63 last_frame_size_received_ = frame_size;
64 last_scale_factor_received_ = scale_factor;
67 cc::SurfaceId last_surface_id_received_;
68 gfx::Size last_frame_size_received_;
69 float last_scale_factor_received_;
71 bool received_delegated_frame_;
74 } // namespace
76 class RenderWidgetHostViewChildFrameTest : public testing::Test {
77 public:
78 RenderWidgetHostViewChildFrameTest() {}
80 void SetUp() override {
81 browser_context_.reset(new TestBrowserContext);
83 // ImageTransportFactory doesn't exist on Android.
84 #if !defined(OS_ANDROID)
85 ImageTransportFactory::InitializeForUnitTests(
86 scoped_ptr<ImageTransportFactory>(
87 new NoTransportImageTransportFactory));
88 #endif
90 MockRenderProcessHost* process_host =
91 new MockRenderProcessHost(browser_context_.get());
92 widget_host_ = new RenderWidgetHostImpl(
93 &delegate_, process_host, MSG_ROUTING_NONE, false);
94 view_ = new RenderWidgetHostViewChildFrame(widget_host_);
96 test_frame_connector_ = new MockCrossProcessFrameConnector();
97 view_->set_cross_process_frame_connector(test_frame_connector_);
100 void TearDown() override {
101 if (view_)
102 view_->Destroy();
103 delete widget_host_;
104 delete test_frame_connector_;
106 browser_context_.reset();
108 message_loop_.DeleteSoon(FROM_HERE, browser_context_.release());
109 message_loop_.RunUntilIdle();
110 #if !defined(OS_ANDROID)
111 ImageTransportFactory::Terminate();
112 #endif
115 cc::SurfaceId surface_id() { return view_->surface_id_; }
117 protected:
118 base::MessageLoopForUI message_loop_;
119 scoped_ptr<BrowserContext> browser_context_;
120 MockRenderWidgetHostDelegate delegate_;
122 // Tests should set these to NULL if they've already triggered their
123 // destruction.
124 RenderWidgetHostImpl* widget_host_;
125 RenderWidgetHostViewChildFrame* view_;
126 MockCrossProcessFrameConnector* test_frame_connector_;
128 private:
129 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewChildFrameTest);
132 scoped_ptr<cc::CompositorFrame> CreateDelegatedFrame(float scale_factor,
133 gfx::Size size,
134 const gfx::Rect& damage) {
135 scoped_ptr<cc::CompositorFrame> frame(new cc::CompositorFrame);
136 frame->metadata.device_scale_factor = scale_factor;
137 frame->delegated_frame_data.reset(new cc::DelegatedFrameData);
139 scoped_ptr<cc::RenderPass> pass = cc::RenderPass::Create();
140 pass->SetNew(cc::RenderPassId(1, 1), gfx::Rect(size), damage,
141 gfx::Transform());
142 frame->delegated_frame_data->render_pass_list.push_back(pass.Pass());
143 return frame;
146 TEST_F(RenderWidgetHostViewChildFrameTest, VisibilityTest) {
147 view_->Show();
148 ASSERT_TRUE(view_->IsShowing());
150 view_->Hide();
151 ASSERT_FALSE(view_->IsShowing());
154 // Verify that OnSwapCompositorFrame behavior is correct when a delegated
155 // frame is received from a renderer process.
156 TEST_F(RenderWidgetHostViewChildFrameTest, SwapCompositorFrame) {
157 gfx::Size view_size(100, 100);
158 gfx::Rect view_rect(view_size);
159 float scale_factor = 1.f;
161 view_->SetSize(view_size);
162 view_->Show();
164 view_->OnSwapCompositorFrame(
165 0, CreateDelegatedFrame(scale_factor, view_size, view_rect));
167 if (UseSurfacesEnabled()) {
168 cc::SurfaceId id = surface_id();
169 if (!id.is_null()) {
170 #if !defined(OS_ANDROID)
171 ImageTransportFactory* factory = ImageTransportFactory::GetInstance();
172 cc::SurfaceManager* manager = factory->GetSurfaceManager();
173 cc::Surface* surface = manager->GetSurfaceForId(id);
174 EXPECT_TRUE(surface);
175 // There should be a SurfaceSequence created by the RWHVChildFrame.
176 EXPECT_EQ(1u, surface->GetDestructionDependencyCount());
177 #endif
179 // Surface ID should have been passed to CrossProcessFrameConnector to
180 // be sent to the embedding renderer.
181 EXPECT_EQ(id, test_frame_connector_->last_surface_id_received_);
182 EXPECT_EQ(view_size, test_frame_connector_->last_frame_size_received_);
183 EXPECT_EQ(scale_factor,
184 test_frame_connector_->last_scale_factor_received_);
186 } else {
187 EXPECT_TRUE(test_frame_connector_->received_delegated_frame_);
188 EXPECT_EQ(view_size, test_frame_connector_->last_frame_size_received_);
189 EXPECT_EQ(scale_factor, test_frame_connector_->last_scale_factor_received_);
193 } // namespace content