|
1 // Copyright (c) 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 /** |
|
17 @file texmlstring.cpp |
|
18 @internalTechnology |
|
19 */ |
|
20 |
|
21 #include "texml2teststep.h" |
|
22 #include <xml/utils/xmlengxestrings.h> |
|
23 #include <string.h> |
|
24 #include <utf.h> |
|
25 #include <e32des16.h> |
|
26 |
|
27 /** |
|
28 * Class CXmlStringStep Implementation |
|
29 */ |
|
30 |
|
31 /** |
|
32 * Constructor. Sets the test step name so that it can be |
|
33 * differentiated from within doTestStepL() |
|
34 */ |
|
35 CXmlStringStep::CXmlStringStep(const TDesC& aStepName) |
|
36 { |
|
37 SetTestStepName(aStepName); |
|
38 } |
|
39 |
|
40 /** |
|
41 * TEF invokes doTestStepL interface along with the step name |
|
42 * to let appropriate action to be taken for the test step. |
|
43 */ |
|
44 TVerdict CXmlStringStep::doTestStepL(void) |
|
45 { |
|
46 if (TestStepName() == KXmlStringCopy) |
|
47 { |
|
48 INFO_PRINTF1(KXmlStringCopy); |
|
49 SetTestStepResult(TestKXmlStringCopy()); |
|
50 } |
|
51 else if (TestStepName() == KXmlStringSetStep1) |
|
52 { |
|
53 INFO_PRINTF1(KXmlStringSetStep1); |
|
54 SetTestStepResult(TestKXmlStringSetStep1()); |
|
55 } |
|
56 else if (TestStepName() == KXmlStringSetStep2) |
|
57 { |
|
58 INFO_PRINTF1(KXmlStringSetStep2); |
|
59 SetTestStepResult(TestKXmlStringSetStep2()); |
|
60 } |
|
61 else if (TestStepName() == KXmlStringAlloc1) |
|
62 { |
|
63 INFO_PRINTF1(KXmlStringAlloc1); |
|
64 SetTestStepResult(TestKXmlStringAlloc1()); |
|
65 } |
|
66 else if (TestStepName() == KXmlStringAlloc2) |
|
67 { |
|
68 INFO_PRINTF1(KXmlStringAlloc2); |
|
69 SetTestStepResult(TestKXmlStringAlloc2()); |
|
70 } |
|
71 else if (TestStepName() == KXmlStringAllocAndFree1) |
|
72 { |
|
73 INFO_PRINTF1(KXmlStringAllocAndFree1); |
|
74 SetTestStepResult (TestKXmlStringAllocAndFree1()); |
|
75 } |
|
76 else if (TestStepName() == KXmlStringAllocAndFree2) |
|
77 { |
|
78 INFO_PRINTF1(KXmlStringAllocAndFree2); |
|
79 SetTestStepResult(TestKXmlStringAllocAndFree2()); |
|
80 } |
|
81 else if (TestStepName() == KXmlStringAppend) |
|
82 { |
|
83 INFO_PRINTF1(KXmlStringAppend); |
|
84 SetTestStepResult (TestKXmlStringAppend()); |
|
85 } |
|
86 else if (TestStepName() == KXmlStringCompare) |
|
87 { |
|
88 INFO_PRINTF1(KXmlStringCompare); |
|
89 SetTestStepResult(TestKXmlStringCompare()); |
|
90 } |
|
91 return TestStepResult(); |
|
92 } |
|
93 /** |
|
94 * Tests the APIs Copy(), Free(), PushL(), Set() |
|
95 */ |
|
96 TVerdict CXmlStringStep::TestKXmlStringCopy() |
|
97 { |
|
98 char *string1 = new char[34]; |
|
99 const char *compareString2 = "<SampleTag>TestString</SampleTag>"; |
|
100 strcpy(string1, "<SampleTag>TestString</SampleTag>"); |
|
101 TXmlEngString *testString = new (ELeave) TXmlEngString(string1); |
|
102 testString->PushL(); |
|
103 char* resultString = testString->CopyL(); |
|
104 if (0 != strcmp(resultString, compareString2)) |
|
105 { |
|
106 return EFail; |
|
107 } |
|
108 CleanupStack::Pop(); |
|
109 User::Free(resultString); |
|
110 resultString = 0; |
|
111 |
|
112 //Test Set(TXmlEngString& src) |
|
113 TXmlEngString *testString2 = new (ELeave) TXmlEngString(); |
|
114 testString2->Set(*testString); |
|
115 resultString = testString2->CopyL(); |
|
116 char* resultString2 = testString->CopyL(); |
|
117 //Check if the testString2 object has the same string |
|
118 //and testString object's string is empty. |
|
119 if (0 != strcmp(resultString, compareString2) && 0 != resultString2) |
|
120 { |
|
121 return EFail; |
|
122 } |
|
123 User::Free(resultString); |
|
124 resultString = 0; |
|
125 //Test if the same object is passed to Set(), it does not delete the |
|
126 //existing string inside. |
|
127 testString2->Set(*testString2); //both have same string inside |
|
128 resultString = testString2->CopyL(); |
|
129 if (0 != strcmp(resultString, compareString2)) |
|
130 { |
|
131 return EFail; |
|
132 } |
|
133 |
|
134 char *singleChar = new char[2]; |
|
135 strcpy(singleChar, "V\0"); |
|
136 testString->Set(singleChar); |
|
137 testString2->Set(*testString); |
|
138 resultString = testString2->CopyL(); |
|
139 if (0 != strcmp(resultString, "V")) |
|
140 { |
|
141 return EFail; |
|
142 } |
|
143 //Free the object |
|
144 testString->Free(); |
|
145 return EPass; |
|
146 } |
|
147 /** |
|
148 * Tests TXmlEngString::SetL(const TDesC8& aDes8) |
|
149 */ |
|
150 TVerdict CXmlStringStep::TestKXmlStringSetStep1() |
|
151 { |
|
152 _LIT(KText, "<TestData>SomeData</TestData>"); |
|
153 const char *compareString = "<TestData>SomeData</TestData>"; |
|
154 TBufC<30> buf(KText); |
|
155 TPtrC desc = buf.Des(); |
|
156 TBuf8<30> buf8; |
|
157 CnvUtfConverter::ConvertFromUnicodeToUtf8(buf8, desc); |
|
158 |
|
159 char *string1 = new char[34]; |
|
160 strcpy(string1, "<SampleTag>TestString</SampleTag>"); |
|
161 TXmlEngString *testString = new (ELeave) TXmlEngString(string1); |
|
162 |
|
163 //now set the new value for testString |
|
164 testString->SetL(buf8); |
|
165 char* resultString = testString->CopyL(); |
|
166 if ( 0 != strcmp(resultString, compareString)) |
|
167 { |
|
168 return EFail; |
|
169 } |
|
170 User::Free(resultString); |
|
171 resultString = 0; |
|
172 //now try setting value to empty TXmlEngString object |
|
173 TXmlEngString *testString2 = new (ELeave) TXmlEngString(); |
|
174 testString2->SetL(buf8); |
|
175 resultString = testString2->CopyL(); |
|
176 if ( 0 != strcmp(resultString, compareString)) |
|
177 { |
|
178 return EFail; |
|
179 } |
|
180 return EPass; |
|
181 } |
|
182 |
|
183 /** |
|
184 * Tests TXmlEngString::SetL(const TDesC8& aDes8) |
|
185 */ |
|
186 TVerdict CXmlStringStep::TestKXmlStringSetStep2() |
|
187 { |
|
188 _LIT(KText, "<TestData>SomeData</TestData>"); |
|
189 const char *compareString = "<TestData>SomeData</TestData>"; |
|
190 TBufC<30> buf(KText); |
|
191 TPtrC desc = buf.Des(); |
|
192 char *string1 = new char[34]; |
|
193 strcpy(string1, "<SampleTag>TestString</SampleTag>"); |
|
194 TXmlEngString *testString = new (ELeave) TXmlEngString(string1); |
|
195 |
|
196 //now set the new value for testString |
|
197 testString->SetL(buf); |
|
198 char* resultString = testString->CopyL(); |
|
199 if ( 0 == testString->Length() || 0 != strcmp(resultString, compareString)) |
|
200 { |
|
201 return EFail; |
|
202 } |
|
203 User::Free(resultString); |
|
204 resultString = 0; |
|
205 //now try setting value to empty TXmlEngString object |
|
206 TXmlEngString *testString2 = new (ELeave) TXmlEngString(); |
|
207 resultString = testString2->CopyL(); |
|
208 if (testString2->Length() != 0 && resultString != 0) |
|
209 { |
|
210 return EFail; |
|
211 } |
|
212 testString2->SetL(buf); |
|
213 resultString = testString2->CopyL(); |
|
214 if ( 0 != strcmp(resultString, compareString)) |
|
215 { |
|
216 return EFail; |
|
217 } |
|
218 return EPass; |
|
219 } |
|
220 |
|
221 /** |
|
222 * Tests AppendL(), Size(), Copy() |
|
223 */ |
|
224 TVerdict CXmlStringStep::TestKXmlStringAppend() |
|
225 { |
|
226 char *string2 = new char[34]; |
|
227 const char *compareString = "<SampleTag>TestString</SampleTag>"; |
|
228 const char *compareString2 = |
|
229 "<SampleTag>TestString</SampleTag><SampleTag>TestString</SampleTag>"; |
|
230 strcpy(string2, "<SampleTag>TestString</SampleTag>"); |
|
231 |
|
232 //empty testString to start with |
|
233 TXmlEngString *testString = new (ELeave) TXmlEngString(); |
|
234 //without string |
|
235 TXmlEngString *appendString1 = new (ELeave) TXmlEngString(); |
|
236 //with string |
|
237 TXmlEngString *appendString2 = new (ELeave) TXmlEngString(string2); |
|
238 |
|
239 testString->AppendL(*appendString1, *appendString2); |
|
240 char* resultString = testString->CopyL(); |
|
241 TInt len = appendString1->Size(); |
|
242 if (len) |
|
243 { |
|
244 return EFail; |
|
245 } |
|
246 if ( 0 != strcmp(resultString, compareString)) |
|
247 { |
|
248 return EFail; |
|
249 } |
|
250 User::Free(resultString); |
|
251 resultString = 0; |
|
252 |
|
253 appendString2->AppendL(*testString); |
|
254 resultString = appendString2->CopyL(); |
|
255 if ( 0 != strcmp(compareString2, resultString)) |
|
256 { |
|
257 return EFail; |
|
258 } |
|
259 return EPass; |
|
260 } |
|
261 |
|
262 /** |
|
263 * Tests TXmlEngString::AllocL |
|
264 */ |
|
265 TVerdict CXmlStringStep::TestKXmlStringAlloc1() |
|
266 { |
|
267 char *string = new char[34]; |
|
268 strcpy(string, "<SampleTag>TestString</SampleTag>"); |
|
269 //empty string |
|
270 TXmlEngString *testString = new (ELeave) TXmlEngString(); |
|
271 HBufC* resBuf = testString->AllocL(); |
|
272 |
|
273 if (resBuf->Size()) |
|
274 { |
|
275 return EFail; |
|
276 } |
|
277 TXmlEngString *testString2 = new (ELeave) TXmlEngString(string); |
|
278 resBuf = testString2->AllocL(); |
|
279 _LIT(KText, "<SampleTag>TestString</SampleTag>"); |
|
280 TBufC<33> buf(KText); |
|
281 |
|
282 if (resBuf->Compare(buf.Des())) |
|
283 { |
|
284 return EFail; |
|
285 } |
|
286 return EPass; |
|
287 } |
|
288 |
|
289 /** |
|
290 * Tests TXmlEngString::AllocLC |
|
291 */ |
|
292 TVerdict CXmlStringStep::TestKXmlStringAlloc2() |
|
293 { |
|
294 char *string = new char[34]; |
|
295 strcpy(string, "<SampleTag>TestString</SampleTag>"); |
|
296 //empty string |
|
297 TXmlEngString *testString = new (ELeave) TXmlEngString(); |
|
298 HBufC* resBuf = testString->AllocLC(); |
|
299 TInt compare = resBuf->Size(); |
|
300 CleanupStack::PopAndDestroy(); |
|
301 if (compare) |
|
302 { |
|
303 return EFail; |
|
304 } |
|
305 |
|
306 TXmlEngString *testString2 = new (ELeave) TXmlEngString(string); |
|
307 resBuf = testString2->AllocLC(); |
|
308 _LIT(KText, "<SampleTag>TestString</SampleTag>"); |
|
309 TBufC<33> buf(KText); |
|
310 compare = resBuf->Compare(buf.Des()); |
|
311 CleanupStack::PopAndDestroy(); |
|
312 if (compare) |
|
313 { |
|
314 return EFail; |
|
315 } |
|
316 return EPass; |
|
317 } |
|
318 /** |
|
319 * Tests TXmlEngString::AllocAndFreeL |
|
320 */ |
|
321 TVerdict CXmlStringStep::TestKXmlStringAllocAndFree1() |
|
322 { |
|
323 char *string = new char[34]; |
|
324 strcpy(string, "<SampleTag>TestString</SampleTag>"); |
|
325 //empty string |
|
326 TXmlEngString *testString = new (ELeave) TXmlEngString(); |
|
327 HBufC* resBuf = testString->AllocAndFreeL(); |
|
328 |
|
329 if (resBuf->Size()) |
|
330 { |
|
331 return EFail; |
|
332 } |
|
333 TXmlEngString *testString2 = new (ELeave) TXmlEngString(string); |
|
334 resBuf = testString2->AllocAndFreeL(); |
|
335 _LIT(KText, "<SampleTag>TestString</SampleTag>"); |
|
336 TBufC<33> buf(KText); |
|
337 |
|
338 if (resBuf->Compare(buf.Des())) |
|
339 { |
|
340 return EFail; |
|
341 } |
|
342 return EPass; |
|
343 } |
|
344 |
|
345 /** |
|
346 * Tests TXmlEngString::AllocAndFreeLC |
|
347 */ |
|
348 TVerdict CXmlStringStep::TestKXmlStringAllocAndFree2() |
|
349 { |
|
350 char *string = new char[34]; |
|
351 strcpy(string, "<SampleTag>TestString</SampleTag>"); |
|
352 //empty string |
|
353 TXmlEngString *testString = new (ELeave) TXmlEngString(); |
|
354 HBufC* resBuf = testString->AllocAndFreeLC(); |
|
355 TInt compare = resBuf->Size(); |
|
356 CleanupStack::PopAndDestroy(); |
|
357 if (compare) |
|
358 { |
|
359 return EFail; |
|
360 } |
|
361 |
|
362 TXmlEngString *testString2 = new (ELeave) TXmlEngString(string); |
|
363 resBuf = testString2->AllocAndFreeLC(); |
|
364 _LIT(KText, "<SampleTag>TestString</SampleTag>"); |
|
365 TBufC<33> buf(KText); |
|
366 compare = resBuf->Compare(buf.Des()); |
|
367 CleanupStack::PopAndDestroy(); |
|
368 if (compare) |
|
369 { |
|
370 return EFail; |
|
371 } |
|
372 return EPass; |
|
373 } |
|
374 |
|
375 /** |
|
376 * Tests TXmlEngString::Compare |
|
377 */ |
|
378 TVerdict CXmlStringStep::TestKXmlStringCompare() |
|
379 { |
|
380 char *string = new char[12]; |
|
381 strcpy(string, "<SampleTag>"); |
|
382 TXmlEngString *testString1 = new (ELeave) TXmlEngString(); |
|
383 TXmlEngString *testString2 = new (ELeave) TXmlEngString(); |
|
384 |
|
385 //both strings empty |
|
386 if (testString1->Compare(*testString2)) |
|
387 { |
|
388 return EFail; |
|
389 } |
|
390 _LIT(KText, "<TestData>SomeData</TestData>"); |
|
391 TBufC<30> buf(KText); |
|
392 TPtrC desc = buf.Des(); |
|
393 testString1->SetL(desc); |
|
394 //testString1 has <TestData>SomeData</TestData>, testString2 is empty |
|
395 if (!testString1->Compare(*testString2)) |
|
396 { |
|
397 return EFail; |
|
398 } |
|
399 char *empty = new char[1]; |
|
400 empty[0] = '\0'; |
|
401 //testString1 is set to \0, testString2 is empty |
|
402 testString1->Set(empty); |
|
403 if (testString2->Compare(*testString1)) |
|
404 { |
|
405 return EFail; |
|
406 } |
|
407 if (testString1->Compare(*testString2)) |
|
408 { |
|
409 return EFail; |
|
410 } |
|
411 |
|
412 testString2->Free(); |
|
413 char *singleChar = new char[2]; |
|
414 strcpy(singleChar, "V\0"); |
|
415 testString1->Set(singleChar); |
|
416 if (!testString2->Compare(*testString1)) |
|
417 { |
|
418 return EFail; |
|
419 } |
|
420 |
|
421 char *string1 = new char[13]; |
|
422 strcpy(string1, "<TestString>"); |
|
423 char *string2 = new char[13]; |
|
424 strcpy(string2, "<TestString>"); |
|
425 testString1->Set(string1); |
|
426 testString2->Set(string2); |
|
427 if(testString1->Compare(*testString2)) |
|
428 { |
|
429 return EFail; |
|
430 } |
|
431 return EPass; |
|
432 } |