summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_groupviewpanic.cpp
blob: 9a76a99ed5b28f6eb3dea6af9b202205a16eec5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
* GroupViewPanic Test module
*
*/



#include <e32test.h>
#include <cntdef.h>
#include <cntdb.h>
#include <cntitem.h>
#include <cntfield.h>
#include <cntfldst.h>
#include <cntviewbase.h>

#include "ccontactvieweventqueue.h"

_LIT(KTestName, "T_GroupViewPanic");

_LIT(KTestDbName, "c:T_GroupViewPanic.cdb");


LOCAL_D RTest test(KTestName);


class CRemoteViewTestResources : public CBase
    {
    public:
        static CRemoteViewTestResources* NewLC();
        void ConstructL();
        ~CRemoteViewTestResources();
        void CreateTestGroupsL();

        CContactDatabase* iDb;
        CContactIdArray* iContacts;
        CContactViewEventQueue* iViewEventQueue;
        RContactViewSortOrder iViewSortOrder;
        CContactRemoteView* iRemoteView;
    };
   
CRemoteViewTestResources* CRemoteViewTestResources::NewLC()
    {
    CRemoteViewTestResources* self = new(ELeave) CRemoteViewTestResources;
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

void CRemoteViewTestResources::ConstructL()
    {          
    iDb = CContactDatabase::ReplaceL(KTestDbName);
	iContacts = CContactIdArray::NewL();
    iViewEventQueue = CContactViewEventQueue::NewL();
    iViewSortOrder.AppendL(KUidContactFieldTemplateLabel);

    const TContactViewPreferences prefs = static_cast<TContactViewPreferences>
    			(EGroupsOnly |ESingleWhiteSpaceIsEmptyField | EUnSortedAtEnd);       
    iRemoteView = CContactRemoteView::NewL(*iViewEventQueue, *iDb, iViewSortOrder, prefs);
    
    // Wait for view to get ready
    TContactViewEvent event;
	
	TBool eventReady = iViewEventQueue->ListenForEvent(10,event);
    __ASSERT_ALWAYS(eventReady,User::Invariant());
    ASSERT(event.iEventType == TContactViewEvent::EReady);
    }

CRemoteViewTestResources::~CRemoteViewTestResources()
    {
    if (iRemoteView)
    	{
    	iRemoteView->Close(*iViewEventQueue);	
    	delete iViewEventQueue;
    	}
    iViewSortOrder.Close();    
    delete iContacts;
    delete iDb;
    TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName));
    }
    
class CTestResources : public CBase
    {
    public:
        static CTestResources* NewLC();
        void ConstructL();
        void CreateTestContactsL();
        void CreateTestGroupsL();
        ~CTestResources();

        CContactDatabase* iDb;
        CContactIdArray* iContacts;
        CContactViewEventQueue* iViewEventQueue;
        RContactViewSortOrder iViewSortOrder;
        CContactLocalView* iLocalView;
    };
    
CTestResources* CTestResources::NewLC()
    {
    CTestResources* self = new(ELeave) CTestResources;
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }




void CTestResources::ConstructL()
    {
    iDb = CContactDatabase::ReplaceL(KTestDbName);
    iContacts = CContactIdArray::NewL();

    // Create some contacts into the database (don't create any groups)
    CreateTestContactsL();

    // Close and reopen the DB (this does not help with the bug)
    /*
    delete iDb;
    iDb = NULL;
    iDb = CContactDatabase::OpenL(KTestDbName);
    */

    iViewEventQueue = CContactViewEventQueue::NewL();

    iViewSortOrder.AppendL(KUidContactFieldTemplateLabel);
    
    // Create a groups only contact view
    const TContactViewPreferences prefs = 
        static_cast<TContactViewPreferences>(EGroupsOnly);
    iLocalView = CContactLocalView::NewL
        (*iViewEventQueue, *iDb, iViewSortOrder, prefs);
#if defined (_DEBUG)
    // Wait for view to get ready
    TContactViewEvent event;
#endif
    ASSERT(iViewEventQueue->ListenForEvent(10,event));
    ASSERT(event.iEventType == TContactViewEvent::EReady);
    }

LOCAL_C void AddFieldToContactL
        (CContactItem& aContact, TFieldType aFieldType, const TDesC& aText)
    {
    CContactItemField* field = CContactItemField::NewLC(KStorageTypeText, aFieldType);
    field->TextStorage()->SetTextL(aText);
    aContact.AddFieldL(*field);
    CleanupStack::Pop(field);
    }

LOCAL_C TContactItemId CreateTestContactL
        (CContactDatabase& aDb,
        const TDesC& aFamilyName, 
        const TDesC& aGivenName, 
        const TDesC& aCompanyName)
    {
    CContactCard* card = CContactCard::NewLC();
    AddFieldToContactL(*card, KUidContactFieldFamilyName, aFamilyName);
    AddFieldToContactL(*card, KUidContactFieldGivenName, aGivenName);
    AddFieldToContactL(*card, KUidContactFieldCompanyName, aCompanyName);
    const TContactItemId id = aDb.AddNewContactL(*card);
    CleanupStack::PopAndDestroy(card);
    return id;
    }

// Creates a few test contacts.
void CTestResources::CreateTestContactsL()
    {
    TContactItemId id;
    id = CreateTestContactL(*iDb, _L("Hakkinen"), _L("Mika"), _L("McLaren"));
    iContacts->AddL(id);
    id = CreateTestContactL(*iDb, _L("Coulthard"), _L("David"), _L("McLaren"));
    iContacts->AddL(id);
    id = CreateTestContactL(*iDb, _L("Schumacher"), _L("Michael"), _L("Ferrari"));
    iContacts->AddL(id);
    id = CreateTestContactL(*iDb, _L("Raikkonen"), _L("Kimi"), _L("Sauber"));
    iContacts->AddL(id);
    }

CTestResources::~CTestResources()
    {
    if (iLocalView)
    	{
    	iLocalView->Close(*iViewEventQueue);	
    	delete iViewEventQueue;
    	}
    iViewSortOrder.Close();    
    delete iContacts;
    delete iDb;
    TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName));
    }

LOCAL_C void TestDeleteContactsL()
    {
    // Create test resources
    CTestResources* res = CTestResources::NewLC();

    // Delete all contacts in the DB
    for (TInt i=0; i < res->iContacts->Count(); ++i)
        {
        res->iDb->DeleteContactL((*res->iContacts)[i]);
        }

    CleanupStack::PopAndDestroy(res);
    }

void CheckEvent(const TContactViewEvent& event, 
				TContactViewEvent::TEventType expectedEventType,
				TInt expectedUnderlyingIndex,
				TContactItemId expectedItemId)
	{	
	test(event.iEventType == expectedEventType);
	test(event.iInt == expectedUnderlyingIndex);
	test(event.iContactId == expectedItemId);
	}


void CRemoteViewTestResources::CreateTestGroupsL()
    {
	test.Next(_L("Test for creating a remote view and validating that queued events are received in the correct order."));

	TContactViewEvent event;

	// Create A then "unnanmed", then rename "unnamed" to B
	test.Next(_L("Test for Create group A, then create group B"));

	CContactItem* group1 = iDb->CreateContactGroupLC(_L("A"));	
	iViewEventQueue->ListenForEvent(2,event);
	CheckEvent(event, TContactViewEvent::EItemAdded, 0, group1->Id());

	CContactItem* group0 = iDb->CreateContactGroupLC(_L(""));
	iViewEventQueue->ListenForEvent(2,event);
	CheckEvent(event, TContactViewEvent::EItemAdded, 1, group0->Id());
	 	
	CContactItem *group00 = iDb->OpenContactL(group0->Id());
	static_cast<CContactGroup*> (group00)->SetGroupLabelL(_L("B"));
	iDb->CommitContactL(*group00);
		
	// Received 3 events, grp changed, removed and added
	iViewEventQueue->ListenForEvent(2,event);
	CheckEvent(event, TContactViewEvent::EGroupChanged, 0, group00->Id());
	iViewEventQueue->ListenForEvent(2,event);
	CheckEvent(event, TContactViewEvent::EItemRemoved, 1, group00->Id());
	iViewEventQueue->ListenForEvent(2,event);
	CheckEvent(event, TContactViewEvent::EItemAdded, 1, group00->Id()); 	

	// Create D then "unnamed", then rename "unnamed" to C
	test.Next(_L("Test for Create group D, then create group C"));

	CContactItem* group2 = iDb->CreateContactGroupLC(_L("D"));	
	iViewEventQueue->ListenForEvent(2,event);
	CheckEvent(event, TContactViewEvent::EItemAdded, 2, group2->Id());

	CContactItem* group3 = iDb->CreateContactGroupLC(_L(""));
	iViewEventQueue->ListenForEvent(2,event);
	CheckEvent(event, TContactViewEvent::EItemAdded, 3, group3->Id());
	 	
	CContactItem *group4 = iDb->OpenContactL(group3->Id());
	static_cast<CContactGroup*> (group4)->SetGroupLabelL(_L("C"));
	iDb->CommitContactL(*group4);
		
	// Received 3 events, grp changed, removed and added
	iViewEventQueue->ListenForEvent(2,event);
	CheckEvent(event, TContactViewEvent::EGroupChanged, 0, group4->Id());
	iViewEventQueue->ListenForEvent(2,event);
	CheckEvent(event, TContactViewEvent::EItemRemoved, 3, group4->Id());
	iViewEventQueue->ListenForEvent(2,event);
	CheckEvent(event, TContactViewEvent::EItemAdded, 2, group4->Id()); 

	// More complex set of add and deletes.
	test.Next(_L("Test-Addgroup E, Addgroup F, Addgroup G , RemoveGroup G Addgroup H RenameGroup H as EA"));

	CContactItem* group5 = iDb->CreateContactGroupLC(_L("E"));	   
	CContactItem* group6 = iDb->CreateContactGroupLC(_L("F"));	    
	CContactItem* group7 = iDb->CreateContactGroupLC(_L("G"));    
	iDb->DeleteContactL(group7->Id());	    
	CContactItem* group8 = iDb->CreateContactGroupLC(_L("H"));	

	CContactItem *group01 = iDb->OpenContactL(group8->Id());
	static_cast<CContactGroup*> (group01)->SetGroupLabelL(_L("EA"));
	iDb->CommitContactL(*group01);


	// Should receive events for operations above, order is not important 
	// Should receive events as add for E F EA(H) - In the order E EA(H) F 
	// (none for G as add, then remove)
    iViewEventQueue->ListenForEvent( 2, event );
    CheckEvent( event, TContactViewEvent::EItemAdded, 4, group5->Id() );
    iViewEventQueue->ListenForEvent( 2, event );
    CheckEvent( event, TContactViewEvent::EItemAdded, 5, group6->Id() );
    iViewEventQueue->ListenForEvent( 2, event );
    CheckEvent( event, TContactViewEvent::EItemAdded, 6, group8->Id() );
    iViewEventQueue->ListenForEvent( 2, event );
    CheckEvent( event, TContactViewEvent::EItemAdded, 7, group7->Id() );
    iViewEventQueue->ListenForEvent( 2, event );
    CheckEvent( event, TContactViewEvent::EItemRemoved, 7, group7->Id() );
    iViewEventQueue->ListenForEvent( 2, event );
    CheckEvent( event, TContactViewEvent::EGroupChanged, 0, group8->Id() );

	// Cleanup
	delete group01;
	delete group00;
	delete group4;    
	CleanupStack::PopAndDestroy(group8);
	CleanupStack::PopAndDestroy(group7);
	CleanupStack::PopAndDestroy(group6);
	CleanupStack::PopAndDestroy(group5);
	CleanupStack::PopAndDestroy(group3);
	CleanupStack::PopAndDestroy(group2);     
	CleanupStack::PopAndDestroy(group0);
	CleanupStack::PopAndDestroy(group1);
    }    

/**
@SYMTestCaseID PIM-T-GROUPVIEWPANIC-0001
@SYMTestType UT
@SYMTestPriority Urgent
@SYMDEF INC097928
@SYMTestCaseDesc Check the sequence of events when groups are added, removed or edited
@SYMTestActions
@SYMTestExpectedResults The sequence of events should be proper.
*/    
LOCAL_C void TestEventsForGroupCreationL()
    {
    // Create test resources
    CRemoteViewTestResources * pRes = CRemoteViewTestResources::NewLC();
    pRes->CreateTestGroupsL();  
    CleanupStack::PopAndDestroy(pRes);  
    }   

LOCAL_C void DoTestsL()
    {
	test.Start(_L("@SYMTESTCaseID:PIM-T-GROUPVIEWPANIC-0001 T_GroupViewPanic"));

    TestDeleteContactsL();
    TestEventsForGroupCreationL();    
    test.End();
    test.Close();
    }

GLDEF_C TInt E32Main()
	{
    // Init
    CTrapCleanup* cleanupStack = CTrapCleanup::New();
    if (!cleanupStack)
        {
        return KErrNoMemory;
        }

    CActiveScheduler* activeScheduler = new CActiveScheduler;
    if (!activeScheduler)
        {
        return KErrNoMemory;
        }
    CActiveScheduler::Install(activeScheduler);

    // Run the tests
    __UHEAP_MARK;
    TRAPD(err, DoTestsL());
    __UHEAP_MARKEND;

    // Cleanup
    delete activeScheduler;
    delete cleanupStack;

	return err;
    }