1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/win/scoped_variant.h"
6 #include "testing/gtest/include/gtest/gtest.h"
13 static const wchar_t kTestString1
[] = L
"Used to create BSTRs";
14 static const wchar_t kTestString2
[] = L
"Also used to create BSTRs";
16 void GiveMeAVariant(VARIANT
* ret
) {
17 EXPECT_TRUE(ret
!= NULL
);
19 V_BSTR(ret
) = ::SysAllocString(kTestString1
);
22 // A dummy IDispatch implementation (if you can call it that).
23 // The class does nothing intelligent really. Only increments a counter
24 // when AddRef is called and decrements it when Release is called.
25 class FakeComObject
: public IDispatch
{
27 FakeComObject() : ref_(0) {
30 STDMETHOD_(DWORD
, AddRef
)() override
{
35 STDMETHOD_(DWORD
, Release
)() override
{
40 STDMETHOD(QueryInterface
)(REFIID
, void**) override
{ return E_NOTIMPL
; }
42 STDMETHOD(GetTypeInfoCount
)(UINT
*) override
{ return E_NOTIMPL
; }
44 STDMETHOD(GetTypeInfo
)(UINT
, LCID
, ITypeInfo
**) override
{ return E_NOTIMPL
; }
46 STDMETHOD(GetIDsOfNames
)(REFIID
, LPOLESTR
*, UINT
, LCID
, DISPID
*) override
{
50 STDMETHOD(Invoke
)(DISPID
,
61 // A way to check the internal reference count of the class.
62 int ref_count() const {
72 TEST(ScopedVariantTest
, ScopedVariant
) {
74 EXPECT_TRUE(var
.type() == VT_EMPTY
);
75 // V_BSTR(var.ptr()) = NULL; <- NOTE: Assignment like that is not supported.
77 ScopedVariant
var_bstr(L
"VT_BSTR");
78 EXPECT_EQ(VT_BSTR
, V_VT(var_bstr
.ptr()));
79 EXPECT_TRUE(V_BSTR(var_bstr
.ptr()) != NULL
); // can't use EXPECT_NE for BSTR
81 EXPECT_NE(VT_BSTR
, V_VT(var_bstr
.ptr()));
82 var_bstr
.Set(kTestString2
);
83 EXPECT_EQ(VT_BSTR
, V_VT(var_bstr
.ptr()));
85 VARIANT tmp
= var_bstr
.Release();
86 EXPECT_EQ(VT_EMPTY
, V_VT(var_bstr
.ptr()));
87 EXPECT_EQ(VT_BSTR
, V_VT(&tmp
));
88 EXPECT_EQ(0, lstrcmp(V_BSTR(&tmp
), kTestString2
));
91 EXPECT_EQ(VT_BSTR
, V_VT(var
.ptr()));
92 EXPECT_EQ(0, lstrcmpW(V_BSTR(var
.ptr()), kTestString2
));
95 EXPECT_EQ(VT_EMPTY
, V_VT(var
.ptr()));
96 EXPECT_EQ(VT_BSTR
, V_VT(var_bstr
.ptr()));
97 EXPECT_EQ(0, lstrcmpW(V_BSTR(var_bstr
.ptr()), kTestString2
));
100 // Test the Compare and Copy routines.
101 GiveMeAVariant(var_bstr
.Receive());
102 ScopedVariant
var_bstr2(V_BSTR(var_bstr
.ptr()));
103 EXPECT_EQ(0, var_bstr
.Compare(var_bstr2
));
105 EXPECT_NE(0, var_bstr
.Compare(var_bstr2
));
106 var_bstr2
.Reset(var_bstr
.Copy());
107 EXPECT_EQ(0, var_bstr
.Compare(var_bstr2
));
109 var_bstr2
.Set(V_BSTR(var_bstr
.ptr()));
110 EXPECT_EQ(0, var_bstr
.Compare(var_bstr2
));
114 // Test for the SetDate setter.
116 ::GetSystemTime(&sys_time
);
118 ::SystemTimeToVariantTime(&sys_time
, &date
);
121 EXPECT_EQ(VT_DATE
, var
.type());
122 EXPECT_EQ(date
, V_DATE(var
.ptr()));
124 // Simple setter tests. These do not require resetting the variant
125 // after each test since the variant type is not "leakable" (i.e. doesn't
126 // need to be freed explicitly).
128 // We need static cast here since char defaults to int (!?).
129 var
.Set(static_cast<int8
>('v'));
130 EXPECT_EQ(VT_I1
, var
.type());
131 EXPECT_EQ('v', V_I1(var
.ptr()));
133 var
.Set(static_cast<short>(123));
134 EXPECT_EQ(VT_I2
, var
.type());
135 EXPECT_EQ(123, V_I2(var
.ptr()));
137 var
.Set(static_cast<int32
>(123));
138 EXPECT_EQ(VT_I4
, var
.type());
139 EXPECT_EQ(123, V_I4(var
.ptr()));
141 var
.Set(static_cast<int64
>(123));
142 EXPECT_EQ(VT_I8
, var
.type());
143 EXPECT_EQ(123, V_I8(var
.ptr()));
145 var
.Set(static_cast<uint8
>(123));
146 EXPECT_EQ(VT_UI1
, var
.type());
147 EXPECT_EQ(123, V_UI1(var
.ptr()));
149 var
.Set(static_cast<unsigned short>(123));
150 EXPECT_EQ(VT_UI2
, var
.type());
151 EXPECT_EQ(123, V_UI2(var
.ptr()));
153 var
.Set(static_cast<uint32
>(123));
154 EXPECT_EQ(VT_UI4
, var
.type());
155 EXPECT_EQ(123, V_UI4(var
.ptr()));
157 var
.Set(static_cast<uint64
>(123));
158 EXPECT_EQ(VT_UI8
, var
.type());
159 EXPECT_EQ(123, V_UI8(var
.ptr()));
162 EXPECT_EQ(VT_R4
, var
.type());
163 EXPECT_EQ(123.123f
, V_R4(var
.ptr()));
165 var
.Set(static_cast<double>(123.123));
166 EXPECT_EQ(VT_R8
, var
.type());
167 EXPECT_EQ(123.123, V_R8(var
.ptr()));
170 EXPECT_EQ(VT_BOOL
, var
.type());
171 EXPECT_EQ(VARIANT_TRUE
, V_BOOL(var
.ptr()));
173 EXPECT_EQ(VT_BOOL
, var
.type());
174 EXPECT_EQ(VARIANT_FALSE
, V_BOOL(var
.ptr()));
176 // Com interface tests
178 var
.Set(static_cast<IDispatch
*>(NULL
));
179 EXPECT_EQ(VT_DISPATCH
, var
.type());
180 EXPECT_EQ(NULL
, V_DISPATCH(var
.ptr()));
183 var
.Set(static_cast<IUnknown
*>(NULL
));
184 EXPECT_EQ(VT_UNKNOWN
, var
.type());
185 EXPECT_EQ(NULL
, V_UNKNOWN(var
.ptr()));
189 EXPECT_EQ(0, faker
.ref_count());
190 var
.Set(static_cast<IDispatch
*>(&faker
));
191 EXPECT_EQ(VT_DISPATCH
, var
.type());
192 EXPECT_EQ(&faker
, V_DISPATCH(var
.ptr()));
193 EXPECT_EQ(1, faker
.ref_count());
195 EXPECT_EQ(0, faker
.ref_count());
197 var
.Set(static_cast<IUnknown
*>(&faker
));
198 EXPECT_EQ(VT_UNKNOWN
, var
.type());
199 EXPECT_EQ(&faker
, V_UNKNOWN(var
.ptr()));
200 EXPECT_EQ(1, faker
.ref_count());
202 EXPECT_EQ(0, faker
.ref_count());
205 ScopedVariant
disp_var(&faker
);
206 EXPECT_EQ(VT_DISPATCH
, disp_var
.type());
207 EXPECT_EQ(&faker
, V_DISPATCH(disp_var
.ptr()));
208 EXPECT_EQ(1, faker
.ref_count());
210 EXPECT_EQ(0, faker
.ref_count());
213 ScopedVariant
ref1(&faker
);
214 EXPECT_EQ(1, faker
.ref_count());
215 ScopedVariant
ref2(static_cast<const VARIANT
&>(ref1
));
216 EXPECT_EQ(2, faker
.ref_count());
218 ref3
= static_cast<const VARIANT
&>(ref2
);
219 EXPECT_EQ(3, faker
.ref_count());
221 EXPECT_EQ(0, faker
.ref_count());
224 ScopedVariant
unk_var(static_cast<IUnknown
*>(&faker
));
225 EXPECT_EQ(VT_UNKNOWN
, unk_var
.type());
226 EXPECT_EQ(&faker
, V_UNKNOWN(unk_var
.ptr()));
227 EXPECT_EQ(1, faker
.ref_count());
229 EXPECT_EQ(0, faker
.ref_count());
233 raw
.punkVal
= &faker
;
234 EXPECT_EQ(0, faker
.ref_count());
236 EXPECT_EQ(1, faker
.ref_count());
238 EXPECT_EQ(0, faker
.ref_count());
241 ScopedVariant
number(123);
242 EXPECT_EQ(VT_I4
, number
.type());
243 EXPECT_EQ(123, V_I4(number
.ptr()));
247 var
.Set(static_cast<SAFEARRAY
*>(NULL
));
248 EXPECT_EQ(VT_EMPTY
, var
.type());
250 SAFEARRAY
* sa
= ::SafeArrayCreateVector(VT_UI1
, 0, 100);
251 ASSERT_TRUE(sa
!= NULL
);
254 EXPECT_TRUE(ScopedVariant::IsLeakableVarType(var
.type()));
255 EXPECT_EQ(VT_ARRAY
| VT_UI1
, var
.type());
256 EXPECT_EQ(sa
, V_ARRAY(var
.ptr()));
257 // The array is destroyed in the destructor of var.