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. 4 5 #include "base/bind.h" 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" 13 14 using base::SharedMemory; 15 using testing::_; 16 using testing::DoAll; 17 using testing::Return; 18 using testing::SetArgumentPointee; 19 using testing::StrictMock; 20 21 namespace gpu { 22 23 class CommandBufferServiceTest : public testing::Test { 24 protected: 25 virtual void SetUp() { 26 { 27 TransferBufferManager* manager = new TransferBufferManager(); 28 transfer_buffer_manager_.reset(manager); 29 EXPECT_TRUE(manager->Initialize()); 30 } 31 command_buffer_.reset( 32 new CommandBufferService(transfer_buffer_manager_.get())); 33 EXPECT_TRUE(command_buffer_->Initialize()); 34 } 35 36 int32 GetGetOffset() { 37 return command_buffer_->GetState().get_offset; 38 } 39 40 int32 GetPutOffset() { 41 return command_buffer_->GetState().put_offset; 42 } 43 44 int32 GetToken() { 45 return command_buffer_->GetState().token; 46 } 47 48 int32 GetError() { 49 return command_buffer_->GetState().error; 50 } 51 52 bool Initialize(size_t size) { 53 int32 id; 54 command_buffer_->CreateTransferBuffer(size, &id); 55 EXPECT_GT(id, 0); 56 command_buffer_->SetGetBuffer(id); 57 return true; 58 } 59 60 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_; 61 scoped_ptr<CommandBufferService> command_buffer_; 62 }; 63 64 TEST_F(CommandBufferServiceTest, InitializesCommandBuffer) { 65 EXPECT_TRUE(Initialize(1024)); 66 CommandBuffer::State state = command_buffer_->GetState(); 67 EXPECT_EQ(0, state.get_offset); 68 EXPECT_EQ(0, state.put_offset); 69 EXPECT_EQ(0, state.token); 70 EXPECT_EQ(error::kNoError, state.error); 71 } 72 73 namespace { 74 75 class CallbackTest { 76 public: 77 virtual void PutOffsetChanged() = 0; 78 virtual bool GetBufferChanged(int32 id) = 0; 79 }; 80 81 class MockCallbackTest : public CallbackTest { 82 public: 83 MOCK_METHOD0(PutOffsetChanged, void()); 84 MOCK_METHOD1(GetBufferChanged, bool(int32)); 85 }; 86 87 } // anonymous namespace 88 89 TEST_F(CommandBufferServiceTest, CanSyncGetAndPutOffset) { 90 Initialize(1024); 91 92 scoped_ptr<StrictMock<MockCallbackTest> > change_callback( 93 new StrictMock<MockCallbackTest>); 94 command_buffer_->SetPutOffsetChangeCallback( 95 base::Bind( 96 &CallbackTest::PutOffsetChanged, 97 base::Unretained(change_callback.get()))); 98 99 EXPECT_CALL(*change_callback, PutOffsetChanged()); 100 command_buffer_->Flush(2); 101 EXPECT_EQ(0, GetGetOffset()); 102 EXPECT_EQ(2, GetPutOffset()); 103 104 EXPECT_CALL(*change_callback, PutOffsetChanged()); 105 command_buffer_->Flush(4); 106 EXPECT_EQ(0, GetGetOffset()); 107 EXPECT_EQ(4, GetPutOffset()); 108 109 command_buffer_->SetGetOffset(2); 110 EXPECT_EQ(2, GetGetOffset()); 111 EXPECT_CALL(*change_callback, PutOffsetChanged()); 112 command_buffer_->Flush(6); 113 114 command_buffer_->Flush(-1); 115 EXPECT_NE(error::kNoError, GetError()); 116 command_buffer_->Flush(1024); 117 EXPECT_NE(error::kNoError, GetError()); 118 } 119 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); 124 125 scoped_ptr<StrictMock<MockCallbackTest> > change_callback( 126 new StrictMock<MockCallbackTest>); 127 command_buffer_->SetGetBufferChangeCallback( 128 base::Bind( 129 &CallbackTest::GetBufferChanged, 130 base::Unretained(change_callback.get()))); 131 132 EXPECT_CALL(*change_callback, GetBufferChanged(ring_buffer_id)) 133 .WillOnce(Return(true)); 134 135 command_buffer_->SetGetBuffer(ring_buffer_id); 136 EXPECT_EQ(0, GetGetOffset()); 137 } 138 139 TEST_F(CommandBufferServiceTest, DefaultTokenIsZero) { 140 EXPECT_EQ(0, GetToken()); 141 } 142 143 TEST_F(CommandBufferServiceTest, CanSetToken) { 144 command_buffer_->SetToken(7); 145 EXPECT_EQ(7, GetToken()); 146 } 147 148 TEST_F(CommandBufferServiceTest, DefaultParseErrorIsNoError) { 149 EXPECT_EQ(0, GetError()); 150 } 151 152 TEST_F(CommandBufferServiceTest, CanSetParseError) { 153 command_buffer_->SetParseError(error::kInvalidSize); 154 EXPECT_EQ(1, GetError()); 155 } 156 } // namespace gpu 157