| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 | |
| 5 #include "base/scoped_bstr_win.h" | |
| 6 #include "testing/gtest/include/gtest/gtest.h" | |
| 7 | |
| 8 namespace { | |
| 9 | |
| 10 static const wchar_t kTestString1[] = L"123"; | |
| 11 static const wchar_t kTestString2[] = L"456789"; | |
| 12 size_t test1_len = arraysize(kTestString1) - 1; | |
| 13 size_t test2_len = arraysize(kTestString2) - 1; | |
| 14 | |
| 15 void DumbBstrTests() { | |
| 16 ScopedBstr b; | |
| 17 EXPECT_TRUE(b == NULL); | |
| 18 EXPECT_EQ(0, b.Length()); | |
| 19 EXPECT_EQ(0, b.ByteLength()); | |
| 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_EQ(test1_len, b1.Length()); | |
| 35 EXPECT_EQ(test1_len * sizeof(kTestString1[0]), b1.ByteLength()); | |
| 36 | |
| 37 ScopedBstr b2; | |
| 38 b1.Swap(b2); | |
| 39 EXPECT_EQ(test1_len, b2.Length()); | |
| 40 EXPECT_EQ(0, b1.Length()); | |
| 41 EXPECT_EQ(0, lstrcmp(b2, kTestString1)); | |
| 42 BSTR tmp = b2.Release(); | |
| 43 EXPECT_TRUE(tmp != NULL); | |
| 44 EXPECT_EQ(0, lstrcmp(tmp, kTestString1)); | |
| 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_EQ(100, b2.ByteLength()); | |
| 53 EXPECT_EQ(100 / sizeof(kTestString1[0]), b2.Length()); | |
| 54 lstrcpy(static_cast<BSTR>(b2), kTestString1); | |
| 55 EXPECT_EQ(test1_len, lstrlen(b2)); | |
| 56 EXPECT_EQ(100 / sizeof(kTestString1[0]), b2.Length()); | |
| 57 b2.SetByteLen(lstrlen(b2) * sizeof(kTestString2[0])); | |
| 58 EXPECT_EQ(b2.Length(), lstrlen(b2)); | |
| 59 | |
| 60 EXPECT_TRUE(b1.Allocate(kTestString2) != NULL); | |
| 61 EXPECT_EQ(test2_len, b1.Length()); | |
| 62 b1.SetByteLen((test2_len - 1) * sizeof(kTestString2[0])); | |
| 63 EXPECT_EQ(test2_len - 1, b1.Length()); | |
| 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(VARCMP_EQ, | |
| 80 VarBstrCmp(system_bstr, stack_bstr, LOCALE_USER_DEFAULT, 0)); | |
| 81 | |
| 82 StackBstrVar(kSourceStrEmpty, empty); | |
| 83 UINT l1 = SysStringLen(stack_bstr); | |
| 84 UINT l2 = SysStringLen(StackBstr(kSourceStr)); | |
| 85 UINT l3 = SysStringLen(system_bstr); | |
| 86 EXPECT_EQ(l1, l2); | |
| 87 EXPECT_EQ(l2, l3); | |
| 88 EXPECT_EQ(0, SysStringLen(empty)); | |
| 89 | |
| 90 const wchar_t one_more_test[] = L"this is my const string"; | |
| 91 EXPECT_EQ(SysStringLen(StackBstr(one_more_test)), | |
| 92 lstrlen(one_more_test)); | |
| 93 } | |
| OLD | NEW |