PageLanguageDetectionTest has the failure rate of 5 - 6% on XP/Vista. Mark it
[chromium-blink-merge.git] / base / scoped_variant_win_unittest.cc
blob8588e17b58f4cbf228522002c085866f7746ed35
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"
8 namespace {
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);
15 ret->vt = VT_BSTR;
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 {
23 public:
24 FakeComObject() : ref_(0) {
27 STDMETHOD_(DWORD, AddRef)() {
28 ref_++;
29 return ref_;
32 STDMETHOD_(DWORD, Release)() {
33 ref_--;
34 return ref_;
37 STDMETHOD(QueryInterface)(REFIID, void**) {
38 return E_NOTIMPL;
41 STDMETHOD(GetTypeInfoCount)(UINT*) {
42 return E_NOTIMPL;
45 STDMETHOD(GetTypeInfo)(UINT, LCID, ITypeInfo**) {
46 return E_NOTIMPL;
49 STDMETHOD(GetIDsOfNames)(REFIID, LPOLESTR*, UINT, LCID, DISPID*) {
50 return E_NOTIMPL;
53 STDMETHOD(Invoke)(DISPID, REFIID, LCID, WORD, DISPPARAMS*, VARIANT*,
54 EXCEPINFO*, UINT*) {
55 return E_NOTIMPL;
58 // A way to check the internal reference count of the class.
59 int ref_count() const {
60 return ref_;
63 protected:
64 int ref_;
67 } // namespace
69 TEST(ScopedVariantTest, ScopedVariant) {
70 ScopedVariant var;
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
77 var_bstr.Reset();
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));
87 var.Reset(tmp);
88 EXPECT_EQ(VT_BSTR, V_VT(&var));
89 EXPECT_EQ(0, lstrcmpW(V_BSTR(&var), kTestString2));
91 var_bstr.Swap(var);
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));
95 var_bstr.Reset();
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));
101 var_bstr2.Reset();
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));
105 var_bstr2.Reset();
106 var_bstr2.Set(V_BSTR(&var_bstr));
107 EXPECT_EQ(0, var_bstr.Compare(var_bstr2));
108 var_bstr2.Reset();
109 var_bstr.Reset();
111 // Test for the SetDate setter.
112 SYSTEMTIME sys_time;
113 ::GetSystemTime(&sys_time);
114 DATE date;
115 ::SystemTimeToVariantTime(&sys_time, &date);
116 var.Reset();
117 var.SetDate(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));
158 var.Set(123.123f);
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));
166 var.Set(true);
167 EXPECT_EQ(VT_BOOL, var.type());
168 EXPECT_EQ(VARIANT_TRUE, V_BOOL(&var));
169 var.Set(false);
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));
178 var.Reset();
180 var.Set(static_cast<IUnknown*>(NULL));
181 EXPECT_EQ(VT_UNKNOWN, var.type());
182 EXPECT_EQ(NULL, V_UNKNOWN(&var));
183 var.Reset();
185 FakeComObject faker;
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());
191 var.Reset();
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());
198 var.Reset();
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());
214 ScopedVariant ref3;
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());
228 VARIANT raw;
229 raw.vt = VT_UNKNOWN;
230 raw.punkVal = &faker;
231 EXPECT_EQ(0, faker.ref_count());
232 var.Set(raw);
233 EXPECT_EQ(1, faker.ref_count());
234 var.Reset();
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));
243 // SAFEARRAY tests
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);
250 var.Set(sa);
251 #ifndef OFFICIAL_BUILD
252 EXPECT_TRUE(ScopedVariant::IsLeakableVarType(var.type()));
253 #endif
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.