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.
|
|
|
|
|
2019-10-28 22:15:47 +00:00
|
|
|
#ifndef MOCK_WEBGPU_H
|
|
|
|
#define MOCK_WEBGPU_H
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-15 11:44:38 +00:00
|
|
|
#include <dawn/dawn_proc_table.h>
|
2019-10-28 22:15:47 +00:00
|
|
|
#include <dawn/webgpu.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();
|
|
|
|
|
2019-10-28 22:15:47 +00:00
|
|
|
void GetProcTableAndDevice(DawnProcTable* table, WGPUDevice* device);
|
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"] %}
|
2019-11-22 13:18:22 +00:00
|
|
|
{% for method in type.methods if len(method.arguments) < 10 and 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 %}
|
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
|
|
|
{% endfor %}
|
|
|
|
|
2017-06-09 14:51:29 +00:00
|
|
|
// Stores callback and userdata and calls the On* methods
|
2019-10-28 22:15:47 +00:00
|
|
|
void DeviceSetUncapturedErrorCallback(WGPUDevice self,
|
|
|
|
WGPUErrorCallback callback,
|
2019-05-29 13:03:50 +00:00
|
|
|
void* userdata);
|
2019-12-18 18:59:20 +00:00
|
|
|
void DeviceSetDeviceLostCallback(WGPUDevice self,
|
|
|
|
WGPUDeviceLostCallback callback,
|
|
|
|
void* userdata);
|
2019-10-28 22:15:47 +00:00
|
|
|
bool DevicePopErrorScope(WGPUDevice self, WGPUErrorCallback callback, void* userdata);
|
|
|
|
void DeviceCreateBufferMappedAsync(WGPUDevice self,
|
|
|
|
const WGPUBufferDescriptor* descriptor,
|
|
|
|
WGPUBufferCreateMappedCallback callback,
|
2019-06-14 17:35:56 +00:00
|
|
|
void* userdata);
|
2019-10-28 22:15:47 +00:00
|
|
|
void BufferMapReadAsync(WGPUBuffer self,
|
|
|
|
WGPUBufferMapReadCallback callback,
|
2019-05-29 13:03:50 +00:00
|
|
|
void* userdata);
|
2019-10-28 22:15:47 +00:00
|
|
|
void BufferMapWriteAsync(WGPUBuffer self,
|
|
|
|
WGPUBufferMapWriteCallback callback,
|
2019-05-29 13:03:50 +00:00
|
|
|
void* userdata);
|
2019-10-28 22:15:47 +00:00
|
|
|
void FenceOnCompletion(WGPUFence self,
|
2018-12-03 16:57:34 +00:00
|
|
|
uint64_t value,
|
2019-10-28 22:15:47 +00:00
|
|
|
WGPUFenceOnCompletionCallback callback,
|
2019-05-29 13:03:50 +00:00
|
|
|
void* userdata);
|
2017-05-16 20:05:51 +00:00
|
|
|
|
|
|
|
// Special cased mockable methods
|
2019-10-28 22:15:47 +00:00
|
|
|
virtual void OnDeviceSetUncapturedErrorCallback(WGPUDevice device,
|
|
|
|
WGPUErrorCallback callback,
|
2019-05-29 13:03:50 +00:00
|
|
|
void* userdata) = 0;
|
2019-12-18 18:59:20 +00:00
|
|
|
virtual void OnDeviceSetDeviceLostCallback(WGPUDevice device,
|
|
|
|
WGPUDeviceLostCallback callback,
|
|
|
|
void* userdata) = 0;
|
2019-10-28 22:15:47 +00:00
|
|
|
virtual bool OnDevicePopErrorScopeCallback(WGPUDevice device,
|
|
|
|
WGPUErrorCallback callback,
|
2019-09-04 22:54:03 +00:00
|
|
|
void* userdata) = 0;
|
2019-10-28 22:15:47 +00:00
|
|
|
virtual void OnDeviceCreateBufferMappedAsyncCallback(WGPUDevice self,
|
|
|
|
const WGPUBufferDescriptor* descriptor,
|
|
|
|
WGPUBufferCreateMappedCallback callback,
|
2019-06-14 17:35:56 +00:00
|
|
|
void* userdata) = 0;
|
2019-10-28 22:15:47 +00:00
|
|
|
virtual void OnBufferMapReadAsyncCallback(WGPUBuffer buffer,
|
|
|
|
WGPUBufferMapReadCallback callback,
|
2019-05-29 13:03:50 +00:00
|
|
|
void* userdata) = 0;
|
2019-10-28 22:15:47 +00:00
|
|
|
virtual void OnBufferMapWriteAsyncCallback(WGPUBuffer buffer,
|
|
|
|
WGPUBufferMapWriteCallback callback,
|
2019-05-29 13:03:50 +00:00
|
|
|
void* userdata) = 0;
|
2019-10-28 22:15:47 +00:00
|
|
|
virtual void OnFenceOnCompletionCallback(WGPUFence fence,
|
2018-12-03 16:57:34 +00:00
|
|
|
uint64_t value,
|
2019-10-28 22:15:47 +00:00
|
|
|
WGPUFenceOnCompletionCallback callback,
|
2019-05-29 13:03:50 +00:00
|
|
|
void* userdata) = 0;
|
2017-05-16 20:05:51 +00:00
|
|
|
|
2017-05-17 15:16:00 +00:00
|
|
|
// Calls the stored callbacks
|
2019-10-28 22:15:47 +00:00
|
|
|
void CallDeviceErrorCallback(WGPUDevice device, WGPUErrorType type, const char* message);
|
2019-12-18 18:59:20 +00:00
|
|
|
void CallDeviceLostCallback(WGPUDevice device, const char* message);
|
2019-10-28 22:15:47 +00:00
|
|
|
void CallCreateBufferMappedCallback(WGPUDevice device, WGPUBufferMapAsyncStatus status, WGPUCreateBufferMappedResult result);
|
|
|
|
void CallMapReadCallback(WGPUBuffer buffer, WGPUBufferMapAsyncStatus status, const void* data, uint64_t dataLength);
|
|
|
|
void CallMapWriteCallback(WGPUBuffer buffer, WGPUBufferMapAsyncStatus status, void* data, uint64_t dataLength);
|
|
|
|
void CallFenceOnCompletionCallback(WGPUFence fence, WGPUFenceCompletionStatus status);
|
2017-05-17 15:16:00 +00:00
|
|
|
|
2017-05-16 20:05:51 +00:00
|
|
|
struct Object {
|
|
|
|
ProcTableAsClass* procs = nullptr;
|
2019-10-28 22:15:47 +00:00
|
|
|
WGPUErrorCallback deviceErrorCallback = nullptr;
|
2019-12-18 18:59:20 +00:00
|
|
|
WGPUDeviceLostCallback deviceLostCallback = nullptr;
|
2019-10-28 22:15:47 +00:00
|
|
|
WGPUBufferCreateMappedCallback createBufferMappedCallback = nullptr;
|
|
|
|
WGPUBufferMapReadCallback mapReadCallback = nullptr;
|
|
|
|
WGPUBufferMapWriteCallback mapWriteCallback = nullptr;
|
|
|
|
WGPUFenceOnCompletionCallback fenceOnCompletionCallback = nullptr;
|
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();
|
|
|
|
|
2019-03-28 10:44:41 +00:00
|
|
|
void IgnoreAllReleaseCalls();
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
{% for type in by_category["object"] %}
|
2019-11-22 13:18:22 +00:00
|
|
|
{% for method in type.methods if len(method.arguments) < 10 and not has_callback_arguments(method) %}
|
2017-04-20 18:38:20 +00:00
|
|
|
MOCK_METHOD{{len(method.arguments) + 1}}(
|
|
|
|
{{-as_MethodSuffix(type.name, method.name)}},
|
|
|
|
{{as_cType(method.return_type.name)}}(
|
|
|
|
{{-as_cType(type.name)}} {{as_varName(type.name)}}
|
|
|
|
{%- for arg in method.arguments -%}
|
|
|
|
, {{as_annotated_cType(arg)}}
|
|
|
|
{%- endfor -%}
|
|
|
|
));
|
|
|
|
{% endfor %}
|
|
|
|
|
2017-05-16 20:05:51 +00:00
|
|
|
MOCK_METHOD1({{as_MethodSuffix(type.name, Name("reference"))}}, void({{as_cType(type.name)}} self));
|
|
|
|
MOCK_METHOD1({{as_MethodSuffix(type.name, Name("release"))}}, void({{as_cType(type.name)}} self));
|
2017-04-20 18:38:20 +00:00
|
|
|
{% endfor %}
|
2017-05-16 20:05:51 +00:00
|
|
|
|
2019-10-28 22:15:47 +00:00
|
|
|
MOCK_METHOD3(OnDeviceSetUncapturedErrorCallback, void(WGPUDevice device, WGPUErrorCallback callback, void* userdata));
|
2019-12-18 18:59:20 +00:00
|
|
|
MOCK_METHOD3(OnDeviceSetDeviceLostCallback,
|
|
|
|
void(WGPUDevice device, WGPUDeviceLostCallback callback, void* userdata));
|
2019-10-28 22:15:47 +00:00
|
|
|
MOCK_METHOD3(OnDevicePopErrorScopeCallback, bool(WGPUDevice device, WGPUErrorCallback callback, void* userdata));
|
|
|
|
MOCK_METHOD4(OnDeviceCreateBufferMappedAsyncCallback, void(WGPUDevice device, const WGPUBufferDescriptor* descriptor, WGPUBufferCreateMappedCallback callback, void* userdata));
|
|
|
|
MOCK_METHOD3(OnBufferMapReadAsyncCallback, void(WGPUBuffer buffer, WGPUBufferMapReadCallback callback, void* userdata));
|
|
|
|
MOCK_METHOD3(OnBufferMapWriteAsyncCallback, void(WGPUBuffer buffer, WGPUBufferMapWriteCallback callback, void* userdata));
|
2018-12-03 16:57:34 +00:00
|
|
|
MOCK_METHOD4(OnFenceOnCompletionCallback,
|
2019-10-28 22:15:47 +00:00
|
|
|
void(WGPUFence fence,
|
2018-12-03 16:57:34 +00:00
|
|
|
uint64_t value,
|
2019-10-28 22:15:47 +00:00
|
|
|
WGPUFenceOnCompletionCallback callback,
|
2019-05-29 13:03:50 +00:00
|
|
|
void* userdata));
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
2019-10-28 22:15:47 +00:00
|
|
|
#endif // MOCK_WEBGPU_H
|