1 // Copyright (c) 2006-2008 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_bstr_win.h"
6 #include "testing/gtest/include/gtest/gtest.h"
10 static const wchar_t kTestString1
[] = L
"123";
11 static const wchar_t kTestString2
[] = L
"456789";
12 uint32 test1_len
= arraysize(kTestString1
) - 1;
13 uint32 test2_len
= arraysize(kTestString2
) - 1;
15 void DumbBstrTests() {
17 EXPECT_TRUE(b
== NULL
);
18 EXPECT_TRUE(b
.Length() == 0);
19 EXPECT_TRUE(b
.ByteLength() == 0);
21 EXPECT_TRUE(b
== NULL
);
22 EXPECT_TRUE(b
.Release() == NULL
);
25 EXPECT_TRUE(b2
== NULL
);
28 void GiveMeABstr(BSTR
* ret
) {
29 *ret
= SysAllocString(kTestString1
);
32 void BasicBstrTests() {
33 ScopedBstr
b1(kTestString1
);
34 EXPECT_TRUE(b1
.Length() == test1_len
);
35 EXPECT_TRUE(b1
.ByteLength() == test1_len
* sizeof(kTestString1
[0]));
39 EXPECT_TRUE(b2
.Length() == test1_len
);
40 EXPECT_TRUE(b1
.Length() == 0);
41 EXPECT_TRUE(lstrcmpW(b2
, kTestString1
) == 0);
42 BSTR tmp
= b2
.Release();
43 EXPECT_TRUE(tmp
!= NULL
);
44 EXPECT_TRUE(lstrcmpW(tmp
, kTestString1
) == 0);
45 EXPECT_TRUE(b2
== NULL
);
48 GiveMeABstr(b2
.Receive());
49 EXPECT_TRUE(b2
!= NULL
);
51 EXPECT_TRUE(b2
.AllocateBytes(100) != NULL
);
52 EXPECT_TRUE(b2
.ByteLength() == 100);
53 EXPECT_TRUE(b2
.Length() == 100 / sizeof(kTestString1
[0]));
54 lstrcpyW(static_cast<BSTR
>(b2
), kTestString1
);
55 EXPECT_TRUE(lstrlen(b2
) == test1_len
);
56 EXPECT_TRUE(b2
.Length() == 100 / sizeof(kTestString1
[0]));
57 b2
.SetByteLen(lstrlen(b2
) * sizeof(kTestString2
[0]));
58 EXPECT_TRUE(lstrlen(b2
) == b2
.Length());
60 EXPECT_TRUE(b1
.Allocate(kTestString2
) != NULL
);
61 EXPECT_TRUE(b1
.Length() == test2_len
);
62 b1
.SetByteLen((test2_len
- 1) * sizeof(kTestString2
[0]));
63 EXPECT_TRUE(b1
.Length() == test2_len
- 1);
68 TEST(ScopedBstrTest
, ScopedBstr
) {
73 #define kSourceStr L"this is a string"
74 #define kSourceStrEmpty L""
76 TEST(StackBstrTest
, StackBstr
) {
77 ScopedBstr
system_bstr(kSourceStr
);
78 StackBstrVar(kSourceStr
, stack_bstr
);
79 EXPECT_EQ(VarBstrCmp(system_bstr
, stack_bstr
, LOCALE_USER_DEFAULT
, 0),
82 StackBstrVar(kSourceStrEmpty
, empty
);
83 uint32 l1
= SysStringLen(stack_bstr
);
84 uint32 l2
= SysStringLen(StackBstr(kSourceStr
));
85 uint32 l3
= SysStringLen(system_bstr
);
86 EXPECT_TRUE(l1
== l2
);
87 EXPECT_TRUE(l2
== l3
);
88 EXPECT_TRUE(SysStringLen(empty
) == 0);
90 const wchar_t one_more_test
[] = L
"this is my const string";
91 EXPECT_EQ(SysStringLen(StackBstr(one_more_test
)),
92 lstrlenW(one_more_test
));