Bug 369034 - TestTXMgr has error and leaks object; (Cv1) Functional cleanup and documentation; r=peterv
authorSerge Gautherie <sgautherie.bz@free.fr>
Tue, 09 Jun 2009 01:15:36 +0200
changeset 25914 db8c7dd92ece010dfaa56575ba1dbfb060cab6c7
parent 25913 4578911cc1430d446860286be6cf0f45b94d7cd1
child 25915 6aca7b6b2ca83c03bdc46b4fd19059980c87873f
push id1668
push usersgautherie.bz@free.fr
push dateMon, 08 Jun 2009 23:20:45 +0000
reviewerspeterv
bugs369034
milestone1.9.1pre
Bug 369034 - TestTXMgr has error and leaks object; (Cv1) Functional cleanup and documentation; r=peterv
editor/txmgr/tests/TestTXMgr.cpp
--- a/editor/txmgr/tests/TestTXMgr.cpp
+++ b/editor/txmgr/tests/TestTXMgr.cpp
@@ -444,30 +444,32 @@ public:
 
   NS_DECL_ISUPPORTS
 };
 
 NS_IMPL_ISUPPORTS1(TestTransaction, nsITransaction)
 
 class SimpleTransaction : public TestTransaction
 {
-public:
+protected:
 
 #define NONE_FLAG               0
 #define THROWS_DO_ERROR_FLAG    1
 #define THROWS_UNDO_ERROR_FLAG  2
 #define THROWS_REDO_ERROR_FLAG  4
 #define MERGE_FLAG              8
 #define TRANSIENT_FLAG         16
 #define BATCH_FLAG             32
 #define ALL_ERROR_FLAGS        (THROWS_DO_ERROR_FLAG|THROWS_UNDO_ERROR_FLAG|THROWS_REDO_ERROR_FLAG)
 
   PRInt32 mVal;
   PRInt32 mFlags;
 
+public:
+
   SimpleTransaction(PRInt32 aFlags=NONE_FLAG)
                     : mVal(++sConstructorCount), mFlags(aFlags)
   {}
 
   virtual ~SimpleTransaction()
   {
     //
     // Make sure transactions are being destroyed in the order we expect!
@@ -590,27 +592,27 @@ private:
     mTXMgr              = aTXMgr;
     mFlags              = aFlags & (~ALL_ERROR_FLAGS);
     mErrorFlags         = aFlags & ALL_ERROR_FLAGS;
     mTXMgr              = aTXMgr;
     mMaxLevel           = aMaxLevel;
     mNumChildrenPerNode = aNumChildrenPerNode;
   }
 
-
-public:
   nsITransactionManager *mTXMgr;
 
   PRInt32 mLevel;
   PRInt32 mNumber;
   PRInt32 mErrorFlags;
 
   PRInt32 mMaxLevel;
   PRInt32 mNumChildrenPerNode;
 
+public:
+
   AggregateTransaction(nsITransactionManager *aTXMgr,
                        PRInt32 aMaxLevel, PRInt32 aNumChildrenPerNode,
                        PRInt32 aFlags=NONE_FLAG)
   {
     mLevel              = 1;
     mNumber             = 1;
     mFlags              = aFlags & (~ALL_ERROR_FLAGS);
     mErrorFlags         = aFlags & ALL_ERROR_FLAGS;
@@ -730,22 +732,24 @@ public:
   TestTransaction *create(nsITransactionManager *txmgr, PRInt32 flags)
   {
     return (TestTransaction *)new SimpleTransaction(flags);
   }
 };
 
 class AggregateTransactionFactory : public TestTransactionFactory
 {
-public:
+private:
 
   PRInt32 mMaxLevel;
   PRInt32 mNumChildrenPerNode;
   PRInt32 mFixedFlags;
 
+public:
+
   AggregateTransactionFactory(PRInt32 aMaxLevel, PRInt32 aNumChildrenPerNode,
                               PRInt32 aFixedFlags=NONE_FLAG)
       : mMaxLevel(aMaxLevel), mNumChildrenPerNode(aNumChildrenPerNode),
         mFixedFlags(aFixedFlags)
   {
   }
 
   virtual TestTransaction *create(nsITransactionManager *txmgr, PRInt32 flags)
@@ -769,16 +773,19 @@ reset_globals()
 
   sUndoCount          = 0;
   sUndoOrderArr       = 0;
 
   sRedoCount          = 0;
   sRedoOrderArr       = 0;
 }
 
+/**
+ * Test behaviors in non-batch mode.
+ **/
 nsresult
 quick_test(TestTransactionFactory *factory)
 {
   /*******************************************************************
    *
    * Create a transaction manager implementation:
    *
    *******************************************************************/
@@ -805,18 +812,17 @@ quick_test(TestTransactionFactory *facto
    *
    * Call DoTransaction() with a null transaction:
    *
    *******************************************************************/
 
   printf("Call DoTransaction() with null transaction ... ");
   result = mgr->DoTransaction(0);
 
-  if (NS_FAILED(result)
-      && result != NS_ERROR_NULL_POINTER) {
+  if (result != NS_ERROR_NULL_POINTER) {
     printf("ERROR: DoTransaction() returned unexpected error. (%d)\n", result);
     return result;
   }
 
   printf("passed\n");
 
   /*******************************************************************
    *
@@ -1017,48 +1023,46 @@ quick_test(TestTransactionFactory *facto
    * Call AddListener() with a null listener pointer:
    *
    *******************************************************************/
 
   printf("Call AddListener() with null listener ... ");
 
   result = mgr->AddListener(0);
 
-  if (NS_FAILED(result)
-      && result != NS_ERROR_NULL_POINTER) {
+  if (result != NS_ERROR_NULL_POINTER) {
     printf("ERROR: AddListener() returned unexpected error. (%d)\n", result);
     return result;
   }
 
   printf("passed\n");
 
   /*******************************************************************
    *
    * Call RemoveListener() with a null listener pointer:
    *
    *******************************************************************/
 
   printf("Call RemoveListener() with null listener ... ");
 
   result = mgr->RemoveListener(0);
 
-  if (NS_FAILED(result)
-      && result != NS_ERROR_NULL_POINTER) {
+  if (result != NS_ERROR_NULL_POINTER) {
     printf("ERROR: RemoveListener() returned unexpected error. (%d)\n", result);
     return result;
   }
 
   printf("passed\n");
 
   /*******************************************************************
    *
    * Test coalescing by executing a transaction that can merge any
    * command into itself. Then execute 20 transaction. Afterwards,
    * we should still have the first transaction sitting on the undo
-   * stack.
+   * stack. Then clear the undo and redo stacks.
    *
    *******************************************************************/
 
   printf("Test coalescing of transactions ... ");
 
   result = mgr->SetMaxTransactionCount(10);
 
   if (NS_FAILED(result)) {
@@ -1724,17 +1728,17 @@ quick_test(TestTransactionFactory *facto
 
   if (NS_FAILED(result)) {
     printf("ERROR: Initial PeekRedoStack() failed. (%d)\n", result);
     return result;
   }
 
   result = mgr->DoTransaction(tx);
 
-  if (NS_FAILED(result) && result != NS_ERROR_FAILURE) {
+  if (result != NS_ERROR_FAILURE) {
     printf("ERROR: DoTransaction() returned unexpected error. (%d)\n", result);
     return result;
   }
 
   tx->Release();
 
   result = mgr->PeekUndoStack(&u2);
 
@@ -1844,17 +1848,17 @@ quick_test(TestTransactionFactory *facto
 
   if (NS_FAILED(result)) {
     printf("ERROR: Initial PeekRedoStack() failed. (%d)\n", result);
     return result;
   }
 
   result = mgr->UndoTransaction();
 
-  if (NS_FAILED(result) && result != NS_ERROR_FAILURE) {
+  if (result != NS_ERROR_FAILURE) {
     printf("ERROR: UndoTransaction() returned unexpected error. (%d)\n", result);
     return result;
   }
 
   result = mgr->PeekUndoStack(&u2);
 
   TEST_TXMGR_IF_RELEASE(u2); // Don't hold onto any references!
 
@@ -2008,17 +2012,17 @@ quick_test(TestTransactionFactory *facto
 
   if (NS_FAILED(result)) {
     printf("ERROR: Initial PeekRedoStack() failed. (%d)\n", result);
     return result;
   }
 
   result = mgr->RedoTransaction();
 
-  if (NS_FAILED(result) && result != NS_ERROR_FAILURE) {
+  if (result != NS_ERROR_FAILURE) {
     printf("ERROR: RedoTransaction() returned unexpected error. (%d)\n", result);
     return result;
   }
 
   result = mgr->PeekUndoStack(&u2);
 
   TEST_TXMGR_IF_RELEASE(u2); // Don't hold onto any references!
 
@@ -2505,17 +2509,17 @@ quick_test(TestTransactionFactory *facto
 
   TEST_TXMGR_IF_RELEASE(u2); // Don't hold onto any references!
 
   if (NS_FAILED(result)) {
     printf("ERROR: Second PeekUndoStack() failed. (%d)\n", result);
     return result;
   }
 
-  if (u1 == u2 || u2) {
+  if (u2) {
     printf("ERROR: Unexpected item at top of undo stack. (%d)\n", result);
     return NS_ERROR_FAILURE;
   }
 
   result = mgr->PeekRedoStack(&r2);
 
   TEST_TXMGR_IF_RELEASE(r2); // Don't hold onto any references!
 
@@ -2684,21 +2688,16 @@ quick_test(TestTransactionFactory *facto
   printf("Number of transactions created and destroyed match ... ");
 
   if (sConstructorCount != sDestructorCount) {
     printf("ERROR: Transaction constructor count (%d) != destructor count (%d).\n",
            sConstructorCount, sDestructorCount);
     return NS_ERROR_FAILURE;
   }
 
-  if (NS_FAILED(result)) {
-    printf("ERROR: nsITransactionManager Release() failed. (%d)\n", result);
-    return result;
-  }
-
   printf("passed\n");
   printf("%d transactions processed during quick test.\n", sConstructorCount);
 
   return NS_OK;
 }
 
 nsresult
 simple_test()
@@ -2754,16 +2753,19 @@ aggregation_test()
 
   printf("\n-----------------------------------------------------\n");
   printf("- Begin Aggregate Transaction Test:\n");
   printf("-----------------------------------------------------\n");
 
   return quick_test(&factory);
 }
 
+/**
+ * Test behaviors in batch mode.
+ **/
 nsresult
 quick_batch_test(TestTransactionFactory *factory)
 {
   /*******************************************************************
    *
    * Create a transaction manager implementation:
    *
    *******************************************************************/
@@ -3752,17 +3754,17 @@ quick_batch_test(TestTransactionFactory 
 
   if (NS_FAILED(result)) {
     printf("ERROR: BeginBatch() failed. (%d)\n", result);
     return result;
   }
 
   result = mgr->DoTransaction(tx);
 
-  if (NS_FAILED(result) && result != NS_ERROR_FAILURE) {
+  if (result != NS_ERROR_FAILURE) {
     printf("ERROR: DoTransaction() returned unexpected error. (%d)\n", result);
     return result;
   }
 
   tx->Release();
 
   result = mgr->EndBatch();
 
@@ -3893,17 +3895,17 @@ quick_batch_test(TestTransactionFactory 
 
   if (NS_FAILED(result)) {
     printf("ERROR: Initial PeekRedoStack() failed. (%d)\n", result);
     return result;
   }
 
   result = mgr->UndoTransaction();
 
-  if (NS_FAILED(result) && result != NS_ERROR_FAILURE) {
+  if (result != NS_ERROR_FAILURE) {
     printf("ERROR: UndoTransaction() returned unexpected error. (%d)\n", result);
     return result;
   }
 
   result = mgr->PeekUndoStack(&u2);
 
   TEST_TXMGR_IF_RELEASE(u2); // Don't hold onto any references!
 
@@ -4071,17 +4073,17 @@ quick_batch_test(TestTransactionFactory 
 
   if (NS_FAILED(result)) {
     printf("ERROR: Initial PeekRedoStack() failed. (%d)\n", result);
     return result;
   }
 
   result = mgr->RedoTransaction();
 
-  if (NS_FAILED(result) && result != NS_ERROR_FAILURE) {
+  if (result != NS_ERROR_FAILURE) {
     printf("ERROR: RedoTransaction() returned unexpected error. (%d)\n", result);
     return result;
   }
 
   result = mgr->PeekUndoStack(&u2);
 
   TEST_TXMGR_IF_RELEASE(u2); // Don't hold onto any references!
 
@@ -4392,21 +4394,16 @@ quick_batch_test(TestTransactionFactory 
   printf("Number of transactions created and destroyed match ... ");
 
   if (sConstructorCount != sDestructorCount) {
     printf("ERROR: Transaction constructor count (%d) != destructor count (%d).\n",
            sConstructorCount, sDestructorCount);
     return NS_ERROR_FAILURE;
   }
 
-  if (NS_FAILED(result)) {
-    printf("ERROR: nsITransactionManager Release() failed. (%d)\n", result);
-    return result;
-  }
-
   printf("passed\n");
   printf("%d transactions processed during quick batch test.\n",
          sConstructorCount);
 
   return NS_OK;
 }
 
 nsresult
@@ -4463,16 +4460,20 @@ aggregation_batch_test()
 
   printf("\n-----------------------------------------------------\n");
   printf("- Begin Batch Aggregate Transaction Test:\n");
   printf("-----------------------------------------------------\n");
 
   return quick_batch_test(&factory);
 }
 
+/**
+ * Create 'iterations * (iterations + 1) / 2' transactions;
+ * do/undo/redo/undo them.
+ **/
 nsresult
 stress_test(TestTransactionFactory *factory, PRInt32 iterations)
 {
   /*******************************************************************
    *
    * Create a transaction manager:
    *
    *******************************************************************/
@@ -4489,17 +4490,17 @@ stress_test(TestTransactionFactory *fact
   if (NS_FAILED(result) || !mgr) {
     printf("ERROR: Failed to create Transaction Manager instance.\n");
     return NS_ERROR_OUT_OF_MEMORY;
   }
 
   for (i = 1; i <= iterations; i++) {
     /*******************************************************************
      *
-     * Execute a bunch of transactions:
+     * Create and execute a bunch of transactions:
      *
      *******************************************************************/
 
     for (j = 1; j <= i; j++) {
       TestTransaction *tximpl = factory->create(mgr, NONE_FLAG);
 
       if (!tximpl) {
         printf("ERROR: Failed to allocate transaction %d.\n", j);
@@ -4678,45 +4679,44 @@ int
 main (int argc, char *argv[])
 {
   ScopedXPCOM xpcom("nsITransactionManager");
   if (xpcom.failed())
     return 1;
 
   nsresult result;
 
+  //
+  // quick_test() part:
+  //
+
   result = simple_test();
-
-  if (NS_FAILED(result))
-    return result;
+  NS_ENSURE_SUCCESS(result, result);
+
+  result = aggregation_test();
+  NS_ENSURE_SUCCESS(result, result);
+
+  //
+  // quick_batch_test() part:
+  //
 
   result = simple_batch_test();
-
-  if (NS_FAILED(result))
-    return result;
-
-  result = aggregation_test();
-
-  if (NS_FAILED(result))
-    return result;
+  NS_ENSURE_SUCCESS(result, result);
 
   result = aggregation_batch_test();
-
-  if (NS_FAILED(result))
-    return result;
+  NS_ENSURE_SUCCESS(result, result);
+
+  //
+  // stress_test() part:
+  //
 
   result = simple_stress_test();
-
-  if (NS_FAILED(result))
-    return result;
+  NS_ENSURE_SUCCESS(result, result);
 
   result = aggregation_stress_test();
-
-  if (NS_FAILED(result))
-    return result;
+  NS_ENSURE_SUCCESS(result, result);
 
   result = aggregation_batch_stress_test();
-
-  if (NS_FAILED(result))
-    return result;
+  NS_ENSURE_SUCCESS(result, result);
+
 
   return NS_OK;
 }