1 // Copyright (c) 2009 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/scoped_variant_win.h"
6 #include "testing/gtest/include/gtest/gtest.h"
10 static const wchar_t kTestString1
[] = L
"Used to create BSTRs";
11 static const wchar_t kTestString2
[] = L
"Also used to create BSTRs";
13 void GiveMeAVariant(VARIANT
* ret
) {
14 EXPECT_TRUE(ret
!= NULL
);
16 V_BSTR(ret
) = ::SysAllocString(kTestString1
);
19 // A dummy IDispatch implementation (if you can call it that).
20 // The class does nothing intelligent really. Only increments a counter
21 // when AddRef is called and decrements it when Release is called.
22 class FakeComObject
: public IDispatch
{
24 FakeComObject() : ref_(0) {
27 STDMETHOD_(DWORD
, AddRef
)() {
32 STDMETHOD_(DWORD
, Release
)() {
37 STDMETHOD(QueryInterface
)(REFIID
, void**) {
41 STDMETHOD(GetTypeInfoCount
)(UINT
*) {
45 STDMETHOD(GetTypeInfo
)(UINT
, LCID
, ITypeInfo
**) {
49 STDMETHOD(GetIDsOfNames
)(REFIID
, LPOLESTR
*, UINT
, LCID
, DISPID
*) {
53 STDMETHOD(Invoke
)(DISPID
, REFIID
, LCID
, WORD
, DISPPARAMS
*, VARIANT
*,
58 // A way to check the internal reference count of the class.
59 int ref_count() const {
69 TEST(ScopedVariantTest
, ScopedVariant
) {
71 EXPECT_TRUE(var
.type() == VT_EMPTY
);
72 // V_BSTR(&var) = NULL; <- NOTE: Assignment like that is not supported
74 ScopedVariant
var_bstr(L
"VT_BSTR");
75 EXPECT_EQ(VT_BSTR
, V_VT(&var_bstr
));
76 EXPECT_TRUE(V_BSTR(&var_bstr
) != NULL
); // can't use EXPECT_NE for BSTR
78 EXPECT_NE(VT_BSTR
, V_VT(&var_bstr
));
79 var_bstr
.Set(kTestString2
);
80 EXPECT_EQ(VT_BSTR
, V_VT(&var_bstr
));
82 VARIANT tmp
= var_bstr
.Release();
83 EXPECT_EQ(VT_EMPTY
, V_VT(&var_bstr
));
84 EXPECT_EQ(VT_BSTR
, V_VT(&tmp
));
85 EXPECT_EQ(0, lstrcmp(V_BSTR(&tmp
), kTestString2
));
88 EXPECT_EQ(VT_BSTR
, V_VT(&var
));
89 EXPECT_EQ(0, lstrcmpW(V_BSTR(&var
), kTestString2
));
92 EXPECT_EQ(VT_EMPTY
, V_VT(&var
));
93 EXPECT_EQ(VT_BSTR
, V_VT(&var_bstr
));
94 EXPECT_EQ(0, lstrcmpW(V_BSTR(&var_bstr
), kTestString2
));
97 // Test the Compare and Copy routines.
98 GiveMeAVariant(var_bstr
.Receive());
99 ScopedVariant
var_bstr2(V_BSTR(&var_bstr
));
100 EXPECT_EQ(0, var_bstr
.Compare(var_bstr2
));
102 EXPECT_NE(0, var_bstr
.Compare(var_bstr2
));
103 var_bstr2
.Reset(var_bstr
.Copy());
104 EXPECT_EQ(0, var_bstr
.Compare(var_bstr2
));
106 var_bstr2
.Set(V_BSTR(&var_bstr
));
107 EXPECT_EQ(0, var_bstr
.Compare(var_bstr2
));
111 // Test for the SetDate setter.
113 ::GetSystemTime(&sys_time
);
115 ::SystemTimeToVariantTime(&sys_time
, &date
);
118 EXPECT_EQ(VT_DATE
, var
.type());
119 EXPECT_EQ(date
, V_DATE(&var
));
121 // Simple setter tests. These do not require resetting the variant
122 // after each test since the variant type is not "leakable" (i.e. doesn't
123 // need to be freed explicitly).
125 // We need static cast here since char defaults to int (!?).
126 var
.Set(static_cast<int8
>('v'));
127 EXPECT_EQ(VT_I1
, var
.type());
128 EXPECT_EQ('v', V_I1(&var
));
130 var
.Set(static_cast<short>(123));
131 EXPECT_EQ(VT_I2
, var
.type());
132 EXPECT_EQ(123, V_I2(&var
));
134 var
.Set(static_cast<int32
>(123));
135 EXPECT_EQ(VT_I4
, var
.type());
136 EXPECT_EQ(123, V_I4(&var
));
138 var
.Set(static_cast<int64
>(123));
139 EXPECT_EQ(VT_I8
, var
.type());
140 EXPECT_EQ(123, V_I8(&var
));
142 var
.Set(static_cast<uint8
>(123));
143 EXPECT_EQ(VT_UI1
, var
.type());
144 EXPECT_EQ(123, V_UI1(&var
));
146 var
.Set(static_cast<unsigned short>(123));
147 EXPECT_EQ(VT_UI2
, var
.type());
148 EXPECT_EQ(123, V_UI2(&var
));
150 var
.Set(static_cast<uint32
>(123));
151 EXPECT_EQ(VT_UI4
, var
.type());
152 EXPECT_EQ(123, V_UI4(&var
));
154 var
.Set(static_cast<uint64
>(123));
155 EXPECT_EQ(VT_UI8
, var
.type());
156 EXPECT_EQ(123, V_UI8(&var
));
159 EXPECT_EQ(VT_R4
, var
.type());
160 EXPECT_EQ(123.123f
, V_R4(&var
));
162 var
.Set(static_cast<double>(123.123));
163 EXPECT_EQ(VT_R8
, var
.type());
164 EXPECT_EQ(123.123, V_R8(&var
));
167 EXPECT_EQ(VT_BOOL
, var
.type());
168 EXPECT_EQ(VARIANT_TRUE
, V_BOOL(&var
));
170 EXPECT_EQ(VT_BOOL
, var
.type());
171 EXPECT_EQ(VARIANT_FALSE
, V_BOOL(&var
));
173 // Com interface tests
175 var
.Set(static_cast<IDispatch
*>(NULL
));
176 EXPECT_EQ(VT_DISPATCH
, var
.type());
177 EXPECT_EQ(NULL
, V_DISPATCH(&var
));
180 var
.Set(static_cast<IUnknown
*>(NULL
));
181 EXPECT_EQ(VT_UNKNOWN
, var
.type());
182 EXPECT_EQ(NULL
, V_UNKNOWN(&var
));
186 EXPECT_EQ(0, faker
.ref_count());
187 var
.Set(static_cast<IDispatch
*>(&faker
));
188 EXPECT_EQ(VT_DISPATCH
, var
.type());
189 EXPECT_EQ(&faker
, V_DISPATCH(&var
));
190 EXPECT_EQ(1, faker
.ref_count());
192 EXPECT_EQ(0, faker
.ref_count());
194 var
.Set(static_cast<IUnknown
*>(&faker
));
195 EXPECT_EQ(VT_UNKNOWN
, var
.type());
196 EXPECT_EQ(&faker
, V_UNKNOWN(&var
));
197 EXPECT_EQ(1, faker
.ref_count());
199 EXPECT_EQ(0, faker
.ref_count());
202 ScopedVariant
disp_var(&faker
);
203 EXPECT_EQ(VT_DISPATCH
, disp_var
.type());
204 EXPECT_EQ(&faker
, V_DISPATCH(&disp_var
));
205 EXPECT_EQ(1, faker
.ref_count());
207 EXPECT_EQ(0, faker
.ref_count());
210 ScopedVariant
ref1(&faker
);
211 EXPECT_EQ(1, faker
.ref_count());
212 ScopedVariant
ref2(static_cast<const VARIANT
&>(ref1
));
213 EXPECT_EQ(2, faker
.ref_count());
215 ref3
= static_cast<const VARIANT
&>(ref2
);
216 EXPECT_EQ(3, faker
.ref_count());
218 EXPECT_EQ(0, faker
.ref_count());
221 ScopedVariant
unk_var(static_cast<IUnknown
*>(&faker
));
222 EXPECT_EQ(VT_UNKNOWN
, unk_var
.type());
223 EXPECT_EQ(&faker
, V_UNKNOWN(&unk_var
));
224 EXPECT_EQ(1, faker
.ref_count());
226 EXPECT_EQ(0, faker
.ref_count());
230 raw
.punkVal
= &faker
;
231 EXPECT_EQ(0, faker
.ref_count());
233 EXPECT_EQ(1, faker
.ref_count());
235 EXPECT_EQ(0, faker
.ref_count());
238 ScopedVariant
number(123);
239 EXPECT_EQ(VT_I4
, number
.type());
240 EXPECT_EQ(123, V_I4(&number
));
244 var
.Set(static_cast<SAFEARRAY
*>(NULL
));
245 EXPECT_EQ(VT_EMPTY
, var
.type());
247 SAFEARRAY
* sa
= ::SafeArrayCreateVector(VT_UI1
, 0, 100);
248 ASSERT_TRUE(sa
!= NULL
);
251 #ifndef OFFICIAL_BUILD
252 EXPECT_TRUE(ScopedVariant::IsLeakableVarType(var
.type()));
254 EXPECT_EQ(VT_ARRAY
| VT_UI1
, var
.type());
255 EXPECT_EQ(sa
, V_ARRAY(&var
));
256 // The array is destroyed in the destructor of var.