Add copy of .ttf font with .eot extension for testing
[wine-gecko.git] / xpcom / tests / nsIFileTest.cpp
blobab9c5f35604b6ac3abef37ef579f35ed50551680
1 #include "nsILocalFile.h"
2 #include "nsStringGlue.h"
4 #include <stdio.h>
5 #include "nsXPCOM.h"
6 #include "nsIComponentManager.h"
7 #include "nsIComponentRegistrar.h"
8 #include "nsIServiceManager.h"
9 #include "nsIMemory.h"
11 #include "nsComponentManagerUtils.h"
12 #include "nsCOMPtr.h"
14 void Passed();
15 void Failed(const char* explanation = nsnull);
16 void Inspect();
17 void Banner(const char* bannerString);
19 void VerifyResult(nsresult rv)
21 if (NS_FAILED(rv))
23 Failed("rv failed");
24 printf("rv = %d\n", rv);
27 //----------------------------------------------------------------------------
28 void Banner(const char* bannerString)
29 //----------------------------------------------------------------------------
31 printf("---------------------------\n");
32 printf("%s\n", bannerString);
33 printf("---------------------------\n");
36 //----------------------------------------------------------------------------
37 void Passed()
38 //----------------------------------------------------------------------------
40 printf("Test passed.");
43 //----------------------------------------------------------------------------
44 void Failed(const char* explanation)
45 //----------------------------------------------------------------------------
47 printf("ERROR : Test failed.\n");
48 printf("REASON: %s.\n", explanation);
51 //----------------------------------------------------------------------------
52 void Inspect()
53 //----------------------------------------------------------------------------
55 printf("^^^^^^^^^^ PLEASE INSPECT OUTPUT FOR ERRORS\n");
58 void GetPaths(nsILocalFile* file)
60 nsresult rv;
61 nsCAutoString pathName;
63 printf("Getting Path\n");
65 rv = file->GetNativePath(pathName);
66 VerifyResult(rv);
68 printf("filepath: %s\n", pathName.get());
71 void InitTest(const char* creationPath, const char* appendPath)
73 nsILocalFile* file = nsnull;
74 nsresult rv = CallCreateInstance(NS_LOCAL_FILE_CONTRACTID, &file);
76 if (NS_FAILED(rv) || (!file))
78 printf("create nsILocalFile failed\n");
79 return;
82 nsCAutoString leafName;
84 Banner("InitWithPath");
85 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
87 rv = file->InitWithNativePath(nsDependentCString(creationPath));
88 VerifyResult(rv);
90 printf("Getting Filename\n");
91 rv = file->GetNativeLeafName(leafName);
92 printf(" %s\n", leafName.get());
93 VerifyResult(rv);
95 printf("Appending %s \n", appendPath);
96 rv = file->AppendNative(nsDependentCString(appendPath));
97 VerifyResult(rv);
99 printf("Getting Filename\n");
100 rv = file->GetNativeLeafName(leafName);
101 printf(" %s\n", leafName.get());
102 VerifyResult(rv);
104 GetPaths(file);
107 printf("Check For Existence\n");
109 PRBool exists;
110 file->Exists(&exists);
112 if (exists)
113 printf("Yup!\n");
114 else
115 printf("no.\n");
119 void CreationTest(const char* creationPath, const char* appendPath,
120 PRInt32 whatToCreate, PRInt32 perm)
122 nsresult rv;
123 nsCOMPtr<nsILocalFile> file =
124 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
126 if (NS_FAILED(rv) || (!file))
128 printf("create nsILocalFile failed\n");
129 return;
132 Banner("Creation Test");
133 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
135 rv = file->InitWithNativePath(nsDependentCString(creationPath));
136 VerifyResult(rv);
138 printf("Appending %s\n", appendPath);
139 rv = file->AppendRelativeNativePath(nsDependentCString(appendPath));
140 VerifyResult(rv);
142 printf("Check For Existence\n");
144 PRBool exists;
145 file->Exists(&exists);
147 if (exists)
148 printf("Yup!\n");
149 else
150 printf("no.\n");
153 rv = file->Create(whatToCreate, perm);
154 VerifyResult(rv);
156 rv = file->Exists(&exists);
157 VerifyResult(rv);
160 if (!exists)
162 Failed("Did not create file system object!");
163 return;
168 void CreateUniqueTest(const char* creationPath, const char* appendPath,
169 PRInt32 whatToCreate, PRInt32 perm)
171 nsresult rv;
172 nsCOMPtr<nsILocalFile> file =
173 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
175 if (NS_FAILED(rv) || (!file))
177 printf("create nsILocalFile failed\n");
178 return;
181 Banner("Creation Test");
182 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
184 rv = file->InitWithNativePath(nsDependentCString(creationPath));
185 VerifyResult(rv);
187 printf("Appending %s\n", appendPath);
188 rv = file->AppendNative(nsDependentCString(appendPath));
189 VerifyResult(rv);
191 printf("Check For Existence\n");
193 PRBool exists;
194 file->Exists(&exists);
196 if (exists)
197 printf("Yup!\n");
198 else
199 printf("no.\n");
202 rv = file->CreateUnique(whatToCreate, perm);
203 VerifyResult(rv);
205 rv = file->Exists(&exists);
206 VerifyResult(rv);
209 if (!exists)
211 Failed("Did not create file system object!");
212 return;
218 void
219 CopyTest(const char *testFile, const char *targetDir)
221 printf("start copy test\n");
223 nsresult rv;
224 nsCOMPtr<nsILocalFile> file =
225 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
227 if (NS_FAILED(rv) || (!file))
229 printf("create nsILocalFile failed\n");
230 return;
233 rv = file->InitWithNativePath(nsDependentCString(testFile));
234 VerifyResult(rv);
236 nsCOMPtr<nsILocalFile> dir =
237 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
239 if (NS_FAILED(rv) || (!dir))
241 printf("create nsILocalFile failed\n");
242 return;
245 rv = dir->InitWithNativePath(nsDependentCString(targetDir));
246 VerifyResult(rv);
248 rv = file->CopyTo(dir, EmptyString());
249 VerifyResult(rv);
251 printf("end copy test\n");
254 void
255 DeletionTest(const char* creationPath, const char* appendPath, PRBool recursive)
257 nsresult rv;
258 nsCOMPtr<nsILocalFile> file =
259 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
261 if (NS_FAILED(rv) || (!file))
263 printf("create nsILocalFile failed\n");
264 return;
267 Banner("Deletion Test");
268 printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
270 rv = file->InitWithNativePath(nsDependentCString(creationPath));
271 VerifyResult(rv);
273 printf("Appending %s\n", appendPath);
274 rv = file->AppendNative(nsDependentCString(appendPath));
275 VerifyResult(rv);
277 printf("Check For Existance\n");
279 PRBool exists;
280 file->Exists(&exists);
282 if (exists)
283 printf("Yup!\n");
284 else
285 printf("no.\n");
287 rv = file->Remove(recursive);
288 VerifyResult(rv);
290 rv = file->Exists(&exists);
291 VerifyResult(rv);
293 if (exists)
295 Failed("Did not create delete system object!");
296 return;
301 void
302 MoveTest(const char *testFile, const char *targetDir)
304 Banner("Move Test");
306 printf("start move test\n");
308 nsresult rv;
309 nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
311 if (!file)
313 printf("create nsILocalFile failed\n");
314 return;
317 rv = file->InitWithNativePath(nsDependentCString(testFile));
318 VerifyResult(rv);
320 nsCOMPtr<nsILocalFile> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
322 if (!dir)
324 printf("create nsILocalFile failed\n");
325 return;
328 rv = dir->InitWithNativePath(nsDependentCString(targetDir));
329 VerifyResult(rv);
331 rv = file->MoveToNative(dir, NS_LITERAL_CSTRING("newtemp"));
332 VerifyResult(rv);
333 if (NS_FAILED(rv))
335 printf("MoveToNative() test Failed.\n");
337 printf("end move test\n");
340 // move up the number of directories in moveUpCount, then append "foo/bar"
341 void
342 NormalizeTest(const char *testPath, int moveUpCount,
343 const char *expected)
345 Banner("Normalize Test");
347 nsresult rv;
348 nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
350 if (!file)
352 printf("create nsILocalFile failed\n");
353 return;
356 rv = file->InitWithNativePath(nsDependentCString(testPath));
357 VerifyResult(rv);
359 nsCOMPtr<nsIFile> parent;
360 nsAutoString path;
361 for (int i=0; i < moveUpCount; i++)
363 rv = file->GetParent(getter_AddRefs(parent));
364 VerifyResult(rv);
365 rv = parent->GetPath(path);
366 VerifyResult(rv);
367 rv = file->InitWithPath(path);
368 VerifyResult(rv);
371 if (!parent) {
372 printf("Getting parent failed!\n");
373 return;
376 rv = parent->Append(NS_LITERAL_STRING("foo"));
377 VerifyResult(rv);
378 rv = parent->Append(NS_LITERAL_STRING("bar"));
379 VerifyResult(rv);
381 rv = parent->Normalize();
382 VerifyResult(rv);
384 nsCAutoString newPath;
385 rv = parent->GetNativePath(newPath);
386 VerifyResult(rv);
388 nsCOMPtr<nsILocalFile>
389 expectedFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
391 if (!expectedFile)
393 printf("create nsILocalFile failed\n");
394 return;
396 rv = expectedFile->InitWithNativePath(nsDependentCString(expected));
397 VerifyResult(rv);
399 rv = expectedFile->Normalize();
400 VerifyResult(rv);
402 nsCAutoString expectedPath;
403 rv = expectedFile->GetNativePath(expectedPath);
404 VerifyResult(rv);
406 if (!newPath.Equals(expectedPath)) {
407 printf("ERROR: Normalize() test Failed!\n");
408 printf(" Got: %s\n", newPath.get());
409 printf("Expected: %s\n", expectedPath.get());
412 printf("end normalize test.\n");
415 int main(void)
417 nsCOMPtr<nsIServiceManager> servMan;
418 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
419 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
420 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
421 registrar->AutoRegister(nsnull);
423 #if defined(XP_WIN) || defined(XP_OS2)
424 InitTest("c:\\temp\\", "sub1/sub2/"); // expect failure
425 InitTest("d:\\temp\\", "sub1\\sub2\\"); // expect failure
427 CreationTest("c:\\temp\\", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
428 DeletionTest("c:\\temp\\", "file.txt", PR_FALSE);
430 MoveTest("c:\\newtemp\\", "d:");
432 CreationTest("c:\\temp\\", "mumble\\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\", nsIFile::DIRECTORY_TYPE, 0644);
433 DeletionTest("c:\\temp\\", "mumble", PR_TRUE);
435 CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
436 CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
437 CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
438 CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
439 DeletionTest("c:\\temp\\", "foo", PR_TRUE);
440 DeletionTest("c:\\temp\\", "foo-1", PR_TRUE);
441 DeletionTest("c:\\temp\\", "bar.xx", PR_TRUE);
442 DeletionTest("c:\\temp\\", "bar-1.xx", PR_TRUE);
444 #else
445 #ifdef XP_UNIX
446 InitTest("/tmp/", "sub1/sub2/"); // expect failure
448 CreationTest("/tmp", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
449 DeletionTest("/tmp/", "file.txt", PR_FALSE);
451 CreationTest("/tmp", "mumble/a/b/c/d/e/f/g/h/i/j/k/", nsIFile::DIRECTORY_TYPE, 0644);
452 DeletionTest("/tmp", "mumble", PR_TRUE);
454 CreationTest("/tmp", "file", nsIFile::NORMAL_FILE_TYPE, 0644);
455 CopyTest("/tmp/file", "/tmp/newDir");
456 MoveTest("/tmp/file", "/tmp/newDir/anotherNewDir");
457 DeletionTest("/tmp", "newDir", PR_TRUE);
459 CreationTest("/tmp", "qux/quux", nsIFile::NORMAL_FILE_TYPE, 0644);
460 CreationTest("/tmp", "foo/bar", nsIFile::NORMAL_FILE_TYPE, 0644);
461 NormalizeTest("/tmp/qux/quux/..", 1, "/tmp/foo/bar");
462 DeletionTest("/tmp", "qux", PR_TRUE);
463 DeletionTest("/tmp", "foo", PR_TRUE);
465 #endif /* XP_UNIX */
466 #endif /* XP_WIN || XP_OS2 */
467 return 0;