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 // This file contains definitions for mock objects, used for testing. 6 7 // TODO(apatrick): This file "manually" defines some mock objects. Using gMock 8 // would be definitely preferable, unfortunately it doesn't work on Windows yet. 9 10 #ifndef GPU_COMMAND_BUFFER_SERVICE_MOCKS_H_ 11 #define GPU_COMMAND_BUFFER_SERVICE_MOCKS_H_ 12 13 #include <string> 14 #include <vector> 15 16 #include "base/logging.h" 17 #include "gpu/command_buffer/service/cmd_parser.h" 18 #include "gpu/command_buffer/service/cmd_buffer_engine.h" 19 #include "gpu/command_buffer/service/memory_tracking.h" 20 #include "gpu/command_buffer/service/program_cache.h" 21 #include "gpu/command_buffer/service/shader_translator.h" 22 #include "testing/gmock/include/gmock/gmock.h" 23 24 namespace gpu { 25 26 // Mocks an AsyncAPIInterface, using GMock. 27 class AsyncAPIMock : public AsyncAPIInterface { 28 public: 29 AsyncAPIMock(); 30 virtual ~AsyncAPIMock(); 31 32 // Predicate that matches args passed to DoCommand, by looking at the values. 33 class IsArgs { 34 public: 35 IsArgs(unsigned int arg_count, const void* args) 36 : arg_count_(arg_count), 37 args_(static_cast<CommandBufferEntry*>(const_cast<void*>(args))) { 38 } 39 40 bool operator() (const void* _args) const { 41 const CommandBufferEntry* args = 42 static_cast<const CommandBufferEntry*>(_args) + 1; 43 for (unsigned int i = 0; i < arg_count_; ++i) { 44 if (args[i].value_uint32 != args_[i].value_uint32) return false; 45 } 46 return true; 47 } 48 49 private: 50 unsigned int arg_count_; 51 CommandBufferEntry *args_; 52 }; 53 54 MOCK_METHOD3(DoCommand, error::Error( 55 unsigned int command, 56 unsigned int arg_count, 57 const void* cmd_data)); 58 59 const char* GetCommandName(unsigned int command_id) const { 60 return ""; 61 }; 62 63 // Sets the engine, to forward SetToken commands to it. 64 void set_engine(CommandBufferEngine *engine) { engine_ = engine; } 65 66 // Forwards the SetToken commands to the engine. 67 void SetToken(unsigned int command, 68 unsigned int arg_count, 69 const void* _args); 70 71 private: 72 CommandBufferEngine *engine_; 73 }; 74 75 namespace gles2 { 76 77 class MockShaderTranslator : public ShaderTranslatorInterface { 78 public: 79 MockShaderTranslator(); 80 virtual ~MockShaderTranslator(); 81 82 MOCK_METHOD5(Init, bool( 83 ShShaderType shader_type, 84 ShShaderSpec shader_spec, 85 const ShBuiltInResources* resources, 86 GlslImplementationType glsl_implementation_type, 87 ShCompileOptions driver_bug_workarounds)); 88 MOCK_METHOD1(Translate, bool(const char* shader)); 89 MOCK_CONST_METHOD0(translated_shader, const char*()); 90 MOCK_CONST_METHOD0(info_log, const char*()); 91 MOCK_CONST_METHOD0(attrib_map, const VariableMap&()); 92 MOCK_CONST_METHOD0(uniform_map, const VariableMap&()); 93 MOCK_CONST_METHOD0(varying_map, const VariableMap&()); 94 MOCK_CONST_METHOD0(name_map, const NameMap&()); 95 MOCK_CONST_METHOD0( 96 GetStringForOptionsThatWouldEffectCompilation, std::string()); 97 }; 98 99 class MockProgramCache : public ProgramCache { 100 public: 101 MockProgramCache(); 102 virtual ~MockProgramCache(); 103 104 MOCK_METHOD7(LoadLinkedProgram, ProgramLoadResult( 105 GLuint program, 106 Shader* shader_a, 107 const ShaderTranslatorInterface* translator_a, 108 Shader* shader_b, 109 const ShaderTranslatorInterface* translator_b, 110 const LocationMap* bind_attrib_location_map, 111 const ShaderCacheCallback& callback)); 112 113 MOCK_METHOD7(SaveLinkedProgram, void( 114 GLuint program, 115 const Shader* shader_a, 116 const ShaderTranslatorInterface* translator_a, 117 const Shader* shader_b, 118 const ShaderTranslatorInterface* translator_b, 119 const LocationMap* bind_attrib_location_map, 120 const ShaderCacheCallback& callback)); 121 MOCK_METHOD1(LoadProgram, void(const std::string&)); 122 123 private: 124 MOCK_METHOD0(ClearBackend, void()); 125 }; 126 127 class MockMemoryTracker : public MemoryTracker { 128 public: 129 MockMemoryTracker(); 130 131 MOCK_METHOD3(TrackMemoryAllocatedChange, void( 132 size_t old_size, size_t new_size, Pool pool)); 133 MOCK_METHOD1(EnsureGPUMemoryAvailable, bool(size_t size_needed)); 134 135 private: 136 friend class ::testing::StrictMock<MockMemoryTracker>; 137 friend class base::RefCounted< ::testing::StrictMock<MockMemoryTracker> >; 138 virtual ~MockMemoryTracker(); 139 }; 140 141 } // namespace gles2 142 } // namespace gpu 143 144 #endif // GPU_COMMAND_BUFFER_SERVICE_MOCKS_H_ 145