#include "dawn/tests/unittests/wire/WireTest.h"
#include "dawn/dawn_proc.h"
#include "dawn/tests/MockCallback.h"
#include "dawn/utils/TerribleCommandBuffer.h"
#include "dawn/wire/WireClient.h"
#include "dawn/wire/WireServer.h"
_;
AnyNumber;
AtMost;
Exactly;
Invoke;
Mock;
MockCallback;
NotNull;
Return;
SaveArg;
WithArg;
WireTest::WireTest() {}
WireTest::~WireTest() {}
dawn::wire::client::MemoryTransferService* WireTest::GetClientMemoryTransferService() {
return nullptr;
}
dawn::wire::server::MemoryTransferService* WireTest::GetServerMemoryTransferService() {
return nullptr;
}
void WireTest::SetUp() {
DawnProcTable mockProcs;
api.GetProcTable(&mockProcs);
SetupIgnoredCallExpectations();
mS2cBuf = std::make_unique<dawn::utils::TerribleCommandBuffer>();
mC2sBuf = std::make_unique<dawn::utils::TerribleCommandBuffer>(mWireServer.get());
dawn::wire::WireServerDescriptor serverDesc = {};
serverDesc.procs = &mockProcs;
serverDesc.serializer = mS2cBuf.get();
serverDesc.memoryTransferService = GetServerMemoryTransferService();
mWireServer.reset(new dawn::wire::WireServer(serverDesc));
mC2sBuf->SetHandler(mWireServer.get());
dawn::wire::WireClientDescriptor clientDesc = {};
clientDesc.serializer = mC2sBuf.get();
clientDesc.memoryTransferService = GetClientMemoryTransferService();
mWireClient.reset(new dawn::wire::WireClient(clientDesc));
mS2cBuf->SetHandler(mWireClient.get());
dawnProcSetProcs(&dawn::wire::client::GetProcs());
auto reservedInstance = GetWireClient()->ReserveInstance();
instance = wgpu::Instance::Acquire(reservedInstance.instance);
apiInstance = api.GetNewInstance();
EXPECT_CALL(…);
EXPECT_TRUE(…);
apiAdapter = api.GetNewAdapter();
MockCallback<void (*)(wgpu::RequestAdapterStatus, wgpu::Adapter, const char*, void*)> adapterCb;
instance.RequestAdapter(nullptr, wgpu::CallbackMode::AllowSpontaneous, adapterCb.Callback(),
adapterCb.MakeUserdata(this));
EXPECT_CALL(…).WillOnce([&]() {
EXPECT_CALL(…).WillRepeatedly(Return(false));
EXPECT_CALL(…)
.WillOnce(WithArg<1>(Invoke([&](WGPUAdapterInfo* info) {
*info = {};
info->vendor = "";
info->architecture = "";
info->device = "";
info->description = "";
return WGPUStatus_Success;
})));
EXPECT_CALL(…)
.WillOnce(WithArg<1>(Invoke([&](WGPUSupportedLimits* limits) {
*limits = {};
return WGPUStatus_Success;
})));
EXPECT_CALL(…)
.WillOnce(Return(0))
.WillOnce(Return(0));
api.CallInstanceRequestAdapter2Callback(apiInstance, WGPURequestAdapterStatus_Success,
apiAdapter, nullptr);
});
FlushClient();
EXPECT_CALL(…)
.WillOnce(SaveArg<1>(&adapter));
FlushServer();
EXPECT_NE(…);
apiDevice = api.GetNewDevice();
wgpu::DeviceDescriptor deviceDesc = {};
deviceDesc.deviceLostCallbackInfo = {nullptr, wgpu::CallbackMode::AllowSpontaneous,
deviceLostCallback.Callback(),
deviceLostCallback.MakeUserdata(this)};
deviceDesc.uncapturedErrorCallbackInfo = {nullptr, uncapturedErrorCallback.Callback(),
uncapturedErrorCallback.MakeUserdata(this)};
EXPECT_CALL(…).Times(AtMost(1));
MockCallback<void (*)(wgpu::RequestDeviceStatus, wgpu::Device, const char*, void*)> deviceCb;
adapter.RequestDevice(&deviceDesc, wgpu::CallbackMode::AllowSpontaneous, deviceCb.Callback(),
deviceCb.MakeUserdata(this));
EXPECT_CALL(…)
.WillOnce(WithArg<1>([&](const WGPUDeviceDescriptor* desc) {
EXPECT_CALL(…).Times(1);
ProcTableAsClass::Object* object =
reinterpret_cast<ProcTableAsClass::Object*>(apiDevice);
object->mDeviceLostCallback = desc->deviceLostCallbackInfo2.callback;
object->mDeviceLostUserdata1 = desc->deviceLostCallbackInfo2.userdata1;
object->mDeviceLostUserdata2 = desc->deviceLostCallbackInfo2.userdata2;
object->mUncapturedErrorCallback = desc->uncapturedErrorCallbackInfo2.callback;
object->mUncapturedErrorUserdata1 = desc->uncapturedErrorCallbackInfo2.userdata1;
object->mUncapturedErrorUserdata2 = desc->uncapturedErrorCallbackInfo2.userdata2;
EXPECT_CALL(…)
.WillOnce(WithArg<1>(Invoke([&](WGPUSupportedLimits* limits) {
*limits = {};
return WGPUStatus_Success;
})));
EXPECT_CALL(…)
.WillOnce(Return(0))
.WillOnce(Return(0));
api.CallAdapterRequestDevice2Callback(apiAdapter, WGPURequestDeviceStatus_Success,
apiDevice, nullptr);
}));
FlushClient();
EXPECT_CALL(…)
.WillOnce(SaveArg<1>(&device));
FlushServer();
EXPECT_NE(…);
queue = device.GetQueue();
apiQueue = api.GetNewQueue();
EXPECT_CALL(…).WillOnce(Return(apiQueue));
FlushClient();
cDevice = device.Get();
cQueue = queue.Get();
}
void WireTest::TearDown() {
instance = nullptr;
adapter = nullptr;
device = nullptr;
queue = nullptr;
dawnProcSetProcs(nullptr);
api.IgnoreAllReleaseCalls();
mS2cBuf->SetHandler(nullptr);
mWireClient = nullptr;
if (mWireServer && apiDevice) {
EXPECT_CALL(…).Times(Exactly(1));
}
mC2sBuf->SetHandler(nullptr);
mWireServer = nullptr;
}
void WireTest::DefaultApiDeviceWasReleased() {
apiDevice = nullptr;
}
void WireTest::DefaultApiAdapterWasReleased() {
apiAdapter = nullptr;
}
void WireTest::FlushClient(bool success) {
ASSERT_EQ(…);
Mock::VerifyAndClearExpectations(&api);
SetupIgnoredCallExpectations();
}
void WireTest::FlushServer(bool success) {
ASSERT_EQ(…);
}
dawn::wire::WireServer* WireTest::GetWireServer() {
return mWireServer.get();
}
dawn::wire::WireClient* WireTest::GetWireClient() {
return mWireClient.get();
}
void WireTest::DeleteServer() {
EXPECT_CALL(…).Times(1);
EXPECT_CALL(…).Times(1);
EXPECT_CALL(…).Times(1);
EXPECT_CALL(…).Times(1);
if (mWireServer) {
EXPECT_CALL(…).Times(Exactly(1));
}
mC2sBuf->SetHandler(nullptr);
mWireServer = nullptr;
}
void WireTest::DeleteClient() {
mS2cBuf->SetHandler(nullptr);
mWireClient = nullptr;
}
void WireTest::SetupIgnoredCallExpectations() {
EXPECT_CALL(…).Times(AnyNumber());
EXPECT_CALL(…).Times(AnyNumber());
}