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. Qt Test
Forum Updated to NodeBB v4.3 + New Features

Qt Test

Scheduled Pinned Locked Moved Solved QML and Qt Quick
13 Posts 2 Posters 1.5k 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.
  • J JasmineSethi
    22 Apr 2019, 06:00

    Hi,
    I have a simple code where on click color changes as shown below. I want to write QTest for this. As i am new to Qtest i want what all steps need to refer for testing i.e. onclick color changes to yellow or not, also text color is black or not.

    import QtQuick 2.11
    import QtQuick.Window 2.11

    Window
    {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Rectangle
    {
        id: b1
        width: 100
        height: 50
        color: "red"
        anchors.centerIn: parent
        Text
        {
            id: name
            text: qsTr("Button")
            color: "black"
            font.pixelSize: 10
            anchors.centerIn: parent
        }
        MouseArea
        {
            anchors.fill: parent
            onClicked:
            {
                b1.color = "yellow"
            }
        }
    }
    

    }

    K Offline
    K Offline
    KillerSmath
    wrote on 22 Apr 2019, 06:49 last edited by KillerSmath
    #2

    @JasmineSethi
    I suggest you to read the QtTest documentation.
    https://doc.qt.io/qt-5/qml-qttest-testcase.html

    Check out this example:
    Edit:
    "red" -> "#ff000"
    "yellow" -> "#ffff00"
    mouseClick(mouseArea) instead mouseArea.clicked()

    TestCase {
        name: "rectangleColor"
        when: windowShown
        function test_rectColor() {
            compare(b1.color, "#ff0000"); // check color
            mouseClick(mouseArea); // emit clicked signal
            compare(b1.color, "#ffff00"); // check color
        }
    }
    

    @Computer Science Student - Brazil
    Web Developer and Researcher
    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

    J 1 Reply Last reply 23 Apr 2019, 11:09
    3
    • K KillerSmath
      22 Apr 2019, 06:49

      @JasmineSethi
      I suggest you to read the QtTest documentation.
      https://doc.qt.io/qt-5/qml-qttest-testcase.html

      Check out this example:
      Edit:
      "red" -> "#ff000"
      "yellow" -> "#ffff00"
      mouseClick(mouseArea) instead mouseArea.clicked()

      TestCase {
          name: "rectangleColor"
          when: windowShown
          function test_rectColor() {
              compare(b1.color, "#ff0000"); // check color
              mouseClick(mouseArea); // emit clicked signal
              compare(b1.color, "#ffff00"); // check color
          }
      }
      
      J Offline
      J Offline
      JasmineSethi
      wrote on 23 Apr 2019, 11:09 last edited by JasmineSethi
      #3

      @KillerSmath Hi, Thanks for your previous response.

      I have another question.

      I have a function which is part of button.qml as shown below. I want to do qtest for this. How can i ?

      function setDisableState(disable)
      {
      properties.disableButton = disable
      //properties is part of QtObject.
      //i.e. property bool disableButton: false

          if(disable === true)
          {
              buttonAudioPause.color = properties.disableStateModern
              buttonAudioPause.border.color = properties.disableStateModern
              mainAudioPauseButton.enabled = false
              properties.btnTextColor =  properties.modernGray4
          }
      
          else if(disable === false)
          {
              buttonAudioPause.color =  properties.buttonColor
              buttonAudioPause.border.color = properties.buttonColor
              mainAudioPauseButton.enabled = true
              properties.btnTextColor =  properties.blackColor
      
          }
      
      }
      
      K 1 Reply Last reply 23 Apr 2019, 11:34
      0
      • J JasmineSethi
        23 Apr 2019, 11:09

        @KillerSmath Hi, Thanks for your previous response.

        I have another question.

        I have a function which is part of button.qml as shown below. I want to do qtest for this. How can i ?

        function setDisableState(disable)
        {
        properties.disableButton = disable
        //properties is part of QtObject.
        //i.e. property bool disableButton: false

            if(disable === true)
            {
                buttonAudioPause.color = properties.disableStateModern
                buttonAudioPause.border.color = properties.disableStateModern
                mainAudioPauseButton.enabled = false
                properties.btnTextColor =  properties.modernGray4
            }
        
            else if(disable === false)
            {
                buttonAudioPause.color =  properties.buttonColor
                buttonAudioPause.border.color = properties.buttonColor
                mainAudioPauseButton.enabled = true
                properties.btnTextColor =  properties.blackColor
        
            }
        
        }
        
        K Offline
        K Offline
        KillerSmath
        wrote on 23 Apr 2019, 11:34 last edited by
        #4

        @JasmineSethi
        You can call the function using the object identifier buttonAudioPause.setDisableState(true) and check if the variable has been changed using compare calls.

        @Computer Science Student - Brazil
        Web Developer and Researcher
        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

        J 1 Reply Last reply 24 Apr 2019, 05:33
        0
        • K KillerSmath
          23 Apr 2019, 11:34

          @JasmineSethi
          You can call the function using the object identifier buttonAudioPause.setDisableState(true) and check if the variable has been changed using compare calls.

          J Offline
          J Offline
          JasmineSethi
          wrote on 24 Apr 2019, 05:33 last edited by
          #5

          @KillerSmath

          I wrote 2 function set() and get()
          function setDisableState(disable)
          {
          properties.disableButton = disable

              if(disable === true)
              {
                  buttonAudioPause.color = "some color value"
                  buttonAudioPause.border.color = "some color value"
                  mainAudioPauseButton.enabled = false
                  properties.btnTextColor =  "some color value"
              }
          
              else if(disable === false)
              {
                  buttonAudioPause.color =  "some color value"
                  buttonAudioPause.border.color ="some color value" 
                  mainAudioPauseButton.enabled = true
                  properties.btnTextColor =  "some color value"
          
              }
          
          }
          function getDisableState(disable)
          {
              return properties.disableButton
          }
          

          My TestCase looks something like this:

          I am instantiating qml file and using its id to call the set() and get() function.

          RSBB_ButtonWidget_AudioPause
          {
          id: rsbbAudioPauseButton

          }
          

          TestCase
          {
          name: "Disable state"
          when: windowShown

              function test_disableStateForAudioPauseButton()
              {
                  rsbbAudioPauseButton.setDisableState(true)
                  compare(rsbbAudioPauseButton.getDisableState(),?,?)//??
          
              }
          }
          

          //My question is how to test more than one condition(for example buttonAudioPause.color, buttonAudioPause.border.color) in "compare" statement when setDisableState(true) and setDisableState(false) ?

          K 1 Reply Last reply 24 Apr 2019, 06:06
          0
          • J JasmineSethi
            24 Apr 2019, 05:33

            @KillerSmath

            I wrote 2 function set() and get()
            function setDisableState(disable)
            {
            properties.disableButton = disable

                if(disable === true)
                {
                    buttonAudioPause.color = "some color value"
                    buttonAudioPause.border.color = "some color value"
                    mainAudioPauseButton.enabled = false
                    properties.btnTextColor =  "some color value"
                }
            
                else if(disable === false)
                {
                    buttonAudioPause.color =  "some color value"
                    buttonAudioPause.border.color ="some color value" 
                    mainAudioPauseButton.enabled = true
                    properties.btnTextColor =  "some color value"
            
                }
            
            }
            function getDisableState(disable)
            {
                return properties.disableButton
            }
            

            My TestCase looks something like this:

            I am instantiating qml file and using its id to call the set() and get() function.

            RSBB_ButtonWidget_AudioPause
            {
            id: rsbbAudioPauseButton

            }
            

            TestCase
            {
            name: "Disable state"
            when: windowShown

                function test_disableStateForAudioPauseButton()
                {
                    rsbbAudioPauseButton.setDisableState(true)
                    compare(rsbbAudioPauseButton.getDisableState(),?,?)//??
            
                }
            }
            

            //My question is how to test more than one condition(for example buttonAudioPause.color, buttonAudioPause.border.color) in "compare" statement when setDisableState(true) and setDisableState(false) ?

            K Offline
            K Offline
            KillerSmath
            wrote on 24 Apr 2019, 06:06 last edited by KillerSmath
            #6

            @JasmineSethi
            Compare function only compares pairs of data.
            However, it may be generalize using object comparison.

            {
              key_1: value_1, 
              key_2: value_2
            }
            

            So, you can return a object with button states and compare with an expected result. Here is a example.

            function getDisableState()
            {
                return {
                    enabled: yourButtonID.enabled, 
                    color: yourButtonID.color
                }
            }
            // your code
            TestCase 
            { 
                function test_disableStateForAudioPauseButton()
                {
                    yourButtonID.setDisableState(true)
                    compare(yourButtonID.getDisableState(), {enabled: false, color: "some color value"})
                }
            }
            

            @Computer Science Student - Brazil
            Web Developer and Researcher
            “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

            J 1 Reply Last reply 24 Apr 2019, 09:41
            0
            • K KillerSmath
              24 Apr 2019, 06:06

              @JasmineSethi
              Compare function only compares pairs of data.
              However, it may be generalize using object comparison.

              {
                key_1: value_1, 
                key_2: value_2
              }
              

              So, you can return a object with button states and compare with an expected result. Here is a example.

              function getDisableState()
              {
                  return {
                      enabled: yourButtonID.enabled, 
                      color: yourButtonID.color
                  }
              }
              // your code
              TestCase 
              { 
                  function test_disableStateForAudioPauseButton()
                  {
                      yourButtonID.setDisableState(true)
                      compare(yourButtonID.getDisableState(), {enabled: false, color: "some color value"})
                  }
              }
              
              J Offline
              J Offline
              JasmineSethi
              wrote on 24 Apr 2019, 09:41 last edited by
              #7

              @KillerSmath

              Not able to return more than one object as shown neither this nor the way u mentioned.

              0_1556098759055_f95d93cb-117e-4114-8923-e9a6d9778f66-image.png

              K 1 Reply Last reply 24 Apr 2019, 09:46
              0
              • J JasmineSethi
                24 Apr 2019, 09:41

                @KillerSmath

                Not able to return more than one object as shown neither this nor the way u mentioned.

                0_1556098759055_f95d93cb-117e-4114-8923-e9a6d9778f66-image.png

                K Offline
                K Offline
                KillerSmath
                wrote on 24 Apr 2019, 09:46 last edited by
                #8

                @JasmineSethi said in Qt Test:

                @KillerSmath

                Not able to return more than one object as shown neither this nor the way u mentioned.

                0_1556098759055_f95d93cb-117e-4114-8923-e9a6d9778f66-image.png

                I have mentioned this structure:

                {
                  key_1: value_1, 
                  key_2: value_2
                }
                

                @Computer Science Student - Brazil
                Web Developer and Researcher
                “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                J 1 Reply Last reply 24 Apr 2019, 09:51
                0
                • K KillerSmath
                  24 Apr 2019, 09:46

                  @JasmineSethi said in Qt Test:

                  @KillerSmath

                  Not able to return more than one object as shown neither this nor the way u mentioned.

                  0_1556098759055_f95d93cb-117e-4114-8923-e9a6d9778f66-image.png

                  I have mentioned this structure:

                  {
                    key_1: value_1, 
                    key_2: value_2
                  }
                  
                  J Offline
                  J Offline
                  JasmineSethi
                  wrote on 24 Apr 2019, 09:51 last edited by
                  #9

                  @KillerSmath

                  Tried the same way too. It didn't worked
                  0_1556099468270_26dd44cf-dd4a-41a0-8c06-8bcc0c268a0b-image.png

                  K 1 Reply Last reply 24 Apr 2019, 10:04
                  0
                  • J JasmineSethi
                    24 Apr 2019, 09:51

                    @KillerSmath

                    Tried the same way too. It didn't worked
                    0_1556099468270_26dd44cf-dd4a-41a0-8c06-8bcc0c268a0b-image.png

                    K Offline
                    K Offline
                    KillerSmath
                    wrote on 24 Apr 2019, 10:04 last edited by
                    #10

                    @JasmineSethi
                    A explanation about why it is happening.
                    https://stackoverflow.com/questions/18221963/javascript-function-fails-to-return-object-when-there-is-a-line-break-between-th

                    @Computer Science Student - Brazil
                    Web Developer and Researcher
                    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                    J 1 Reply Last reply 24 Apr 2019, 10:24
                    0
                    • K KillerSmath
                      24 Apr 2019, 10:04

                      @JasmineSethi
                      A explanation about why it is happening.
                      https://stackoverflow.com/questions/18221963/javascript-function-fails-to-return-object-when-there-is-a-line-break-between-th

                      J Offline
                      J Offline
                      JasmineSethi
                      wrote on 24 Apr 2019, 10:24 last edited by
                      #11

                      @KillerSmath
                      Yeah i went through this link. Then i think its not a good practice to keep the return statement for more than one line. Then wt's the best solution to do qt test for the problem i stated? Any idea?

                      K 1 Reply Last reply 24 Apr 2019, 10:36
                      0
                      • J JasmineSethi
                        24 Apr 2019, 10:24

                        @KillerSmath
                        Yeah i went through this link. Then i think its not a good practice to keep the return statement for more than one line. Then wt's the best solution to do qt test for the problem i stated? Any idea?

                        K Offline
                        K Offline
                        KillerSmath
                        wrote on 24 Apr 2019, 10:36 last edited by KillerSmath
                        #12

                        @JasmineSethi
                        You could call the change function and test each property with individual compare calls. And btw, it is better than using an object, because in the case of object, the error output will print "Object is different of Object".
                        but using the unique property, the value of the properties will be printed, facilitating the diagnosis.

                        @Computer Science Student - Brazil
                        Web Developer and Researcher
                        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                        J 1 Reply Last reply 24 Apr 2019, 11:59
                        0
                        • K KillerSmath
                          24 Apr 2019, 10:36

                          @JasmineSethi
                          You could call the change function and test each property with individual compare calls. And btw, it is better than using an object, because in the case of object, the error output will print "Object is different of Object".
                          but using the unique property, the value of the properties will be printed, facilitating the diagnosis.

                          J Offline
                          J Offline
                          JasmineSethi
                          wrote on 24 Apr 2019, 11:59 last edited by JasmineSethi
                          #13

                          @KillerSmath
                          Hi,
                          I took an array and kept all the properties like :
                          property variant getdisablearray: [buttonAudioPause.color,
                          buttonAudioPause.border.color, mainAudioPauseButton.enabled, properties.btnTextColor]

                          In get() returned this array
                          function getDisableState()
                          {
                          return properties.getdisablearray
                          }

                          //Test case looks like below:
                          property variant abc:[]

                          TestCase
                          {
                              name: "Disable state for audio pause"
                              when: windowShown
                          
                              function test_disableStateForAudioPauseButton()
                              {
                                  rsbbAudioPauseButton.setDisableState(true)
                                  abc = rsbbAudioPauseButton.getDisableState()  
                                  compare(abc[0],"#17202a","test not pass")
                                  compare(abc[1],"#17202a","test not pass")
                                  compare(abc[2],false,"test not pass")
                                  compare(abc[3],"#666666","test not pass")
                          
                              }
                          }
                          
                          1 Reply Last reply
                          0

                          11/13

                          24 Apr 2019, 10:24

                          • Login

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