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#include <QtGui/QtGui>
32#include <QtWidgets/QColorDialog>
33
34QT_FORWARD_DECLARE_CLASS(QtTestEventThread)
35
36class tst_QColorDialog : public QObject
37{
38 Q_OBJECT
39public:
40 tst_QColorDialog();
41 virtual ~tst_QColorDialog();
42
43public slots:
44 void postKeyReturn();
45 void testGetRgba();
46 void testNativeActiveModalWidget();
47
48private slots:
49 void defaultOkButton();
50 void native_activeModalWidget();
51 void task247349_alpha();
52 void QTBUG_43548_initialColor();
53};
54
55class TestNativeDialog : public QColorDialog
56{
57 Q_OBJECT
58public:
59 QWidget *m_activeModalWidget;
60
61 TestNativeDialog(QWidget *parent = 0)
62 : QColorDialog(parent), m_activeModalWidget(0)
63 {
64 QTimer::singleShot(msec: 1, receiver: this, SLOT(test_activeModalWidgetSignal()));
65 }
66
67public slots:
68 void test_activeModalWidgetSignal()
69 {
70 m_activeModalWidget = qApp->activeModalWidget();
71 }
72};
73
74tst_QColorDialog::tst_QColorDialog()
75{
76}
77
78tst_QColorDialog::~tst_QColorDialog()
79{
80}
81
82void tst_QColorDialog::testNativeActiveModalWidget()
83{
84 // Check that QApplication::activeModalWidget retruns the
85 // color dialog when it is executing, even when using a native
86 // dialog:
87#if defined(Q_OS_LINUX)
88 QSKIP("This test crashes sometimes. Although rarely, but it happens. See QTBUG-50842.");
89#endif
90 TestNativeDialog d;
91 QTimer::singleShot(msec: 1000, receiver: &d, SLOT(hide()));
92 d.exec();
93 QCOMPARE(&d, d.m_activeModalWidget);
94}
95
96void tst_QColorDialog::native_activeModalWidget()
97{
98 QTimer::singleShot(msec: 3000, qApp, SLOT(quit()));
99 QTimer::singleShot(msec: 0, receiver: this, SLOT(testNativeActiveModalWidget()));
100 qApp->exec();
101}
102
103void tst_QColorDialog::postKeyReturn() {
104 QWidgetList list = QApplication::topLevelWidgets();
105 for (int i=0; i<list.count(); ++i) {
106 QColorDialog *dialog = qobject_cast<QColorDialog *>(object: list[i]);
107 if (dialog) {
108 QTest::keyClick( widget: list[i], key: Qt::Key_Return, modifier: Qt::NoModifier );
109 return;
110 }
111 }
112}
113
114void tst_QColorDialog::testGetRgba()
115{
116#ifdef Q_OS_MAC
117 QEXPECT_FAIL("", "Sending QTest::keyClick to OSX color dialog helper fails, see QTBUG-24320", Continue);
118#endif
119 QTimer::singleShot(interval: 500, receiver: this, slot: &tst_QColorDialog::postKeyReturn);
120 const QColor color = QColorDialog::getColor(initial: QColor::fromRgba(rgba: 0xffffffff), parent: nullptr, title: QString(),
121 options: QColorDialog::ShowAlphaChannel);
122 QVERIFY(color.isValid());
123}
124
125void tst_QColorDialog::defaultOkButton()
126{
127#if defined(Q_OS_LINUX)
128 QSKIP("This test crashes sometimes. Although rarely, but it happens. See QTBUG-50842.");
129#endif
130 QTimer::singleShot(msec: 4000, qApp, SLOT(quit()));
131 QTimer::singleShot(msec: 0, receiver: this, SLOT(testGetRgba()));
132 qApp->exec();
133}
134
135void tst_QColorDialog::task247349_alpha()
136{
137 QColorDialog dialog;
138 dialog.setOption(option: QColorDialog::ShowAlphaChannel, on: true);
139 int alpha = 0x17;
140 dialog.setCurrentColor(QColor(0x01, 0x02, 0x03, alpha));
141 QCOMPARE(alpha, dialog.currentColor().alpha());
142 QCOMPARE(alpha, qAlpha(dialog.currentColor().rgba()));
143}
144
145void tst_QColorDialog::QTBUG_43548_initialColor()
146{
147 QColorDialog dialog;
148 dialog.setOption(option: QColorDialog::DontUseNativeDialog);
149 dialog.setCurrentColor(QColor(Qt::red));
150 QColor a(Qt::red);
151 QCOMPARE(a, dialog.currentColor());
152}
153
154QTEST_MAIN(tst_QColorDialog)
155#include "tst_qcolordialog.moc"
156

source code of qtbase/tests/auto/widgets/dialogs/qcolordialog/tst_qcolordialog.cpp