Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | //* Copyright 2017 The Dawn Authors |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 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 | |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 15 | {% set API = metadata.api.upper() %} |
| 16 | {% set api = API.lower() %} |
| 17 | #ifndef MOCK_{{API}}_H |
| 18 | #define MOCK_{{API}}_H |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 19 | |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 20 | {% set Prefix = metadata.proc_table_prefix %} |
| 21 | {% set prefix = Prefix.lower() %} |
dan sinclair | d0fb4a3 | 2022-04-19 14:24:04 +0000 | [diff] [blame] | 22 | #include "dawn/{{prefix}}_proc_table.h" |
| 23 | #include "dawn/{{api}}.h" |
Corentin Wallez | 9649682 | 2019-10-15 11:44:38 +0000 | [diff] [blame] | 24 | #include <gmock/gmock.h> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 25 | |
Corentin Wallez | 944b60f | 2017-05-29 11:33:33 -0700 | [diff] [blame] | 26 | #include <memory> |
| 27 | |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 28 | // An abstract base class representing a proc table so that API calls can be mocked. Most API calls |
| 29 | // are directly represented by a delete virtual method but others need minimal state tracking to be |
| 30 | // useful as mocks. |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 31 | class ProcTableAsClass { |
| 32 | public: |
| 33 | virtual ~ProcTableAsClass(); |
| 34 | |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 35 | void GetProcTable({{Prefix}}ProcTable* table); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 36 | |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 37 | // Creates an object that can be returned by a mocked call as in WillOnce(Return(foo)). |
| 38 | // It returns an object of the write type that isn't equal to any previously returned object. |
| 39 | // Otherwise some mock expectation could be triggered by two different objects having the same |
| 40 | // value. |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 41 | {% for type in by_category["object"] %} |
| 42 | {{as_cType(type.name)}} GetNew{{type.name.CamelCase()}}(); |
| 43 | {% endfor %} |
| 44 | |
| 45 | {% for type in by_category["object"] %} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 46 | {% for method in type.methods if not has_callback_arguments(method) %} |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 47 | virtual {{as_cType(method.return_type.name)}} {{as_MethodSuffix(type.name, method.name)}}( |
| 48 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 49 | {%- for arg in method.arguments -%} |
| 50 | , {{as_annotated_cType(arg)}} |
| 51 | {%- endfor -%} |
| 52 | ) = 0; |
| 53 | {% endfor %} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 54 | |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 55 | virtual void {{as_MethodSuffix(type.name, Name("reference"))}}({{as_cType(type.name)}} self) = 0; |
| 56 | 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] | 57 | |
| 58 | {% for method in type.methods if has_callback_arguments(method) %} |
| 59 | {% set Suffix = as_MethodSuffix(type.name, method.name) %} |
| 60 | //* Stores callback and userdata and calls the On* method. |
| 61 | {{as_cType(method.return_type.name)}} {{Suffix}}( |
| 62 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 63 | {%- for arg in method.arguments -%} |
| 64 | , {{as_annotated_cType(arg)}} |
| 65 | {%- endfor -%} |
| 66 | ); |
| 67 | //* The virtual function to call after saving the callback and userdata in the proc. |
| 68 | //* This function can be mocked. |
| 69 | virtual {{as_cType(method.return_type.name)}} On{{Suffix}}( |
| 70 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 71 | {%- for arg in method.arguments -%} |
| 72 | , {{as_annotated_cType(arg)}} |
| 73 | {%- endfor -%} |
| 74 | ) = 0; |
| 75 | |
| 76 | //* Calls the stored callback. |
fujunwei | 23f7162 | 2021-12-02 07:41:21 +0000 | [diff] [blame] | 77 | {% 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] | 78 | void Call{{as_MethodSuffix(type.name, method.name)}}Callback( |
| 79 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 80 | {%- for arg in callback_arg.type.arguments -%} |
| 81 | {%- if not loop.last -%}, {{as_annotated_cType(arg)}}{%- endif -%} |
| 82 | {%- endfor -%} |
| 83 | ); |
| 84 | {% endfor %} |
| 85 | {% endfor %} |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 86 | {% endfor %} |
| 87 | |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 88 | struct Object { |
| 89 | ProcTableAsClass* procs = nullptr; |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 90 | {% for type in by_category["object"] %} |
| 91 | {% for method in type.methods if has_callback_arguments(method) %} |
fujunwei | 23f7162 | 2021-12-02 07:41:21 +0000 | [diff] [blame] | 92 | {% 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] | 93 | {{as_cType(callback_arg.type.name)}} m{{as_MethodSuffix(type.name, method.name)}}Callback = nullptr; |
| 94 | {% endfor %} |
| 95 | {% endfor %} |
| 96 | {% endfor %} |
Natasha Lee | 9bba4a9 | 2019-12-18 18:59:20 +0000 | [diff] [blame] | 97 | void* userdata = 0; |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 100 | private: |
Corentin Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 101 | // Remembers the values returned by GetNew* so they can be freed. |
Corentin Wallez | 42dbde1 | 2017-11-23 16:04:26 -0500 | [diff] [blame] | 102 | std::vector<std::unique_ptr<Object>> mObjects; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | class MockProcTable : public ProcTableAsClass { |
| 106 | public: |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 107 | MockProcTable(); |
Corentin Wallez | 3741f73 | 2020-04-16 18:08:23 +0000 | [diff] [blame] | 108 | ~MockProcTable() override; |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 109 | |
Corentin Wallez | d754fb2 | 2019-03-28 10:44:41 +0000 | [diff] [blame] | 110 | void IgnoreAllReleaseCalls(); |
| 111 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 112 | {% for type in by_category["object"] %} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 113 | {% for method in type.methods if not has_callback_arguments(method) %} |
Corentin Wallez | f941205 | 2020-04-16 16:39:06 +0000 | [diff] [blame] | 114 | MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "}} |
| 115 | {{-as_MethodSuffix(type.name, method.name)}}, ( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 116 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 117 | {%- for arg in method.arguments -%} |
| 118 | , {{as_annotated_cType(arg)}} |
| 119 | {%- endfor -%} |
Corentin Wallez | f941205 | 2020-04-16 16:39:06 +0000 | [diff] [blame] | 120 | ), (override)); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 121 | {% endfor %} |
| 122 | |
Corentin Wallez | f941205 | 2020-04-16 16:39:06 +0000 | [diff] [blame] | 123 | MOCK_METHOD(void, {{as_MethodSuffix(type.name, Name("reference"))}}, ({{as_cType(type.name)}} self), (override)); |
| 124 | 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] | 125 | |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 126 | {% for method in type.methods if has_callback_arguments(method) %} |
| 127 | MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "-}} |
| 128 | On{{as_MethodSuffix(type.name, method.name)}}, ( |
| 129 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
| 130 | {%- for arg in method.arguments -%} |
| 131 | , {{as_annotated_cType(arg)}} |
| 132 | {%- endfor -%} |
| 133 | ), (override)); |
| 134 | {% endfor %} |
| 135 | {% endfor %} |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 136 | }; |
| 137 | |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 138 | #endif // MOCK_{{API}}_H |