SignalSpy.qml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the test suite of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQuick 2.0
  40. import QtTest 1.1
  41. /*!
  42. \qmltype SignalSpy
  43. \inqmlmodule QtTest
  44. \brief Enables introspection of signal emission
  45. \since 4.8
  46. \ingroup qtquicktest
  47. In the following example, a SignalSpy is installed to watch the
  48. "clicked" signal on a user-defined Button type. When the signal
  49. is emitted, the \l count property on the spy will be increased.
  50. \code
  51. Button {
  52. id: button
  53. SignalSpy {
  54. id: spy
  55. target: button
  56. signalName: "clicked"
  57. }
  58. TestCase {
  59. name: "ButtonClick"
  60. function test_click() {
  61. compare(spy.count, 0)
  62. button.clicked();
  63. compare(spy.count, 1)
  64. }
  65. }
  66. }
  67. \endcode
  68. The above style of test is suitable for signals that are emitted
  69. synchronously. For asynchronous signals, the wait() method can be
  70. used to block the test until the signal occurs (or a timeout expires).
  71. \sa {QtTest::TestCase}{TestCase}, {Qt Quick Test Reference Documentation}
  72. */
  73. Item {
  74. id: spy
  75. visible: false
  76. TestUtil {
  77. id: util
  78. }
  79. // Public API.
  80. /*!
  81. \qmlproperty object SignalSpy::target
  82. This property defines the target object that will be used to
  83. listen for emissions of the \l signalName signal.
  84. \sa signalName, count
  85. */
  86. property var target: null
  87. /*!
  88. \qmlproperty string SignalSpy::signalName
  89. This property defines the name of the signal on \l target to
  90. listen for.
  91. \sa target, count
  92. */
  93. property string signalName: ""
  94. /*!
  95. \qmlproperty int SignalSpy::count
  96. This property defines the number of times that \l signalName has
  97. been emitted from \l target since the last call to clear().
  98. \sa target, signalName, clear()
  99. \readonly
  100. */
  101. readonly property alias count: spy.qtest_count
  102. /*!
  103. \qmlproperty bool SignalSpy::valid
  104. This property defines the current signal connection status. It will be true when the \l signalName of the \l target is connected successfully, otherwise it will be false.
  105. \sa count, target, signalName, clear()
  106. \readonly
  107. */
  108. readonly property alias valid:spy.qtest_valid
  109. /*!
  110. \qmlproperty list SignalSpy::signalArguments
  111. This property holds a list of emitted signal arguments. Each emission of the signal will append one item to the list, containing the arguments of the signal.
  112. When connecting to a new \l target or new \l signalName or calling the \l clear() method, the \l signalArguments will be reset to empty.
  113. \sa signalName, clear()
  114. \readonly
  115. */
  116. readonly property alias signalArguments:spy.qtest_signalArguments
  117. /*!
  118. \qmlmethod SignalSpy::clear()
  119. Clears \l count to 0, resets \l valid to false and clears the \l signalArguments to empty.
  120. \sa count, wait()
  121. */
  122. function clear() {
  123. qtest_count = 0
  124. qtest_expectedCount = 0
  125. qtest_signalArguments = []
  126. }
  127. /*!
  128. \qmlmethod SignalSpy::wait(timeout = 5000)
  129. Waits for the signal \l signalName on \l target to be emitted,
  130. for up to \a timeout milliseconds. The test case will fail if
  131. the signal is not emitted.
  132. \code
  133. SignalSpy {
  134. id: spy
  135. target: button
  136. signalName: "clicked"
  137. }
  138. function test_async_click() {
  139. ...
  140. // do something that will cause clicked() to be emitted
  141. ...
  142. spy.wait()
  143. compare(spy.count, 1)
  144. }
  145. \endcode
  146. There are two possible scenarios: the signal has already been
  147. emitted when wait() is called, or the signal has not yet been
  148. emitted. The wait() function handles the first scenario by immediately
  149. returning if the signal has already occurred.
  150. The clear() method can be used to discard information about signals
  151. that have already occurred to synchronize wait() with future signal
  152. emissions.
  153. \sa clear(), TestCase::tryCompare()
  154. */
  155. function wait(timeout) {
  156. if (timeout === undefined)
  157. timeout = 5000
  158. var expected = ++qtest_expectedCount
  159. var i = 0
  160. while (i < timeout && qtest_count < expected) {
  161. qtest_results.wait(50)
  162. i += 50
  163. }
  164. var success = (qtest_count >= expected)
  165. if (!qtest_results.verify(success, "wait for signal " + signalName, util.callerFile(), util.callerLine()))
  166. throw new Error("QtQuickTest::fail")
  167. }
  168. // Internal implementation detail follows.
  169. TestResult { id: qtest_results }
  170. onTargetChanged: {
  171. qtest_update()
  172. }
  173. onSignalNameChanged: {
  174. qtest_update()
  175. }
  176. /*! \internal */
  177. property var qtest_prevTarget: null
  178. /*! \internal */
  179. property string qtest_prevSignalName: ""
  180. /*! \internal */
  181. property int qtest_expectedCount: 0
  182. /*! \internal */
  183. property var qtest_signalArguments:[]
  184. /*! \internal */
  185. property int qtest_count: 0
  186. /*! \internal */
  187. property bool qtest_valid:false
  188. /*! \internal */
  189. /*! \internal */
  190. function qtest_update() {
  191. if (qtest_prevTarget != null) {
  192. var prevHandlerName = qtest_signalHandlerName(qtest_prevSignalName)
  193. var prevFunc = qtest_prevTarget[prevHandlerName]
  194. if (prevFunc)
  195. prevFunc.disconnect(spy.qtest_activated)
  196. qtest_prevTarget = null
  197. qtest_prevSignalName = ""
  198. }
  199. if (target != null && signalName != "") {
  200. var handlerName = qtest_signalHandlerName(signalName)
  201. var func = target[handlerName]
  202. if (func === undefined) {
  203. spy.qtest_valid = false
  204. console.log("Signal '" + signalName + "' not found")
  205. } else {
  206. qtest_prevTarget = target
  207. qtest_prevSignalName = signalName
  208. func.connect(spy.qtest_activated)
  209. spy.qtest_valid = true
  210. spy.qtest_signalArguments = []
  211. }
  212. } else {
  213. spy.qtest_valid = false
  214. }
  215. }
  216. /*! \internal */
  217. function qtest_activated() {
  218. ++qtest_count
  219. spy.qtest_signalArguments[spy.qtest_signalArguments.length] = arguments
  220. }
  221. /*! \internal */
  222. function qtest_signalHandlerName(sn) {
  223. if (sn.substr(0, 2) === "on" && sn[2] === sn[2].toUpperCase())
  224. return sn
  225. return "on" + sn.substr(0, 1).toUpperCase() + sn.substr(1)
  226. }
  227. }