1/*
2 *  SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
3 *
4 *  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7import QtQuick 2.7
8import QtQuick.Controls 2.0
9import QtQuick.Window 2.1
10import org.kde.kirigami 2.4 as Kirigami
11import QtTest 1.0
12
13TestCase {
14    id: testCase
15    width: 400
16    height: 400
17    name: "GoBack"
18
19    function applicationWindow() { return mainWindow; }
20
21    Kirigami.ApplicationWindow {
22        id: mainWindow
23        width: 480
24        height: 360
25        pageStack.initialPage: Kirigami.Page {
26            Rectangle {
27                anchors.fill: parent
28                color: "green"
29            }
30        }
31    }
32
33    Component {
34        id: randomPage
35        Kirigami.Page {
36            Rectangle {
37                anchors.fill: parent
38                color: "red"
39            }
40        }
41    }
42
43    SignalSpy {
44        id: spyCurrentIndex
45        target: mainWindow.pageStack
46        signalName: "currentIndexChanged"
47    }
48
49    SignalSpy {
50        id: spyActive
51        target: mainWindow
52        signalName: "activeChanged"
53    }
54
55    function initTestCase() {
56        mainWindow.show()
57    }
58
59    function cleanupTestCase() {
60        mainWindow.close()
61    }
62
63    function init() {
64        mainWindow.pageStack.clear()
65        spyActive.clear()
66        spyCurrentIndex.clear()
67    }
68
69    function test_pop() {
70        compare(mainWindow.pageStack.depth, 0)
71        mainWindow.pageStack.push(randomPage)
72        compare(mainWindow.pageStack.depth, 1)
73        mainWindow.pageStack.pop()
74        compare(mainWindow.pageStack.depth, 0)
75    }
76
77    function test_goBack() {
78        compare(mainWindow.pageStack.depth, 0)
79        mainWindow.pageStack.push(randomPage)
80        mainWindow.pageStack.push(randomPage)
81        compare(mainWindow.pageStack.depth, 2)
82        compare(mainWindow.pageStack.currentIndex, 1)
83        compare(spyCurrentIndex.count, 3)
84        spyActive.clear()
85        mainWindow.requestActivate()
86        spyCurrentIndex.clear()
87        if (!mainWindow.active)
88            spyActive.wait()
89        verify(mainWindow.active)
90        keyClick(Qt.Key_Left, Qt.AltModifier)
91
92        spyCurrentIndex.wait()
93
94        compare(mainWindow.pageStack.depth, 2)
95        compare(mainWindow.pageStack.currentIndex, 0)
96        compare(spyCurrentIndex.count, 1)
97        mainWindow.pageStack.pop()
98        compare(mainWindow.pageStack.depth, 1)
99    }
100
101    property int destructions: 0
102    Component {
103        id: destroyedPage
104        Kirigami.Page {
105            id: page
106            Rectangle {
107                anchors.fill: parent
108                color: "blue"
109                Component.onDestruction: {
110                    testCase.destructions++
111                }
112            }
113        }
114    }
115    SignalSpy {
116        id: spyDestructions
117        target: testCase
118        signalName: "destructionsChanged"
119    }
120    function test_clearPages() {
121        mainWindow.pageStack.push(destroyedPage)
122        mainWindow.pageStack.push(destroyedPage)
123        mainWindow.pageStack.push(destroyedPage)
124        compare(mainWindow.pageStack.depth, 3)
125        mainWindow.pageStack.clear()
126
127        compare(mainWindow.pageStack.depth, 0)
128        spyDestructions.wait()
129        compare(testCase.destructions, 2)
130    }
131}
132