diff --git a/storybook/main.qml b/storybook/main.qml index 216f9d8b6a..0eb1cbc0bc 100644 --- a/storybook/main.qml +++ b/storybook/main.qml @@ -89,6 +89,10 @@ ApplicationWindow { onReloaded: hotReloaderControls.notifyReload() } + TestRunnerController { + id: testRunnerController + } + SplitView { anchors.fill: parent @@ -190,6 +194,8 @@ ApplicationWindow { figmaPagesCount: currentPageModelItem.object ? currentPageModelItem.object.figma.count : 0 + testRunnerController: testRunnerController + Instantiator { id: currentPageModelItem diff --git a/storybook/src/Storybook/PageToolBar.qml b/storybook/src/Storybook/PageToolBar.qml index 0f75f1fd90..03c53daebb 100644 --- a/storybook/src/Storybook/PageToolBar.qml +++ b/storybook/src/Storybook/PageToolBar.qml @@ -1,16 +1,15 @@ -import QtQuick 2.14 -import QtQuick.Controls 2.14 -import QtQuick.Layouts 1.14 - -import Storybook 1.0 +import QtQuick 2.15 +import QtQuick.Controls 2.15 +import QtQuick.Layouts 1.15 ToolBar { id: root property string componentName - property int figmaPagesCount: 0 + required property TestRunnerController testRunnerController + signal figmaPreviewClicked signal inspectClicked @@ -59,49 +58,54 @@ ToolBar { TestRunnerControls { id: testRunnerControls - property var testProcess: null readonly property string testFileName: `tst_${root.componentName}.qml` onTestFileNameChanged: { - if (testRunnerControls.testProcess) - testRunnerControls.testProcess.kill() + if (testRunnerController.running) + testRunnerController.abort() testRunnerControls.mode = TestRunnerControls.Mode.Base } - onRunClicked: { - const testsCount = TestsRunner.testsCount(testFileName) + Connections { + target: testRunnerController - if (testsCount === 0) - return noTestsDialog.open() + function onStarted() { + testRunnerControls.mode = TestRunnerControls.Mode.InProgress + } - testRunnerControls.mode = TestRunnerControls.Mode.InProgress - - const process = TestsRunner.runTests(testFileName) - testRunnerControls.testProcess = process - - process.finished.connect((exitCode, exitStatus) => { + function onFinished(failedTests, aborted, crashed) { if (testRunnerControls.mode !== TestRunnerControls.Mode.InProgress) return - if (exitStatus) { + if (aborted) { + testRunnerControls.mode = TestRunnerControls.Mode.Aborted + return + } + + if (crashed) { testRunnerControls.mode = TestRunnerControls.Mode.Crashed return } - if (exitCode) - testRunnerControls.mode = TestRunnerControls.Mode.Failed - else - testRunnerControls.mode = TestRunnerControls.Mode.Success + testRunnerControls.mode = failedTests + ? TestRunnerControls.Mode.Failed + : TestRunnerControls.Mode.Success - testRunnerControls.numberOfFailedTests = exitCode - }) + testRunnerControls.numberOfFailedTests = failedTests + } } - onAbortClicked: { - testRunnerControls.testProcess.kill() - testRunnerControls.mode = TestRunnerControls.Mode.Aborted + onRunClicked: { + const testsCount = testRunnerController.getTestsCount(testFileName) + + if (testsCount === 0) + return noTestsDialog.open() + + testRunnerController.runTests(testFileName) } + + onAbortClicked: testRunnerController.abort() } ToolSeparator {} @@ -141,7 +145,7 @@ ToolBar { : "" } - onAccepted: Qt.openUrlExternally(Qt.resolvedUrl(TestsRunner.testsPath())) + onAccepted: Qt.openUrlExternally(testRunnerController.getTestsPath()) Component.onCompleted: standardButton(Dialog.Ok).text = "Open tests folder" } } diff --git a/storybook/src/Storybook/TestRunnerController.qml b/storybook/src/Storybook/TestRunnerController.qml new file mode 100644 index 0000000000..42df762390 --- /dev/null +++ b/storybook/src/Storybook/TestRunnerController.qml @@ -0,0 +1,51 @@ +import QtQml 2.15 + +import Storybook 1.0 + +QtObject { + id: root + + readonly property alias running: d.running + readonly property alias aborted: d.aborted + + signal started + signal finished(int failedTests, bool aborted, bool crashed) + + function getTestsCount(testFileName) { + return TestsRunner.testsCount(testFileName) + } + + function getTestsPath() { + return Qt.resolvedUrl(TestsRunner.testsPath()) + } + + function runTests(testFileName) { + console.assert(d.testProcess === null) + + const process = TestsRunner.runTests(testFileName) + d.testProcess = process + d.running = true + + process.finished.connect((exitCode, exitStatus) => { + root.finished(exitCode, d.aborted, exitStatus !== 0) + + d.running = false + d.aborted = false + }) + + started() + } + + function abort() { + d.aborted = true + d.testProcess.kill() + } + + readonly property QtObject _d: QtObject { + id: d + + property var testProcess: null + property bool aborted: false + property bool running: false + } +} diff --git a/storybook/src/Storybook/qmldir b/storybook/src/Storybook/qmldir index 7f0a10c78f..53632a3e14 100644 --- a/storybook/src/Storybook/qmldir +++ b/storybook/src/Storybook/qmldir @@ -25,6 +25,7 @@ PopupBackground 1.0 PopupBackground.qml SettingsLayout 1.0 SettingsLayout.qml SingleItemProxyModel 1.0 SingleItemProxyModel.qml SourceCodeBox 1.0 SourceCodeBox.qml +TestRunnerController 1.0 TestRunnerController.qml TestRunnerControls 1.0 TestRunnerControls.qml singleton FigmaUtils 1.0 FigmaUtils.qml singleton InspectionUtils 1.0 InspectionUtils.qml