summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_datacompatibility.cpp
blob: 6439a00ba274b347e300393f6678496ff3984fef (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
/*
* 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: 
*
*/


#include <e32std.h>
#include <e32test.h>
#include <cntdb.h>
#include <f32file.h>
#include <coreappstest/testserver.h>

#define __SYMBIAN_CNTMODEL_USE_SQLITE__

_LIT(KTestName, "T_DataCompatibility");

#ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__ 

//That file is a contact model database from a Calypso device
_LIT(KDatabaseV4, "Z:\\Core-Apps\\App-Engines\\CntModel\\Databases\\FormatV4.cdb"); 
_LIT(KDatabaseV5, "Z:\\Core-Apps\\App-Engines\\CntModel\\Databases\\FormatV5.cdb"); 

_LIT(KDatabaseV6With10Entries, "Z:\\Core-Apps\\App-Engines\\CntModel\\Databases\\FormatV6-10.cdb"); 
_LIT(KDatabaseV7With25Entries, "Z:\\Core-Apps\\App-Engines\\CntModel\\Databases\\FormatV7-25.cdb"); 
_LIT(KDatabaseV4With4Entries, "Z:\\Core-Apps\\App-Engines\\CntModel\\Databases\\FormatV4-4.cdb");
_LIT(KDatabaseV8, "Z:\\Core-Apps\\App-Engines\\CntModel\\Databases\\DBS_100065FF_performance_100_4.cdb");

_LIT(KDatabaseCDriveFullPath,"C:\\private\\10003A73\\contacts.cdb");
_LIT(KOldDatabaseCDriveFullPath,"C:\\private\\10003A73\\DBS_100065FF_performance_100_4.cdb");

_LIT(KDatabaseCDrive,"C:contacts.cdb");
_LIT(KOldDatabaseCDrive,"C:DBS_100065FF_performance_100_4.cdb");
    
_LIT(KOldFileFormatPrefix,"DBS_100065FF");

RFs fs;

// Logging macros & utilities //
GLDEF_C RTest test(KTestName);


LOCAL_C void CleanupFilesL()
    {
    // delete the database files
    if (CContactDatabase::ContactDatabaseExistsL(KDatabaseCDrive) )
        {
        CContactDatabase::DeleteDatabaseL(KDatabaseCDrive);
        }
    if (CContactDatabase::ContactDatabaseExistsL(KOldDatabaseCDrive) )
        {
        CContactDatabase::DeleteDatabaseL(KOldDatabaseCDrive);
        }
    }





void CopyCdbFileToCDriveL(const TDesC& aFileName)
    {
    //Will probably need more additions to sos to do this
    RPIMTestServer serv;
    User::LeaveIfError(serv.Connect());
    serv.CopyFileL(aFileName, KDatabaseCDriveFullPath);
    serv.Close();
    }
    
void CopyOldCdbFileToCDriveL(const TDesC& aFileName)
    {
    //Will probably need more additions to sos to do this
    RPIMTestServer serv;
    User::LeaveIfError(serv.Connect());
    serv.CopyFileL(aFileName, KOldDatabaseCDriveFullPath);
    serv.Close();
    }

typedef void (*TTestFunction)(TBool);

void DoTestV8DatabaseL()
    {
    CopyOldCdbFileToCDriveL(KDatabaseV8);
    CContactDatabase* db=CContactDatabase::OpenL(KDatabaseCDrive); 
    TDriveUnit driveUnit = _L("c:");
    TBuf<190> driveName;
    CDesCArray* dataBaseList = db->ListDatabasesL(driveUnit);
    CleanupStack::PushL(dataBaseList);
    TInt theCount = dataBaseList->Count();
    TBool oldFileFormat = EFalse;
    for(TInt i = 0; i< theCount; i++)
        {
        driveName = (*dataBaseList)[i];
        test.Printf(_L("ListDatabaseTestL, ListDatabasesL: %s \n"),driveName.PtrZ());
        TPtrC filenamePtr(driveName);
        TInt found = filenamePtr.FindF(KOldFileFormatPrefix);
        if (found != 0 && found != KErrNotFound)
            {
            oldFileFormat = ETrue;  
            }
        }
    test(oldFileFormat);

    CleanupStack::PopAndDestroy(dataBaseList);
    delete db;
    }

void DoTestV7DatabaseL()
    {
    CopyCdbFileToCDriveL(KDatabaseV7With25Entries);
    CContactDatabase* db=CContactDatabase::OpenL(KDatabaseCDrive); 
    delete db;
    }

void DoTestV6DatabaseL()
    {
    CopyCdbFileToCDriveL(KDatabaseV6With10Entries);
    CContactDatabase* db=CContactDatabase::OpenL(KDatabaseCDrive); 
    delete db;
    }

void DoTestV5DatabaseL()
    {
    CopyCdbFileToCDriveL(KDatabaseV5);
    CContactDatabase* db=CContactDatabase::OpenL(KDatabaseCDrive); 
    delete db;
    }

void DoTestV4DatabaseL(TBool aOOMTest = ETrue)
    {
    if (aOOMTest)
        {
        CopyCdbFileToCDriveL(KDatabaseV4With4Entries);
        }
    else
        {
        CopyCdbFileToCDriveL(KDatabaseV4);
        }
    
    TTime startTime;
    TTime endTime;
        
        
    //Test end profiling
    startTime.UniversalTime(); //start profiling
    CContactDatabase* db=CContactDatabase::OpenL(KDatabaseCDrive); 
    endTime.UniversalTime(); //end profiling
    CleanupStack::PushL(db);
    
    //need to sort item otherwise CountL return 0 - only in old model
/*  CContactDatabase::TSortPref sortPref(KUidContactFieldGivenName);
    CArrayFix<CContactDatabase::TSortPref>* sortOrder=new(ELeave) CArrayFixFlat<CContactDatabase::TSortPref>(1);
    CleanupStack::PushL(sortOrder);
    sortOrder->AppendL(sortPref);
    db->SortL(sortOrder); //Takes ownership
    CleanupStack::Pop(sortOrder);*/

    TInt count=db->CountL();
    
    //Now compute delay and print on the screen
    TTimeIntervalMicroSeconds delay=endTime.MicroSecondsFrom(startTime);
    test.Printf(_L("A v4 database containing %d contacts was converted in %d µs\n"),count,delay.Int64());

    CleanupStack::PopAndDestroy(db); //db
    }

void DoOOMTestL(TTestFunction aTestFunction)
    {
    TInt ret = KErrNoMemory;
    TInt failAt = 0;
    while(ret != KErrNone)
        {
        failAt += 1 + failAt/30;
        test.Printf(_L("OOM step: %d\n"), failAt);
        __UHEAP_SETFAIL(RHeap::EDeterministic, failAt);
        __UHEAP_MARK;
        TRAP(ret, aTestFunction(ETrue));
        __UHEAP_MARKEND;
        __UHEAP_RESET;
        
        if  (ret != KErrNoMemory && ret != KErrNone)
            {
            test.Printf(_L("OOM test failed: %d\n"),ret);
            }

        if (!(ret == KErrNoMemory || ret == KErrNone) )
            {
            TRAP_IGNORE(CleanupFilesL() );
            }
        test(ret == KErrNoMemory || ret == KErrNone);
        }
    }


void DoTestL()
    {
    User::LeaveIfError(fs.Connect());
    CleanupClosePushL(fs);
    CTestRegister* TempFiles = CTestRegister::NewLC();
    TempFiles->RegisterL(KDatabaseCDrive, EFileTypeCnt);

    _LIT(KTestV8Database,"Opening DBMS secured (old) database...");
    _LIT(KTestV7Database,"Opening v7 format database...");
    _LIT(KTestV6Database,"Opening v6 format database...");
    _LIT(KTestV5Database,"Opening v5 format database...");
    _LIT(KTestV4Database,"Opening v4 format database...");
    
    
    test.Next(_L("Opening v7 format database..."));

    DoTestV7DatabaseL();

    test.Next(_L("Opening DBMS secured (old) database..."));

    DoTestV8DatabaseL();

        
    test.Next(_L("Opening v6 format database..."));

    DoTestV6DatabaseL();
    
    test.Next(_L("Opening v5 format database..."));

    DoTestV5DatabaseL();

    test.Next(_L("Opening v4 format database..."));

    DoTestV4DatabaseL(EFalse);

    //v4 conversion to v7 executes code involved in any other conversion.
    DoOOMTestL(DoTestV4DatabaseL); 
    //DoOOMTestL(DoTestV5DatabaseL);
    //DoOOMTestL(DoTestV6DatabaseL);
    //DoOOMTestL(DoTestV7DatabaseL);

    CleanupStack::PopAndDestroy(2); // TempFiles, fs
    }

/**

@SYMTestCaseID     PIM-T-DATACOMPATIBILITY-0001

*/

GLDEF_C TInt E32Main()
    {
    __UHEAP_MARK;
    CActiveScheduler* scheduler=new CActiveScheduler;
    if (scheduler)
        {
        CActiveScheduler::Install(scheduler);
        CTrapCleanup* cleanup=CTrapCleanup::New();
        if (cleanup)
            {
            test.Title();
            test.Start(_L("@SYMTESTCaseID:PIM-T-DATACOMPATIBILITY-0001 T_DataCompatibility"));

            TRAPD(err, DoTestL());
            // delete database files if they are not cleaned up elsewhere 
            TRAP_IGNORE(CleanupFilesL() );
            test(err == KErrNone);
            test.End();
            User::After(1000000); // wait 1 second and exit
            test.Close();
            delete cleanup;
            }
        delete scheduler;
        }
    __UHEAP_MARKEND;
    return KErrNone;
    }
#else

_LIT(KDatabase9_2, "Z:\\Core-Apps\\App-Engines\\CntModel\\Databases\\SQLite__Contacts.cdb");
_LIT(KDatabaseCDriveFullPath,"C:\\private\\10003A73\\SQLite__OLDVERSION_9_2.CDB");
_LIT(KDatabaseCDrive,"C:OLDVERSION_9_2.cdb");

RFs fs;

//////////// Logging macros & utilities ///////////////
LOCAL_D RTest test(KTestName);


LOCAL_C void CleanupFilesL()
	{
    // delete the database files
	if (CContactDatabase::ContactDatabaseExistsL(KDatabaseCDrive) )
		{
		CContactDatabase::DeleteDatabaseL(KDatabaseCDrive);
		}
	}

	
void Copy9_2CdbFileToCDriveL(const TDesC& aFileName)
	{
	//Will probably need more additions to sos to do this
	RPIMTestServer serv;
	User::LeaveIfError(serv.Connect());
	serv.CopyFileL(aFileName, KDatabaseCDriveFullPath);
	serv.Close();
	}

/*
Reads the database and gets the existing column count for this table


TInt GetExistingColumnCountFromDatabaseL()
    {    
    _LIT(KTableName, "contact");
    
    RSqlDatabase iDatabase;
    iDatabase.OpenL( KDatabaseCDriveFullPath );
    
    //Get the table information using the PRAGMA Statement
    RSqlStatement ColumnInfoStatement;
    CleanupClosePushL( ColumnInfoStatement );
    
    HBufC* sqlstmt = HBufC::NewLC( KPragmaStmnt().Length() + 
            KTableName().Length() );
    sqlstmt->Des().Format( KPragmaStmnt, &KTableName() );    
        
    //Get the name and type
    User::LeaveIfError( ColumnInfoStatement.Prepare( iDatabase, *sqlstmt ) );
    TInt count = ColumnInfoStatement.ColumnCount();
        
    CleanupStack::PopAndDestroy() ; //sqlstmt
    CleanupStack::PopAndDestroy( &ColumnInfoStatement );    
    
    return count;
    }*/

typedef void (*TTestFunction)(TBool);

void DoTest9_2_DatabaseL()
	{
	Copy9_2CdbFileToCDriveL(KDatabase9_2);	
	CContactDatabase* db=CContactDatabase::OpenL(KDatabaseCDrive);	
	delete db;	
	}


void DoTestL()
	{
	User::LeaveIfError(fs.Connect());
	CleanupClosePushL(fs);
	CTestRegister* TempFiles = CTestRegister::NewLC();
	TempFiles->RegisterL(KDatabaseCDrive, EFileTypeCnt);
	
	test.Next(_L("Opening DBMS secured (9.2 old) database..."));
	
	DoTest9_2_DatabaseL();		

	CleanupStack::PopAndDestroy(2); // TempFiles, fs
	}

/**

@SYMTestCaseID     PIM-T-DATACOMPATIBILITY-0001

*/

GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
	CActiveScheduler* scheduler=new CActiveScheduler;
	if (scheduler)
		{
		CActiveScheduler::Install(scheduler);
		CTrapCleanup* cleanup=CTrapCleanup::New();
		if (cleanup)
			{
			test.Title();
			test.Start(_L("@SYMTESTCaseID:PIM-T-DATACOMPATIBILITY-0001 T_DataCompatibility"));

			TRAPD(err, DoTestL());
			// delete database files if they are not cleaned up elsewhere 
			TRAP_IGNORE(CleanupFilesL() );
			test(err == KErrNone);
			test.End();
			User::After(1000000); // wait 1 second and exit
			test.Close();
			delete cleanup;
			}
		delete scheduler;
		}
	__UHEAP_MARKEND;
	return KErrNone;
    }


#endif