LCOV - code coverage report
Current view: directory - toolkit/components/places/tests/cpp - places_test_harness_tail.h (source / functions) Found Hit Coverage
Test: app.info Lines: 53 50 94.3 %
Date: 2012-06-02 Functions: 12 11 91.7 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
       2                 :  * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
       3                 :  * ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is places test code.
      17                 :  *
      18                 :  * The Initial Developer of the Original Code is
      19                 :  * the Mozilla Foundation.
      20                 :  * Portions created by the Initial Developer are Copyright (C) 2009
      21                 :  * the Initial Developer. All Rights Reserved.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *   Shawn Wilsher <me@shawnwilsher.com> (Original Author)
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #include "nsWidgetsCID.h"
      41                 : #include "nsIComponentRegistrar.h"
      42                 : 
      43                 : #ifndef TEST_NAME
      44                 : #error "Must #define TEST_NAME before including places_test_harness_tail.h"
      45                 : #endif
      46                 : 
      47                 : #ifndef TEST_FILE
      48                 : #error "Must #define TEST_FILE before include places_test_harness_tail.h"
      49                 : #endif
      50                 : 
      51                 : int gTestsIndex = 0;
      52                 : 
      53                 : #define TEST_INFO_STR "TEST-INFO | (%s) | "
      54                 : 
      55                 : class RunNextTest : public nsRunnable
      56              90 : {
      57                 : public:
      58              18 :   NS_IMETHOD Run()
      59                 :   {
      60              18 :     NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
      61              18 :     if (gTestsIndex < int(mozilla::ArrayLength(gTests))) {
      62              17 :       do_test_pending();
      63              17 :       Test &test = gTests[gTestsIndex++];
      64                 :       (void)fprintf(stderr, TEST_INFO_STR "Running %s.\n", TEST_FILE,
      65              17 :                     test.name);
      66              17 :       test.func();
      67                 :     }
      68                 : 
      69              18 :     do_test_finished();
      70              18 :     return NS_OK;
      71                 :   }
      72                 : };
      73                 : 
      74                 : void
      75              18 : run_next_test()
      76                 : {
      77              36 :   nsCOMPtr<nsIRunnable> event = new RunNextTest();
      78              18 :   do_check_success(NS_DispatchToCurrentThread(event));
      79              18 : }
      80                 : 
      81                 : int gPendingTests = 0;
      82                 : 
      83                 : void
      84              18 : do_test_pending()
      85                 : {
      86              18 :   NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
      87              18 :   gPendingTests++;
      88              18 : }
      89                 : 
      90                 : void
      91              18 : do_test_finished()
      92                 : {
      93              18 :   NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
      94              18 :   NS_ASSERTION(gPendingTests > 0, "Invalid pending test count!");
      95              18 :   gPendingTests--;
      96              18 : }
      97                 : 
      98                 : void
      99               1 : disable_idle_service()
     100                 : {
     101               1 :   (void)fprintf(stderr, TEST_INFO_STR  "Disabling Idle Service.\n", TEST_FILE);
     102                 :   static NS_DEFINE_IID(kIdleCID, NS_IDLE_SERVICE_CID);
     103                 :   nsresult rv;
     104               2 :   nsCOMPtr<nsIFactory> idleFactory = do_GetClassObject(kIdleCID, &rv);
     105               1 :   do_check_success(rv);
     106               2 :   nsCOMPtr<nsIComponentRegistrar> registrar;
     107               1 :   rv = NS_GetComponentRegistrar(getter_AddRefs(registrar));
     108               1 :   do_check_success(rv);
     109               1 :   rv = registrar->UnregisterFactory(kIdleCID, idleFactory);
     110               1 :   do_check_success(rv);
     111               1 : }
     112                 : 
     113                 : int
     114               1 : main(int aArgc,
     115                 :      char** aArgv)
     116                 : {
     117               2 :   ScopedXPCOM xpcom(TEST_NAME);
     118               1 :   if (xpcom.failed())
     119               0 :     return -1;
     120                 :   // Initialize a profile folder to ensure a clean shutdown.
     121               2 :   nsCOMPtr<nsIFile> profile = xpcom.GetProfileDirectory();
     122               1 :   if (!profile) {
     123               0 :     fail("Couldn't get the profile directory.");
     124               0 :     return -1;
     125                 :   }
     126                 : 
     127               2 :   nsRefPtr<WaitForConnectionClosed> spinClose = new WaitForConnectionClosed();
     128                 : 
     129                 :   // Tinderboxes are constantly on idle.  Since idle tasks can interact with
     130                 :   // tests, causing random failures, disable the idle service.
     131               1 :   disable_idle_service();
     132                 : 
     133               1 :   do_test_pending();
     134               1 :   run_next_test();
     135                 : 
     136                 :   // Spin the event loop until we've run out of tests to run.
     137              34 :   while (gPendingTests) {
     138              32 :     (void)NS_ProcessNextEvent();
     139                 :   }
     140                 : 
     141                 :   // And let any other events finish before we quit.
     142               1 :   (void)NS_ProcessPendingEvents(nsnull);
     143                 : 
     144                 :   // Check that we have passed all of our tests, and output accordingly.
     145               1 :   if (gPassedTests == gTotalTests) {
     146               1 :     passed(TEST_FILE);
     147                 :   }
     148                 : 
     149                 :   (void)fprintf(stderr, TEST_INFO_STR  "%u of %u tests passed\n",
     150               1 :                 TEST_FILE, unsigned(gPassedTests), unsigned(gTotalTests));
     151                 : 
     152               1 :   return gPassedTests == gTotalTests ? 0 : -1;
     153               3 : }

Generated by: LCOV version 1.7