VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/sample/nsTestSample.cpp@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
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/
9 *
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.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Suresh Duddi <[email protected]>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39
40/**
41 * A Test application that exercises the sample moudule. This is intented
42 * to be a sample application for using xpcom standalone.
43 */
44
45#include <stdio.h>
46
47#include "nsISample.h"
48#include "nsIServiceManager.h"
49#include "nsIComponentRegistrar.h"
50
51#ifdef XPCOM_GLUE
52#include "nsXPCOMGlue.h"
53#include "nsMemory.h"
54#else
55#include "nsXPIDLString.h"
56#endif
57
58#define NS_SAMPLE_CONTRACTID "@mozilla.org/sample;1"
59
60int
61main(void)
62{
63 nsresult rv;
64
65#ifdef XPCOM_GLUE
66 XPCOMGlueStartup(nsnull);
67#endif
68
69 // Initialize XPCOM
70 nsCOMPtr<nsIServiceManager> servMan;
71 rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
72 if (NS_FAILED(rv))
73 {
74 printf("ERROR: XPCOM intialization error [%x].\n", rv);
75 return -1;
76 }
77 // register all components in our default component directory
78 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
79 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
80 registrar->AutoRegister(nsnull);
81
82 nsCOMPtr<nsIComponentManager> manager = do_QueryInterface(registrar);
83 NS_ASSERTION(registrar, "Null nsIComponentManager");
84
85 // Create an instance of our component
86 nsCOMPtr<nsISample> mysample;
87 rv = manager->CreateInstanceByContractID(NS_SAMPLE_CONTRACTID,
88 nsnull,
89 NS_GET_IID(nsISample),
90 getter_AddRefs(mysample));
91 if (NS_FAILED(rv))
92 {
93 printf("ERROR: Cannot create instance of component " NS_SAMPLE_CONTRACTID " [%x].\n"
94 "Debugging hint:\n"
95 "\tsetenv NSPR_LOG_MODULES nsComponentManager:5\n"
96 "\tsetenv NSPR_LOG_FILE xpcom.log\n"
97 "\t./nsTestSample\n"
98 "\t<check the contents for xpcom.log for possible cause of error>.\n",
99 rv);
100 return -2;
101 }
102
103 // Call methods on our sample to test it out.
104 rv = mysample->WriteValue("Inital print:");
105 if (NS_FAILED(rv))
106 {
107 printf("ERROR: Calling nsISample::WriteValue() [%x]\n", rv);
108 return -3;
109 }
110
111 const char *testValue = "XPCOM defies gravity";
112 rv = mysample->SetValue(testValue);
113 if (NS_FAILED(rv))
114 {
115 printf("ERROR: Calling nsISample::SetValue() [%x]\n", rv);
116 return -3;
117 }
118 printf("Set value to: %s\n", testValue);
119#ifndef XPCOM_GLUE
120 nsXPIDLCString str;
121 rv = mysample->GetValue(getter_Copies(str));
122#else
123 char *str;
124 rv = mysample->GetValue(&str);
125#endif
126
127 if (NS_FAILED(rv))
128 {
129 printf("ERROR: Calling nsISample::GetValue() [%x]\n", rv);
130 return -3;
131 }
132 if (strcmp(str, testValue))
133 {
134 printf("Test FAILED.\n");
135 return -4;
136 }
137
138#ifdef XPCOM_GLUE
139 nsMemory::Free(str);
140#endif
141 rv = mysample->WriteValue("Final print :");
142 printf("Test passed.\n");
143
144 // All nsCOMPtr's must be deleted prior to calling shutdown XPCOM
145 // as we should not hold references passed XPCOM Shutdown.
146 servMan = 0;
147 registrar = 0;
148 manager = 0;
149 mysample = 0;
150
151 // Shutdown XPCOM
152 NS_ShutdownXPCOM(nsnull);
153
154#ifdef XPCOM_GLUE
155 XPCOMGlueShutdown();
156#endif
157 return 0;
158}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette