VirtualBox

Ignore:
Timestamp:
Feb 4, 2007 12:25:57 PM (18 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
18212
Message:

Make it build and run on Mac OS X.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/xpcom18a4/xpcom/tests/nsIFileTest.cpp

    r1 r589  
    11#include "nsILocalFile.h"
     2#if 0 /* too new */
     3#include "nsStringGlue.h"
     4#else
    25#include "nsString.h"
     6#endif
    37
    48#include <stdio.h>
     
    812#include "nsIServiceManager.h"
    913#include "nsIMemory.h"
    10 #include "nsXPIDLString.h"
     14
     15#include "nsComponentManagerUtils.h"
     16#include "nsCOMPtr.h"
    1117
    1218void Passed();
     
    6369    rv = file->GetNativePath(pathName);
    6470    VerifyResult(rv);
    65    
     71
    6672    printf("filepath: %s\n", pathName.get());
    6773}
     
    7076{
    7177    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))
    7882    {
    7983        printf("create nsILocalFile failed\n");
     
    8892    rv = file->InitWithNativePath(nsDependentCString(creationPath));
    8993    VerifyResult(rv);
    90    
     94
    9195    printf("Getting Filename\n");
    9296    rv = file->GetNativeLeafName(leafName);
     
    105109    GetPaths(file);
    106110
    107    
     111
    108112    printf("Check For Existence\n");
    109113
     
    121125                  PRInt32 whatToCreate, PRInt32 perm)
    122126{
    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))
    131132    {
    132133        printf("create nsILocalFile failed\n");
     
    139140    rv = file->InitWithNativePath(nsDependentCString(creationPath));
    140141    VerifyResult(rv);
    141  
     142
    142143    printf("Appending %s\n", appendPath);
    143144    rv = file->AppendRelativeNativePath(nsDependentCString(appendPath));
    144145    VerifyResult(rv);
    145    
     146
    146147    printf("Check For Existence\n");
    147148
     
    155156
    156157
    157     rv = file->Create(whatToCreate, perm); 
     158    rv = file->Create(whatToCreate, perm);
    158159    VerifyResult(rv);
    159160
     
    161162    VerifyResult(rv);
    162163
    163    
     164
    164165    if (!exists)
    165166    {
     
    167168        return;
    168169    }
    169    
    170 }   
     170
     171}
    171172
    172173void CreateUniqueTest(const char* creationPath, const char* appendPath,
    173174                 PRInt32 whatToCreate, PRInt32 perm)
    174175{
    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))
    183181    {
    184182        printf("create nsILocalFile failed\n");
     
    191189    rv = file->InitWithNativePath(nsDependentCString(creationPath));
    192190    VerifyResult(rv);
    193  
     191
    194192    printf("Appending %s\n", appendPath);
    195193    rv = file->AppendNative(nsDependentCString(appendPath));
    196194    VerifyResult(rv);
    197    
     195
    198196    printf("Check For Existence\n");
    199197
     
    207205
    208206
    209     rv = file->CreateUnique(whatToCreate, perm); 
     207    rv = file->CreateUnique(whatToCreate, perm);
    210208    VerifyResult(rv);
    211209
     
    213211    VerifyResult(rv);
    214212
    215    
     213
    216214    if (!exists)
    217215    {
     
    219217        return;
    220218    }
    221    
    222 }   
     219
     220}
    223221
    224222
     
    226224CopyTest(const char *testFile, const char *targetDir)
    227225{
    228   nsCOMPtr<nsILocalFile> file;
    229   nsCOMPtr<nsILocalFile> dir;
    230 
    231226  printf("start copy test\n");
    232227
    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))
    239233  {
    240234    printf("create nsILocalFile failed\n");
     
    244238  rv = file->InitWithNativePath(nsDependentCString(testFile));
    245239  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))
    252245  {
    253246    printf("create nsILocalFile failed\n");
     
    263256  printf("end copy test\n");
    264257}
    265    
     258
    266259void
    267260DeletionTest(const char* creationPath, const char* appendPath, PRBool recursive)
    268261{
    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))
    276267    {
    277268        printf("create nsILocalFile failed\n");
     
    284275    rv = file->InitWithNativePath(nsDependentCString(creationPath));
    285276    VerifyResult(rv);
    286  
     277
    287278    printf("Appending %s\n", appendPath);
    288279    rv = file->AppendNative(nsDependentCString(appendPath));
    289280    VerifyResult(rv);
    290    
     281
    291282    printf("Check For Existance\n");
    292283
     
    299290        printf("no.\n");
    300291
    301     rv = file->Remove(recursive); 
     292    rv = file->Remove(recursive);
    302293    VerifyResult(rv);
    303294
    304295    rv = file->Exists(&exists);
    305296    VerifyResult(rv);
    306    
     297
    307298    if (exists)
    308299    {
     
    310301        return;
    311302    }
    312    
    313 }
    314 
    315 void 
     303
     304}
     305
     306void
    316307MoveTest(const char *testFile, const char *targetDir)
    317308{
     
    322313  nsresult rv;
    323314  nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
    324      
    325   if (!file) 
     315
     316  if (!file)
    326317  {
    327318    printf("create nsILocalFile failed\n");
     
    331322  rv = file->InitWithNativePath(nsDependentCString(testFile));
    332323  VerifyResult(rv);
    333  
     324
    334325  nsCOMPtr<nsILocalFile> dir(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
    335  
    336   if (!dir) 
     326
     327  if (!dir)
    337328  {
    338329    printf("create nsILocalFile failed\n");
     
    352343}
    353344
    354 
     345// move up the number of directories in moveUpCount, then append "foo/bar"
     346void
     347NormalizeTest(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}
    355419
    356420int main(void)
     
    361425    NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
    362426    registrar->AutoRegister(nsnull);
    363  
     427
    364428#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
    367431
    368432    CreationTest("c:\\temp\\", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
     
    385449#else
    386450#ifdef XP_UNIX
    387     InitTest("/tmp/", "sub1/sub2/");
    388    
     451    InitTest("/tmp/", "sub1/sub2/"); // expect failure
     452
    389453    CreationTest("/tmp", "file.txt", nsIFile::NORMAL_FILE_TYPE, 0644);
    390454    DeletionTest("/tmp/", "file.txt", PR_FALSE);
    391    
     455
    392456    CreationTest("/tmp", "mumble/a/b/c/d/e/f/g/h/i/j/k/", nsIFile::DIRECTORY_TYPE, 0644);
    393457    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);
    395469
    396470#endif /* XP_UNIX */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette