~ci-train-bot/ubuntu-settings-components/ubuntu-settings-components-ubuntu-zesty-2397

« back to all changes in this revision

Viewing changes to tests/qmltests/Components/tst_FilePicker.qml

  • Committer: Bileto Bot
  • Date: 2017-01-27 15:03:09 UTC
  • mfrom: (155.3.12 filepicker)
  • Revision ID: ci-train-bot@canonical.com-20170127150309-sa1soll41wiqdzul
create file picker component for export  

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by Jonas G. Drange <jonas.drange@canonical.com>
 
17
 */
 
18
 
 
19
import Qt.labs.folderlistmodel 2.1
 
20
import QtQuick 2.4
 
21
import QtTest 1.0
 
22
import Ubuntu.Components 1.3
 
23
import Ubuntu.Components.Popups 1.3
 
24
import Ubuntu.Settings.Components 0.1
 
25
import Ubuntu.Test 0.1
 
26
 
 
27
MainView {
 
28
    id: testRoot
 
29
    width: units.gu(42)
 
30
    height: units.gu(75)
 
31
 
 
32
    Component {
 
33
        id: filePickerComp
 
34
 
 
35
        FilePicker {}
 
36
    }
 
37
 
 
38
    Component {
 
39
        id: fsModelComp
 
40
 
 
41
        FolderListModel {}
 
42
    }
 
43
 
 
44
    SignalSpy {
 
45
        id: spy
 
46
    }
 
47
 
 
48
    UbuntuTestCase {
 
49
        name: "FilePicker"
 
50
        when: windowShown
 
51
 
 
52
        property var instance: null
 
53
        property var fsModel: null
 
54
 
 
55
        function init() {
 
56
            fsModel = fsModelComp.createObject(testRoot);
 
57
        }
 
58
 
 
59
        function cleanup() {
 
60
            PopupUtils.close(instance);
 
61
            tryCompareFunction(function () {
 
62
                return !!findChild(testRoot, "filePicker");
 
63
            }, false);
 
64
            fsModel.destroy();
 
65
            spy.clear();
 
66
            spy.target = null;
 
67
            spy.signalName = "";
 
68
        }
 
69
 
 
70
        function test_breadCrumb_data() {
 
71
            return [
 
72
                {
 
73
                    folder: "file:///home/user/",
 
74
                    parentFolder: "file:///home",
 
75
                    count: 0,
 
76
                    targetText: i18n.dtr("ubuntu-settings-components", "%1 (%2 files)")
 
77
                        .arg(i18n.tr("Device") + "/home/user")
 
78
                        .arg(0)
 
79
                },
 
80
                {
 
81
                    folder: "file:///",
 
82
                    parentFolder: "file:///",
 
83
                    count: 10,
 
84
                    targetText: i18n.dtr("ubuntu-settings-components", "%1 (%2 files)")
 
85
                        .arg(i18n.tr("Device"))
 
86
                        .arg(10)
 
87
                },
 
88
                {
 
89
                    folder: "file:///bar/baz",
 
90
                    parentFolder: "file:///bar",
 
91
                    count: 1,
 
92
                    targetText: i18n.dtr("ubuntu-settings-components", "%1 (%2 file)")
 
93
                        .arg(i18n.tr("Device") + "/bar/baz")
 
94
                        .arg(1)
 
95
                },
 
96
            ]
 
97
        }
 
98
 
 
99
        function test_breadCrumb(data) {
 
100
            fsModel.folder = data.folder;
 
101
            fsModel.parentFolder = data.parentFolder;
 
102
            fsModel.count = data.count;
 
103
            instance = PopupUtils.open(filePickerComp, null, { "fsModel": fsModel });
 
104
            waitForRendering(instance);
 
105
 
 
106
            var crumbs = findChild(instance, "filePickerBreadcrumb");
 
107
            compare(crumbs.text, data.targetText)
 
108
        }
 
109
 
 
110
        function test_backButtonEnabled_data() {
 
111
            return [
 
112
                { folder: "file:///", parentFolder: "file:///", targetEnabled: false },
 
113
                { folder: "file:///bar", parentFolder: "file:///", targetEnabled: true },
 
114
                { folder: "file:///bar/baz", parentFolder: "file:///bar", targetEnabled: true },
 
115
            ]
 
116
        }
 
117
 
 
118
        function test_backButtonEnabled(data) {
 
119
            fsModel.folder = data.folder;
 
120
            fsModel.parentFolder = data.parentFolder;
 
121
            instance = PopupUtils.open(filePickerComp, null, { "fsModel": fsModel });
 
122
            waitForRendering(instance);
 
123
 
 
124
            var button = findChild(instance, "filePickerBackButton");
 
125
            compare(button.enabled, data.targetEnabled);
 
126
        }
 
127
 
 
128
        function test_backButtonAction_data() {
 
129
            return [
 
130
                { folder: "file:///", parentFolder: "file:///" },
 
131
                { folder: "file:///bar", parentFolder: "file:///" },
 
132
                { folder: "file:///bar/baz", parentFolder: "file:///bar" }
 
133
            ]
 
134
        }
 
135
 
 
136
        function test_backButtonAction(data) {
 
137
            fsModel.folder = data.folder;
 
138
            fsModel.parentFolder = data.parentFolder;
 
139
            instance = PopupUtils.open(filePickerComp, null, { "fsModel": fsModel });
 
140
            waitForRendering(instance);
 
141
 
 
142
            var button = findChild(instance, "filePickerBackButton");
 
143
            mouseClick(button, button.width / 2, button.height / 2);
 
144
            compare(fsModel.folder, fsModel.parentFolder);
 
145
        }
 
146
 
 
147
        function test_renderFiles_data() {
 
148
            return [
 
149
                { files: [ ["fileA.txt", "/home/user/fileA.txt", "", 5000, "txt", false, true] ] },
 
150
                { files: [
 
151
                    ["fileA.txt", "/home/user/fileA.txt", "", 5000, "txt", false, true],
 
152
                    ["fileB.txt", "/fileB.txt", "", 5000, "txt", false, true],
 
153
                    ["somedir", "/somedir", "somedir", 4001, "", true, false]
 
154
                ]}
 
155
            ]
 
156
        }
 
157
 
 
158
        function test_renderFiles(data) {
 
159
            for (var i = 0; i < data.files.length; i++) {
 
160
                fsModel.mockAddFile.apply(fsModel, data.files[i]);
 
161
            }
 
162
            instance = PopupUtils.open(filePickerComp, null, { "fsModel": fsModel });
 
163
            waitForRendering(instance);
 
164
 
 
165
            // Make sure each file was rendered.
 
166
            for (var i = 0; i < data.files.length; i++) {
 
167
                verify(findChild(instance, "filePickerFileItem_" + i));
 
168
            }
 
169
        }
 
170
 
 
171
        function test_accept_data() {
 
172
            var files = [
 
173
                ["fileA.txt", "file:///home/user/fileA.txt", "", 5000, "txt", false, true],
 
174
                ["fileB.txt", "file:///fileB.txt", "", 5000, "txt", false, true]
 
175
            ]
 
176
            return [
 
177
                { "files": files, accept: 0, targetAcceptPath: "file:///home/user/fileA.txt" },
 
178
                { "files": files, accept: 1, targetAcceptPath: "file:///fileB.txt" },
 
179
            ]
 
180
        }
 
181
 
 
182
        function test_accept(data) {
 
183
            for (var i = 0; i < data.files.length; i++) {
 
184
                fsModel.mockAddFile.apply(fsModel, data.files[i]);
 
185
            }
 
186
            instance = PopupUtils.open(filePickerComp, null, { "fsModel": fsModel });
 
187
            waitForRendering(instance);
 
188
 
 
189
            var item = findChild(instance, "filePickerFileItem_" + data.accept);
 
190
            mouseClick(item, item.width / 2, item.height / 2);
 
191
 
 
192
            spy.target = instance;
 
193
            spy.signalName = "accept";
 
194
 
 
195
            var btn = findChild(instance, "filePickerAccept");
 
196
            mouseClick(btn, btn.width / 2, btn.height / 2);
 
197
            spy.wait();
 
198
            compare(spy.count, 1);
 
199
            compare(spy.signalArguments[0][0], data.targetAcceptPath);
 
200
        }
 
201
 
 
202
        function test_reject() {
 
203
            instance = PopupUtils.open(filePickerComp, null, { "fsModel": fsModel });
 
204
            waitForRendering(instance);
 
205
 
 
206
            spy.target = instance;
 
207
            spy.signalName = "reject";
 
208
 
 
209
            var btn = findChild(instance, "filePickerCancel");
 
210
            mouseClick(btn, btn.width / 2, btn.height / 2);
 
211
            spy.wait();
 
212
            compare(spy.count, 1);
 
213
        }
 
214
    }
 
215
}