summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/gtest/TestNsRefPtr.cpp
blob: a085c29660f7c7ef3beaa1ac5180b924a8cd053d (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/RefPtr.h"
#include "nsCOMPtr.h"
#include "nsISupports.h"
#include "nsQueryObject.h"
#include "mozilla/Unused.h"

#include "gtest/gtest.h"

namespace TestNsRefPtr
{

#define NS_FOO_IID \
{ 0x6f7652e0,  0xee43, 0x11d1, \
  { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }

class Foo : public nsISupports
{
public:
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_FOO_IID)

public:
  Foo();
  // virtual dtor because Bar uses our Release()
  virtual ~Foo();

  NS_IMETHOD_(MozExternalRefCountType) AddRef();
  NS_IMETHOD_(MozExternalRefCountType) Release();
  NS_IMETHOD QueryInterface( const nsIID&, void** );
  void MemberFunction( int, int*, int& );
  virtual void VirtualMemberFunction( int, int*, int& );
  virtual void VirtualConstMemberFunction( int, int*, int& ) const;

  void NonconstMethod() {}
  void ConstMethod() const {}

  int refcount_;

  static int total_constructions_;
  static int total_destructions_;
  static int total_addrefs_;
  static int total_queries_;
};

NS_DEFINE_STATIC_IID_ACCESSOR(Foo, NS_FOO_IID)

int Foo::total_constructions_;
int Foo::total_destructions_;
int Foo::total_addrefs_;
int Foo::total_queries_;

Foo::Foo()
  : refcount_(0)
{
  ++total_constructions_;
}

Foo::~Foo()
{
  ++total_destructions_;
}

MozExternalRefCountType
Foo::AddRef()
{
  ++refcount_;
  ++total_addrefs_;
  return refcount_;
}

MozExternalRefCountType
Foo::Release()
{
  int newcount = --refcount_;
  if ( newcount == 0 )
  {
    delete this;
  }

  return newcount;
}

nsresult
Foo::QueryInterface( const nsIID& aIID, void** aResult )
{
  ++total_queries_;

  nsISupports* rawPtr = 0;
  nsresult status = NS_OK;

  if ( aIID.Equals(NS_GET_IID(Foo)) )
    rawPtr = this;
  else
  {
    nsID iid_of_ISupports = NS_ISUPPORTS_IID;
    if ( aIID.Equals(iid_of_ISupports) )
      rawPtr = static_cast<nsISupports*>(this);
    else
      status = NS_ERROR_NO_INTERFACE;
  }

  NS_IF_ADDREF(rawPtr);
  *aResult = rawPtr;

  return status;
}

void
Foo::MemberFunction( int aArg1, int* aArgPtr, int& aArgRef )
{
}

void
Foo::VirtualMemberFunction( int aArg1, int* aArgPtr, int& aArgRef )
{
}

void
Foo::VirtualConstMemberFunction( int aArg1, int* aArgPtr, int& aArgRef ) const
{
}

nsresult
CreateFoo( void** result )
  // a typical factory function (that calls AddRef)
{
  Foo* foop = new Foo;

  foop->AddRef();
  *result = foop;

  return NS_OK;
}

void
set_a_Foo( RefPtr<Foo>* result )
{
  assert(result);

  RefPtr<Foo> foop( do_QueryObject(new Foo) );
  *result = foop;
}

RefPtr<Foo>
return_a_Foo()
{
  RefPtr<Foo> foop( do_QueryObject(new Foo) );
  return foop;
}

#define NS_BAR_IID \
{ 0x6f7652e1,  0xee43, 0x11d1, \
  { 0x9c, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }

class Bar : public Foo
{
public:
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_BAR_IID)

public:
  Bar();
  virtual ~Bar();

  NS_IMETHOD QueryInterface( const nsIID&, void** ) override;

  virtual void VirtualMemberFunction( int, int*, int& ) override;
  virtual void VirtualConstMemberFunction( int, int*, int& ) const override;

  static int total_constructions_;
  static int total_destructions_;
  static int total_queries_;
};

NS_DEFINE_STATIC_IID_ACCESSOR(Bar, NS_BAR_IID)

int Bar::total_constructions_;
int Bar::total_destructions_;
int Bar::total_queries_;

Bar::Bar()
{
  ++total_constructions_;
}

Bar::~Bar()
{
  ++total_destructions_;
}

nsresult
Bar::QueryInterface( const nsID& aIID, void** aResult )
{
  ++total_queries_;

  nsISupports* rawPtr = 0;
  nsresult status = NS_OK;

  if ( aIID.Equals(NS_GET_IID(Bar)) )
    rawPtr = this;
  else if ( aIID.Equals(NS_GET_IID(Foo)) )
    rawPtr = static_cast<Foo*>(this);
  else
  {
    nsID iid_of_ISupports = NS_ISUPPORTS_IID;
    if ( aIID.Equals(iid_of_ISupports) )
      rawPtr = static_cast<nsISupports*>(this);
    else
      status = NS_ERROR_NO_INTERFACE;
  }

  NS_IF_ADDREF(rawPtr);
  *aResult = rawPtr;

  return status;
}

void
Bar::VirtualMemberFunction( int aArg1, int* aArgPtr, int& aArgRef )
{
}
void
Bar::VirtualConstMemberFunction( int aArg1, int* aArgPtr, int& aArgRef ) const
{
}

} // namespace TestNsRefPtr

using namespace TestNsRefPtr;

TEST(nsRefPtr, AddRefAndRelease)
{
  Foo::total_constructions_ = 0;
  Foo::total_destructions_ = 0;

  {
    RefPtr<Foo> foop( do_QueryObject(new Foo) );
    ASSERT_EQ(Foo::total_constructions_, 1);
    ASSERT_EQ(Foo::total_destructions_, 0);
    ASSERT_EQ(foop->refcount_, 1);

    foop = do_QueryObject(new Foo);
    ASSERT_EQ(Foo::total_constructions_, 2);
    ASSERT_EQ(Foo::total_destructions_, 1);

    // [Shouldn't compile] Is it a compile time error to try to |AddRef| by hand?
    //foop->AddRef();

    // [Shouldn't compile] Is it a compile time error to try to |Release| be hand?
    //foop->Release();

    // [Shouldn't compile] Is it a compile time error to try to |delete| an |nsCOMPtr|?
    //delete foop;

    static_cast<Foo*>(foop)->AddRef();
    ASSERT_EQ(foop->refcount_, 2);

    static_cast<Foo*>(foop)->Release();
    ASSERT_EQ(foop->refcount_, 1);
  }

  ASSERT_EQ(Foo::total_destructions_, 2);

  {
    RefPtr<Foo> fooP( do_QueryObject(new Foo) );
    ASSERT_EQ(Foo::total_constructions_, 3);
    ASSERT_EQ(Foo::total_destructions_, 2);
    ASSERT_EQ(fooP->refcount_, 1);

    Foo::total_addrefs_ = 0;
    RefPtr<Foo> fooP2( fooP.forget() );
    ASSERT_EQ(Foo::total_addrefs_, 0);
  }
}

TEST(nsRefPtr, VirtualDestructor)
{
  Bar::total_destructions_ = 0;

  {
    RefPtr<Foo> foop( do_QueryObject(new Bar) );
    mozilla::Unused << foop;
  }

  ASSERT_EQ(Bar::total_destructions_, 1);
}

TEST(nsRefPtr, Equality)
{
  Foo::total_constructions_ = 0;
  Foo::total_destructions_ = 0;

  {
    RefPtr<Foo> foo1p( do_QueryObject(new Foo) );
    RefPtr<Foo> foo2p( do_QueryObject(new Foo) );

    ASSERT_EQ(Foo::total_constructions_, 2);
    ASSERT_EQ(Foo::total_destructions_, 0);

    ASSERT_NE(foo1p, foo2p);

    ASSERT_NE(foo1p, nullptr);
    ASSERT_NE(nullptr, foo1p);
    ASSERT_FALSE(foo1p == nullptr);
    ASSERT_FALSE(nullptr == foo1p);

    ASSERT_NE(foo1p, foo2p.get());

    foo1p = foo2p;

    ASSERT_EQ(Foo::total_constructions_, 2);
    ASSERT_EQ(Foo::total_destructions_, 1);
    ASSERT_EQ(foo1p, foo2p);

    ASSERT_EQ(foo2p, foo2p.get());

    ASSERT_EQ(RefPtr<Foo>(foo2p.get()), foo2p);

    ASSERT_TRUE(foo1p);
  }

  ASSERT_EQ(Foo::total_constructions_, 2);
  ASSERT_EQ(Foo::total_destructions_, 2);
}

TEST(nsRefPtr, AddRefHelpers)
{
  Foo::total_addrefs_ = 0;

  {
    Foo* raw_foo1p = new Foo;
    raw_foo1p->AddRef();

    Foo* raw_foo2p = new Foo;
    raw_foo2p->AddRef();

    ASSERT_EQ(Foo::total_addrefs_, 2);

    RefPtr<Foo> foo1p( dont_AddRef(raw_foo1p) );

    ASSERT_EQ(Foo::total_addrefs_, 2);

    RefPtr<Foo> foo2p;
    foo2p = dont_AddRef(raw_foo2p);

    ASSERT_EQ(Foo::total_addrefs_, 2);
  }

  {
    // Test that various assignment helpers compile.
    RefPtr<Foo> foop;
    CreateFoo( RefPtrGetterAddRefs<Foo>(foop) );
    CreateFoo( getter_AddRefs(foop) );
    set_a_Foo(address_of(foop));
    foop = return_a_Foo();
  }
}

TEST(nsRefPtr, QueryInterface)
{
  Foo::total_queries_ = 0;
  Bar::total_queries_ = 0;

  {
    RefPtr<Foo> fooP;
    fooP = do_QueryObject(new Foo);
    ASSERT_EQ(Foo::total_queries_, 1);
  }

  {
    RefPtr<Foo> fooP;
    fooP = do_QueryObject(new Foo);
    ASSERT_EQ(Foo::total_queries_, 2);

    RefPtr<Foo> foo2P;
    foo2P = fooP;
    ASSERT_EQ(Foo::total_queries_, 2);
  }

  {
    RefPtr<Bar> barP( do_QueryObject(new Bar) );
    ASSERT_EQ(Bar::total_queries_, 1);

    RefPtr<Foo> fooP( do_QueryObject(barP) );
    ASSERT_TRUE(fooP);
    ASSERT_EQ(Foo::total_queries_, 2);
    ASSERT_EQ(Bar::total_queries_, 2);
  }
}

// -------------------------------------------------------------------------
// TODO(ER): The following tests should be moved to MFBT.

#define NS_INLINE_DECL_THREADSAFE_MUTABLE_REFCOUNTING(_class)               \
public:                                                                     \
NS_METHOD_(MozExternalRefCountType) AddRef(void) const {                    \
  MOZ_ASSERT_TYPE_OK_FOR_REFCOUNTING(_class)                                \
  MOZ_RELEASE_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt");              \
  nsrefcnt count = ++mRefCnt;                                               \
  return (nsrefcnt) count;                                                  \
}                                                                           \
NS_METHOD_(MozExternalRefCountType) Release(void) const {                   \
  MOZ_RELEASE_ASSERT(int32_t(mRefCnt) > 0, "dup release");                  \
  nsrefcnt count = --mRefCnt;                                               \
  if (count == 0) {                                                         \
    delete (this);                                                          \
    return 0;                                                               \
  }                                                                         \
  return count;                                                             \
}                                                                           \
protected:                                                                  \
mutable ::mozilla::ThreadSafeAutoRefCnt mRefCnt;                            \
public:

class ObjectForConstPtr
{
  private:
    // Reference-counted classes cannot have public destructors.
    ~ObjectForConstPtr()
    {
    }
  public:
    NS_INLINE_DECL_THREADSAFE_MUTABLE_REFCOUNTING(ObjectForConstPtr)
      void ConstMemberFunction( int aArg1, int* aArgPtr, int& aArgRef ) const
      {
      }
};
#undef NS_INLINE_DECL_THREADSAFE_MUTABLE_REFCOUNTING

namespace TestNsRefPtr
{
void AnFooPtrPtrContext(Foo**) { }
void AVoidPtrPtrContext(void**) { }
} // namespace TestNsRefPtr

TEST(nsRefPtr, RefPtrCompilationTests)
{

  {
    RefPtr<Foo> fooP;

    AnFooPtrPtrContext( getter_AddRefs(fooP) );
    AVoidPtrPtrContext( getter_AddRefs(fooP) );
  }

  {
    RefPtr<Foo> fooP(new Foo);
    RefPtr<const Foo> constFooP = fooP;
    constFooP->ConstMethod();

    // [Shouldn't compile] Is it a compile time error to call a non-const method on an |RefPtr<const T>|?
    //constFooP->NonconstMethod();

    // [Shouldn't compile] Is it a compile time error to construct an |RefPtr<T> from an |RefPtr<const T>|?
    //RefPtr<Foo> otherFooP(constFooP);
  }

  {
    RefPtr<Foo> foop = new Foo;
    RefPtr<Foo> foop2 = new Bar;
    RefPtr<const ObjectForConstPtr> foop3 = new ObjectForConstPtr;
    int test = 1;
    void (Foo::*fPtr)( int, int*, int& ) = &Foo::MemberFunction;
    void (Foo::*fVPtr)( int, int*, int& ) = &Foo::VirtualMemberFunction;
    void (Foo::*fVCPtr)( int, int*, int& ) const = &Foo::VirtualConstMemberFunction;
    void (ObjectForConstPtr::*fCPtr2)( int, int*, int& ) const = &ObjectForConstPtr::ConstMemberFunction;

    (foop->*fPtr)(test, &test, test);
    (foop2->*fVPtr)(test, &test, test);
    (foop2->*fVCPtr)(test, &test, test);
    (foop3->*fCPtr2)(test, &test, test);
  }

  // Looks like everything ran.
  ASSERT_TRUE(true);
}