1 /******************************************************************************
2 
3   This source file is part of the Avogadro project.
4 
5   Copyright 2013 Kitware, Inc.
6 
7   This source code is released under the New BSD License, (the "License").
8 
9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License.
14 
15 ******************************************************************************/
16 
17 #include <gtest/gtest.h>
18 
19 #include "qtguitests.h"
20 
21 #include <avogadro/molequeue/inputgeneratorwidget.h>
22 #include <avogadro/qtgui/filebrowsewidget.h>
23 #include <avogadro/qtgui/molecule.h>
24 
25 #include <QtWidgets/QApplication>
26 #include <QtWidgets/QCheckBox>
27 #include <QtWidgets/QComboBox>
28 #include <QtWidgets/QLineEdit>
29 #include <QtWidgets/QPushButton>
30 #include <QtWidgets/QSpinBox>
31 #include <QtWidgets/QTextEdit>
32 
33 #include <QtCore/QFile>
34 #include <QtCore/QTimer>
35 
36 using Avogadro::QtGui::FileBrowseWidget;
37 using Avogadro::MoleQueue::InputGeneratorWidget;
38 using Avogadro::QtGui::Molecule;
39 
40 namespace {
41 
flushEvents()42 void flushEvents()
43 {
44   // Post a quit event at the end of the event queue
45   QTimer::singleShot(0, qApp, SLOT(quit()));
46   // Process events in the queue
47   qApp->exec();
48 }
49 
50 } // end anon namespace
51 
TEST(InputGeneratorWidgetTest,exercise)52 TEST(InputGeneratorWidgetTest, exercise)
53 {
54   // Fake a QApplication -- needed to instantiate widgets.
55   int argc = 1;
56   char argName[] = "FakeApp.exe";
57   char* argv[2] = { argName, nullptr };
58   QApplication app(argc, argv);
59   Q_UNUSED(app);
60 
61   // Setup the widget
62   InputGeneratorWidget widget;
63   QString scriptFilePath(AVOGADRO_DATA
64                          "/tests/avogadro/scripts/inputgeneratortest.py");
65   widget.setInputGeneratorScript(scriptFilePath);
66   Molecule mol;
67   mol.addAtom(6).setPosition3d(Avogadro::Vector3(1, 1, 1));
68   mol.addAtom(1).setPosition3d(Avogadro::Vector3(2, 3, 4));
69   mol.addAtom(8).setPosition3d(Avogadro::Vector3(-2, 3, -4));
70   widget.setMolecule(&mol);
71 
72   // Check that the generator is configured properly.
73   EXPECT_EQ(widget.inputGenerator().displayName().toStdString(),
74             std::string("Input Generator Test"));
75 
76   // Verify that appropriate widgets are produced for each parameter type:
77   EXPECT_TRUE(widget.findChild<QComboBox*>("Test StringList") != nullptr);
78   EXPECT_TRUE(widget.findChild<QLineEdit*>("Test String") != nullptr);
79   EXPECT_TRUE(widget.findChild<QSpinBox*>("Test Integer") != nullptr);
80   EXPECT_TRUE(widget.findChild<QCheckBox*>("Test Boolean") != nullptr);
81   EXPECT_TRUE(widget.findChild<FileBrowseWidget*>("Test FilePath") != nullptr);
82 
83   // Set a test filepath
84   FileBrowseWidget* testFilePathWidget(
85     widget.findChild<FileBrowseWidget*>("Test FilePath"));
86   QString testFilePath(AVOGADRO_DATA "/data/ethane.cml");
87   testFilePathWidget->setFileName(testFilePath);
88 
89   // Show the widget so that events are processed
90   widget.show();
91 
92   // Clear out the event queue so that the text edits are updated:
93   flushEvents();
94 
95   // Check the contents of the filepath file:
96   QTextEdit* filePathEdit = widget.findChild<QTextEdit*>("job.testFilePath");
97   QFile testFile(testFilePath);
98   EXPECT_TRUE(testFile.open(QFile::ReadOnly | QFile::Text));
99   QByteArray refData(testFile.readAll());
100   EXPECT_EQ(std::string(refData.constData()),
101             filePathEdit->document()->toPlainText().toStdString());
102 
103   // Check the coords:
104   QTextEdit* coordsEdit = widget.findChild<QTextEdit*>("job.coords");
105   QString coords(coordsEdit->document()->toPlainText());
106   EXPECT_TRUE(
107     coords.contains("C      1.000000 0    1.000000 1    1.000000 1 Carbon"));
108   EXPECT_TRUE(
109     coords.contains("H      2.000000 0    3.000000 1    4.000000 1 Hydrogen"));
110   EXPECT_TRUE(
111     coords.contains("O     -2.000000 0    3.000000 1   -4.000000 1 Oxygen"));
112 
113   // Test the default reset -- trigger a reset, then verify that testFilePath
114   // is cleared (we set it earlier)
115   QPushButton* defaultsButton(widget.findChild<QPushButton*>("defaultsButton"));
116   defaultsButton->click();
117   flushEvents();
118   EXPECT_TRUE(testFilePathWidget->fileName().isEmpty());
119   EXPECT_EQ(filePathEdit->document()->toPlainText().toStdString(),
120             std::string("Reference file '' does not exist."));
121 
122   // Test the autogenerated title:
123   QLineEdit* titleEdit = widget.findChild<QLineEdit*>("Title");
124   EXPECT_EQ(titleEdit->placeholderText().toStdString(),
125             std::string("CHO | Equilibrium Geometry | B3LYP/6-31G(d)"));
126 }
127