BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
projectmanager.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/testmodel/projectmanager.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 "folderbasedtest.h"
16 #include "google_test.h"
21 #include "mvvm/utils/fileutils.h"
22 #include "test_utils.h"
23 #include <cctype>
24 
25 using namespace ModelView;
26 
27 namespace {
28 const std::string samplemodel_name = "samplemodel";
29 
30 } // namespace
31 
32 //! Tests for ProjectManager class.
33 
35 public:
37  : FolderBasedTest("test_ProjectManager")
38  , sample_model(std::make_unique<ModelView::SessionModel>(samplemodel_name))
39  {
40  }
42 
43  std::vector<SessionModel*> models() const { return {sample_model.get()}; };
44 
46  {
47  ProjectContext result;
48  result.m_models_callback = [this]() { return models(); };
49  return result;
50  }
51 
52  std::unique_ptr<SessionModel> sample_model;
53 };
54 
56 
57 //! Initial state of ProjectManager. Project created, and not-saved.
58 
59 TEST_F(ProjectManagerTest, initialState)
60 {
61  ProjectManager manager(createContext());
62  EXPECT_TRUE(manager.currentProjectDir().empty());
63  EXPECT_FALSE(manager.isModified());
64 }
65 
66 // ----------------------------------------------------------------------------
67 // Untitled, empty project
68 // ----------------------------------------------------------------------------
69 
70 //! Creating new project. Use untitled+empty project as a starting point.
71 //! Should succeed, since old empty project doesn't need to be saved.
72 
73 TEST_F(ProjectManagerTest, untitledEmptyNew)
74 {
75  ProjectManager manager(createContext());
76 
77  const auto project_dir = createEmptyDir("Project_untitledEmptyNew");
78  EXPECT_TRUE(manager.createNewProject(project_dir));
79 
80  EXPECT_EQ(manager.currentProjectDir(), project_dir);
81  EXPECT_FALSE(manager.isModified());
82 
83  // project directory should contain a json file with the model
84  auto model_json = Utils::join(project_dir, samplemodel_name + ".json");
85  EXPECT_TRUE(Utils::exists(model_json));
86 }
87 
88 //! Saving of new project. Use untitled+empty project as a starting point.
89 //! Should fail since project directory is not defined.
90 
91 TEST_F(ProjectManagerTest, untitledEmptySave)
92 {
93  ProjectManager manager(createContext());
94  EXPECT_FALSE(manager.saveCurrentProject());
95  EXPECT_FALSE(manager.isModified());
96 }
97 
98 //! Saving of new project. Use untitled+empty project as a starting point.
99 //! Should be saved, file sould appear on disk.
100 
101 TEST_F(ProjectManagerTest, untitledEmptySaveAs)
102 {
103  ProjectManager manager(createContext());
104 
105  const auto project_dir = createEmptyDir("Project_untitledEmptySaveAs");
106  EXPECT_TRUE(manager.saveProjectAs(project_dir));
107  EXPECT_FALSE(manager.isModified());
108 
109  // project directory should contain a json file with the model
110  auto model_json = Utils::join(project_dir, samplemodel_name + ".json");
111  EXPECT_TRUE(Utils::exists(model_json));
112 }
113 
114 // ----------------------------------------------------------------------------
115 // Untitled, modified
116 // ----------------------------------------------------------------------------
117 
118 //! Creating new project. Use untitled+modified project as a starting point.
119 //! Should fail, since modified old project will prevent creation of the new one.
120 
121 TEST_F(ProjectManagerTest, untitledModifiedNew)
122 {
123  ProjectManager manager(createContext());
124 
125  // modifying the model
126  sample_model->insertItem<PropertyItem>();
127 
128  EXPECT_TRUE(manager.isModified());
129 
130  const auto project_dir = createEmptyDir("Project_untitledModifiedNew");
131  EXPECT_FALSE(manager.createNewProject(project_dir));
132 
133  EXPECT_TRUE(manager.currentProjectDir().empty());
134  EXPECT_TRUE(manager.isModified());
135 
136  // project directory should be empty
137  auto model_json = Utils::join(project_dir, samplemodel_name + ".json");
138  EXPECT_FALSE(Utils::exists(model_json));
139 }
140 
141 //! Saving of new project. Use untitled+modified project as a starting point.
142 //! Should fail since project directory is not defined.
143 
144 TEST_F(ProjectManagerTest, untitledModifiedSave)
145 {
146  ProjectManager manager(createContext());
147  // modifying the model
148  sample_model->insertItem<PropertyItem>();
149 
150  EXPECT_FALSE(manager.saveCurrentProject());
151  EXPECT_TRUE(manager.isModified());
152 }
153 
154 //! Saving of new project. Use untitled+empty project as a starting point.
155 //! Should be saved, file sould appear on disk.
156 
157 TEST_F(ProjectManagerTest, untitledModifiedSaveAs)
158 {
159  ProjectManager manager(createContext());
160  sample_model->insertItem<PropertyItem>(); // modifying the model
161 
162  const auto project_dir = createEmptyDir("Project_untitledModifiedSaveAs");
163  EXPECT_TRUE(manager.saveProjectAs(project_dir));
164  EXPECT_FALSE(manager.isModified());
165 
166  // project directory should contain a json file with the model
167  auto model_json = Utils::join(project_dir, samplemodel_name + ".json");
168  EXPECT_TRUE(Utils::exists(model_json));
169 }
170 
171 // ----------------------------------------------------------------------------
172 // Titled, unmodified
173 // ----------------------------------------------------------------------------
174 
175 //! Creating new project. Use titled+unmodified project as a starting point.
176 //! Should succeed, since old empty project doesn't need to be saved.
177 
178 TEST_F(ProjectManagerTest, titledUnmodifiedNew)
179 {
180  ProjectManager manager(createContext());
181 
182  const auto project_dir = createEmptyDir("Project_titledUnmodifiedNew");
183  EXPECT_TRUE(manager.saveProjectAs(project_dir));
184  EXPECT_EQ(manager.currentProjectDir(), project_dir);
185 
186  const auto project_dir2 = createEmptyDir("Project_titledUnmodifiedNew2");
187  EXPECT_TRUE(manager.createNewProject(project_dir2));
188 
189  EXPECT_EQ(manager.currentProjectDir(), project_dir2);
190  EXPECT_FALSE(manager.isModified());
191 
192  // project directory should contain a json file with the model
193  auto model_json = Utils::join(project_dir2, samplemodel_name + ".json");
194  EXPECT_TRUE(Utils::exists(model_json));
195 }
196 
197 // ----------------------------------------------------------------------------
198 // Titled, modified
199 // ----------------------------------------------------------------------------
200 
201 //! Saving of new project. Use titled+modified project as a starting point.
202 //! Should succeed.
203 
204 TEST_F(ProjectManagerTest, titledModifiedSave)
205 {
206  ProjectManager manager(createContext());
207 
208  const auto project_dir = createEmptyDir("Project_titledModifiedSave");
209  EXPECT_TRUE(manager.saveProjectAs(project_dir));
210  EXPECT_EQ(manager.currentProjectDir(), project_dir);
211 
212  // modifying the model
213  sample_model->insertItem<PropertyItem>();
214 
215  EXPECT_TRUE(manager.saveCurrentProject());
216  EXPECT_FALSE(manager.isModified());
217 }
218 
219 // ----------------------------------------------------------------------------
220 // Callbacks
221 // ----------------------------------------------------------------------------
222 
224 {
225  int project_modified_count{0};
226 
227  auto context = createContext();
228  context.m_modified_callback = [&project_modified_count]() { ++project_modified_count; };
229 
230  ProjectManager manager(context);
231 
232  EXPECT_EQ(project_modified_count, 0);
233 
234  // saving the project
235  const auto project_dir = createEmptyDir("Project_callback");
236  EXPECT_TRUE(manager.saveProjectAs(project_dir));
237  EXPECT_EQ(manager.currentProjectDir(), project_dir);
238  EXPECT_EQ(project_modified_count, 0);
239 
240  // modifying the model
241  sample_model->insertItem<PropertyItem>();
242  EXPECT_EQ(project_modified_count, 1);
243  EXPECT_TRUE(manager.isModified());
244 
245  // modifying the model second time
246  sample_model->insertItem<PropertyItem>();
247  EXPECT_EQ(project_modified_count, 1); // do not sum up
248  EXPECT_TRUE(manager.isModified());
249 
250  EXPECT_TRUE(manager.saveCurrentProject());
251  EXPECT_FALSE(manager.isModified());
252  EXPECT_EQ(project_modified_count, 1);
253 }
Convenience class which creates a directory on disk for test content.
Responsible for handling new/save/save-as/close Project logic, where the Project represents a collect...
bool saveProjectAs(const std::string &dirname) override
Saves the project under a given directory, returns true in the case of success.
bool saveCurrentProject() override
Saves current project, returns 'true' in the case of success.
bool createNewProject(const std::string &dirname) override
Creates a new project, returns 'true' in the case of success.
std::string currentProjectDir() const override
Returns current project directory.
bool isModified() const override
Returns true if project was modified since last save.
Item to carry concrete editable entity (e.g.
Definition: propertyitem.h:27
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
Tests for ProjectManager class.
std::unique_ptr< SessionModel > sample_model
ProjectContext createContext()
std::vector< SessionModel * > models() const
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
MVVM_MODEL_EXPORT std::string join(const std::string &part1, const std::string &part2)
Joins two path elements into the path.
Definition: fileutils.cpp:37
MVVM_MODEL_EXPORT bool exists(const std::string &fileName)
Returns true if file exists.
Definition: fileutils.cpp:27
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
Defines class CLASS?
TEST_F(ProjectManagerTest, initialState)
Initial state of ProjectManager. Project created, and not-saved.
Defines class CLASS?
Defines class CLASS?
Provides necessary information for Project construction.
Definition: project_types.h:32
models_callback_t m_models_callback
Definition: project_types.h:42
Defines class CLASS?