Changeset 589 in vbox for trunk/src/libs/xpcom18a4/xpcom/tests
- Timestamp:
- Feb 4, 2007 12:25:57 PM (18 years ago)
- svn:sync-xref-src-repo-rev:
- 18212
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/libs/xpcom18a4/xpcom/tests/nsIFileTest.cpp
r1 r589 1 1 #include "nsILocalFile.h" 2 #if 0 /* too new */ 3 #include "nsStringGlue.h" 4 #else 2 5 #include "nsString.h" 6 #endif 3 7 4 8 #include <stdio.h> … … 8 12 #include "nsIServiceManager.h" 9 13 #include "nsIMemory.h" 10 #include "nsXPIDLString.h" 14 15 #include "nsComponentManagerUtils.h" 16 #include "nsCOMPtr.h" 11 17 12 18 void Passed(); … … 63 69 rv = file->GetNativePath(pathName); 64 70 VerifyResult(rv); 65 71 66 72 printf("filepath: %s\n", pathName.get()); 67 73 } … … 70 76 { 71 77 nsILocalFile* file = nsnull; 72 nsresult rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, 73 nsnull, 74 NS_GET_IID(nsILocalFile), 75 (void**)&file); 76 77 if (NS_FAILED(rv) || (!file)) 78 nsresult rv = CallCreateInstance(NS_LOCAL_FILE_CONTRACTID, &file); 79 80 81 if (NS_FAILED(rv) || (!file)) 78 82 { 79 83 printf("create nsILocalFile failed\n"); … … 88 92 rv = file->InitWithNativePath(nsDependentCString(creationPath)); 89 93 VerifyResult(rv); 90 94 91 95 printf("Getting Filename\n"); 92 96 rv = file->GetNativeLeafName(leafName); … … 105 109 GetPaths(file); 106 110 107 111 108 112 printf("Check For Existence\n"); 109 113 … … 121 125 PRInt32 whatToCreate, PRInt32 perm) 122 126 { 123 nsCOMPtr<nsILocalFile> file; 124 nsresult rv = 125 nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, 126 nsnull, 127 NS_GET_IID(nsILocalFile), 128 (void **)getter_AddRefs(file)); 129 130 if (NS_FAILED(rv) || (!file)) 127 nsresult rv; 128 nsCOMPtr<nsILocalFile> file = 129 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv); 130 131 if (NS_FAILED(rv) || (!file)) 131 132 { 132 133 printf("create nsILocalFile failed\n"); … … 139 140 rv = file->InitWithNativePath(nsDependentCString(creationPath)); 140 141 VerifyResult(rv); 141 142 142 143 printf("Appending %s\n", appendPath); 143 144 rv = file->AppendRelativeNativePath(nsDependentCString(appendPath)); 144 145 VerifyResult(rv); 145 146 146 147 printf("Check For Existence\n"); 147 148 … … 155 156 156 157 157 rv = file->Create(whatToCreate, perm); 158 rv = file->Create(whatToCreate, perm); 158 159 VerifyResult(rv); 159 160 … … 161 162 VerifyResult(rv); 162 163 163 164 164 165 if (!exists) 165 166 { … … 167 168 return; 168 169 } 169 170 } 170 171 } 171 172 172 173 void CreateUniqueTest(const char* creationPath, const char* appendPath, 173 174 PRInt32 whatToCreate, PRInt32 perm) 174 175 { 175 nsCOMPtr<nsILocalFile> file; 176 nsresult rv = 177 nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, 178 nsnull, 179 NS_GET_IID(nsILocalFile), 180 (void **)getter_AddRefs(file)); 181 182 if (NS_FAILED(rv) || (!file)) 176 nsresult rv; 177 nsCOMPtr<nsILocalFile> file = 178 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv); 179 180 if (NS_FAILED(rv) || (!file)) 183 181 { 184 182 printf("create nsILocalFile failed\n"); … … 191 189 rv = file->InitWithNativePath(nsDependentCString(creationPath)); 192 190 VerifyResult(rv); 193 191 194 192 printf("Appending %s\n", appendPath); 195 193 rv = file->AppendNative(nsDependentCString(appendPath)); 196 194 VerifyResult(rv); 197 195 198 196 printf("Check For Existence\n"); 199 197 … … 207 205 208 206 209 rv = file->CreateUnique(whatToCreate, perm); 207 rv = file->CreateUnique(whatToCreate, perm); 210 208 VerifyResult(rv); 211 209 … … 213 211 VerifyResult(rv); 214 212 215 213 216 214 if (!exists) 217 215 { … … 219 217 return; 220 218 } 221 222 } 219 220 } 223 221 224 222 … … 226 224 CopyTest(const char *testFile, const char *targetDir) 227 225 { 228 nsCOMPtr<nsILocalFile> file;229 nsCOMPtr<nsILocalFile> dir;230 231 226 printf("start copy test\n"); 232 227 233 nsresult rv = 234 nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, NULL, 235 NS_GET_IID(nsILocalFile), 236 (void**)getter_AddRefs(file)); 237 238 if (NS_FAILED(rv) || (!file)) 228 nsresult rv; 229 nsCOMPtr<nsILocalFile> file = 230 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv); 231 232 if (NS_FAILED(rv) || (!file)) 239 233 { 240 234 printf("create nsILocalFile failed\n"); … … 244 238 rv = file->InitWithNativePath(nsDependentCString(testFile)); 245 239 VerifyResult(rv); 246 247 rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, NULL, 248 NS_GET_IID(nsILocalFile), 249 (void**)getter_AddRefs(dir)); 250 251 if (NS_FAILED(rv) || (!dir)) 240 241 nsCOMPtr<nsILocalFile> dir = 242 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv); 243 244 if (NS_FAILED(rv) || (!dir)) 252 245 { 253 246 printf("create nsILocalFile failed\n"); … … 263 256 printf("end copy test\n"); 264 257 } 265 258 266 259 void 267 260 DeletionTest(const char* creationPath, const char* appendPath, PRBool recursive) 268 261 { 269 nsCOMPtr<nsILocalFile> file; 270 nsresult rv = 271 nsComponentManager::CreateInstance(NS_LOCAL_FILE_CONTRACTID, NULL, 272 NS_GET_IID(nsILocalFile), 273 (void**)getter_AddRefs(file)); 274 275 if (NS_FAILED(rv) || (!file)) 262 nsresult rv; 263 nsCOMPtr<nsILocalFile> file = 264 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv); 265 266 if (NS_FAILED(rv) || (!file)) 276 267 { 277 268 printf("create nsILocalFile failed\n"); … … 284 275 rv = file->InitWithNativePath(nsDependentCString(creationPath)); 285 276 VerifyResult(rv); 286 277 287 278 printf("Appending %s\n", appendPath); 288 279 rv = file->AppendNative(nsDependentCString(appendPath)); 289 280 VerifyResult(rv); 290 281 291 282 printf("Check For Existance\n"); 292 283 … … 299 290 printf("no.\n"); 300 291 301 rv = file->Remove(recursive); 292 rv = file->Remove(recursive); 302 293 VerifyResult(rv); 303 294 304 295 rv = file->Exists(&exists); 305 296 VerifyResult(rv); 306 297 307 298 if (exists) 308 299 { … … 310 301 return; 311 302 } 312 313 } 314 315 void 303 304 } 305 306 void 316 307 MoveTest(const char *testFile, const char *targetDir) 317 308 { … … 322 313 nsresult rv; 323 314 nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID)); 324 325 if (!file) 315 316 if (!file) 326 317 { 327 318 printf("create nsILocalFile failed\n"); … … 331 322 rv = file->InitWithNativePath(nsDependentCString(testFile)); 332 323 VerifyResult(rv); 333 324 334 325 nsCOMPtr<nsILocalFile> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID)); 335 336 if (!dir) 326 327 if (!dir) 337 328 { 338 329 printf("create nsILocalFile failed\n"); … … 352 343 } 353 344 354 345 // move up the number of directories in moveUpCount, then append "foo/bar" 346 void 347 NormalizeTest(const char *testPath, int moveUpCount, 348 const char *expected) 349 { 350 Banner("Normalize Test"); 351 352 nsresult rv; 353 nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID)); 354 355 if (!file) 356 { 357 printf("create nsILocalFile failed\n"); 358 return; 359 } 360 361 rv = file->InitWithNativePath(nsDependentCString(testPath)); 362 VerifyResult(rv); 363 364 nsCOMPtr<nsIFile> parent; 365 nsAutoString path; 366 for (int i=0; i < moveUpCount; i++) 367 { 368 rv = file->GetParent(getter_AddRefs(parent)); 369 VerifyResult(rv); 370 rv = parent->GetPath(path); 371 VerifyResult(rv); 372 rv = file->InitWithPath(path); 373 VerifyResult(rv); 374 } 375 376 if (!parent) { 377 printf("Getting parent failed!\n"); 378 return; 379 } 380 381 rv = parent->Append(NS_LITERAL_STRING("foo")); 382 VerifyResult(rv); 383 rv = parent->Append(NS_LITERAL_STRING("bar")); 384 VerifyResult(rv); 385 386 rv = parent->Normalize(); 387 VerifyResult(rv); 388 389 nsCAutoString newPath; 390 rv = parent->GetNativePath(newPath); 391 VerifyResult(rv); 392 393 nsCOMPtr<nsILocalFile> 394 expectedFile(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID)); 395 396 if (!expectedFile) 397 { 398 printf("create nsILocalFile failed\n"); 399 return; 400 } 401 rv = expectedFile->InitWithNativePath(nsDependentCString(expected)); 402 VerifyResult(rv); 403 404 rv = expectedFile->Normalize(); 405 VerifyResult(rv); 406 407 nsCAutoString expectedPath; 408 rv = expectedFile->GetNativePath(expectedPath); 409 VerifyResult(rv); 410 411 if (!newPath.Equals(expectedPath)) { 412 printf("ERROR: Normalize() test Failed!\n"); 413 printf(" Got: %s\n", newPath.get()); 414 printf("Expected: %s\n", expectedPath); 415 } 416 417 printf("end normalize test.\n"); 418 } 355 419 356 420 int main(void) … … 361 425 NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); 362 426 registrar->AutoRegister(nsnull); 363 427 364 428 #if defined(XP_WIN) || defined(XP_OS2) 365 InitTest("c:\\temp\\", "sub1/sub2/"); 366 InitTest("d:\\temp\\", "sub1\\sub2\\"); 429 InitTest("c:\\temp\\", "sub1/sub2/"); // expect failure 430 InitTest("d:\\temp\\", "sub1\\sub2\\"); // expect failure 367 431 368 432 CreationTest("c:\\temp\\", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644); … … 385 449 #else 386 450 #ifdef XP_UNIX 387 InitTest("/tmp/", "sub1/sub2/"); 388 451 InitTest("/tmp/", "sub1/sub2/"); // expect failure 452 389 453 CreationTest("/tmp", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644); 390 454 DeletionTest("/tmp/", "file.txt", PR_FALSE); 391 455 392 456 CreationTest("/tmp", "mumble/a/b/c/d/e/f/g/h/i/j/k/", nsIFile::DIRECTORY_TYPE, 0644); 393 457 DeletionTest("/tmp", "mumble", PR_TRUE); 394 CopyTest("/tmp/test.txt", "/tmp/foo"); 458 459 CreationTest("/tmp", "file", nsIFile::NORMAL_FILE_TYPE, 0644); 460 CopyTest("/tmp/file", "/tmp/newDir"); 461 MoveTest("/tmp/file", "/tmp/newDir/anotherNewDir"); 462 DeletionTest("/tmp", "newDir", PR_TRUE); 463 464 CreationTest("/tmp", "qux/quux", nsIFile::NORMAL_FILE_TYPE, 0644); 465 CreationTest("/tmp", "foo/bar", nsIFile::NORMAL_FILE_TYPE, 0644); 466 NormalizeTest("/tmp/qux/quux/..", 1, "/tmp/foo/bar"); 467 DeletionTest("/tmp", "qux", PR_TRUE); 468 DeletionTest("/tmp", "foo", PR_TRUE); 395 469 396 470 #endif /* XP_UNIX */
Note:
See TracChangeset
for help on using the changeset viewer.