Bug 1313469 - Part 2: Convert TestCOMArray to a gtest. r=froydnj
☠☠ backed out by 561b87e98ada ☠ ☠
authorEric Rahm <erahm@mozilla.com>
Thu, 03 Nov 2016 17:55:21 -0700
changeset 347685 82bf00ff65052d93ec3dbc337bf223df9bb8b75d
parent 347684 5364fc8db9b0e29c209f58ea6e06fd9d980436d9
child 347686 e7d5b8135ae61384a51c0b909e5eb2aac9e6e035
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1313469
milestone52.0a1
Bug 1313469 - Part 2: Convert TestCOMArray to a gtest. r=froydnj MozReview-Commit-ID: CEhXX60lpZ8
testing/cppunittest.ini
xpcom/tests/TestCOMArray.cpp
xpcom/tests/gtest/TestCOMArray.cpp
xpcom/tests/gtest/moz.build
xpcom/tests/moz.build
xpcom/threads/StateWatching.h
xpcom/threads/TaskDispatcher.h
--- a/testing/cppunittest.ini
+++ b/testing/cppunittest.ini
@@ -6,17 +6,16 @@
 [TestAudioBuffers]
 skip-if = os == 'b2g'  # Bug 1062937
 [TestAudioMixer]
 [TestBinarySearch]
 [TestBind]
 [TestBloomFilter]
 [TestCOM]
 skip-if = os != 'win'
-[TestCOMArray]
 [TestCOMPtr]
 [TestCOMPtrEq]
 [TestCSPParser]
 skip-if = os == 'b2g' || (os == 'android' && debug) # Bug 1054246
 [TestCasting]
 [TestCeilingFloor]
 [TestCertDB]
 [TestCheckedInt]
rename from xpcom/tests/TestCOMArray.cpp
rename to xpcom/tests/gtest/TestCOMArray.cpp
--- a/xpcom/tests/TestCOMArray.cpp
+++ b/xpcom/tests/gtest/TestCOMArray.cpp
@@ -1,17 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 // vim:cindent:ts=4:et:sw=4:
 /* 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 "TestHarness.h"
 #include "nsCOMArray.h"
-#include "mozilla/Attributes.h"
+#include "gtest/gtest.h"
 
 // {9e70a320-be02-11d1-8031-006008159b5a}
 #define NS_IFOO_IID \
   {0x9e70a320, 0xbe02, 0x11d1,    \
     {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
 
 class IFoo : public nsISupports {
 public:
@@ -54,19 +53,16 @@ Foo::Foo(int32_t aID)
 Foo::~Foo()
 {
   --gCount;
 }
 
 NS_IMPL_ISUPPORTS(Foo, IFoo)
 
 
-typedef nsCOMArray<IFoo> Array;
-
-
 // {0e70a320-be02-11d1-8031-006008159b5a}
 #define NS_IBAR_IID \
   {0x0e70a320, 0xbe02, 0x11d1,    \
     {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
 
 class IBar : public nsISupports {
 public:
 
@@ -97,93 +93,74 @@ typedef nsCOMArray<IBar> Array2;
 
 Bar::Bar(Array2& aArray)
   : mArray(aArray)
 {
 }
 
 Bar::~Bar()
 {
-  if (mArray.RemoveObject(this)) {
-    fail("We should never manage to remove the object here");
-  }
+  EXPECT_FALSE(mArray.RemoveObject(this));
 }
 
 NS_IMPL_ADDREF(Bar)
 NS_IMPL_QUERY_INTERFACE(Bar, IBar)
 
 NS_IMETHODIMP_(MozExternalRefCountType)
 Bar::Release(void)
 {
   ++Bar::sReleaseCalled;
-  MOZ_RELEASE_ASSERT(int32_t(mRefCnt) > 0, "dup release");
+  EXPECT_GT(int(mRefCnt), 0);
   NS_ASSERT_OWNINGTHREAD(_class);
   --mRefCnt;
   NS_LOG_RELEASE(this, mRefCnt, "Bar");
   if (mRefCnt == 0) {
     mRefCnt = 1; /* stabilize */
     delete this;
     return 0;
   }
   return mRefCnt;
 }
 
-void Sizing()
+TEST(COMArray, Sizing)
 {
-  int rv = 0;
-
-  Array arr;
+  nsCOMArray<IFoo> arr;
 
   for (int32_t i = 0; i < 20; ++i) {
     nsCOMPtr<IFoo> foo = new Foo(i);
     arr.AppendObject(foo);
   }
 
-  if (arr.Count() != 20 || Foo::gCount != 20) {
-    fail("nsCOMArray::AppendObject failed");
-    rv = 1;
-  }
+  ASSERT_EQ(arr.Count(), int32_t(20));
+  ASSERT_EQ(Foo::gCount, int32_t(20));
 
   arr.TruncateLength(10);
 
-  if (arr.Count() != 10 || Foo::gCount != 10) {
-    fail("nsCOMArray::TruncateLength shortening of array failed");
-    rv = 1;
-  }
+  ASSERT_EQ(arr.Count(), int32_t(10));
+  ASSERT_EQ(Foo::gCount, int32_t(10));
 
   arr.SetCount(30);
 
-  if (arr.Count() != 30 || Foo::gCount != 10) {
-    fail("nsCOMArray::SetCount lengthening of array failed");
-    rv = 1;
-  }
+  ASSERT_EQ(arr.Count(), int32_t(30));
+  ASSERT_EQ(Foo::gCount, int32_t(10));
 
   for (int32_t i = 0; i < 10; ++i) {
-	if (arr[i] == nullptr) {
-      fail("nsCOMArray elements should be non-null");
-      rv = 1;
-	  break;
-    }
+	ASSERT_NE(arr[i], nullptr);
   }
 
   for (int32_t i = 10; i < 30; ++i) {
-	if (arr[i] != nullptr) {
-      fail("nsCOMArray elements should be null");
-      rv = 1;
-	  break;
-    }
+	ASSERT_EQ(arr[i], nullptr);
   }
-
 }
 
-void ObjectFunctions()
+TEST(COMArray, ObjectFunctions)
 {
   int32_t base;
   {
-    Array2 arr2;
+    nsCOMArray<IBar> arr2;
 
     IBar *thirdObject = nullptr,
          *fourthObject = nullptr,
          *fifthObject = nullptr,
          *ninthObject = nullptr;
     for (int32_t i = 0; i < 20; ++i) {
       nsCOMPtr<IBar> bar = new Bar(arr2);
       switch (i) {
@@ -197,78 +174,50 @@ void ObjectFunctions()
         ninthObject = bar; break;
       }
       arr2.AppendObject(bar);
     }
 
     base = Bar::sReleaseCalled;
 
     arr2.SetCount(10);
-    if (Bar::sReleaseCalled != base + 10) {
-      fail("Release called multiple times for SetCount");
-    }
-    if (arr2.Count() != 10) {
-      fail("SetCount(10) should remove exactly ten objects");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 10);
+    ASSERT_EQ(arr2.Count(), int32_t(10));
 
     arr2.RemoveObjectAt(9);
-    if (Bar::sReleaseCalled != base + 11) {
-      fail("Release called multiple times for RemoveObjectAt");
-    }
-    if (arr2.Count() != 9) {
-      fail("RemoveObjectAt should remove exactly one object");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 11);
+    ASSERT_EQ(arr2.Count(), int32_t(9));
 
     arr2.RemoveObject(ninthObject);
-    if (Bar::sReleaseCalled != base + 12) {
-      fail("Release called multiple times for RemoveObject");
-    }
-    if (arr2.Count() != 8) {
-      fail("RemoveObject should remove exactly one object");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 12);
+    ASSERT_EQ(arr2.Count(), int32_t(8));
 
     arr2.RemoveObjectsAt(2, 3);
-    if (Bar::sReleaseCalled != base + 15) {
-      fail("Release called more or less than three times for RemoveObjectsAt");
-    }
-    if (arr2.Count() != 5) {
-      fail("RemoveObjectsAt should remove exactly three objects");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 15);
+    ASSERT_EQ(arr2.Count(), int32_t(5));
     for (int32_t j = 0; j < arr2.Count(); ++j) {
-      if (arr2.ObjectAt(j) == thirdObject) {
-        fail("RemoveObjectsAt should have removed thirdObject");
-      }
-      if (arr2.ObjectAt(j) == fourthObject) {
-        fail("RemoveObjectsAt should have removed fourthObject");
-      }
-      if (arr2.ObjectAt(j) == fifthObject) {
-        fail("RemoveObjectsAt should have removed fifthObject");
-      }
+      ASSERT_NE(arr2.ObjectAt(j), thirdObject);
+      ASSERT_NE(arr2.ObjectAt(j), fourthObject);
+      ASSERT_NE(arr2.ObjectAt(j), fifthObject);
     }
 
     arr2.RemoveObjectsAt(4, 1);
-    if (Bar::sReleaseCalled != base + 16) {
-      fail("Release called more or less than one time for RemoveObjectsAt");
-    }
-    if (arr2.Count() != 4) {
-      fail("RemoveObjectsAt should work for removing the last element");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 16);
+    ASSERT_EQ(arr2.Count(), int32_t(4));
 
     arr2.Clear();
-    if (Bar::sReleaseCalled != base + 20) {
-      fail("Release called multiple times for Clear");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 20);
   }
 }
 
-void ElementFunctions()
+TEST(COMArray, ElementFunctions)
 {
   int32_t base;
   {
-    Array2 arr2;
+    nsCOMArray<IBar> arr2;
 
     IBar *thirdElement = nullptr,
          *fourthElement = nullptr,
          *fifthElement = nullptr,
          *ninthElement = nullptr;
     for (int32_t i = 0; i < 20; ++i) {
       nsCOMPtr<IBar> bar = new Bar(arr2);
       switch (i) {
@@ -282,101 +231,56 @@ void ElementFunctions()
         ninthElement = bar; break;
       }
       arr2.AppendElement(bar);
     }
 
     base = Bar::sReleaseCalled;
 
     arr2.TruncateLength(10);
-    if (Bar::sReleaseCalled != base + 10) {
-      fail("Release called multiple times for TruncateLength");
-    }
-    if (arr2.Length() != 10) {
-      fail("TruncateLength(10) should remove exactly ten objects");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 10);
+    ASSERT_EQ(arr2.Length(), uint32_t(10));
 
     arr2.RemoveElementAt(9);
-    if (Bar::sReleaseCalled != base + 11) {
-      fail("Release called multiple times for RemoveElementAt");
-    }
-    if (arr2.Length() != 9) {
-      fail("RemoveElementAt should remove exactly one object");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 11);
+    ASSERT_EQ(arr2.Length(), uint32_t(9));
 
     arr2.RemoveElement(ninthElement);
-    if (Bar::sReleaseCalled != base + 12) {
-      fail("Release called multiple times for RemoveElement");
-    }
-    if (arr2.Length() != 8) {
-      fail("RemoveElement should remove exactly one object");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 12);
+    ASSERT_EQ(arr2.Length(), uint32_t(8));
 
     arr2.RemoveElementsAt(2, 3);
-    if (Bar::sReleaseCalled != base + 15) {
-      fail("Release called more or less than three times for RemoveElementsAt");
-    }
-    if (arr2.Length() != 5) {
-      fail("RemoveElementsAt should remove exactly three objects");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 15);
+    ASSERT_EQ(arr2.Length(), uint32_t(5));
     for (uint32_t j = 0; j < arr2.Length(); ++j) {
-      if (arr2.ElementAt(j) == thirdElement) {
-        fail("RemoveElementsAt should have removed thirdElement");
-      }
-      if (arr2.ElementAt(j) == fourthElement) {
-        fail("RemoveElementsAt should have removed fourthElement");
-      }
-      if (arr2.ElementAt(j) == fifthElement) {
-        fail("RemoveElementsAt should have removed fifthElement");
-      }
+      ASSERT_NE(arr2.ElementAt(j), thirdElement);
+      ASSERT_NE(arr2.ElementAt(j), fourthElement);
+      ASSERT_NE(arr2.ElementAt(j), fifthElement);
     }
 
     arr2.RemoveElementsAt(4, 1);
-    if (Bar::sReleaseCalled != base + 16) {
-      fail("Release called more or less than one time for RemoveElementsAt");
-    }
-    if (arr2.Length() != 4) {
-      fail("RemoveElementsAt should work for removing the last element");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 16);
+    ASSERT_EQ(arr2.Length(), uint32_t(4));
 
     arr2.Clear();
-    if (Bar::sReleaseCalled != base + 20) {
-      fail("Release called multiple times for Clear");
-    }
+    ASSERT_EQ(Bar::sReleaseCalled, base + 20);
   }
 }
 
-void Destructor()
+TEST(COMArray, Destructor)
 {
   int32_t base;
   Bar::sReleaseCalled = 0;
 
   {
-    Array2 arr2;
+    nsCOMArray<IBar> arr2;
 
     for (int32_t i = 0; i < 20; ++i) {
       nsCOMPtr<IBar> bar  = new Bar(arr2);
       arr2.AppendObject(bar);
     }
 
     base = Bar::sReleaseCalled;
 
     // Let arr2 be destroyed
   }
-  if (Bar::sReleaseCalled != base + 20) {
-    fail("Release called multiple times for nsCOMArray::~nsCOMArray");
-  }
+  ASSERT_EQ(Bar::sReleaseCalled, base + 20);
 }
-
-int main(int argc, char **argv)
-{
-  ScopedXPCOM xpcom("nsCOMArrayTests");
-  if (xpcom.failed()) {
-    return 1;
-  }
-
-  Sizing();
-  ObjectFunctions();
-  ElementFunctions();
-  Destructor();
-
-  return 0;
-}
--- a/xpcom/tests/gtest/moz.build
+++ b/xpcom/tests/gtest/moz.build
@@ -5,16 +5,17 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 UNIFIED_SOURCES += [
     'Helpers.cpp',
     'TestAtoms.cpp',
     'TestAutoPtr.cpp',
     'TestAutoRef.cpp',
     'TestCloneInputStream.cpp',
+    'TestCOMArray.cpp',
     'TestCRT.cpp',
     'TestEncoding.cpp',
     'TestEscapeURL.cpp',
     'TestExpirationTracker.cpp',
     'TestNSPRLogModulesParser.cpp',
     'TestPipes.cpp',
     'TestPLDHash.cpp',
     'TestPriorityQueue.cpp',
--- a/xpcom/tests/moz.build
+++ b/xpcom/tests/moz.build
@@ -41,17 +41,16 @@ if CONFIG['OS_TARGET'] == 'WINNT':
 if CONFIG['WRAP_STL_INCLUDES'] and not CONFIG['CLANG_CL']:
     GeckoSimplePrograms([
         'TestSTLWrappers',
     ])
 
 XPCSHELL_TESTS_MANIFESTS += ['unit/xpcshell.ini']
 
 GeckoCppUnitTests([
-    'TestCOMArray',
     'TestCOMPtr',
     'TestCOMPtrEq',
     'TestFile',
     'TestHashtables',
     'TestID',
     'TestNsRefPtr',
     'TestObserverArray',
     'TestObserverService',
--- a/xpcom/threads/StateWatching.h
+++ b/xpcom/threads/StateWatching.h
@@ -3,16 +3,17 @@
 /* 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/. */
 
 #if !defined(StateWatching_h_)
 #define StateWatching_h_
 
 #include "mozilla/AbstractThread.h"
+#include "mozilla/Logging.h"
 #include "mozilla/TaskDispatcher.h"
 #include "mozilla/UniquePtr.h"
 #include "mozilla/Unused.h"
 
 #include "nsISupportsImpl.h"
 
 /*
  * The state-watching machinery automates the process of responding to changes
--- a/xpcom/threads/TaskDispatcher.h
+++ b/xpcom/threads/TaskDispatcher.h
@@ -3,16 +3,17 @@
 /* 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/. */
 
 #if !defined(TaskDispatcher_h_)
 #define TaskDispatcher_h_
 
 #include "mozilla/AbstractThread.h"
+#include "mozilla/Maybe.h"
 #include "mozilla/UniquePtr.h"
 #include "mozilla/Unused.h"
 
 #include "nsISupportsImpl.h"
 #include "nsTArray.h"
 #include "nsThreadUtils.h"
 
 #include <queue>