|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: test calenimporter api |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // CLASS HEADER |
|
21 #include "testcalenimporter.h" |
|
22 #include "calenimptestconstants.h" |
|
23 |
|
24 // EXTERNAL INCLUDES |
|
25 #include <EUnitMacros.h> |
|
26 #include <calsession.h> |
|
27 #include <CalEntry.h> // CCalEntry |
|
28 #include <DelimitedPathSegment8.h> |
|
29 #include <UTF.H> |
|
30 #include <CalenImporter.h> |
|
31 |
|
32 // ----------------------------------------------------------------------------- |
|
33 // CCalenImporterTest::NewL |
|
34 // Two-phased constructor. |
|
35 // ----------------------------------------------------------------------------- |
|
36 // |
|
37 CCalenImporterTest* CCalenImporterTest::NewL() |
|
38 { |
|
39 CCalenImporterTest* self = CCalenImporterTest::NewLC(); |
|
40 CleanupStack::Pop(); |
|
41 |
|
42 return self; |
|
43 } |
|
44 |
|
45 // ----------------------------------------------------------------------------- |
|
46 // CCalenImporterTest::NewLC |
|
47 // Two-phased constructor. |
|
48 // ----------------------------------------------------------------------------- |
|
49 // |
|
50 CCalenImporterTest* CCalenImporterTest::NewLC() |
|
51 { |
|
52 CCalenImporterTest* self = new( ELeave ) CCalenImporterTest(); |
|
53 CleanupStack::PushL( self ); |
|
54 |
|
55 self->ConstructL(); |
|
56 |
|
57 return self; |
|
58 } |
|
59 |
|
60 // ----------------------------------------------------------------------------- |
|
61 // CCalenImporterTest::~CCalenImporterTest() |
|
62 // Destructor (virtual by CBase) |
|
63 // ----------------------------------------------------------------------------- |
|
64 // |
|
65 CCalenImporterTest::~CCalenImporterTest() |
|
66 { |
|
67 } |
|
68 |
|
69 // ----------------------------------------------------------------------------- |
|
70 // CCalenImporterTest::CCalenImporterTest() |
|
71 // Default constructor |
|
72 // ----------------------------------------------------------------------------- |
|
73 // |
|
74 CCalenImporterTest::CCalenImporterTest(): iImportFile( NULL ), |
|
75 iExportFile( NULL ), |
|
76 iCheckFile( NULL ), |
|
77 iType( NULL ) |
|
78 { |
|
79 } |
|
80 |
|
81 // ----------------------------------------------------------------------------- |
|
82 // CCalenImporterTest::ConstructL |
|
83 // Second phase construction |
|
84 // ----------------------------------------------------------------------------- |
|
85 // |
|
86 void CCalenImporterTest::ConstructL() |
|
87 { |
|
88 // The ConstructL from the base class CEUnitTestSuiteClass must be called. |
|
89 // It generates the test case table. |
|
90 CEUnitTestSuiteClass::ConstructL(); |
|
91 |
|
92 } |
|
93 |
|
94 //============================================================================================ |
|
95 // Import/export ical and vcal |
|
96 //============================================================================================ |
|
97 |
|
98 |
|
99 // ----------------------------------------------------------------------------- |
|
100 // CCalenImporterTest::SetupL |
|
101 // Setup & Teardown |
|
102 // ----------------------------------------------------------------------------- |
|
103 // |
|
104 void CCalenImporterTest::SetupL() |
|
105 { |
|
106 //Create file server session |
|
107 EUNIT_ASSERT_EQUALS(iFs.Connect(), KErrNone); |
|
108 |
|
109 //Make an output directory if needed |
|
110 iFs.MkDirAll( KOutputDirName ); |
|
111 |
|
112 iCalSession = CCalSession::NewL(); |
|
113 iCalSession->OpenL( iCalSession->DefaultFileNameL() ); |
|
114 |
|
115 iCalenImporter = CCalenImporter::NewL( *iCalSession ); |
|
116 } |
|
117 |
|
118 |
|
119 // ----------------------------------------------------------------------------- |
|
120 // CCalenImporterTest::Teardown |
|
121 // Teardown |
|
122 // ----------------------------------------------------------------------------- |
|
123 // |
|
124 void CCalenImporterTest::Teardown() |
|
125 { |
|
126 if( iType ) |
|
127 { |
|
128 delete iType; |
|
129 iType = NULL; |
|
130 } |
|
131 if( iImportFile ) |
|
132 { |
|
133 delete iImportFile; // File to import from |
|
134 iImportFile = NULL; |
|
135 } |
|
136 |
|
137 if( iExportFile ) |
|
138 { |
|
139 delete iExportFile; // File to export to |
|
140 iExportFile = NULL; |
|
141 } |
|
142 |
|
143 if( iCheckFile ) |
|
144 { |
|
145 delete iCheckFile; // File to verify export against |
|
146 iCheckFile = NULL; |
|
147 } |
|
148 |
|
149 iEntries.ResetAndDestroy(); |
|
150 |
|
151 if( iCalenImporter ) |
|
152 { |
|
153 delete iCalenImporter; |
|
154 iCalenImporter = NULL; |
|
155 } |
|
156 |
|
157 if( iCalSession ) |
|
158 { |
|
159 delete iCalSession; |
|
160 iCalSession = NULL; |
|
161 } |
|
162 |
|
163 //Close File Server |
|
164 iFs.Close(); |
|
165 } |
|
166 |
|
167 // ----------------------------------------------------------------------------- |
|
168 // CCalenImporterTest::ImportL |
|
169 // Imports an iCalendar entry from iImportFile. |
|
170 // @param aExportFile the file to import from. Default 0 means take from ini file. |
|
171 // @internalTechnology |
|
172 // ----------------------------------------------------------------------------- |
|
173 // |
|
174 void CCalenImporterTest::ImportL() |
|
175 { |
|
176 //iEntries.ResetAndDestroy(); |
|
177 |
|
178 RFileReadStream fileStream; |
|
179 EUNIT_ASSERT_EQUALS(fileStream.Open(iFs, *iImportFile, EFileRead), KErrNone); |
|
180 CleanupClosePushL(fileStream); |
|
181 |
|
182 if( iType->Match( KTypeVCal ) != KErrNotFound ) |
|
183 { |
|
184 TRAPD( error, iCalenImporter->ImportVCalendarL( fileStream, iEntries ) ); |
|
185 if(error) |
|
186 { |
|
187 //to avoid warning |
|
188 } |
|
189 } |
|
190 else if( iType->Match( KTypeICal ) != KErrNotFound ) |
|
191 { |
|
192 TRAPD( error, iCalenImporter->ImportICalendarL( fileStream, iEntries ) ); |
|
193 if(error) |
|
194 { |
|
195 //to avoid warning |
|
196 } |
|
197 } |
|
198 |
|
199 CleanupStack::PopAndDestroy(); // fileStream |
|
200 } |
|
201 |
|
202 // ----------------------------------------------------------------------------- |
|
203 // CCalenImporterTest::ParseFileL |
|
204 // Parses the ini file and sends the content to ParseLineL. |
|
205 // @param aFile the ini file. |
|
206 // @internalTechnology |
|
207 // ----------------------------------------------------------------------------- |
|
208 // |
|
209 void CCalenImporterTest::ParseFileL( const TDesC16& aFile ) |
|
210 { |
|
211 HBufC* configFile = HBufC::NewLC( KInputDirName().Length() + aFile.Length() ); |
|
212 |
|
213 configFile->Des().Insert( 0, KInputDirName ); |
|
214 configFile->Des().Append( aFile ); |
|
215 |
|
216 RFileReadStream fileStream; |
|
217 EUNIT_ASSERT_EQUALS(fileStream.Open(iFs, *configFile, EFileRead), KErrNone); |
|
218 CleanupClosePushL(fileStream); |
|
219 |
|
220 // Buffer to hold each line of a file |
|
221 HBufC8* buf = HBufC8::NewLC(KICalMaxContentLineLength); |
|
222 TPtr8 bufPtr = buf->Des(); |
|
223 |
|
224 TInt error(KErrNone); |
|
225 TBool found = EFalse; |
|
226 |
|
227 while (error == KErrNone && !found) |
|
228 { |
|
229 |
|
230 TRAP( error, fileStream.ReadL( bufPtr, TChar(KNewline) ) ); |
|
231 |
|
232 if( error == KErrNone ) |
|
233 { |
|
234 ParseLineL( bufPtr ); |
|
235 |
|
236 RunTestL(); |
|
237 } |
|
238 } |
|
239 CleanupStack::PopAndDestroy(buf); |
|
240 CleanupStack::PopAndDestroy(&fileStream); |
|
241 CleanupStack::PopAndDestroy(configFile); |
|
242 } |
|
243 |
|
244 // ----------------------------------------------------------------------------- |
|
245 // CCalenImporterTest::ParseLineL |
|
246 // Parses a RFC2396 specified line. |
|
247 // @param aLine the line to parse. |
|
248 // @internalTechnology |
|
249 // ----------------------------------------------------------------------------- |
|
250 // |
|
251 void CCalenImporterTest::ParseLineL( const TDesC8& aLine ) |
|
252 { |
|
253 EUNIT_PRINT( aLine ); |
|
254 /* |
|
255 TDelimitedPathSegmentParser8 Parses path segments whose |
|
256 components are delimited by the ';' character, |
|
257 as defined in RFC2396. |
|
258 */ |
|
259 |
|
260 TDelimitedPathSegmentParser8 parser; |
|
261 parser.Parse( aLine ); |
|
262 TPtrC8 segment; |
|
263 TPtr8 test(NULL, 0); |
|
264 |
|
265 //The line parsed from the file is expected to have the following format: |
|
266 //type;import_file_name;check_file_name;export_file_name; |
|
267 |
|
268 |
|
269 //Get type vcal/ical |
|
270 if( parser.GetNext( segment ) != KErrNotFound ) |
|
271 { |
|
272 if( iType ) |
|
273 { |
|
274 delete iType; |
|
275 iType = NULL; |
|
276 } |
|
277 |
|
278 iType = CnvUtfConverter::ConvertToUnicodeFromUtf8L( segment); |
|
279 } |
|
280 |
|
281 //Get Import file name |
|
282 if( parser.GetNext( segment ) != KErrNotFound ) |
|
283 { |
|
284 if( iImportFile ) |
|
285 { |
|
286 delete iImportFile; |
|
287 iImportFile = NULL; |
|
288 } |
|
289 |
|
290 iImportFile = CnvUtfConverter::ConvertToUnicodeFromUtf8L( segment); |
|
291 iImportFile = iImportFile->ReAlloc( iImportFile->Length() + KInputDirName().Length() ); |
|
292 iImportFile->Des().Insert( 0, KInputDirName ); |
|
293 } |
|
294 |
|
295 //Get Check file name |
|
296 if( parser.GetNext( segment ) != KErrNotFound ) |
|
297 { |
|
298 if( iCheckFile ) |
|
299 { |
|
300 delete iCheckFile; |
|
301 iCheckFile = NULL; |
|
302 } |
|
303 |
|
304 iCheckFile = CnvUtfConverter::ConvertToUnicodeFromUtf8L( segment); |
|
305 iCheckFile = iCheckFile->ReAlloc( iCheckFile->Length() + KInputDirName().Length() ); |
|
306 iCheckFile->Des().Insert( 0, KInputDirName); |
|
307 } |
|
308 |
|
309 //Get Export file name |
|
310 if( parser.GetNext( segment ) != KErrNotFound ) |
|
311 { |
|
312 if( iExportFile ) |
|
313 { |
|
314 delete iExportFile; |
|
315 iExportFile = NULL; |
|
316 } |
|
317 |
|
318 iExportFile = CnvUtfConverter::ConvertToUnicodeFromUtf8L( segment); |
|
319 iExportFile = iExportFile->ReAlloc( iExportFile->Length() + KOutputDirName().Length() ); |
|
320 iExportFile->Des().Insert( 0, KOutputDirName ); |
|
321 } |
|
322 } |
|
323 |
|
324 // ----------------------------------------------------------------------------- |
|
325 // CCalenImporterTest:: |
|
326 // This function is called by ParseFileL for each |
|
327 // line in the ini file |
|
328 // ----------------------------------------------------------------------------- |
|
329 // |
|
330 void CCalenImporterTest::RunTestL() |
|
331 { |
|
332 ImportL(); |
|
333 } |
|
334 |
|
335 // ----------------------------------------------------------------------------- |
|
336 // CCalenImporterTest::ImportTestL |
|
337 // |
|
338 // ----------------------------------------------------------------------------- |
|
339 // |
|
340 void CCalenImporterTest::ImportTestL( ) |
|
341 { |
|
342 /* |
|
343 Starts the whole test |
|
344 |
|
345 ImportExportTestL --> |
|
346 ParseFileL --> (Loops through the whole file) |
|
347 ParseLineL |
|
348 RunTest --> |
|
349 ImportL(); |
|
350 ExportL(); |
|
351 CompareFilesL(); |
|
352 */ |
|
353 EUNIT_ASSERT_NO_LEAVE( ParseFileL( KTestIniFile ) ); |
|
354 } |
|
355 |
|
356 //============================================================================================ |
|
357 // specific import function test cases |
|
358 //============================================================================================ |
|
359 |
|
360 // ----------------------------------------------------------------------------- |
|
361 // CCalenImporterTest::SetupImporterL |
|
362 // Setup only importer |
|
363 // ----------------------------------------------------------------------------- |
|
364 // |
|
365 void CCalenImporterTest::SetupImporterL() |
|
366 { |
|
367 |
|
368 iCalSession = CCalSession::NewL(); |
|
369 iCalSession->OpenL( iCalSession->DefaultFileNameL() ); |
|
370 |
|
371 iCalenImporter = CCalenImporter::NewL( *iCalSession ); |
|
372 } |
|
373 |
|
374 // ----------------------------------------------------------------------------- |
|
375 // CCalenImporterTest::TeardownImporter |
|
376 // Teardown only importer |
|
377 // ----------------------------------------------------------------------------- |
|
378 // |
|
379 void CCalenImporterTest::TeardownImporter() |
|
380 { |
|
381 if( iCalenImporter ) |
|
382 { |
|
383 delete iCalenImporter; |
|
384 iCalenImporter = NULL; |
|
385 } |
|
386 |
|
387 if( iCalSession ) |
|
388 { |
|
389 delete iCalSession; |
|
390 iCalSession = NULL; |
|
391 } |
|
392 } |
|
393 |
|
394 // ----------------------------------------------------------------------------- |
|
395 // CCalenImporterTest::SetImportModeTestL |
|
396 // SetImportMode |
|
397 // ----------------------------------------------------------------------------- |
|
398 // |
|
399 void CCalenImporterTest::SetImportModeTestL() |
|
400 { |
|
401 iCalenImporter->SetImportMode( ECalenImportModeNormal ); |
|
402 iCalenImporter->SetImportMode( ECalenImportModeExtended ); |
|
403 } |
|
404 |
|
405 // TEST TABLE |
|
406 EUNIT_BEGIN_TEST_TABLE( |
|
407 CCalenImporterTest, |
|
408 "Add test suite description here.", |
|
409 "MODULE" ) |
|
410 |
|
411 //Import and Export |
|
412 EUNIT_TEST( |
|
413 "Test a bunch of iCal and vCal", |
|
414 "CCalenImporter", |
|
415 "Add tested function name here", |
|
416 "FUNCTIONALITY", |
|
417 SetupL, ImportTestL, Teardown ) |
|
418 |
|
419 //Specific import test cases |
|
420 EUNIT_TEST( |
|
421 "Test setting the import mode", |
|
422 "CCalenImporter", |
|
423 "Add tested function name here", |
|
424 "FUNCTIONALITY", |
|
425 SetupImporterL, SetImportModeTestL, TeardownImporter ) |
|
426 |
|
427 EUNIT_END_TEST_TABLE |
|
428 |
|
429 // END OF FILE |