Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | //* Copyright 2017 The Dawn & Tint Authors |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 2 | //* |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | //* Redistribution and use in source and binary forms, with or without |
| 4 | //* modification, are permitted provided that the following conditions are met: |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 5 | //* |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | //* 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | //* list of conditions and the following disclaimer. |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 8 | //* |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | //* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | //* this list of conditions and the following disclaimer in the documentation |
| 11 | //* and/or other materials provided with the distribution. |
| 12 | //* |
| 13 | //* 3. Neither the name of the copyright holder nor the names of its |
| 14 | //* contributors may be used to endorse or promote products derived from |
| 15 | //* this software without specific prior written permission. |
| 16 | //* |
| 17 | //* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | //* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | //* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | //* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | //* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | //* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | //* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | //* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | //* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | //* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 27 | |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 28 | {% set API = metadata.api.upper() %} |
| 29 | {% set api = API.lower() %} |
| 30 | #ifndef MOCK_{{API}}_H |
| 31 | #define MOCK_{{API}}_H |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 32 | |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 33 | {% set Prefix = metadata.proc_table_prefix %} |
| 34 | {% set prefix = Prefix.lower() %} |
dan sinclair | d0fb4a3 | 2022-04-19 14:24:04 +0000 | [diff] [blame] | 35 | #include "dawn/{{prefix}}_proc_table.h" |
| 36 | #include "dawn/{{api}}.h" |
Corentin Wallez | 9649682 | 2019-10-15 11:44:38 +0000 | [diff] [blame] | 37 | #include <gmock/gmock.h> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 38 | |
Corentin Wallez | 944b60f | 2017-05-29 11:33:33 -0700 | [diff] [blame] | 39 | #include <memory> |
| 40 | |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 41 | // An abstract base class representing a proc table so that API calls can be mocked. Most API calls |
| 42 | // are directly represented by a delete virtual method but others need minimal state tracking to be |
| 43 | // useful as mocks. |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 44 | class ProcTableAsClass { |
| 45 | public: |
| 46 | virtual ~ProcTableAsClass(); |
| 47 | |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 48 | void GetProcTable({{Prefix}}ProcTable* table); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 49 | |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 50 | // Creates an object that can be returned by a mocked call as in WillOnce(Return(foo)). |
| 51 | // It returns an object of the write type that isn't equal to any previously returned object. |
| 52 | // Otherwise some mock expectation could be triggered by two different objects having the same |
| 53 | // value. |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 54 | {% for type in by_category["object"] %} |
| 55 | {{as_cType(type.name)}} GetNew{{type.name.CamelCase()}}(); |
| 56 | {% endfor %} |
| 57 | |
| 58 | {% for type in by_category["object"] %} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 59 | {% for method in type.methods if not has_callback_arguments(method) %} |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 60 | virtual {{as_cType(method.return_type.name)}} {{as_MethodSuffix(type.name, method.name)}}( |
| 61 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 62 | {%- for arg in method.arguments -%} |
| 63 | , {{as_annotated_cType(arg)}} |
| 64 | {%- endfor -%} |
| 65 | ) = 0; |
| 66 | {% endfor %} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 67 | |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 68 | virtual void {{as_MethodSuffix(type.name, Name("reference"))}}({{as_cType(type.name)}} self) = 0; |
| 69 | virtual void {{as_MethodSuffix(type.name, Name("release"))}}({{as_cType(type.name)}} self) = 0; |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 70 | |
| 71 | {% for method in type.methods if has_callback_arguments(method) %} |
| 72 | {% set Suffix = as_MethodSuffix(type.name, method.name) %} |
| 73 | //* Stores callback and userdata and calls the On* method. |
| 74 | {{as_cType(method.return_type.name)}} {{Suffix}}( |
| 75 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 76 | {%- for arg in method.arguments -%} |
| 77 | , {{as_annotated_cType(arg)}} |
| 78 | {%- endfor -%} |
| 79 | ); |
| 80 | //* The virtual function to call after saving the callback and userdata in the proc. |
| 81 | //* This function can be mocked. |
| 82 | virtual {{as_cType(method.return_type.name)}} On{{Suffix}}( |
| 83 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 84 | {%- for arg in method.arguments -%} |
| 85 | , {{as_annotated_cType(arg)}} |
| 86 | {%- endfor -%} |
| 87 | ) = 0; |
| 88 | |
| 89 | //* Calls the stored callback. |
fujunwei | 23f7162 | 2021-12-02 07:41:21 +0000 | [diff] [blame] | 90 | {% for callback_arg in method.arguments if callback_arg.type.category == 'function pointer' %} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 91 | void Call{{as_MethodSuffix(type.name, method.name)}}Callback( |
| 92 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 93 | {%- for arg in callback_arg.type.arguments -%} |
| 94 | {%- if not loop.last -%}, {{as_annotated_cType(arg)}}{%- endif -%} |
| 95 | {%- endfor -%} |
| 96 | ); |
| 97 | {% endfor %} |
| 98 | {% endfor %} |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 99 | {% endfor %} |
| 100 | |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 101 | struct Object { |
| 102 | ProcTableAsClass* procs = nullptr; |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 103 | {% for type in by_category["object"] %} |
| 104 | {% for method in type.methods if has_callback_arguments(method) %} |
fujunwei | 23f7162 | 2021-12-02 07:41:21 +0000 | [diff] [blame] | 105 | {% for callback_arg in method.arguments if callback_arg.type.category == 'function pointer' %} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 106 | {{as_cType(callback_arg.type.name)}} m{{as_MethodSuffix(type.name, method.name)}}Callback = nullptr; |
| 107 | {% endfor %} |
| 108 | {% endfor %} |
| 109 | {% endfor %} |
Natasha Lee | 9bba4a9 | 2019-12-18 18:59:20 +0000 | [diff] [blame] | 110 | void* userdata = 0; |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 113 | private: |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 114 | // Remembers the values returned by GetNew* so they can be freed. |
Corentin Wallez | 42dbde1 | 2017-11-23 16:04:26 -0500 | [diff] [blame] | 115 | std::vector<std::unique_ptr<Object>> mObjects; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | class MockProcTable : public ProcTableAsClass { |
| 119 | public: |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 120 | MockProcTable(); |
Corentin Wallez | 3741f73 | 2020-04-16 18:08:23 +0000 | [diff] [blame] | 121 | ~MockProcTable() override; |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 122 | |
Corentin Wallez | d754fb2 | 2019-03-28 10:44:41 +0000 | [diff] [blame] | 123 | void IgnoreAllReleaseCalls(); |
| 124 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 125 | {% for type in by_category["object"] %} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 126 | {% for method in type.methods if not has_callback_arguments(method) %} |
Corentin Wallez | f941205 | 2020-04-16 16:39:06 +0000 | [diff] [blame] | 127 | MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "}} |
| 128 | {{-as_MethodSuffix(type.name, method.name)}}, ( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 129 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 130 | {%- for arg in method.arguments -%} |
| 131 | , {{as_annotated_cType(arg)}} |
| 132 | {%- endfor -%} |
Corentin Wallez | f941205 | 2020-04-16 16:39:06 +0000 | [diff] [blame] | 133 | ), (override)); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 134 | {% endfor %} |
| 135 | |
Corentin Wallez | f941205 | 2020-04-16 16:39:06 +0000 | [diff] [blame] | 136 | MOCK_METHOD(void, {{as_MethodSuffix(type.name, Name("reference"))}}, ({{as_cType(type.name)}} self), (override)); |
| 137 | MOCK_METHOD(void, {{as_MethodSuffix(type.name, Name("release"))}}, ({{as_cType(type.name)}} self), (override)); |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 138 | |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 139 | {% for method in type.methods if has_callback_arguments(method) %} |
| 140 | MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "-}} |
| 141 | On{{as_MethodSuffix(type.name, method.name)}}, ( |
| 142 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 143 | {%- for arg in method.arguments -%} |
| 144 | , {{as_annotated_cType(arg)}} |
| 145 | {%- endfor -%} |
| 146 | ), (override)); |
| 147 | {% endfor %} |
| 148 | {% endfor %} |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 149 | }; |
| 150 | |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 151 | #endif // MOCK_{{API}}_H |