2018-07-18 09:40:26 +00:00
|
|
|
//* Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//*
|
|
|
|
//* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
//* you may not use this file except in compliance with the License.
|
|
|
|
//* You may obtain a copy of the License at
|
|
|
|
//*
|
|
|
|
//* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//*
|
|
|
|
//* Unless required by applicable law or agreed to in writing, software
|
|
|
|
//* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
//* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//* See the License for the specific language governing permissions and
|
|
|
|
//* limitations under the License.
|
|
|
|
|
2021-12-15 04:35:26 +00:00
|
|
|
{% set API = metadata.api.upper() %}
|
|
|
|
{% set api = API.lower() %}
|
|
|
|
#ifndef MOCK_{{API}}_H
|
|
|
|
#define MOCK_{{API}}_H
|
|
|
|
|
|
|
|
{% set Prefix = metadata.proc_table_prefix %}
|
|
|
|
{% set prefix = Prefix.lower() %}
|
|
|
|
#include <dawn/{{prefix}}_proc_table.h>
|
|
|
|
#include <dawn/{{api}}.h>
|
2019-10-15 11:44:38 +00:00
|
|
|
#include <gmock/gmock.h>
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-05-29 18:33:33 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2017-05-16 20:05:51 +00:00
|
|
|
// An abstract base class representing a proc table so that API calls can be mocked. Most API calls
|
|
|
|
// are directly represented by a delete virtual method but others need minimal state tracking to be
|
|
|
|
// useful as mocks.
|
2017-04-20 18:38:20 +00:00
|
|
|
class ProcTableAsClass {
|
|
|
|
public:
|
|
|
|
virtual ~ProcTableAsClass();
|
|
|
|
|
2021-12-15 04:35:26 +00:00
|
|
|
void GetProcTable({{Prefix}}ProcTable* table);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-05-16 20:05:51 +00:00
|
|
|
// Creates an object that can be returned by a mocked call as in WillOnce(Return(foo)).
|
|
|
|
// It returns an object of the write type that isn't equal to any previously returned object.
|
|
|
|
// Otherwise some mock expectation could be triggered by two different objects having the same
|
|
|
|
// value.
|
2017-04-20 18:38:20 +00:00
|
|
|
{% for type in by_category["object"] %}
|
|
|
|
{{as_cType(type.name)}} GetNew{{type.name.CamelCase()}}();
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
{% for type in by_category["object"] %}
|
2020-12-17 17:59:37 +00:00
|
|
|
{% for method in type.methods if not has_callback_arguments(method) %}
|
2017-04-20 18:38:20 +00:00
|
|
|
virtual {{as_cType(method.return_type.name)}} {{as_MethodSuffix(type.name, method.name)}}(
|
|
|
|
{{-as_cType(type.name)}} {{as_varName(type.name)}}
|
|
|
|
{%- for arg in method.arguments -%}
|
|
|
|
, {{as_annotated_cType(arg)}}
|
|
|
|
{%- endfor -%}
|
|
|
|
) = 0;
|
|
|
|
{% endfor %}
|
2020-12-17 17:59:37 +00:00
|
|
|
|
2017-05-16 20:05:51 +00:00
|
|
|
virtual void {{as_MethodSuffix(type.name, Name("reference"))}}({{as_cType(type.name)}} self) = 0;
|
|
|
|
virtual void {{as_MethodSuffix(type.name, Name("release"))}}({{as_cType(type.name)}} self) = 0;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-12-17 17:59:37 +00:00
|
|
|
{% for method in type.methods if has_callback_arguments(method) %}
|
|
|
|
{% set Suffix = as_MethodSuffix(type.name, method.name) %}
|
|
|
|
//* Stores callback and userdata and calls the On* method.
|
|
|
|
{{as_cType(method.return_type.name)}} {{Suffix}}(
|
|
|
|
{{-as_cType(type.name)}} {{as_varName(type.name)}}
|
|
|
|
{%- for arg in method.arguments -%}
|
|
|
|
, {{as_annotated_cType(arg)}}
|
|
|
|
{%- endfor -%}
|
|
|
|
);
|
|
|
|
//* The virtual function to call after saving the callback and userdata in the proc.
|
|
|
|
//* This function can be mocked.
|
|
|
|
virtual {{as_cType(method.return_type.name)}} On{{Suffix}}(
|
|
|
|
{{-as_cType(type.name)}} {{as_varName(type.name)}}
|
|
|
|
{%- for arg in method.arguments -%}
|
|
|
|
, {{as_annotated_cType(arg)}}
|
|
|
|
{%- endfor -%}
|
|
|
|
) = 0;
|
|
|
|
|
|
|
|
//* Calls the stored callback.
|
2021-12-02 07:41:21 +00:00
|
|
|
{% for callback_arg in method.arguments if callback_arg.type.category == 'function pointer' %}
|
2020-12-17 17:59:37 +00:00
|
|
|
void Call{{as_MethodSuffix(type.name, method.name)}}Callback(
|
|
|
|
{{-as_cType(type.name)}} {{as_varName(type.name)}}
|
|
|
|
{%- for arg in callback_arg.type.arguments -%}
|
|
|
|
{%- if not loop.last -%}, {{as_annotated_cType(arg)}}{%- endif -%}
|
|
|
|
{%- endfor -%}
|
|
|
|
);
|
|
|
|
{% endfor %}
|
|
|
|
{% endfor %}
|
|
|
|
{% endfor %}
|
2017-05-17 15:16:00 +00:00
|
|
|
|
2017-05-16 20:05:51 +00:00
|
|
|
struct Object {
|
|
|
|
ProcTableAsClass* procs = nullptr;
|
2020-12-17 17:59:37 +00:00
|
|
|
{% for type in by_category["object"] %}
|
|
|
|
{% for method in type.methods if has_callback_arguments(method) %}
|
2021-12-02 07:41:21 +00:00
|
|
|
{% for callback_arg in method.arguments if callback_arg.type.category == 'function pointer' %}
|
2020-12-17 17:59:37 +00:00
|
|
|
{{as_cType(callback_arg.type.name)}} m{{as_MethodSuffix(type.name, method.name)}}Callback = nullptr;
|
|
|
|
{% endfor %}
|
|
|
|
{% endfor %}
|
|
|
|
{% endfor %}
|
2019-12-18 18:59:20 +00:00
|
|
|
void* userdata = 0;
|
2017-05-16 20:05:51 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
private:
|
2017-05-16 20:05:51 +00:00
|
|
|
// Remembers the values returned by GetNew* so they can be freed.
|
2017-11-23 21:04:26 +00:00
|
|
|
std::vector<std::unique_ptr<Object>> mObjects;
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class MockProcTable : public ProcTableAsClass {
|
|
|
|
public:
|
2018-08-22 13:37:29 +00:00
|
|
|
MockProcTable();
|
2020-04-16 18:08:23 +00:00
|
|
|
~MockProcTable() override;
|
2018-08-22 13:37:29 +00:00
|
|
|
|
2019-03-28 10:44:41 +00:00
|
|
|
void IgnoreAllReleaseCalls();
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
{% for type in by_category["object"] %}
|
2020-12-17 17:59:37 +00:00
|
|
|
{% for method in type.methods if not has_callback_arguments(method) %}
|
2020-04-16 16:39:06 +00:00
|
|
|
MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "}}
|
|
|
|
{{-as_MethodSuffix(type.name, method.name)}}, (
|
2017-04-20 18:38:20 +00:00
|
|
|
{{-as_cType(type.name)}} {{as_varName(type.name)}}
|
|
|
|
{%- for arg in method.arguments -%}
|
|
|
|
, {{as_annotated_cType(arg)}}
|
|
|
|
{%- endfor -%}
|
2020-04-16 16:39:06 +00:00
|
|
|
), (override));
|
2017-04-20 18:38:20 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
2020-04-16 16:39:06 +00:00
|
|
|
MOCK_METHOD(void, {{as_MethodSuffix(type.name, Name("reference"))}}, ({{as_cType(type.name)}} self), (override));
|
|
|
|
MOCK_METHOD(void, {{as_MethodSuffix(type.name, Name("release"))}}, ({{as_cType(type.name)}} self), (override));
|
2017-05-16 20:05:51 +00:00
|
|
|
|
2020-12-17 17:59:37 +00:00
|
|
|
{% for method in type.methods if has_callback_arguments(method) %}
|
|
|
|
MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "-}}
|
|
|
|
On{{as_MethodSuffix(type.name, method.name)}}, (
|
|
|
|
{{-as_cType(type.name)}} {{as_varName(type.name)}}
|
|
|
|
{%- for arg in method.arguments -%}
|
|
|
|
, {{as_annotated_cType(arg)}}
|
|
|
|
{%- endfor -%}
|
|
|
|
), (override));
|
|
|
|
{% endfor %}
|
|
|
|
{% endfor %}
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
2021-12-15 04:35:26 +00:00
|
|
|
#endif // MOCK_{{API}}_H
|