1
// SPDX-License-Identifier: LGPL-2.1-or-later
2
/***************************************************************************
3
* Copyright (c) 2023 David Carter <dcarter@david.carter.ca> *
5
* This file is part of FreeCAD. *
7
* FreeCAD is free software: you can redistribute it and/or modify it *
8
* under the terms of the GNU Lesser General Public License as *
9
* published by the Free Software Foundation, either version 2.1 of the *
10
* License, or (at your option) any later version. *
12
* FreeCAD is distributed in the hope that it will be useful, but *
13
* WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15
* Lesser General Public License for more details. *
17
* You should have received a copy of the GNU Lesser General Public *
18
* License along with FreeCAD. If not, see *
19
* <https://www.gnu.org/licenses/>. *
21
**************************************************************************/
23
#include <gtest/gtest.h>
25
#include <Mod/Material/App/PreCompiled.h>
32
#include <App/Application.h>
33
#include <Base/Quantity.h>
34
#include <Gui/MetaTypes.h>
35
#include <src/App/InitApplication.h>
37
#include <Mod/Material/App/MaterialLibrary.h>
38
#include <Mod/Material/App/MaterialManager.h>
39
#include <Mod/Material/App/MaterialValue.h>
40
#include <Mod/Material/App/Model.h>
41
#include <Mod/Material/App/ModelManager.h>
42
#include <Mod/Material/App/ModelUuids.h>
46
class TestMaterialCards : public ::testing::Test {
48
static void SetUpTestSuite() {
49
if (App::Application::GetARGC() == 0) {
50
tests::initApplication();
54
void SetUp() override {
55
// Create a temporary library
56
QString libPath = QDir::tempPath() + QString::fromStdString("/TestMaterialCards");
58
libDir.removeRecursively(); // Clear old run data
59
libDir.mkdir(libPath);
60
_library = std::make_shared<Materials::MaterialLibrary>(QString::fromStdString("Testing"),
62
QString::fromStdString(":/icons/preferences-general.svg"),
64
_modelManager = new Materials::ModelManager();
65
_materialManager = new Materials::MaterialManager();
67
_testMaterialUUID = QString::fromStdString("c6c64159-19c1-40b5-859c-10561f20f979");
70
// void TearDown() override {}
71
Materials::ModelManager* _modelManager;
72
Materials::MaterialManager* _materialManager;
73
std::shared_ptr<Materials::MaterialLibrary> _library;
74
QString _testMaterialUUID;
77
TEST_F(TestMaterialCards, TestCopy)
79
ASSERT_NE(_modelManager, nullptr);
80
ASSERT_TRUE(_library);
81
// FAIL() << "Test library " << _library->getDirectoryPath().toStdString() << "\n";
83
auto testMaterial = _materialManager->getMaterial(_testMaterialUUID);
84
auto newMaterial = std::make_shared<Materials::Material>(*testMaterial);
86
EXPECT_EQ(testMaterial->getUUID(), _testMaterialUUID);
87
EXPECT_EQ(newMaterial->getUUID(), _testMaterialUUID);
90
_materialManager->saveMaterial(_library,
92
QString::fromStdString("/Test Material2.FCMat"),
95
false); // saveInherited
96
EXPECT_EQ(newMaterial->getUUID(), _testMaterialUUID);
97
EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material2"));
99
// Save it when it already exists throwing an error
100
EXPECT_THROW(_materialManager->saveMaterial(_library,
102
QString::fromStdString("/Test Material2.FCMat"),
105
false) // saveInherited
106
, Materials::MaterialExists);
107
EXPECT_EQ(newMaterial->getUUID(), _testMaterialUUID);
108
EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material2"));
110
// Overwrite the existing file
111
_materialManager->saveMaterial(_library,
113
QString::fromStdString("/Test Material2.FCMat"),
116
false);// saveInherited
117
EXPECT_EQ(newMaterial->getUUID(), _testMaterialUUID);
118
EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material2"));
120
// Save to a new file, inheritance mode
121
_materialManager->saveMaterial(_library,
123
QString::fromStdString("/Test Material3.FCMat"),
126
true);// saveInherited
127
EXPECT_EQ(newMaterial->getUUID(), _testMaterialUUID);
128
EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material3"));
130
// Save to a new file, inheritance mode. no copy
131
_materialManager->saveMaterial(_library,
133
QString::fromStdString("/Test Material4.FCMat"),
136
true);// saveInherited
137
EXPECT_NE(newMaterial->getUUID(), _testMaterialUUID);
138
EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material4"));
139
QString uuid1 = newMaterial->getUUID();
141
// Save to a new file, inheritance mode, testing overwrite, new copy
142
_materialManager->saveMaterial(_library,
144
QString::fromStdString("/Test Material5.FCMat"),
147
true);// saveInherited
148
EXPECT_EQ(newMaterial->getUUID(), uuid1);
149
EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material5"));
151
_materialManager->saveMaterial(_library,
153
QString::fromStdString("/Test Material5.FCMat"),
156
true);// saveInherited
157
EXPECT_EQ(newMaterial->getUUID(), uuid1);
158
EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material5"));
160
// Save to a new file, inheritance mode, testing overwrite as no copy, new copy
161
_materialManager->saveMaterial(_library,
163
QString::fromStdString("/Test Material6.FCMat"),
166
true);// saveInherited
167
EXPECT_EQ(newMaterial->getUUID(), uuid1);
168
EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material6"));
170
_materialManager->saveMaterial(_library,
172
QString::fromStdString("/Test Material6.FCMat"),
175
true);// saveInherited
176
EXPECT_EQ(newMaterial->getUUID(), uuid1);
177
EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material6"));
180
TEST_F(TestMaterialCards, TestColumns)
182
ASSERT_NE(_modelManager, nullptr);
183
ASSERT_TRUE(_library);
185
auto testMaterial = _materialManager->getMaterial(_testMaterialUUID);
187
EXPECT_TRUE(testMaterial->hasPhysicalProperty(QString::fromStdString("TestArray2D")));
188
auto array2d = testMaterial->getPhysicalProperty(QString::fromStdString("TestArray2D"))->getMaterialValue();
189
EXPECT_TRUE(array2d);
190
EXPECT_EQ(dynamic_cast<Materials::Material2DArray &>(*array2d).columns(), 2);
192
EXPECT_TRUE(testMaterial->hasPhysicalProperty(QString::fromStdString("TestArray2D3Column")));
193
auto array2d3Column = testMaterial->getPhysicalProperty(QString::fromStdString("TestArray2D3Column"))->getMaterialValue();
194
EXPECT_TRUE(array2d3Column);
195
EXPECT_EQ(dynamic_cast<Materials::Material2DArray &>(*array2d3Column).columns(), 3);
197
EXPECT_TRUE(testMaterial->hasPhysicalProperty(QString::fromStdString("TestArray3D")));
198
auto array3d = testMaterial->getPhysicalProperty(QString::fromStdString("TestArray3D"))->getMaterialValue();
199
EXPECT_TRUE(array3d);
200
EXPECT_EQ(dynamic_cast<Materials::Material3DArray &>(*array3d).columns(), 2);