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// Helper classes for implementing gpu client side unit tests.
6
7#ifndef GPU_COMMAND_BUFFER_CLIENT_CLIENT_TEST_HELPER_H_
8#define GPU_COMMAND_BUFFER_CLIENT_CLIENT_TEST_HELPER_H_
9
10#include "base/compiler_specific.h"
11#include "base/memory/scoped_ptr.h"
12#include "gpu/command_buffer/common/cmd_buffer_common.h"
13#include "gpu/command_buffer/common/command_buffer.h"
14#include "gpu/command_buffer/common/gpu_control.h"
15#include "gpu/command_buffer/common/gpu_memory_allocation.h"
16#include "testing/gmock/include/gmock/gmock.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace gpu {
20
21class CommandBufferHelper;
22
23class MockCommandBufferBase : public CommandBuffer {
24 public:
25  static const int32 kTransferBufferBaseId = 0x123;
26  static const int32 kMaxTransferBuffers = 6;
27
28  MockCommandBufferBase() { }
29  virtual ~MockCommandBufferBase();
30
31  virtual bool Initialize() OVERRIDE;
32  virtual State GetState() OVERRIDE;
33  virtual State GetLastState() OVERRIDE;
34  virtual int32 GetLastToken() OVERRIDE;
35  virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
36  virtual void SetGetBuffer(int transfer_buffer_id) OVERRIDE;
37  virtual void SetGetOffset(int32 get_offset) OVERRIDE;
38  virtual Buffer CreateTransferBuffer(size_t size, int32* id) OVERRIDE;
39  virtual Buffer GetTransferBuffer(int32 id) OVERRIDE;
40  virtual void SetToken(int32 token) OVERRIDE;
41  virtual void SetParseError(error::Error error) OVERRIDE;
42  virtual void SetContextLostReason(error::ContextLostReason reason) OVERRIDE;
43
44  // Get's the Id of the next transfer buffer that will be returned
45  // by CreateTransferBuffer. This is useful for testing expected ids.
46  int32 GetNextFreeTransferBufferId();
47
48  void FlushHelper(int32 put_offset);
49  void DestroyTransferBufferHelper(int32 id);
50
51  virtual void OnFlush() = 0;
52
53 private:
54  scoped_ptr<int8[]> transfer_buffers_[kMaxTransferBuffers];
55  Buffer transfer_buffer_buffers_[kMaxTransferBuffers];
56  CommandBufferEntry* ring_buffer_;
57  Buffer ring_buffer_buffer_;
58  State state_;
59};
60
61class MockClientCommandBuffer : public MockCommandBufferBase {
62 public:
63  MockClientCommandBuffer();
64  virtual ~MockClientCommandBuffer();
65
66  // This is so we can use all the gmock functions when Flush is called.
67  MOCK_METHOD0(OnFlush, void());
68  MOCK_METHOD1(DestroyTransferBuffer, void(int32 id));
69
70  virtual void Flush(int32 put_offset) OVERRIDE;
71
72  void DelegateToFake();
73};
74
75class MockClientCommandBufferMockFlush : public MockClientCommandBuffer {
76 public:
77  MockClientCommandBufferMockFlush();
78  virtual ~MockClientCommandBufferMockFlush();
79
80  MOCK_METHOD1(Flush, void(int32 put_offset));
81
82  void DelegateToFake();
83};
84
85class MockClientGpuControl : public GpuControl {
86 public:
87  MockClientGpuControl();
88  virtual ~MockClientGpuControl();
89
90  MOCK_METHOD0(GetCapabilities, Capabilities());
91  MOCK_METHOD4(CreateGpuMemoryBuffer,
92               gfx::GpuMemoryBuffer*(size_t width,
93                                     size_t height,
94                                     unsigned internalformat,
95                                     int32* id));
96  MOCK_METHOD1(DestroyGpuMemoryBuffer, void(int32 id));
97  MOCK_METHOD2(GenerateMailboxNames, bool(unsigned num,
98                                          std::vector<gpu::Mailbox>* names));
99  MOCK_METHOD0(InsertSyncPoint, uint32());
100  MOCK_METHOD2(SignalSyncPoint, void(uint32 id,
101                                     const base::Closure& callback));
102  MOCK_METHOD1(Echo, void(const base::Closure& callback));
103
104  MOCK_METHOD2(SignalQuery, void(uint32 query, const base::Closure& callback));
105  MOCK_METHOD1(SetSurfaceVisible, void(bool visible));
106  MOCK_METHOD1(SendManagedMemoryStats,
107               void(const ManagedMemoryStats& stats));
108
109 private:
110  DISALLOW_COPY_AND_ASSIGN(MockClientGpuControl);
111};
112
113}  // namespace gpu
114
115#endif  // GPU_COMMAND_BUFFER_CLIENT_CLIENT_TEST_HELPER_H_
116
117