|
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // GroupDeleteBug Test module |
|
15 // |
|
16 // |
|
17 |
|
18 |
|
19 #include <e32test.h> |
|
20 #include <cntdef.h> |
|
21 #include <cntdb.h> |
|
22 #include <cntitem.h> |
|
23 #include <cntfield.h> |
|
24 #include <cntfldst.h> |
|
25 #include <cntviewbase.h> |
|
26 #include "CContactViewEventQueue.h" |
|
27 #include "CContactDbEventQueue.h" |
|
28 |
|
29 _LIT(KTestName, "T_GroupDeleteBug"); |
|
30 |
|
31 _LIT(KTestDbName, "c:T_GroupDeleteBug.cdb"); |
|
32 |
|
33 LOCAL_D RTest test(KTestName); |
|
34 |
|
35 |
|
36 class CTestResources : public CBase |
|
37 { |
|
38 public: |
|
39 static CTestResources* NewLC(); |
|
40 void ConstructL(); |
|
41 void CreateTestContactsL(); |
|
42 ~CTestResources(); |
|
43 |
|
44 CContactDatabase* iDb; |
|
45 CContactIdArray* iContacts; |
|
46 TContactItemId iGroupId; |
|
47 CContactDbEventQueue* iDbEventQueue; |
|
48 CContactViewEventQueue* iViewEventQueue; |
|
49 RContactViewSortOrder iViewSortOrder; |
|
50 CContactLocalView* iLocalView; |
|
51 CContactViewEventQueue* iGroupViewEventQueue; |
|
52 CContactGroupView* iGroupView; |
|
53 }; |
|
54 |
|
55 CTestResources* CTestResources::NewLC() |
|
56 { |
|
57 CTestResources* self = new(ELeave) CTestResources; |
|
58 CleanupStack::PushL(self); |
|
59 self->ConstructL(); |
|
60 return self; |
|
61 } |
|
62 |
|
63 void CTestResources::ConstructL() |
|
64 { |
|
65 iDb = CContactDatabase::ReplaceL(KTestDbName); |
|
66 iDbEventQueue = CContactDbEventQueue::NewL(iDb); |
|
67 |
|
68 CreateTestContactsL(); |
|
69 |
|
70 iViewEventQueue = CContactViewEventQueue::NewL(); |
|
71 |
|
72 iViewSortOrder.AppendL(KUidContactFieldFamilyName); |
|
73 iViewSortOrder.AppendL(KUidContactFieldGivenName); |
|
74 iViewSortOrder.AppendL(KUidContactFieldCompanyName); |
|
75 |
|
76 iLocalView = CContactLocalView::NewL |
|
77 (*iViewEventQueue, *iDb, iViewSortOrder, EGroupsOnly); |
|
78 |
|
79 iGroupViewEventQueue = CContactViewEventQueue::NewL(); |
|
80 iGroupView = CContactGroupView::NewL |
|
81 (*iDb, *iLocalView, *iGroupViewEventQueue, iGroupId, |
|
82 CContactGroupView::EShowContactsInGroup); |
|
83 |
|
84 // Wait for group view to get ready |
|
85 TContactViewEvent event; |
|
86 test(iGroupViewEventQueue->ListenForEvent(10, event)); |
|
87 test(event.iEventType == TContactViewEvent::EReady); |
|
88 |
|
89 // Flush all other events |
|
90 iDbEventQueue->Flush(); |
|
91 iViewEventQueue->Flush(); |
|
92 iGroupViewEventQueue->Flush(); |
|
93 } |
|
94 |
|
95 void CTestResources::CreateTestContactsL() |
|
96 { |
|
97 const TInt KContacts = 4; |
|
98 TInt i; |
|
99 iContacts = CContactIdArray::NewL(); |
|
100 |
|
101 // Create contacts |
|
102 for (i=1; i <= KContacts; ++i) |
|
103 { |
|
104 CContactCard* card = CContactCard::NewLC(); |
|
105 CContactItemField* field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName); |
|
106 TBuf<30> name; |
|
107 name.Format(_L("Contact%02d"), i); |
|
108 field->TextStorage()->SetTextL(name); |
|
109 card->AddFieldL(*field); |
|
110 CleanupStack::Pop(field); |
|
111 const TContactItemId contactId = iDb->AddNewContactL(*card); |
|
112 iContacts->AddL(contactId); |
|
113 CleanupStack::PopAndDestroy(card); |
|
114 // Eat away contact db events |
|
115 TContactDbObserverEvent event; |
|
116 test(iDbEventQueue->ListenForEvent(10,event)); |
|
117 } |
|
118 |
|
119 // Create a group |
|
120 CContactItem* group = iDb->CreateContactGroupLC(_L("TestGroup")); |
|
121 iGroupId = group->Id(); |
|
122 CleanupStack::PopAndDestroy(group); |
|
123 |
|
124 // Connect half of the contacts to the group |
|
125 for (i=0; i < iContacts->Count(); ++i) |
|
126 { |
|
127 if (i%2 == 0) |
|
128 { |
|
129 iDb->AddContactToGroupL((*iContacts)[i], iGroupId); |
|
130 } |
|
131 } |
|
132 } |
|
133 |
|
134 CTestResources::~CTestResources() |
|
135 { |
|
136 if (iGroupView) iGroupView->Close(*iGroupViewEventQueue); |
|
137 delete iGroupViewEventQueue; |
|
138 if (iLocalView) iLocalView->Close(*iViewEventQueue); |
|
139 iViewSortOrder.Close(); |
|
140 delete iViewEventQueue; |
|
141 delete iDbEventQueue; |
|
142 delete iContacts; |
|
143 delete iDb; |
|
144 TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName)); |
|
145 } |
|
146 |
|
147 LOCAL_C void CheckConsistentL |
|
148 (CContactDatabase& aDb, |
|
149 const CContactGroupView& aGroupView, |
|
150 const CContactViewBase& aBaseView, |
|
151 TContactItemId aGroupId) |
|
152 { |
|
153 const TInt groupViewCount = aGroupView.CountL(); |
|
154 test(groupViewCount <= aBaseView.CountL()); |
|
155 CContactGroup* group = static_cast<CContactGroup*>(aDb.ReadContactLC(aGroupId)); |
|
156 |
|
157 const TInt count = aBaseView.CountL(); |
|
158 TInt checked = 0; |
|
159 for (TInt i=0; i < count; ++i) |
|
160 { |
|
161 const TContactItemId id = aBaseView.AtL(i); |
|
162 if (group->ContainsItem(id)) |
|
163 { |
|
164 test(aGroupView.FindL(id) != KErrNotFound); |
|
165 ++checked; |
|
166 } |
|
167 } |
|
168 test(checked == groupViewCount); |
|
169 CleanupStack::PopAndDestroy(group); |
|
170 } |
|
171 |
|
172 LOCAL_C void CheckConsistentL(CTestResources& aRes) |
|
173 { |
|
174 CheckConsistentL(*aRes.iDb, *aRes.iGroupView, *aRes.iLocalView, aRes.iGroupId); |
|
175 } |
|
176 |
|
177 LOCAL_C void TestAddContactToGroupL() |
|
178 { |
|
179 test.Next(_L("Add a contact to the group")); |
|
180 |
|
181 CTestResources* res = CTestResources::NewLC(); |
|
182 |
|
183 CheckConsistentL(*res); |
|
184 |
|
185 // Find a contact to add into group |
|
186 for (TInt i=0; i < res->iContacts->Count(); ++i) |
|
187 { |
|
188 const TContactItemId contactId = (*res->iContacts)[i]; |
|
189 // Item found, add it to the group |
|
190 res->iDb->AddContactToGroupL(contactId, res->iGroupId); |
|
191 |
|
192 // delete the group, this might happen anytime on a multiuser db! |
|
193 if (i == 3) |
|
194 { |
|
195 res->iDb->DeleteContactL(res->iGroupId); |
|
196 } |
|
197 |
|
198 // Listen to the event from the DB |
|
199 TContactDbObserverEvent dbEvent; |
|
200 res->iDbEventQueue->ListenForEvent(2, dbEvent); |
|
201 |
|
202 // Listen to the event from the group view |
|
203 TContactViewEvent groupViewEvent; |
|
204 res->iGroupViewEventQueue->ListenForEvent(2, groupViewEvent); |
|
205 } |
|
206 |
|
207 // Eat away all events from queue to give iGroupView a chance to update itself |
|
208 TContactViewEvent groupViewEvent; |
|
209 while (res->iGroupViewEventQueue->ListenForEvent(2, groupViewEvent)) |
|
210 { |
|
211 } |
|
212 CleanupStack::PopAndDestroy(res); |
|
213 } |
|
214 |
|
215 |
|
216 LOCAL_C void TestRemoveContactFromGroupL() |
|
217 { |
|
218 test.Next(_L("Remove a contact from the group")); |
|
219 |
|
220 CTestResources* res = CTestResources::NewLC(); |
|
221 |
|
222 CheckConsistentL(*res); |
|
223 |
|
224 // Need to open and commit the group to get any events from CContactDatabase! |
|
225 CContactGroup* group = static_cast<CContactGroup*>(res->iDb->OpenContactLX(res->iGroupId)); |
|
226 CleanupStack::PushL(group); |
|
227 // Commit the group to get group changed event from Contact Db |
|
228 res->iDb->CloseContactL(group->Id()); |
|
229 |
|
230 const TContactItemId contactId = (*group->ItemsContained())[0]; |
|
231 res->iDb->RemoveContactFromGroupL(contactId, group->Id()); |
|
232 |
|
233 // Listen to the event from the DB |
|
234 TContactDbObserverEvent dbEvent; |
|
235 test(res->iDbEventQueue->ListenForEvent(2,dbEvent)); |
|
236 test(dbEvent.iType == EContactDbObserverEventGroupChanged); |
|
237 test(dbEvent.iContactId == group->Id()); |
|
238 |
|
239 // Listen to the event from the group view |
|
240 TContactViewEvent groupViewEvent; |
|
241 test(res->iGroupViewEventQueue->ListenForEvent(2,groupViewEvent)); |
|
242 |
|
243 CleanupStack::PopAndDestroy(2); // group, lock |
|
244 |
|
245 // Eat away all events from queue to give iGroupView a chance to update itself |
|
246 while (res->iGroupViewEventQueue->ListenForEvent(2,groupViewEvent)) |
|
247 { |
|
248 } |
|
249 |
|
250 CheckConsistentL(*res); |
|
251 CleanupStack::PopAndDestroy(res); |
|
252 } |
|
253 |
|
254 /** |
|
255 |
|
256 @SYMTestCaseID PIM-T-GROUPDELETEBUG-0001 |
|
257 |
|
258 */ |
|
259 |
|
260 void DoTestsL() |
|
261 { |
|
262 test.Start(_L("@SYMTESTCaseID:PIM-T-GROUPDELETEBUG-0001 T_GroupDeleteBug")); |
|
263 |
|
264 TestAddContactToGroupL(); |
|
265 TestRemoveContactFromGroupL(); |
|
266 test.End(); |
|
267 test.Close(); |
|
268 } |
|
269 |
|
270 GLDEF_C TInt E32Main() |
|
271 { |
|
272 // Init |
|
273 __UHEAP_MARK; |
|
274 CTrapCleanup* cleanupStack = CTrapCleanup::New(); |
|
275 if (!cleanupStack) |
|
276 { |
|
277 return KErrNoMemory; |
|
278 } |
|
279 |
|
280 CActiveScheduler* activeScheduler = new CActiveScheduler; |
|
281 if (!activeScheduler) |
|
282 { |
|
283 return KErrNoMemory; |
|
284 } |
|
285 CActiveScheduler::Install(activeScheduler); |
|
286 |
|
287 // Run the tests |
|
288 TRAPD(err, DoTestsL()); |
|
289 test(err == KErrNone); |
|
290 |
|
291 // Cleanup |
|
292 delete activeScheduler; |
|
293 delete cleanupStack; |
|
294 __UHEAP_MARKEND; |
|
295 return err; |
|
296 } |