Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / compositor / reflector_impl_unittest.cc
blob2f0c299fcf1fd47c104dd071d2b50ce7507c1c81
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/message_loop/message_loop.h"
6 #include "base/run_loop.h"
7 #include "base/single_thread_task_runner.h"
8 #include "cc/test/fake_output_surface_client.h"
9 #include "cc/test/test_context_provider.h"
10 #include "cc/test/test_web_graphics_context_3d.h"
11 #include "content/browser/compositor/browser_compositor_output_surface.h"
12 #include "content/browser/compositor/browser_compositor_overlay_candidate_validator.h"
13 #include "content/browser/compositor/reflector_impl.h"
14 #include "content/browser/compositor/test/no_transport_image_transport_factory.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/compositor/compositor.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/compositor/test/context_factories_for_test.h"
20 #if defined(USE_OZONE)
21 #include "content/browser/compositor/browser_compositor_overlay_candidate_validator_ozone.h"
22 #include "ui/ozone/public/overlay_candidates_ozone.h"
23 #endif // defined(USE_OZONE)
25 namespace content {
26 namespace {
27 class FakeTaskRunner : public base::SingleThreadTaskRunner {
28 public:
29 FakeTaskRunner() {}
31 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
32 const base::Closure& task,
33 base::TimeDelta delay) override {
34 return true;
36 bool PostDelayedTask(const tracked_objects::Location& from_here,
37 const base::Closure& task,
38 base::TimeDelta delay) override {
39 return true;
41 bool RunsTasksOnCurrentThread() const override { return true; }
43 protected:
44 ~FakeTaskRunner() override {}
47 #if defined(USE_OZONE)
48 class TestOverlayCandidatesOzone : public ui::OverlayCandidatesOzone {
49 public:
50 TestOverlayCandidatesOzone() {}
51 ~TestOverlayCandidatesOzone() override {}
53 void CheckOverlaySupport(OverlaySurfaceCandidateList* surfaces) override {
54 (*surfaces)[0].overlay_handled = true;
57 #endif // defined(USE_OZONE)
59 scoped_ptr<BrowserCompositorOverlayCandidateValidator>
60 CreateTestValidatorOzone() {
61 #if defined(USE_OZONE)
62 return scoped_ptr<BrowserCompositorOverlayCandidateValidator>(
63 new BrowserCompositorOverlayCandidateValidatorOzone(
64 0, new TestOverlayCandidatesOzone()));
65 #else
66 return nullptr;
67 #endif // defined(USE_OZONE)
70 class TestOutputSurface : public BrowserCompositorOutputSurface {
71 public:
72 TestOutputSurface(
73 const scoped_refptr<cc::ContextProvider>& context_provider,
74 const scoped_refptr<ui::CompositorVSyncManager>& vsync_manager)
75 : BrowserCompositorOutputSurface(context_provider,
76 vsync_manager,
77 CreateTestValidatorOzone().Pass()) {}
79 void SetFlip(bool flip) { capabilities_.flipped_output_surface = flip; }
81 void SwapBuffers(cc::CompositorFrame* frame) override {}
83 #if defined(OS_MACOSX)
84 void OnSurfaceDisplayed() override {}
85 void SetSurfaceSuspendedForRecycle(bool suspended) override {}
86 bool SurfaceShouldNotShowFramesAfterSuspendForRecycle() const override {
87 return false;
89 #endif
91 gfx::Size SurfaceSize() const override { return gfx::Size(256, 256); }
94 const gfx::Rect kSubRect = gfx::Rect(0, 0, 64, 64);
95 const SkIRect kSkSubRect = SkIRect::MakeXYWH(0, 0, 64, 64);
97 } // namespace
99 class ReflectorImplTest : public testing::Test {
100 public:
101 void SetUp() override {
102 bool enable_pixel_output = false;
103 ui::ContextFactory* context_factory =
104 ui::InitializeContextFactoryForTests(enable_pixel_output);
105 ImageTransportFactory::InitializeForUnitTests(
106 scoped_ptr<ImageTransportFactory>(
107 new NoTransportImageTransportFactory));
108 message_loop_.reset(new base::MessageLoop());
109 proxy_ = message_loop_->message_loop_proxy();
110 compositor_task_runner_ = new FakeTaskRunner();
111 compositor_.reset(new ui::Compositor(gfx::kNullAcceleratedWidget,
112 context_factory,
113 compositor_task_runner_.get()));
114 context_provider_ = cc::TestContextProvider::Create(
115 cc::TestWebGraphicsContext3D::Create().Pass());
116 output_surface_ =
117 scoped_ptr<TestOutputSurface>(
118 new TestOutputSurface(context_provider_,
119 compositor_->vsync_manager())).Pass();
120 CHECK(output_surface_->BindToClient(&output_surface_client_));
122 mirroring_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR));
123 gfx::Size size = output_surface_->SurfaceSize();
124 mirroring_layer_->SetBounds(gfx::Rect(size.width(), size.height()));
127 void SetUpReflector() {
128 reflector_ = make_scoped_ptr(
129 new ReflectorImpl(compositor_.get(), mirroring_layer_.get()));
130 reflector_->OnSourceSurfaceReady(output_surface_.get());
133 void TearDown() override {
134 cc::TextureMailbox mailbox;
135 scoped_ptr<cc::SingleReleaseCallback> release;
136 if (mirroring_layer_->PrepareTextureMailbox(&mailbox, &release, false)) {
137 release->Run(0, false);
139 compositor_.reset();
140 ui::TerminateContextFactoryForTests();
141 ImageTransportFactory::Terminate();
144 void UpdateTexture() { reflector_->OnSourcePostSubBuffer(kSubRect); }
146 protected:
147 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
148 scoped_refptr<cc::ContextProvider> context_provider_;
149 cc::FakeOutputSurfaceClient output_surface_client_;
150 scoped_ptr<base::MessageLoop> message_loop_;
151 scoped_refptr<base::MessageLoopProxy> proxy_;
152 scoped_ptr<ui::Compositor> compositor_;
153 scoped_ptr<ui::Layer> mirroring_layer_;
154 scoped_ptr<ReflectorImpl> reflector_;
155 scoped_ptr<TestOutputSurface> output_surface_;
158 namespace {
159 TEST_F(ReflectorImplTest, CheckNormalOutputSurface) {
160 output_surface_->SetFlip(false);
161 SetUpReflector();
162 UpdateTexture();
163 EXPECT_TRUE(mirroring_layer_->TextureFlipped());
164 EXPECT_EQ(SkRegion(SkIRect::MakeXYWH(
165 0, output_surface_->SurfaceSize().height() - kSubRect.height(),
166 kSubRect.width(), kSubRect.height())),
167 mirroring_layer_->damaged_region());
170 TEST_F(ReflectorImplTest, CheckInvertedOutputSurface) {
171 output_surface_->SetFlip(true);
172 SetUpReflector();
173 UpdateTexture();
174 EXPECT_FALSE(mirroring_layer_->TextureFlipped());
175 EXPECT_EQ(SkRegion(kSkSubRect), mirroring_layer_->damaged_region());
178 #if defined(USE_OZONE)
179 TEST_F(ReflectorImplTest, CheckOverlayNoReflector) {
180 cc::OverlayCandidateList list;
181 cc::OverlayCandidate plane_1, plane_2;
182 plane_1.plane_z_order = 0;
183 plane_2.plane_z_order = 1;
184 list.push_back(plane_1);
185 list.push_back(plane_2);
186 output_surface_->GetOverlayCandidateValidator()->CheckOverlaySupport(&list);
187 EXPECT_TRUE(list[0].overlay_handled);
190 TEST_F(ReflectorImplTest, CheckOverlaySWMirroring) {
191 SetUpReflector();
192 cc::OverlayCandidateList list;
193 cc::OverlayCandidate plane_1, plane_2;
194 plane_1.plane_z_order = 0;
195 plane_2.plane_z_order = 1;
196 list.push_back(plane_1);
197 list.push_back(plane_2);
198 output_surface_->GetOverlayCandidateValidator()->CheckOverlaySupport(&list);
199 EXPECT_FALSE(list[0].overlay_handled);
201 #endif // defined(USE_OZONE)
203 } // namespace
204 } // namespace content