|
| 1 | +/* This source file is part of the Tomviz project, https://tomviz.org/. |
| 2 | + It is released under the 3-Clause BSD License, see "LICENSE". */ |
| 3 | + |
| 4 | +#include <QApplication> |
| 5 | +#include <QDebug> |
| 6 | +#include <QDir> |
| 7 | +#include <QFileInfo> |
| 8 | +#include <QLineEdit> |
| 9 | +#include <QProcess> |
| 10 | +#include <QProcessEnvironment> |
| 11 | +#include <QTest> |
| 12 | +#include <QThread> |
| 13 | + |
| 14 | +#include <pqPVApplicationCore.h> |
| 15 | + |
| 16 | +#include "PythonUtilities.h" |
| 17 | +#include "PtychoDialog.h" |
| 18 | +#include "PtychoRunner.h" |
| 19 | + |
| 20 | +#include "TomvizTest.h" |
| 21 | + |
| 22 | +using namespace tomviz; |
| 23 | + |
| 24 | +const QDir ROOT_DATA_DIR = QString(SOURCE_DIR) + "/data"; |
| 25 | +const QDir DATA_DIR = ROOT_DATA_DIR.absolutePath() + "/Pt_Zn_Phase"; |
| 26 | + |
| 27 | +template <typename T> |
| 28 | +T* findWidget() |
| 29 | +{ |
| 30 | + for (auto* widget : QApplication::topLevelWidgets()) { |
| 31 | + if (qobject_cast<T*>(widget)) { |
| 32 | + return qobject_cast<T*>(widget); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return nullptr; |
| 37 | +} |
| 38 | + |
| 39 | +class PtychoWorkflowTest : public QObject |
| 40 | +{ |
| 41 | + Q_OBJECT |
| 42 | + |
| 43 | +private: |
| 44 | + void downloadDataIfMissing() |
| 45 | + { |
| 46 | + if (DATA_DIR.exists()) { |
| 47 | + // Nothing to do |
| 48 | + return; |
| 49 | + } |
| 50 | + // Download the data if it is not present |
| 51 | + QString python = "python"; |
| 52 | + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); |
| 53 | + if (env.contains("TOMVIZ_TEST_PYTHON_EXECUTABLE")) { |
| 54 | + python = env.value("TOMVIZ_TEST_PYTHON_EXECUTABLE"); |
| 55 | + } |
| 56 | + |
| 57 | + auto scriptFile = QFileInfo(QString(SOURCE_DIR) + "/fixtures/download_and_unzip.py"); |
| 58 | + QString scriptPath = scriptFile.absoluteFilePath(); |
| 59 | + |
| 60 | + QString url = "https://data.kitware.com/api/v1/file/6914aad883abdcd84d150c91/download"; |
| 61 | + |
| 62 | + // We unzip into the parent directory, which will then have `Pt_Zn_Phase` |
| 63 | + // after unzipping. |
| 64 | + QStringList arguments; |
| 65 | + arguments << scriptPath << url << ROOT_DATA_DIR.absolutePath(); |
| 66 | + |
| 67 | + QProcess process; |
| 68 | + process.setProcessChannelMode(QProcess::ForwardedChannels); |
| 69 | + process.start(python, arguments); |
| 70 | + |
| 71 | + // Timeout in seconds |
| 72 | + int timeout = 60 * 10; |
| 73 | + QVERIFY(process.waitForFinished(timeout * 1000)); |
| 74 | + QVERIFY(process.exitCode() == 0); |
| 75 | + } |
| 76 | + |
| 77 | +private slots: |
| 78 | + void initTestCase() |
| 79 | + { |
| 80 | + downloadDataIfMissing(); |
| 81 | + } |
| 82 | + |
| 83 | + void cleanupTestCase() {} |
| 84 | + |
| 85 | + void runTest() |
| 86 | + { |
| 87 | + QString ptychoDir = DATA_DIR.absolutePath() + "/Ptycho/recon_result/"; |
| 88 | + QString outputDir = DATA_DIR.absolutePath() + "/output/"; |
| 89 | + |
| 90 | + auto* runner = new PtychoRunner(this); |
| 91 | + runner->setAutoLoadFinalData(false); |
| 92 | + runner->start(); |
| 93 | + |
| 94 | + auto* dialog = findWidget<PtychoDialog>(); |
| 95 | + QVERIFY(dialog); |
| 96 | + |
| 97 | + auto* ptychoDirEdit = dialog->findChild<QLineEdit*>("ptychoDirectory"); |
| 98 | + QVERIFY(ptychoDirEdit); |
| 99 | + ptychoDirEdit->setText(ptychoDir); |
| 100 | + |
| 101 | + // Trigger the necessary changes |
| 102 | + emit ptychoDirEdit->editingFinished(); |
| 103 | + |
| 104 | + auto* outputDirEdit = dialog->findChild<QLineEdit*>("outputDirectory"); |
| 105 | + QVERIFY(outputDirEdit); |
| 106 | + outputDirEdit->setText(outputDir); |
| 107 | + |
| 108 | + dialog->accept(); |
| 109 | + |
| 110 | + QStringList outputFileNames = {"ptycho_object.emd", "ptycho_probe.emd"}; |
| 111 | + |
| 112 | + auto checkAllExist = [&outputDir, &outputFileNames]() { |
| 113 | + for (const auto& filename : outputFileNames) { |
| 114 | + if (!QFile::exists(outputDir + filename)) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + return true; |
| 120 | + }; |
| 121 | + |
| 122 | + // Wait for it to finish, and verify the output files were written |
| 123 | + int timeout = 30; |
| 124 | + int waitTime = 0; |
| 125 | + bool found = false; |
| 126 | + |
| 127 | + while (waitTime < timeout) { |
| 128 | + if (checkAllExist()) { |
| 129 | + found = true; |
| 130 | + break; |
| 131 | + } |
| 132 | + QThread::sleep(1); |
| 133 | + ++waitTime; |
| 134 | + } |
| 135 | + // Verify everything was found |
| 136 | + QVERIFY(found); |
| 137 | + } |
| 138 | + |
| 139 | +}; |
| 140 | + |
| 141 | +int main(int argc, char** argv) |
| 142 | +{ |
| 143 | + QApplication app(argc, argv); |
| 144 | + |
| 145 | + pqPVApplicationCore appCore(argc, argv); |
| 146 | + |
| 147 | + Python::initialize(); |
| 148 | + |
| 149 | + PtychoWorkflowTest tc; |
| 150 | + return QTest::qExec(&tc, argc, argv); |
| 151 | +} |
| 152 | + |
| 153 | +#include "PtychoWorkflowTest.moc" |
0 commit comments