1 // Copyright (c) 2012 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 "gpu/command_buffer/service/transfer_buffer_manager.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "testing/gmock/include/gmock/gmock.h"
11 using base::SharedMemory
;
15 const static size_t kBufferSize
= 1024;
17 class TransferBufferManagerTest
: public testing::Test
{
19 void SetUp() override
{
20 TransferBufferManager
* manager
= new TransferBufferManager(nullptr);
21 transfer_buffer_manager_
= manager
;
22 ASSERT_TRUE(manager
->Initialize());
25 scoped_refptr
<TransferBufferManagerInterface
> transfer_buffer_manager_
;
28 TEST_F(TransferBufferManagerTest
, ZeroHandleMapsToNull
) {
29 EXPECT_TRUE(NULL
== transfer_buffer_manager_
->GetTransferBuffer(0).get());
32 TEST_F(TransferBufferManagerTest
, NegativeHandleMapsToNull
) {
33 EXPECT_TRUE(NULL
== transfer_buffer_manager_
->GetTransferBuffer(-1).get());
36 TEST_F(TransferBufferManagerTest
, OutOfRangeHandleMapsToNull
) {
37 EXPECT_TRUE(NULL
== transfer_buffer_manager_
->GetTransferBuffer(1).get());
40 TEST_F(TransferBufferManagerTest
, CanRegisterTransferBuffer
) {
41 scoped_ptr
<base::SharedMemory
> shm(new base::SharedMemory());
42 shm
->CreateAndMapAnonymous(kBufferSize
);
43 base::SharedMemory
* shm_raw_pointer
= shm
.get();
44 scoped_ptr
<SharedMemoryBufferBacking
> backing(
45 new SharedMemoryBufferBacking(shm
.Pass(), kBufferSize
));
46 SharedMemoryBufferBacking
* backing_raw_ptr
= backing
.get();
49 transfer_buffer_manager_
->RegisterTransferBuffer(1, backing
.Pass()));
50 scoped_refptr
<Buffer
> registered
=
51 transfer_buffer_manager_
->GetTransferBuffer(1);
53 // Shared-memory ownership is transfered. It should be the same memory.
54 EXPECT_EQ(backing_raw_ptr
, registered
->backing());
55 EXPECT_EQ(shm_raw_pointer
, backing_raw_ptr
->shared_memory());
58 class FakeBufferBacking
: public BufferBacking
{
60 void* GetMemory() const override
{
61 return reinterpret_cast<void*>(0xBADF00D0);
63 size_t GetSize() const override
{ return 42; }
64 static scoped_ptr
<BufferBacking
> Make() {
65 return scoped_ptr
<BufferBacking
>(new FakeBufferBacking
);
69 TEST_F(TransferBufferManagerTest
, CanDestroyTransferBuffer
) {
70 EXPECT_TRUE(transfer_buffer_manager_
->RegisterTransferBuffer(
71 1, scoped_ptr
<BufferBacking
>(new FakeBufferBacking
)));
72 transfer_buffer_manager_
->DestroyTransferBuffer(1);
73 scoped_refptr
<Buffer
> registered
=
74 transfer_buffer_manager_
->GetTransferBuffer(1);
76 scoped_refptr
<Buffer
> null_buffer
;
77 EXPECT_EQ(null_buffer
, registered
);
80 TEST_F(TransferBufferManagerTest
, CannotRegregisterTransferBufferId
) {
81 EXPECT_TRUE(transfer_buffer_manager_
->RegisterTransferBuffer(
82 1, FakeBufferBacking::Make()));
83 EXPECT_FALSE(transfer_buffer_manager_
->RegisterTransferBuffer(
84 1, FakeBufferBacking::Make()));
85 EXPECT_FALSE(transfer_buffer_manager_
->RegisterTransferBuffer(
86 1, FakeBufferBacking::Make()));
89 TEST_F(TransferBufferManagerTest
, CanReuseTransferBufferIdAfterDestroying
) {
90 EXPECT_TRUE(transfer_buffer_manager_
->RegisterTransferBuffer(
91 1, FakeBufferBacking::Make()));
92 transfer_buffer_manager_
->DestroyTransferBuffer(1);
93 EXPECT_TRUE(transfer_buffer_manager_
->RegisterTransferBuffer(
94 1, FakeBufferBacking::Make()));
97 TEST_F(TransferBufferManagerTest
, DestroyUnusedTransferBufferIdDoesNotCrash
) {
98 transfer_buffer_manager_
->DestroyTransferBuffer(1);
101 TEST_F(TransferBufferManagerTest
, CannotRegisterNullTransferBuffer
) {
102 EXPECT_FALSE(transfer_buffer_manager_
->RegisterTransferBuffer(
103 0, FakeBufferBacking::Make()));
106 TEST_F(TransferBufferManagerTest
, CannotRegisterNegativeTransferBufferId
) {
107 scoped_ptr
<base::SharedMemory
> shm(new base::SharedMemory());
108 shm
->CreateAndMapAnonymous(kBufferSize
);
109 EXPECT_FALSE(transfer_buffer_manager_
->RegisterTransferBuffer(
110 -1, FakeBufferBacking::Make()));