OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016 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 "content/browser/browser_thread_impl.h" | |
6 #include "content/browser/compositor/test/no_transport_image_transport_factory.h " | |
7 #include "content/browser/renderer_host/offscreen_canvas_surface_impl.h" | |
8 #include "content/browser/renderer_host/offscreen_canvas_surface_manager.h" | |
9 #include "mojo/public/cpp/bindings/binding.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 #if defined(OS_ANDROID) | |
13 #include "content/browser/renderer_host/context_provider_factory_impl_android.h" | |
14 #include "content/test/mock_gpu_channel_establish_factory.h" | |
15 #else | |
16 #include "content/browser/compositor/image_transport_factory.h" | |
17 #endif | |
18 | |
19 namespace content { | |
20 | |
21 class OffscreenCanvasSurfaceManagerTest : public testing::Test { | |
22 public: | |
23 int getNumSurfaceImplInstances() { | |
24 return OffscreenCanvasSurfaceManager::GetInstance() | |
25 ->registered_surface_instances_.size(); | |
26 } | |
27 const cc::SurfaceId getCurrentSurfaceId() { return current_surface_id_; } | |
kinuko
2016/11/17 05:26:10
Did you mean const ref / const method?
const cc::
| |
28 void setSurfaceId(const cc::SurfaceId& surface_id) { | |
29 current_surface_id_ = surface_id; | |
30 } | |
31 | |
32 protected: | |
33 void SetUp() override; | |
34 void TearDown() override; | |
35 | |
36 private: | |
37 cc::SurfaceId current_surface_id_; | |
38 std::unique_ptr<BrowserThreadImpl> ui_thread_; | |
kinuko
2016/11/17 05:26:10
Could we just use TestBrowserThread and initialize
| |
39 base::MessageLoopForUI message_loop_; | |
40 #if defined(OS_ANDROID) | |
41 MockGpuChannelEstablishFactory gpu_channel_factory_; | |
42 #endif | |
43 }; | |
44 | |
45 void OffscreenCanvasSurfaceManagerTest::SetUp() { | |
46 #if defined(OS_ANDROID) | |
47 ContextProviderFactoryImpl::Initialize(&gpu_channel_factory_); | |
48 ui::ContextProviderFactory::SetInstance( | |
49 ContextProviderFactoryImpl::GetInstance()); | |
50 #else | |
51 ImageTransportFactory::InitializeForUnitTests( | |
52 std::unique_ptr<ImageTransportFactory>( | |
53 new NoTransportImageTransportFactory)); | |
54 #endif | |
55 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, &message_loop_)); | |
56 } | |
57 | |
58 void OffscreenCanvasSurfaceManagerTest::TearDown() { | |
59 #if defined(OS_ANDROID) | |
60 ui::ContextProviderFactory::SetInstance(nullptr); | |
61 ContextProviderFactoryImpl::Terminate(); | |
62 #else | |
63 ImageTransportFactory::Terminate(); | |
64 #endif | |
65 } | |
66 | |
67 // This test mimics the workflow of OffscreenCanvas.commit() on renderer | |
68 // process. | |
69 TEST_F(OffscreenCanvasSurfaceManagerTest, | |
70 SingleHTMLCanvasElementTransferToOffscreen) { | |
71 // Assume that HTMLCanvasElement.transferControlToOffscreen() is triggered and | |
72 // it will invoke GetSurfaceId function on OffscreenCanvasSurfaceImpl to | |
73 // obtain a unique SurfaceId from browser. | |
74 auto surface_impl = base::WrapUnique(new OffscreenCanvasSurfaceImpl()); | |
75 surface_impl->GetSurfaceId( | |
76 base::Bind(&OffscreenCanvasSurfaceManagerTest::setSurfaceId, | |
77 base::Unretained(this))); | |
78 | |
79 EXPECT_TRUE(this->getCurrentSurfaceId().is_valid()); | |
80 EXPECT_EQ(1, this->getNumSurfaceImplInstances()); | |
81 cc::FrameSinkId frame_sink_id = surface_impl.get()->frame_sink_id(); | |
82 EXPECT_EQ(frame_sink_id, this->getCurrentSurfaceId().frame_sink_id()); | |
83 EXPECT_EQ(surface_impl.get(), | |
84 OffscreenCanvasSurfaceManager::GetInstance()->GetSurfaceInstance( | |
85 frame_sink_id)); | |
86 | |
87 surface_impl = nullptr; | |
88 EXPECT_EQ(0, this->getNumSurfaceImplInstances()); | |
89 } | |
90 | |
91 TEST_F(OffscreenCanvasSurfaceManagerTest, | |
92 MultiHTMLCanvasElementTransferToOffscreen) { | |
93 // Same scenario as above test except that now we have two HTMLCanvasElement | |
94 // transferControlToOffscreen at the same time. | |
95 auto surface_impl_a = base::WrapUnique(new OffscreenCanvasSurfaceImpl()); | |
96 surface_impl_a->GetSurfaceId( | |
97 base::Bind(&OffscreenCanvasSurfaceManagerTest::setSurfaceId, | |
98 base::Unretained(this))); | |
99 cc::SurfaceId surface_id_a = this->getCurrentSurfaceId(); | |
100 | |
101 EXPECT_TRUE(surface_id_a.is_valid()); | |
102 | |
103 auto surface_impl_b = base::WrapUnique(new OffscreenCanvasSurfaceImpl()); | |
104 surface_impl_b->GetSurfaceId( | |
105 base::Bind(&OffscreenCanvasSurfaceManagerTest::setSurfaceId, | |
106 base::Unretained(this))); | |
107 cc::SurfaceId surface_id_b = this->getCurrentSurfaceId(); | |
108 | |
109 EXPECT_TRUE(surface_id_b.is_valid()); | |
110 EXPECT_NE(surface_id_a, surface_id_b); | |
111 | |
112 EXPECT_EQ(2, this->getNumSurfaceImplInstances()); | |
113 EXPECT_EQ(surface_impl_a.get(), | |
114 OffscreenCanvasSurfaceManager::GetInstance()->GetSurfaceInstance( | |
115 surface_id_a.frame_sink_id())); | |
116 EXPECT_EQ(surface_impl_b.get(), | |
117 OffscreenCanvasSurfaceManager::GetInstance()->GetSurfaceInstance( | |
118 surface_id_b.frame_sink_id())); | |
119 | |
120 surface_impl_a = nullptr; | |
121 EXPECT_EQ(1, this->getNumSurfaceImplInstances()); | |
122 surface_impl_b = nullptr; | |
123 EXPECT_EQ(0, this->getNumSurfaceImplInstances()); | |
124 } | |
125 | |
126 } // namespace content | |
OLD | NEW |