File indexing completed on 2024-05-05 04:14:47
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2022-10-10 0007 * Description : Common class to provides convenient access to digiKam 0008 * test data directories and files. 0009 * 0010 * SPDX-FileCopyrightText: 2022 Steve Robbins <steve at sumost dot ca> 0011 * 0012 * SPDX-License-Identifier: GPL-2.0-or-later 0013 * 0014 * ============================================================ */ 0015 0016 #ifndef DIGIKAM_DTEST_DATA_DIR_H 0017 #define DIGIKAM_DTEST_DATA_DIR_H 0018 0019 // Qt includes 0020 0021 #include <QDir> 0022 0023 /** 0024 * \brief Class that provides convenient access to digiKam test data directories and files. 0025 * 0026 * When instantiated, the test-data directory is located dynamically; this algorithm works as long as 0027 * the current directory is the source root directory or any sub-directory. After construction, 0028 * the function \ref isValid returns true if the test data was successfully located. 0029 */ 0030 0031 class DTestDataDir 0032 { 0033 public: 0034 0035 /** 0036 * True if the instance is correctly instantiated. 0037 * Valid means that the desired root directory was located. 0038 */ 0039 bool isValid() const 0040 { 0041 return m_isValid; 0042 } 0043 0044 /** 0045 * Root directory of test data hierarchy. 0046 */ 0047 QDir root() const 0048 { 0049 return m_testDataDir; 0050 } 0051 0052 /** 0053 * Path to any test file or directory, specified using relative path from root. 0054 */ 0055 QString path(const QString& name) const 0056 { 0057 return root().filePath(name); 0058 } 0059 0060 /** 0061 * Any test directory, specified using relative path from root. 0062 */ 0063 QDir dir(const QString& relPath) const 0064 { 0065 return QDir(path(relPath)); 0066 } 0067 0068 /** 0069 * Any test file, specified using relative path from root. 0070 */ 0071 QFile file(const QString& name) const 0072 { 0073 return QFile(path(name)); 0074 } 0075 0076 /** 0077 * Returns DTestDataDir for the digiKam Test Data root directory. 0078 * This provides access to all files in Digikam Test Data. 0079 */ 0080 static DTestDataDir TestDataRoot() 0081 { 0082 return DTestDataDir(); 0083 } 0084 0085 /** 0086 * Returns DTestDataDir for a sub-tree of the digiKam Test Data. 0087 * This provides access to files in the subtree. 0088 * 0089 * \param subdirPath path of subdir, relative to the Digikam Test Data root 0090 */ 0091 static DTestDataDir TestData(const QString& subdirPath) 0092 { 0093 return DTestDataDir(subdirPath); 0094 } 0095 0096 private: 0097 0098 QDir m_testDataDir; 0099 bool m_isValid = false; 0100 0101 private: 0102 0103 static bool s_findDirectoryUpwards(const QDir& directory, const QString& target, QDir& result) 0104 { 0105 QDir dir = directory; 0106 0107 while (dir.exists(target) == false) 0108 { 0109 if (!dir.cdUp()) 0110 { 0111 return false; 0112 } 0113 } 0114 0115 if (!dir.cd(target)) 0116 { 0117 return false; 0118 } 0119 0120 result = dir; 0121 0122 return true; 0123 } 0124 0125 void initialize() 0126 { 0127 m_isValid = s_findDirectoryUpwards(QDir(), QString::fromUtf8("test-data"), m_testDataDir); 0128 } 0129 0130 protected: 0131 0132 /** 0133 * Constructor with internal instance creation. 0134 */ 0135 DTestDataDir() 0136 { 0137 initialize(); 0138 } 0139 0140 DTestDataDir(const QString& subdirPath) 0141 { 0142 initialize(); 0143 0144 if (!m_isValid) 0145 { 0146 return; 0147 } 0148 0149 m_isValid = m_testDataDir.exists(subdirPath); 0150 0151 if (!m_isValid) 0152 { 0153 return; 0154 } 0155 0156 bool b = m_testDataDir.cd(subdirPath); 0157 Q_UNUSED(b); 0158 } 0159 }; 0160 0161 #endif // DIGIKAM_DTEST_DATA_DIR