1 /*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*******************************************************************************
21 * Includes
22 ******************************************************************************/
23 #include <gtest/gtest.h>
24 #include <librepcb/common/application.h>
25 #include <librepcb/common/fileio/fileutils.h>
26 #include <librepcb/workspace/settings/workspacesettings.h>
27
28 #include <QtCore>
29
30 /*******************************************************************************
31 * Namespace
32 ******************************************************************************/
33 namespace librepcb {
34 namespace workspace {
35 namespace tests {
36
37 /*******************************************************************************
38 * Test Class
39 ******************************************************************************/
40
41 class WorkspaceSettingsTest : public ::testing::Test {};
42
43 /*******************************************************************************
44 * Test Methods
45 ******************************************************************************/
46
TEST_F(WorkspaceSettingsTest,testConstructFromSExpressionV01)47 TEST_F(WorkspaceSettingsTest, testConstructFromSExpressionV01) {
48 // Attention: Do NOT modify this string! It represents the freezed(!) file
49 // format V0.1 and even current versions of LibrePCB must be able to load it!
50 SExpression sexpr = SExpression::parse(
51 "(librepcb_workspace_settings\n"
52 " (user \"Foo Bar\")\n"
53 " (application_locale \"de_CH\")\n"
54 " (default_length_unit micrometers)\n"
55 " (project_autosave_interval 120)\n"
56 " (use_opengl true)\n"
57 " (library_locale_order\n"
58 " (locale \"de_DE\")\n"
59 " )\n"
60 " (library_norm_order\n"
61 " (norm \"IEC 60617\")\n"
62 " )\n"
63 " (repositories\n"
64 " (repository \"https://api.librepcb.org\")\n"
65 " )\n"
66 ")",
67 FilePath());
68 FilePath fp = FilePath::getRandomTempPath().getPathTo("test.lp");
69 FileUtils::writeFile(fp, sexpr.toByteArray());
70
71 WorkspaceSettings obj(fp, Version::fromString("0.1"));
72 EXPECT_EQ("Foo Bar", obj.userName.get());
73 EXPECT_EQ("de_CH", obj.applicationLocale.get());
74 EXPECT_EQ(LengthUnit::micrometers(), obj.defaultLengthUnit.get());
75 EXPECT_EQ(120U, obj.projectAutosaveIntervalSeconds.get());
76 EXPECT_EQ(true, obj.useOpenGl.get());
77 EXPECT_EQ(QStringList{"de_DE"}, obj.libraryLocaleOrder.get());
78 EXPECT_EQ(QStringList{"IEC 60617"}, obj.libraryNormOrder.get());
79 EXPECT_EQ(QList<QUrl>{QUrl("https://api.librepcb.org")},
80 obj.repositoryUrls.get());
81 }
82
TEST_F(WorkspaceSettingsTest,testConstructFromSExpressionCurrentVersion)83 TEST_F(WorkspaceSettingsTest, testConstructFromSExpressionCurrentVersion) {
84 SExpression sexpr = SExpression::parse(
85 "(librepcb_workspace_settings\n"
86 " (user \"Foo Bar\")\n"
87 " (application_locale \"de_CH\")\n"
88 " (default_length_unit micrometers)\n"
89 " (project_autosave_interval 120)\n"
90 " (use_opengl true)\n"
91 " (library_locale_order\n"
92 " (locale \"de_DE\")\n"
93 " )\n"
94 " (library_norm_order\n"
95 " (norm \"IEC 60617\")\n"
96 " )\n"
97 " (repositories\n"
98 " (repository \"https://api.librepcb.org\")\n"
99 " )\n"
100 ")",
101 FilePath());
102 FilePath fp = FilePath::getRandomTempPath().getPathTo("test.lp");
103 FileUtils::writeFile(fp, sexpr.toByteArray());
104
105 WorkspaceSettings obj(fp, qApp->getFileFormatVersion());
106 EXPECT_EQ("Foo Bar", obj.userName.get());
107 EXPECT_EQ("de_CH", obj.applicationLocale.get());
108 EXPECT_EQ(LengthUnit::micrometers(), obj.defaultLengthUnit.get());
109 EXPECT_EQ(120U, obj.projectAutosaveIntervalSeconds.get());
110 EXPECT_EQ(true, obj.useOpenGl.get());
111 EXPECT_EQ(QStringList{"de_DE"}, obj.libraryLocaleOrder.get());
112 EXPECT_EQ(QStringList{"IEC 60617"}, obj.libraryNormOrder.get());
113 EXPECT_EQ(QList<QUrl>{QUrl("https://api.librepcb.org")},
114 obj.repositoryUrls.get());
115 }
116
TEST_F(WorkspaceSettingsTest,testStoreAndLoad)117 TEST_F(WorkspaceSettingsTest, testStoreAndLoad) {
118 FilePath fp = FilePath::getRandomTempPath().getPathTo("test.lp");
119
120 // Store
121 WorkspaceSettings obj1(fp, qApp->getFileFormatVersion());
122 obj1.userName.set("foo bar");
123 obj1.applicationLocale.set("de_CH");
124 obj1.defaultLengthUnit.set(LengthUnit::nanometers());
125 obj1.projectAutosaveIntervalSeconds.set(1234);
126 obj1.useOpenGl.set(!obj1.useOpenGl.get());
127 obj1.libraryLocaleOrder.set({"de_CH", "en_US"});
128 obj1.libraryNormOrder.set({"foo", "bar"});
129 obj1.repositoryUrls.set({QUrl("https://foo"), QUrl("https://bar")});
130 obj1.saveToFile();
131
132 // Load
133 WorkspaceSettings obj2(fp, qApp->getFileFormatVersion());
134 EXPECT_EQ(obj1.userName.get().toStdString(),
135 obj2.userName.get().toStdString());
136 EXPECT_EQ(obj1.applicationLocale.get().toStdString(),
137 obj2.applicationLocale.get().toStdString());
138 EXPECT_EQ(obj1.defaultLengthUnit.get(), obj2.defaultLengthUnit.get());
139 EXPECT_EQ(obj1.projectAutosaveIntervalSeconds.get(),
140 obj2.projectAutosaveIntervalSeconds.get());
141 EXPECT_EQ(obj1.useOpenGl.get(), obj2.useOpenGl.get());
142 EXPECT_EQ(obj1.libraryLocaleOrder.get(), obj2.libraryLocaleOrder.get());
143 EXPECT_EQ(obj1.libraryNormOrder.get(), obj2.libraryNormOrder.get());
144 EXPECT_EQ(obj1.repositoryUrls.get(), obj2.repositoryUrls.get());
145
146 // Check if serialization of loaded settings leads to same file content
147 SExpression sexpr1 = obj1.serializeToDomElement("settings");
148 SExpression sexpr2 = obj2.serializeToDomElement("settings");
149 EXPECT_EQ(sexpr1.toByteArray(), sexpr2.toByteArray());
150 }
151
152 /*******************************************************************************
153 * End of File
154 ******************************************************************************/
155
156 } // namespace tests
157 } // namespace workspace
158 } // namespace librepcb
159