/* \req QTOPIA-78 \groups Test case where an interface is implemented by a class which inherits a class which implements one other interface. */ void tst_QtUiTestWidgets::cast_inherit_single() { QCOMPARE(WidgetObject::constructed, 0); QCOMPARE(WidgetObject::destructed, 0); QCOMPARE(TextWidgetObject::constructed, 0); QCOMPARE(TextWidgetObject::destructed, 0); QtUiTestWidgets::instance()->refreshPlugins(); Widget *w; TextWidget *tw; { QWidget widget; QLabel label; w = qtuitest_cast<Widget*>(&widget); tw = qtuitest_cast<TextWidget*>(&label); QVERIFY(w); QVERIFY(tw); QCOMPARE(TextWidgetObject::constructed, 1); QCOMPARE(TextWidgetObject::destructed, 0); /* One or more widget objects may have been constructed while finding * the interfaces for QLabel. However there should be exactly 2 * still existing. */ QCOMPARE(WidgetObject::constructed - WidgetObject::destructed, 2); QCOMPARE(tw->text(), QString()); label.setText("Hi there"); QCOMPARE(tw->text(), QString("Hi there")); } QCOMPARE(TextWidgetObject::constructed, 1); QCOMPARE(TextWidgetObject::destructed, 1); QCOMPARE(WidgetObject::constructed - WidgetObject::destructed, 0); }
/* \req QTOPIA-78 \groups Test casting of a widget class which implements its own interfaces. */ void tst_QtUiTestWidgets::cast_self() { QtUiTestWidgets::instance()->clear(); QCOMPARE( TestableCheckBox::constructed, 0 ); QCOMPARE( TestableCheckBox::destructed, 0 ); Widget *w; TextWidget *tw; CheckWidget *cw; { TestableCheckBox tcb; QCheckBox *cb = &tcb; QCOMPARE( TestableCheckBox::constructed, 1 ); QCOMPARE( TestableCheckBox::destructed, 0 ); /* Doesn't need to be registered before cast. */ QVERIFY( w = qtuitest_cast<Widget*>(&tcb) ); QVERIFY( tw = qtuitest_cast<TextWidget*>(&tcb) ); QVERIFY( cw = qtuitest_cast<CheckWidget*>(&tcb) ); QCOMPARE( w, &tcb ); QCOMPARE( tw, &tcb ); QCOMPARE( cw, &tcb ); /* However, should still work when other classes are registered...*/ QtUiTestWidgets::instance()->refreshPlugins(); QCOMPARE( qtuitest_cast<Widget*>(&tcb), w ); QCOMPARE( qtuitest_cast<TextWidget*>(&tcb), tw ); QCOMPARE( qtuitest_cast<CheckWidget*>(&tcb), cw ); QCOMPARE( WidgetObject::constructed, 0 ); QCOMPARE( WidgetObject::destructed, 0 ); QCOMPARE( TestableCheckBox::constructed, 1 ); QCOMPARE( TestableCheckBox::destructed, 0 ); /* Use it a bit and make sure it works as expected. */ QCOMPARE( w->geometry(), tcb.geometry() ); tcb.setText("Walk the Dog"); QCOMPARE( tw->text(), tcb.text() ); tcb.setTristate(true); cb->setCheckState(Qt::PartiallyChecked); QCOMPARE( cw->isTristate(), tcb.isTristate() ); QCOMPARE( cw->checkState(), tcb.checkState() ); } /* Ensure we didn't double-delete */ QCOMPARE( TestableCheckBox::constructed, 1 ); QCOMPARE( TestableCheckBox::destructed, 1 ); /* Ensure we can make another one with no problems */ { TestableCheckBox tcb; QCOMPARE( TestableCheckBox::constructed, 2 ); QCOMPARE( TestableCheckBox::destructed, 1 ); QVERIFY( w = qtuitest_cast<Widget*>(&tcb) ); QVERIFY( tw = qtuitest_cast<TextWidget*>(&tcb) ); QVERIFY( cw = qtuitest_cast<CheckWidget*>(&tcb) ); QCOMPARE( w, &tcb ); QCOMPARE( tw, &tcb ); QCOMPARE( cw, &tcb ); } QCOMPARE( TestableCheckBox::constructed, 2 ); QCOMPARE( TestableCheckBox::destructed, 2 ); /* Ensure casting a null pointer has no ill effects */ w = qtuitest_cast<Widget*>( static_cast<TestableCheckBox*>(0) ); QVERIFY( !w ); QCOMPARE( TestableCheckBox::constructed, 2 ); QCOMPARE( TestableCheckBox::destructed, 2 ); QCOMPARE( WidgetObject::constructed, 0 ); QCOMPARE( WidgetObject::destructed, 0 ); }