summaryrefslogtreecommitdiffstats
path: root/base/scoped_comptr_win_unittest.cc
blob: 5e4ad3d3101782b0042732430f35b37a1121f348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <shlobj.h>

#include "base/scoped_comptr_win.h"
#include "testing/gtest/include/gtest/gtest.h"

TEST(ScopedComPtrTest, ScopedComPtr) {
  EXPECT_TRUE(memcmp(&ScopedComPtr<IUnknown>::iid(), &IID_IUnknown,
                     sizeof(IID)) == 0);

  EXPECT_TRUE(SUCCEEDED(::CoInitialize(NULL)));

  {
    ScopedComPtr<IUnknown> unk;
    EXPECT_TRUE(SUCCEEDED(unk.CreateInstance(CLSID_ShellLink)));
    ScopedComPtr<IUnknown> unk2;
    unk2.Attach(unk.Detach());
    EXPECT_TRUE(unk == NULL);
    EXPECT_TRUE(unk2 != NULL);

    ScopedComPtr<IMalloc> mem_alloc;
    EXPECT_TRUE(SUCCEEDED(CoGetMalloc(1, mem_alloc.Receive())));

    // test ScopedComPtr& constructor
    ScopedComPtr<IMalloc> copy1(mem_alloc);
    EXPECT_TRUE(copy1.IsSameObject(mem_alloc));
    EXPECT_FALSE(copy1.IsSameObject(unk2));  // unk2 is valid but different
    EXPECT_FALSE(copy1.IsSameObject(unk));  // unk is NULL

    IMalloc* naked_copy = copy1.Detach();
    copy1 = naked_copy;  // Test the =(T*) operator.
    naked_copy->Release();

    copy1.Release();
    EXPECT_FALSE(copy1.IsSameObject(unk2));  // unk2 is valid, copy1 is not

    // test Interface* constructor
    ScopedComPtr<IMalloc> copy2(static_cast<IMalloc*>(mem_alloc));
    EXPECT_TRUE(copy2.IsSameObject(mem_alloc));

    EXPECT_TRUE(SUCCEEDED(unk.QueryFrom(mem_alloc)));
    EXPECT_TRUE(unk != NULL);
    unk.Release();
    EXPECT_TRUE(unk == NULL);
    EXPECT_TRUE(unk.IsSameObject(copy1));  // both are NULL
  }

  ::CoUninitialize();
}