6#include <wrl/implements.h>
13#include "gtest/gtest.h"
22constexpr wchar_t kTestString[] =
L"Test string for BSTRs.";
24void InitializeVariantWithBstr(
VARIANT* var) {
26 ADD_FAILURE() <<
"|var| cannot be null.";
31 V_BSTR(var) = ::SysAllocString(kTestString);
34void ExpectRefCount(
ULONG expected_refcount, IUnknown*
object) {
38 EXPECT_EQ(expected_refcount + 1, object->AddRef());
39 EXPECT_EQ(expected_refcount, object->Release());
42void ExpectVariantType(VARENUM var_type,
const ScopedVariant& var) {
43 EXPECT_EQ(var_type, var.type());
44 EXPECT_EQ(var_type, V_VT(var.ptr()));
51 ExpectVariantType(VT_EMPTY, var);
54TEST(ScopedVariantTest, ConstructBstr) {
56 ExpectVariantType(VT_BSTR, var);
57 EXPECT_STREQ(kTestString, V_BSTR(var.
ptr()));
60TEST(ScopedVariantTest, SetBstr) {
63 ExpectVariantType(VT_BSTR, var);
64 EXPECT_STREQ(kTestString, V_BSTR(var.
ptr()));
67TEST(ScopedVariantTest, ReleaseBstr) {
71 ExpectVariantType(VT_EMPTY, var);
72 EXPECT_EQ(VT_BSTR, V_VT(&released_variant));
73 EXPECT_STREQ(kTestString, V_BSTR(&released_variant));
74 ::VariantClear(&released_variant);
77TEST(ScopedVariantTest, ResetToEmptyBstr) {
79 ExpectVariantType(VT_BSTR, var);
81 ExpectVariantType(VT_EMPTY, var);
84TEST(ScopedVariantTest, TakeOwnershipBstr) {
86 bstr_variant.vt = VT_BSTR;
87 bstr_variant.bstrVal = ::SysAllocString(kTestString);
90 var.
Reset(bstr_variant);
91 ExpectVariantType(VT_BSTR, var);
92 EXPECT_EQ(bstr_variant.bstrVal, V_BSTR(var.
ptr()));
95TEST(ScopedVariantTest, SwapBstr) {
99 ExpectVariantType(VT_EMPTY, from);
100 ExpectVariantType(VT_BSTR, to);
101 EXPECT_STREQ(kTestString, V_BSTR(to.
ptr()));
104TEST(ScopedVariantTest, CompareBstr) {
106 InitializeVariantWithBstr(var_bstr1.
Receive());
108 EXPECT_EQ(0, var_bstr1.
Compare(var_bstr2));
111 EXPECT_NE(0, var_bstr1.
Compare(var_bstr2));
114TEST(ScopedVariantTest, ReceiveAndCopyBstr) {
116 InitializeVariantWithBstr(var_bstr1.
Receive());
119 EXPECT_EQ(0, var_bstr1.
Compare(var_bstr2));
122TEST(ScopedVariantTest, SetBstrFromBstrVariant) {
124 InitializeVariantWithBstr(var_bstr1.
Receive());
126 var_bstr2.
Set(V_BSTR(var_bstr1.
ptr()));
127 EXPECT_EQ(0, var_bstr1.
Compare(var_bstr2));
130TEST(ScopedVariantTest, SetDate) {
133 ::GetSystemTime(&sys_time);
135 ::SystemTimeToVariantTime(&sys_time, &date);
137 ExpectVariantType(VT_DATE, var);
138 EXPECT_EQ(date, V_DATE(var.
ptr()));
141TEST(ScopedVariantTest, SetSigned1Byte) {
143 var.
Set(
static_cast<int8_t
>(
'v'));
144 ExpectVariantType(VT_I1, var);
145 EXPECT_EQ(
'v', V_I1(var.
ptr()));
148TEST(ScopedVariantTest, SetSigned2Byte) {
150 var.
Set(
static_cast<short>(123));
151 ExpectVariantType(VT_I2, var);
152 EXPECT_EQ(123, V_I2(var.
ptr()));
155TEST(ScopedVariantTest, SetSigned4Byte) {
158 ExpectVariantType(VT_I4, var);
159 EXPECT_EQ(123, V_I4(var.
ptr()));
162TEST(ScopedVariantTest, SetSigned8Byte) {
164 var.
Set(
static_cast<int64_t
>(123));
165 ExpectVariantType(VT_I8, var);
166 EXPECT_EQ(123, V_I8(var.
ptr()));
169TEST(ScopedVariantTest, SetUnsigned1Byte) {
171 var.
Set(
static_cast<uint8_t
>(123));
172 ExpectVariantType(VT_UI1, var);
173 EXPECT_EQ(123u, V_UI1(var.
ptr()));
176TEST(ScopedVariantTest, SetUnsigned2Byte) {
178 var.
Set(
static_cast<unsigned short>(123));
179 ExpectVariantType(VT_UI2, var);
180 EXPECT_EQ(123u, V_UI2(var.
ptr()));
183TEST(ScopedVariantTest, SetUnsigned4Byte) {
185 var.
Set(
static_cast<uint32_t
>(123));
186 ExpectVariantType(VT_UI4, var);
187 EXPECT_EQ(123u, V_UI4(var.
ptr()));
190TEST(ScopedVariantTest, SetUnsigned8Byte) {
192 var.
Set(
static_cast<uint64_t
>(123));
193 ExpectVariantType(VT_UI8, var);
194 EXPECT_EQ(123u, V_UI8(var.
ptr()));
197TEST(ScopedVariantTest, SetReal4Byte) {
200 ExpectVariantType(VT_R4, var);
201 EXPECT_EQ(123.123f, V_R4(var.
ptr()));
204TEST(ScopedVariantTest, SetReal8Byte) {
206 var.
Set(
static_cast<double>(123.123));
207 ExpectVariantType(VT_R8, var);
208 EXPECT_EQ(123.123, V_R8(var.
ptr()));
211TEST(ScopedVariantTest, SetBooleanTrue) {
214 ExpectVariantType(VT_BOOL, var);
215 EXPECT_EQ(VARIANT_TRUE, V_BOOL(var.
ptr()));
218TEST(ScopedVariantTest, SetBooleanFalse) {
221 ExpectVariantType(VT_BOOL, var);
222 EXPECT_EQ(VARIANT_FALSE, V_BOOL(var.
ptr()));
225TEST(ScopedVariantTest, SetComIDispatch) {
227 Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
228 Microsoft::WRL::Make<DispatchStub>();
229 ExpectRefCount(1U, dispatch_stub.Get());
230 var.
Set(dispatch_stub.Get());
231 ExpectVariantType(VT_DISPATCH, var);
232 EXPECT_EQ(dispatch_stub.Get(), V_DISPATCH(var.
ptr()));
233 ExpectRefCount(2U, dispatch_stub.Get());
235 ExpectRefCount(1U, dispatch_stub.Get());
238TEST(ScopedVariantTest, SetComNullIDispatch) {
240 var.
Set(
static_cast<IDispatch*
>(
nullptr));
241 ExpectVariantType(VT_DISPATCH, var);
242 EXPECT_EQ(
nullptr, V_DISPATCH(var.
ptr()));
245TEST(ScopedVariantTest, SetComIUnknown) {
247 Microsoft::WRL::ComPtr<IUnknown> unknown_stub =
248 Microsoft::WRL::Make<DispatchStub>();
249 ExpectRefCount(1U, unknown_stub.Get());
250 var.
Set(unknown_stub.Get());
251 ExpectVariantType(VT_UNKNOWN, var);
252 EXPECT_EQ(unknown_stub.Get(), V_UNKNOWN(var.
ptr()));
253 ExpectRefCount(2U, unknown_stub.Get());
255 ExpectRefCount(1U, unknown_stub.Get());
258TEST(ScopedVariantTest, SetComNullIUnknown) {
260 var.
Set(
static_cast<IUnknown*
>(
nullptr));
261 ExpectVariantType(VT_UNKNOWN, var);
262 EXPECT_EQ(
nullptr, V_UNKNOWN(var.
ptr()));
266 Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
267 Microsoft::WRL::Make<DispatchStub>();
270 ExpectVariantType(VT_DISPATCH, var);
271 EXPECT_EQ(dispatch_stub.Get(), V_DISPATCH(var.
ptr()));
272 ExpectRefCount(2U, dispatch_stub.Get());
274 ExpectRefCount(1U, dispatch_stub.Get());
278 Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
279 Microsoft::WRL::Make<DispatchStub>();
282 ExpectRefCount(2U, dispatch_stub.Get());
284 ExpectRefCount(2U, dispatch_stub.Get());
286 var3 = std::move(var2);
287 ExpectRefCount(2U, dispatch_stub.Get());
289 ExpectRefCount(1U, dispatch_stub.Get());
293 Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
294 Microsoft::WRL::Make<DispatchStub>();
297 ExpectRefCount(2U, dispatch_stub.Get());
299 ExpectRefCount(3U, dispatch_stub.Get());
301 var3 =
static_cast<const VARIANT&
>(var2);
302 ExpectRefCount(4U, dispatch_stub.Get());
304 ExpectRefCount(1U, dispatch_stub.Get());
308 Microsoft::WRL::ComPtr<IUnknown> unknown_stub =
309 Microsoft::WRL::Make<DispatchStub>();
312 ExpectVariantType(VT_UNKNOWN, unk_var);
313 EXPECT_EQ(unknown_stub.Get(), V_UNKNOWN(unk_var.
ptr()));
314 ExpectRefCount(2U, unknown_stub.Get());
316 ExpectRefCount(1U, unknown_stub.Get());
321 Microsoft::WRL::ComPtr<IUnknown> unknown_stub =
322 Microsoft::WRL::Make<DispatchStub>();
325 raw.punkVal = unknown_stub.Get();
326 ExpectRefCount(1U, unknown_stub.Get());
328 ExpectRefCount(2U, unknown_stub.Get());
330 ExpectRefCount(1U, unknown_stub.Get());
334 SAFEARRAY* sa = ::SafeArrayCreateVector(VT_UI1, 0, 100);
340 ExpectVariantType(
static_cast<VARENUM
>(VT_ARRAY | VT_UI1), var);
341 EXPECT_EQ(sa, V_ARRAY(var.
ptr()));
348 var.
Set(
static_cast<SAFEARRAY*
>(
nullptr));
349 ExpectVariantType(VT_EMPTY, var);
static bool IsLeakableVarType(VARTYPE vt)
const VARIANT * ptr() const
void Set(const wchar_t *str)
void Reset(const VARIANT &var=kEmptyVariant)
int Compare(const VARIANT &other, bool ignore_case=false) const
void Swap(ScopedVariant &var)
SK_API sk_sp< SkShader > Empty()
TEST(Display, ScaleFactorToFloat)
#define EXPECT_TRUE(handle)