1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <qsignalspy.h>
#include <QtQml/qqmlengine.h>
#include <QtQml/qqmlcomponent.h>
#include <QtQuick/qquickview.h>
#include <QtQuick/private/qquickrectangle_p.h>
#include <QtQuick/private/qquicktext_p.h>
#include <QtQuick/private/qquickbehavior_p.h>
#include <QtQuick/private/qquickanimation_p.h>
#include <QtQuick/private/qquicksmoothedanimation_p.h>
#include <private/qquickitem_p.h>
#include "../../shared/util.h"
class tst_qquickbehaviors : public QQmlDataTest
{
Q_OBJECT
public:
tst_qquickbehaviors() {}
private slots:
void init() { qApp->processEvents(); } //work around animation timer bug (QTBUG-22865)
void simpleBehavior();
void scriptTriggered();
void cppTriggered();
void loop();
void colorBehavior();
void parentBehavior();
void replaceBinding();
//void transitionOverrides();
void group();
void valueType();
void emptyBehavior();
void explicitSelection();
void nonSelectingBehavior();
void reassignedAnimation();
void disabled();
void dontStart();
void startup();
void groupedPropertyCrash();
void runningTrue();
void sameValue();
void delayedRegistration();
void startOnCompleted();
void multipleChangesToValueType();
void currentValue();
void disabledWriteWhileRunning();
void aliasedProperty();
void innerBehaviorOverwritten();
void oneWay();
void safeToDelete();
void targetProperty();
};
void tst_qquickbehaviors::simpleBehavior()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("simple.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QTRY_VERIFY(qobject_cast<QQuickBehavior*>(rect->findChild<QQuickBehavior*>("MyBehavior"))->animation());
QQuickItemPrivate::get(rect.data())->setState("moved");
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() > 0);
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() < 200);
//i.e. the behavior has been triggered
}
void tst_qquickbehaviors::scriptTriggered()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("scripttrigger.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
rect->setColor(QColor("red"));
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() > 0);
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() < 200);
//i.e. the behavior has been triggered
}
void tst_qquickbehaviors::cppTriggered()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("cpptrigger.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickRectangle *innerRect = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"));
QTRY_VERIFY(innerRect);
innerRect->setProperty("x", 200);
QTRY_VERIFY(innerRect->x() > 0);
QTRY_VERIFY(innerRect->x() < 200); //i.e. the behavior has been triggered
}
void tst_qquickbehaviors::loop()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("loop.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
//don't crash
QQuickItemPrivate::get(rect.data())->setState("moved");
}
void tst_qquickbehaviors::colorBehavior()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("color.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickItemPrivate::get(rect.data())->setState("red");
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->color() != QColor("red"));
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->color() != QColor("green"));
//i.e. the behavior has been triggered
}
void tst_qquickbehaviors::parentBehavior()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("parent.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickItemPrivate::get(rect.data())->setState("reparented");
QTRY_VERIFY(rect->findChild<QQuickRectangle*>("MyRect")->parentItem() != rect->findChild<QQuickItem*>("NewParent"));
QTRY_VERIFY(rect->findChild<QQuickRectangle*>("MyRect")->parentItem() == rect->findChild<QQuickItem*>("NewParent"));
}
void tst_qquickbehaviors::replaceBinding()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("binding.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickItemPrivate::get(rect.data())->setState("moved");
QQuickRectangle *innerRect = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"));
QTRY_VERIFY(innerRect);
QTRY_VERIFY(innerRect->x() > 0);
QTRY_VERIFY(innerRect->x() < 200);
//i.e. the behavior has been triggered
QTRY_COMPARE(innerRect->x(), (qreal)200);
rect->setProperty("basex", 10);
QTRY_COMPARE(innerRect->x(), (qreal)200);
rect->setProperty("movedx", 210);
QTRY_COMPARE(innerRect->x(), (qreal)210);
QQuickItemPrivate::get(rect.data())->setState("");
QTRY_VERIFY(innerRect->x() > 10);
QTRY_VERIFY(innerRect->x() < 210); //i.e. the behavior has been triggered
QTRY_COMPARE(innerRect->x(), (qreal)10);
rect->setProperty("movedx", 200);
QTRY_COMPARE(innerRect->x(), (qreal)10);
rect->setProperty("basex", 20);
QTRY_COMPARE(innerRect->x(), (qreal)20);
}
void tst_qquickbehaviors::group()
{
/* XXX TODO Create a test element for this case.
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("groupProperty.qml")));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickItemPrivate::get(rect.data())->setState("moved");
//QTest::qWait(200);
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() > 0);
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() < 200);
//i.e. the behavior has been triggered
}
*/
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("groupProperty2.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickItemPrivate::get(rect.data())->setState("moved");
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->border()->width() > 0);
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->border()->width() < 4);
//i.e. the behavior has been triggered
}
}
void tst_qquickbehaviors::valueType()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("valueType.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
//QTBUG-20827
QCOMPARE(rect->color(), QColor::fromRgb(255,0,255));
}
void tst_qquickbehaviors::emptyBehavior()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("empty.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickItemPrivate::get(rect.data())->setState("moved");
qreal x = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x();
QCOMPARE(x, qreal(200)); //should change immediately
}
void tst_qquickbehaviors::explicitSelection()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("explicit.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickItemPrivate::get(rect.data())->setState("moved");
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() > 0);
QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() < 200);
//i.e. the behavior has been triggered
}
void tst_qquickbehaviors::nonSelectingBehavior()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("nonSelecting2.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickItemPrivate::get(rect.data())->setState("moved");
qreal x = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x();
QCOMPARE(x, qreal(200)); //should change immediately
}
void tst_qquickbehaviors::reassignedAnimation()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("reassignedAnimation.qml"));
QString warning = testFileUrl("reassignedAnimation.qml").toString() + ":9:9: QML Behavior: Cannot change the animation assigned to a Behavior.";
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QCOMPARE(qobject_cast<QQuickNumberAnimation*>(
rect->findChild<QQuickBehavior*>("MyBehavior")->animation())->duration(), 200);
}
void tst_qquickbehaviors::disabled()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("disabled.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QCOMPARE(rect->findChild<QQuickBehavior*>("MyBehavior")->enabled(), false);
QQuickItemPrivate::get(rect.data())->setState("moved");
qreal x = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x();
QCOMPARE(x, qreal(200)); //should change immediately
}
void tst_qquickbehaviors::dontStart()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("dontStart.qml"));
QString warning = c.url().toString() + ":13:13: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickAbstractAnimation *myAnim = rect->findChild<QQuickAbstractAnimation*>("MyAnim");
QVERIFY(myAnim);
QVERIFY(!myAnim->qtAnimation());
}
void tst_qquickbehaviors::startup()
{
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("startup.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickRectangle *innerRect = rect->findChild<QQuickRectangle*>("innerRect");
QVERIFY(innerRect);
QCOMPARE(innerRect->x(), qreal(100)); //should be set immediately
}
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("startup2.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickRectangle *innerRect = rect->findChild<QQuickRectangle*>("innerRect");
QVERIFY(innerRect);
QQuickText *text = rect->findChild<QQuickText*>();
QVERIFY(text);
QCOMPARE(innerRect->x(), text->width()); //should be set immediately
}
}
//QTBUG-10799
void tst_qquickbehaviors::groupedPropertyCrash()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("groupedPropertyCrash.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString())); //don't crash
}
//QTBUG-5491
void tst_qquickbehaviors::runningTrue()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("runningTrue.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickAbstractAnimation *animation = rect->findChild<QQuickAbstractAnimation*>("rotAnim");
QVERIFY(animation);
QSignalSpy runningSpy(animation, SIGNAL(runningChanged(bool)));
rect->setProperty("myValue", 180);
QTRY_VERIFY(runningSpy.count() > 0);
}
//QTBUG-12295
void tst_qquickbehaviors::sameValue()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("qtbug12295.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickRectangle *target = rect->findChild<QQuickRectangle*>("myRect");
QVERIFY(target);
target->setX(100);
QCOMPARE(target->x(), qreal(100));
target->setProperty("x", 0);
QTRY_VERIFY(target->x() != qreal(0) && target->x() != qreal(100));
QTRY_VERIFY(target->x() == qreal(0)); //make sure Behavior has finished.
target->setX(100);
QCOMPARE(target->x(), qreal(100));
//this is the main point of the test -- the behavior needs to be triggered again
//even though we set 0 twice in a row.
target->setProperty("x", 0);
QTRY_VERIFY(target->x() != qreal(0) && target->x() != qreal(100));
}
//QTBUG-18362
void tst_qquickbehaviors::delayedRegistration()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("delayedRegistration.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickItem *innerRect = rect->property("myItem").value<QQuickItem*>();
QVERIFY(innerRect != nullptr);
QCOMPARE(innerRect->property("x").toInt(), int(0));
QTRY_COMPARE(innerRect->property("x").toInt(), int(100));
}
//QTBUG-22555
void tst_qquickbehaviors::startOnCompleted()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("startOnCompleted.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickItem *innerRect = rect->findChild<QQuickRectangle*>();
QVERIFY(innerRect != nullptr);
QCOMPARE(innerRect->property("x").toInt(), int(0));
QTRY_COMPARE(innerRect->property("x").toInt(), int(100));
}
//QTBUG-25139
void tst_qquickbehaviors::multipleChangesToValueType()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("multipleChangesToValueType.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle *>(c.create()));
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickText *text = rect->findChild<QQuickText *>();
QVERIFY(text != nullptr);
QFont value;
value.setPointSize(24);
QCOMPARE(text->property("font").value<QFont>(), value);
QVERIFY(QMetaObject::invokeMethod(rect.data(), "updateFontProperties"));
value.setItalic(true);
value.setWeight(QFont::Bold);
QCOMPARE(text->property("font").value<QFont>(), value);
value.setPointSize(48);
QTRY_COMPARE(text->property("font").value<QFont>(), value);
}
//QTBUG-21549
void tst_qquickbehaviors::currentValue()
{
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("qtbug21549.qml"));
QScopedPointer<QQuickItem> item(qobject_cast<QQuickItem*>(c.create()));
QVERIFY2(!item.isNull(), qPrintable(c.errorString()));
QQuickRectangle *target = item->findChild<QQuickRectangle*>("myRect");
QVERIFY(target);
QCOMPARE(target->x(), qreal(0));
target->setProperty("x", 50);
QCOMPARE(item->property("behaviorCount").toInt(), 1);
QCOMPARE(target->x(), qreal(50));
target->setProperty("x", 50);
QCOMPARE(item->property("behaviorCount").toInt(), 1);
QCOMPARE(target->x(), qreal(50));
target->setX(100);
target->setProperty("x", 100);
QCOMPARE(item->property("behaviorCount").toInt(), 1);
QCOMPARE(target->x(), qreal(100));
}
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("qtbug21549-2.qml"));
QScopedPointer<QQuickItem> item(qobject_cast<QQuickItem*>(c.create()));
QVERIFY2(!item.isNull(), qPrintable(c.errorString()));
QQuickRectangle *target = item->findChild<QQuickRectangle*>("myRect");
QVERIFY(target);
QCOMPARE(target->x(), qreal(0));
target->setProperty("x", 100);
// the spring animation should smoothly transition to the new value triggered
// in the QML (which should be between 50 and 80);
QTRY_COMPARE(item->property("animRunning").toBool(), true);
QTRY_COMPARE(item->property("animRunning").toBool(), false);
QVERIFY2(target->x() > qreal(50) && target->x() < qreal(80), QByteArray::number(target->x()));
}
}
void tst_qquickbehaviors::disabledWriteWhileRunning()
{
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("disabledWriteWhileRunning.qml"));
QScopedPointer<QQuickItem> root(qobject_cast<QQuickItem*>(c.create()));
QVERIFY2(!root.isNull(), qPrintable(c.errorString()));
QQuickRectangle *myRect = qobject_cast<QQuickRectangle*>(root->findChild<QQuickRectangle*>("MyRect"));
QQuickBehavior *myBehavior = qobject_cast<QQuickBehavior*>(root->findChild<QQuickBehavior*>("MyBehavior"));
QQuickNumberAnimation *myAnimation = qobject_cast<QQuickNumberAnimation*>(root->findChild<QQuickNumberAnimation*>("MyAnimation"));
QVERIFY(myRect);
QVERIFY(myBehavior);
QVERIFY(myAnimation);
// initial values
QCOMPARE(myBehavior->enabled(), true);
QCOMPARE(myAnimation->isRunning(), false);
QCOMPARE(myRect->x(), qreal(0));
// start animation
myRect->setProperty("x", 200);
QCOMPARE(myAnimation->isRunning(), true);
QTRY_VERIFY(myRect->x() != qreal(0) && myRect->x() != qreal(200));
QCOMPARE(myBehavior->targetValue(), 200); // grabbed before starting the animation
// set disabled while animation is in progress
myBehavior->setProperty("enabled", false);
QCOMPARE(myAnimation->isRunning(), true);
// force new value while disabled and previous animation is in progress.
// animation should be stopped and value stay at forced value
myRect->setProperty("x", 100);
QCOMPARE(myAnimation->isRunning(), false);
QCOMPARE(myRect->x(), qreal(100));
QCOMPARE(myBehavior->targetValue(), 100);
QTest::qWait(200);
QCOMPARE(myRect->x(), qreal(100));
}
//test additional complications with SmoothedAnimation
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("disabledWriteWhileRunning2.qml"));
QScopedPointer<QQuickItem> root(qobject_cast<QQuickItem*>(c.create()));
QVERIFY2(!root.isNull(), qPrintable(c.errorString()));
QQuickRectangle *myRect = qobject_cast<QQuickRectangle*>(root->findChild<QQuickRectangle*>("MyRect"));
QQuickBehavior *myBehavior = qobject_cast<QQuickBehavior*>(root->findChild<QQuickBehavior*>("MyBehavior"));
QQuickSmoothedAnimation *myAnimation = qobject_cast<QQuickSmoothedAnimation*>(root->findChild<QQuickNumberAnimation*>("MyAnimation"));
QVERIFY(myRect);
QVERIFY(myBehavior);
QVERIFY(myAnimation);
// initial values
QCOMPARE(myBehavior->enabled(), true);
QCOMPARE(myBehavior->targetValue(), QVariant());
QCOMPARE(myAnimation->isRunning(), false);
QCOMPARE(myRect->x(), qreal(0));
// start animation
myRect->setProperty("x", 200);
QCOMPARE(myAnimation->isRunning(), true);
QCOMPARE(myBehavior->targetValue(), 200);
QTRY_VERIFY(myRect->x() != qreal(0) && myRect->x() != qreal(200));
//set second value
myRect->setProperty("x", 300);
QCOMPARE(myAnimation->isRunning(), true);
QCOMPARE(myBehavior->targetValue(), 300);
QTRY_VERIFY(myRect->x() != qreal(0) && myRect->x() != qreal(200));
// set disabled while animation is in progress
myBehavior->setProperty("enabled", false);
QCOMPARE(myAnimation->isRunning(), true);
// force new value while disabled and previous animation is in progress.
// animation should be stopped and value stay at forced value
myRect->setProperty("x", 100);
QCOMPARE(myAnimation->isRunning(), false);
QCOMPARE(myRect->x(), qreal(100));
QCOMPARE(myBehavior->targetValue(), 100);
QTest::qWait(200);
QCOMPARE(myRect->x(), qreal(100));
}
}
void tst_qquickbehaviors::aliasedProperty()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("aliased.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickBehavior* behavior =
qobject_cast<QQuickBehavior*>(rect->findChild<QQuickBehavior*>("behavior"));
QSignalSpy targetValueSpy(behavior, SIGNAL(targetValueChanged()));
QQuickItemPrivate::get(rect.data())->setState("moved");
QCOMPARE(behavior->targetValue(), 400);
QCOMPARE(targetValueSpy.count(), 1);
QScopedPointer<QQuickRectangle> acc(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("acc")));
QScopedPointer<QQuickRectangle> range(qobject_cast<QQuickRectangle*>(acc->findChild<QQuickRectangle*>("range")));
QTRY_VERIFY(acc->property("value").toDouble() > 0);
QTRY_VERIFY(range->width() > 0);
QTRY_VERIFY(acc->property("value").toDouble() < 400);
QTRY_VERIFY(range->width() < 400);
//i.e. the behavior has been triggered
}
void tst_qquickbehaviors::innerBehaviorOverwritten()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("overwrittenbehavior.qml"));
QScopedPointer<QQuickItem> item(qobject_cast<QQuickItem*>(c.create()));
QQuickBehavior* behavior =
qobject_cast<QQuickBehavior*>(item->findChild<QQuickBehavior*>("behavior"));
QVERIFY(item->property("behaviorTriggered").toBool());
QCOMPARE(behavior->targetValue(), true);
}
void tst_qquickbehaviors::oneWay()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("oneway.qml"));
QScopedPointer<QQuickRectangle> rect(qobject_cast<QQuickRectangle*>(c.create()));;
QVERIFY2(!rect.isNull(), qPrintable(c.errorString()));
QQuickBehavior* behavior =
qobject_cast<QQuickBehavior*>(rect->findChild<QQuickBehavior*>("MyBehaviorOneWay"));
QCOMPARE(behavior->enabled(), false);
QCOMPARE(behavior->targetValue(), QVariant());
QSignalSpy targetValueSpy(behavior, SIGNAL(targetValueChanged()));
QQuickRectangle *myRect = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRectOneWay"));
myRect->setProperty("x", 100);
QCOMPARE(behavior->targetValue(), 100);
QCOMPARE(targetValueSpy.count(), 1);
QCOMPARE(behavior->enabled(), false);
qreal x = myRect->x();
QCOMPARE(x, qreal(100)); //should change immediately
QQuickNumberAnimation *myAnimation =
qobject_cast<QQuickNumberAnimation*>(behavior->findChild<QQuickNumberAnimation*>("MyAnimationOneWay"));
QCOMPARE(myAnimation->isRunning(), false);
myRect->setProperty("x", 0);
QCOMPARE(behavior->targetValue(), 0);
QCOMPARE(targetValueSpy.count(), 2);
QCOMPARE(behavior->enabled(), true);
QCOMPARE(myAnimation->isRunning(), true);
QVERIFY(myRect->x() > 0.0);
QTRY_VERIFY(myRect->x() < 100.0);
QTRY_COMPARE(myRect->x(), qreal(0));
QCOMPARE(myAnimation->isRunning(), false);
}
// QTBUG-76749
void tst_qquickbehaviors::safeToDelete()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("delete.qml"));
QVERIFY(c.create());
}
Q_DECLARE_METATYPE(QQmlProperty)
void tst_qquickbehaviors::targetProperty()
{
QQmlEngine engine;
QQmlComponent c(&engine, testFileUrl("targetProperty.qml"));
QScopedPointer<QQuickItem> item(qobject_cast<QQuickItem*>(c.create()));
QVERIFY2(!item.isNull(), qPrintable(c.errorString()));
QQuickBehavior* xBehavior =
qobject_cast<QQuickBehavior*>(item->findChild<QQuickBehavior*>("xBehavior"));
QCOMPARE(xBehavior->property("targetProperty").value<QQmlProperty>(), QQmlProperty(item.data(), "x"));
QCOMPARE(item->property("xBehaviorObject").value<QObject*>(), item.data());
QCOMPARE(item->property("xBehaviorName").toString(), "x");
QQuickBehavior* emptyBehavior =
qobject_cast<QQuickBehavior*>(item->findChild<QQuickBehavior*>("emptyBehavior"));
QCOMPARE(emptyBehavior->property("targetProperty").value<QQmlProperty>().isValid(), false);
QCOMPARE(item->property("emptyBehaviorObject").value<QObject*>(), nullptr);
QCOMPARE(item->property("emptyBehaviorName").toString(), "");
}
QTEST_MAIN(tst_qquickbehaviors)
#include "tst_qquickbehaviors.moc"
|