|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 // INCLUDE FILES |
|
19 #include <e32std.h> |
|
20 #include <e32test.h> |
|
21 #include <cntdb.h> |
|
22 #include <cntdef.h> |
|
23 #include <cntitem.h> |
|
24 #include <cntfield.h> |
|
25 |
|
26 #include <cntdef.hrh> |
|
27 #include <cntview.h> |
|
28 #include <cntfldst.h> |
|
29 |
|
30 #include "CntModelTesterAsynch.h" |
|
31 #include "CntModelTesterSetToneToContacts.h" |
|
32 |
|
33 _LIT(KTestName,"asynaccess"); |
|
34 _LIT(KLogFileName,"asynaccess.log"); |
|
35 _LIT(KThreadOpenAsync,"OpenAsync"); |
|
36 _LIT(KThreadOpenSync,"OpenSync"); |
|
37 |
|
38 LOCAL_D RTest test(KTestName); |
|
39 |
|
40 CCntModelTesterAsynch* gAsync; |
|
41 // |
|
42 // CTestConductor. |
|
43 // |
|
44 |
|
45 CTestConductor* CTestConductor::NewL() |
|
46 { |
|
47 CTestConductor* self=new(ELeave) CTestConductor(); |
|
48 CleanupStack::PushL(self); |
|
49 self->ConstructL(); |
|
50 self->RunTestsL(); |
|
51 CleanupStack::Pop(); |
|
52 return self; |
|
53 } |
|
54 |
|
55 CTestConductor::~CTestConductor() |
|
56 { |
|
57 delete iLog; |
|
58 delete iDb; |
|
59 delete iRandomGenerator; |
|
60 TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KDbFileName)); |
|
61 iFs.Close(); |
|
62 } |
|
63 |
|
64 CTestConductor::CTestConductor() |
|
65 { |
|
66 } |
|
67 |
|
68 void CTestConductor::ConstructL() |
|
69 { |
|
70 User::LeaveIfError(iFs.Connect()); |
|
71 iLog=CLog::NewL(test,KLogFileName); |
|
72 iDb=CContactDatabase::ReplaceL(KDbFileName); |
|
73 iRandomGenerator=CRandomContactGenerator::NewL(); |
|
74 iRandomGenerator->SetDbL(*iDb); |
|
75 } |
|
76 |
|
77 |
|
78 void CTestConductor::RunTestsL() |
|
79 { |
|
80 iAsync = CCntModelTesterAsynch:: NewL(*iLog); |
|
81 gAsync = iAsync; |
|
82 CleanupStack::PushL(iAsync); |
|
83 CActiveScheduler::Start(); |
|
84 CleanupStack::PopAndDestroy(); // tester. |
|
85 } |
|
86 |
|
87 void CTestConductor::SetTestError(TInt aTestError) |
|
88 { |
|
89 // error from tester |
|
90 iTestError = aTestError; |
|
91 } |
|
92 |
|
93 |
|
94 // |
|
95 //Class CCntModelTesterAsynch |
|
96 // |
|
97 const TInt KContactCount = 500; |
|
98 |
|
99 // ================= MEMBER FUNCTIONS ======================= |
|
100 |
|
101 CCntModelTesterAsynch::CCntModelTesterAsynch(CLog& aLog): |
|
102 CActive(CActive::EPriorityIdle),iLog(aLog) |
|
103 { |
|
104 CActiveScheduler::Add(this); |
|
105 } |
|
106 |
|
107 CCntModelTesterAsynch* CCntModelTesterAsynch::NewL(CLog& aLog) |
|
108 { |
|
109 CCntModelTesterAsynch* self = new(ELeave) CCntModelTesterAsynch(aLog); |
|
110 CleanupStack::PushL(self); |
|
111 self->ConstructL(); |
|
112 CleanupStack::Pop(self); |
|
113 return self; |
|
114 } |
|
115 |
|
116 |
|
117 void CCntModelTesterAsynch::ConstructL() |
|
118 { |
|
119 iDb= CContactDatabase::OpenL(KDbFileName); |
|
120 |
|
121 //Create a group |
|
122 iGroup = iDb->CreateContactGroupLC(_L("My Group")); |
|
123 |
|
124 iArray = CContactIdArray::NewL(); |
|
125 CreateContactsL(); |
|
126 CleanupStack::Pop(iGroup); |
|
127 iOperation = CCntModelTesterSetToneToContacts::NewL(*iArray, _L("c:\\testdata\\Ring.wav")); |
|
128 |
|
129 IssueRequest(); |
|
130 } |
|
131 |
|
132 CCntModelTesterAsynch::~CCntModelTesterAsynch() |
|
133 { |
|
134 delete iGroup; |
|
135 delete iDb; |
|
136 Cancel(); |
|
137 delete iArray; |
|
138 delete iOperation; |
|
139 } |
|
140 |
|
141 void CCntModelTesterAsynch::DoCancel() |
|
142 { |
|
143 } |
|
144 |
|
145 void CCntModelTesterAsynch::RunL() |
|
146 { |
|
147 if (!iOperation->IsProcessDone()) |
|
148 { |
|
149 iLog.LogLine(_L("Operation continue, count=%d\n"),iOperation->iSetCount); |
|
150 TRAPD(err, iOperation->StepL()); |
|
151 if (err != KErrNone ) |
|
152 { |
|
153 iLog.LogLine(_L("Operation failed, error=%d\n"),err); |
|
154 if (err != KErrLocked ) |
|
155 { |
|
156 User::Leave(err); |
|
157 } |
|
158 } |
|
159 IssueRequest(); |
|
160 |
|
161 if (iOperation->iSetCount == KContactCount/2) |
|
162 { |
|
163 // Launch Thread to Open Db Asynchronously |
|
164 CDbOpenLauncher::Create(KThreadOpenAsync, KThreadOpenAsync); |
|
165 } |
|
166 |
|
167 } |
|
168 else |
|
169 { |
|
170 iLog.LogLine(_L("Operation ready. Deleting contacts..\n")); |
|
171 // delete contacts: |
|
172 //TRAP_IGNORE(iDb->DeleteContactsL(*iArray)); |
|
173 for(TInt i=0;i<iArray->Count();i++) |
|
174 { |
|
175 TRAPD(err, iDb->DeleteContactL((*iArray)[i])); |
|
176 if (err != KErrNone ) |
|
177 { |
|
178 iLog.LogLine(_L("Operation failed, error=%d\n"),err); |
|
179 if (err != KErrLocked ) |
|
180 { |
|
181 User::Leave(err); |
|
182 } |
|
183 } |
|
184 } |
|
185 CActiveScheduler::Stop(); |
|
186 } |
|
187 |
|
188 } |
|
189 |
|
190 void CCntModelTesterAsynch::Error(TInt aError) |
|
191 { |
|
192 iLog.LogLine(_L("elieli: CCntModelTesterAsynch::RunError(%d)"), aError); |
|
193 return; |
|
194 } |
|
195 |
|
196 void CCntModelTesterAsynch::IssueRequest() |
|
197 { |
|
198 TRequestStatus* status = &iStatus; |
|
199 User::RequestComplete(status, KErrNone); |
|
200 SetActive(); |
|
201 } |
|
202 |
|
203 void CCntModelTesterAsynch::CreateContactsL() |
|
204 { |
|
205 iLog.LogLine(_L("Creating contacts synchronously.\n")); |
|
206 |
|
207 CContactDatabase* db = CContactDatabase::OpenL(KDbFileName); |
|
208 CleanupStack::PushL(db); |
|
209 |
|
210 TContactItemId groupId = iGroup->Id(); |
|
211 TBuf16<30> name; |
|
212 for (TInt i=0; i<KContactCount; i++) |
|
213 { |
|
214 name.Zero(); |
|
215 name.Append(_L("test ")); |
|
216 name.AppendNum(i+1); |
|
217 iArray->AddL(CreateContactL(db, name, _L("12456789"))); |
|
218 iLog.LogLine(_L("contact ID of index %d is %d.\n"), i, (*iArray)[i]); |
|
219 //Add the contact item to the group |
|
220 db->AddContactToGroupL((*iArray)[i],groupId); |
|
221 } |
|
222 |
|
223 CleanupStack::PopAndDestroy(db); |
|
224 test.Printf(_L("Contacts ready. Setting Ringingtones started.\n")); |
|
225 iLog.LogLine(_L("Contacts ready. Setting Ringingtones started.\n")); |
|
226 } |
|
227 |
|
228 TContactItemId CCntModelTesterAsynch::CreateContactL(CContactDatabase* aDb, |
|
229 const TDesC& aName, |
|
230 const TDesC& aNumber) |
|
231 { |
|
232 CContactCard* card = CContactCard::NewLC(); |
|
233 |
|
234 card->AddFieldL(*CreateFieldLC(KUidContactFieldVCardMapUnusedN, |
|
235 KUidContactFieldFamilyName, |
|
236 aName)); |
|
237 CleanupStack::Pop(); |
|
238 card->AddFieldL(*CreateFieldLC(KUidContactFieldVCardMapTEL, |
|
239 KUidContactFieldPhoneNumber, |
|
240 aNumber)); |
|
241 CleanupStack::Pop(); |
|
242 |
|
243 TContactItemId id = aDb->AddNewContactL(*card); |
|
244 CleanupStack::PopAndDestroy(card); |
|
245 return id; |
|
246 } |
|
247 |
|
248 CContactItemField* CCntModelTesterAsynch::CreateFieldLC(const TUid aMappingUId, |
|
249 TFieldType aFieldType, |
|
250 const TDesC &aText) |
|
251 { |
|
252 CContactItemField* field = CContactItemField::NewLC(KStorageTypeText, aFieldType); |
|
253 field->SetMapping(aMappingUId); |
|
254 field->TextStorage()->SetTextL(aText); |
|
255 return field; |
|
256 } |
|
257 |
|
258 // |
|
259 // Separate Thread to Open Database |
|
260 // |
|
261 CDbOpenLauncher::CViewer* CDbOpenLauncher::CViewer::NewL(const TDesC& aViewName) |
|
262 { |
|
263 CViewer* self = new(ELeave) CViewer; |
|
264 CleanupStack::PushL(self); |
|
265 self->ConstructL(aViewName); |
|
266 CleanupStack::Pop(self); |
|
267 return(self); |
|
268 } |
|
269 |
|
270 CDbOpenLauncher::CViewer::~CViewer() |
|
271 { |
|
272 delete iDatabase; |
|
273 } |
|
274 |
|
275 void CDbOpenLauncher::CViewer::ConstructL(const TDesC& aViewName) |
|
276 { |
|
277 if(aViewName.Compare(KThreadOpenAsync) == 0) |
|
278 { |
|
279 // Create Async Open |
|
280 tester=CTester::NewL(); |
|
281 } |
|
282 else if (aViewName.Compare(KThreadOpenSync) == 0) |
|
283 { |
|
284 //Open the database Synchronously |
|
285 //Here we do nothing |
|
286 ; |
|
287 } |
|
288 |
|
289 } |
|
290 |
|
291 void CDbOpenLauncher::DoMainL(const TDesC& aName) |
|
292 { |
|
293 CActiveScheduler* scheduler = new(ELeave) CActiveScheduler; |
|
294 CActiveScheduler::Install(scheduler); |
|
295 CViewer* view = CViewer::NewL(aName); |
|
296 CleanupStack::PushL(view); |
|
297 |
|
298 CActiveScheduler::Start(); |
|
299 |
|
300 CleanupStack::PopAndDestroy(view); |
|
301 } |
|
302 |
|
303 // This is the only thing one needs to call in order to set the view in motion. |
|
304 void CDbOpenLauncher::Create(const TDesC& aName, const TDesC& aViewName) |
|
305 { |
|
306 RThread thread; |
|
307 thread.Create( aName, CDbOpenLauncher::LaunchThread, KDefaultStackSize, 0x2000, 0x20000, (void*) &aViewName, EOwnerThread ); |
|
308 thread.Resume(); |
|
309 thread.SetPriority(EPriorityNormal); |
|
310 thread.Close(); |
|
311 } |
|
312 |
|
313 |
|
314 TInt CDbOpenLauncher::LaunchThread(TAny* aAny) |
|
315 { |
|
316 __UHEAP_MARK; |
|
317 |
|
318 CTrapCleanup* cleanUpStack=CTrapCleanup::New(); |
|
319 if (!cleanUpStack) |
|
320 { |
|
321 return KErrNoMemory; |
|
322 } |
|
323 |
|
324 TRAPD(r,DoMainL(*((TDesC*) aAny))); |
|
325 |
|
326 delete CActiveScheduler::Current(); |
|
327 delete cleanUpStack; |
|
328 |
|
329 __UHEAP_MARKEND; |
|
330 return r; |
|
331 } |
|
332 |
|
333 |
|
334 // |
|
335 // CTester Active Object to test Async Open in thread whilst async open is going on in other thread |
|
336 // |
|
337 CTester* CTester::NewL() |
|
338 { |
|
339 CTester* self = new(ELeave) CTester(); |
|
340 CleanupStack::PushL(self); |
|
341 self->ConstructL(); |
|
342 CleanupStack::Pop(); |
|
343 return self; |
|
344 } |
|
345 |
|
346 void CTester::DoCancel() |
|
347 { |
|
348 } |
|
349 |
|
350 CTester::~CTester() |
|
351 { |
|
352 //state deleted during tests |
|
353 } |
|
354 |
|
355 CTester::CTester() : CActive(EPriorityIdle),iCurrentTestNumber(0) |
|
356 { |
|
357 CActiveScheduler::Add(this); |
|
358 } |
|
359 |
|
360 void CTester::ConstructL() |
|
361 { |
|
362 NextTest(); |
|
363 CActiveScheduler::Start(); |
|
364 } |
|
365 |
|
366 void CTester::NextTest() |
|
367 { |
|
368 TRequestStatus *pS = &iStatus; |
|
369 User::RequestComplete(pS,KErrNone); |
|
370 SetActive(); |
|
371 } |
|
372 |
|
373 void CTester::RunL() |
|
374 { |
|
375 if (iCurrentTestNumber == EAsyncOpenNamed) |
|
376 { |
|
377 //test.Printf(_L("Open asynchrously..\n")); |
|
378 SetActive(); |
|
379 iContactOpenOperation = CContactDatabase::Open(KDbFileName,iStatus); |
|
380 ++iCurrentTestNumber; |
|
381 } |
|
382 else if(iCurrentTestNumber <= EAsyncOpenedNamed) |
|
383 { |
|
384 //test.Printf(_L("check opened result..\n")); |
|
385 if (iStatus.Int() == KErrLocked) |
|
386 { |
|
387 //test.Printf(_L("database is locked.\n")); |
|
388 iCurrentTestNumber = EAsyncOpenNamed; |
|
389 iStatus=KErrNone; |
|
390 NextTest(); |
|
391 return; |
|
392 } |
|
393 test(iStatus.Int() == KErrNone); |
|
394 |
|
395 if (iDb == NULL) |
|
396 { |
|
397 iDb = iContactOpenOperation->TakeDatabase(); |
|
398 if (iDb == NULL) |
|
399 { |
|
400 iCurrentTestNumber=EAsyncOpenNamed; |
|
401 iStatus=KErrNone; |
|
402 NextTest(); |
|
403 return; |
|
404 } |
|
405 } |
|
406 |
|
407 const TContactItemId id = (*gAsync->iArray)[iCurrentTestNumber]; |
|
408 CContactItem* item=iDb->OpenContactLX(id); |
|
409 CleanupStack::PushL(item); |
|
410 |
|
411 item->RemoveField(0); |
|
412 |
|
413 TRAPD(err, iDb->CommitContactL(*item)); |
|
414 if (err != KErrNone && err != KErrLocked ) |
|
415 { |
|
416 //test.Printf(_L("CTest::Run CommitContact failed, error=%d\n"),err); |
|
417 User::Leave(err); |
|
418 } |
|
419 |
|
420 CleanupStack::PopAndDestroy(2); // item, lock |
|
421 |
|
422 if (iCurrentTestNumber == EAsyncOpenedNamed) |
|
423 { |
|
424 delete iContactOpenOperation; |
|
425 delete iDb; |
|
426 } |
|
427 |
|
428 ++iCurrentTestNumber; |
|
429 |
|
430 |
|
431 NextTest(); |
|
432 } |
|
433 else if (iCurrentTestNumber <= ESyncOpenedNamed) |
|
434 { |
|
435 //Open synchronously |
|
436 TRAPD(err, iDb=CContactDatabase::OpenL(KDbFileName)); |
|
437 if (err != KErrNone ) |
|
438 { |
|
439 //test.Printf(_L("CTest::RunL Open failed, error=%d\n"),err); |
|
440 if (err != KErrLocked ) |
|
441 { |
|
442 User::Leave(err); |
|
443 } |
|
444 else |
|
445 { |
|
446 iStatus=KErrNone; |
|
447 NextTest(); |
|
448 return; |
|
449 } |
|
450 } |
|
451 |
|
452 CleanupStack::PushL(iDb); |
|
453 test(iDb != NULL); |
|
454 |
|
455 |
|
456 const TContactItemId id = (*gAsync->iArray)[iCurrentTestNumber]; |
|
457 CContactItem* item=iDb->OpenContactLX(id); |
|
458 CleanupStack::PushL(item); |
|
459 |
|
460 item->RemoveField(0); |
|
461 |
|
462 TRAP(err, iDb->CommitContactL(*item)); |
|
463 if (err != KErrNone ) |
|
464 { |
|
465 //test.Printf(_L("CTest::RunL CommitContact failed, error=%d\n"),err); |
|
466 if (err != KErrLocked ) |
|
467 { |
|
468 User::Leave(err); |
|
469 } |
|
470 } |
|
471 |
|
472 CleanupStack::PopAndDestroy(2); // item, lock |
|
473 CleanupStack::PopAndDestroy(iDb); //iDb |
|
474 |
|
475 if (iCurrentTestNumber < ESyncOpenedNamed) |
|
476 { |
|
477 ++iCurrentTestNumber; |
|
478 NextTest(); |
|
479 } |
|
480 else |
|
481 { |
|
482 CActiveScheduler::Stop(); |
|
483 } |
|
484 |
|
485 } |
|
486 |
|
487 } |
|
488 |
|
489 void CTester::Error(TInt aError) |
|
490 { |
|
491 test.Printf(_L("elieli: CTester::RunError(%d)"), aError); |
|
492 return; |
|
493 } |
|
494 // |
|
495 // Main. |
|
496 // |
|
497 |
|
498 /** |
|
499 |
|
500 @SYMTestCaseID PIM-CNTMODELTESTERASYNCH-0001 |
|
501 |
|
502 */ |
|
503 |
|
504 GLDEF_C TInt E32Main() |
|
505 { |
|
506 __UHEAP_MARK; |
|
507 CActiveScheduler* scheduler=new CActiveScheduler; |
|
508 test.Start(_L("@SYMTESTCaseID:PIM-CNTMODELTESTERASYNCH-0001 Testing Asynchronously Access")); |
|
509 |
|
510 if (scheduler) |
|
511 { |
|
512 CActiveScheduler::Install(scheduler); |
|
513 CTrapCleanup* cleanup=CTrapCleanup::New(); |
|
514 if (cleanup) |
|
515 { |
|
516 CTestConductor* testConductor=NULL; |
|
517 TRAP_IGNORE(testConductor = CTestConductor::NewL()); |
|
518 delete testConductor; |
|
519 delete cleanup; |
|
520 } |
|
521 delete scheduler; |
|
522 } |
|
523 test.End(); |
|
524 test.Close(); |
|
525 __UHEAP_MARKEND; |
|
526 return KErrNone; |
|
527 } |
|
528 // End of File |