From fbcc860f28bbd062855359c635dd67420d6e8c9e Mon Sep 17 00:00:00 2001
From: J-P Nurmi <jpnurmi@theqtcompany.com>
Date: Tue, 25 Nov 2014 14:02:03 +0100
Subject: [PATCH] Fix StackView focus handling

Make StackView a FocusScope so inner focus changes don't have
undesired side effects in other parts of the application.

Task-number: QTBUG-42506
Change-Id: I0d901083196cf90f236a24e1dcd2e91bd9f320db
Reviewed-by: Mitch Curtis <mitch.curtis@digia.com>
Reviewed-by: Caroline Chao <caroline.chao@theqtcompany.com>
---
 src/controls/StackView.qml                 |  2 +-
 tests/auto/controls/controls.pro           |  2 ++
 tests/auto/controls/data/tst_stackview.qml | 26 +++++++++++++++++++++-
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/src/controls/StackView.qml b/src/controls/StackView.qml
index bd9c4de9c..002355dc7 100644
--- a/src/controls/StackView.qml
+++ b/src/controls/StackView.qml
@@ -473,7 +473,7 @@ import QtQuick.Controls.Private 1.0
     \endlist
 */
 
-Item {
+FocusScope {
     id: root
 
     /*! \qmlproperty int StackView::depth
diff --git a/tests/auto/controls/controls.pro b/tests/auto/controls/controls.pro
index c3f000941..6ba1e3ea6 100644
--- a/tests/auto/controls/controls.pro
+++ b/tests/auto/controls/controls.pro
@@ -34,6 +34,8 @@ OTHER_FILES += \
     $$PWD/data/tst_rowlayout.qml \
     $$PWD/data/tst_gridlayout.qml \
     $$PWD/data/tst_slider.qml \
+    $$PWD/data/tst_stack.qml \
+    $$PWD/data/tst_stackview.qml \
     $$PWD/data/tst_statusbar.qml \
     $$PWD/data/tst_switch.qml \
     $$PWD/data/tst_tab.qml \
diff --git a/tests/auto/controls/data/tst_stackview.qml b/tests/auto/controls/data/tst_stackview.qml
index 848be7258..f574feedf 100644
--- a/tests/auto/controls/data/tst_stackview.qml
+++ b/tests/auto/controls/data/tst_stackview.qml
@@ -46,10 +46,12 @@ TestCase {
     id: testCase
     name: "Tests_StackView"
     when: windowShown
+    visible: true
     width: 400
     height: 400
 
     Item { id: anItem  }
+    TextField { id: textField }
     Component {
         id: pageComponent
         Item {}
@@ -57,7 +59,7 @@ TestCase {
 
     Component {
         id: stackComponent
-        StackView {}
+        StackView { anchors.fill: parent }
     }
 
     function test_stackview() {
@@ -73,5 +75,27 @@ TestCase {
         verify (stack.depth === 1)
         stack.push(pageComponent)
         verify (stack.depth === 2)
+        stack.destroy()
+    }
+
+    function test_focus() {
+        var stack = stackComponent.createObject(testCase, {initialItem: anItem})
+        verify (stack !== null, "stackview created is null")
+        compare(stack.currentItem, anItem)
+
+        stack.forceActiveFocus()
+        verify(stack.activeFocus)
+
+        stack.push({item: textField, immediate: true})
+        compare(stack.currentItem, textField)
+        textField.forceActiveFocus()
+        verify(textField.activeFocus)
+
+        stack.pop({immediate: true})
+        compare(stack.currentItem, anItem)
+        verify(stack.activeFocus)
+        verify(!textField.activeFocus)
+
+        stack.destroy()
     }
 }
-- 
GitLab