1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #include "UGUITestBase.h"
23 
24 #include <U2Core/TextUtils.h>
25 
26 namespace U2 {
27 
getInstance()28 UGUITestBase *UGUITestBase::getInstance() {
29     static UGUITestBase *instance = new UGUITestBase();
30     return instance;
31 }
32 
registerTest(GUITest * test,TestType testType)33 bool UGUITestBase::registerTest(GUITest *test, TestType testType) {
34     Q_ASSERT(test);
35     QString fullTestName = test->getFullName();
36     if (getTest(fullTestName, type) != nullptr) {
37         return false;
38     }
39     getMap(testType).insert(fullTestName, test);
40     return true;
41 }
42 
getTest(const QString & name,TestType testType) const43 GUITest *UGUITestBase::getTest(const QString &name, TestType testType) const {
44     return getConstMap(testType).value(name);
45 }
46 
getTest(const QString & suite,const QString & name,TestType testType) const47 GUITest *UGUITestBase::getTest(const QString &suite, const QString &name, TestType testType) const {
48     return getTest(HI::GUITest::getFullTestName(suite, name), testType);
49 }
50 
getConstMap(TestType testType) const51 const QMap<QString, GUITest *> &UGUITestBase::getConstMap(TestType testType) const {
52     switch (testType) {
53         case PreAdditional:
54             return preAdditional;
55         case PostAdditionalChecks:
56             return postAdditionalChecks;
57         case PostAdditionalActions:
58             return postAdditionalActions;
59         case Normal:
60         default:
61             return tests;
62     }
63 }
64 
getMap(TestType testType)65 QMap<QString, GUITest *> &UGUITestBase::getMap(TestType testType) {
66     switch (testType) {
67         case PreAdditional:
68             return preAdditional;
69         case PostAdditionalChecks:
70             return postAdditionalChecks;
71         case PostAdditionalActions:
72             return postAdditionalActions;
73         case Normal:
74         default:
75             return tests;
76     }
77 }
78 
79 /**
80  * Returns true if set1 shares common elements with set2.
81  * Note: We can't use QSet::intersects today because it is not available in QT5.4 (was added in QT5.6)
82  */
intersects(const QSet<QString> & set1,const QSet<QString> & set2)83 static bool intersects(const QSet<QString> &set1, const QSet<QString> &set2) {
84     for (const QString &value1 : qAsConst(set1)) {
85         if (set2.contains(value1)) {
86             return true;
87         }
88     }
89     return false;
90 }
91 
getTests(TestType testType,const QStringList & labelList) const92 QList<GUITest *> UGUITestBase::getTests(TestType testType, const QStringList &labelList) const {
93     QList<GUITest *> allTestList = getConstMap(testType).values();
94     if (labelList.isEmpty()) {
95         return allTestList;
96     }
97     QList<GUITest *> filteredTestList;
98     QSet<QString> includeLabelSet;
99     QSet<QString> excludeLabelSet;
100     for (const QString &label : qAsConst(labelList)) {
101         if (label.startsWith("-")) {
102             excludeLabelSet << label;
103         } else {
104             includeLabelSet << label;
105         }
106     }
107     for (GUITest *test : qAsConst(allTestList)) {
108         if (test->labelSet.contains(includeLabelSet) && !intersects(test->labelSet, excludeLabelSet)) {
109             filteredTestList << test;
110         }
111     }
112     return filteredTestList;
113 }
114 
115 }  // namespace U2
116