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 <QtWidgets/QWidget> |
30 | #include <QtWidgets/QPushButton> |
31 | #include <QtTest/QtTest> |
32 | |
33 | QT_BEGIN_NAMESPACE |
34 | namespace QtSharedPointer { |
35 | Q_CORE_EXPORT void internalSafetyCheckCleanCheck(); |
36 | } |
37 | QT_END_NAMESPACE |
38 | |
39 | class tst_QSharedPointer_and_QWidget: public QObject |
40 | { |
41 | Q_OBJECT |
42 | private slots: |
43 | void weak_externalDelete(); |
44 | void weak_parentDelete(); |
45 | void weak_parentDelete_setParent(); |
46 | |
47 | void strong_weak(); |
48 | |
49 | void strong_sharedptrDelete(); |
50 | |
51 | public slots: |
52 | void cleanup() { safetyCheck(); } |
53 | |
54 | public: |
55 | inline void safetyCheck() |
56 | { |
57 | #ifdef QT_BUILD_INTERNAL |
58 | QtSharedPointer::internalSafetyCheckCleanCheck(); |
59 | #endif |
60 | } |
61 | }; |
62 | |
63 | void tst_QSharedPointer_and_QWidget::weak_externalDelete() |
64 | { |
65 | QWidget *w = new QWidget; |
66 | QPointer<QWidget> ptr = w; |
67 | |
68 | QVERIFY(!ptr.isNull()); |
69 | |
70 | delete w; |
71 | QVERIFY(ptr.isNull()); |
72 | } |
73 | |
74 | void tst_QSharedPointer_and_QWidget::weak_parentDelete() |
75 | { |
76 | QWidget *parent = new QWidget; |
77 | QWidget *w = new QWidget(parent); |
78 | QPointer<QWidget> ptr = w; |
79 | |
80 | QVERIFY(!ptr.isNull()); |
81 | |
82 | delete parent; |
83 | QVERIFY(ptr.isNull()); |
84 | } |
85 | |
86 | void tst_QSharedPointer_and_QWidget::weak_parentDelete_setParent() |
87 | { |
88 | QWidget *parent = new QWidget; |
89 | QWidget *w = new QWidget; |
90 | QPointer<QWidget> ptr = w; |
91 | w->setParent(parent); |
92 | |
93 | QVERIFY(!ptr.isNull()); |
94 | |
95 | delete parent; |
96 | QVERIFY(ptr.isNull()); |
97 | } |
98 | |
99 | // -- mixed -- |
100 | |
101 | void tst_QSharedPointer_and_QWidget::strong_weak() |
102 | { |
103 | QSharedPointer<QWidget> ptr(new QWidget); |
104 | QPointer<QWidget> weak = ptr.data(); |
105 | QWeakPointer<QWidget> weak2 = ptr; |
106 | |
107 | QVERIFY(!weak.isNull()); |
108 | QVERIFY(!weak2.isNull()); |
109 | |
110 | ptr.clear(); // deletes |
111 | |
112 | QVERIFY(weak.isNull()); |
113 | QVERIFY(weak2.isNull()); |
114 | } |
115 | |
116 | |
117 | // ---- strong management ---- |
118 | |
119 | void tst_QSharedPointer_and_QWidget::strong_sharedptrDelete() |
120 | { |
121 | QWidget *parent = new QWidget; |
122 | QSharedPointer<QWidget> ptr(new QWidget(parent)); |
123 | QWeakPointer<QWidget> weak = ptr; |
124 | QPointer<QWidget> check = ptr.data(); |
125 | |
126 | QVERIFY(!check.isNull()); |
127 | QVERIFY(!weak.isNull()); |
128 | |
129 | ptr.clear(); // deletes |
130 | |
131 | QVERIFY(check.isNull()); |
132 | QVERIFY(weak.isNull()); |
133 | |
134 | delete parent; // mustn't crash |
135 | } |
136 | |
137 | QTEST_MAIN(tst_QSharedPointer_and_QWidget) |
138 | |
139 | #include "tst_qsharedpointer_and_qwidget.moc" |
140 |