1
#include "nsILocalFile.h"
6
#include "nsIComponentManager.h"
7
#include "nsIComponentRegistrar.h"
8
#include "nsIServiceManager.h"
10
#include "nsXPIDLString.h"
13
void Failed(const char* explanation = nsnull);
15
void Banner(const char* bannerString);
17
void VerifyResult(nsresult rv)
22
printf("rv = %d\n", rv);
25
//----------------------------------------------------------------------------
26
void Banner(const char* bannerString)
27
//----------------------------------------------------------------------------
29
printf("---------------------------\n");
30
printf("%s\n", bannerString);
31
printf("---------------------------\n");
34
//----------------------------------------------------------------------------
36
//----------------------------------------------------------------------------
38
printf("Test passed.");
41
//----------------------------------------------------------------------------
42
void Failed(const char* explanation)
43
//----------------------------------------------------------------------------
45
printf("ERROR : Test failed.\n");
46
printf("REASON: %s.\n", explanation);
49
//----------------------------------------------------------------------------
51
//----------------------------------------------------------------------------
53
printf("^^^^^^^^^^ PLEASE INSPECT OUTPUT FOR ERRORS\n");
56
void GetPaths(nsILocalFile* file)
59
nsCAutoString pathName;
61
printf("Getting Path\n");
63
rv = file->GetNativePath(pathName);
66
printf("filepath: %s\n", pathName.get());
69
void InitTest(const char* creationPath, const char* appendPath)
71
nsILocalFile* file = nsnull;
72
nsresult rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID,
74
NS_GET_IID(nsILocalFile),
77
if (NS_FAILED(rv) || (!file))
79
printf("create nsILocalFile failed\n");
83
nsCAutoString leafName;
85
Banner("InitWithPath");
86
printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
88
rv = file->InitWithNativePath(nsDependentCString(creationPath));
91
printf("Getting Filename\n");
92
rv = file->GetNativeLeafName(leafName);
93
printf(" %s\n", leafName.get());
96
printf("Appending %s \n", appendPath);
97
rv = file->AppendNative(nsDependentCString(appendPath));
100
printf("Getting Filename\n");
101
rv = file->GetNativeLeafName(leafName);
102
printf(" %s\n", leafName.get());
108
printf("Check For Existence\n");
111
file->Exists(&exists);
120
void CreationTest(const char* creationPath, const char* appendPath,
121
PRInt32 whatToCreate, PRInt32 perm)
123
nsCOMPtr<nsILocalFile> file;
125
nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID,
127
NS_GET_IID(nsILocalFile),
128
(void **)getter_AddRefs(file));
130
if (NS_FAILED(rv) || (!file))
132
printf("create nsILocalFile failed\n");
136
Banner("Creation Test");
137
printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
139
rv = file->InitWithNativePath(nsDependentCString(creationPath));
142
printf("Appending %s\n", appendPath);
143
rv = file->AppendRelativeNativePath(nsDependentCString(appendPath));
146
printf("Check For Existence\n");
149
file->Exists(&exists);
157
rv = file->Create(whatToCreate, perm);
160
rv = file->Exists(&exists);
166
Failed("Did not create file system object!");
172
void CreateUniqueTest(const char* creationPath, const char* appendPath,
173
PRInt32 whatToCreate, PRInt32 perm)
175
nsCOMPtr<nsILocalFile> file;
177
nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID,
179
NS_GET_IID(nsILocalFile),
180
(void **)getter_AddRefs(file));
182
if (NS_FAILED(rv) || (!file))
184
printf("create nsILocalFile failed\n");
188
Banner("Creation Test");
189
printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
191
rv = file->InitWithNativePath(nsDependentCString(creationPath));
194
printf("Appending %s\n", appendPath);
195
rv = file->AppendNative(nsDependentCString(appendPath));
198
printf("Check For Existence\n");
201
file->Exists(&exists);
209
rv = file->CreateUnique(whatToCreate, perm);
212
rv = file->Exists(&exists);
218
Failed("Did not create file system object!");
226
CopyTest(const char *testFile, const char *targetDir)
228
nsCOMPtr<nsILocalFile> file;
229
nsCOMPtr<nsILocalFile> dir;
231
printf("start copy test\n");
234
nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, NULL,
235
NS_GET_IID(nsILocalFile),
236
(void**)getter_AddRefs(file));
238
if (NS_FAILED(rv) || (!file))
240
printf("create nsILocalFile failed\n");
244
rv = file->InitWithNativePath(nsDependentCString(testFile));
247
rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, NULL,
248
NS_GET_IID(nsILocalFile),
249
(void**)getter_AddRefs(dir));
251
if (NS_FAILED(rv) || (!dir))
253
printf("create nsILocalFile failed\n");
257
rv = dir->InitWithNativePath(nsDependentCString(targetDir));
260
rv = file->CopyTo(dir, nsString());
263
printf("end copy test\n");
267
DeletionTest(const char* creationPath, const char* appendPath, PRBool recursive)
269
nsCOMPtr<nsILocalFile> file;
271
nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, NULL,
272
NS_GET_IID(nsILocalFile),
273
(void**)getter_AddRefs(file));
275
if (NS_FAILED(rv) || (!file))
277
printf("create nsILocalFile failed\n");
281
Banner("Deletion Test");
282
printf("creationPath == %s\nappendPath == %s\n", creationPath, appendPath);
284
rv = file->InitWithNativePath(nsDependentCString(creationPath));
287
printf("Appending %s\n", appendPath);
288
rv = file->AppendNative(nsDependentCString(appendPath));
291
printf("Check For Existance\n");
294
file->Exists(&exists);
301
rv = file->Remove(recursive);
304
rv = file->Exists(&exists);
309
Failed("Did not create delete system object!");
316
MoveTest(const char *testFile, const char *targetDir)
320
printf("start move test\n");
323
nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
327
printf("create nsILocalFile failed\n");
331
rv = file->InitWithNativePath(nsDependentCString(testFile));
334
nsCOMPtr<nsILocalFile> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
338
printf("create nsILocalFile failed\n");
342
rv = dir->InitWithNativePath(nsDependentCString(targetDir));
345
rv = file->MoveToNative(dir, NS_LITERAL_CSTRING("newtemp"));
349
printf("MoveToNative() test Failed.\n");
351
printf("end move test\n");
358
nsCOMPtr<nsIServiceManager> servMan;
359
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
360
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
361
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
362
registrar->AutoRegister(nsnull);
364
#if defined(XP_WIN) || defined(XP_OS2)
365
InitTest("c:\\temp\\", "sub1/sub2/");
366
InitTest("d:\\temp\\", "sub1\\sub2\\");
368
CreationTest("c:\\temp\\", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
369
DeletionTest("c:\\temp\\", "file.txt", PR_FALSE);
371
MoveTest("c:\\newtemp\\", "d:");
373
CreationTest("c:\\temp\\", "mumble\\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\", nsIFile::DIRECTORY_TYPE, 0644);
374
DeletionTest("c:\\temp\\", "mumble", PR_TRUE);
376
CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
377
CreateUniqueTest("c:\\temp\\", "foo", nsIFile::NORMAL_FILE_TYPE, 0644);
378
CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
379
CreateUniqueTest("c:\\temp\\", "bar.xx", nsIFile::DIRECTORY_TYPE, 0644);
380
DeletionTest("c:\\temp\\", "foo", PR_TRUE);
381
DeletionTest("c:\\temp\\", "foo-1", PR_TRUE);
382
DeletionTest("c:\\temp\\", "bar.xx", PR_TRUE);
383
DeletionTest("c:\\temp\\", "bar-1.xx", PR_TRUE);
387
InitTest("/tmp/", "sub1/sub2/");
389
CreationTest("/tmp", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
390
DeletionTest("/tmp/", "file.txt", PR_FALSE);
392
CreationTest("/tmp", "mumble/a/b/c/d/e/f/g/h/i/j/k/", nsIFile::DIRECTORY_TYPE, 0644);
393
DeletionTest("/tmp", "mumble", PR_TRUE);
394
CopyTest("/tmp/test.txt", "/tmp/foo");
397
#endif /* XP_WIN || XP_OS2 */