| 1 |
// Copyright (C) 2017 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtTest/QtTest> |
| 5 |
#include <QtGui/QImage> |
| 6 |
#include <QtCharts/QChartView> |
| 7 |
#include <QtCharts/QAreaSeries> |
| 8 |
#include <QtCharts/QLineSeries> |
| 9 |
#include <QtCharts/QChartView> |
| 10 |
#include <QtCharts/QValueAxis> |
| 11 |
#include <tst_definitions.h> |
| 12 |
|
| 13 |
QT_USE_NAMESPACE |
| 14 |
|
| 15 |
class tst_QAreaSeries : public QObject |
| 16 |
{ |
| 17 |
Q_OBJECT |
| 18 |
|
| 19 |
public slots: |
| 20 |
void initTestCase(); |
| 21 |
void cleanupTestCase(); |
| 22 |
void init(); |
| 23 |
void cleanup(); |
| 24 |
private slots: |
| 25 |
void areaSeries(); |
| 26 |
void dynamicEdgeSeriesChange(); |
| 27 |
|
| 28 |
protected: |
| 29 |
QLineSeries *createUpperSeries(); |
| 30 |
QLineSeries *createLowerSeries(); |
| 31 |
void checkPixels(const QColor &upperColor, const QColor ¢erColor, const QColor &lowerColor); |
| 32 |
|
| 33 |
QChartView *m_view; |
| 34 |
QChart *m_chart; |
| 35 |
QColor m_brushColor; |
| 36 |
QColor m_backgroundColor; |
| 37 |
QValueAxis *m_axisX; |
| 38 |
QValueAxis *m_axisY; |
| 39 |
}; |
| 40 |
|
| 41 |
void tst_QAreaSeries::initTestCase() |
| 42 |
{ |
| 43 |
m_brushColor = QColor("#123456"); |
| 44 |
m_backgroundColor = QColor("#abcfef"); |
| 45 |
} |
| 46 |
|
| 47 |
void tst_QAreaSeries::cleanupTestCase() |
| 48 |
{ |
| 49 |
QTest::qWait(1); // Allow final deleteLaters to run |
| 50 |
} |
| 51 |
|
| 52 |
void tst_QAreaSeries::init() |
| 53 |
{ |
| 54 |
m_view = new QChartView(newQChartOrQPolarChart()); |
| 55 |
m_view->setGeometry(0, 0, 400, 400); |
| 56 |
m_chart = m_view->chart(); |
| 57 |
m_chart->setBackgroundBrush(m_backgroundColor); |
| 58 |
m_chart->legend()->setVisible(false); |
| 59 |
m_axisX = new QValueAxis; |
| 60 |
m_axisY = new QValueAxis; |
| 61 |
m_axisX->setRange(0, 4); |
| 62 |
m_axisY->setRange(0, 10); |
| 63 |
// Hide axes so they don't confuse color checks |
| 64 |
m_axisX->setVisible(false); |
| 65 |
m_axisY->setVisible(false); |
| 66 |
m_chart->addAxis(m_axisX, Qt::AlignBottom); |
| 67 |
m_chart->addAxis(m_axisY, Qt::AlignRight); |
| 68 |
} |
| 69 |
|
| 70 |
void tst_QAreaSeries::cleanup() |
| 71 |
{ |
| 72 |
delete m_view; |
| 73 |
m_view = 0; |
| 74 |
m_chart = 0; |
| 75 |
} |
| 76 |
|
| 77 |
void tst_QAreaSeries::areaSeries() |
| 78 |
{ |
| 79 |
QLineSeries *series0 = createUpperSeries(); |
| 80 |
QLineSeries *series1 = createLowerSeries(); |
| 81 |
QAreaSeries *series = new QAreaSeries(series0, series1); |
| 82 |
|
| 83 |
QCOMPARE(series->brush(), QBrush()); |
| 84 |
QCOMPARE(series->pen(), QPen()); |
| 85 |
QCOMPARE(series->pointsVisible(), false); |
| 86 |
QCOMPARE(series->pointLabelsVisible(), false); |
| 87 |
QCOMPARE(series->pointLabelsFormat(), QLatin1String("@xPoint, @yPoint")); |
| 88 |
QCOMPARE(series->pointLabelsClipping(), true); |
| 89 |
QCOMPARE(series->pointLabelsColor(), QPen().color()); |
| 90 |
QCOMPARE(series->upperSeries(), series0); |
| 91 |
QCOMPARE(series->lowerSeries(), series1); |
| 92 |
QCOMPARE(series->color(), QPen().color()); |
| 93 |
QCOMPARE(series->borderColor(), QPen().color()); |
| 94 |
|
| 95 |
series->setBrush(QBrush(m_brushColor)); |
| 96 |
m_chart->addSeries(series); |
| 97 |
series->attachAxis(m_axisX); |
| 98 |
series->attachAxis(m_axisY); |
| 99 |
m_view->show(); |
| 100 |
QVERIFY(QTest::qWaitForWindowExposed(m_view)); |
| 101 |
|
| 102 |
checkPixels(m_backgroundColor, m_brushColor, m_backgroundColor); |
| 103 |
} |
| 104 |
|
| 105 |
void tst_QAreaSeries::dynamicEdgeSeriesChange() |
| 106 |
{ |
| 107 |
QAreaSeries *series = new QAreaSeries; |
| 108 |
series->setBrush(QBrush(m_brushColor)); |
| 109 |
|
| 110 |
m_chart->addSeries(series); |
| 111 |
series->attachAxis(m_axisX); |
| 112 |
series->attachAxis(m_axisY); |
| 113 |
m_view->show(); |
| 114 |
QVERIFY(QTest::qWaitForWindowExposed(m_view)); |
| 115 |
|
| 116 |
checkPixels(m_backgroundColor, m_backgroundColor, m_backgroundColor); |
| 117 |
|
| 118 |
QLineSeries *series0 = createUpperSeries(); |
| 119 |
series->setUpperSeries(series0); |
| 120 |
|
| 121 |
QApplication::processEvents(); |
| 122 |
checkPixels(m_backgroundColor, m_brushColor, m_brushColor); |
| 123 |
|
| 124 |
QLineSeries *series1 = createLowerSeries(); |
| 125 |
series->setLowerSeries(series1); |
| 126 |
|
| 127 |
QApplication::processEvents(); |
| 128 |
checkPixels(m_backgroundColor, m_brushColor, m_backgroundColor); |
| 129 |
|
| 130 |
series->setLowerSeries(nullptr); |
| 131 |
|
| 132 |
QApplication::processEvents(); |
| 133 |
checkPixels(m_backgroundColor, m_brushColor, m_brushColor); |
| 134 |
|
| 135 |
series->setUpperSeries(nullptr); |
| 136 |
|
| 137 |
QApplication::processEvents(); |
| 138 |
checkPixels(m_backgroundColor, m_backgroundColor, m_backgroundColor); |
| 139 |
} |
| 140 |
|
| 141 |
QLineSeries *tst_QAreaSeries::createUpperSeries() |
| 142 |
{ |
| 143 |
QLineSeries *series = new QLineSeries(); |
| 144 |
*series << QPointF(0, 10) << QPointF(1, 7) << QPointF(2, 6) << QPointF(3, 7) << QPointF(4, 10); |
| 145 |
return series; |
| 146 |
} |
| 147 |
|
| 148 |
QLineSeries *tst_QAreaSeries::createLowerSeries() |
| 149 |
{ |
| 150 |
QLineSeries *series = new QLineSeries(); |
| 151 |
*series << QPointF(0, 0) << QPointF(1, 3) << QPointF(2, 4) << QPointF(3, 3) << QPointF(4, 0); |
| 152 |
return series; |
| 153 |
} |
| 154 |
|
| 155 |
void tst_QAreaSeries::checkPixels(const QColor &upperColor, |
| 156 |
const QColor ¢erColor, |
| 157 |
const QColor &lowerColor) |
| 158 |
{ |
| 159 |
QImage screenGrab = m_view->grab().toImage(); |
| 160 |
QCOMPARE(QColor(screenGrab.pixel(200, 50)), upperColor); |
| 161 |
QCOMPARE(QColor(screenGrab.pixel(200, 200)), centerColor); |
| 162 |
QCOMPARE(QColor(screenGrab.pixel(200, 350)), lowerColor); |
| 163 |
} |
| 164 |
|
| 165 |
QTEST_MAIN(tst_QAreaSeries) |
| 166 |
|
| 167 |
#include "tst_qareaseries.moc" |
| 168 |
|