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.
6 #include "base/bind_helpers.h"
7 #include "base/threading/thread.h"
8 #include "gpu/command_buffer/common/cmd_buffer_common.h"
9 #include "gpu/command_buffer/service/command_buffer_service.h"
10 #include "gpu/command_buffer/service/transfer_buffer_manager.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/gmock/include/gmock/gmock.h"
14 using base::SharedMemory
;
17 using testing::Return
;
18 using testing::SetArgumentPointee
;
19 using testing::StrictMock
;
23 class CommandBufferServiceTest
: public testing::Test
{
25 void SetUp() override
{
27 TransferBufferManager
* manager
= new TransferBufferManager();
28 transfer_buffer_manager_
.reset(manager
);
29 EXPECT_TRUE(manager
->Initialize());
31 command_buffer_
.reset(
32 new CommandBufferService(transfer_buffer_manager_
.get()));
33 EXPECT_TRUE(command_buffer_
->Initialize());
36 int32
GetGetOffset() {
37 return command_buffer_
->GetLastState().get_offset
;
40 int32
GetPutOffset() {
41 return command_buffer_
->GetPutOffset();
45 return command_buffer_
->GetLastState().token
;
49 return command_buffer_
->GetLastState().error
;
52 bool Initialize(size_t size
) {
54 command_buffer_
->CreateTransferBuffer(size
, &id
);
56 command_buffer_
->SetGetBuffer(id
);
60 scoped_ptr
<TransferBufferManagerInterface
> transfer_buffer_manager_
;
61 scoped_ptr
<CommandBufferService
> command_buffer_
;
64 TEST_F(CommandBufferServiceTest
, InitializesCommandBuffer
) {
65 EXPECT_TRUE(Initialize(1024));
66 CommandBuffer::State state
= command_buffer_
->GetLastState();
67 EXPECT_EQ(0, state
.get_offset
);
68 EXPECT_EQ(0, command_buffer_
->GetPutOffset());
69 EXPECT_EQ(0, state
.token
);
70 EXPECT_EQ(error::kNoError
, state
.error
);
77 virtual void PutOffsetChanged() = 0;
78 virtual bool GetBufferChanged(int32 id
) = 0;
81 class MockCallbackTest
: public CallbackTest
{
83 MOCK_METHOD0(PutOffsetChanged
, void());
84 MOCK_METHOD1(GetBufferChanged
, bool(int32
));
87 } // anonymous namespace
89 TEST_F(CommandBufferServiceTest
, CanSyncGetAndPutOffset
) {
92 scoped_ptr
<StrictMock
<MockCallbackTest
> > change_callback(
93 new StrictMock
<MockCallbackTest
>);
94 command_buffer_
->SetPutOffsetChangeCallback(
96 &CallbackTest::PutOffsetChanged
,
97 base::Unretained(change_callback
.get())));
99 EXPECT_CALL(*change_callback
, PutOffsetChanged());
100 command_buffer_
->Flush(2);
101 EXPECT_EQ(0, GetGetOffset());
102 EXPECT_EQ(2, GetPutOffset());
104 EXPECT_CALL(*change_callback
, PutOffsetChanged());
105 command_buffer_
->Flush(4);
106 EXPECT_EQ(0, GetGetOffset());
107 EXPECT_EQ(4, GetPutOffset());
109 command_buffer_
->SetGetOffset(2);
110 EXPECT_EQ(2, GetGetOffset());
111 EXPECT_CALL(*change_callback
, PutOffsetChanged());
112 command_buffer_
->Flush(6);
114 command_buffer_
->Flush(-1);
115 EXPECT_NE(error::kNoError
, GetError());
116 command_buffer_
->Flush(1024);
117 EXPECT_NE(error::kNoError
, GetError());
120 TEST_F(CommandBufferServiceTest
, SetGetBuffer
) {
121 int32 ring_buffer_id
;
122 command_buffer_
->CreateTransferBuffer(1024, &ring_buffer_id
);
123 EXPECT_GT(ring_buffer_id
, 0);
125 scoped_ptr
<StrictMock
<MockCallbackTest
> > change_callback(
126 new StrictMock
<MockCallbackTest
>);
127 command_buffer_
->SetGetBufferChangeCallback(
129 &CallbackTest::GetBufferChanged
,
130 base::Unretained(change_callback
.get())));
132 EXPECT_CALL(*change_callback
, GetBufferChanged(ring_buffer_id
))
133 .WillOnce(Return(true));
135 command_buffer_
->SetGetBuffer(ring_buffer_id
);
136 EXPECT_EQ(0, GetGetOffset());
139 TEST_F(CommandBufferServiceTest
, DefaultTokenIsZero
) {
140 EXPECT_EQ(0, GetToken());
143 TEST_F(CommandBufferServiceTest
, CanSetToken
) {
144 command_buffer_
->SetToken(7);
145 EXPECT_EQ(7, GetToken());
148 TEST_F(CommandBufferServiceTest
, DefaultParseErrorIsNoError
) {
149 EXPECT_EQ(0, GetError());
152 TEST_F(CommandBufferServiceTest
, CanSetParseError
) {
153 command_buffer_
->SetParseError(error::kInvalidSize
);
154 EXPECT_EQ(1, GetError());