1 // Copyright 2019 The Dawn Authors
2 //
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
15 #include "tests/unittests/wire/WireTest.h"
16
17 #include "dawn/dawn_proc.h"
18 #include "dawn_wire/WireClient.h"
19 #include "dawn_wire/WireServer.h"
20 #include "utils/TerribleCommandBuffer.h"
21
22 using namespace testing;
23 using namespace dawn_wire;
24
WireTest()25 WireTest::WireTest() {
26 }
27
~WireTest()28 WireTest::~WireTest() {
29 }
30
GetClientMemoryTransferService()31 client::MemoryTransferService* WireTest::GetClientMemoryTransferService() {
32 return nullptr;
33 }
34
GetServerMemoryTransferService()35 server::MemoryTransferService* WireTest::GetServerMemoryTransferService() {
36 return nullptr;
37 }
38
SetUp()39 void WireTest::SetUp() {
40 DawnProcTable mockProcs;
41 WGPUDevice mockDevice;
42 api.GetProcTableAndDevice(&mockProcs, &mockDevice);
43
44 // This SetCallback call cannot be ignored because it is done as soon as we start the server
45 EXPECT_CALL(api, OnDeviceSetUncapturedErrorCallback(_, _, _)).Times(Exactly(1));
46 EXPECT_CALL(api, OnDeviceSetLoggingCallback(_, _, _)).Times(Exactly(1));
47 EXPECT_CALL(api, OnDeviceSetDeviceLostCallback(_, _, _)).Times(Exactly(1));
48 SetupIgnoredCallExpectations();
49
50 mS2cBuf = std::make_unique<utils::TerribleCommandBuffer>();
51 mC2sBuf = std::make_unique<utils::TerribleCommandBuffer>(mWireServer.get());
52
53 WireServerDescriptor serverDesc = {};
54 serverDesc.procs = &mockProcs;
55 serverDesc.serializer = mS2cBuf.get();
56 serverDesc.memoryTransferService = GetServerMemoryTransferService();
57
58 mWireServer.reset(new WireServer(serverDesc));
59 mC2sBuf->SetHandler(mWireServer.get());
60
61 WireClientDescriptor clientDesc = {};
62 clientDesc.serializer = mC2sBuf.get();
63 clientDesc.memoryTransferService = GetClientMemoryTransferService();
64
65 mWireClient.reset(new WireClient(clientDesc));
66 mS2cBuf->SetHandler(mWireClient.get());
67
68 dawnProcSetProcs(&dawn_wire::client::GetProcs());
69
70 auto deviceReservation = mWireClient->ReserveDevice();
71 EXPECT_CALL(api, DeviceReference(mockDevice));
72 mWireServer->InjectDevice(mockDevice, deviceReservation.id, deviceReservation.generation);
73
74 device = deviceReservation.device;
75 apiDevice = mockDevice;
76
77 // The GetQueue is done on WireClient startup so we expect it now.
78 queue = wgpuDeviceGetQueue(device);
79 apiQueue = api.GetNewQueue();
80 EXPECT_CALL(api, DeviceGetQueue(apiDevice)).WillOnce(Return(apiQueue));
81 FlushClient();
82 }
83
TearDown()84 void WireTest::TearDown() {
85 dawnProcSetProcs(nullptr);
86
87 // Derived classes should call the base TearDown() first. The client must
88 // be reset before any mocks are deleted.
89 // Incomplete client callbacks will be called on deletion, so the mocks
90 // cannot be null.
91 api.IgnoreAllReleaseCalls();
92 mWireClient = nullptr;
93
94 if (mWireServer && apiDevice) {
95 // These are called on server destruction to clear the callbacks. They must not be
96 // called after the server is destroyed.
97 EXPECT_CALL(api, OnDeviceSetUncapturedErrorCallback(apiDevice, nullptr, nullptr))
98 .Times(Exactly(1));
99 EXPECT_CALL(api, OnDeviceSetLoggingCallback(apiDevice, nullptr, nullptr)).Times(Exactly(1));
100 EXPECT_CALL(api, OnDeviceSetDeviceLostCallback(apiDevice, nullptr, nullptr))
101 .Times(Exactly(1));
102 }
103 mWireServer = nullptr;
104 }
105
106 // This should be called if |apiDevice| is no longer exists on the wire.
107 // This signals that expectations in |TearDowb| shouldn't be added.
DefaultApiDeviceWasReleased()108 void WireTest::DefaultApiDeviceWasReleased() {
109 apiDevice = nullptr;
110 }
111
FlushClient(bool success)112 void WireTest::FlushClient(bool success) {
113 ASSERT_EQ(mC2sBuf->Flush(), success);
114
115 Mock::VerifyAndClearExpectations(&api);
116 SetupIgnoredCallExpectations();
117 }
118
FlushServer(bool success)119 void WireTest::FlushServer(bool success) {
120 ASSERT_EQ(mS2cBuf->Flush(), success);
121 }
122
GetWireServer()123 dawn_wire::WireServer* WireTest::GetWireServer() {
124 return mWireServer.get();
125 }
126
GetWireClient()127 dawn_wire::WireClient* WireTest::GetWireClient() {
128 return mWireClient.get();
129 }
130
DeleteServer()131 void WireTest::DeleteServer() {
132 EXPECT_CALL(api, QueueRelease(apiQueue)).Times(1);
133 EXPECT_CALL(api, DeviceRelease(apiDevice)).Times(1);
134
135 if (mWireServer) {
136 // These are called on server destruction to clear the callbacks. They must not be
137 // called after the server is destroyed.
138 EXPECT_CALL(api, OnDeviceSetUncapturedErrorCallback(apiDevice, nullptr, nullptr))
139 .Times(Exactly(1));
140 EXPECT_CALL(api, OnDeviceSetLoggingCallback(apiDevice, nullptr, nullptr)).Times(Exactly(1));
141 EXPECT_CALL(api, OnDeviceSetDeviceLostCallback(apiDevice, nullptr, nullptr))
142 .Times(Exactly(1));
143 }
144 mWireServer = nullptr;
145 }
146
DeleteClient()147 void WireTest::DeleteClient() {
148 mWireClient = nullptr;
149 }
150
SetupIgnoredCallExpectations()151 void WireTest::SetupIgnoredCallExpectations() {
152 EXPECT_CALL(api, DeviceTick(_)).Times(AnyNumber());
153 }
154