Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: base/win/scoped_comptr_unittest.cc

Issue 11050009: Use ScopedCOMInitializer in more places. While this doesn't always simplify code, it does mean we … (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <shlobj.h> 7 #include <shlobj.h>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/win/scoped_com_initializer.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 12
12 namespace base { 13 namespace base {
13 namespace win { 14 namespace win {
14 15
15 namespace { 16 namespace {
16 17
17 struct Dummy { 18 struct Dummy {
18 Dummy() : adds(0), releases(0) { } 19 Dummy() : adds(0), releases(0) { }
19 void AddRef() { ++adds; } 20 void AddRef() { ++adds; }
20 void Release() { ++releases; } 21 void Release() { ++releases; }
21 22
22 int adds; 23 int adds;
23 int releases; 24 int releases;
24 }; 25 };
25 26
26 extern const IID dummy_iid; 27 extern const IID dummy_iid;
27 const IID dummy_iid = { 0x12345678u, 0x1234u, 0x5678u, 01, 23, 45, 67, 89, 28 const IID dummy_iid = { 0x12345678u, 0x1234u, 0x5678u, 01, 23, 45, 67, 89,
28 01, 23, 45 }; 29 01, 23, 45 };
29 30
30 } // namespace 31 } // namespace
31 32
32 TEST(ScopedComPtrTest, ScopedComPtr) { 33 TEST(ScopedComPtrTest, ScopedComPtr) {
33 EXPECT_TRUE(memcmp(&ScopedComPtr<IUnknown>::iid(), &IID_IUnknown, 34 EXPECT_TRUE(memcmp(&ScopedComPtr<IUnknown>::iid(), &IID_IUnknown,
34 sizeof(IID)) == 0); 35 sizeof(IID)) == 0);
35 36
36 EXPECT_TRUE(SUCCEEDED(::CoInitialize(NULL))); 37 base::win::ScopedCOMInitializer com_initializer;
38 EXPECT_TRUE(com_initializer.succeeded());
37 39
38 { 40 ScopedComPtr<IUnknown> unk;
39 ScopedComPtr<IUnknown> unk; 41 EXPECT_TRUE(SUCCEEDED(unk.CreateInstance(CLSID_ShellLink)));
40 EXPECT_TRUE(SUCCEEDED(unk.CreateInstance(CLSID_ShellLink))); 42 ScopedComPtr<IUnknown> unk2;
41 ScopedComPtr<IUnknown> unk2; 43 unk2.Attach(unk.Detach());
42 unk2.Attach(unk.Detach()); 44 EXPECT_TRUE(unk == NULL);
43 EXPECT_TRUE(unk == NULL); 45 EXPECT_TRUE(unk2 != NULL);
44 EXPECT_TRUE(unk2 != NULL);
45 46
46 ScopedComPtr<IMalloc> mem_alloc; 47 ScopedComPtr<IMalloc> mem_alloc;
47 EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.Receive()))); 48 EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.Receive())));
48 49
49 ScopedComPtr<IUnknown> qi_test; 50 ScopedComPtr<IUnknown> qi_test;
50 EXPECT_HRESULT_SUCCEEDED(mem_alloc.QueryInterface(IID_IUnknown, 51 EXPECT_HRESULT_SUCCEEDED(mem_alloc.QueryInterface(IID_IUnknown,
51 reinterpret_cast<void**>(qi_test.Receive()))); 52 reinterpret_cast<void**>(qi_test.Receive())));
52 EXPECT_TRUE(qi_test.get() != NULL); 53 EXPECT_TRUE(qi_test.get() != NULL);
53 qi_test.Release(); 54 qi_test.Release();
54 55
55 // test ScopedComPtr& constructor 56 // test ScopedComPtr& constructor
56 ScopedComPtr<IMalloc> copy1(mem_alloc); 57 ScopedComPtr<IMalloc> copy1(mem_alloc);
57 EXPECT_TRUE(copy1.IsSameObject(mem_alloc)); 58 EXPECT_TRUE(copy1.IsSameObject(mem_alloc));
58 EXPECT_FALSE(copy1.IsSameObject(unk2)); // unk2 is valid but different 59 EXPECT_FALSE(copy1.IsSameObject(unk2)); // unk2 is valid but different
59 EXPECT_FALSE(copy1.IsSameObject(unk)); // unk is NULL 60 EXPECT_FALSE(copy1.IsSameObject(unk)); // unk is NULL
60 61
61 IMalloc* naked_copy = copy1.Detach(); 62 IMalloc* naked_copy = copy1.Detach();
62 copy1 = naked_copy; // Test the =(T*) operator. 63 copy1 = naked_copy; // Test the =(T*) operator.
63 naked_copy->Release(); 64 naked_copy->Release();
64 65
65 copy1.Release(); 66 copy1.Release();
66 EXPECT_FALSE(copy1.IsSameObject(unk2)); // unk2 is valid, copy1 is not 67 EXPECT_FALSE(copy1.IsSameObject(unk2)); // unk2 is valid, copy1 is not
67 68
68 // test Interface* constructor 69 // test Interface* constructor
69 ScopedComPtr<IMalloc> copy2(static_cast<IMalloc*>(mem_alloc)); 70 ScopedComPtr<IMalloc> copy2(static_cast<IMalloc*>(mem_alloc));
70 EXPECT_TRUE(copy2.IsSameObject(mem_alloc)); 71 EXPECT_TRUE(copy2.IsSameObject(mem_alloc));
71 72
72 EXPECT_TRUE(SUCCEEDED(unk.QueryFrom(mem_alloc))); 73 EXPECT_TRUE(SUCCEEDED(unk.QueryFrom(mem_alloc)));
73 EXPECT_TRUE(unk != NULL); 74 EXPECT_TRUE(unk != NULL);
74 unk.Release(); 75 unk.Release();
75 EXPECT_TRUE(unk == NULL); 76 EXPECT_TRUE(unk == NULL);
76 EXPECT_TRUE(unk.IsSameObject(copy1)); // both are NULL 77 EXPECT_TRUE(unk.IsSameObject(copy1)); // both are NULL
77 }
78
79 ::CoUninitialize();
80 } 78 }
81 79
82 TEST(ScopedComPtrTest, ScopedComPtrVector) { 80 TEST(ScopedComPtrTest, ScopedComPtrVector) {
83 // Verify we don't get error C2558. 81 // Verify we don't get error C2558.
84 typedef ScopedComPtr<Dummy, &dummy_iid> Ptr; 82 typedef ScopedComPtr<Dummy, &dummy_iid> Ptr;
85 std::vector<Ptr> bleh; 83 std::vector<Ptr> bleh;
86 84
87 scoped_ptr<Dummy> p(new Dummy); 85 scoped_ptr<Dummy> p(new Dummy);
88 { 86 {
89 Ptr p2(p.get()); 87 Ptr p2(p.get());
(...skipping 14 matching lines...) Expand all
104 bleh.pop_back(); 102 bleh.pop_back();
105 EXPECT_EQ(p->adds, 4); 103 EXPECT_EQ(p->adds, 4);
106 EXPECT_EQ(p->releases, 2); 104 EXPECT_EQ(p->releases, 2);
107 } 105 }
108 EXPECT_EQ(p->adds, 4); 106 EXPECT_EQ(p->adds, 4);
109 EXPECT_EQ(p->releases, 4); 107 EXPECT_EQ(p->releases, 4);
110 } 108 }
111 109
112 } // namespace win 110 } // namespace win
113 } // namespace base 111 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/win/shortcut_unittest.cc » ('j') | chrome_frame/test/reliability/reliability_test_suite.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698