Bug 400795 - initial form fill and username autocomplete should share common code...
[wine-gecko.git] / xpcom / tests / TestHarness.h
blob1127a6d180d6bd8aac1045fea26581b4f2a4aa91
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Jeff Walden <jwalden+code@mit.edu>.
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * Test harness for XPCOM objects, providing a scoped XPCOM initializer,
40 * nsCOMPtr, nsRefPtr, do_CreateInstance, do_GetService, ns(Auto|C|)String,
41 * and stdio.h/stdlib.h.
44 #ifndef TestHarness_h__
45 #define TestHarness_h__
47 #include "nsComponentManagerUtils.h"
48 #include "nsServiceManagerUtils.h"
49 #include "nsCOMPtr.h"
50 #include "nsAutoPtr.h"
51 #include "nsStringGlue.h"
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <stdarg.h>
56 /**
57 * Prints the given failure message and arguments using printf, prepending
58 * "FAIL " for the benefit of the test harness and appending "\n" to eliminate
59 * having to type it at each call site.
61 void fail(const char* msg, ...)
63 va_list ap;
65 printf("FAIL ");
67 va_start(ap, msg);
68 vprintf(msg, ap);
69 va_end(ap);
71 putchar('\n');
74 /**
75 * Prints the given string followed by " PASSED!\n", to be used at the end
76 * of a successful test function.
78 void passed(const char* test)
80 printf("%s PASSED!\n", test);
84 class ScopedXPCOM
86 public:
87 ScopedXPCOM(const char* testName,
88 nsIDirectoryServiceProvider *dirSvcProvider = NULL)
90 mTestName = testName;
91 printf("Running %s tests...\n", mTestName);
93 nsresult rv = NS_InitXPCOM2(&mServMgr, NULL, dirSvcProvider);
94 if (NS_FAILED(rv))
96 fail("NS_InitXPCOM2 returned failure code 0x%x", rv);
97 mServMgr = NULL;
101 ~ScopedXPCOM()
103 if (mServMgr)
105 NS_RELEASE(mServMgr);
106 nsresult rv = NS_ShutdownXPCOM(NULL);
107 if (NS_FAILED(rv))
109 fail("XPCOM shutdown failed with code 0x%x", rv);
110 exit(1);
114 printf("Finished running %s tests.\n", mTestName);
117 PRBool failed()
119 return mServMgr == NULL;
122 private:
123 const char* mTestName;
124 nsIServiceManager* mServMgr;
127 #endif // TestHarness_h__