cppu_ifcontainer.cxx (fda69661) cppu_ifcontainer.cxx (8269660f)
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 7 unchanged lines hidden (view full) ---

16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 7 unchanged lines hidden (view full) ---

16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24#include <testshl/simpleheader.hxx>
25
26#include "com/sun/star/lang/XEventListener.hpp"
27#include "cppuhelper/interfacecontainer.hxx"
28#include "cppuhelper/queryinterface.hxx"
29#include "cppuhelper/implbase1.hxx"
30#include "cppuhelper/propshlp.hxx"
24#include "com/sun/star/lang/XEventListener.hpp"
25#include "cppuhelper/interfacecontainer.hxx"
26#include "cppuhelper/queryinterface.hxx"
27#include "cppuhelper/implbase1.hxx"
28#include "cppuhelper/propshlp.hxx"
29#include "gtest/gtest.h"
31
32using namespace com::sun::star;
33using namespace com::sun::star::uno;
34using namespace com::sun::star::lang;
35
36class ContainerListener;
37
38struct ContainerStats {

--- 13 unchanged lines hidden (view full) ---

52 throw (RuntimeException)
53 {
54 m_pStats->m_nDisposed++;
55 }
56};
57
58namespace cppu_ifcontainer
59{
30
31using namespace com::sun::star;
32using namespace com::sun::star::uno;
33using namespace com::sun::star::lang;
34
35class ContainerListener;
36
37struct ContainerStats {

--- 13 unchanged lines hidden (view full) ---

51 throw (RuntimeException)
52 {
53 m_pStats->m_nDisposed++;
54 }
55};
56
57namespace cppu_ifcontainer
58{
60 class IfTest : public CppUnit::TestFixture
59 class IfTest : public ::testing::Test
61 {
60 {
61 protected:
62 osl::Mutex m_aGuard;
63 static const int nTests = 10;
64 public:
62 osl::Mutex m_aGuard;
63 static const int nTests = 10;
64 public:
65 void testCreateDispose()
66 {
67 ContainerStats aStats;
68 cppu::OInterfaceContainerHelper *pContainer;
69
65
70 pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
71
72 CPPUNIT_ASSERT_MESSAGE("Empty container not empty",
73 pContainer->getLength() == 0);
74
75 int i;
76 for (i = 0; i < nTests; i++)
77 {
78 Reference<XEventListener> xRef = new ContainerListener(&aStats);
79 int nNewLen = pContainer->addInterface(xRef);
80
81 CPPUNIT_ASSERT_MESSAGE("addition length mismatch",
82 nNewLen == i + 1);
83 CPPUNIT_ASSERT_MESSAGE("addition length mismatch",
84 pContainer->getLength() == i + 1);
85 }
86 CPPUNIT_ASSERT_MESSAGE("alive count mismatch",
87 aStats.m_nAlive == nTests);
88
89 EventObject aObj;
90 pContainer->disposeAndClear(aObj);
91
92 CPPUNIT_ASSERT_MESSAGE("dispose count mismatch",
93 aStats.m_nDisposed == nTests);
94 CPPUNIT_ASSERT_MESSAGE("leaked container left alive",
95 aStats.m_nAlive == 0);
96
97 delete pContainer;
98 }
99
100 void testEnumerate()
101 {
102 int i;
103 ContainerStats aStats;
104 cppu::OInterfaceContainerHelper *pContainer;
105 pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
106
107 std::vector< Reference< XEventListener > > aListeners;
108 for (i = 0; i < nTests; i++)
109 {
110 Reference<XEventListener> xRef = new ContainerListener(&aStats);
111 int nNewLen = pContainer->addInterface(xRef);
112 aListeners.push_back(xRef);
113 }
114 Sequence< Reference< XInterface > > aElements;
115 aElements = pContainer->getElements();
116
117 CPPUNIT_ASSERT_MESSAGE("query contents",
118 (int)aElements.getLength() == nTests);
119 if ((int)aElements.getLength() == nTests)
120 {
121 for (i = 0; i < nTests; i++)
122 {
123 CPPUNIT_ASSERT_MESSAGE("mismatching elements",
124 aElements[i] == aListeners[i]);
125 }
126 }
127 pContainer->clear();
128
129 CPPUNIT_ASSERT_MESSAGE("non-empty container post clear",
130 pContainer->getLength() == 0);
131 delete pContainer;
132 }
133
134 template < typename ContainerType, typename ContainedType >
135 void doContainerTest(const ContainedType *pTypes)
136 {
137 ContainerStats aStats;
138 ContainerType *pContainer;
139 pContainer = new ContainerType(m_aGuard);
140
141 int i;

--- 8 unchanged lines hidden (view full) ---

150
151 // check it is all there
152 for (i = 0; i < nTests; i++)
153 {
154 cppu::OInterfaceContainerHelper *pHelper;
155
156 pHelper = pContainer->getContainer(pTypes[i]);
157
66 template < typename ContainerType, typename ContainedType >
67 void doContainerTest(const ContainedType *pTypes)
68 {
69 ContainerStats aStats;
70 ContainerType *pContainer;
71 pContainer = new ContainerType(m_aGuard);
72
73 int i;

--- 8 unchanged lines hidden (view full) ---

82
83 // check it is all there
84 for (i = 0; i < nTests; i++)
85 {
86 cppu::OInterfaceContainerHelper *pHelper;
87
88 pHelper = pContainer->getContainer(pTypes[i]);
89
158 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
90 ASSERT_TRUE(pHelper != NULL) << "no helper";
159 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
91 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
160 CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 2);
161 CPPUNIT_ASSERT_MESSAGE("match", aSeq[0] == xRefs[i*2]);
162 CPPUNIT_ASSERT_MESSAGE("match", aSeq[1] == xRefs[i*2+1]);
92 ASSERT_TRUE(aSeq.getLength() == 2) << "wrong num elements";
93 ASSERT_TRUE(aSeq[0] == xRefs[i*2]) << "match";
94 ASSERT_TRUE(aSeq[1] == xRefs[i*2+1]) << "match";
163 }
164
165 // remove every other interface
166 for (i = 0; i < nTests; i++)
167 pContainer->removeInterface(pTypes[i], xRefs[i*2+1]);
168
169 // check it is half there
170 for (i = 0; i < nTests; i++)
171 {
172 cppu::OInterfaceContainerHelper *pHelper;
173
174 pHelper = pContainer->getContainer(pTypes[i]);
175
95 }
96
97 // remove every other interface
98 for (i = 0; i < nTests; i++)
99 pContainer->removeInterface(pTypes[i], xRefs[i*2+1]);
100
101 // check it is half there
102 for (i = 0; i < nTests; i++)
103 {
104 cppu::OInterfaceContainerHelper *pHelper;
105
106 pHelper = pContainer->getContainer(pTypes[i]);
107
176 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
108 ASSERT_TRUE(pHelper != NULL) << "no helper";
177 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
109 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
178 CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 1);
179 CPPUNIT_ASSERT_MESSAGE("match", aSeq[0] == xRefs[i*2]);
110 ASSERT_TRUE(aSeq.getLength() == 1) << "wrong num elements";
111 ASSERT_TRUE(aSeq[0] == xRefs[i*2]) << "match";
180 }
181
182 // remove the 1st half of the rest
183 for (i = 0; i < nTests / 2; i++)
184 pContainer->removeInterface(pTypes[i], xRefs[i*2]);
185
186 // check it is half there
187 for (i = 0; i < nTests / 2; i++)
188 {
189 cppu::OInterfaceContainerHelper *pHelper;
190
191 pHelper = pContainer->getContainer(pTypes[i]);
112 }
113
114 // remove the 1st half of the rest
115 for (i = 0; i < nTests / 2; i++)
116 pContainer->removeInterface(pTypes[i], xRefs[i*2]);
117
118 // check it is half there
119 for (i = 0; i < nTests / 2; i++)
120 {
121 cppu::OInterfaceContainerHelper *pHelper;
122
123 pHelper = pContainer->getContainer(pTypes[i]);
192 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
124 ASSERT_TRUE(pHelper != NULL) << "no helper";
193 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
125 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
194 CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 0);
126 ASSERT_TRUE(aSeq.getLength() == 0) << "wrong num elements";
195 }
196
197 delete pContainer;
198 }
127 }
128
129 delete pContainer;
130 }
131 };
199
132
200 void testOMultiTypeInterfaceContainerHelper()
133 TEST_F(IfTest, testCreateDispose)
134 {
135 ContainerStats aStats;
136 cppu::OInterfaceContainerHelper *pContainer;
137
138 pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
139
140 ASSERT_TRUE(pContainer->getLength() == 0) << "Empty container not empty";
141
142 int i;
143 for (i = 0; i < nTests; i++)
201 {
144 {
202 uno::Type pTypes[nTests] =
203 {
204 ::cppu::UnoType< bool >::get(),
205 ::cppu::UnoType< float >::get(),
206 ::cppu::UnoType< double >::get(),
207 ::cppu::UnoType< ::sal_uInt64 >::get(),
208 ::cppu::UnoType< ::sal_Int64 >::get(),
209 ::cppu::UnoType< ::sal_uInt32 >::get(),
210 ::cppu::UnoType< ::sal_Int32 >::get(),
211 ::cppu::UnoType< ::sal_Int16 >::get(),
212 ::cppu::UnoType< ::rtl::OUString >::get(),
213 ::cppu::UnoType< ::sal_Int8 >::get()
214 };
215 doContainerTest< cppu::OMultiTypeInterfaceContainerHelper,
216 uno::Type> (pTypes);
145 Reference<XEventListener> xRef = new ContainerListener(&aStats);
146 int nNewLen = pContainer->addInterface(xRef);
147
148 ASSERT_TRUE(nNewLen == i + 1) << "addition length mismatch";
149 ASSERT_TRUE(pContainer->getLength() == i + 1) << "addition length mismatch";
217 }
150 }
151 ASSERT_TRUE(aStats.m_nAlive == nTests) << "alive count mismatch";
218
152
219 void testOMultiTypeInterfaceContainerHelperInt32()
153 EventObject aObj;
154 pContainer->disposeAndClear(aObj);
155
156 ASSERT_TRUE(aStats.m_nDisposed == nTests) << "dispose count mismatch";
157 ASSERT_TRUE(aStats.m_nAlive == 0) << "leaked container left alive";
158
159 delete pContainer;
160 }
161
162 TEST_F(IfTest, testEnumerate)
163 {
164 int i;
165 ContainerStats aStats;
166 cppu::OInterfaceContainerHelper *pContainer;
167 pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
168
169 std::vector< Reference< XEventListener > > aListeners;
170 for (i = 0; i < nTests; i++)
220 {
171 {
221 sal_Int32 pTypes[nTests] =
222 {
223 0,
224 -1,
225 1,
226 256,
227 1024,
228 3,
229 7,
230 8,
231 9,
232 10
233 };
234 doContainerTest< cppu::OMultiTypeInterfaceContainerHelperInt32, sal_Int32> (pTypes);
172 Reference<XEventListener> xRef = new ContainerListener(&aStats);
173 int nNewLen = pContainer->addInterface(xRef);
174 aListeners.push_back(xRef);
235 }
175 }
176 Sequence< Reference< XInterface > > aElements;
177 aElements = pContainer->getElements();
236
178
237 void testOMultiTypeInterfaceContainerHelperVar()
179 ASSERT_TRUE((int)aElements.getLength() == nTests) << "query contents";
180 if ((int)aElements.getLength() == nTests)
238 {
181 {
239 typedef ::cppu::OMultiTypeInterfaceContainerHelperVar<
240 const char*, rtl::CStringHash, rtl::CStringEqual> StrContainer;
241
242 const char *pTypes[nTests] =
182 for (i = 0; i < nTests; i++)
243 {
183 {
244 "this_is", "such", "fun", "writing", "unit", "tests", "when", "it", "works", "anyway"
245 };
246 doContainerTest< StrContainer, const char *> (pTypes);
184 ASSERT_TRUE(aElements[i] == aListeners[i]) << "mismatching elements";
185 }
247 }
186 }
187 pContainer->clear();
248
188
249 // Automatic registration code
250 CPPUNIT_TEST_SUITE(IfTest);
251 CPPUNIT_TEST(testCreateDispose);
252 CPPUNIT_TEST(testEnumerate);
253 CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelper);
254 CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperVar);
255 CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperInt32);
256 CPPUNIT_TEST_SUITE_END();
257 };
258} // namespace cppu_ifcontainer
189 ASSERT_TRUE(pContainer->getLength() == 0) << "non-empty container post clear";
190 delete pContainer;
191 }
259
192
260CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(cppu_ifcontainer::IfTest,
261 "cppu_ifcontainer");
193 TEST_F(IfTest, testOMultiTypeInterfaceContainerHelper)
194 {
195 uno::Type pTypes[nTests] =
196 {
197 ::cppu::UnoType< bool >::get(),
198 ::cppu::UnoType< float >::get(),
199 ::cppu::UnoType< double >::get(),
200 ::cppu::UnoType< ::sal_uInt64 >::get(),
201 ::cppu::UnoType< ::sal_Int64 >::get(),
202 ::cppu::UnoType< ::sal_uInt32 >::get(),
203 ::cppu::UnoType< ::sal_Int32 >::get(),
204 ::cppu::UnoType< ::sal_Int16 >::get(),
205 ::cppu::UnoType< ::rtl::OUString >::get(),
206 ::cppu::UnoType< ::sal_Int8 >::get()
207 };
208 doContainerTest< cppu::OMultiTypeInterfaceContainerHelper,
209 uno::Type> (pTypes);
210 }
262
211
263NOADDITIONAL;
212 TEST_F(IfTest, testOMultiTypeInterfaceContainerHelperInt32)
213 {
214 sal_Int32 pTypes[nTests] =
215 {
216 0,
217 -1,
218 1,
219 256,
220 1024,
221 3,
222 7,
223 8,
224 9,
225 10
226 };
227 doContainerTest< cppu::OMultiTypeInterfaceContainerHelperInt32, sal_Int32> (pTypes);
228 }
229
230 TEST_F(IfTest, testOMultiTypeInterfaceContainerHelperVar)
231 {
232 typedef ::cppu::OMultiTypeInterfaceContainerHelperVar<
233 const char*, rtl::CStringHash, rtl::CStringEqual> StrContainer;
234
235 const char *pTypes[nTests] =
236 {
237 "this_is", "such", "fun", "writing", "unit", "tests", "when", "it", "works", "anyway"
238 };
239 doContainerTest< StrContainer, const char *> (pTypes);
240 }
241
242
243} // namespace cppu_ifcontainer
244