summaryrefslogtreecommitdiffstats
path: root/ceee/testing/utils/test_utils_unittest.cc
blob: 4ca4a4e0954d189dc5149d7e925bf17244367ef3 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2010 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.
//
// Unit tests for test utils.

#include "ceee/testing/utils/test_utils.h"

#include "base/scoped_vector.h"
#include "ceee/common/initializing_coclass.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

TEST(LogDisabler, ItDisables) {
  testing::LogDisabler disabler;
  DCHECK(false);
}

}  // namespace

namespace {

using testing::_;
using testing::CopyBSTRToArgument;
using testing::CopyInterfaceToArgument;
using testing::CopyStringToArgument;
using testing::CopyVariantToArgument;
using testing::DoAll;
using testing::Return;

class IInt : public IUnknown {
 public:
  virtual int get() const = 0;
  virtual void set(int i) = 0;
};

// {FA26DE41-C212-4e11-A23F-C7DC405180A2}
const GUID IID_IInt =
  { 0xfa26de41, 0xc212, 0x4e11,
    { 0xa2, 0x3f, 0xc7, 0xdc, 0x40, 0x51, 0x80, 0xa2 } };

class MockInt
    : public CComObjectRootEx<CComSingleThreadModel>,
      public IInt {
 public:
  BEGIN_COM_MAP(MockInt)
    COM_INTERFACE_ENTRY_IID(IID_IInt, IInt)
  END_COM_MAP()

  HRESULT Initialize(MockInt** i) {
    *i = this;
    return S_OK;
  }

  int get() const { return i_; }
  void set(int i) { i_ = i; }

 private:
  int i_;
};

class MockGetter {
 public:
  MOCK_METHOD1(GetString, void(wchar_t* str));
  MOCK_METHOD1(GetBSTR, void(BSTR* str));
  MOCK_METHOD1(GetIInt, void(IInt** i));
  MOCK_METHOD1(GetVariant, void(VARIANT* var));
};

const wchar_t* kStr = L"hello";
const int kInt = 5;

TEST(CopyStringToArgument, ItCopies) {
  MockGetter getter;

  EXPECT_CALL(getter, GetString(_)).WillOnce(CopyStringToArgument<0>(kStr));

  wchar_t* ret = new wchar_t[lstrlenW(kStr) + 1];
  getter.GetString(ret);
  ASSERT_EQ(wcscmp(ret, kStr), 0);
  delete[] ret;
}

TEST(CopyBSTRToArgument, ItCopies) {
  MockGetter getter;

  EXPECT_CALL(getter, GetBSTR(_)).WillOnce(CopyBSTRToArgument<0>(kStr));

  CComBSTR expected = kStr;
  CComBSTR ret;
  getter.GetBSTR(&ret);
  ASSERT_EQ(ret, expected);
}

TEST(CopyInterfaceToArgument, ItCopies) {
  MockGetter getter;

  {
    // Cause ptr to go out of scope.
    MockInt* mock_int;
    CComPtr<IInt> int_ptr;
    ASSERT_HRESULT_SUCCEEDED(
          InitializingCoClass<MockInt>::CreateInitializedIID(
              &mock_int, IID_IInt, &int_ptr));
    int_ptr->set(kInt);

    EXPECT_CALL(getter, GetIInt(_))
        .WillOnce(CopyInterfaceToArgument<0>(int_ptr));
  }

  CComPtr<IInt> int_ptr;
  getter.GetIInt(&int_ptr);
  ASSERT_EQ(int_ptr->get(), kInt);
}

TEST(CopyVariantToArgument, ItCopies) {
  MockGetter getter;

  {
    // Cause the variant to go out of scope.
    MockInt* mock_int;
    CComPtr<IInt> int_ptr;
    ASSERT_HRESULT_SUCCEEDED(
          InitializingCoClass<MockInt>::CreateInitializedIID(
              &mock_int, IID_IInt, &int_ptr));
    int_ptr->set(kInt);

    CComVariant var(int_ptr);
    EXPECT_CALL(getter, GetVariant(_)).WillOnce(CopyVariantToArgument<0>(var));
  }

  CComVariant ret;
  getter.GetVariant(&ret);
  ASSERT_EQ(V_VT(&ret), VT_UNKNOWN);

  CComPtr<IInt> int_ptr;
  ASSERT_HRESULT_SUCCEEDED(V_UNKNOWN(&ret)->QueryInterface(
        IID_IInt, reinterpret_cast<void**>(&int_ptr)));
  ASSERT_EQ(int_ptr->get(), kInt);
}

}  // namespace