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

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

Issue 719363002: Remove implicit conversions from scoped_refptr to T* in base/win/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revert silly comparison rewrite Created 6 years, 1 month 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/win/scoped_com_initializer.h"
(...skipping 23 matching lines...) Expand all
34 EXPECT_TRUE(memcmp(&ScopedComPtr<IUnknown>::iid(), &IID_IUnknown, 34 EXPECT_TRUE(memcmp(&ScopedComPtr<IUnknown>::iid(), &IID_IUnknown,
35 sizeof(IID)) == 0); 35 sizeof(IID)) == 0);
36 36
37 base::win::ScopedCOMInitializer com_initializer; 37 base::win::ScopedCOMInitializer com_initializer;
38 EXPECT_TRUE(com_initializer.succeeded()); 38 EXPECT_TRUE(com_initializer.succeeded());
39 39
40 ScopedComPtr<IUnknown> unk; 40 ScopedComPtr<IUnknown> unk;
41 EXPECT_TRUE(SUCCEEDED(unk.CreateInstance(CLSID_ShellLink))); 41 EXPECT_TRUE(SUCCEEDED(unk.CreateInstance(CLSID_ShellLink)));
42 ScopedComPtr<IUnknown> unk2; 42 ScopedComPtr<IUnknown> unk2;
43 unk2.Attach(unk.Detach()); 43 unk2.Attach(unk.Detach());
44 EXPECT_TRUE(unk == NULL); 44 EXPECT_TRUE(unk.get() == NULL);
45 EXPECT_TRUE(unk2 != NULL); 45 EXPECT_TRUE(unk2.get() != NULL);
46 46
47 ScopedComPtr<IMalloc> mem_alloc; 47 ScopedComPtr<IMalloc> mem_alloc;
48 EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.Receive()))); 48 EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.Receive())));
49 49
50 ScopedComPtr<IUnknown> qi_test; 50 ScopedComPtr<IUnknown> qi_test;
51 EXPECT_HRESULT_SUCCEEDED(mem_alloc.QueryInterface(IID_IUnknown, 51 EXPECT_HRESULT_SUCCEEDED(mem_alloc.QueryInterface(IID_IUnknown,
52 reinterpret_cast<void**>(qi_test.Receive()))); 52 reinterpret_cast<void**>(qi_test.Receive())));
53 EXPECT_TRUE(qi_test.get() != NULL); 53 EXPECT_TRUE(qi_test.get() != NULL);
54 qi_test.Release(); 54 qi_test.Release();
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)); 58 EXPECT_TRUE(copy1.IsSameObject(mem_alloc.get()));
59 EXPECT_FALSE(copy1.IsSameObject(unk2)); // unk2 is valid but different 59 EXPECT_FALSE(copy1.IsSameObject(unk2.get())); // unk2 is valid but different
60 EXPECT_FALSE(copy1.IsSameObject(unk)); // 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.Release();
67 EXPECT_FALSE(copy1.IsSameObject(unk2)); // 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)); 70 ScopedComPtr<IMalloc> copy2(static_cast<IMalloc*>(mem_alloc.get()));
71 EXPECT_TRUE(copy2.IsSameObject(mem_alloc)); 71 EXPECT_TRUE(copy2.IsSameObject(mem_alloc.get()));
72 72
73 EXPECT_TRUE(SUCCEEDED(unk.QueryFrom(mem_alloc))); 73 EXPECT_TRUE(SUCCEEDED(unk.QueryFrom(mem_alloc.get())));
74 EXPECT_TRUE(unk != NULL); 74 EXPECT_TRUE(unk.get() != NULL);
75 unk.Release(); 75 unk.Release();
76 EXPECT_TRUE(unk == NULL); 76 EXPECT_TRUE(unk.get() == NULL);
77 EXPECT_TRUE(unk.IsSameObject(copy1)); // 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 scoped_ptr<Dummy> p(new Dummy); 85 scoped_ptr<Dummy> p(new Dummy);
86 { 86 {
87 Ptr p2(p.get()); 87 Ptr p2(p.get());
88 EXPECT_EQ(p->adds, 1); 88 EXPECT_EQ(p->adds, 1);
89 EXPECT_EQ(p->releases, 0); 89 EXPECT_EQ(p->releases, 0);
90 Ptr p3 = p2; 90 Ptr p3 = p2;
91 EXPECT_EQ(p->adds, 2); 91 EXPECT_EQ(p->adds, 2);
92 EXPECT_EQ(p->releases, 0); 92 EXPECT_EQ(p->releases, 0);
93 p3 = p2; 93 p3 = p2;
94 EXPECT_EQ(p->adds, 3); 94 EXPECT_EQ(p->adds, 3);
95 EXPECT_EQ(p->releases, 1); 95 EXPECT_EQ(p->releases, 1);
96 // To avoid hitting a reallocation. 96 // To avoid hitting a reallocation.
97 bleh.reserve(1); 97 bleh.reserve(1);
98 bleh.push_back(p2); 98 bleh.push_back(p2);
99 EXPECT_EQ(p->adds, 4); 99 EXPECT_EQ(p->adds, 4);
100 EXPECT_EQ(p->releases, 1); 100 EXPECT_EQ(p->releases, 1);
101 EXPECT_EQ(bleh[0], p.get()); 101 EXPECT_EQ(bleh[0].get(), p.get());
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
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698