|
1 /* |
|
2 * Copyright (c) 2009 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 // CLASS HEADER |
|
19 #include <mt_apiaistrparser.h> |
|
20 |
|
21 // external includes |
|
22 #include <digia/eunit/eunitmacros.h> |
|
23 #include <utf.h> |
|
24 |
|
25 // internal includes |
|
26 #include <aistrparser.h> |
|
27 #include <aiutility.h> |
|
28 #include <aistrcnv.h> |
|
29 |
|
30 // CONSTANTS |
|
31 namespace |
|
32 { |
|
33 _LIT( KUnicode, "Text with unicode characters ä and \x20AC" ); |
|
34 _LIT8( KUtf8, "Text" ); |
|
35 } |
|
36 |
|
37 _LIT16( KHexPrefix16, "0x" ); |
|
38 |
|
39 TInt ParseInt |
|
40 ( TInt32& aValue, const TDesC16& aStringValue ) |
|
41 { |
|
42 const TInt pos = aStringValue.FindF( KHexPrefix16 ); |
|
43 if (pos != KErrNotFound) |
|
44 { |
|
45 TLex16 lex( aStringValue.Mid( pos + KHexPrefix16().Length() ) ); |
|
46 // Hex parsing needs unsigned int |
|
47 TUint32 value = 0; |
|
48 const TInt parseResult = lex.Val( value, EHex ); |
|
49 if ( parseResult == KErrNone ) |
|
50 { |
|
51 aValue = value; |
|
52 } |
|
53 return parseResult; |
|
54 } |
|
55 else |
|
56 { |
|
57 TLex16 lex( aStringValue ); |
|
58 return lex.Val(aValue); |
|
59 } |
|
60 } |
|
61 |
|
62 |
|
63 // CONSTRUCTION |
|
64 MT_ApiAiStrParser* MT_ApiAiStrParser::NewL() |
|
65 { |
|
66 MT_ApiAiStrParser* self = MT_ApiAiStrParser::NewLC(); |
|
67 CleanupStack::Pop(); |
|
68 |
|
69 return self; |
|
70 } |
|
71 |
|
72 MT_ApiAiStrParser* MT_ApiAiStrParser::NewLC() |
|
73 { |
|
74 MT_ApiAiStrParser* self = new( ELeave ) MT_ApiAiStrParser(); |
|
75 CleanupStack::PushL( self ); |
|
76 |
|
77 self->ConstructL(); |
|
78 |
|
79 return self; |
|
80 } |
|
81 |
|
82 // Destructor (virtual by CBase) |
|
83 MT_ApiAiStrParser::~MT_ApiAiStrParser() |
|
84 { |
|
85 } |
|
86 |
|
87 // Default constructor |
|
88 MT_ApiAiStrParser::MT_ApiAiStrParser() |
|
89 { |
|
90 } |
|
91 |
|
92 // Second phase construct |
|
93 void MT_ApiAiStrParser::ConstructL() |
|
94 { |
|
95 // The ConstructL from the base class CEUnitTestSuiteClass must be called. |
|
96 // It generates the test case table. |
|
97 CEUnitTestSuiteClass::ConstructL(); |
|
98 } |
|
99 |
|
100 // METHODS |
|
101 |
|
102 |
|
103 void MT_ApiAiStrParser::SetupL() |
|
104 { |
|
105 iStrParser = AiUtility::CreateStrParserL(); |
|
106 } |
|
107 |
|
108 void MT_ApiAiStrParser::SetupUtf8BufferL() |
|
109 { |
|
110 iStrParser = AiUtility::CreateStrParserL(); |
|
111 iUtf8Buffer = HBufC8::NewL(0); |
|
112 } |
|
113 |
|
114 void MT_ApiAiStrParser::SetupUnicodeBufferL() |
|
115 { |
|
116 iStrParser = AiUtility::CreateStrParserL(); |
|
117 iUtf8Buffer = HBufC8::NewL(0); |
|
118 } |
|
119 |
|
120 void MT_ApiAiStrParser::SetupHexIntL() |
|
121 { |
|
122 iStrParser = AiUtility::CreateStrParserL(); |
|
123 _LIT8( KTestValue, "0xbad" ); |
|
124 iUtf8Buffer = KTestValue().AllocL(); |
|
125 iTestValue = 0xbad; |
|
126 iTestResult = KErrNone; |
|
127 } |
|
128 |
|
129 void MT_ApiAiStrParser::SetupHexIntUnicodeL() |
|
130 { |
|
131 iStrParser = AiUtility::CreateStrParserL(); |
|
132 _LIT( KTestValue, "0xbad" ); |
|
133 iUnicodeBuffer = KTestValue().AllocL(); |
|
134 iTestValue = 0xbad; |
|
135 iTestResult = KErrNone; |
|
136 } |
|
137 |
|
138 void MT_ApiAiStrParser::SetupIntL() |
|
139 { |
|
140 iStrParser = AiUtility::CreateStrParserL(); |
|
141 _LIT8( KTestValue, "42" ); |
|
142 iUtf8Buffer = KTestValue().AllocL(); |
|
143 iTestValue = 42; |
|
144 iTestResult = KErrNone; |
|
145 } |
|
146 |
|
147 void MT_ApiAiStrParser::SetupIntUnicodeL() |
|
148 { |
|
149 iStrParser = AiUtility::CreateStrParserL(); |
|
150 _LIT( KTestValue, "42" ); |
|
151 iUnicodeBuffer = KTestValue().AllocL(); |
|
152 iTestValue = 42; |
|
153 iTestResult = KErrNone; |
|
154 } |
|
155 |
|
156 void MT_ApiAiStrParser::SetupNoIntL() |
|
157 { |
|
158 SetupUtf8BufferL(); |
|
159 iTestResult = KErrGeneral; |
|
160 } |
|
161 |
|
162 void MT_ApiAiStrParser::SetupNoIntUnicodeL() |
|
163 { |
|
164 iStrParser = AiUtility::CreateStrParserL(); |
|
165 iUnicodeBuffer = HBufC::NewL(0); |
|
166 iTestResult = KErrGeneral; |
|
167 } |
|
168 |
|
169 void MT_ApiAiStrParser::Teardown( ) |
|
170 { |
|
171 delete iUnicodeBuffer; |
|
172 iUnicodeBuffer = NULL; |
|
173 |
|
174 delete iUtf8Buffer; |
|
175 iUtf8Buffer = NULL; |
|
176 |
|
177 Release( iStrParser ); |
|
178 } |
|
179 |
|
180 void MT_ApiAiStrParser::TestCopyUnicodeToUnicodeL() |
|
181 { |
|
182 iUnicodeBuffer = iStrParser->CopyToBufferL(iUnicodeBuffer, KUnicode ); |
|
183 |
|
184 EUNIT_ASSERT( *iUnicodeBuffer == KUnicode ); |
|
185 } |
|
186 |
|
187 void MT_ApiAiStrParser::TestCopyUnicodeToUtf8L() |
|
188 { |
|
189 iUtf8Buffer = iStrParser->CopyToBufferL( iUtf8Buffer, KUnicode ); |
|
190 |
|
191 HBufC8* reference = CnvUtfConverter::ConvertFromUnicodeToUtf8L( KUnicode ); |
|
192 |
|
193 EUNIT_ASSERT_EQUALS( *iUtf8Buffer, *reference ); |
|
194 |
|
195 delete reference; |
|
196 } |
|
197 |
|
198 void MT_ApiAiStrParser::TestCopyUtf8ToUnicodeL() |
|
199 { |
|
200 iUnicodeBuffer = iStrParser->CopyToBufferL( iUnicodeBuffer, KUtf8 ); |
|
201 |
|
202 HBufC* reference = CnvUtfConverter::ConvertToUnicodeFromUtf8L( KUtf8 ); |
|
203 |
|
204 EUNIT_ASSERT_EQUALS( *iUnicodeBuffer, *reference ); |
|
205 |
|
206 delete reference; |
|
207 } |
|
208 |
|
209 void MT_ApiAiStrParser::TestCopyUtf8ToUtf8L() |
|
210 { |
|
211 iUtf8Buffer = iStrParser->CopyToBufferL( iUtf8Buffer, KUtf8 ); |
|
212 |
|
213 EUNIT_ASSERT_EQUALS( *iUtf8Buffer, KUtf8 ); |
|
214 } |
|
215 |
|
216 void MT_ApiAiStrParser::TestParseInt() |
|
217 { |
|
218 TInt32 value(0); |
|
219 |
|
220 TInt result = iStrParser->ParseInt( value, *iUtf8Buffer ); |
|
221 |
|
222 EUNIT_ASSERT_EQUALS( result, iTestResult ); |
|
223 |
|
224 if ( result == KErrNone ) |
|
225 { |
|
226 EUNIT_ASSERT_EQUALS( value, iTestValue ); |
|
227 } |
|
228 } |
|
229 |
|
230 void MT_ApiAiStrParser::TestParseIntUnicode() |
|
231 { |
|
232 TInt32 value(0); |
|
233 |
|
234 TInt result = ParseInt( value, *iUnicodeBuffer ); |
|
235 |
|
236 EUNIT_ASSERT_EQUALS( result, iTestResult ); |
|
237 |
|
238 if ( result == KErrNone ) |
|
239 { |
|
240 EUNIT_ASSERT_EQUALS( value, iTestValue ); |
|
241 } |
|
242 } |
|
243 |
|
244 void MT_ApiAiStrParser::TestEnsureBufMaxLengthL() |
|
245 { |
|
246 _LIT( KTestValue, "text1 text2 text3 " ); |
|
247 _LIT( KTestValue2, "text4" ); |
|
248 _LIT( KTestValue3, "text1 text2 text3 text4" ); |
|
249 |
|
250 HBufC* buffer = KTestValue().AllocL(); |
|
251 CleanupStack::PushL(buffer); |
|
252 TPtr ptr = AiUtility::EnsureBufMaxLengthL(buffer , 25 ); |
|
253 ptr.Append(KTestValue2); |
|
254 |
|
255 |
|
256 |
|
257 EUNIT_ASSERT(!ptr.Compare(KTestValue3)); |
|
258 |
|
259 CleanupStack::PopAndDestroy(buffer); |
|
260 |
|
261 } |
|
262 |
|
263 // TEST TABLE |
|
264 |
|
265 EUNIT_BEGIN_TEST_TABLE( |
|
266 MT_ApiAiStrParser, |
|
267 "Unit test suite for class XmlUiController utility functions", |
|
268 "UNIT" ) |
|
269 |
|
270 EUNIT_TEST( |
|
271 "Copy unicode to unicode, no buffer", |
|
272 "AiXmlUiController", |
|
273 "CopyToBufferL( HBufC16*, const TDesC16& )", |
|
274 "FUNCTIONALITY", |
|
275 SetupL, TestCopyUnicodeToUnicodeL, Teardown) |
|
276 |
|
277 EUNIT_TEST( |
|
278 "Copy unicode to unicode, reallocate buffer", |
|
279 "AiXmlUiController", |
|
280 "CopyToBufferL( HBufC16*, const TDesC16& )", |
|
281 "FUNCTIONALITY", |
|
282 SetupUnicodeBufferL, TestCopyUnicodeToUnicodeL, Teardown) |
|
283 |
|
284 EUNIT_TEST( |
|
285 "Copy UTF8 to unicode, no buffer", |
|
286 "AiXmlUiController", |
|
287 "CopyToBufferL( HBufC16*, const TDesC8& )", |
|
288 "FUNCTIONALITY", |
|
289 SetupL, TestCopyUtf8ToUnicodeL, Teardown) |
|
290 |
|
291 |
|
292 EUNIT_TEST( |
|
293 "Copy UTF8 to unicode, reallocate buffer", |
|
294 "AiXmlUiController", |
|
295 "CopyToBufferL( HBufC16*, const TDesC8& )", |
|
296 "FUNCTIONALITY", |
|
297 SetupUnicodeBufferL, TestCopyUtf8ToUnicodeL, Teardown) |
|
298 |
|
299 EUNIT_TEST( |
|
300 "Copy unicode to UTF8, no buffer", |
|
301 "AiXmlUiController", |
|
302 "CopyToBufferL( HBufC8*, const TDesC16& )", |
|
303 "FUNCTIONALITY", |
|
304 SetupL, TestCopyUnicodeToUtf8L, Teardown) |
|
305 |
|
306 EUNIT_TEST( |
|
307 "Copy unicode to UTF8, reallocate buffer", |
|
308 "AiXmlUiController", |
|
309 "CopyToBufferL( HBufC8*, const TDesC16& )", |
|
310 "FUNCTIONALITY", |
|
311 SetupUtf8BufferL, TestCopyUnicodeToUtf8L, Teardown) |
|
312 |
|
313 EUNIT_TEST( |
|
314 "Copy UTF8 to UTF8, no buffer", |
|
315 "AiXmlUiController", |
|
316 "CopyToBufferL( HBufC8*, const TDesC8& )", |
|
317 "FUNCTIONALITY", |
|
318 SetupL, TestCopyUtf8ToUtf8L, Teardown) |
|
319 |
|
320 EUNIT_TEST( |
|
321 "Copy UTF8 to UTF8, reallocate buffer", |
|
322 "AiXmlUiController", |
|
323 "CopyToBufferL( HBufC8*, const TDesC8& )", |
|
324 "FUNCTIONALITY", |
|
325 SetupUtf8BufferL, TestCopyUtf8ToUtf8L, Teardown) |
|
326 |
|
327 EUNIT_TEST( |
|
328 "Parse hex integer", |
|
329 "AiXmlUiController", |
|
330 "ParseInt", |
|
331 "FUNCTIONALITY", |
|
332 SetupHexIntL, TestParseInt, Teardown) |
|
333 |
|
334 EUNIT_TEST( |
|
335 "Parse decimal integer", |
|
336 "AiXmlUiController", |
|
337 "ParseInt", |
|
338 "FUNCTIONALITY", |
|
339 SetupIntL, TestParseInt, Teardown) |
|
340 |
|
341 EUNIT_TEST( |
|
342 "Parse invalid buffer", |
|
343 "AiXmlUiController", |
|
344 "ParseInt", |
|
345 "ERROR HANDLING", |
|
346 SetupNoIntL, TestParseInt, Teardown) |
|
347 |
|
348 EUNIT_TEST( |
|
349 "Parse UNICODE hex integer", |
|
350 "AiXmlUiController", |
|
351 "ParseInt", |
|
352 "FUNCTIONALITY", |
|
353 SetupHexIntUnicodeL, TestParseIntUnicode, Teardown) |
|
354 |
|
355 EUNIT_TEST( |
|
356 "Parse UNICODE decimal integer", |
|
357 "AiXmlUiController", |
|
358 "ParseInt", |
|
359 "FUNCTIONALITY", |
|
360 SetupIntUnicodeL, TestParseIntUnicode, Teardown) |
|
361 |
|
362 EUNIT_TEST( |
|
363 "Parse UNICODE invalid buffer", |
|
364 "AiXmlUiController", |
|
365 "ParseInt", |
|
366 "ERROR HANDLING", |
|
367 SetupNoIntUnicodeL, TestParseIntUnicode, Teardown) |
|
368 |
|
369 EUNIT_TEST( |
|
370 "Copy string to buffer", |
|
371 "AiXmlUiController", |
|
372 "EnsureBufMaxLengthL", |
|
373 "ERROR HANDLING", |
|
374 SetupL, TestEnsureBufMaxLengthL, Teardown) |
|
375 |
|
376 EUNIT_END_TEST_TABLE |
|
377 |
|
378 // END OF FILE |