BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
widgetutils.test.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/tests/testview/widgetutils.test.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "google_test.h"
17 #include "test_utils.h"
18 #include <QDebug>
19 #include <QDir>
20 
21 using namespace ModelView;
22 
23 //! Testing functions from utils.
24 
25 class WidgetUtilsTest : public ::testing::Test {
26 public:
28 };
29 
31 
32 //! Test of WithTildeHomePath function.
33 
35 {
37  auto test_dir = QString::fromStdString(TestUtils::TestOutputDir());
38  EXPECT_EQ(Utils::WithTildeHomePath(test_dir), test_dir);
39  } else {
40  auto home_path = QDir::homePath();
41  auto test_dir = QString::fromStdString(TestUtils::TestOutputDir());
42  auto expected = test_dir.startsWith(home_path)
43  ? QString("~") + test_dir.mid(home_path.size())
44  : test_dir;
45 
46  // "/home/user/build-debug/test_output" -> ~/build-debug/test_output"
47  EXPECT_EQ(Utils::WithTildeHomePath(test_dir).toStdString(), expected.toStdString());
48 
49  EXPECT_EQ(Utils::WithTildeHomePath("/opt/sw/build").toStdString(),
50  std::string("/opt/sw/build"));
51  }
52 }
53 
55 {
56  // untitled and unmodified project
57  EXPECT_EQ(Utils::ProjectWindowTitle(QString(""), false), "Untitled");
58 
59  // untitled and modified project
60  EXPECT_EQ(Utils::ProjectWindowTitle(QString(""), true), "*Untitled");
61 
62  // unmodified project without projectDir
63  EXPECT_EQ(Utils::ProjectWindowTitle(QString("Untitled"), false), "Untitled");
64 
65  // modified project without projectDir
66  EXPECT_EQ(Utils::ProjectWindowTitle(QString("Untitled"), true), "*Untitled");
67 
68  // unmodified project with projectDir
69  EXPECT_EQ(Utils::ProjectWindowTitle(QString("/home/user/project1"), false), "project1");
70 
71  // modified project with projectDir
72  EXPECT_EQ(Utils::ProjectWindowTitle(QString("/home/user/project1"), true), "*project1");
73 }
74 
76 {
77  EXPECT_EQ(Utils::ClickableText("abc", "site.com"), QString("<a href=\"site.com\">abc</a>"));
78 }
79 
81 {
82  using vec_t = std::vector<std::string>;
83  EXPECT_EQ(Utils::toStringList(vec_t()), QStringList());
84  EXPECT_EQ(Utils::toStringList(vec_t({"abc", "cde"})), QStringList({"abc", "cde"}));
85 }
86 
88 {
89  using vec_t = std::vector<std::string>;
90  EXPECT_EQ(Utils::fromStringList(QStringList()), vec_t());
91  EXPECT_EQ(Utils::fromStringList(QStringList({"abc", "cde"})), vec_t({"abc", "cde"}));
92 }
93 
94 TEST_F(WidgetUtilsTest, toFromByteArray)
95 {
96  QStringList expected = QStringList() << "aaa"
97  << "bbb"
98  << "ccc";
99 
100  auto array = Utils::serialize(expected);
101  EXPECT_EQ(Utils::deserialize(array), expected);
102 }
Testing functions from utils.
Defines class CLASS?
MVVM_MODEL_EXPORT std::string ProjectWindowTitle(const ProjectInterface &project)
Returns a MainWindow title for given project.
MVVM_VIEW_EXPORT QStringList deserialize(const QByteArray &byteArray)
Converts byte array to vector of strings.
MVVM_VIEW_EXPORT QString WithTildeHomePath(const QString &path)
Returns a string where Linux path to the file is striped using '~/'.
Definition: widgetutils.cpp:79
MVVM_VIEW_EXPORT bool IsWindowsHost()
Returns true if it is Windows.
Definition: widgetutils.cpp:52
MVVM_VIEW_EXPORT QByteArray serialize(const QStringList &data)
Converts vector of strings to byte array.
MVVM_VIEW_EXPORT QStringList toStringList(const std::vector< std::string > &vec)
Converts vector of strings to QStringList.
MVVM_VIEW_EXPORT QString ClickableText(const QString &text, const QString &tag={})
Returns text wrapped into 'href' tag to provide clickable links in QLabel.
MVVM_VIEW_EXPORT std::vector< std::string > fromStringList(const QStringList &string_list)
Converts vector of strings to QStringList.
MVVM_VIEW_EXPORT QString ProjectWindowTitle(const QString &project_dir, bool is_modified)
Returns a title composed from last part of project path, and is_modified flag.
Definition: widgetutils.cpp:98
materialitems.h Collection of materials to populate MaterialModel.
std::string TestOutputDir()
Returns full path to the main test folder, as defined by CMake at compile time.
Definition: test_utils.cpp:35
Defines class CLASS?
Defines class CLASS?
TEST_F(WidgetUtilsTest, WithTildeHomePath)
Test of WithTildeHomePath function.