tests/auto/qdiriterator/tst_qdiriterator.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    68     Q_OBJECT
    68     Q_OBJECT
    69 
    69 
    70 public:
    70 public:
    71     tst_QDirIterator();
    71     tst_QDirIterator();
    72     virtual ~tst_QDirIterator();
    72     virtual ~tst_QDirIterator();
       
    73 
       
    74 private: // convenience functions
       
    75     QStringList createdDirectories;
       
    76     QStringList createdFiles;
       
    77 
       
    78     QDir currentDir;
       
    79     bool createDirectory(const QString &dirName)
       
    80     {
       
    81         if (currentDir.mkdir(dirName)) {
       
    82             createdDirectories.prepend(dirName);
       
    83             return true;
       
    84         }
       
    85         return false;
       
    86     }
       
    87 
       
    88     enum Cleanup { DoDelete, DontDelete };
       
    89     bool createFile(const QString &fileName, Cleanup cleanup = DoDelete)
       
    90     {
       
    91         QFile file(fileName);
       
    92         if (file.open(QIODevice::WriteOnly)) {
       
    93             if (cleanup == DoDelete)
       
    94                 createdFiles << fileName;
       
    95             return true;
       
    96         }
       
    97         return false;
       
    98     }
       
    99 
       
   100     bool createLink(const QString &destination, const QString &linkName)
       
   101     {
       
   102         if (QFile::link(destination, linkName)) {
       
   103             createdFiles << linkName;
       
   104             return true;
       
   105         }
       
   106         return false;
       
   107     }
    73 
   108 
    74 private slots:
   109 private slots:
    75     void iterateRelativeDirectory_data();
   110     void iterateRelativeDirectory_data();
    76     void iterateRelativeDirectory();
   111     void iterateRelativeDirectory();
    77     void iterateResource_data();
   112     void iterateResource_data();
    94     QFile::remove("entrylist/directory/entrylist1.lnk");
   129     QFile::remove("entrylist/directory/entrylist1.lnk");
    95     QFile::remove("entrylist/directory/entrylist2.lnk");
   130     QFile::remove("entrylist/directory/entrylist2.lnk");
    96     QFile::remove("entrylist/directory/entrylist3.lnk");
   131     QFile::remove("entrylist/directory/entrylist3.lnk");
    97     QFile::remove("entrylist/directory/entrylist4.lnk");
   132     QFile::remove("entrylist/directory/entrylist4.lnk");
    98 
   133 
       
   134     createDirectory("entrylist");
       
   135     createDirectory("entrylist/directory");
       
   136     createFile("entrylist/file", DontDelete);
       
   137     createFile("entrylist/writable");
       
   138     createFile("entrylist/directory/dummy", DontDelete);
       
   139 
       
   140     createDirectory("recursiveDirs");
       
   141     createDirectory("recursiveDirs/dir1");
       
   142     createFile("recursiveDirs/textFileA.txt");
       
   143     createFile("recursiveDirs/dir1/aPage.html");
       
   144     createFile("recursiveDirs/dir1/textFileB.txt");
       
   145 
       
   146     createDirectory("foo");
       
   147     createDirectory("foo/bar");
       
   148     createFile("foo/bar/readme.txt");
       
   149 
    99 #ifndef Q_NO_SYMLINKS
   150 #ifndef Q_NO_SYMLINKS
   100 #  if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
   151 #  if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
   101     // ### Sadly, this is a platform difference right now.
   152     // ### Sadly, this is a platform difference right now.
   102     QFile::link("entrylist/file", "entrylist/linktofile.lnk");
   153     createLink("entrylist/file", "entrylist/linktofile.lnk");
   103 #    ifndef Q_NO_SYMLINKS_TO_DIRS
   154 #    ifndef Q_NO_SYMLINKS_TO_DIRS
   104     QFile::link("entrylist/directory", "entrylist/linktodirectory.lnk");
   155     createLink("entrylist/directory", "entrylist/linktodirectory.lnk");
   105 #    endif
   156 #    endif
   106     QFile::link("entrylist/nothing", "entrylist/brokenlink.lnk");
   157     createLink("entrylist/nothing", "entrylist/brokenlink.lnk");
   107 #  else
   158 #  else
   108     QFile::link("file", "entrylist/linktofile.lnk");
   159     createLink("file", "entrylist/linktofile.lnk");
   109 #    ifndef Q_NO_SYMLINKS_TO_DIRS
   160 #    ifndef Q_NO_SYMLINKS_TO_DIRS
   110     QFile::link("directory", "entrylist/linktodirectory.lnk");
   161     createLink("directory", "entrylist/linktodirectory.lnk");
   111 #    endif
   162 #    endif
   112     QFile::link("nothing", "entrylist/brokenlink.lnk");
   163     createLink("nothing", "entrylist/brokenlink.lnk");
   113 #  endif
   164 #  endif
   114 #endif
   165 #endif
   115     QFile("entrylist/writable").open(QIODevice::ReadWrite);
       
   116 }
   166 }
   117 
   167 
   118 tst_QDirIterator::~tst_QDirIterator()
   168 tst_QDirIterator::~tst_QDirIterator()
   119 {
   169 {
   120     QFile::remove("entrylist/directory");
   170     Q_FOREACH(QString fileName, createdFiles)
   121     QFile::remove("entrylist/linktofile.lnk");
   171         QFile::remove(fileName);
   122     QFile::remove("entrylist/linktodirectory.lnk");
   172 
   123     QFile::remove("entrylist/brokenlink.lnk");
   173     Q_FOREACH(QString dirName, createdDirectories)
   124     QFile::remove("entrylist/brokenlink");
   174         currentDir.rmdir(dirName);
   125     QFile::remove("entrylist/writable");
       
   126     QFile::remove("entrylist/entrylist1.lnk");
       
   127     QFile::remove("entrylist/entrylist2.lnk");
       
   128     QFile::remove("entrylist/entrylist3.lnk");
       
   129     QFile::remove("entrylist/entrylist4.lnk");
       
   130     QFile::remove("entrylist/directory/entrylist1.lnk");
       
   131     QFile::remove("entrylist/directory/entrylist2.lnk");
       
   132     QFile::remove("entrylist/directory/entrylist3.lnk");
       
   133     QFile::remove("entrylist/directory/entrylist4.lnk");
       
   134 }
   175 }
   135 
   176 
   136 void tst_QDirIterator::iterateRelativeDirectory_data()
   177 void tst_QDirIterator::iterateRelativeDirectory_data()
   137 {
   178 {
   138     QTest::addColumn<QString>("dirName"); // relative from current path or abs
   179     QTest::addColumn<QString>("dirName"); // relative from current path or abs
   147         << QString(
   188         << QString(
   148 #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
   189 #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
   149                   "entrylist/.,"
   190                   "entrylist/.,"
   150                    "entrylist/..,"
   191                    "entrylist/..,"
   151 #endif
   192 #endif
       
   193                    "entrylist/file,"
       
   194 #ifndef Q_NO_SYMLINKS
       
   195                    "entrylist/linktofile.lnk,"
       
   196 #endif
       
   197                    "entrylist/directory,"
       
   198 #if !defined(Q_NO_SYMLINKS) && !defined(Q_NO_SYMLINKS_TO_DIRS)
       
   199                    "entrylist/linktodirectory.lnk,"
       
   200 #endif
       
   201                    "entrylist/writable").split(',');
       
   202 
       
   203     QTest::newRow("NoDot")
       
   204         << QString("entrylist") << QDirIterator::IteratorFlags(0)
       
   205         << QDir::Filters(QDir::AllEntries | QDir::NoDot) << QStringList("*")
       
   206         << QString(
       
   207 #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
       
   208                    "entrylist/..,"
       
   209 #endif
       
   210                    "entrylist/file,"
       
   211 #ifndef Q_NO_SYMLINKS
       
   212                    "entrylist/linktofile.lnk,"
       
   213 #endif
       
   214                    "entrylist/directory,"
       
   215 #if !defined(Q_NO_SYMLINKS) && !defined(Q_NO_SYMLINKS_TO_DIRS)
       
   216                    "entrylist/linktodirectory.lnk,"
       
   217 #endif
       
   218                    "entrylist/writable").split(',');
       
   219 
       
   220     QTest::newRow("NoDotDot")
       
   221         << QString("entrylist") << QDirIterator::IteratorFlags(0)
       
   222         << QDir::Filters(QDir::AllEntries | QDir::NoDotDot) << QStringList("*")
       
   223         << QString(
       
   224 #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
       
   225                   "entrylist/.,"
       
   226 #endif
       
   227                    "entrylist/file,"
       
   228 #ifndef Q_NO_SYMLINKS
       
   229                    "entrylist/linktofile.lnk,"
       
   230 #endif
       
   231                    "entrylist/directory,"
       
   232 #if !defined(Q_NO_SYMLINKS) && !defined(Q_NO_SYMLINKS_TO_DIRS)
       
   233                    "entrylist/linktodirectory.lnk,"
       
   234 #endif
       
   235                    "entrylist/writable").split(',');
       
   236 
       
   237     QTest::newRow("NoDotAndDotDot")
       
   238         << QString("entrylist") << QDirIterator::IteratorFlags(0)
       
   239         << QDir::Filters(QDir::AllEntries | QDir::NoDotAndDotDot) << QStringList("*")
       
   240         << QString(
   152                    "entrylist/file,"
   241                    "entrylist/file,"
   153 #ifndef Q_NO_SYMLINKS
   242 #ifndef Q_NO_SYMLINKS
   154                    "entrylist/linktofile.lnk,"
   243                    "entrylist/linktofile.lnk,"
   155 #endif
   244 #endif
   156                    "entrylist/directory,"
   245                    "entrylist/directory,"
   296 
   385 
   297 void tst_QDirIterator::stopLinkLoop()
   386 void tst_QDirIterator::stopLinkLoop()
   298 {
   387 {
   299 #ifdef Q_OS_WIN
   388 #ifdef Q_OS_WIN
   300     // ### Sadly, this is a platform difference right now.
   389     // ### Sadly, this is a platform difference right now.
   301     QFile::link(QDir::currentPath() + QLatin1String("/entrylist"), "entrylist/entrylist1.lnk");
   390     createLink(QDir::currentPath() + QLatin1String("/entrylist"), "entrylist/entrylist1.lnk");
   302     QFile::link("entrylist/.", "entrylist/entrylist2.lnk");
   391     createLink("entrylist/.", "entrylist/entrylist2.lnk");
   303     QFile::link("entrylist/../entrylist/.", "entrylist/entrylist3.lnk");
   392     createLink("entrylist/../entrylist/.", "entrylist/entrylist3.lnk");
   304     QFile::link("entrylist/..", "entrylist/entrylist4.lnk");
   393     createLink("entrylist/..", "entrylist/entrylist4.lnk");
   305     QFile::link(QDir::currentPath() + QLatin1String("/entrylist"), "entrylist/directory/entrylist1.lnk");
   394     createLink(QDir::currentPath() + QLatin1String("/entrylist"), "entrylist/directory/entrylist1.lnk");
   306     QFile::link("entrylist/.", "entrylist/directory/entrylist2.lnk");
   395     createLink("entrylist/.", "entrylist/directory/entrylist2.lnk");
   307     QFile::link("entrylist/../directory/.", "entrylist/directory/entrylist3.lnk");
   396     createLink("entrylist/../directory/.", "entrylist/directory/entrylist3.lnk");
   308     QFile::link("entrylist/..", "entrylist/directory/entrylist4.lnk");
   397     createLink("entrylist/..", "entrylist/directory/entrylist4.lnk");
   309 #else
   398 #else
   310     QFile::link(QDir::currentPath() + QLatin1String("/entrylist"), "entrylist/entrylist1.lnk");
   399     createLink(QDir::currentPath() + QLatin1String("/entrylist"), "entrylist/entrylist1.lnk");
   311     QFile::link(".", "entrylist/entrylist2.lnk");
   400     createLink(".", "entrylist/entrylist2.lnk");
   312     QFile::link("../entrylist/.", "entrylist/entrylist3.lnk");
   401     createLink("../entrylist/.", "entrylist/entrylist3.lnk");
   313     QFile::link("..", "entrylist/entrylist4.lnk");
   402     createLink("..", "entrylist/entrylist4.lnk");
   314     QFile::link(QDir::currentPath() + QLatin1String("/entrylist"), "entrylist/directory/entrylist1.lnk");
   403     createLink(QDir::currentPath() + QLatin1String("/entrylist"), "entrylist/directory/entrylist1.lnk");
   315     QFile::link(".", "entrylist/directory/entrylist2.lnk");
   404     createLink(".", "entrylist/directory/entrylist2.lnk");
   316     QFile::link("../directory/.", "entrylist/directory/entrylist3.lnk");
   405     createLink("../directory/.", "entrylist/directory/entrylist3.lnk");
   317     QFile::link("..", "entrylist/directory/entrylist4.lnk");
   406     createLink("..", "entrylist/directory/entrylist4.lnk");
   318 #endif
   407 #endif
   319 
   408 
   320     QDirIterator it(QLatin1String("entrylist"), QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
   409     QDirIterator it(QLatin1String("entrylist"), QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
   321     QStringList list;
   410     QStringList list;
   322     int max = 200;
   411     int max = 200;
   323     while (--max && it.hasNext())
   412     while (--max && it.hasNext())
   324         it.next();
   413         it.next();
   325     QVERIFY(max);
   414     QVERIFY(max);
   326 
   415 
   327     // The goal of this test is only to ensure that the test above don't malfunction
   416     // The goal of this test is only to ensure that the test above don't malfunction
   328 
       
   329 #ifdef Q_OS_WIN
       
   330     // ### Sadly, this is a platform difference right now.
       
   331     QFile::link(QDir::currentPath() + QLatin1String("/entrylist"), "entrylist/entrylist1.lnk");
       
   332     QFile::link("../../entrylist/.", "entrylist/entrylist2.lnk");
       
   333     QFile::link("entrylist/..", "entrylist/entrylist3.lnk");
       
   334 #else
       
   335     QFile::remove("entrylist/entrylist1.lnk");
       
   336     QFile::remove("entrylist/entrylist2.lnk");
       
   337     QFile::remove("entrylist/entrylist3.lnk");
       
   338     QFile::remove("entrylist/entrylist4.lnk");
       
   339     QFile::remove("entrylist/directory/entrylist1.lnk");
       
   340     QFile::remove("entrylist/directory/entrylist2.lnk");
       
   341     QFile::remove("entrylist/directory/entrylist3.lnk");
       
   342     QFile::remove("entrylist/directory/entrylist4.lnk");
       
   343 #endif
       
   344 }
   417 }
   345 
   418 
   346 class EngineWithNoIterator : public QFSFileEngine
   419 class EngineWithNoIterator : public QFSFileEngine
   347 {
   420 {
   348 public:
   421 public: