1 //* Copyright 2017 The Dawn Authors 2 //* 3 //* Licensed under the Apache License, Version 2.0 (the "License"); 4 //* you may not use this file except in compliance with the License. 5 //* You may obtain a copy of the License at 6 //* 7 //* http://www.apache.org/licenses/LICENSE-2.0 8 //* 9 //* Unless required by applicable law or agreed to in writing, software 10 //* distributed under the License is distributed on an "AS IS" BASIS, 11 //* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 //* See the License for the specific language governing permissions and 13 //* limitations under the License. 14 15 #ifndef MOCK_WEBGPU_H 16 #define MOCK_WEBGPU_H 17 18 #include <dawn/dawn_proc_table.h> 19 #include <dawn/webgpu.h> 20 #include <gmock/gmock.h> 21 22 #include <memory> 23 24 // An abstract base class representing a proc table so that API calls can be mocked. Most API calls 25 // are directly represented by a delete virtual method but others need minimal state tracking to be 26 // useful as mocks. 27 class ProcTableAsClass { 28 public: 29 virtual ~ProcTableAsClass(); 30 31 void GetProcTableAndDevice(DawnProcTable* table, WGPUDevice* device); 32 33 // Creates an object that can be returned by a mocked call as in WillOnce(Return(foo)). 34 // It returns an object of the write type that isn't equal to any previously returned object. 35 // Otherwise some mock expectation could be triggered by two different objects having the same 36 // value. 37 {% for type in by_category["object"] %} 38 {{as_cType(type.name)}} GetNew{{type.name.CamelCase()}}(); 39 {% endfor %} 40 41 {% for type in by_category["object"] %} 42 {% for method in type.methods if not has_callback_arguments(method) %} 43 virtual {{as_cType(method.return_type.name)}} {{as_MethodSuffix(type.name, method.name)}}( 44 {{-as_cType(type.name)}} {{as_varName(type.name)}} 45 {%- for arg in method.arguments -%} 46 , {{as_annotated_cType(arg)}} 47 {%- endfor -%} 48 ) = 0; 49 {% endfor %} 50 51 virtual void {{as_MethodSuffix(type.name, Name("reference"))}}({{as_cType(type.name)}} self) = 0; 52 virtual void {{as_MethodSuffix(type.name, Name("release"))}}({{as_cType(type.name)}} self) = 0; 53 54 {% for method in type.methods if has_callback_arguments(method) %} 55 {% set Suffix = as_MethodSuffix(type.name, method.name) %} 56 //* Stores callback and userdata and calls the On* method. 57 {{as_cType(method.return_type.name)}} {{Suffix}}( 58 {{-as_cType(type.name)}} {{as_varName(type.name)}} 59 {%- for arg in method.arguments -%} 60 , {{as_annotated_cType(arg)}} 61 {%- endfor -%} 62 ); 63 //* The virtual function to call after saving the callback and userdata in the proc. 64 //* This function can be mocked. 65 virtual {{as_cType(method.return_type.name)}} On{{Suffix}}( 66 {{-as_cType(type.name)}} {{as_varName(type.name)}} 67 {%- for arg in method.arguments -%} 68 , {{as_annotated_cType(arg)}} 69 {%- endfor -%} 70 ) = 0; 71 72 //* Calls the stored callback. 73 {% for callback_arg in method.arguments if callback_arg.type.category == 'function pointer' %} 74 void Call{{as_MethodSuffix(type.name, method.name)}}Callback( 75 {{-as_cType(type.name)}} {{as_varName(type.name)}} 76 {%- for arg in callback_arg.type.arguments -%} 77 {%- if not loop.last -%}, {{as_annotated_cType(arg)}}{%- endif -%} 78 {%- endfor -%} 79 ); 80 {% endfor %} 81 {% endfor %} 82 {% endfor %} 83 84 struct Object { 85 ProcTableAsClass* procs = nullptr; 86 {% for type in by_category["object"] %} 87 {% for method in type.methods if has_callback_arguments(method) %} 88 {% for callback_arg in method.arguments if callback_arg.type.category == 'function pointer' %} 89 {{as_cType(callback_arg.type.name)}} m{{as_MethodSuffix(type.name, method.name)}}Callback = nullptr; 90 {% endfor %} 91 {% endfor %} 92 {% endfor %} 93 void* userdata = 0; 94 }; 95 96 private: 97 // Remembers the values returned by GetNew* so they can be freed. 98 std::vector<std::unique_ptr<Object>> mObjects; 99 }; 100 101 class MockProcTable : public ProcTableAsClass { 102 public: 103 MockProcTable(); 104 ~MockProcTable() override; 105 106 void IgnoreAllReleaseCalls(); 107 108 {% for type in by_category["object"] %} 109 {% for method in type.methods if not has_callback_arguments(method) %} 110 MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "}} 111 {{-as_MethodSuffix(type.name, method.name)}}, ( 112 {{-as_cType(type.name)}} {{as_varName(type.name)}} 113 {%- for arg in method.arguments -%} 114 , {{as_annotated_cType(arg)}} 115 {%- endfor -%} 116 ), (override)); 117 {% endfor %} 118 119 MOCK_METHOD(void, {{as_MethodSuffix(type.name, Name("reference"))}}, ({{as_cType(type.name)}} self), (override)); 120 MOCK_METHOD(void, {{as_MethodSuffix(type.name, Name("release"))}}, ({{as_cType(type.name)}} self), (override)); 121 122 {% for method in type.methods if has_callback_arguments(method) %} 123 MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "-}} 124 On{{as_MethodSuffix(type.name, method.name)}}, ( 125 {{-as_cType(type.name)}} {{as_varName(type.name)}} 126 {%- for arg in method.arguments -%} 127 , {{as_annotated_cType(arg)}} 128 {%- endfor -%} 129 ), (override)); 130 {% endfor %} 131 {% endfor %} 132 }; 133 134 #endif // MOCK_WEBGPU_H 135