xpcom/tests/TestAutoRef.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     2 // vim:cindent:ts=4:et:sw=4:
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #include "nsAutoRef.h"
     8 #include "TestHarness.h"
    10 #define TEST(aCondition, aMsg) \
    11   if (!(aCondition)) { fail("TestAutoRef: "#aMsg); exit(1); }
    13 struct TestObjectA {
    14 public:
    15   TestObjectA() : mRefCnt(0) {
    16   }
    18   ~TestObjectA() {
    19     TEST(mRefCnt == 0, "mRefCnt in destructor");
    20   }
    22 public:
    23   int mRefCnt;
    24 };
    26 template <>
    27 class nsAutoRefTraits<TestObjectA> : public nsPointerRefTraits<TestObjectA>
    28 {
    29 public:
    30   static int mTotalRefsCnt;
    32   static void Release(TestObjectA *ptr) {
    33     ptr->mRefCnt--;
    34     if (ptr->mRefCnt == 0) {
    35       delete ptr;
    36     }
    37   }
    39   static void AddRef(TestObjectA *ptr) {
    40     ptr->mRefCnt++;
    41   }
    42 };
    44 int nsAutoRefTraits<TestObjectA>::mTotalRefsCnt = 0;
    46 int main()
    47 {
    48   {
    49     nsCountedRef<TestObjectA> a(new TestObjectA());
    50     TEST(a->mRefCnt == 1, "nsCountedRef instantiation with valid RawRef");
    52     nsCountedRef<TestObjectA> b;
    53     TEST(b.get() == nullptr, "nsCountedRef instantiation with invalid RawRef");
    55     a.swap(b);
    56     TEST(b->mRefCnt, "nsAutoRef::swap() t1");
    57     TEST(a.get() == nullptr, "nsAutoRef::swap() t2");
    58   }
    60   TEST(true, "All tests pass");
    61   return 0;
    62 }

mercurial