| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/win/scoped_comptr.h" | 5 #include "base/win/scoped_comptr.h" |
| 6 | 6 |
| 7 #include <objbase.h> | 7 #include <objbase.h> |
| 8 #include <shlobj.h> | 8 #include <shlobj.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/win/scoped_com_initializer.h" | 12 #include "base/win/scoped_com_initializer.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 namespace win { | 16 namespace win { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 struct Dummy { | 20 struct Dummy { |
| 21 Dummy() : adds(0), releases(0) { } | 21 Dummy() : adds(0), releases(0) { } |
| 22 void AddRef() { ++adds; } | 22 unsigned long AddRef() { return ++adds; } |
| 23 void Release() { ++releases; } | 23 unsigned long Release() { return ++releases; } |
| 24 | 24 |
| 25 int adds; | 25 int adds; |
| 26 int releases; | 26 int releases; |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 extern const IID dummy_iid; | 29 extern const IID dummy_iid; |
| 30 const IID dummy_iid = {0x12345678u, | 30 const IID dummy_iid = {0x12345678u, |
| 31 0x1234u, | 31 0x1234u, |
| 32 0x5678u, | 32 0x5678u, |
| 33 {01, 23, 45, 67, 89, 01, 23, 45}}; | 33 {01, 23, 45, 67, 89, 01, 23, 45}}; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 44 unk2.Attach(unk.Detach()); | 44 unk2.Attach(unk.Detach()); |
| 45 EXPECT_TRUE(unk.get() == NULL); | 45 EXPECT_TRUE(unk.get() == NULL); |
| 46 EXPECT_TRUE(unk2.get() != NULL); | 46 EXPECT_TRUE(unk2.get() != NULL); |
| 47 | 47 |
| 48 ScopedComPtr<IMalloc> mem_alloc; | 48 ScopedComPtr<IMalloc> mem_alloc; |
| 49 EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.Receive()))); | 49 EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.Receive()))); |
| 50 | 50 |
| 51 ScopedComPtr<IUnknown> qi_test; | 51 ScopedComPtr<IUnknown> qi_test; |
| 52 EXPECT_HRESULT_SUCCEEDED(mem_alloc.QueryInterface(IID_PPV_ARGS(&qi_test))); | 52 EXPECT_HRESULT_SUCCEEDED(mem_alloc.QueryInterface(IID_PPV_ARGS(&qi_test))); |
| 53 EXPECT_TRUE(qi_test.get() != NULL); | 53 EXPECT_TRUE(qi_test.get() != NULL); |
| 54 qi_test.Release(); | 54 qi_test.Reset(); |
| 55 | 55 |
| 56 // test ScopedComPtr& constructor | 56 // test ScopedComPtr& constructor |
| 57 ScopedComPtr<IMalloc> copy1(mem_alloc); | 57 ScopedComPtr<IMalloc> copy1(mem_alloc); |
| 58 EXPECT_TRUE(copy1.IsSameObject(mem_alloc.get())); | 58 EXPECT_TRUE(copy1.IsSameObject(mem_alloc.get())); |
| 59 EXPECT_FALSE(copy1.IsSameObject(unk2.get())); // unk2 is valid but different | 59 EXPECT_FALSE(copy1.IsSameObject(unk2.get())); // unk2 is valid but different |
| 60 EXPECT_FALSE(copy1.IsSameObject(unk.get())); // unk is NULL | 60 EXPECT_FALSE(copy1.IsSameObject(unk.get())); // unk is NULL |
| 61 | 61 |
| 62 IMalloc* naked_copy = copy1.Detach(); | 62 IMalloc* naked_copy = copy1.Detach(); |
| 63 copy1 = naked_copy; // Test the =(T*) operator. | 63 copy1 = naked_copy; // Test the =(T*) operator. |
| 64 naked_copy->Release(); | 64 naked_copy->Release(); |
| 65 | 65 |
| 66 copy1.Release(); | 66 copy1.Reset(); |
| 67 EXPECT_FALSE(copy1.IsSameObject(unk2.get())); // unk2 is valid, copy1 is not | 67 EXPECT_FALSE(copy1.IsSameObject(unk2.get())); // unk2 is valid, copy1 is not |
| 68 | 68 |
| 69 // test Interface* constructor | 69 // test Interface* constructor |
| 70 ScopedComPtr<IMalloc> copy2(static_cast<IMalloc*>(mem_alloc.get())); | 70 ScopedComPtr<IMalloc> copy2(static_cast<IMalloc*>(mem_alloc.get())); |
| 71 EXPECT_TRUE(copy2.IsSameObject(mem_alloc.get())); | 71 EXPECT_TRUE(copy2.IsSameObject(mem_alloc.get())); |
| 72 | 72 |
| 73 EXPECT_TRUE(SUCCEEDED(unk.QueryFrom(mem_alloc.get()))); | 73 EXPECT_TRUE(SUCCEEDED(unk.QueryFrom(mem_alloc.get()))); |
| 74 EXPECT_TRUE(unk.get() != NULL); | 74 EXPECT_TRUE(unk.get() != NULL); |
| 75 unk.Release(); | 75 unk.Reset(); |
| 76 EXPECT_TRUE(unk.get() == NULL); | 76 EXPECT_TRUE(unk.get() == NULL); |
| 77 EXPECT_TRUE(unk.IsSameObject(copy1.get())); // both are NULL | 77 EXPECT_TRUE(unk.IsSameObject(copy1.get())); // both are NULL |
| 78 } | 78 } |
| 79 | 79 |
| 80 TEST(ScopedComPtrTest, ScopedComPtrVector) { | 80 TEST(ScopedComPtrTest, ScopedComPtrVector) { |
| 81 // Verify we don't get error C2558. | 81 // Verify we don't get error C2558. |
| 82 typedef ScopedComPtr<Dummy, &dummy_iid> Ptr; | 82 typedef ScopedComPtr<Dummy, &dummy_iid> Ptr; |
| 83 std::vector<Ptr> bleh; | 83 std::vector<Ptr> bleh; |
| 84 | 84 |
| 85 std::unique_ptr<Dummy> p(new Dummy); | 85 std::unique_ptr<Dummy> p(new Dummy); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 102 bleh.pop_back(); | 102 bleh.pop_back(); |
| 103 EXPECT_EQ(p->adds, 4); | 103 EXPECT_EQ(p->adds, 4); |
| 104 EXPECT_EQ(p->releases, 2); | 104 EXPECT_EQ(p->releases, 2); |
| 105 } | 105 } |
| 106 EXPECT_EQ(p->adds, 4); | 106 EXPECT_EQ(p->adds, 4); |
| 107 EXPECT_EQ(p->releases, 4); | 107 EXPECT_EQ(p->releases, 4); |
| 108 } | 108 } |
| 109 | 109 |
| 110 } // namespace win | 110 } // namespace win |
| 111 } // namespace base | 111 } // namespace base |
| OLD | NEW |