FreeCAD

Форк
0
/
TestMaterialCards.cpp 
203 строки · 8.9 Кб
1
// SPDX-License-Identifier: LGPL-2.1-or-later
2
/***************************************************************************
3
 *   Copyright (c) 2023 David Carter <dcarter@david.carter.ca>             *
4
 *                                                                         *
5
 *   This file is part of FreeCAD.                                         *
6
 *                                                                         *
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.                       *
11
 *                                                                         *
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.                       *
16
 *                                                                         *
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/>.                                      *
20
 *                                                                         *
21
 **************************************************************************/
22

23
#include <gtest/gtest.h>
24

25
#include <Mod/Material/App/PreCompiled.h>
26
#ifndef _PreComp_
27
#endif
28

29
#include <QMetaType>
30
#include <QString>
31

32
#include <App/Application.h>
33
#include <Base/Quantity.h>
34
#include <Gui/MetaTypes.h>
35
#include <src/App/InitApplication.h>
36

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>
43

44
// clang-format off
45

46
class TestMaterialCards : public ::testing::Test {
47
protected:
48
    static void SetUpTestSuite() {
49
        if (App::Application::GetARGC() == 0) {
50
            tests::initApplication();
51
        }
52
    }
53

54
    void SetUp() override {
55
        // Create a temporary library
56
        QString libPath = QDir::tempPath() + QString::fromStdString("/TestMaterialCards");
57
        QDir libDir(libPath);
58
        libDir.removeRecursively(); // Clear old run data
59
        libDir.mkdir(libPath);
60
        _library = std::make_shared<Materials::MaterialLibrary>(QString::fromStdString("Testing"),
61
                        libPath,
62
                        QString::fromStdString(":/icons/preferences-general.svg"),
63
                        false);
64
        _modelManager = new Materials::ModelManager();
65
        _materialManager = new Materials::MaterialManager();
66

67
        _testMaterialUUID = QString::fromStdString("c6c64159-19c1-40b5-859c-10561f20f979");
68
    }
69

70
    // void TearDown() override {}
71
    Materials::ModelManager* _modelManager;
72
    Materials::MaterialManager* _materialManager;
73
    std::shared_ptr<Materials::MaterialLibrary> _library;
74
    QString _testMaterialUUID;
75
};
76

77
TEST_F(TestMaterialCards, TestCopy)
78
{
79
    ASSERT_NE(_modelManager, nullptr);
80
    ASSERT_TRUE(_library);
81
    // FAIL() << "Test library " << _library->getDirectoryPath().toStdString() << "\n";
82

83
    auto testMaterial = _materialManager->getMaterial(_testMaterialUUID);
84
    auto newMaterial = std::make_shared<Materials::Material>(*testMaterial);
85

86
    EXPECT_EQ(testMaterial->getUUID(), _testMaterialUUID);
87
    EXPECT_EQ(newMaterial->getUUID(), _testMaterialUUID);
88

89
    // Save the material
90
    _materialManager->saveMaterial(_library,
91
                      newMaterial,
92
                      QString::fromStdString("/Test Material2.FCMat"),
93
                      false, // overwrite
94
                      true,  // saveAsCopy
95
                      false); // saveInherited
96
    EXPECT_EQ(newMaterial->getUUID(), _testMaterialUUID);
97
    EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material2"));
98

99
    // Save it when it already exists throwing an error
100
    EXPECT_THROW(_materialManager->saveMaterial(_library,
101
                      newMaterial,
102
                      QString::fromStdString("/Test Material2.FCMat"),
103
                      false, // overwrite
104
                      true,  // saveAsCopy
105
                      false) // saveInherited
106
                      , Materials::MaterialExists);
107
    EXPECT_EQ(newMaterial->getUUID(), _testMaterialUUID);
108
    EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material2"));
109

110
    // Overwrite the existing file
111
    _materialManager->saveMaterial(_library,
112
                      newMaterial,
113
                      QString::fromStdString("/Test Material2.FCMat"),
114
                      true,  // overwrite
115
                      true,  // saveAsCopy
116
                      false);// saveInherited
117
    EXPECT_EQ(newMaterial->getUUID(), _testMaterialUUID);
118
    EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material2"));
119

120
    // Save to a new file, inheritance mode
121
    _materialManager->saveMaterial(_library,
122
                      newMaterial,
123
                      QString::fromStdString("/Test Material3.FCMat"),
124
                      false,  // overwrite
125
                      true,  // saveAsCopy
126
                      true);// saveInherited
127
    EXPECT_EQ(newMaterial->getUUID(), _testMaterialUUID);
128
    EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material3"));
129

130
    // Save to a new file, inheritance mode. no copy
131
    _materialManager->saveMaterial(_library,
132
                      newMaterial,
133
                      QString::fromStdString("/Test Material4.FCMat"),
134
                      false,  // overwrite
135
                      false,  // saveAsCopy
136
                      true);// saveInherited
137
    EXPECT_NE(newMaterial->getUUID(), _testMaterialUUID);
138
    EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material4"));
139
    QString uuid1 = newMaterial->getUUID();
140

141
    // Save to a new file, inheritance mode, testing overwrite, new copy
142
    _materialManager->saveMaterial(_library,
143
                      newMaterial,
144
                      QString::fromStdString("/Test Material5.FCMat"),
145
                      false,  // overwrite
146
                      true,  // saveAsCopy
147
                      true);// saveInherited
148
    EXPECT_EQ(newMaterial->getUUID(), uuid1);
149
    EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material5"));
150

151
    _materialManager->saveMaterial(_library,
152
                      newMaterial,
153
                      QString::fromStdString("/Test Material5.FCMat"),
154
                      true,  // overwrite
155
                      true,  // saveAsCopy
156
                      true);// saveInherited
157
    EXPECT_EQ(newMaterial->getUUID(), uuid1);
158
    EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material5"));
159

160
    // Save to a new file, inheritance mode, testing overwrite as no copy, new copy
161
    _materialManager->saveMaterial(_library,
162
                      newMaterial,
163
                      QString::fromStdString("/Test Material6.FCMat"),
164
                      false,  // overwrite
165
                      true,  // saveAsCopy
166
                      true);// saveInherited
167
    EXPECT_EQ(newMaterial->getUUID(), uuid1);
168
    EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material6"));
169

170
    _materialManager->saveMaterial(_library,
171
                      newMaterial,
172
                      QString::fromStdString("/Test Material6.FCMat"),
173
                      true,  // overwrite
174
                      false,  // saveAsCopy
175
                      true);// saveInherited
176
    EXPECT_EQ(newMaterial->getUUID(), uuid1);
177
    EXPECT_EQ(newMaterial->getName(), QString::fromStdString("Test Material6"));
178
}
179

180
TEST_F(TestMaterialCards, TestColumns)
181
{
182
    ASSERT_NE(_modelManager, nullptr);
183
    ASSERT_TRUE(_library);
184

185
    auto testMaterial = _materialManager->getMaterial(_testMaterialUUID);
186

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);
191

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);
196

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);
201
}
202

203
// clang-format on
204

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.