1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include <QtTest/QtTest>
31#include <QtGui/QImage>
32#include <QtCharts/QChartView>
33#include <QtCharts/QAreaSeries>
34#include <QtCharts/QLineSeries>
35#include <QtCharts/QChartView>
36#include <QtCharts/QValueAxis>
37#include <tst_definitions.h>
38
39QT_CHARTS_USE_NAMESPACE
40
41class tst_QAreaSeries : public QObject
42{
43 Q_OBJECT
44
45public slots:
46 void initTestCase();
47 void cleanupTestCase();
48 void init();
49 void cleanup();
50private slots:
51 void areaSeries();
52 void dynamicEdgeSeriesChange();
53
54protected:
55 QLineSeries *createUpperSeries();
56 QLineSeries *createLowerSeries();
57 void checkPixels(const QColor &upperColor, const QColor &centerColor, const QColor &lowerColor);
58
59 QChartView *m_view;
60 QChart *m_chart;
61 QColor m_brushColor;
62 QColor m_backgroundColor;
63 QValueAxis *m_axisX;
64 QValueAxis *m_axisY;
65};
66
67void tst_QAreaSeries::initTestCase()
68{
69 m_brushColor = QColor("#123456");
70 m_backgroundColor = QColor("#abcfef");
71}
72
73void tst_QAreaSeries::cleanupTestCase()
74{
75 QTest::qWait(ms: 1); // Allow final deleteLaters to run
76}
77
78void tst_QAreaSeries::init()
79{
80 m_view = new QChartView(newQChartOrQPolarChart());
81 m_view->setGeometry(ax: 0, ay: 0, aw: 400, ah: 400);
82 m_chart = m_view->chart();
83 m_chart->setBackgroundBrush(m_backgroundColor);
84 m_chart->legend()->setVisible(false);
85 m_axisX = new QValueAxis;
86 m_axisY = new QValueAxis;
87 m_axisX->setRange(min: 0, max: 4);
88 m_axisY->setRange(min: 0, max: 10);
89 // Hide axes so they don't confuse color checks
90 m_axisX->setVisible(false);
91 m_axisY->setVisible(false);
92 m_chart->addAxis(axis: m_axisX, alignment: Qt::AlignBottom);
93 m_chart->addAxis(axis: m_axisY, alignment: Qt::AlignRight);
94}
95
96void tst_QAreaSeries::cleanup()
97{
98 delete m_view;
99 m_view = 0;
100 m_chart = 0;
101}
102
103void tst_QAreaSeries::areaSeries()
104{
105 QLineSeries *series0 = createUpperSeries();
106 QLineSeries *series1 = createLowerSeries();
107 QAreaSeries *series = new QAreaSeries(series0, series1);
108
109 QCOMPARE(series->brush(), QBrush());
110 QCOMPARE(series->pen(), QPen());
111 QCOMPARE(series->pointsVisible(), false);
112 QCOMPARE(series->pointLabelsVisible(), false);
113 QCOMPARE(series->pointLabelsFormat(), QLatin1String("@xPoint, @yPoint"));
114 QCOMPARE(series->pointLabelsClipping(), true);
115 QCOMPARE(series->pointLabelsColor(), QPen().color());
116 QCOMPARE(series->upperSeries(), series0);
117 QCOMPARE(series->lowerSeries(), series1);
118 QCOMPARE(series->color(), QPen().color());
119 QCOMPARE(series->borderColor(), QPen().color());
120
121 series->setBrush(QBrush(m_brushColor));
122 m_chart->addSeries(series);
123 series->attachAxis(axis: m_axisX);
124 series->attachAxis(axis: m_axisY);
125 m_view->show();
126 QVERIFY(QTest::qWaitForWindowExposed(m_view));
127
128 checkPixels(upperColor: m_backgroundColor, centerColor: m_brushColor, lowerColor: m_backgroundColor);
129}
130
131void tst_QAreaSeries::dynamicEdgeSeriesChange()
132{
133 QAreaSeries *series = new QAreaSeries;
134 series->setBrush(QBrush(m_brushColor));
135
136 m_chart->addSeries(series);
137 series->attachAxis(axis: m_axisX);
138 series->attachAxis(axis: m_axisY);
139 m_view->show();
140 QVERIFY(QTest::qWaitForWindowExposed(m_view));
141
142 checkPixels(upperColor: m_backgroundColor, centerColor: m_backgroundColor, lowerColor: m_backgroundColor);
143
144 QLineSeries *series0 = createUpperSeries();
145 series->setUpperSeries(series0);
146
147 QApplication::processEvents();
148 checkPixels(upperColor: m_backgroundColor, centerColor: m_brushColor, lowerColor: m_brushColor);
149
150 QLineSeries *series1 = createLowerSeries();
151 series->setLowerSeries(series1);
152
153 QApplication::processEvents();
154 checkPixels(upperColor: m_backgroundColor, centerColor: m_brushColor, lowerColor: m_backgroundColor);
155
156 series->setLowerSeries(nullptr);
157
158 QApplication::processEvents();
159 checkPixels(upperColor: m_backgroundColor, centerColor: m_brushColor, lowerColor: m_brushColor);
160
161 series->setUpperSeries(nullptr);
162
163 QApplication::processEvents();
164 checkPixels(upperColor: m_backgroundColor, centerColor: m_backgroundColor, lowerColor: m_backgroundColor);
165}
166
167QLineSeries *tst_QAreaSeries::createUpperSeries()
168{
169 QLineSeries *series = new QLineSeries();
170 *series << QPointF(0, 10) << QPointF(1, 7) << QPointF(2, 6) << QPointF(3, 7) << QPointF(4, 10);
171 return series;
172}
173
174QLineSeries *tst_QAreaSeries::createLowerSeries()
175{
176 QLineSeries *series = new QLineSeries();
177 *series << QPointF(0, 0) << QPointF(1, 3) << QPointF(2, 4) << QPointF(3, 3) << QPointF(4, 0);
178 return series;
179}
180
181void tst_QAreaSeries::checkPixels(const QColor &upperColor,
182 const QColor &centerColor,
183 const QColor &lowerColor)
184{
185 QImage screenGrab = m_view->grab().toImage();
186 QCOMPARE(QColor(screenGrab.pixel(200, 50)), upperColor);
187 QCOMPARE(QColor(screenGrab.pixel(200, 200)), centerColor);
188 QCOMPARE(QColor(screenGrab.pixel(200, 350)), lowerColor);
189}
190
191QTEST_MAIN(tst_QAreaSeries)
192
193#include "tst_qareaseries.moc"
194
195

source code of qtcharts/tests/auto/qareaseries/tst_qareaseries.cpp