// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "o3d/gpu_plugin/command_buffer.h" #include "o3d/gpu_plugin/np_utils/dynamic_np_object.h" #include "o3d/gpu_plugin/np_utils/np_browser_mock.h" #include "o3d/gpu_plugin/np_utils/np_object_mock.h" #include "o3d/gpu_plugin/np_utils/np_object_pointer.h" #include "o3d/gpu_plugin/system_services/shared_memory_mock.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/gmock/include/gmock/gmock.h" using testing::_; using testing::DoAll; using testing::Return; using testing::SetArgumentPointee; using testing::StrictMock; namespace o3d { namespace gpu_plugin { class MockSystemNPObject : public DefaultNPObject { public: explicit MockSystemNPObject(NPP npp) { } MOCK_METHOD1(CreateSharedMemory, NPObjectPointer(int32)); NP_UTILS_BEGIN_DISPATCHER_CHAIN(MockSystemNPObject, DefaultNPObject) NP_UTILS_DISPATCHER(CreateSharedMemory, NPObjectPointer(int32)) NP_UTILS_END_DISPATCHER_CHAIN private: DISALLOW_COPY_AND_ASSIGN(MockSystemNPObject); }; class CommandBufferTest : public testing::Test { protected: virtual void SetUp() { command_buffer_ = NPCreateObject(NULL); window_object_ = NPCreateObject(NULL); chromium_object_ = NPCreateObject(NULL); NPSetProperty(NULL, window_object_, "chromium", chromium_object_); system_object_ = NPCreateObject >(NULL); NPSetProperty(NULL, chromium_object_, "system", system_object_); } MockNPBrowser mock_browser_; NPObjectPointer command_buffer_; NPObjectPointer window_object_; NPObjectPointer chromium_object_; NPObjectPointer system_object_; }; TEST_F(CommandBufferTest, NullRingBufferByDefault) { EXPECT_EQ(NPObjectPointer(), command_buffer_->GetRingBuffer()); } TEST_F(CommandBufferTest, InitializesCommandBuffer) { EXPECT_CALL(mock_browser_, GetWindowNPObject(NULL)) .WillOnce(Return(window_object_.ToReturned())); NPObjectPointer expected_shared_memory = NPCreateObject >(NULL); EXPECT_CALL(*system_object_.Get(), CreateSharedMemory(1024)) .WillOnce(Return(expected_shared_memory)); EXPECT_CALL(*expected_shared_memory.Get(), Map()) .WillOnce(Return(true)); EXPECT_TRUE(command_buffer_->Initialize(1024)); EXPECT_EQ(expected_shared_memory, command_buffer_->GetRingBuffer()); // Cannot reinitialize. EXPECT_FALSE(command_buffer_->Initialize(1024)); EXPECT_EQ(expected_shared_memory, command_buffer_->GetRingBuffer()); } TEST_F(CommandBufferTest, InitializeFailsIfCannotCreateSharedMemory) { EXPECT_CALL(mock_browser_, GetWindowNPObject(NULL)) .WillOnce(Return(window_object_.ToReturned())); EXPECT_CALL(*system_object_.Get(), CreateSharedMemory(1024)) .WillOnce(Return(NPObjectPointer())); EXPECT_FALSE(command_buffer_->Initialize(1024)); EXPECT_EQ(NPObjectPointer(), command_buffer_->GetRingBuffer()); } TEST_F(CommandBufferTest, InitializeFailsIfCannotMapSharedMemory) { EXPECT_CALL(mock_browser_, GetWindowNPObject(NULL)) .WillOnce(Return(window_object_.ToReturned())); NPObjectPointer expected_shared_memory = NPCreateObject >(NULL); EXPECT_CALL(*system_object_.Get(), CreateSharedMemory(1024)) .WillOnce(Return(expected_shared_memory)); EXPECT_CALL(*expected_shared_memory.Get(), Map()) .WillOnce(Return(false)); EXPECT_FALSE(command_buffer_->Initialize(1024)); EXPECT_EQ(NPObjectPointer(), command_buffer_->GetRingBuffer()); } TEST_F(CommandBufferTest, GetAndPutOffsetsDefaultToZero) { EXPECT_EQ(0, command_buffer_->GetGetOffset()); EXPECT_EQ(0, command_buffer_->GetPutOffset()); } class MockCallback : public CallbackRunner { public: MOCK_METHOD1(RunWithParams, void(const Tuple0&)); }; TEST_F(CommandBufferTest, CanSyncGetAndPutOffset) { EXPECT_CALL(mock_browser_, GetWindowNPObject(NULL)) .WillOnce(Return(window_object_.ToReturned())); NPObjectPointer expected_shared_memory = NPCreateObject >(NULL); EXPECT_CALL(*system_object_.Get(), CreateSharedMemory(1024)) .WillOnce(Return(expected_shared_memory)); EXPECT_CALL(*expected_shared_memory.Get(), Map()) .WillOnce(Return(true)); EXPECT_TRUE(command_buffer_->Initialize(1024)); StrictMock* put_offset_change_callback = new StrictMock; command_buffer_->SetPutOffsetChangeCallback(put_offset_change_callback); EXPECT_CALL(*put_offset_change_callback, RunWithParams(_)); EXPECT_EQ(0, command_buffer_->SyncOffsets(2)); EXPECT_EQ(2, command_buffer_->GetPutOffset()); EXPECT_CALL(*put_offset_change_callback, RunWithParams(_)); EXPECT_EQ(0, command_buffer_->SyncOffsets(4)); EXPECT_EQ(4, command_buffer_->GetPutOffset()); command_buffer_->SetGetOffset(2); EXPECT_EQ(2, command_buffer_->GetGetOffset()); EXPECT_CALL(*put_offset_change_callback, RunWithParams(_)); EXPECT_EQ(2, command_buffer_->SyncOffsets(6)); EXPECT_EQ(-1, command_buffer_->SyncOffsets(-1)); EXPECT_EQ(-1, command_buffer_->SyncOffsets(1024)); } } // namespace gpu_plugin } // namespace o3d