1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
base / win / scoped_variant_unittest.cc [blame]
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <wrl/client.h>
#include <wrl/implements.h>
#include <utility>
#include "base/win/dispatch_stub.h"
#include "base/win/scoped_variant.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::win::test::DispatchStub;
namespace base {
namespace win {
namespace {
constexpr wchar_t kTestString[] = L"Test string for BSTRs.";
void InitializeVariantWithBstr(VARIANT* var) {
if (!var) {
ADD_FAILURE() << "|var| cannot be null.";
return;
}
var->vt = VT_BSTR;
V_BSTR(var) = ::SysAllocString(kTestString);
}
void ExpectRefCount(ULONG expected_refcount, IUnknown* object) {
// In general, code should not check the values of AddRef() and Release().
// However, tests need to validate that ScopedVariant safely owns a COM object
// so they are checked for this unit test.
EXPECT_EQ(expected_refcount + 1, object->AddRef());
EXPECT_EQ(expected_refcount, object->Release());
}
void ExpectVariantType(VARENUM var_type, const ScopedVariant& var) {
EXPECT_EQ(var_type, var.type());
EXPECT_EQ(var_type, V_VT(var.ptr()));
}
} // namespace
TEST(ScopedVariantTest, Empty) {
ScopedVariant var;
ExpectVariantType(VT_EMPTY, var);
}
TEST(ScopedVariantTest, ConstructBstr) {
ScopedVariant var(kTestString);
ExpectVariantType(VT_BSTR, var);
EXPECT_STREQ(kTestString, V_BSTR(var.ptr()));
}
TEST(ScopedVariantTest, SetBstr) {
ScopedVariant var;
var.Set(kTestString);
ExpectVariantType(VT_BSTR, var);
EXPECT_STREQ(kTestString, V_BSTR(var.ptr()));
}
TEST(ScopedVariantTest, ReleaseBstr) {
ScopedVariant var;
var.Set(kTestString);
VARIANT released_variant = var.Release();
ExpectVariantType(VT_EMPTY, var);
EXPECT_EQ(VT_BSTR, V_VT(&released_variant));
EXPECT_STREQ(kTestString, V_BSTR(&released_variant));
::VariantClear(&released_variant);
}
TEST(ScopedVariantTest, ResetToEmptyBstr) {
ScopedVariant var(kTestString);
ExpectVariantType(VT_BSTR, var);
var.Reset();
ExpectVariantType(VT_EMPTY, var);
}
TEST(ScopedVariantTest, TakeOwnershipBstr) {
VARIANT bstr_variant;
bstr_variant.vt = VT_BSTR;
bstr_variant.bstrVal = ::SysAllocString(kTestString);
ScopedVariant var;
var.Reset(bstr_variant);
ExpectVariantType(VT_BSTR, var);
EXPECT_EQ(bstr_variant.bstrVal, V_BSTR(var.ptr()));
}
TEST(ScopedVariantTest, SwapBstr) {
ScopedVariant from(kTestString);
ScopedVariant to;
to.Swap(from);
ExpectVariantType(VT_EMPTY, from);
ExpectVariantType(VT_BSTR, to);
EXPECT_STREQ(kTestString, V_BSTR(to.ptr()));
}
TEST(ScopedVariantTest, CompareBstr) {
ScopedVariant var_bstr1;
InitializeVariantWithBstr(var_bstr1.Receive());
ScopedVariant var_bstr2(V_BSTR(var_bstr1.ptr()));
EXPECT_EQ(0, var_bstr1.Compare(var_bstr2));
var_bstr2.Reset();
EXPECT_NE(0, var_bstr1.Compare(var_bstr2));
}
TEST(ScopedVariantTest, ReceiveAndCopyBstr) {
ScopedVariant var_bstr1;
InitializeVariantWithBstr(var_bstr1.Receive());
ScopedVariant var_bstr2;
var_bstr2.Reset(var_bstr1.Copy());
EXPECT_EQ(0, var_bstr1.Compare(var_bstr2));
}
TEST(ScopedVariantTest, SetBstrFromBstrVariant) {
ScopedVariant var_bstr1;
InitializeVariantWithBstr(var_bstr1.Receive());
ScopedVariant var_bstr2;
var_bstr2.Set(V_BSTR(var_bstr1.ptr()));
EXPECT_EQ(0, var_bstr1.Compare(var_bstr2));
}
TEST(ScopedVariantTest, SetDate) {
ScopedVariant var;
SYSTEMTIME sys_time;
::GetSystemTime(&sys_time);
DATE date;
::SystemTimeToVariantTime(&sys_time, &date);
var.SetDate(date);
ExpectVariantType(VT_DATE, var);
EXPECT_EQ(date, V_DATE(var.ptr()));
}
TEST(ScopedVariantTest, SetSigned1Byte) {
ScopedVariant var;
var.Set(static_cast<int8_t>('v'));
ExpectVariantType(VT_I1, var);
EXPECT_EQ('v', V_I1(var.ptr()));
}
TEST(ScopedVariantTest, SetSigned2Byte) {
ScopedVariant var;
var.Set(static_cast<int16_t>(123));
ExpectVariantType(VT_I2, var);
EXPECT_EQ(123, V_I2(var.ptr()));
}
TEST(ScopedVariantTest, SetSigned4Byte) {
ScopedVariant var;
var.Set(123);
ExpectVariantType(VT_I4, var);
EXPECT_EQ(123, V_I4(var.ptr()));
}
TEST(ScopedVariantTest, SetSigned8Byte) {
ScopedVariant var;
var.Set(static_cast<int64_t>(123));
ExpectVariantType(VT_I8, var);
EXPECT_EQ(123, V_I8(var.ptr()));
}
TEST(ScopedVariantTest, SetUnsigned1Byte) {
ScopedVariant var;
var.Set(static_cast<uint8_t>(123));
ExpectVariantType(VT_UI1, var);
EXPECT_EQ(123u, V_UI1(var.ptr()));
}
TEST(ScopedVariantTest, SetUnsigned2Byte) {
ScopedVariant var;
var.Set(static_cast<uint16_t>(123));
ExpectVariantType(VT_UI2, var);
EXPECT_EQ(123u, V_UI2(var.ptr()));
}
TEST(ScopedVariantTest, SetUnsigned4Byte) {
ScopedVariant var;
var.Set(static_cast<uint32_t>(123));
ExpectVariantType(VT_UI4, var);
EXPECT_EQ(123u, V_UI4(var.ptr()));
}
TEST(ScopedVariantTest, SetUnsigned8Byte) {
ScopedVariant var;
var.Set(static_cast<uint64_t>(123));
ExpectVariantType(VT_UI8, var);
EXPECT_EQ(123u, V_UI8(var.ptr()));
}
TEST(ScopedVariantTest, SetReal4Byte) {
ScopedVariant var;
var.Set(123.123f);
ExpectVariantType(VT_R4, var);
EXPECT_EQ(123.123f, V_R4(var.ptr()));
}
TEST(ScopedVariantTest, SetReal8Byte) {
ScopedVariant var;
var.Set(static_cast<double>(123.123));
ExpectVariantType(VT_R8, var);
EXPECT_EQ(123.123, V_R8(var.ptr()));
}
TEST(ScopedVariantTest, SetBooleanTrue) {
ScopedVariant var;
var.Set(true);
ExpectVariantType(VT_BOOL, var);
EXPECT_EQ(VARIANT_TRUE, V_BOOL(var.ptr()));
}
TEST(ScopedVariantTest, SetBooleanFalse) {
ScopedVariant var;
var.Set(false);
ExpectVariantType(VT_BOOL, var);
EXPECT_EQ(VARIANT_FALSE, V_BOOL(var.ptr()));
}
TEST(ScopedVariantTest, SetComIDispatch) {
ScopedVariant var;
Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
Microsoft::WRL::Make<DispatchStub>();
ExpectRefCount(1U, dispatch_stub.Get());
var.Set(dispatch_stub.Get());
ExpectVariantType(VT_DISPATCH, var);
EXPECT_EQ(dispatch_stub.Get(), V_DISPATCH(var.ptr()));
ExpectRefCount(2U, dispatch_stub.Get());
var.Reset();
ExpectRefCount(1U, dispatch_stub.Get());
}
TEST(ScopedVariantTest, SetComNullIDispatch) {
ScopedVariant var;
var.Set(static_cast<IDispatch*>(nullptr));
ExpectVariantType(VT_DISPATCH, var);
EXPECT_EQ(nullptr, V_DISPATCH(var.ptr()));
}
TEST(ScopedVariantTest, SetComIUnknown) {
ScopedVariant var;
Microsoft::WRL::ComPtr<IUnknown> unknown_stub =
Microsoft::WRL::Make<DispatchStub>();
ExpectRefCount(1U, unknown_stub.Get());
var.Set(unknown_stub.Get());
ExpectVariantType(VT_UNKNOWN, var);
EXPECT_EQ(unknown_stub.Get(), V_UNKNOWN(var.ptr()));
ExpectRefCount(2U, unknown_stub.Get());
var.Reset();
ExpectRefCount(1U, unknown_stub.Get());
}
TEST(ScopedVariantTest, SetComNullIUnknown) {
ScopedVariant var;
var.Set(static_cast<IUnknown*>(nullptr));
ExpectVariantType(VT_UNKNOWN, var);
EXPECT_EQ(nullptr, V_UNKNOWN(var.ptr()));
}
TEST(ScopedVariant, ScopedComIDispatchConstructor) {
Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
Microsoft::WRL::Make<DispatchStub>();
{
ScopedVariant var(dispatch_stub.Get());
ExpectVariantType(VT_DISPATCH, var);
EXPECT_EQ(dispatch_stub.Get(), V_DISPATCH(var.ptr()));
ExpectRefCount(2U, dispatch_stub.Get());
}
ExpectRefCount(1U, dispatch_stub.Get());
}
TEST(ScopedVariant, ScopedComIDispatchMove) {
Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
Microsoft::WRL::Make<DispatchStub>();
{
ScopedVariant var1(dispatch_stub.Get());
ExpectRefCount(2U, dispatch_stub.Get());
ScopedVariant var2(std::move(var1));
ExpectRefCount(2U, dispatch_stub.Get());
ScopedVariant var3;
var3 = std::move(var2);
ExpectRefCount(2U, dispatch_stub.Get());
}
ExpectRefCount(1U, dispatch_stub.Get());
}
TEST(ScopedVariant, ScopedComIDispatchCopy) {
Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
Microsoft::WRL::Make<DispatchStub>();
{
ScopedVariant var1(dispatch_stub.Get());
ExpectRefCount(2U, dispatch_stub.Get());
ScopedVariant var2(static_cast<const VARIANT&>(var1));
ExpectRefCount(3U, dispatch_stub.Get());
ScopedVariant var3;
var3 = static_cast<const VARIANT&>(var2);
ExpectRefCount(4U, dispatch_stub.Get());
}
ExpectRefCount(1U, dispatch_stub.Get());
}
TEST(ScopedVariant, ScopedComIUnknownConstructor) {
Microsoft::WRL::ComPtr<IUnknown> unknown_stub =
Microsoft::WRL::Make<DispatchStub>();
{
ScopedVariant unk_var(unknown_stub.Get());
ExpectVariantType(VT_UNKNOWN, unk_var);
EXPECT_EQ(unknown_stub.Get(), V_UNKNOWN(unk_var.ptr()));
ExpectRefCount(2U, unknown_stub.Get());
}
ExpectRefCount(1U, unknown_stub.Get());
}
TEST(ScopedVariant, ScopedComIUnknownWithRawVariant) {
ScopedVariant var;
Microsoft::WRL::ComPtr<IUnknown> unknown_stub =
Microsoft::WRL::Make<DispatchStub>();
VARIANT raw;
raw.vt = VT_UNKNOWN;
raw.punkVal = unknown_stub.Get();
ExpectRefCount(1U, unknown_stub.Get());
var.Set(raw);
ExpectRefCount(2U, unknown_stub.Get());
var.Reset();
ExpectRefCount(1U, unknown_stub.Get());
}
TEST(ScopedVariant, SetSafeArray) {
SAFEARRAY* sa = ::SafeArrayCreateVector(VT_UI1, 0, 100);
ASSERT_TRUE(sa);
ScopedVariant var;
var.Set(sa);
EXPECT_TRUE(ScopedVariant::IsLeakableVarType(var.type()));
ExpectVariantType(static_cast<VARENUM>(VT_ARRAY | VT_UI1), var);
EXPECT_EQ(sa, V_ARRAY(var.ptr()));
// The array is destroyed in the destructor of var.
sa = nullptr;
}
TEST(ScopedVariant, SetNullSafeArray) {
ScopedVariant var;
var.Set(static_cast<SAFEARRAY*>(nullptr));
ExpectVariantType(VT_EMPTY, var);
}
} // namespace win
} // namespace base