summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/cntviewstore/t_viewstore.cpp
blob: 4427003536b5fafc5eacb792376ec31b10b32e32 (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* Contact: http://www.qt-project.org/legal
* 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: 
* RTest based unit test to verify the operation of the functions
* to get/set 'default' view definitions in cntmodel. The functions are:
* TContactViewStore::DefaultViewsL()
* TContactViewStore::SetDefaultViewsL()
*
*/


#include <e32base.h>
#include <e32test.h>
#include <cntdb.h>
#include <cntviewbase.h>
#include <cntviewstore.h>
#include <centralrepository.h>
#include "t_viewstore.h"
#include "cntviewprivate.h"
#include "cntviewstoreprivate.h"

/*
  List of test cases
  
  Need to mainly test the set function, so test various
  combinations of setting view defs. e.g.
  
  - Set the default techview settings
  - Set multiple (valid) view settings
  - Set invalid view settings
  - Set mixed valid & invalid view settings
  
  Other tests that may be of use:
  
  - Different default repository files, some with invalid data?
  - Error conditions:
    - Calling DefaultViewsL() with a non-empty RArray: KErrArgument
    - (Set)DefaultViewsL() with no repository: KErrNotFound

*/

_LIT(KTitle, "t_viewstore");

RTest test(KTitle);

class CTestManager : public CBase, public MContactViewObserver
	{
public:	
	static CTestManager* NewLC();
	
	void HandleContactViewEvent(const CContactViewBase &aView, const TContactViewEvent &aEvent);
	
	void TestDefaultDefinitionL();
	void TestInvalidViewNameL();
	void TestInvalidSortPluginNameL();
	void TestMultipleValidViewDefinitionsL();
	void TestMultipleValidInvalidViewDefinitionsL();
	void TestRetrievingExistingViewsL();	
	void TestDeleteReplaceL();
	
private:
	CTestManager() {}
	~CTestManager();
	void ConstructL();
	void Close();
	
private:
	CContactDatabase* iDb;
	RPointerArray<CContactViewBase> iViews;
	};

/*
  operator == for CContactDefaultViewDefinition
*/
TBool operator == (const CContactDefaultViewDefinition& viewDef1, const CContactDefaultViewDefinition& viewDef2)
	{
	if (viewDef1.ViewPreferences() != viewDef2.ViewPreferences())
		{
		return EFalse;
		}
		
	if (!(viewDef1.SortOrder() == viewDef2.SortOrder()))
		{
		return EFalse;
		}
					
	if (viewDef1.ViewNameL().Compare(viewDef2.ViewNameL()) != 0)
		{
		return EFalse;
		}
		
	if (viewDef1.SortPluginNameL().Compare(viewDef2.SortPluginNameL()) != 0)
		{
		return EFalse;
		}
	
	if (viewDef1.ViewType() != viewDef2.ViewType())
		{
		return EFalse;
		}
	
	return ETrue;
	}

/*
*/
CContactViewDefinitionGenerator* CContactViewDefinitionGenerator::NewLC()
	{
	CContactViewDefinitionGenerator* self = new (ELeave) CContactViewDefinitionGenerator;
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}
	
/*
*/
void CContactViewDefinitionGenerator::ConstructL()
	{
	}
	
/*
*/
CContactDefaultViewDefinition* CContactViewDefinitionGenerator::CreateDefinitionLC(TViewDescription aDesc)
	{
	CContactDefaultViewDefinition* viewDef = 0;
	RContactViewSortOrder viewSortOrder;
	CleanupClosePushL(viewSortOrder);

	switch (aDesc)
		{
		case ETechviewContactsAppView:
			{
			// This is the view definition used
			// by the Techview Contacts application
			_LIT(KContactsAppViewName, "KMainContactsAppView");
			_LIT8(KSortPluginName, "");
			
			viewSortOrder.AppendL(KUidContactFieldFamilyName);
			viewSortOrder.AppendL(KUidContactFieldGivenName);
			viewSortOrder.AppendL(KUidContactFieldCompanyName);
			
			viewDef = CContactDefaultViewDefinition::NewLC(CContactDefaultViewDefinition::ENamedRemoteView, 
												   KContactsAppViewName, viewSortOrder, 
												   EContactsOnly, KSortPluginName);
			break;
			}
		case EValidLFCView:
			{
			// This is a generic remote view typical
			// of what might be used on a phone. It
			// sorts the contacts based on last name,
			// first name and company name, in that order.
			_LIT(KGenericLFCViewName, "KGenericLFCView");
			_LIT8(KSortPluginName, "");
			
			viewSortOrder.AppendL(KUidContactFieldFamilyName);
			viewSortOrder.AppendL(KUidContactFieldGivenName);
			viewSortOrder.AppendL(KUidContactFieldPhoneNumber);
			
			viewDef = CContactDefaultViewDefinition::NewLC(CContactDefaultViewDefinition::ENamedRemoteView, 
												   KGenericLFCViewName, viewSortOrder, 
												   EContactsOnly, KSortPluginName);
			break;
			}
		case EValidLFPView:
			{
			// This is a generic remote view typical
			// of what might be used on a phone. It
			// sorts the contacts based on last name,
			// first name and phone number, in that order.
			_LIT(KGenericLFPViewName, "KGenericLFPView");
			_LIT8(KSortPluginName, "");
			
			viewSortOrder.AppendL(KUidContactFieldPhoneNumber);
			viewSortOrder.AppendL(KUidContactFieldGivenName);
			viewSortOrder.AppendL(KUidContactFieldFamilyName);
			viewSortOrder.AppendL(KUidContactFieldCompanyName);
			
			viewDef = CContactDefaultViewDefinition::NewLC(CContactDefaultViewDefinition::ENamedRemoteView, 
												   KGenericLFPViewName, viewSortOrder, 
												   EContactsOnly, KSortPluginName);
			break;
			}
		case EInvalidViewNameView:
			{
			// This view is invalid because the view name
			// is longer than KMaxUnicodeStringLength, the 
			// maximum length descriptor that can be stored
			// in a repository key.
			_LIT(KInvalidViewName, "KViewWithANameThatIsLongerThanTheMaximumAllowedLengthInACentralRepositoryKey");
			_LIT8(KSortPluginName, "");
			
			TBuf<NCentralRepositoryConstants::KMaxUnicodeStringLength + 2> invalidDesc;
			invalidDesc.Copy(KInvalidViewName);
			invalidDesc.AppendFill('*', (invalidDesc.MaxLength() - invalidDesc.Length()));
			
			viewSortOrder.AppendL(KUidContactFieldGivenName);
			
			viewDef = CContactDefaultViewDefinition::NewLC(CContactDefaultViewDefinition::ENamedRemoteView, 
												   invalidDesc, viewSortOrder, 
												   EContactsOnly, KSortPluginName);
			break;
			}
		case EInvalidSortPluginNameView:
			{
			// This view is invalid because the view name
			// is longer than KMaxBinaryLength, the 
			// maximum length 8-bit descriptor that can be 
			// stored in a repository key
			_LIT(KInvalidSortPluginViewName, "KInvalidSortPluginNameView");
			_LIT8(KInvalidSortPluginName, "InvalidSortPluginNameThatIsTooLongToFitIntoACentralRepositoryKey");
			
			TBuf8<NCentralRepositoryConstants::KMaxBinaryLength + 2> invalidDesc;
			invalidDesc.Copy(KInvalidSortPluginName);
			invalidDesc.AppendFill('*', (invalidDesc.MaxLength() - invalidDesc.Length()));
			
			viewSortOrder.AppendL(KUidContactFieldFamilyName);
			viewSortOrder.AppendL(KUidContactFieldGivenName);
			
			viewDef = CContactDefaultViewDefinition::NewLC(CContactDefaultViewDefinition::ENamedRemoteView, 
												   KInvalidSortPluginViewName, viewSortOrder, 
												   EContactsOnly, invalidDesc);
			break;
			}
		default:
			{
			User::Leave(KErrArgument);
			}
		}

	CleanupStack::Pop(viewDef);
	CleanupStack::PopAndDestroy(&viewSortOrder);
	CleanupStack::PushL(viewDef);	
	return viewDef;
	}

//
// CTestManager
//
CTestManager::~CTestManager()
	{
	Close();
	}
	
CTestManager* CTestManager::NewLC()
	{
	CTestManager* self = new (ELeave) CTestManager;
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

void CTestManager::ConstructL()
	{
	}
	
void CTestManager::Close()
	{
	for (TInt i = 0; i < iViews.Count(); i++)
		{
		iViews[i]->Close(*this);
		}
	iViews.Close();
	if (iDb)
		{
		delete iDb;
		iDb = NULL;
		}
	}

/*
  This gets called when a view is ready to use
*/
void CTestManager::HandleContactViewEvent(const CContactViewBase& /*aView*/, const TContactViewEvent& /*aEvent*/)
	{
	CActiveScheduler::Stop();
	}

/*
  Test the storing and retrieving of the
  view definition used by the Techview
  Contacts app
*/
void CTestManager::TestDefaultDefinitionL()
	{	
	CContactViewDefinitionGenerator* gen = CContactViewDefinitionGenerator::NewLC();
	RPointerArray<CContactDefaultViewDefinition> viewDefs;
	CleanupResetAndDestroyPushL(viewDefs);
	
	// Store the 'default' view definition
	CContactDefaultViewDefinition* viewDef = gen->CreateDefinitionLC(CContactViewDefinitionGenerator::ETechviewContactsAppView);
	viewDefs.AppendL(viewDef);	
	TRAPD(err, TContactViewStore::SetDefaultViewsL(viewDefs));
	test(err == KErrNone);
	viewDefs.Reset();
	
	// Retrieve the view definition(s) and compare to what was stored
	//for (TInt i=4 ;; i++)
	//	{
		//__UHEAP_SETFAIL(RHeap::EFailNext, i);
		//__UHEAP_MARK;
		TRAP(err, TContactViewStore::GetDefaultViewsL(viewDefs));
		//viewDefs.ResetAndDestroy();
		//__UHEAP_MARKEND;
		//__UHEAP_RESET;
		
	//	if (err != KErrNoMemory)
	//		{
	//		break;
	//		}
	//	}
	test(err == KErrNone);
	test(viewDefs.Count() == 1);
	test(*viewDef == *viewDefs[0]);
	
	CleanupStack::PopAndDestroy(viewDef);
	CleanupStack::PopAndDestroy(&viewDefs);
	CleanupStack::PopAndDestroy(gen);	
	}
	
/*
  Test the storing of a view with an invalid name. 
  i.e. the name is to long to fit into the cenrep.
*/
void CTestManager::TestInvalidViewNameL()
	{
	CContactViewDefinitionGenerator* gen = CContactViewDefinitionGenerator::NewLC();
	CContactDefaultViewDefinition* viewDef = gen->CreateDefinitionLC(CContactViewDefinitionGenerator::EInvalidViewNameView);
	RPointerArray<CContactDefaultViewDefinition> viewDefs;
	
	// SetDefaultViewsL should leave with KErrArgument because
	// the view name is too long.
	viewDefs.AppendL(viewDef);
	CleanupStack::Pop(viewDef);
	TRAPD(err, TContactViewStore::SetDefaultViewsL(viewDefs));
	test(err == KErrArgument);
	
	viewDefs.ResetAndDestroy();
	CleanupStack::PopAndDestroy(gen);	
	}

/*
  Test the storing of a view with an invalid sort plugin name. 
  i.e. the name is to long to fit into the cenrep.
*/
void CTestManager::TestInvalidSortPluginNameL()
	{
	CContactViewDefinitionGenerator* gen = CContactViewDefinitionGenerator::NewLC();
	CContactDefaultViewDefinition* viewDef = gen->CreateDefinitionLC(CContactViewDefinitionGenerator::EInvalidSortPluginNameView);
	RPointerArray<CContactDefaultViewDefinition> viewDefs;
	
	// SetDefaultViewsL should leave with KErrArgument because
	// the view name is too long.
	viewDefs.AppendL(viewDef);
	CleanupStack::Pop(viewDef);
	TRAPD(err, TContactViewStore::SetDefaultViewsL(viewDefs));
	test(err == KErrArgument);
	
	viewDefs.ResetAndDestroy();
	CleanupStack::PopAndDestroy(gen);	
	}

/*
  Test the storing and retrieval of multiple
  (valid) view definitions.
*/
void CTestManager::TestMultipleValidViewDefinitionsL()
	{
	CContactViewDefinitionGenerator* gen = CContactViewDefinitionGenerator::NewLC();
	RPointerArray<CContactDefaultViewDefinition> viewDefs;
	CContactDefaultViewDefinition* viewDef;
	CContactViewDefinitionGenerator::TViewDescription desc;
	
	// create some view definitions
	for (TInt i = 0; i < 100; i++)
		{
		desc = static_cast<CContactViewDefinitionGenerator::TViewDescription>(i % CContactViewDefinitionGenerator::ENumValidViewDescriptions);
		viewDef = gen->CreateDefinitionLC(desc);
		viewDefs.AppendL(viewDef);
		CleanupStack::Pop(viewDef);
		}

	// store the view definitions
	TRAPD(err, TContactViewStore::SetDefaultViewsL(viewDefs));
	test(err == KErrNone);
	
	// retrieve the definitions
	RPointerArray<CContactDefaultViewDefinition> storedViewDefs;
	TRAP(err, TContactViewStore::GetDefaultViewsL(storedViewDefs));
	test(err == KErrNone);
	
	// check the retrieved ones match the ones which were stored
	test(storedViewDefs.Count() == viewDefs.Count());
	for (TInt j = 0; j < storedViewDefs.Count(); j++)
		{
		test(*viewDefs[j] == *storedViewDefs[j]);
		}
	
	viewDefs.ResetAndDestroy();
	storedViewDefs.ResetAndDestroy();
	CleanupStack::PopAndDestroy(gen);	
	}

/*
  Test the storing and retrieval of multiple
  valid and invalid view definitions. This
  should fail, leaving with KErrArgument
*/
void CTestManager::TestMultipleValidInvalidViewDefinitionsL()
	{
	CContactViewDefinitionGenerator* gen = CContactViewDefinitionGenerator::NewLC();
	RPointerArray<CContactDefaultViewDefinition> viewDefs;
	CContactDefaultViewDefinition* viewDef;
	CContactViewDefinitionGenerator::TViewDescription desc;
	
	// create some view definitions
	for (TInt i = 0; i < 10; i++)
		{
		desc = static_cast<CContactViewDefinitionGenerator::TViewDescription>(i % CContactViewDefinitionGenerator::ENumViewDescriptions);
		viewDef = gen->CreateDefinitionLC(desc);
		viewDefs.AppendL(viewDef);
		CleanupStack::Pop(viewDef);
		}

	// store the view definitions - this should leave
	// with KErrArgument because of the invalid view(s)
	TRAPD(err, TContactViewStore::SetDefaultViewsL(viewDefs));
	test(err == KErrArgument);
	
	viewDefs.ResetAndDestroy();
	CleanupStack::PopAndDestroy(gen);	
	}
	
/*
*/
void CTestManager::TestRetrievingExistingViewsL()
	{
	CContactViewDefinitionGenerator* gen = CContactViewDefinitionGenerator::NewLC();
	RPointerArray<CContactDefaultViewDefinition> viewDefs;
	CContactDefaultViewDefinition* viewDef;
	TInt i;
	
	// create some view definitions
	for (i = 0; i < CContactViewDefinitionGenerator::ENumValidViewDescriptions; i++)
		{
		viewDef = gen->CreateDefinitionLC(CContactViewDefinitionGenerator::TViewDescription(i));
		viewDefs.AppendL(viewDef);
		CleanupStack::Pop(viewDef);
		}

	// store the view definitions
	TRAPD(err, TContactViewStore::SetDefaultViewsL(viewDefs));
	test(err == KErrNone);
	
	// retrieve the definitions
	RPointerArray<CContactDefaultViewDefinition> storedViewDefs;
	TRAP(err, TContactViewStore::GetDefaultViewsL(storedViewDefs));
	test(err == KErrNone);
	
	// create the views that were retrieved
	TRAP(err, iDb = CContactDatabase::OpenL());
	if (err != KErrNone)
		{
		if (err == KErrNotFound)
			{
			RDebug::Printf("Error: Couldn't find default database.");
			}
		User::Leave(err);
		}
	CContactViewBase* view = NULL;
	for (i = 0; i < storedViewDefs.Count(); i++)
		{
		viewDef = storedViewDefs[i];
		if (viewDef->ViewType() == CContactDefaultViewDefinition::ERemoteView)
			{
			view = CContactRemoteView::NewL(*this, *iDb, viewDef->SortOrder(), viewDef->ViewPreferences());
			}
		else
			{
			view = CContactNamedRemoteView::NewL(*this, viewDef->ViewNameL(),
												*iDb, viewDef->SortOrder(), 
												viewDef->ViewPreferences());
			}
		iViews.Append(view);

		// Wait for the view to be created
		CActiveScheduler::Start();			
		}
		
	// get the list of existing views and checked against stored ones
	RPointerArray<CContactDefaultViewDefinition> existingViewDefs;
	TContactViewStorePrivate::GetDefinitionsOfExistingViewsL(KNullDesC, existingViewDefs);
	test(storedViewDefs.Count() == existingViewDefs.Count());
	for (i = 0; i < existingViewDefs.Count(); i++)
		{
		test(*existingViewDefs[i] == *storedViewDefs[i]);
		}
	
	existingViewDefs.ResetAndDestroy();
	storedViewDefs.ResetAndDestroy();
	viewDefs.ResetAndDestroy();
	CleanupStack::PopAndDestroy(gen);
	}

/*
*/
void CTestManager::TestDeleteReplaceL()
	{
	// Quick test: assuming server has been started in non-transient at start-up,
	// try to replace the default database.
	Close();

	CContactDatabase::DeleteDefaultFileL();
	
	// Start the contacts server in non-transient mode
	
	// Attempt to delete the default file. This should not be blocked.
	
	// Stop the contacts server
	
	// Start contacts server in non-transient mode
	
	// Attempt to replace default file.
	}
			
/*
  Implementation of test code
*/

/**

@SYMTestCaseID     PIM-T-VIEWSTORE-0001

*/

LOCAL_C void doMainL()
	{
	CTestManager* testMgr = CTestManager::NewLC();
	
	test.Start(_L("@SYMTESTCaseID:PIM-T-VIEWSTORE-0001 Storing the Techview Contacts App view definition"));

	testMgr->TestDefaultDefinitionL();
	
	test.Next(_L("Storing a view definition with invalid view name"));

	testMgr->TestInvalidViewNameL();
	
	test.Next(_L("Storing a view definition with invalid sort plugin name"));

	testMgr->TestInvalidSortPluginNameL();
	
	test.Next(_L("Storing multiple valid view definitions"));

	testMgr->TestMultipleValidViewDefinitionsL();
	
	test.Next(_L("Storing mixed valid and invalid views"));

	testMgr->TestMultipleValidInvalidViewDefinitionsL();
	
	test.Next(_L("Retrieving the list of views that exist in the server"));

	testMgr->TestRetrievingExistingViewsL();
	
	test.Next(_L("Delete/Replace of default database when server is non-transient"));

	testMgr->TestDeleteReplaceL();

	CleanupStack::PopAndDestroy(testMgr);
	}

/*
  t_viewstore entry point
*/
GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
	
	test.Title();
		
	CTrapCleanup* cleanup = CTrapCleanup::New();
	if (!cleanup)
		{
		return KErrNoMemory;
		}
		
	CActiveScheduler * scheduler = new (ELeave) CActiveScheduler;
	CActiveScheduler::Install(scheduler);

	TRAPD(err, doMainL());
	if (err != KErrNone)
		{
		_LIT(KOutputRes,"t_viewstore returned with %d \n");
		TBuf<64> formattable;
		formattable.Format(KOutputRes, err);
		test.Printf(formattable);
		}
	test(err == KErrNone);

	delete scheduler;
	delete cleanup;
		
	test.End();
	test.Close();	
	
	__UHEAP_MARKEND;
	return err;
	}