-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QPainterStateGuard: make movable, add test coverage
There's no real reason not to be able to move a state guard. Since a moved-from QPainterStateGuard must not have a restore- level count, we have to implement the move-constructor explicily. Add a test case, which verifies that we don't end up with an un-balanced save/restore count, and that verifies that we would trigger the Q_ASSERT if we do. Address comment from header review, amends 9ecf47a. Task-number: QTBUG-132090 Pick-to: 6.9 Change-Id: I1db135bf48c0fa0a7bac4fdae7b7263c356b5eb6 Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
- Loading branch information
Showing
5 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (C) 2024 The Qt Company Ltd. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
##################################################################### | ||
## tst_qpainterstateguard Test: | ||
##################################################################### | ||
|
||
if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) | ||
cmake_minimum_required(VERSION 3.16) | ||
project(tst_qpainterstateguard LANGUAGES CXX) | ||
find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) | ||
endif() | ||
|
||
qt_internal_add_test(tst_qpainterstateguard | ||
SOURCES | ||
tst_qpainterstateguard.cpp | ||
LIBRARIES | ||
Qt::Gui | ||
) |
69 changes: 69 additions & 0 deletions
69
tests/auto/gui/painting/qpainterstateguard/tst_qpainterstateguard.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (C) 2024 The Qt Company Ltd. | ||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only | ||
|
||
#define Q_ASSERT(cond) ((cond) ? static_cast<void>(0) : qCritical(#cond)) | ||
|
||
#include <QTest> | ||
|
||
#include <QtGui/qpixmap.h> | ||
#include <QtGui/qpainter.h> | ||
#include <QtGui/qpainterstateguard.h> | ||
|
||
class tst_QPainterStateGuard : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
private slots: | ||
void basics_data(); | ||
void basics(); | ||
}; | ||
|
||
void tst_QPainterStateGuard::basics_data() | ||
{ | ||
QTest::addColumn<QPainterStateGuard::InitialState>("initialState"); | ||
QTest::addColumn<bool>("expectWarning"); | ||
|
||
QTest::addRow("Save") << QPainterStateGuard::InitialState::Save << false; | ||
QTest::addRow("NoSave") << QPainterStateGuard::InitialState::NoSave << true; | ||
} | ||
|
||
void tst_QPainterStateGuard::basics() | ||
{ | ||
QFETCH(const QPainterStateGuard::InitialState, initialState); | ||
QFETCH(const bool, expectWarning); | ||
|
||
if (expectWarning) { | ||
QTest::ignoreMessage(QtCriticalMsg, "m_level > 0"); | ||
QTest::ignoreMessage(QtWarningMsg, "QPainter::restore: Unbalanced save/restore"); | ||
} else { | ||
QTest::failOnWarning("QPainter::restore: Unbalanced save/restore"); | ||
} | ||
QPixmap pixmap(100, 100); | ||
QPainter painter(&pixmap); | ||
const QPen defaultPen = painter.pen(); | ||
|
||
{ | ||
auto makeGuard = [initialState](QPainter *p) -> QPainterStateGuard { | ||
return QPainterStateGuard(p, initialState); | ||
}; | ||
QPainterStateGuard guard = makeGuard(&painter); | ||
|
||
painter.setPen(Qt::red); | ||
QCOMPARE(painter.pen().color(), Qt::red); | ||
|
||
QPainterStateGuard guard2 = std::move(guard); | ||
|
||
QCOMPARE(painter.pen().color(), Qt::red); | ||
|
||
guard2.restore(); | ||
} | ||
|
||
if (expectWarning) | ||
QCOMPARE_NE(painter.pen(), defaultPen); | ||
else | ||
QCOMPARE(painter.pen(), defaultPen); | ||
} | ||
|
||
QTEST_MAIN(tst_QPainterStateGuard) | ||
|
||
#include "tst_qpainterstateguard.moc" |