|
1 // Copyright (c) 1997-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 // CHtmlToCrtConverter test code |
|
15 // (1)convert path |
|
16 // (2)convert single file synchronously/asynchronously |
|
17 // (3)synchronous/asynchronous oom test |
|
18 // the resulting richtext is written to the clipboard |
|
19 // |
|
20 // |
|
21 |
|
22 #include <e32std.h> |
|
23 #include <e32test.h> |
|
24 #include <f32file.h> |
|
25 #include <e32math.h> |
|
26 |
|
27 #include <confndr.h> |
|
28 #include <conlist.h> |
|
29 #include <concnf.h> |
|
30 #include "CHtmlToCrtConverter.h" |
|
31 #include "CHtmlToCrtConvActive.h" |
|
32 |
|
33 |
|
34 _LIT(KTestName,"CHtmlToCrtConverter"); |
|
35 _LIT(KTest1,"@SYMTestCaseID PIM-CONVERTERTEST-0001 CHtmlToCrtConverter test"); |
|
36 |
|
37 LOCAL_D RTest test(KTestName); |
|
38 |
|
39 //================================================================================ |
|
40 //asynchronousL |
|
41 //tests asynchronous conversion |
|
42 //================================================================================ |
|
43 void asynchronousL(TDesC& aSourceFile, TDesC& aTargetFile) |
|
44 { |
|
45 CConversionObserver* observer=CConversionObserver::NewLC(); |
|
46 CTestScheduler* scheduler = new(ELeave) CTestScheduler; |
|
47 CleanupStack::PushL(scheduler); |
|
48 CActiveScheduler::Install(scheduler); |
|
49 CActiveConv* active = CActiveConv::NewL(aSourceFile, aTargetFile, observer); |
|
50 CleanupStack::PushL(active); |
|
51 active->StartConversionL(); |
|
52 TInt error = scheduler->GetError(); |
|
53 User::LeaveIfError(error); |
|
54 |
|
55 CleanupStack::PopAndDestroy(active); |
|
56 CleanupStack::PopAndDestroy(scheduler); |
|
57 CleanupStack::PopAndDestroy(observer); |
|
58 |
|
59 _LIT(KString,"%S"); |
|
60 test.Printf(KString,&(aSourceFile)); |
|
61 test.Printf(_L(" - asynchronous conversion\n")); |
|
62 } |
|
63 //================================================================================ |
|
64 //oomAsynchronousL |
|
65 //================================================================================ |
|
66 void oomAsynchronousL(TDesC& aSourceFile, TDesC& aTargetFile) |
|
67 { |
|
68 test.Printf(_L("starting OOM test\n")); |
|
69 |
|
70 //reserve space on cleanup stack |
|
71 for(TInt i=0; i<1000; i++) |
|
72 { |
|
73 CleanupStack::PushL(&i); |
|
74 } |
|
75 CleanupStack::Pop(1000); |
|
76 |
|
77 TInt ret=KErrNoMemory; |
|
78 TInt failAt=0; |
|
79 while (ret!=KErrNone) |
|
80 { |
|
81 failAt++; |
|
82 __UHEAP_SETFAIL(RHeap::EDeterministic,failAt); |
|
83 __UHEAP_MARK; |
|
84 TRAP(ret, asynchronousL(aSourceFile, aTargetFile)); |
|
85 if (ret!=KErrNone) |
|
86 { |
|
87 __UHEAP_MARKEND; |
|
88 } |
|
89 __UHEAP_RESET; |
|
90 test(ret==KErrNoMemory||ret==KErrNone); |
|
91 test.Printf(_L(".")); |
|
92 } |
|
93 |
|
94 test.Printf(_L("asynchronous OOM test complete\n")); |
|
95 } |
|
96 //================================================================================ |
|
97 //synchronousL |
|
98 //tests synchronous conversion |
|
99 //================================================================================ |
|
100 void synchronousL(TDesC& aSourceFile, TDesC& aTargetFile) |
|
101 { |
|
102 //get converter |
|
103 CCnaConverterList* convList=CCnaConverterList::NewLC(); |
|
104 const TUid KUidCHtmlToCrtConverter = {0x1000a90e}; |
|
105 CConverterBase* conv = convList->NewConverterL(KUidCHtmlToCrtConverter); |
|
106 CleanupStack::PushL(conv); |
|
107 if(conv) |
|
108 { |
|
109 //do conversion |
|
110 conv->ConvertL(aSourceFile, aTargetFile); |
|
111 } |
|
112 CleanupStack::PopAndDestroy(conv); |
|
113 convList->Release(); |
|
114 CleanupStack::PopAndDestroy(convList); |
|
115 } |
|
116 //================================================================================ |
|
117 //oomSynchronousL |
|
118 //================================================================================ |
|
119 void oomSynchronousL(TDesC& aSourceFile, TDesC& aTargetFile) |
|
120 { |
|
121 test.Printf(_L("starting OOM test\n")); |
|
122 |
|
123 //reserve space on cleanup stack |
|
124 for(TInt i=0; i<1000; i++) |
|
125 { |
|
126 CleanupStack::PushL(&i); |
|
127 } |
|
128 CleanupStack::Pop(1000); |
|
129 |
|
130 TInt ret=KErrNoMemory; |
|
131 TInt failAt=0; |
|
132 while (ret!=KErrNone) |
|
133 { |
|
134 failAt++; |
|
135 __UHEAP_SETFAIL(RHeap::EDeterministic,failAt); |
|
136 __UHEAP_MARK; |
|
137 TRAP(ret, synchronousL(aSourceFile, aTargetFile)); |
|
138 if (ret!=KErrNone) |
|
139 { |
|
140 __UHEAP_MARKEND; |
|
141 } |
|
142 __UHEAP_RESET; |
|
143 test(ret==KErrNoMemory||ret==KErrNone); |
|
144 test.Printf(_L(".")); |
|
145 } |
|
146 |
|
147 test.Printf(_L("\nsynchronous OOM test complete\n")); |
|
148 } |
|
149 //================================================================================ |
|
150 //getPathL |
|
151 //================================================================================ |
|
152 TBool getPathL(TDes& aPath, RFs& aFs) |
|
153 { |
|
154 // Generate the folder spec to search in |
|
155 TFileName searchSpec; |
|
156 TChar driveLetter = 'C'; |
|
157 searchSpec.Append(driveLetter); |
|
158 searchSpec.Append(aPath); |
|
159 |
|
160 CDir* entryList = NULL; |
|
161 TFindFile finder(aFs); |
|
162 TInt ret = finder.FindWildByPath(searchSpec, NULL, entryList); |
|
163 if(ret<KErrNone) |
|
164 { |
|
165 //try z drive |
|
166 driveLetter = 'Z'; |
|
167 searchSpec.Zero(); |
|
168 searchSpec.Append(driveLetter); |
|
169 searchSpec.Append(aPath); |
|
170 delete entryList; |
|
171 entryList = NULL; |
|
172 |
|
173 ret = finder.FindWildByPath(searchSpec, NULL, entryList); |
|
174 if (ret < KErrNone) |
|
175 { |
|
176 //path not found |
|
177 delete entryList; |
|
178 test.Printf(_L("files not found\n")); |
|
179 return EFalse; |
|
180 } |
|
181 } |
|
182 |
|
183 delete entryList; |
|
184 //set path |
|
185 aPath.Copy(searchSpec); |
|
186 return ETrue; |
|
187 } |
|
188 //================================================================================ |
|
189 //convertPathL |
|
190 //================================================================================ |
|
191 void convertPathL(TDesC& aPath, TDesC& aPathName, RFs& aFs, TDesC& aTargetFile) |
|
192 { |
|
193 aFs.MkDirAll(aPathName); |
|
194 |
|
195 CDir* entryList = NULL; |
|
196 TInt error = aFs.GetDir(aPath,KEntryAttMatchMask,ESortByName,entryList); |
|
197 User::LeaveIfError(error); |
|
198 |
|
199 TInt numberOfFiles = entryList->Count(); |
|
200 for (TInt i=0; i<numberOfFiles; i++) |
|
201 { |
|
202 //get the source file |
|
203 HBufC* temp=HBufC::NewLC(((*entryList)[i].iName).Length()); |
|
204 TPtr sourceFileName(temp->Des()); |
|
205 sourceFileName.Copy((*entryList)[i].iName); |
|
206 |
|
207 HBufC* temp2=HBufC::NewLC(((*entryList)[i].iName).Length()+aPathName.Length()); |
|
208 TPtr sourceFile(temp2->Des()); |
|
209 sourceFile = aPathName; |
|
210 sourceFile.Append(sourceFileName); |
|
211 //do the conversion |
|
212 synchronousL(sourceFile, aTargetFile); |
|
213 //output result |
|
214 _LIT(KString,"%S"); |
|
215 test.Printf(KString,&(sourceFileName)); |
|
216 test.Printf(_L("\n")); |
|
217 CleanupStack::PopAndDestroy(2);//temp, temp2 |
|
218 } |
|
219 |
|
220 delete entryList; |
|
221 test.Printf(_L("\n%d files converted\n"),numberOfFiles); |
|
222 } |
|
223 //================================================================================ |
|
224 //================================================================================ |
|
225 |
|
226 /** |
|
227 @SYMTestCaseID PIM-CONVERTERTEST-0001 |
|
228 */ |
|
229 void doMainL() |
|
230 { |
|
231 test.Start(KTest1); |
|
232 |
|
233 RFs fs; |
|
234 User::LeaveIfError(fs.Connect()); |
|
235 CleanupClosePushL(fs); |
|
236 TBufC<11> targetFile(_L("target file")); |
|
237 |
|
238 //======================================================== |
|
239 //path |
|
240 TBuf<17> path(_L(":\\testHtml\\*.*")); |
|
241 //single file |
|
242 TBuf<25> sourceFile; |
|
243 _LIT(KFileName, "test1.txt"); |
|
244 //======================================================== |
|
245 if(getPathL(path, fs)) |
|
246 { |
|
247 //CONVERT PATH |
|
248 TBuf<17> pathName(path); |
|
249 pathName.SetLength(pathName.Length()-3); |
|
250 |
|
251 convertPathL(path, pathName, fs, targetFile); |
|
252 |
|
253 //CONVERT SINGLE FILE |
|
254 |
|
255 sourceFile.Copy(pathName); |
|
256 sourceFile.Append(KFileName); |
|
257 |
|
258 synchronousL(sourceFile, targetFile); |
|
259 // asynchronousL(sourceFile, targetFile); |
|
260 |
|
261 //OOM TESTS |
|
262 // oomSynchronousL(sourceFile, targetFile); |
|
263 // oomAsynchronousL(sourceFile, targetFile); |
|
264 } |
|
265 //======================================================== |
|
266 |
|
267 CleanupStack::PopAndDestroy(); // fs |
|
268 |
|
269 REComSession::FinalClose(); //needed, otherwise you will get a memory leak |
|
270 |
|
271 test.End(); |
|
272 test.Close(); |
|
273 } |
|
274 |
|
275 GLDEF_C TInt E32Main() |
|
276 { |
|
277 __UHEAP_MARK; |
|
278 |
|
279 CTrapCleanup* theCleanup=CTrapCleanup::New(); |
|
280 TRAPD(ret,doMainL()); |
|
281 test(ret==KErrNone); |
|
282 delete theCleanup; |
|
283 |
|
284 __UHEAP_MARKEND; |
|
285 return(KErrNone); |
|
286 } |