File: WorksheetTest.cpp

package info (click to toggle)
labplot 2.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,500 kB
  • sloc: cpp: 241,048; ansic: 6,324; python: 915; xml: 400; yacc: 237; sh: 221; awk: 35; makefile: 11
file content (180 lines) | stat: -rw-r--r-- 6,734 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
	File                 : WorksheetTest.cpp
	Project              : LabPlot
	Description          : Tests for Worksheets
	--------------------------------------------------------------------
	SPDX-FileCopyrightText: 2023 Martin Marmsoler <martin.marmsoler@gmail.com>

 SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "WorksheetTest.h"

#include "backend/core/Project.h"
#include "backend/worksheet/Line.h"
#include "backend/worksheet/TreeModel.h"
#include "backend/worksheet/Worksheet.h"
#include "backend/worksheet/WorksheetPrivate.h"
#include "backend/worksheet/plots/cartesian/CartesianPlot.h"
#include "backend/worksheet/plots/cartesian/XYCurve.h"
#include "frontend/GuiTools.h"

#include <QGraphicsItem>
#include <QPen>

void WorksheetTest::cursorCurveColor() {
	Project project;

	auto* worksheet = new Worksheet(QStringLiteral("Worksheet"));
	project.addChild(worksheet);

	auto* plot = new CartesianPlot(QStringLiteral("plot123"));
	worksheet->addChild(plot);

	auto* curve1 = new XYCurve(QStringLiteral("Curve1"));
	plot->addChild(curve1);
	auto* curve2 = new XYCurve(QStringLiteral("Curve2"));
	plot->addChild(curve2);

	curve2->line()->color();
	QVERIFY(curve1->line()->pen().color() != curve2->line()->pen().color());

	const auto* treemodel = worksheet->cursorModel();

	// Row0: X Value
	// Row1: Plot
	QCOMPARE(treemodel->rowCount(), 2);

	QCOMPARE(treemodel->data(treemodel->index(0, (int)WorksheetPrivate::TreeModelColumn::PLOTNAME), Qt::DisplayRole).toString(), QStringLiteral("X"));
	const auto& plotIndex = treemodel->index(1, (int)WorksheetPrivate::TreeModelColumn::PLOTNAME);

	// Row2: Curve1 (Plot as parent index)
	// Row3: Curve2 (Plot as parent index)
	QCOMPARE(treemodel->rowCount(plotIndex), 2);
	QCOMPARE(treemodel->data(plotIndex, Qt::DisplayRole).toString(), QStringLiteral("plot123"));
	QCOMPARE(treemodel->data(treemodel->index(0, (int)WorksheetPrivate::TreeModelColumn::SIGNALNAME, plotIndex), Qt::DisplayRole).toString(), curve1->name());
	QCOMPARE(treemodel->data(treemodel->index(1, (int)WorksheetPrivate::TreeModelColumn::SIGNALNAME, plotIndex), Qt::DisplayRole).toString(), curve2->name());

	{
		QColor color = curve1->line()->pen().color();
		color.setAlpha(worksheet->d_func()->cursorTreeModelCurveBackgroundAlpha);
		QCOMPARE(treemodel->data(treemodel->index(0, (int)WorksheetPrivate::TreeModelColumn::SIGNALNAME, plotIndex), Qt::BackgroundRole).value<QColor>(),
				 color);
	}
	{
		QColor color = curve2->line()->pen().color();
		color.setAlpha(worksheet->d_func()->cursorTreeModelCurveBackgroundAlpha);
		QCOMPARE(treemodel->data(treemodel->index(1, (int)WorksheetPrivate::TreeModelColumn::SIGNALNAME, plotIndex), Qt::BackgroundRole).value<QColor>(),
				 color);
	}
}

/*!
 * tests the replacement of the file extension when switching between the different formats during the export.
 */
// TODO: testing the code in GuiTools only. move it later to another location once we have tests for the common code in the frontend.
void WorksheetTest::exportReplaceExtension() {
	// from no extension to .pdf, no dot character in the path
	QString extension = QStringLiteral(".pdf");
#if defined(Q_OS_WIN)
	QString path = QStringLiteral("C:\\Users\\user\\test_dir\\Worksheet");
	QCOMPARE(GuiTools::replaceExtension(path, extension), QStringLiteral("C:\\Users\\user\\test_dir\\Worksheet.pdf"));
#else
	QString path = QStringLiteral("/home/user/test_dir/Worksheet");
	QCOMPARE(GuiTools::replaceExtension(path, extension), QStringLiteral("/home/user/test_dir/Worksheet.pdf"));
#endif

	// from .pdf to .svg, no dot character in the path
	extension = QStringLiteral(".svg");
#if defined(Q_OS_WIN)
	path = QStringLiteral("C:\\Users\\user\\test_dir\\Worksheet.pdf");
	QCOMPARE(GuiTools::replaceExtension(path, extension), QStringLiteral("C:\\Users\\user\\test_dir\\Worksheet.svg"));
#else
	path = QStringLiteral("/home/user/test_dir/Worksheet.pdf");
	QCOMPARE(GuiTools::replaceExtension(path, extension), QStringLiteral("/home/user/test_dir/Worksheet.svg"));
#endif

	// from no extension to .pdf, dot character in the path
	extension = QStringLiteral(".pdf");
#if defined(Q_OS_WIN)
	path = QStringLiteral("C:\\Users\\user\\test.dir\\Worksheet");
	QCOMPARE(GuiTools::replaceExtension(path, extension), QStringLiteral("C:\\Users\\user\\test.dir\\Worksheet.pdf"));
#else
	path = QStringLiteral("/home/user/test.dir/Worksheet");
	QCOMPARE(GuiTools::replaceExtension(path, extension), QStringLiteral("/home/user/test.dir/Worksheet.pdf"));
#endif

	// from .pdf to .svg, dot character in the path
	extension = QStringLiteral(".svg");
#if defined(Q_OS_WIN)
	path = QStringLiteral("C:\\Users\\user\\test.dir\\Worksheet.pdf");
	QCOMPARE(GuiTools::replaceExtension(path, extension), QStringLiteral("C:\\Users\\user\\test.dir\\Worksheet.svg"));
#else
	path = QStringLiteral("/home/user/test.dir/Worksheet.pdf");
	QCOMPARE(GuiTools::replaceExtension(path, extension), QStringLiteral("/home/user/test.dir/Worksheet.svg"));
#endif
}

/*!
 * tests the handling of z-values on changes in the child hierarchy.
 */
void WorksheetTest::zValueAfterAddMoveRemove() {
	auto* worksheet = new Worksheet(QStringLiteral("Worksheet"));

	auto* plot1 = new CartesianPlot(QStringLiteral("plot1"));
	worksheet->addChild(plot1);

	auto* plot2 = new CartesianPlot(QStringLiteral("plot2"));
	worksheet->addChild(plot2);

	auto* plot3 = new CartesianPlot(QStringLiteral("plot3"));
	worksheet->addChild(plot3);

	// after the objects were added, the order of children is
	// * plot1
	// * plot2
	// * plot3
	int zValuePlot1 = plot1->graphicsItem()->zValue();
	int zValuePlot2 = plot2->graphicsItem()->zValue();
	int zValuePlot3 = plot3->graphicsItem()->zValue();
	QVERIFY(zValuePlot1 < zValuePlot2);
	QVERIFY(zValuePlot2 < zValuePlot3);

	// move plot2 up
	worksheet->moveChild(plot2, -1);

	// after the move, the order of children is
	// * plot2
	// * plot1
	// * plot3
	zValuePlot1 = plot1->graphicsItem()->zValue();
	zValuePlot2 = plot2->graphicsItem()->zValue();
	zValuePlot3 = plot3->graphicsItem()->zValue();
	QVERIFY(zValuePlot2 < zValuePlot1);
	QVERIFY(zValuePlot1 < zValuePlot3);

	// move plot1 down
	worksheet->moveChild(plot1, 1);

	// after the move, the order of children is
	// * plot2
	// * plot3
	// * plot1
	zValuePlot1 = plot1->graphicsItem()->zValue();
	zValuePlot2 = plot2->graphicsItem()->zValue();
	zValuePlot3 = plot3->graphicsItem()->zValue();
	QVERIFY(zValuePlot2 < zValuePlot3);
	QVERIFY(zValuePlot3 < zValuePlot1);

	// remove plot3
	worksheet->removeChild(plot3);

	// after the remove, the order of children is
	// * plot2
	// * plot1
	zValuePlot1 = plot1->graphicsItem()->zValue();
	zValuePlot2 = plot2->graphicsItem()->zValue();
	QVERIFY(zValuePlot2 < zValuePlot1);
}

QTEST_MAIN(WorksheetTest)