|
1 // Copyright (c) 2005-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 // |
|
15 |
|
16 #include <e32test.h> |
|
17 #include <cntdb.h> |
|
18 #include <cntitem.h> |
|
19 #include "t_lowdiskspace.h" |
|
20 |
|
21 #include <cntfldst.h> |
|
22 |
|
23 _LIT(KTestName, "T_LOWDISKSPACE"); |
|
24 _LIT( KFileName, "c:\\anyfile.any" ); |
|
25 _LIT(KDatabaseFileName,"C:T_LOWDISKSPACE"); |
|
26 |
|
27 GLDEF_D RTest test(KTestName); |
|
28 |
|
29 static const TInt KMinusFull = 50000; |
|
30 static const TInt64 KLowDiskThreshold = 0x20000; |
|
31 |
|
32 const TInt KFiveHundredContactItems = 500; |
|
33 |
|
34 CLowDiskSpaceTest* CLowDiskSpaceTest::NewL() |
|
35 { |
|
36 CLowDiskSpaceTest* self = new(ELeave) CLowDiskSpaceTest(); |
|
37 CleanupStack::PushL(self); |
|
38 self->ConstructL(); |
|
39 CleanupStack::Pop(self); |
|
40 return self; |
|
41 } |
|
42 |
|
43 CLowDiskSpaceTest::CLowDiskSpaceTest():CActive(EPriorityIdle), iCount(0), iManyFiles(0) |
|
44 { |
|
45 CActiveScheduler::Add(this); |
|
46 } |
|
47 |
|
48 CLowDiskSpaceTest::~CLowDiskSpaceTest() |
|
49 { |
|
50 Cleanup(); |
|
51 } |
|
52 |
|
53 void CLowDiskSpaceTest::Cleanup() |
|
54 { |
|
55 TRAP_IGNORE(ClearDiskL() ); |
|
56 |
|
57 delete iContactDatabase; |
|
58 iContactDatabase = NULL; |
|
59 |
|
60 iFileSession.Close(); |
|
61 iFile->Close(); |
|
62 delete iFile; |
|
63 iFile = NULL; |
|
64 |
|
65 TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KDatabaseFileName) ); |
|
66 |
|
67 Cancel(); |
|
68 } |
|
69 |
|
70 void CLowDiskSpaceTest::DoCancel() |
|
71 { |
|
72 } |
|
73 |
|
74 void CLowDiskSpaceTest::ConstructL() |
|
75 { |
|
76 iContactDatabase = CContactDatabase::ReplaceL(KDatabaseFileName); |
|
77 |
|
78 // populate the database with 500 contact items |
|
79 for (TInt i = 0; i < KFiveHundredContactItems; ++i) |
|
80 { |
|
81 AddContactToDatabaseL(); |
|
82 } |
|
83 |
|
84 User::LeaveIfError(iFileSession.Connect()); |
|
85 iFile = new(ELeave) RFile(); |
|
86 } |
|
87 |
|
88 void CLowDiskSpaceTest::AddContactToDatabaseL() |
|
89 { |
|
90 _LIT(KXChar, "x"); |
|
91 CContactItem* item = CContactCard::NewLC(); |
|
92 CContactItemField* field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldCompanyName); |
|
93 field->SetMapping(KUidContactFieldVCardMapORG); |
|
94 field->TextStorage()->SetTextL(KXChar() ); |
|
95 item->AddFieldL(*field); |
|
96 CleanupStack::Pop(); // field |
|
97 field = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldPhoneNumber); |
|
98 field->SetMapping(KUidContactFieldVCardMapTEL); |
|
99 field->TextStorage()->SetTextL(KXChar() ); |
|
100 item->AddFieldL(*field); |
|
101 CleanupStack::Pop(); // field |
|
102 field = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldPhoneNumber); |
|
103 field->SetMapping(KUidContactFieldVCardMapTEL); |
|
104 field->TextStorage()->SetTextL(KXChar() ); |
|
105 item->AddFieldL(*field); |
|
106 CleanupStack::Pop(); // field |
|
107 field = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldPhoneNumber); |
|
108 field->SetMapping(KUidContactFieldVCardMapTEL); |
|
109 field->TextStorage()->SetTextL(KXChar() ); |
|
110 item->AddFieldL(*field); |
|
111 CleanupStack::Pop(); // field |
|
112 field = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldPhoneNumber); |
|
113 field->SetMapping(KUidContactFieldVCardMapTEL); |
|
114 field->SetHidden(ETrue); |
|
115 field->TextStorage()->SetTextL(KXChar() ); |
|
116 item->AddFieldL(*field); |
|
117 CleanupStack::Pop(); // field |
|
118 field = CContactItemField::NewLC(KStorageTypeText,KUidContactFieldSuffixName); |
|
119 field->SetMapping(KUidContactFieldVCardMapUnusedN); |
|
120 field->TextStorage()->SetTextL(KXChar() ); |
|
121 item->AddFieldL(*field); |
|
122 CleanupStack::Pop(); // field |
|
123 field=CContactItemField::NewLC(KStorageTypeText,KUidContactFieldPrefixName); |
|
124 field->SetMapping(KUidContactFieldVCardMapUnusedN); |
|
125 field->TextStorage()->SetTextL(KXChar() ); |
|
126 item->AddFieldL(*field); |
|
127 CleanupStack::Pop(); // field |
|
128 |
|
129 iContactDatabase->AddNewContactL(*item); |
|
130 CleanupStack::PopAndDestroy(); // item |
|
131 } |
|
132 |
|
133 |
|
134 void CLowDiskSpaceTest::Start() |
|
135 { |
|
136 TRequestStatus *pS=&iStatus; |
|
137 User::RequestComplete(pS,KErrNone); |
|
138 SetActive(); |
|
139 } |
|
140 |
|
141 void CLowDiskSpaceTest::RunL() |
|
142 { |
|
143 TVolumeInfo tv; |
|
144 switch(iCount) |
|
145 { |
|
146 case EFillDisk: |
|
147 FillDiskL();//Fill the disk |
|
148 iCount++; |
|
149 Start(); |
|
150 break; |
|
151 |
|
152 case EDatabaseTest: |
|
153 User::LeaveIfError( iFileSession.Volume(tv) ); |
|
154 if(tv.iFree >= KLowDiskThreshold) |
|
155 { |
|
156 test.Printf(_L("Free space increased, refill the disk again.\n")); |
|
157 ClearDiskL(); |
|
158 FillDiskL();//Fill the disk |
|
159 Start(); |
|
160 break; |
|
161 } |
|
162 DatabaseTestL(); |
|
163 iCount++; |
|
164 Start(); |
|
165 break; |
|
166 |
|
167 case EClearDisk: |
|
168 ClearDiskL(); |
|
169 iCount++; |
|
170 Start(); |
|
171 break; |
|
172 |
|
173 default: |
|
174 CActiveScheduler::Stop(); |
|
175 break; |
|
176 } |
|
177 } |
|
178 |
|
179 void CLowDiskSpaceTest::FillDiskL() |
|
180 { |
|
181 _LIT(KFillDiskTitle, "Fill the disk"); |
|
182 test.Next(KFillDiskTitle); |
|
183 |
|
184 |
|
185 TVolumeInfo tv; |
|
186 User::LeaveIfError( iFileSession.Volume(tv) ); |
|
187 |
|
188 TInt frees = 0; |
|
189 iManyFiles = tv.iFree / KMaxTInt; |
|
190 |
|
191 if ( iManyFiles > 0) |
|
192 { |
|
193 TPtrC tname( KFileName ); |
|
194 TInt i = 0; |
|
195 for( ; i < iManyFiles; ++i ) |
|
196 { |
|
197 HBufC *fval=HBufC::NewLC( tname.Length()+4 );//assume #files < 10000 |
|
198 TPtr fptr = fval->Des() ; |
|
199 fptr.Append( tname ); |
|
200 fptr.AppendNum( i ); |
|
201 |
|
202 User::LeaveIfError( iFile->Replace( iFileSession, fptr, EFileWrite ) ); |
|
203 User::LeaveIfError( iFile->SetSize( KMaxTInt ) ); |
|
204 iFile->Close(); |
|
205 |
|
206 CleanupStack::PopAndDestroy( fval ); |
|
207 } |
|
208 User::LeaveIfError( iFileSession.Volume(tv) ); |
|
209 |
|
210 frees = tv.iFree - KMinusFull ; |
|
211 if( frees <= 0 ) |
|
212 { |
|
213 frees = tv.iFree; |
|
214 } |
|
215 User::LeaveIfError( iFile->Replace( iFileSession, KFileName, EFileWrite ) ); |
|
216 |
|
217 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__ |
|
218 TInt err = KErrDiskFull; |
|
219 while(err == KErrDiskFull) |
|
220 { |
|
221 err = iFile->SetSize(frees); |
|
222 frees -= 100; |
|
223 if(frees <= 0) |
|
224 { |
|
225 break; |
|
226 } |
|
227 } |
|
228 #else |
|
229 User::LeaveIfError( iFile->SetSize( frees ) ); |
|
230 #endif |
|
231 |
|
232 iFile->Close(); |
|
233 } |
|
234 else |
|
235 { |
|
236 frees = tv.iFree - KMinusFull ; |
|
237 if( frees <= 0 ) |
|
238 { |
|
239 frees = tv.iFree; |
|
240 } |
|
241 User::LeaveIfError( iFile->Replace( iFileSession, KFileName, EFileWrite ) ); |
|
242 |
|
243 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__ |
|
244 TInt err = KErrDiskFull; |
|
245 while(err == KErrDiskFull) |
|
246 { |
|
247 err = iFile->SetSize(frees); |
|
248 frees -= 100; |
|
249 if(frees <= 0) |
|
250 { |
|
251 break; |
|
252 } |
|
253 } |
|
254 #else |
|
255 User::LeaveIfError( iFile->SetSize( frees ) ); |
|
256 #endif |
|
257 |
|
258 iFile->Close(); |
|
259 } |
|
260 } |
|
261 |
|
262 |
|
263 void CLowDiskSpaceTest::DatabaseTestL() |
|
264 { |
|
265 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__ |
|
266 _LIT(KDatabaseTestTitle, "Add new contact and check whether disk is full"); |
|
267 #else |
|
268 _LIT(KDatabaseTestTitle, "Test add/delete contacts in low disk conditions"); |
|
269 #endif |
|
270 |
|
271 test.Next(KDatabaseTestTitle); |
|
272 |
|
273 CContactCard* card=CContactCard::NewL(); |
|
274 CleanupStack::PushL(card); |
|
275 TRAPD(ret, iContactDatabase->AddNewContactL(*card)); |
|
276 if (ret != KErrDiskFull) |
|
277 { |
|
278 Cleanup(); // delete files filling up disk before exiting the test |
|
279 test(EFalse); // fail the test |
|
280 } |
|
281 |
|
282 #ifdef __SYMBIAN_CNTMODEL_USE_SQLITE__ |
|
283 |
|
284 _LIT(KNumContactsMsg, "Number of contacts in the database: %d\n"); |
|
285 _LIT(KDeletingMsg, "Deleting contact item: %d... "); |
|
286 _LIT(KPassMsg, " [PASS -- Contacts deleted]\n"); |
|
287 _LIT(KFailMsg, " [FAILED with err code: %d]\n"); |
|
288 |
|
289 test.Printf(KNumContactsMsg, iContactDatabase->CountL()); |
|
290 const CContactIdArray* idArray = iContactDatabase->SortedItemsL(); // don't take ownership |
|
291 const TInt KCount = idArray->Count(); |
|
292 test(KCount == KFiveHundredContactItems); // check we have all 500 items in the database |
|
293 |
|
294 |
|
295 iContactDatabase->DatabaseBeginLC(ETrue); |
|
296 for (TInt i = KCount - 1; i >= 0; --i) |
|
297 { |
|
298 const TContactItemId KItemId = (*idArray)[i]; |
|
299 test.Printf(KDeletingMsg, KItemId); |
|
300 TRAP(ret, iContactDatabase->DeleteContactL(KItemId) ); |
|
301 if (ret != KErrNone) |
|
302 { |
|
303 test.Printf(KFailMsg, ret); |
|
304 Cleanup(); // delete files filling up disk before exiting the test |
|
305 test(EFalse); // fail the test |
|
306 } |
|
307 } |
|
308 iContactDatabase->DatabaseCommitLP(ETrue); |
|
309 test.Printf(KPassMsg); |
|
310 |
|
311 #endif |
|
312 |
|
313 CleanupStack::PopAndDestroy(card); |
|
314 } |
|
315 |
|
316 void CLowDiskSpaceTest::ClearDiskL() |
|
317 { |
|
318 test.Next(_L("Clear the disk")); |
|
319 |
|
320 |
|
321 TPtrC tname( KFileName ); |
|
322 TInt i = 0; |
|
323 for(i = 0 ; i < iManyFiles; ++i ) |
|
324 { |
|
325 HBufC *fval=HBufC::NewLC( tname.Length()+4 );//assume #files < 10000 |
|
326 TPtr fptr=fval->Des(); |
|
327 fptr.Append( tname ); |
|
328 fptr.AppendNum( i ); |
|
329 TInt err = iFileSession.Delete( fptr ); |
|
330 if( err != KErrNone && err != KErrNotFound ) |
|
331 { |
|
332 User::Leave( err ); |
|
333 } |
|
334 |
|
335 CleanupStack::PopAndDestroy( fval ); |
|
336 } |
|
337 TInt err = iFileSession.Delete( KFileName ); |
|
338 |
|
339 if( err != KErrNone && err != KErrNotFound ) |
|
340 { |
|
341 User::Leave( err ); |
|
342 } |
|
343 } |
|
344 |
|
345 LOCAL_C void DoTestL() |
|
346 { |
|
347 CLowDiskSpaceTest* test = CLowDiskSpaceTest::NewL(); |
|
348 test->Start(); |
|
349 CActiveScheduler::Start(); |
|
350 delete test; |
|
351 } |
|
352 |
|
353 /** |
|
354 |
|
355 @SYMTestCaseID PIM-T-LOWDISKSPACE-0001 |
|
356 |
|
357 */ |
|
358 |
|
359 GLDEF_C TInt E32Main() |
|
360 { |
|
361 __UHEAP_MARK; |
|
362 CActiveScheduler* scheduler=new CActiveScheduler; |
|
363 if (scheduler) |
|
364 { |
|
365 CActiveScheduler::Install(scheduler); |
|
366 CTrapCleanup* cleanup=CTrapCleanup::New(); |
|
367 test.Start(_L("@SYMTESTCaseID:PIM-T-LOWDISKSPACE-0001 T_LOWDISKSPACE")); |
|
368 |
|
369 TRAPD(r, DoTestL()); |
|
370 test(r == KErrNone); |
|
371 test.End(); |
|
372 test.Close(); |
|
373 delete scheduler; |
|
374 delete cleanup; |
|
375 } |
|
376 __UHEAP_MARKEND; |
|
377 return KErrNone; |
|
378 } |
|
379 |