blob: 0acfe8cba49a40990243a84cdd2953c67ec45f77 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001//* Copyright 2017 The Dawn & Tint Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04002//*
Austin Engcc2516a2023-10-17 20:57:54 +00003//* Redistribution and use in source and binary forms, with or without
4//* modification, are permitted provided that the following conditions are met:
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04005//*
Austin Engcc2516a2023-10-17 20:57:54 +00006//* 1. Redistributions of source code must retain the above copyright notice, this
7//* list of conditions and the following disclaimer.
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04008//*
Austin Engcc2516a2023-10-17 20:57:54 +00009//* 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 Wallezf07e3bd2017-04-20 14:38:20 -040027
fujunwei16ae3b82021-12-15 04:35:26 +000028{% set API = metadata.api.upper() %}
29{% set api = API.lower() %}
30#ifndef MOCK_{{API}}_H
31#define MOCK_{{API}}_H
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040032
fujunwei16ae3b82021-12-15 04:35:26 +000033{% set Prefix = metadata.proc_table_prefix %}
34{% set prefix = Prefix.lower() %}
dan sinclaird0fb4a32022-04-19 14:24:04 +000035#include "dawn/{{prefix}}_proc_table.h"
36#include "dawn/{{api}}.h"
Corentin Wallez96496822019-10-15 11:44:38 +000037#include <gmock/gmock.h>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040038
Corentin Wallez944b60f2017-05-29 11:33:33 -070039#include <memory>
40
Corentin Wallez1b7c5e32017-05-16 22:05:51 +020041// 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 Wallezf07e3bd2017-04-20 14:38:20 -040044class ProcTableAsClass {
45 public:
46 virtual ~ProcTableAsClass();
47
fujunwei16ae3b82021-12-15 04:35:26 +000048 void GetProcTable({{Prefix}}ProcTable* table);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040049
Corentin Wallez1b7c5e32017-05-16 22:05:51 +020050 // 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 Wallezf07e3bd2017-04-20 14:38:20 -040054 {% 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 Eng8d38c012020-12-17 17:59:37 +000059 {% for method in type.methods if not has_callback_arguments(method) %}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040060 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 Eng8d38c012020-12-17 17:59:37 +000067
Corentin Wallez1b7c5e32017-05-16 22:05:51 +020068 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 Eng8d38c012020-12-17 17:59:37 +000070
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.
fujunwei23f71622021-12-02 07:41:21 +000090 {% for callback_arg in method.arguments if callback_arg.type.category == 'function pointer' %}
Austin Eng8d38c012020-12-17 17:59:37 +000091 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 Wallezf07e3bd2017-04-20 14:38:20 -040099 {% endfor %}
100
Corentin Wallez1b7c5e32017-05-16 22:05:51 +0200101 struct Object {
102 ProcTableAsClass* procs = nullptr;
Austin Eng8d38c012020-12-17 17:59:37 +0000103 {% for type in by_category["object"] %}
104 {% for method in type.methods if has_callback_arguments(method) %}
fujunwei23f71622021-12-02 07:41:21 +0000105 {% for callback_arg in method.arguments if callback_arg.type.category == 'function pointer' %}
Austin Eng8d38c012020-12-17 17:59:37 +0000106 {{as_cType(callback_arg.type.name)}} m{{as_MethodSuffix(type.name, method.name)}}Callback = nullptr;
107 {% endfor %}
108 {% endfor %}
109 {% endfor %}
Natasha Lee9bba4a92019-12-18 18:59:20 +0000110 void* userdata = 0;
Corentin Wallez1b7c5e32017-05-16 22:05:51 +0200111 };
112
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400113 private:
Corentin Wallez1b7c5e32017-05-16 22:05:51 +0200114 // Remembers the values returned by GetNew* so they can be freed.
Corentin Wallez42dbde12017-11-23 16:04:26 -0500115 std::vector<std::unique_ptr<Object>> mObjects;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400116};
117
118class MockProcTable : public ProcTableAsClass {
119 public:
Corentin Wallez82b65732018-08-22 15:37:29 +0200120 MockProcTable();
Corentin Wallez3741f732020-04-16 18:08:23 +0000121 ~MockProcTable() override;
Corentin Wallez82b65732018-08-22 15:37:29 +0200122
Corentin Wallezd754fb22019-03-28 10:44:41 +0000123 void IgnoreAllReleaseCalls();
124
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400125 {% for type in by_category["object"] %}
Austin Eng8d38c012020-12-17 17:59:37 +0000126 {% for method in type.methods if not has_callback_arguments(method) %}
Corentin Wallezf9412052020-04-16 16:39:06 +0000127 MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "}}
128 {{-as_MethodSuffix(type.name, method.name)}}, (
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400129 {{-as_cType(type.name)}} {{as_varName(type.name)}}
130 {%- for arg in method.arguments -%}
131 , {{as_annotated_cType(arg)}}
132 {%- endfor -%}
Corentin Wallezf9412052020-04-16 16:39:06 +0000133 ), (override));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400134 {% endfor %}
135
Corentin Wallezf9412052020-04-16 16:39:06 +0000136 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 Wallez1b7c5e32017-05-16 22:05:51 +0200138
Austin Eng8d38c012020-12-17 17:59:37 +0000139 {% 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 Wallezf07e3bd2017-04-20 14:38:20 -0400149};
150
fujunwei16ae3b82021-12-15 04:35:26 +0000151#endif // MOCK_{{API}}_H