Bug 1316424 - Convert XPCOM test TestBase64 to a gtest. r=bsmedberg
authorEric Rahm <erahm@mozilla.com>
Thu, 10 Nov 2016 13:01:01 -0800
changeset 352132 e4900824c7eba29f5f93949f03fa99d8a738de69
parent 352131 d1b9fbbc9710402f0b0da8025819534bb98c9321
child 352133 58fb3bb7a39765788927a8e8dfa184f2a3f73479
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs1316424
milestone52.0a1
Bug 1316424 - Convert XPCOM test TestBase64 to a gtest. r=bsmedberg MozReview-Commit-ID: 89kKMhkzwXK
xpcom/tests/TestBase64.cpp
xpcom/tests/gtest/TestBase64.cpp
xpcom/tests/gtest/moz.build
xpcom/tests/moz.build
rename from xpcom/tests/TestBase64.cpp
rename to xpcom/tests/gtest/TestBase64.cpp
--- a/xpcom/tests/TestBase64.cpp
+++ b/xpcom/tests/gtest/TestBase64.cpp
@@ -1,21 +1,19 @@
 /* -*- Mode: C; tab-width: 8; 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 "TestHarness.h"
-
 #include "mozilla/Attributes.h"
 #include "nsIScriptableBase64Encoder.h"
 #include "nsIInputStream.h"
-#include "nsAutoPtr.h"
-#include "nsStringAPI.h"
-#include <wchar.h>
+#include "nsString.h"
+
+#include "gtest/gtest.h"
 
 struct Chunk {
   Chunk(uint32_t l, const char* c)
     : mLength(l), mData(c)
   {}
 
   uint32_t mLength;
   const char* mData;
@@ -156,18 +154,18 @@ public:
     mClosed(false)
   {}
 
   NS_DECL_ISUPPORTS
   NS_DECL_NSIINPUTSTREAM
 
   void Reset();
   bool NextTest();
-  bool CheckTest(nsACString& aResult);
-  bool CheckTest(nsAString& aResult);
+  void CheckTest(nsACString& aResult);
+  void CheckTest(nsAString& aResult);
 private:
   uint32_t mTestNumber;
   const Test* mTest;
   const Chunk* mChunk;
   bool mClosed;
 };
 
 NS_IMPL_ISUPPORTS(FakeInputStream, nsIInputStream)
@@ -248,60 +246,46 @@ FakeInputStream::NextTest()
   mTestNumber++;
   mTest = &kTests[mTestNumber];
   mChunk = &mTest->mChunks[0];
   mClosed = false;
 
   return mTest->mChunks ? true : false;
 }
 
-bool
+void
 FakeInputStream::CheckTest(nsACString& aResult)
 {
-  return !strcmp(aResult.BeginReading(), mTest->mResult) ? true : false;
+  ASSERT_STREQ(aResult.BeginReading(), mTest->mResult);
 }
 
-#ifdef XP_WIN
-static inline int NS_tstrcmp(char16ptr_t x, char16ptr_t y) {
-    return wcscmp(x, y);
-}
-#else
-#define NS_tstrcmp strcmp
-#endif
-
-bool
+void
 FakeInputStream::CheckTest(nsAString& aResult)
 {
-  return !NS_tstrcmp(aResult.BeginReading(),
-                     NS_ConvertASCIItoUTF16(mTest->mResult).BeginReading())
-                     ? true : false;
+  ASSERT_TRUE(aResult.EqualsASCII(mTest->mResult)) <<
+    "Actual:   " << aResult.BeginReading() << std::endl <<
+    "Expected: " << mTest->mResult;
 }
 
-int main(int argc, char** argv)
+TEST(Base64, Test)
 {
-  ScopedXPCOM xpcom("Base64");
-  NS_ENSURE_FALSE(xpcom.failed(), 1);
-
   nsCOMPtr<nsIScriptableBase64Encoder> encoder =
     do_CreateInstance("@mozilla.org/scriptablebase64encoder;1");
-  NS_ENSURE_TRUE(encoder, 1);
+  ASSERT_TRUE(encoder);
 
   RefPtr<FakeInputStream> stream = new FakeInputStream();
   do {
     nsString wideString;
     nsCString string;
 
     nsresult rv;
     rv = encoder->EncodeToString(stream, 0, wideString);
-    NS_ENSURE_SUCCESS(rv, 1);
+    ASSERT_TRUE(NS_SUCCEEDED(rv));
 
     stream->Reset();
 
     rv = encoder->EncodeToCString(stream, 0, string);
-    NS_ENSURE_SUCCESS(rv, 1);
-
-    if (!stream->CheckTest(wideString) || !stream->CheckTest(string))
-      fail("Failed to convert properly\n");
+    ASSERT_TRUE(NS_SUCCEEDED(rv));
 
+    stream->CheckTest(wideString);
+    stream->CheckTest(string);
   } while (stream->NextTest());
-
-  return 0;
 }
--- a/xpcom/tests/gtest/moz.build
+++ b/xpcom/tests/gtest/moz.build
@@ -4,16 +4,17 @@
 # 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/.
 
 UNIFIED_SOURCES += [
     'Helpers.cpp',
     'TestAtoms.cpp',
     'TestAutoPtr.cpp',
     'TestAutoRef.cpp',
+    'TestBase64.cpp',
     'TestCallTemplates.cpp',
     'TestCloneInputStream.cpp',
     'TestCOMArray.cpp',
     'TestCOMPtrEq.cpp',
     'TestCRT.cpp',
     'TestEncoding.cpp',
     'TestEscapeURL.cpp',
     'TestExpirationTracker.cpp',
@@ -57,16 +58,15 @@ if CONFIG['MOZ_DEBUG'] and CONFIG['OS_AR
 
 # Compile TestAllocReplacement separately so Windows headers don't pollute
 # the global namespace for other files.
 SOURCES += [
     'TestAllocReplacement.cpp',
     'TestCOMPtr.cpp', # Redefines IFoo and IBar
     'TestHashtables.cpp', # Redefines IFoo
     'TestNsRefPtr.cpp', # Redefines Foo
-    #'TestTArray2.cpp',
 ]
 
 LOCAL_INCLUDES += [
     '../../base',
 ]
 
 FINAL_LIBRARY = 'xul-gtest'
--- a/xpcom/tests/moz.build
+++ b/xpcom/tests/moz.build
@@ -22,21 +22,16 @@ EXPORTS.testing += [
 SimplePrograms([
     'TestArguments',
     'TestBlockingProcess',
     'TestPRIntN',
     'TestQuickReturn',
     'TestUnicodeArguments',
 ])
 
-if CONFIG['OS_TARGET'] == 'WINNT':
-    GeckoSimplePrograms([
-        'TestBase64',
-    ])
-
 if CONFIG['WRAP_STL_INCLUDES'] and not CONFIG['CLANG_CL']:
     GeckoSimplePrograms([
         'TestSTLWrappers',
     ])
 
 XPCSHELL_TESTS_MANIFESTS += ['unit/xpcshell.ini']
 
 # FIXME: bug 577500 TestStaticAtoms fails when run in dist/bin