[base] Add stub 'mutableArray' interface to 'fact'
[abstract.git] / base / aaBaseTest.cpp
blobcccbf57ac84b8e63af8e5863e3cbbc0b5be73b06
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */
3 /*
4 * Copyright (C) 2007 Sergey Yanovich <ynvich@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include <abstract/aacore.h>
24 #include <stdio.h>
25 #include "nsStringAPI.h"
26 #include "nsIGenericFactory.h"
27 #include "nsComponentManagerUtils.h"
28 #include "nsEmbedString.h"
29 #include "nsIMutableArray.h"
31 /* Unfrozen API */
32 #include <abstract/cxxunit/nsCxxUnit.h>
33 #include <abstract/cxxunit/nsITest.h>
34 #include <abstract/cxxunit/nsITestRunner.h>
36 /* Project includes */
37 #include <abstract/storage/aaIQuery.h>
38 #include <abstract/base/aaIEntity.h>
39 #include <abstract/base/aaIObject.h>
40 #include <abstract/base/aaIFlow.h>
41 #include <abstract/base/aaIAction.h>
42 #include <abstract/base/aaIFact.h>
43 #include "aaObject.h"
44 #include "aaEntity.h"
45 #include "aaFlow.h"
46 #include "aaAction.h"
47 #include "aaFact.h"
49 #define AA_BASE_TEST_CID \
50 {0x502c6e1e, 0x42c4, 0x46b1, {0xa4, 0x7b, 0x19, 0x42, 0x8a, 0xd6, 0x75, 0x58}}
51 #define AA_BASE_TEST_CONTRACT_ID "@aasii.org/base/unit;1"
53 class aaBaseTest: public nsITest,
54 public aaIQuery
56 public:
57 aaBaseTest();
58 virtual ~aaBaseTest();
59 NS_DECL_ISUPPORTS
60 NS_DECL_NSITEST
61 NS_DECL_AAIQUERY
62 private:
63 #define AA_BASE_TEST_ENTITY 1
64 #define AA_BASE_TEST_OBJECT 2
65 #define AA_BASE_TEST_FLOW 4
66 PRUint32 mPackQueries;
67 PRUint32 mPackParam;
69 nsresult testEntity(nsITestRunner *aTestRunner);
70 nsresult testObject(nsITestRunner *aTestRunner);
71 nsresult testFlow(nsITestRunner *aTestRunner);
72 nsresult testAction(nsITestRunner *aTestRunner);
73 nsresult testFact(nsITestRunner *aTestRunner);
76 aaBaseTest::aaBaseTest()
80 aaBaseTest::~aaBaseTest()
84 NS_IMPL_ISUPPORTS1(aaBaseTest, nsITest);
86 NS_IMETHODIMP
87 aaBaseTest::Test(nsITestRunner *aTestRunner)
89 nsITestRunner *cxxUnitTestRunner = aTestRunner;
90 NS_TEST_ASSERT_OK(testEntity(aTestRunner));
91 NS_TEST_ASSERT_OK(testObject(aTestRunner));
92 NS_TEST_ASSERT_OK(testFlow(aTestRunner));
93 NS_TEST_ASSERT_OK(testAction(aTestRunner));
94 NS_TEST_ASSERT_OK(testFact(aTestRunner));
95 return NS_OK;
98 /* XXX Remove it */
99 NS_IMETHODIMP
100 aaBaseTest::GetAsync(PRBool *aAsync)
102 NS_ENSURE_ARG_POINTER( aAsync );
103 *aAsync = PR_FALSE;
104 return NS_OK;
107 /* aaIQuery */
108 NS_IMETHODIMP
109 aaBaseTest::QueryEntity(aaIEntity *aEntity)
111 mPackQueries |= AA_BASE_TEST_ENTITY;
112 if (aEntity)
113 mPackParam |= AA_BASE_TEST_ENTITY;
114 return NS_OK;
117 NS_IMETHODIMP
118 aaBaseTest::QueryObject(aaIObject *aObject)
120 mPackQueries |= AA_BASE_TEST_OBJECT;
121 if (aObject)
122 mPackParam |= AA_BASE_TEST_OBJECT;
123 return NS_OK;
126 NS_IMETHODIMP
127 aaBaseTest::QueryFlow(aaIFlow *aFlow)
129 mPackQueries |= AA_BASE_TEST_FLOW;
130 if (aFlow)
131 mPackParam |= AA_BASE_TEST_FLOW;
132 return NS_OK;
135 /* Private methods */
136 nsresult
137 aaBaseTest::testEntity(nsITestRunner *aTestRunner)
139 NS_TEST_BEGIN( aTestRunner );
140 nsresult rv;
141 nsCOMPtr<aaIEntity> entity(do_CreateInstance(AA_ENTITY_CONTRACT_ID, &rv));
142 NS_TEST_ASSERT_MSG(entity, "aaEntity instance creation" );
143 NS_ENSURE_SUCCESS(rv, rv);
144 nsCOMPtr<aaIDataNode> item(do_QueryInterface(entity, &rv));
145 NS_TEST_ASSERT_MSG(item, "aaEntity aaIDataNode interface" );
146 NS_ENSURE_SUCCESS(rv, rv);
148 mPackParam = mPackQueries = 0;
149 rv = item->Accept(this);
150 NS_TEST_ASSERT_MSG(mPackQueries & AA_BASE_TEST_ENTITY,
151 "aaEntity->Accept doesn't return query" );
152 NS_TEST_ASSERT_MSG(!(mPackQueries & ~AA_BASE_TEST_ENTITY),
153 "aaEntity->Accept returns incorrect query" );
154 NS_TEST_ASSERT_MSG(mPackParam & AA_BASE_TEST_ENTITY,
155 "aaEntity->Accept doesn't provide valid parameter" );
156 NS_TEST_ASSERT_OK(rv);
158 aaEntity* raw = NS_STATIC_CAST(aaEntity *, entity.get());
159 nsEmbedString wstr(NS_LITERAL_STRING("init"));
160 NS_TEST_ASSERT_MSG(! raw->mEdited, "blank aaEntity marked edited");
161 entity->SetTag(wstr);
162 NS_TEST_ASSERT_MSG(! raw->mEdited, "hidden aaEntity marked edited");
163 NS_TEST_ASSERT_MSG(! raw->mRead, "hidden aaEntity marked read");
164 entity->GetTag(wstr);
165 NS_TEST_ASSERT_MSG(! raw->mEdited, "read aaEntity marked edited");
166 NS_TEST_ASSERT_MSG(raw->mRead, "read aaEntity not marked read");
167 entity->SetTag(wstr);
168 NS_TEST_ASSERT_MSG(! raw->mEdited, "unchanged aaEntity marked edited");
169 entity->SetTag(NS_LITERAL_STRING("edit"));
170 NS_TEST_ASSERT_MSG(raw->mEdited, "changed aaEntity not marked edited");
172 return NS_OK;
175 nsresult
176 aaBaseTest::testObject(nsITestRunner *aTestRunner)
178 NS_TEST_BEGIN( aTestRunner );
179 nsresult rv;
180 nsCOMPtr<aaIObject> object(do_CreateInstance(AA_OBJECT_CONTRACT_ID, &rv));
181 NS_TEST_ASSERT_MSG(object, "aaObject instance creation" );
182 NS_ENSURE_SUCCESS(rv, rv);
183 nsCOMPtr<aaIDataNode> item(do_QueryInterface(object, &rv));
184 NS_TEST_ASSERT_MSG(item, "aaObject aaIDataNode interface" );
185 NS_ENSURE_SUCCESS(rv, rv);
187 mPackParam = mPackQueries = 0;
188 rv = item->Accept(this);
189 NS_TEST_ASSERT_MSG(mPackQueries & AA_BASE_TEST_OBJECT,
190 "aaObject->Accept doesn't return query" );
191 NS_TEST_ASSERT_MSG(!(mPackQueries & ~AA_BASE_TEST_OBJECT),
192 "aaObject->Accept returns incorrect query" );
193 NS_TEST_ASSERT_MSG(mPackParam & AA_BASE_TEST_OBJECT,
194 "aaObject->Accept doesn't provide valid parameter" );
195 NS_TEST_ASSERT_OK(rv);
197 aaObject* raw = NS_STATIC_CAST(aaObject *, object.get());
198 nsEmbedString wstr(NS_LITERAL_STRING("init"));
199 NS_TEST_ASSERT_MSG(! raw->mEdited, "blank aaObject marked edited");
200 object->SetTag(wstr);
201 NS_TEST_ASSERT_MSG(! raw->mEdited, "hidden aaObject marked edited");
202 NS_TEST_ASSERT_MSG(! raw->mRead, "hidden aaObject marked read");
203 object->GetTag(wstr);
204 NS_TEST_ASSERT_MSG(! raw->mEdited, "read aaObject marked edited");
205 NS_TEST_ASSERT_MSG(raw->mRead, "read aaObject not marked read");
206 object->SetTag(wstr);
207 NS_TEST_ASSERT_MSG(! raw->mEdited, "unchanged aaObject marked edited");
208 object->SetTag(NS_LITERAL_STRING("edit"));
209 NS_TEST_ASSERT_MSG(raw->mEdited, "changed aaObject not marked edited");
211 return NS_OK;
214 nsresult
215 aaBaseTest::testFlow(nsITestRunner *aTestRunner)
217 NS_TEST_BEGIN( aTestRunner );
218 nsresult rv;
219 nsCOMPtr<aaIFlow> object(do_CreateInstance(AA_FLOW_CONTRACT_ID, &rv));
220 NS_TEST_ASSERT_MSG(object, "aaFlaw instance creation" );
221 NS_ENSURE_SUCCESS(rv, rv);
222 nsCOMPtr<aaIDataNode> item(do_QueryInterface(object, &rv));
223 NS_TEST_ASSERT_MSG(item, "aaFlaw aaIDataNode interface" );
224 NS_ENSURE_SUCCESS(rv, rv);
226 mPackParam = mPackQueries = 0;
227 rv = item->Accept(this);
228 NS_TEST_ASSERT_MSG(mPackQueries & AA_BASE_TEST_FLOW,
229 "aaFlow->Accept doesn't return query" );
230 NS_TEST_ASSERT_MSG(!(mPackQueries & ~AA_BASE_TEST_FLOW),
231 "aaFlow->Accept returns incorrect query" );
232 NS_TEST_ASSERT_MSG(mPackParam & AA_BASE_TEST_FLOW,
233 "aaFlow->Accept doesn't provide valid parameter" );
234 NS_TEST_ASSERT_OK(rv);
236 aaFlow* raw = NS_STATIC_CAST(aaFlow *, object.get());
237 nsEmbedString wstr(NS_LITERAL_STRING("init"));
238 NS_TEST_ASSERT_MSG(! raw->mEdited, "blank aaFlaw marked edited");
239 raw->SetTag(wstr);
240 NS_TEST_ASSERT_MSG(! raw->mEdited, "hidden aaFlaw marked edited");
241 NS_TEST_ASSERT_MSG(! raw->mRead, "hidden aaFlaw marked read");
242 raw->GetTag(wstr);
243 NS_TEST_ASSERT_MSG(! raw->mEdited, "read aaFlaw marked edited");
244 NS_TEST_ASSERT_MSG(raw->mRead, "read aaFlaw not marked read");
245 raw->SetTag(wstr);
246 NS_TEST_ASSERT_MSG(! raw->mEdited, "unchanged aaFlaw marked edited");
247 raw->SetTag(NS_LITERAL_STRING("edit"));
248 NS_TEST_ASSERT_MSG(raw->mEdited, "changed aaFlaw not marked edited");
250 return NS_OK;
253 nsresult
254 aaBaseTest::testAction(nsITestRunner *aTestRunner)
256 NS_TEST_BEGIN( aTestRunner );
257 nsresult rv;
258 nsCOMPtr<aaIAction> action(do_CreateInstance(AA_ACTION_CONTRACT_ID, &rv));
259 NS_TEST_ASSERT_MSG(action, "aaAction instance creation" );
261 return NS_OK;
264 nsresult
265 aaBaseTest::testFact(nsITestRunner *aTestRunner)
267 NS_TEST_BEGIN( aTestRunner );
268 nsresult rv;
269 nsCOMPtr<aaIFact> node(do_CreateInstance(AA_FACT_CONTRACT_ID, &rv));
270 NS_TEST_ASSERT_MSG(node, "aaFact instance creation" );
272 nsCOMPtr<nsIMutableArray> array(do_QueryInterface(node));
273 NS_TEST_ASSERT_MSG(array, "aaFact 'mutableArray' interface" );
275 return NS_OK;
278 NS_GENERIC_FACTORY_CONSTRUCTOR(aaBaseTest)
280 static const nsModuleComponentInfo components[] =
282 { "Base Module Unit Test",
283 AA_BASE_TEST_CID,
284 AA_BASE_TEST_CONTRACT_ID,
285 aaBaseTestConstructor }
288 NS_IMPL_NSGETMODULE(aabaset, components)