Bug 1316904 - Convert windows XPCOM tests to gtest. r=froydnj
authorEric Rahm <erahm@mozilla.com>
Fri, 11 Nov 2016 12:02:52 -0800
changeset 367391 66f3aa6ea71b76f3121f19384ec8e2e7dd18eada
parent 367390 47c1673055172688a6e596115c01a4e978f41118
child 367392 1b76383a51828b2171699ae35af4403fe5ac20e6
push id1369
push userjlorenzo@mozilla.com
push dateMon, 27 Feb 2017 14:59:41 +0000
treeherdermozilla-release@d75a1dba431f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1316904
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1316904 - Convert windows XPCOM tests to gtest. r=froydnj This converts TestNtPathToDosPath and TestCOM to gtests. MozReview-Commit-ID: EeNwvsj4cAs
testing/cppunittest.ini
xpcom/tests/windows/TestCOM.cpp
xpcom/tests/windows/TestNtPathToDosPath.cpp
xpcom/tests/windows/moz.build
--- a/testing/cppunittest.ini
+++ b/testing/cppunittest.ini
@@ -1,18 +1,16 @@
 [ShowSSEConfig]
 [TestAppShellSteadyState]
 [TestArray]
 [TestArrayUtils]
 [TestAtomics]
 [TestBinarySearch]
 [TestBind]
 [TestBloomFilter]
-[TestCOM]
-skip-if = os != 'win'
 [TestCasting]
 [TestCeilingFloor]
 [TestCertDB]
 [TestCheckedInt]
 [TestCookie]
 [TestCountPopulation]
 [TestCountZeroes]
 [TestDllInterceptor]
@@ -28,18 +26,16 @@ skip-if = os != 'win'
 [TestIntegerRange]
 [TestIsCertBuiltInRoot]
 [TestJSONWriter]
 [TestLinkedList]
 [TestMacroArgs]
 [TestMacroForEach]
 [TestMathAlgorithms]
 [TestMaybe]
-[TestNtPathToDosPath]
-skip-if = os != 'win'
 [TestPLDHash]
 skip-if = os == 'b2g'  #Bug 1038197
 [TestPair]
 [TestPoisonArea]
 skip-if = os == 'android' # Bug 1147630
 [TestRefPtr]
 [TestRollingMean]
 [TestScopeExit]
--- a/xpcom/tests/windows/TestCOM.cpp
+++ b/xpcom/tests/windows/TestCOM.cpp
@@ -7,16 +7,18 @@
 #include <unknwn.h>
 #include <stdio.h>
 #include "nsISupports.h"
 #include "nsIFactory.h"
 
 // unknwn.h is needed to build with WIN32_LEAN_AND_MEAN
 #include <unknwn.h>
 
+#include "gtest/gtest.h"
+
 // {5846BA30-B856-11d1-A98A-00805F8A7AC4}
 #define NS_ITEST_COM_IID \
 { 0x5846ba30, 0xb856, 0x11d1, \
   { 0xa9, 0x8a, 0x0, 0x80, 0x5f, 0x8a, 0x7a, 0xc4 } }
 
 class nsITestCom: public nsISupports
 {
 public:
@@ -33,116 +35,124 @@ NS_DEFINE_STATIC_IID_ACCESSOR(nsITestCom
 class nsTestCom final : public nsITestCom {
   NS_DECL_ISUPPORTS
 
 public:
   nsTestCom() {
   }
 
   NS_IMETHOD Test() {
-    printf("Accessed nsITestCom::Test() from COM\n");
     return NS_OK;
   }
 
+  static int sDestructions;
+
 private:
   ~nsTestCom() {
-    printf("nsTestCom instance successfully deleted\n");
+    sDestructions++;
   }
 };
 
+int nsTestCom::sDestructions;
+
 NS_IMPL_QUERY_INTERFACE(nsTestCom, nsITestCom)
 
 MozExternalRefCountType nsTestCom::AddRef()
 {
   nsrefcnt res = ++mRefCnt;
   NS_LOG_ADDREF(this, mRefCnt, "nsTestCom", sizeof(*this));
-  printf("nsTestCom: Adding ref = %p\n", (void *)res);
   return res;
 }
 
 MozExternalRefCountType nsTestCom::Release()
 {
   nsrefcnt res = --mRefCnt;
   NS_LOG_RELEASE(this, mRefCnt, "nsTestCom");
-  printf("nsTestCom: Releasing = %p\n", (void *)res);
   if (res == 0) {
     delete this;
   }
   return res;
 }
 
 class nsTestComFactory final : public nsIFactory {
-  ~nsTestComFactory() {}
+  ~nsTestComFactory() { sDestructions++; }
   NS_DECL_ISUPPORTS
 public:
   nsTestComFactory() {
   }
-  
+
   NS_IMETHOD CreateInstance(nsISupports *aOuter,
                             const nsIID &aIID,
                             void **aResult);
 
   NS_IMETHOD LockFactory(bool aLock) {
-    printf("nsTestComFactory: ");
-    printf("%s", (aLock ? "Locking server" : "Unlocking server"));
-    printf("\n");
     return NS_OK;
   }
+
+  static int sDestructions;
 };
 
+int nsTestComFactory::sDestructions;
+
 NS_IMPL_ISUPPORTS(nsTestComFactory, nsIFactory)
 
 nsresult nsTestComFactory::CreateInstance(nsISupports *aOuter,
 					  const nsIID &aIID,
 					  void **aResult)
 {
   if (aOuter != nullptr) {
     return NS_ERROR_NO_AGGREGATION;
   }
 
   nsTestCom *t = new nsTestCom();
-  
+
   if (t == nullptr) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
-  
+
   NS_ADDREF(t);
   nsresult res = t->QueryInterface(aIID, aResult);
   NS_RELEASE(t);
 
-  if (NS_SUCCEEDED(res)) {
-    printf("nsTestComFactory: successfully created nsTestCom instance\n");
-  }
-
   return res;
 }
 
-/*
- * main
- */
-
-int main(int argc, char *argv[])
+TEST(TestCOM, WindowsInterop)
 {
   nsTestComFactory *inst = new nsTestComFactory();
-  IClassFactory *iFactory;
-  inst->QueryInterface(NS_GET_IID(nsIFactory), (void **) &iFactory);
 
-  IUnknown *iUnknown;  
-  nsITestCom *iTestCom;
+  // Test we can QI nsIFactory to an IClassFactory.
+  IClassFactory *iFactory = nullptr;
+  nsresult rv = inst->QueryInterface(NS_GET_IID(nsIFactory),
+                                      (void **) &iFactory);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+  ASSERT_TRUE(iFactory);
+
+  // Test we can CreateInstance with an IUnknown.
+  IUnknown *iUnknown = nullptr;
 
-  iFactory->LockServer(TRUE);
-  iFactory->CreateInstance(nullptr, IID_IUnknown, (void **) &iUnknown);
-  iFactory->LockServer(FALSE);
+  HRESULT hr = iFactory->LockServer(TRUE);
+  ASSERT_TRUE(SUCCEEDED(hr));
+  hr = iFactory->CreateInstance(nullptr, IID_IUnknown, (void **) &iUnknown);
+  ASSERT_TRUE(SUCCEEDED(hr));
+  ASSERT_TRUE(iUnknown);
+  hr = iFactory->LockServer(FALSE);
+  ASSERT_TRUE(SUCCEEDED(hr));
 
+  // Test we can QI an IUnknown to nsITestCom.
+  nsITestCom *iTestCom = nullptr;
   GUID testGUID = NS_ITEST_COM_IID;
-  HRESULT hres;
-  hres= iUnknown->QueryInterface(testGUID, 
+  hr = iUnknown->QueryInterface(testGUID,
 				 (void **) &iTestCom);
+  ASSERT_TRUE(SUCCEEDED(hr));
+  ASSERT_TRUE(iTestCom);
 
-  iTestCom->Test();
+  // Make sure we can call our test function (and the pointer is valid).
+  rv = iTestCom->Test();
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   iUnknown->Release();
   iTestCom->Release();
   iFactory->Release();
 
-  return 0;
+  ASSERT_EQ(nsTestComFactory::sDestructions, 1);
+  ASSERT_EQ(nsTestCom::sDestructions, 1);
 }
-
--- a/xpcom/tests/windows/TestNtPathToDosPath.cpp
+++ b/xpcom/tests/windows/TestNtPathToDosPath.cpp
@@ -1,23 +1,23 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 <windows.h>
 #include <winnetwk.h>
 
 #include "mozilla/FileUtilsWin.h"
 #include "mozilla/DebugOnly.h"
 #include "nsCRTGlue.h"
 
+#include "gtest/gtest.h"
+
 class DriveMapping
 {
 public:
   DriveMapping(const nsAString& aRemoteUNCPath);
   ~DriveMapping();
 
   bool
   Init();
@@ -122,113 +122,72 @@ DriveToNtPath(const wchar_t aDriveLetter
 }
 
 bool
 TestNtPathToDosPath(const wchar_t* aNtPath,
                     const wchar_t* aExpectedDosPath)
 {
   nsAutoString output;
   bool result = mozilla::NtPathToDosPath(nsDependentString(aNtPath), output);
-  return result && output == aExpectedDosPath;
+  return result && output == reinterpret_cast<const nsAString::char_type *>(aExpectedDosPath);
 }
 
-int main(int argc, char* argv[])
+TEST(NtPathToDosPath, Tests)
 {
-  ScopedXPCOM xpcom("NtPathToDosPath");
-  if (xpcom.failed()) {
-    fail("XPCOM Startup");
-    return 1;
-  }
   nsAutoString cDrive;
-  if (!DriveToNtPath(L'C', cDrive)) {
-    fail("Querying for this machine's C:");
-    return 1;
-  }
-
-  int result = 0;
+  ASSERT_TRUE(DriveToNtPath(L'C', cDrive));
 
   // empty string
-  if (!TestNtPathToDosPath(L"", L"")) {
-    fail("Empty string");
-    result = 1;
-  }
+  EXPECT_TRUE(TestNtPathToDosPath(L"", L""));
+
   // non-existent device, must fail
-  if (TestNtPathToDosPath(L"\\Device\\ThisDeviceDoesNotExist\\Foo", nullptr)) {
-    fail("Non-existent device");
-    result = 1;
-  }
+  EXPECT_FALSE(TestNtPathToDosPath(L"\\Device\\ThisDeviceDoesNotExist\\Foo", nullptr));
+
   // base case
   nsAutoString testPath(cDrive);
   testPath.Append(L"\\Program Files");
-  if (!TestNtPathToDosPath(testPath.get(), L"C:\\Program Files")) {
-    fail("Base case");
-    result = 1;
-  }
+  EXPECT_TRUE(TestNtPathToDosPath(testPath.get(), L"C:\\Program Files"));
+
   // short filename
   nsAutoString ntShortName(cDrive);
   ntShortName.Append(L"\\progra~1");
-  if (!TestNtPathToDosPath(ntShortName.get(), L"C:\\Program Files")) {
-    fail("Short file name");
-    result = 1;
-  }
+  EXPECT_TRUE(TestNtPathToDosPath(ntShortName.get(), L"C:\\Program Files"));
+
   // drive letters as symbolic links (NtCreateFile uses these)
-  if (!TestNtPathToDosPath(L"\\??\\C:\\Foo", L"C:\\Foo")) {
-    fail("Path specified as symbolic link");
-    result = 1;
-  }
+  EXPECT_TRUE(TestNtPathToDosPath(L"\\??\\C:\\Foo", L"C:\\Foo"));
+
   // other symbolic links (should fail)
-  if (TestNtPathToDosPath(L"\\??\\MountPointManager", nullptr)) {
-    fail("Other symbolic link");
-    result = 1;
-  }
+  EXPECT_FALSE(TestNtPathToDosPath(L"\\??\\MountPointManager", nullptr));
+
   // socket (should fail)
-  if (TestNtPathToDosPath(L"\\Device\\Afd\\Endpoint", nullptr)) {
-    fail("Socket");
-    result = 1;
-  }
+  EXPECT_FALSE(TestNtPathToDosPath(L"\\Device\\Afd\\Endpoint", nullptr));
+
   // UNC path (using MUP)
-  if (!TestNtPathToDosPath(L"\\Device\\Mup\\127.0.0.1\\C$",
-                           L"\\\\127.0.0.1\\C$")) {
-    fail("Unmapped UNC path (\\Device\\Mup\\)");
-    result = 1;
-  }
+  EXPECT_TRUE(TestNtPathToDosPath(L"\\Device\\Mup\\127.0.0.1\\C$",
+                           L"\\\\127.0.0.1\\C$"));
+
   // UNC path (using LanmanRedirector)
-  if (!TestNtPathToDosPath(L"\\Device\\LanmanRedirector\\127.0.0.1\\C$",
-                           L"\\\\127.0.0.1\\C$")) {
-    fail("Unmapped UNC path (\\Device\\LanmanRedirector\\)");
-    result = 1;
-  }
+  EXPECT_TRUE(TestNtPathToDosPath(L"\\Device\\LanmanRedirector\\127.0.0.1\\C$",
+                           L"\\\\127.0.0.1\\C$"));
+
   DriveMapping drvMapping(NS_LITERAL_STRING("\\\\127.0.0.1\\C$"));
   // Only run these tests if we were able to map; some machines don't have perms
   if (drvMapping.Init()) {
     wchar_t expected[] = L" :\\";
     expected[0] = drvMapping.GetDriveLetter();
     nsAutoString networkPath;
-    if (!DriveToNtPath(drvMapping.GetDriveLetter(), networkPath)) {
-      fail("Querying network drive");
-      return 1;
-    }
+    ASSERT_TRUE(DriveToNtPath(drvMapping.GetDriveLetter(), networkPath));
+
     networkPath += u"\\";
-    if (!TestNtPathToDosPath(networkPath.get(), expected)) {
-      fail("Mapped UNC path");
-      result = 1;
-    }
+    EXPECT_TRUE(TestNtPathToDosPath(networkPath.get(), expected));
+
     // NtPathToDosPath must correctly handle paths whose drive letter mapping has
     // changed. We need to test this because the APIs called by NtPathToDosPath
     // return different info if this has happened.
-    if (!drvMapping.ChangeDriveLetter()) {
-      fail("Change drive letter");
-      return 1;
-    }
+    ASSERT_TRUE(drvMapping.ChangeDriveLetter());
+
     expected[0] = drvMapping.GetDriveLetter();
-    if (!DriveToNtPath(drvMapping.GetDriveLetter(), networkPath)) {
-      fail("Querying second network drive");
-      return 1;
-    }
+    ASSERT_TRUE(DriveToNtPath(drvMapping.GetDriveLetter(), networkPath));
+
     networkPath += u"\\";
-    if (!TestNtPathToDosPath(networkPath.get(), expected)) {
-      fail("Re-mapped UNC path");
-      result = 1;
-    }
+    EXPECT_TRUE(TestNtPathToDosPath(networkPath.get(), expected));
   }
-
-  return result;
 }
--- a/xpcom/tests/windows/moz.build
+++ b/xpcom/tests/windows/moz.build
@@ -1,16 +1,16 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # 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/.
 
-GeckoCppUnitTests([
-    'TestCOM',
-    'TestNtPathToDosPath',
-])
+UNIFIED_SOURCES += [
+    'TestCOM.cpp',
+    'TestNtPathToDosPath.cpp',
+]
 
 OS_LIBS += [
-    'rpcrt4',
-    'uuid',
     'mpr',
 ]
+
+FINAL_LIBRARY = 'xul-gtest'