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