tests/auto/qobject/signalbug.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "signalbug.h"
       
    43 
       
    44 #include <qcoreapplication.h>
       
    45 #include <qstring.h>
       
    46 
       
    47 #include <stdio.h>
       
    48 
       
    49 static int Step = 0;
       
    50 Sender RandomSender (0, 0);
       
    51 
       
    52 
       
    53 void TRACE (int step, const char *name)
       
    54 {
       
    55 	for (int t = 0; t < step - 1; t++)
       
    56 		fprintf (stderr, "\t");
       
    57 	fprintf (stderr, "Step %d: %s\n", step, name);
       
    58 	return;
       
    59 }
       
    60 
       
    61 
       
    62 Receiver::Receiver ()
       
    63 	: QObject ()
       
    64 {
       
    65 }
       
    66 
       
    67 void Receiver::received ()
       
    68 {
       
    69 	::Step++;
       
    70 	const int stepCopy = ::Step;
       
    71 	TRACE (stepCopy, "Receiver::received()");
       
    72 	Q_ASSERT (::Step == 2 || ::Step == 4);
       
    73 
       
    74 	if (::Step == 2)
       
    75 		s->fire ();
       
    76 
       
    77 	fprintf (stderr, "Receiver<%s>::received() sender=%s\n",
       
    78 		(const char *) objectName ().toAscii (), sender ()->metaObject()->className());
       
    79 
       
    80 	TRACE (stepCopy, "ends Receiver::received()");
       
    81 }
       
    82 
       
    83 
       
    84 Disconnector::Disconnector ()
       
    85 	: QObject ()
       
    86 {
       
    87 }
       
    88 
       
    89 void Disconnector::received ()
       
    90 {
       
    91 	::Step++;
       
    92 	const int stepCopy = ::Step;
       
    93 	TRACE (stepCopy, "Disconnector::received()");
       
    94 	Q_ASSERT (::Step == 5 || ::Step == 6);
       
    95 
       
    96 	fprintf (stderr, "Disconnector<%s>::received() sender=%s\n",
       
    97 		(const char *) objectName ().toAscii (), sender ()->metaObject()->className());
       
    98 	if (sender () == 0)
       
    99 		fprintf (stderr, "WE SHOULD NOT BE RECEIVING THIS SIGNAL\n");
       
   100 
       
   101 	if (::Step == 5)
       
   102 	{
       
   103 		disconnect (s, SIGNAL (fired ()), s->d, SLOT (received ()));
       
   104 
       
   105 		connect (&RandomSender, SIGNAL (fired ()), s->d, SLOT (received ()));
       
   106 	}
       
   107 
       
   108 	TRACE (stepCopy, "ends Disconnector::received()");
       
   109 }
       
   110 
       
   111 
       
   112 Sender::Sender (Receiver *r, Disconnector *d)
       
   113 	: QObject ()
       
   114 {
       
   115 	this->r = r; this->d = d;
       
   116 	if (r)
       
   117 		connect (this, SIGNAL (fired ()), r, SLOT (received ()));
       
   118 	if (d)
       
   119 		connect (this, SIGNAL (fired ()), d, SLOT (received ()));
       
   120 };
       
   121 
       
   122 void Sender::fire ()
       
   123 {
       
   124 	::Step++;
       
   125 	const int stepCopy = ::Step;
       
   126 	TRACE (stepCopy, "Sender::fire()");
       
   127 	Q_ASSERT (::Step == 1 || ::Step == 3);
       
   128 
       
   129 	emit fired ();
       
   130 	TRACE (stepCopy, "ends Sender::fire()");
       
   131 }
       
   132 
       
   133 
       
   134 int main (int argc, char *argv [])
       
   135 {
       
   136 	QCoreApplication app (argc, argv);
       
   137 
       
   138 	Receiver r;
       
   139 	Disconnector d;
       
   140 	Sender s (&r, &d);
       
   141 
       
   142 	r.s = &s;
       
   143 	d.s = &s;
       
   144 
       
   145 
       
   146 	::Step = 0;
       
   147 	s.fire ();
       
   148 	return 0;
       
   149 }
       
   150 
       
   151