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 | #include <QtTest/QtTest> |
30 | |
31 | #include <qsignalmapper.h> |
32 | |
33 | class tst_QSignalMapper : public QObject |
34 | { |
35 | Q_OBJECT |
36 | private slots: |
37 | void mapped(); |
38 | }; |
39 | |
40 | class QtTestObject : public QObject |
41 | { |
42 | Q_OBJECT |
43 | public slots: |
44 | void myIntSlot(int id); |
45 | void myStringSlot(const QString &str); |
46 | |
47 | signals: |
48 | void mysignal(int); |
49 | |
50 | public: |
51 | void emit_mysignal(int); |
52 | |
53 | int id; |
54 | QString str; |
55 | }; |
56 | |
57 | void QtTestObject::myIntSlot(int id) |
58 | { |
59 | this->id = id; |
60 | } |
61 | |
62 | void QtTestObject::myStringSlot(const QString &str) |
63 | { |
64 | this->str = str; |
65 | } |
66 | |
67 | void QtTestObject::emit_mysignal(int value) |
68 | { |
69 | emit mysignal(value); |
70 | } |
71 | |
72 | void tst_QSignalMapper::mapped() |
73 | { |
74 | QSignalMapper mapper; |
75 | |
76 | QtTestObject target; |
77 | QtTestObject src1; |
78 | QtTestObject src2; |
79 | QtTestObject src3; |
80 | |
81 | connect(sender: &src1, SIGNAL(mysignal(int)), receiver: &mapper, SLOT(map())); |
82 | connect(sender: &src2, SIGNAL(mysignal(int)), receiver: &mapper, SLOT(map())); |
83 | connect(sender: &src3, SIGNAL(mysignal(int)), receiver: &mapper, SLOT(map())); |
84 | |
85 | mapper.setMapping(sender: &src1, id: 7); |
86 | mapper.setMapping(sender: &src1, id: 1); |
87 | mapper.setMapping(sender: &src2, id: 2); |
88 | mapper.setMapping(sender: &src2, text: "two" ); |
89 | mapper.setMapping(sender: &src3, text: "three" ); |
90 | |
91 | connect(sender: &mapper, signal: &QSignalMapper::mappedInt, receiver: &target, slot: &QtTestObject::myIntSlot); |
92 | connect(sender: &mapper, signal: &QSignalMapper::mappedString, receiver: &target, slot: &QtTestObject::myStringSlot); |
93 | |
94 | src1.emit_mysignal(value: 20); |
95 | QCOMPARE(target.id, 1); |
96 | QVERIFY(target.str.isEmpty()); |
97 | |
98 | src2.emit_mysignal(value: 20); |
99 | QCOMPARE(target.id, 2); |
100 | QCOMPARE(target.str, QLatin1String("two" )); |
101 | |
102 | src3.emit_mysignal(value: 20); |
103 | QCOMPARE(target.id, 2); |
104 | QCOMPARE(target.str, QLatin1String("three" )); |
105 | } |
106 | |
107 | QTEST_MAIN(tst_QSignalMapper) |
108 | #include "tst_qsignalmapper.moc" |
109 | |