blob: 85beb56ec41f464d70e0cbe8cceac48a2d8c18f8 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001//* Copyright 2017 The Dawn Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04002//*
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
fujunwei16ae3b82021-12-15 04:35:26 +000015{% set API = metadata.api.upper() %}
16{% set api = API.lower() %}
17#ifndef MOCK_{{API}}_H
18#define MOCK_{{API}}_H
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040019
fujunwei16ae3b82021-12-15 04:35:26 +000020{% set Prefix = metadata.proc_table_prefix %}
21{% set prefix = Prefix.lower() %}
dan sinclaird0fb4a32022-04-19 14:24:04 +000022#include "dawn/{{prefix}}_proc_table.h"
23#include "dawn/{{api}}.h"
Corentin Wallez96496822019-10-15 11:44:38 +000024#include <gmock/gmock.h>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040025
Corentin Wallez944b60f2017-05-29 11:33:33 -070026#include <memory>
27
Corentin Wallez1b7c5e32017-05-16 22:05:51 +020028// 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 Wallezf07e3bd2017-04-20 14:38:20 -040031class ProcTableAsClass {
32 public:
33 virtual ~ProcTableAsClass();
34
fujunwei16ae3b82021-12-15 04:35:26 +000035 void GetProcTable({{Prefix}}ProcTable* table);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040036
Corentin Wallez1b7c5e32017-05-16 22:05:51 +020037 // 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 Wallezf07e3bd2017-04-20 14:38:20 -040041 {% 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 Eng8d38c012020-12-17 17:59:37 +000046 {% for method in type.methods if not has_callback_arguments(method) %}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040047 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 Eng8d38c012020-12-17 17:59:37 +000054
Corentin Wallez1b7c5e32017-05-16 22:05:51 +020055 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 Eng8d38c012020-12-17 17:59:37 +000057
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.
fujunwei23f71622021-12-02 07:41:21 +000077 {% for callback_arg in method.arguments if callback_arg.type.category == 'function pointer' %}
Austin Eng8d38c012020-12-17 17:59:37 +000078 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 Wallezf07e3bd2017-04-20 14:38:20 -040086 {% endfor %}
87
Corentin Wallez1b7c5e32017-05-16 22:05:51 +020088 struct Object {
89 ProcTableAsClass* procs = nullptr;
Austin Eng8d38c012020-12-17 17:59:37 +000090 {% for type in by_category["object"] %}
91 {% for method in type.methods if has_callback_arguments(method) %}
fujunwei23f71622021-12-02 07:41:21 +000092 {% for callback_arg in method.arguments if callback_arg.type.category == 'function pointer' %}
Austin Eng8d38c012020-12-17 17:59:37 +000093 {{as_cType(callback_arg.type.name)}} m{{as_MethodSuffix(type.name, method.name)}}Callback = nullptr;
94 {% endfor %}
95 {% endfor %}
96 {% endfor %}
Natasha Lee9bba4a92019-12-18 18:59:20 +000097 void* userdata = 0;
Corentin Wallez1b7c5e32017-05-16 22:05:51 +020098 };
99
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400100 private:
Corentin Wallez1b7c5e32017-05-16 22:05:51 +0200101 // Remembers the values returned by GetNew* so they can be freed.
Corentin Wallez42dbde12017-11-23 16:04:26 -0500102 std::vector<std::unique_ptr<Object>> mObjects;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400103};
104
105class MockProcTable : public ProcTableAsClass {
106 public:
Corentin Wallez82b65732018-08-22 15:37:29 +0200107 MockProcTable();
Corentin Wallez3741f732020-04-16 18:08:23 +0000108 ~MockProcTable() override;
Corentin Wallez82b65732018-08-22 15:37:29 +0200109
Corentin Wallezd754fb22019-03-28 10:44:41 +0000110 void IgnoreAllReleaseCalls();
111
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400112 {% for type in by_category["object"] %}
Austin Eng8d38c012020-12-17 17:59:37 +0000113 {% for method in type.methods if not has_callback_arguments(method) %}
Corentin Wallezf9412052020-04-16 16:39:06 +0000114 MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "}}
115 {{-as_MethodSuffix(type.name, method.name)}}, (
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400116 {{-as_cType(type.name)}} {{as_varName(type.name)}}
117 {%- for arg in method.arguments -%}
118 , {{as_annotated_cType(arg)}}
119 {%- endfor -%}
Corentin Wallezf9412052020-04-16 16:39:06 +0000120 ), (override));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400121 {% endfor %}
122
Corentin Wallezf9412052020-04-16 16:39:06 +0000123 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 Wallez1b7c5e32017-05-16 22:05:51 +0200125
Austin Eng8d38c012020-12-17 17:59:37 +0000126 {% 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 Wallezf07e3bd2017-04-20 14:38:20 -0400136};
137
fujunwei16ae3b82021-12-15 04:35:26 +0000138#endif // MOCK_{{API}}_H