1 #include "nsILocalFile.h"
2 #include "nsStringGlue.h"
6 #include "nsIComponentManager.h"
7 #include "nsIComponentRegistrar.h"
8 #include "nsIServiceManager.h"
11 #include "nsComponentManagerUtils.h"
15 void Failed(const char* explanation
= nsnull
);
17 void Banner(const char* bannerString
);
19 void VerifyResult(nsresult rv
)
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 //----------------------------------------------------------------------------
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 //----------------------------------------------------------------------------
53 //----------------------------------------------------------------------------
55 printf("^^^^^^^^^^ PLEASE INSPECT OUTPUT FOR ERRORS\n");
58 void GetPaths(nsILocalFile
* file
)
61 nsCAutoString pathName
;
63 printf("Getting Path\n");
65 rv
= file
->GetNativePath(pathName
);
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");
82 nsCAutoString leafName
;
84 Banner("InitWithPath");
85 printf("creationPath == %s\nappendPath == %s\n", creationPath
, appendPath
);
87 rv
= file
->InitWithNativePath(nsDependentCString(creationPath
));
90 printf("Getting Filename\n");
91 rv
= file
->GetNativeLeafName(leafName
);
92 printf(" %s\n", leafName
.get());
95 printf("Appending %s \n", appendPath
);
96 rv
= file
->AppendNative(nsDependentCString(appendPath
));
99 printf("Getting Filename\n");
100 rv
= file
->GetNativeLeafName(leafName
);
101 printf(" %s\n", leafName
.get());
107 printf("Check For Existence\n");
110 file
->Exists(&exists
);
119 void CreationTest(const char* creationPath
, const char* appendPath
,
120 PRInt32 whatToCreate
, PRInt32 perm
)
123 nsCOMPtr
<nsILocalFile
> file
=
124 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
, &rv
);
126 if (NS_FAILED(rv
) || (!file
))
128 printf("create nsILocalFile failed\n");
132 Banner("Creation Test");
133 printf("creationPath == %s\nappendPath == %s\n", creationPath
, appendPath
);
135 rv
= file
->InitWithNativePath(nsDependentCString(creationPath
));
138 printf("Appending %s\n", appendPath
);
139 rv
= file
->AppendRelativeNativePath(nsDependentCString(appendPath
));
142 printf("Check For Existence\n");
145 file
->Exists(&exists
);
153 rv
= file
->Create(whatToCreate
, perm
);
156 rv
= file
->Exists(&exists
);
162 Failed("Did not create file system object!");
168 void CreateUniqueTest(const char* creationPath
, const char* appendPath
,
169 PRInt32 whatToCreate
, PRInt32 perm
)
172 nsCOMPtr
<nsILocalFile
> file
=
173 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
, &rv
);
175 if (NS_FAILED(rv
) || (!file
))
177 printf("create nsILocalFile failed\n");
181 Banner("Creation Test");
182 printf("creationPath == %s\nappendPath == %s\n", creationPath
, appendPath
);
184 rv
= file
->InitWithNativePath(nsDependentCString(creationPath
));
187 printf("Appending %s\n", appendPath
);
188 rv
= file
->AppendNative(nsDependentCString(appendPath
));
191 printf("Check For Existence\n");
194 file
->Exists(&exists
);
202 rv
= file
->CreateUnique(whatToCreate
, perm
);
205 rv
= file
->Exists(&exists
);
211 Failed("Did not create file system object!");
219 CopyTest(const char *testFile
, const char *targetDir
)
221 printf("start copy test\n");
224 nsCOMPtr
<nsILocalFile
> file
=
225 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
, &rv
);
227 if (NS_FAILED(rv
) || (!file
))
229 printf("create nsILocalFile failed\n");
233 rv
= file
->InitWithNativePath(nsDependentCString(testFile
));
236 nsCOMPtr
<nsILocalFile
> dir
=
237 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
, &rv
);
239 if (NS_FAILED(rv
) || (!dir
))
241 printf("create nsILocalFile failed\n");
245 rv
= dir
->InitWithNativePath(nsDependentCString(targetDir
));
248 rv
= file
->CopyTo(dir
, EmptyString());
251 printf("end copy test\n");
255 DeletionTest(const char* creationPath
, const char* appendPath
, PRBool recursive
)
258 nsCOMPtr
<nsILocalFile
> file
=
259 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
, &rv
);
261 if (NS_FAILED(rv
) || (!file
))
263 printf("create nsILocalFile failed\n");
267 Banner("Deletion Test");
268 printf("creationPath == %s\nappendPath == %s\n", creationPath
, appendPath
);
270 rv
= file
->InitWithNativePath(nsDependentCString(creationPath
));
273 printf("Appending %s\n", appendPath
);
274 rv
= file
->AppendNative(nsDependentCString(appendPath
));
277 printf("Check For Existance\n");
280 file
->Exists(&exists
);
287 rv
= file
->Remove(recursive
);
290 rv
= file
->Exists(&exists
);
295 Failed("Did not create delete system object!");
302 MoveTest(const char *testFile
, const char *targetDir
)
306 printf("start move test\n");
309 nsCOMPtr
<nsILocalFile
> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
));
313 printf("create nsILocalFile failed\n");
317 rv
= file
->InitWithNativePath(nsDependentCString(testFile
));
320 nsCOMPtr
<nsILocalFile
> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
));
324 printf("create nsILocalFile failed\n");
328 rv
= dir
->InitWithNativePath(nsDependentCString(targetDir
));
331 rv
= file
->MoveToNative(dir
, NS_LITERAL_CSTRING("newtemp"));
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"
342 NormalizeTest(const char *testPath
, int moveUpCount
,
343 const char *expected
)
345 Banner("Normalize Test");
348 nsCOMPtr
<nsILocalFile
> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
));
352 printf("create nsILocalFile failed\n");
356 rv
= file
->InitWithNativePath(nsDependentCString(testPath
));
359 nsCOMPtr
<nsIFile
> parent
;
361 for (int i
=0; i
< moveUpCount
; i
++)
363 rv
= file
->GetParent(getter_AddRefs(parent
));
365 rv
= parent
->GetPath(path
);
367 rv
= file
->InitWithPath(path
);
372 printf("Getting parent failed!\n");
376 rv
= parent
->Append(NS_LITERAL_STRING("foo"));
378 rv
= parent
->Append(NS_LITERAL_STRING("bar"));
381 rv
= parent
->Normalize();
384 nsCAutoString newPath
;
385 rv
= parent
->GetNativePath(newPath
);
388 nsCOMPtr
<nsILocalFile
>
389 expectedFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID
));
393 printf("create nsILocalFile failed\n");
396 rv
= expectedFile
->InitWithNativePath(nsDependentCString(expected
));
399 rv
= expectedFile
->Normalize();
402 nsCAutoString expectedPath
;
403 rv
= expectedFile
->GetNativePath(expectedPath
);
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");
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
);
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
);
466 #endif /* XP_WIN || XP_OS2 */