1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32#include "qpalette.h"
33
34class tst_QPalette : public QObject
35{
36 Q_OBJECT
37private Q_SLOTS:
38 void roleValues_data();
39 void roleValues();
40 void resolve();
41 void copySemantics();
42 void moveSemantics();
43 void setBrush();
44};
45
46void tst_QPalette::roleValues_data()
47{
48 QTest::addColumn<int>(name: "role");
49 QTest::addColumn<int>(name: "value");
50
51 QTest::newRow(dataTag: "QPalette::WindowText") << int(QPalette::WindowText) << 0;
52 QTest::newRow(dataTag: "QPalette::Button") << int(QPalette::Button) << 1;
53 QTest::newRow(dataTag: "QPalette::Light") << int(QPalette::Light) << 2;
54 QTest::newRow(dataTag: "QPalette::Midlight") << int(QPalette::Midlight) << 3;
55 QTest::newRow(dataTag: "QPalette::Dark") << int(QPalette::Dark) << 4;
56 QTest::newRow(dataTag: "QPalette::Mid") << int(QPalette::Mid) << 5;
57 QTest::newRow(dataTag: "QPalette::Text") << int(QPalette::Text) << 6;
58 QTest::newRow(dataTag: "QPalette::BrightText") << int(QPalette::BrightText) << 7;
59 QTest::newRow(dataTag: "QPalette::ButtonText") << int(QPalette::ButtonText) << 8;
60 QTest::newRow(dataTag: "QPalette::Base") << int(QPalette::Base) << 9;
61 QTest::newRow(dataTag: "QPalette::Window") << int(QPalette::Window) << 10;
62 QTest::newRow(dataTag: "QPalette::Shadow") << int(QPalette::Shadow) << 11;
63 QTest::newRow(dataTag: "QPalette::Highlight") << int(QPalette::Highlight) << 12;
64 QTest::newRow(dataTag: "QPalette::HighlightedText") << int(QPalette::HighlightedText) << 13;
65 QTest::newRow(dataTag: "QPalette::Link") << int(QPalette::Link) << 14;
66 QTest::newRow(dataTag: "QPalette::LinkVisited") << int(QPalette::LinkVisited) << 15;
67 QTest::newRow(dataTag: "QPalette::AlternateBase") << int(QPalette::AlternateBase) << 16;
68 QTest::newRow(dataTag: "QPalette::NoRole") << int(QPalette::NoRole) << 17;
69 QTest::newRow(dataTag: "QPalette::ToolTipBase") << int(QPalette::ToolTipBase) << 18;
70 QTest::newRow(dataTag: "QPalette::ToolTipText") << int(QPalette::ToolTipText) << 19;
71 QTest::newRow(dataTag: "QPalette::PlaceholderText") << int(QPalette::PlaceholderText) << 20;
72
73 // Change this value as you add more roles.
74 QTest::newRow(dataTag: "QPalette::NColorRoles") << int(QPalette::NColorRoles) << 21;
75}
76
77void tst_QPalette::roleValues()
78{
79 QFETCH(int, role);
80 QFETCH(int, value);
81 QCOMPARE(role, value);
82}
83
84void tst_QPalette::resolve()
85{
86 QPalette p1;
87 p1.setBrush(acr: QPalette::WindowText, abrush: Qt::green);
88 p1.setBrush(acr: QPalette::Button, abrush: Qt::green);
89
90 QVERIFY(p1.isBrushSet(QPalette::Active, QPalette::WindowText));
91 QVERIFY(p1.isBrushSet(QPalette::Active, QPalette::Button));
92
93 QPalette p2;
94 p2.setBrush(acr: QPalette::WindowText, abrush: Qt::red);
95
96 QVERIFY(p2.isBrushSet(QPalette::Active, QPalette::WindowText));
97 QVERIFY(!p2.isBrushSet(QPalette::Active, QPalette::Button));
98
99 QPalette p1ResolvedTo2 = p1.resolve(p2);
100 // p1ResolvedTo2 gets everything from p1 and nothing copied from p2 because
101 // it already has a WindowText. That is two brushes, and to the same value
102 // as p1.
103 QCOMPARE(p1ResolvedTo2, p1);
104 QVERIFY(p1ResolvedTo2.isBrushSet(QPalette::Active, QPalette::WindowText));
105 QCOMPARE(p1.windowText(), p1ResolvedTo2.windowText());
106 QVERIFY(p1ResolvedTo2.isBrushSet(QPalette::Active, QPalette::Button));
107 QCOMPARE(p1.button(), p1ResolvedTo2.button());
108
109 QPalette p2ResolvedTo1 = p2.resolve(p1);
110 // p2ResolvedTo1 gets the WindowText set, and to the same value as the
111 // original p2, however, Button gets set from p1.
112 QVERIFY(p2ResolvedTo1.isBrushSet(QPalette::Active, QPalette::WindowText));
113 QCOMPARE(p2.windowText(), p2ResolvedTo1.windowText());
114 QVERIFY(p2ResolvedTo1.isBrushSet(QPalette::Active, QPalette::Button));
115 QCOMPARE(p1.button(), p2ResolvedTo1.button());
116
117 QVERIFY(p2ResolvedTo1 != p1);
118 QVERIFY(p2ResolvedTo1 != p2);
119}
120
121void tst_QPalette::copySemantics()
122{
123 QPalette src(Qt::red), dst;
124 const QPalette control = src; // copy construction
125 QVERIFY(src != dst);
126 QVERIFY(!src.isCopyOf(dst));
127 QCOMPARE(src, control);
128 QVERIFY(src.isCopyOf(control));
129 dst = src; // copy assignment
130 QCOMPARE(dst, src);
131 QCOMPARE(dst, control);
132 QVERIFY(dst.isCopyOf(src));
133
134 dst = QPalette(Qt::green);
135 QVERIFY(dst != src);
136 QVERIFY(dst != control);
137 QCOMPARE(src, control);
138 QVERIFY(!dst.isCopyOf(src));
139 QVERIFY(src.isCopyOf(control));
140}
141
142void tst_QPalette::moveSemantics()
143{
144 QPalette src(Qt::red), dst;
145 const QPalette control = src;
146 QVERIFY(src != dst);
147 QCOMPARE(src, control);
148 QVERIFY(!dst.isCopyOf(src));
149 QVERIFY(!dst.isCopyOf(control));
150 dst = std::move(src); // move assignment
151 QVERIFY(!dst.isCopyOf(src)); // isCopyOf() works on moved-from palettes, too
152 QVERIFY(dst.isCopyOf(control));
153 QCOMPARE(dst, control);
154 src = control; // check moved-from 'src' can still be assigned to (doesn't crash)
155 QVERIFY(src.isCopyOf(dst));
156 QVERIFY(src.isCopyOf(control));
157 QPalette dst2(std::move(src)); // move construction
158 QVERIFY(!src.isCopyOf(dst));
159 QVERIFY(!src.isCopyOf(dst2));
160 QVERIFY(!src.isCopyOf(control));
161 QCOMPARE(dst2, control);
162 QVERIFY(dst2.isCopyOf(dst));
163 QVERIFY(dst2.isCopyOf(control));
164 // check moved-from 'src' can still be destroyed (doesn't crash)
165}
166
167void tst_QPalette::setBrush()
168{
169 QPalette p(Qt::red);
170 const QPalette q = p;
171 QVERIFY(q.isCopyOf(p));
172
173 // Setting a different brush will detach
174 p.setBrush(cg: QPalette::Disabled, cr: QPalette::Button, brush: Qt::green);
175 QVERIFY(!q.isCopyOf(p));
176 QVERIFY(q != p);
177
178 // Check we only changed what we said we would
179 for (int i = 0; i < QPalette::NColorGroups; i++)
180 for (int j = 0; j < QPalette::NColorRoles; j++) {
181 const auto g = QPalette::ColorGroup(i);
182 const auto r = QPalette::ColorRole(j);
183 const auto b = p.brush(cg: g, cr: r);
184 if (g == QPalette::Disabled && r == QPalette::Button)
185 QCOMPARE(b, QBrush(Qt::green));
186 else
187 QCOMPARE(b, q.brush(g, r));
188 }
189
190 const QPalette pp = p;
191 QVERIFY(pp.isCopyOf(p));
192 // Setting the same brush won't detach
193 p.setBrush(cg: QPalette::Disabled, cr: QPalette::Button, brush: Qt::green);
194 QVERIFY(pp.isCopyOf(p));
195}
196
197QTEST_MAIN(tst_QPalette)
198#include "tst_qpalette.moc"
199

source code of qtbase/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp