[email protected] | 350404e | 2009-01-14 16:19:46 | [diff] [blame] | 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. |
| 4 | |
[email protected] | f5f3615 | 2009-01-15 17:16:48 | [diff] [blame] | 5 | #include "base/scoped_bstr_win.h" |
[email protected] | 350404e | 2009-01-14 16:19:46 | [diff] [blame] | 6 | #include "testing/gtest/include/gtest/gtest.h" |
| 7 | |
[email protected] | 350404e | 2009-01-14 16:19:46 | [diff] [blame] | 8 | namespace { |
| 9 | |
| 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; |
| 14 | |
| 15 | void DumbBstrTests() { |
| 16 | ScopedBstr b; |
| 17 | EXPECT_TRUE(b == NULL); |
| 18 | EXPECT_TRUE(b.Length() == 0); |
| 19 | EXPECT_TRUE(b.ByteLength() == 0); |
| 20 | b.Reset(NULL); |
| 21 | EXPECT_TRUE(b == NULL); |
| 22 | EXPECT_TRUE(b.Release() == NULL); |
| 23 | ScopedBstr b2; |
| 24 | b.Swap(b2); |
| 25 | EXPECT_TRUE(b2 == NULL); |
| 26 | } |
| 27 | |
| 28 | void GiveMeABstr(BSTR* ret) { |
| 29 | *ret = SysAllocString(kTestString1); |
| 30 | } |
| 31 | |
| 32 | void BasicBstrTests() { |
| 33 | ScopedBstr b1(kTestString1); |
| 34 | EXPECT_TRUE(b1.Length() == test1_len); |
| 35 | EXPECT_TRUE(b1.ByteLength() == test1_len * sizeof(kTestString1[0])); |
| 36 | |
| 37 | ScopedBstr b2; |
| 38 | b1.Swap(b2); |
| 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); |
| 46 | SysFreeString(tmp); |
| 47 | |
| 48 | GiveMeABstr(b2.Receive()); |
| 49 | EXPECT_TRUE(b2 != NULL); |
| 50 | b2.Reset(); |
| 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()); |
| 59 | |
| 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); |
| 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | TEST(ScopedBstrTest, ScopedBstr) { |
| 69 | DumbBstrTests(); |
| 70 | BasicBstrTests(); |
| 71 | } |
| 72 | |
| 73 | #define kSourceStr L"this is a string" |
| 74 | #define kSourceStrEmpty L"" |
| 75 | |
| 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), |
| 80 | VARCMP_EQ); |
| 81 | |
| 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); |
| 89 | |
| 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)); |
| 93 | } |