Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Unable to use setContextProperty() with testable unit test framework

Unable to use setContextProperty() with testable unit test framework

Scheduled Pinned Locked Moved Solved QML and Qt Quick
testablesetcontextpropeqml
4 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ephelip
    wrote on 26 Sept 2017, 20:42 last edited by kshegunov
    #1

    Re: Testable - QML Unit Test Utilities

    Hi !
    Thanks a lot for sharing this framework.

    I have an issue trying to set custom property from test.cpp while running tests with testable.

    In my app.cpp, i'm using the following code to set properties. Unfortunatly I'm unable to find a way to do the same with testable since I'm note instanciating a QQuickView.

    QQuickView view;
    view.engine()->rootContext()->setContextProperty();
    

    Am I missing something ?

    [Moved to QML and Qt Quick ~kshegunov]

    B 1 Reply Last reply 27 Sept 2017, 14:00
    0
    • E ephelip
      26 Sept 2017, 20:42

      Re: Testable - QML Unit Test Utilities

      Hi !
      Thanks a lot for sharing this framework.

      I have an issue trying to set custom property from test.cpp while running tests with testable.

      In my app.cpp, i'm using the following code to set properties. Unfortunatly I'm unable to find a way to do the same with testable since I'm note instanciating a QQuickView.

      QQuickView view;
      view.engine()->rootContext()->setContextProperty();
      

      Am I missing something ?

      [Moved to QML and Qt Quick ~kshegunov]

      B Offline
      B Offline
      benlau
      Qt Champions 2016
      wrote on 27 Sept 2017, 14:00 last edited by
      #3

      @ephelip Hello,

      Do you mean to set a context property for the test case in tst_xxxx.qml ?

      Testable uses Qt Quick Tests to execute QML unit test and unfortunately that Qt Quick Tests does not support to setup context property.

      Usually, it is suggested to not use context property for QML unit tests, and change to a singleton object then register it by qmlRegisterSingletonType.

      It is a bit troublesome as you need to modify your code. There has a dirty hack by Testable, but personally, I don't really like this solution.

      You may register a context property by using the engine hook function call by TestRunner object.

      void callback(QQmlEngine* engine) {
          Q_UNUSED(engine);
      
          QVariantMap property;
          property["value1"] = 1;
          engine->rootContext()->setContextProperty("CustomProperty", property);
      
          // You may register image provider here for QtTest
      }
      

      Reference:
      testable/main.cpp at master · benlau/testable

          TestRunner runner;
          runner.add(QString(SRCDIR));
      
          runner.addImportPath("qrc:///");
          runner.setEngineHook(callback);
      

      Then in your unit test, call TestRunner.prepare("")

      import Testable 1.0
      
      .....
      
              function test_available() {
                  TestRunner.prepare("");
                  // CustomProperty is registered by engine hook
                  compare(CustomProperty.value1, 1);
              }
      

      Reference:
      testable/tst_ContextProperty.qml at master · benlau/testable

      TestRunner.prepare is a dummy function which does nothing by itself. But once a TestRunner singleton object is created, it will trigger the engine hook function to register a context property.

      1 Reply Last reply
      2
      • K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 26 Sept 2017, 22:05 last edited by
        #2

        Ping @benlau

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • E ephelip
          26 Sept 2017, 20:42

          Re: Testable - QML Unit Test Utilities

          Hi !
          Thanks a lot for sharing this framework.

          I have an issue trying to set custom property from test.cpp while running tests with testable.

          In my app.cpp, i'm using the following code to set properties. Unfortunatly I'm unable to find a way to do the same with testable since I'm note instanciating a QQuickView.

          QQuickView view;
          view.engine()->rootContext()->setContextProperty();
          

          Am I missing something ?

          [Moved to QML and Qt Quick ~kshegunov]

          B Offline
          B Offline
          benlau
          Qt Champions 2016
          wrote on 27 Sept 2017, 14:00 last edited by
          #3

          @ephelip Hello,

          Do you mean to set a context property for the test case in tst_xxxx.qml ?

          Testable uses Qt Quick Tests to execute QML unit test and unfortunately that Qt Quick Tests does not support to setup context property.

          Usually, it is suggested to not use context property for QML unit tests, and change to a singleton object then register it by qmlRegisterSingletonType.

          It is a bit troublesome as you need to modify your code. There has a dirty hack by Testable, but personally, I don't really like this solution.

          You may register a context property by using the engine hook function call by TestRunner object.

          void callback(QQmlEngine* engine) {
              Q_UNUSED(engine);
          
              QVariantMap property;
              property["value1"] = 1;
              engine->rootContext()->setContextProperty("CustomProperty", property);
          
              // You may register image provider here for QtTest
          }
          

          Reference:
          testable/main.cpp at master · benlau/testable

              TestRunner runner;
              runner.add(QString(SRCDIR));
          
              runner.addImportPath("qrc:///");
              runner.setEngineHook(callback);
          

          Then in your unit test, call TestRunner.prepare("")

          import Testable 1.0
          
          .....
          
                  function test_available() {
                      TestRunner.prepare("");
                      // CustomProperty is registered by engine hook
                      compare(CustomProperty.value1, 1);
                  }
          

          Reference:
          testable/tst_ContextProperty.qml at master · benlau/testable

          TestRunner.prepare is a dummy function which does nothing by itself. But once a TestRunner singleton object is created, it will trigger the engine hook function to register a context property.

          1 Reply Last reply
          2
          • E Offline
            E Offline
            ephelip
            wrote on 29 Sept 2017, 13:50 last edited by
            #4

            Thanks for the exemple,
            this works great !

            1 Reply Last reply
            0

            4/4

            29 Sept 2017, 13:50

            • Login

            • Login or register to search.
            4 out of 4
            • First post
              4/4
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved