1 #pragma once
2 
3 #include <gtest/gtest.h>
4 
5 #include <QDir>
6 #include <QScopedPointer>
7 #include <QTemporaryDir>
8 
9 #include "mixxxapplication.h"
10 #include "preferences/usersettings.h"
11 
12 #define EXPECT_QSTRING_EQ(expected, test) EXPECT_STREQ(qPrintable(expected), qPrintable(test))
13 #define ASSERT_QSTRING_EQ(expected, test) ASSERT_STREQ(qPrintable(expected), qPrintable(test))
14 
15 class MixxxTest : public testing::Test {
16   public:
17     MixxxTest();
18     ~MixxxTest() override;
19 
20     // ApplicationScope creates QApplication as a singleton and keeps
21     // it alive during all tests. This prevents issues with creating
22     // and destroying the QApplication multiple times in the same process.
23     // http://stackoverflow.com/questions/14243858/qapplication-segfaults-in-googletest
24     class ApplicationScope final {
25       public:
26         ApplicationScope(int& argc, char** argv);
27         ~ApplicationScope();
28     };
29     friend class ApplicationScope;
30 
31   protected:
application()32     static QApplication* application() {
33         return s_pApplication.data();
34     }
35 
config()36     UserSettingsPointer config() const {
37         return m_pConfig;
38     }
39 
40     // Simulate restarting Mixxx by saving and reloading the UserSettings.
41     void saveAndReloadConfig();
42 
getTestDataDir()43     QDir getTestDataDir() const {
44         return m_testDataDir.path();
45     }
46 
47   private:
48     static QScopedPointer<MixxxApplication> s_pApplication;
49 
50     const QTemporaryDir m_testDataDir;
51 
52   protected:
53     UserSettingsPointer m_pConfig;
54 };
55 
56 namespace mixxxtest {
57 
58 /// Returns the full, non-empty file path on success.
59 ///
60 /// For the format of fileNameTemplate refer to QTemporaryFile.
61 QString generateTemporaryFileName(const QString& fileNameTemplate);
62 
63 /// Returns the full, non-empty file path on success.
64 ///
65 /// For the format of fileNameTemplate refer to QTemporaryFile.
66 QString createEmptyTemporaryFile(const QString& fileNameTemplate);
67 
68 bool copyFile(const QString& srcFileName, const QString& dstFileName);
69 
70 class FileRemover final {
71   public:
FileRemover(const QString & fileName)72     explicit FileRemover(const QString& fileName)
73             : m_fileName(fileName) {
74     }
75     ~FileRemover();
76 
keepFile()77     void keepFile() {
78         m_fileName = QString();
79     }
80 
81   private:
82     QString m_fileName;
83 };
84 
85 } // namespace mixxxtest
86