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

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

Issue 2889263002: Remove Interface ID Template Parameter from ScopedComPtr (Closed)
Patch Set: Created 3 years, 7 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
« no previous file with comments | « base/win/scoped_comptr.h ('k') | chrome/utility/importer/ie_importer_win.cc » ('j') | 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 <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 unsigned long AddRef() { return ++adds; } 22 unsigned long AddRef() { return ++adds; }
23 unsigned long Release() { return ++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;
30 const IID dummy_iid = {0x12345678u,
31 0x1234u,
32 0x5678u,
33 {01, 23, 45, 67, 89, 01, 23, 45}};
34
35 } // namespace 29 } // namespace
36 30
37 TEST(ScopedComPtrTest, ScopedComPtr) { 31 TEST(ScopedComPtrTest, ScopedComPtr) {
38 base::win::ScopedCOMInitializer com_initializer; 32 base::win::ScopedCOMInitializer com_initializer;
39 EXPECT_TRUE(com_initializer.succeeded()); 33 EXPECT_TRUE(com_initializer.succeeded());
40 34
41 ScopedComPtr<IUnknown> unk; 35 ScopedComPtr<IUnknown> unk;
42 EXPECT_TRUE(SUCCEEDED(::CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_ALL, 36 EXPECT_TRUE(SUCCEEDED(::CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_ALL,
43 IID_PPV_ARGS(&unk)))); 37 IID_PPV_ARGS(&unk))));
44 ScopedComPtr<IUnknown> unk2; 38 ScopedComPtr<IUnknown> unk2;
45 unk2.Attach(unk.Detach()); 39 unk2.Attach(unk.Detach());
46 EXPECT_TRUE(unk.Get() == NULL); 40 EXPECT_TRUE(unk.Get() == NULL);
47 EXPECT_TRUE(unk2.Get() != NULL); 41 EXPECT_TRUE(unk2.Get() != NULL);
48 42
49 ScopedComPtr<IMalloc> mem_alloc; 43 ScopedComPtr<IMalloc> mem_alloc;
50 EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.GetAddressOf()))); 44 EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.GetAddressOf())));
51 45
52 ScopedComPtr<IUnknown> qi_test; 46 ScopedComPtr<IUnknown> qi_test;
53 EXPECT_HRESULT_SUCCEEDED(mem_alloc.CopyTo(IID_PPV_ARGS(&qi_test))); 47 EXPECT_HRESULT_SUCCEEDED(mem_alloc.CopyTo(IID_PPV_ARGS(&qi_test)));
54 EXPECT_TRUE(qi_test.Get() != NULL); 48 EXPECT_TRUE(qi_test.Get() != NULL);
55 } 49 }
56 50
57 TEST(ScopedComPtrTest, ScopedComPtrVector) { 51 TEST(ScopedComPtrTest, ScopedComPtrVector) {
58 // Verify we don't get error C2558. 52 // Verify we don't get error C2558.
59 typedef ScopedComPtr<Dummy, &dummy_iid> Ptr; 53 typedef ScopedComPtr<Dummy> Ptr;
60 std::vector<Ptr> bleh; 54 std::vector<Ptr> bleh;
61 55
62 std::unique_ptr<Dummy> p(new Dummy); 56 std::unique_ptr<Dummy> p(new Dummy);
63 { 57 {
64 Ptr p2(p.get()); 58 Ptr p2(p.get());
65 EXPECT_EQ(p->adds, 1); 59 EXPECT_EQ(p->adds, 1);
66 EXPECT_EQ(p->releases, 0); 60 EXPECT_EQ(p->releases, 0);
67 Ptr p3 = p2; 61 Ptr p3 = p2;
68 EXPECT_EQ(p->adds, 2); 62 EXPECT_EQ(p->adds, 2);
69 EXPECT_EQ(p->releases, 0); 63 EXPECT_EQ(p->releases, 0);
70 p3 = p2; 64 p3 = p2;
71 EXPECT_EQ(p->adds, 3); 65 EXPECT_EQ(p->adds, 3);
72 EXPECT_EQ(p->releases, 1); 66 EXPECT_EQ(p->releases, 1);
73 // To avoid hitting a reallocation. 67 // To avoid hitting a reallocation.
74 bleh.reserve(1); 68 bleh.reserve(1);
75 bleh.push_back(p2); 69 bleh.push_back(p2);
76 EXPECT_EQ(p->adds, 4); 70 EXPECT_EQ(p->adds, 4);
77 EXPECT_EQ(p->releases, 1); 71 EXPECT_EQ(p->releases, 1);
78 EXPECT_EQ(bleh[0].Get(), p.get()); 72 EXPECT_EQ(bleh[0].Get(), p.get());
79 bleh.pop_back(); 73 bleh.pop_back();
80 EXPECT_EQ(p->adds, 4); 74 EXPECT_EQ(p->adds, 4);
81 EXPECT_EQ(p->releases, 2); 75 EXPECT_EQ(p->releases, 2);
82 } 76 }
83 EXPECT_EQ(p->adds, 4); 77 EXPECT_EQ(p->adds, 4);
84 EXPECT_EQ(p->releases, 4); 78 EXPECT_EQ(p->releases, 4);
85 } 79 }
86 80
87 } // namespace win 81 } // namespace win
88 } // namespace base 82 } // namespace base
OLDNEW
« no previous file with comments | « base/win/scoped_comptr.h ('k') | chrome/utility/importer/ie_importer_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698