[testing] Fix redundant loops in 'flow states' test
[abstract.git] / base / aaBaseTest.cpp
blob4a98c45e4944a4c02dcd4b57f86ba44c035f43aa
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"
30 #include "nsCOMArray.h"
32 /* Unfrozen API */
33 #include <abstract/cxxunit/nsCxxUnit.h>
34 #include <abstract/cxxunit/nsITest.h>
35 #include <abstract/cxxunit/nsITestRunner.h>
37 /* Project includes */
38 #include <abstract/base/aaIEntity.h>
39 #include <abstract/base/aaIObject.h>
40 #include <abstract/base/aaIFlow.h>
41 #include <abstract/base/aaIFact.h>
42 #include <abstract/base/aaIEvent.h>
43 #include <abstract/base/aaIQuery.h>
44 #include "aaObject.h"
45 #include "aaEntity.h"
46 #include "aaFlow.h"
47 #include "aaFact.h"
48 #include "aaEvent.h"
50 #define AA_BASE_TEST_CID \
51 {0x502c6e1e, 0x42c4, 0x46b1, {0xa4, 0x7b, 0x19, 0x42, 0x8a, 0xd6, 0x75, 0x58}}
52 #define AA_BASE_TEST_CONTRACT_ID "@aasii.org/base/unit;1"
54 class aaBaseTest: public nsITest,
55 public aaIQuery
57 public:
58 aaBaseTest();
59 virtual ~aaBaseTest();
60 NS_DECL_ISUPPORTS
61 NS_DECL_NSITEST
62 NS_DECL_AAIQUERY
63 private:
64 #define AA_BASE_TEST_ENTITY 1
65 #define AA_BASE_TEST_OBJECT 2
66 #define AA_BASE_TEST_FLOW 4
67 #define AA_BASE_TEST_FACT 8
68 #define AA_BASE_TEST_EVENT 16
69 PRUint32 mPackQueries;
70 PRUint32 mPackParam;
72 nsresult testEntity(nsITestRunner *aTestRunner);
73 nsresult testObject(nsITestRunner *aTestRunner);
74 nsresult testFlow(nsITestRunner *aTestRunner);
75 nsresult testFact(nsITestRunner *aTestRunner);
76 nsresult testEvent(nsITestRunner *aTestRunner);
79 aaBaseTest::aaBaseTest()
83 aaBaseTest::~aaBaseTest()
87 NS_IMPL_ISUPPORTS1(aaBaseTest, nsITest);
89 NS_IMETHODIMP
90 aaBaseTest::Test(nsITestRunner *aTestRunner)
92 nsITestRunner *cxxUnitTestRunner = aTestRunner;
93 NS_TEST_ASSERT_OK(testEntity(aTestRunner));
94 NS_TEST_ASSERT_OK(testObject(aTestRunner));
95 NS_TEST_ASSERT_OK(testFlow(aTestRunner));
96 NS_TEST_ASSERT_OK(testFact(aTestRunner));
97 NS_TEST_ASSERT_OK(testEvent(aTestRunner));
98 return NS_OK;
101 /* XXX Remove it */
102 NS_IMETHODIMP
103 aaBaseTest::GetAsync(PRBool *aAsync)
105 NS_ENSURE_ARG_POINTER( aAsync );
106 *aAsync = PR_FALSE;
107 return NS_OK;
110 /* aaIQuery */
111 NS_IMETHODIMP
112 aaBaseTest::QueryEntity(aaIEntity *aEntity)
114 mPackQueries |= AA_BASE_TEST_ENTITY;
115 if (aEntity)
116 mPackParam |= AA_BASE_TEST_ENTITY;
117 return NS_OK;
120 NS_IMETHODIMP
121 aaBaseTest::QueryObject(aaIObject *aObject)
123 mPackQueries |= AA_BASE_TEST_OBJECT;
124 if (aObject)
125 mPackParam |= AA_BASE_TEST_OBJECT;
126 return NS_OK;
129 NS_IMETHODIMP
130 aaBaseTest::QueryFlow(aaIFlow *aFlow)
132 mPackQueries |= AA_BASE_TEST_FLOW;
133 if (aFlow)
134 mPackParam |= AA_BASE_TEST_FLOW;
135 return NS_OK;
138 NS_IMETHODIMP
139 aaBaseTest::QueryFact(aaIFact *aFact)
141 mPackQueries |= AA_BASE_TEST_FACT;
142 if (aFact)
143 mPackParam |= AA_BASE_TEST_FACT;
144 return NS_OK;
147 NS_IMETHODIMP
148 aaBaseTest::QueryEvent(aaIEvent *aEvent)
150 mPackQueries |= AA_BASE_TEST_EVENT;
151 if (aEvent)
152 mPackParam |= AA_BASE_TEST_EVENT;
153 return NS_OK;
156 /* Private methods */
157 nsresult
158 aaBaseTest::testEntity(nsITestRunner *aTestRunner)
160 NS_TEST_BEGIN( aTestRunner );
161 nsresult rv;
162 nsCOMPtr<aaIEntity> entity(do_CreateInstance(AA_ENTITY_CONTRACT_ID, &rv));
163 NS_TEST_ASSERT_MSG(entity, "aaEntity instance creation" );
164 NS_ENSURE_SUCCESS(rv, rv);
165 nsCOMPtr<aaIDataNode> item(do_QueryInterface(entity, &rv));
166 NS_TEST_ASSERT_MSG(item, "aaEntity aaIDataNode interface" );
167 NS_ENSURE_SUCCESS(rv, rv);
169 mPackParam = mPackQueries = 0;
170 rv = item->Accept(this);
171 NS_TEST_ASSERT_MSG(mPackQueries & AA_BASE_TEST_ENTITY,
172 "aaEntity->Accept doesn't return query" );
173 NS_TEST_ASSERT_MSG(!(mPackQueries & ~AA_BASE_TEST_ENTITY),
174 "aaEntity->Accept returns incorrect query" );
175 NS_TEST_ASSERT_MSG(mPackParam & AA_BASE_TEST_ENTITY,
176 "aaEntity->Accept doesn't provide valid parameter" );
177 NS_TEST_ASSERT_OK(rv);
179 aaEntity* raw = NS_STATIC_CAST(aaEntity *, entity.get());
180 nsEmbedString wstr(NS_LITERAL_STRING("init"));
181 NS_TEST_ASSERT_MSG(! raw->mEdited, "blank aaEntity marked edited");
182 entity->SetTag(wstr);
183 NS_TEST_ASSERT_MSG(! raw->mEdited, "hidden aaEntity marked edited");
184 NS_TEST_ASSERT_MSG(! raw->mRead, "hidden aaEntity marked read");
185 entity->GetTag(wstr);
186 NS_TEST_ASSERT_MSG(! raw->mEdited, "read aaEntity marked edited");
187 NS_TEST_ASSERT_MSG(raw->mRead, "read aaEntity not marked read");
188 entity->SetTag(wstr);
189 NS_TEST_ASSERT_MSG(! raw->mEdited, "unchanged aaEntity marked edited");
190 entity->SetTag(NS_LITERAL_STRING("edit"));
191 NS_TEST_ASSERT_MSG(raw->mEdited, "changed aaEntity not marked edited");
193 return NS_OK;
196 nsresult
197 aaBaseTest::testObject(nsITestRunner *aTestRunner)
199 NS_TEST_BEGIN( aTestRunner );
200 nsresult rv;
201 nsCOMPtr<aaIObject> object(do_CreateInstance(AA_OBJECT_CONTRACT_ID, &rv));
202 NS_TEST_ASSERT_MSG(object, "aaObject instance creation" );
203 NS_ENSURE_SUCCESS(rv, rv);
204 nsCOMPtr<aaIDataNode> item(do_QueryInterface(object, &rv));
205 NS_TEST_ASSERT_MSG(item, "aaObject aaIDataNode interface" );
206 NS_ENSURE_SUCCESS(rv, rv);
208 mPackParam = mPackQueries = 0;
209 rv = item->Accept(this);
210 NS_TEST_ASSERT_MSG(mPackQueries & AA_BASE_TEST_OBJECT,
211 "aaObject->Accept doesn't return query" );
212 NS_TEST_ASSERT_MSG(!(mPackQueries & ~AA_BASE_TEST_OBJECT),
213 "aaObject->Accept returns incorrect query" );
214 NS_TEST_ASSERT_MSG(mPackParam & AA_BASE_TEST_OBJECT,
215 "aaObject->Accept doesn't provide valid parameter" );
216 NS_TEST_ASSERT_OK(rv);
218 aaObject* raw = NS_STATIC_CAST(aaObject *, object.get());
219 nsEmbedString wstr(NS_LITERAL_STRING("init"));
220 NS_TEST_ASSERT_MSG(! raw->mEdited, "blank aaObject marked edited");
221 object->SetTag(wstr);
222 NS_TEST_ASSERT_MSG(! raw->mEdited, "hidden aaObject marked edited");
223 NS_TEST_ASSERT_MSG(! raw->mRead, "hidden aaObject marked read");
224 object->GetTag(wstr);
225 NS_TEST_ASSERT_MSG(! raw->mEdited, "read aaObject marked edited");
226 NS_TEST_ASSERT_MSG(raw->mRead, "read aaObject not marked read");
227 object->SetTag(wstr);
228 NS_TEST_ASSERT_MSG(! raw->mEdited, "unchanged aaObject marked edited");
229 object->SetTag(NS_LITERAL_STRING("edit"));
230 NS_TEST_ASSERT_MSG(raw->mEdited, "changed aaObject not marked edited");
232 return NS_OK;
235 nsresult
236 aaBaseTest::testFlow(nsITestRunner *aTestRunner)
238 NS_TEST_BEGIN( aTestRunner );
239 nsresult rv;
240 nsCOMPtr<aaIFlow> object(do_CreateInstance(AA_FLOW_CONTRACT_ID, &rv));
241 NS_TEST_ASSERT_MSG(object, "aaFlaw instance creation" );
242 NS_ENSURE_SUCCESS(rv, rv);
243 nsCOMPtr<aaIDataNode> item(do_QueryInterface(object, &rv));
244 NS_TEST_ASSERT_MSG(item, "aaFlaw aaIDataNode interface" );
245 NS_ENSURE_SUCCESS(rv, rv);
247 mPackParam = mPackQueries = 0;
248 rv = item->Accept(this);
249 NS_TEST_ASSERT_MSG(mPackQueries & AA_BASE_TEST_FLOW,
250 "aaFlow->Accept doesn't return query" );
251 NS_TEST_ASSERT_MSG(!(mPackQueries & ~AA_BASE_TEST_FLOW),
252 "aaFlow->Accept returns incorrect query" );
253 NS_TEST_ASSERT_MSG(mPackParam & AA_BASE_TEST_FLOW,
254 "aaFlow->Accept doesn't provide valid parameter" );
255 NS_TEST_ASSERT_OK(rv);
257 aaFlow* raw = NS_STATIC_CAST(aaFlow *, object.get());
258 nsEmbedString wstr(NS_LITERAL_STRING("init"));
259 NS_TEST_ASSERT_MSG(! raw->mEdited, "blank aaFlaw marked edited");
260 raw->SetTag(wstr);
261 NS_TEST_ASSERT_MSG(! raw->mEdited, "hidden aaFlaw marked edited");
262 NS_TEST_ASSERT_MSG(! raw->mRead, "hidden aaFlaw marked read");
263 raw->GetTag(wstr);
264 NS_TEST_ASSERT_MSG(! raw->mEdited, "read aaFlaw marked edited");
265 NS_TEST_ASSERT_MSG(raw->mRead, "read aaFlaw not marked read");
266 raw->SetTag(wstr);
267 NS_TEST_ASSERT_MSG(! raw->mEdited, "unchanged aaFlaw marked edited");
268 raw->SetTag(NS_LITERAL_STRING("edit"));
269 NS_TEST_ASSERT_MSG(raw->mEdited, "changed aaFlaw not marked edited");
271 return NS_OK;
274 nsresult
275 aaBaseTest::testFact(nsITestRunner *aTestRunner)
277 NS_TEST_BEGIN( aTestRunner );
278 nsresult rv;
279 nsCOMPtr<aaIFact> item(do_CreateInstance(AA_FACT_CONTRACT_ID, &rv));
280 NS_TEST_ASSERT_MSG(item, "aaFact instance creation" );
282 mPackParam = mPackQueries = 0;
283 rv = item->Accept(this);
284 NS_TEST_ASSERT_MSG(mPackQueries & AA_BASE_TEST_FACT,
285 "aaFact->Accept doesn't return query" );
286 NS_TEST_ASSERT_MSG(!(mPackQueries & ~AA_BASE_TEST_FACT),
287 "aaFact->Accept returns incorrect query" );
288 NS_TEST_ASSERT_MSG(mPackParam & AA_BASE_TEST_FACT,
289 "aaFact->Accept doesn't provide valid parameter" );
290 NS_TEST_ASSERT_OK(rv);
292 return NS_OK;
295 nsresult
296 aaBaseTest::testEvent(nsITestRunner *aTestRunner)
298 NS_TEST_BEGIN( aTestRunner );
299 nsresult rv;
300 nsCOMPtr<aaIEvent> item(do_CreateInstance(AA_EVENT_CONTRACT_ID, &rv));
301 NS_TEST_ASSERT_MSG(item, "aaEvent instance creation" );
303 nsCOMPtr<nsIMutableArray> array(do_QueryInterface(item));
304 NS_TEST_ASSERT_MSG(array, "aaEvent 'mutableArray' interface" );
306 mPackParam = mPackQueries = 0;
307 rv = item->Accept(this);
308 NS_TEST_ASSERT_MSG(mPackQueries & AA_BASE_TEST_EVENT,
309 "aaEvent->Accept doesn't return query" );
310 NS_TEST_ASSERT_MSG(!(mPackQueries & ~AA_BASE_TEST_EVENT),
311 "aaEvent->Accept returns incorrect query" );
312 NS_TEST_ASSERT_MSG(mPackParam & AA_BASE_TEST_EVENT,
313 "aaEvent->Accept doesn't provide valid parameter" );
314 NS_TEST_ASSERT_OK(rv);
316 return NS_OK;
319 NS_GENERIC_FACTORY_CONSTRUCTOR(aaBaseTest)
321 static const nsModuleComponentInfo components[] =
323 { "Base Module Unit Test",
324 AA_BASE_TEST_CID,
325 AA_BASE_TEST_CONTRACT_ID,
326 aaBaseTestConstructor }
329 NS_IMPL_NSGETMODULE(aabaset, components)