bug 1259753 - fix some C++ unittests to use ScopedXPCOM to init XPCOM. r?ms2ger draft
authorTed Mielczarek <ted@mielczarek.org>
Fri, 25 Mar 2016 10:04:37 -0400
changeset 344730 9b310f4c0222d84920586d27d52c076e3475b9e5
parent 344729 99c1d6087a2c01cf38342bbeca847010cd53ec05
child 517034 5d2f461c7c47c7b1b6d20bc139bb6309e3e3e5c9
push id13914
push usertmielczarek@mozilla.com
push dateFri, 25 Mar 2016 15:01:15 +0000
reviewersms2ger
bugs1259753
milestone48.0a1
bug 1259753 - fix some C++ unittests to use ScopedXPCOM to init XPCOM. r?ms2ger MozReview-Commit-ID: B6xdlB9Di0y
netwerk/test/TestCookie.cpp
security/manager/ssl/tests/compiled/TestCertDB.cpp
xpcom/tests/TestTArray.cpp
--- a/netwerk/test/TestCookie.cpp
+++ b/netwerk/test/TestCookie.cpp
@@ -1,14 +1,15 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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 "TestCommon.h"
+#include "TestHarness.h"
 #include "nsIServiceManager.h"
 #include "nsICookieService.h"
 #include "nsICookieManager.h"
 #include "nsICookieManager2.h"
 #include "nsICookie2.h"
 #include <stdio.h>
 #include "plstr.h"
 #include "prprf.h"
@@ -181,40 +182,26 @@ InitPrefs(nsIPrefBranch *aPrefBranch)
     aPrefBranch->SetIntPref(kCookiesPermissions, 0); // accept all
     aPrefBranch->SetBoolPref(kCookiesLifetimeEnabled, true);
     aPrefBranch->SetIntPref(kCookiesLifetimeCurrentSession, 0);
     aPrefBranch->SetIntPref(kCookiesLifetimeDays, 1);
     // Set the base domain limit to 50 so we have a known value.
     aPrefBranch->SetIntPref(kCookiesMaxPerHost, 50);
 }
 
-class ScopedXPCOM
-{
-public:
-  ScopedXPCOM() : rv(NS_InitXPCOM2(nullptr, nullptr, nullptr)) { }
-  ~ScopedXPCOM()
-  {
-    if (NS_SUCCEEDED(rv))
-      NS_ShutdownXPCOM(nullptr);
-  }
-
-  nsresult rv;
-};
 
 int
 main(int32_t argc, char *argv[])
 {
     if (test_common_init(&argc, &argv) != 0)
         return -1;
 
     bool allTestsPassed = true;
 
-    ScopedXPCOM xpcom;
-    if (NS_FAILED(xpcom.rv))
-      return -1;
+    ScopedXPCOM xpcom("TestCookie");
 
     {
       nsresult rv0;
 
       nsCOMPtr<nsICookieService> cookieService =
         do_GetService(kCookieServiceCID, &rv0);
       if (NS_FAILED(rv0)) return -1;
 
--- a/security/manager/ssl/tests/compiled/TestCertDB.cpp
+++ b/security/manager/ssl/tests/compiled/TestCertDB.cpp
@@ -2,33 +2,33 @@
 /* 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 "nsCOMPtr.h"
 #include "nsIPrefService.h"
 #include "nsIX509CertDB.h"
 #include "nsServiceManagerUtils.h"
+#include "TestHarness.h"
 
 int
 main(int argc, char* argv[])
 {
+  ScopedXPCOM xpcom("TestCertDB");
   {
-    NS_InitXPCOM2(nullptr, nullptr, nullptr);
     nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
     if (!prefs) {
       return -1;
     }
     // When NSS initializes, it attempts to get some localized strings.
     // As a result, Android flips out if this isn't set.
     nsresult rv = prefs->SetBoolPref("intl.locale.matchOS", true);
     if (NS_FAILED(rv)) {
       return -1;
     }
     nsCOMPtr<nsIX509CertDB> certdb(do_GetService(NS_X509CERTDB_CONTRACTID));
     if (!certdb) {
       return -1;
     }
   } // this scopes the nsCOMPtrs
   // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
-  NS_ShutdownXPCOM(nullptr);
   return 0;
 }
--- a/xpcom/tests/TestTArray.cpp
+++ b/xpcom/tests/TestTArray.cpp
@@ -13,16 +13,17 @@
 #include "nsTArray.h"
 #include "nsAutoPtr.h"
 #include "nsStringAPI.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsDirectoryServiceUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsXPCOM.h"
 #include "nsIFile.h"
+#include "TestHarness.h"
 
 using namespace mozilla;
 
 namespace TestTArray {
 
 // Define this so we can use test_basic_array in test_comptr_array
 template <class T>
 inline bool operator<(const nsCOMPtr<T>& lhs, const nsCOMPtr<T>& rhs) {
@@ -1189,24 +1190,22 @@ static const struct Test {
 
 using namespace TestTArray;
 
 int main(int argc, char **argv) {
   int count = 1;
   if (argc > 1)
     count = atoi(argv[1]);
 
-  if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr)))
-    return -1;
+  ScopedXPCOM xpcom("TestTArray");
 
   bool success = true;
   while (count--) {
     for (const Test* t = tests; t->name != nullptr; ++t) {
       bool test_result = t->func();
       printf("%25s : %s\n", t->name, test_result ? "SUCCESS" : "FAILURE");
       if (!test_result)
         success = false;
     }
   }
-  
-  NS_ShutdownXPCOM(nullptr);
+
   return success ? 0 : -1;
 }