~chris.gagnon/+junk/qtpim-coverage

« back to all changes in this revision

Viewing changes to tests/auto/contacts/qmlcontacts/testcases/tst_contacts_intersection_filter_e2e.qml

  • Committer: chris.gagnon
  • Date: 2013-12-10 23:09:37 UTC
  • Revision ID: chris.gagnon@canonical.com-20131210230937-2akf1ft1edcttk87
first post

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtPim module 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 Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
import QtQuick 2.0
 
43
import QtTest 1.0
 
44
import QtContacts 5.0
 
45
 
 
46
ContactsSavingTestCase {
 
47
    name: "ContactsIntersectionFilterTests"
 
48
 
 
49
    ContactModel {
 
50
        id: model
 
51
        manager: getManagerUnderTest()
 
52
        autoUpdate: true
 
53
    }
 
54
 
 
55
    IntersectionFilter {
 
56
        id: intersectionFilter
 
57
    }
 
58
 
 
59
    DetailFilter {
 
60
        id: detailFilter1
 
61
        detail: ContactDetail.Name
 
62
        field: Name.FirstName
 
63
        value: "1"
 
64
        matchFlags: Filter.MatchExactly
 
65
    }
 
66
 
 
67
    DetailFilter {
 
68
        id: detailFilterB
 
69
        detail: ContactDetail.Name
 
70
        field: Name.LastName
 
71
        value: "B"
 
72
        matchFlags: Filter.MatchExactly
 
73
    }
 
74
 
 
75
    IdFilter { // contact id:s are set in initTestCase()
 
76
        id: idFilter_1B_2B_3B
 
77
    }
 
78
 
 
79
    IdFilter { // contact id:s are set in initTestCase()
 
80
        id: idFilter_2A_3A_1B_2B
 
81
    }
 
82
 
 
83
    Contact {
 
84
        id: contact1A
 
85
        Name {
 
86
            firstName: "1"
 
87
            lastName: "A"
 
88
        }
 
89
    }
 
90
 
 
91
    Contact {
 
92
        id: contact2A
 
93
        Name {
 
94
            firstName: "2"
 
95
            lastName: "A"
 
96
        }
 
97
    }
 
98
 
 
99
    Contact {
 
100
        id: contact3A
 
101
        Name {
 
102
            firstName: "3"
 
103
            lastName: "A"
 
104
        }
 
105
    }
 
106
 
 
107
    Contact {
 
108
        id: contact1B
 
109
        Name {
 
110
            firstName: "1"
 
111
            lastName: "B"
 
112
        }
 
113
    }
 
114
 
 
115
    Contact {
 
116
        id: contact2B
 
117
        Name {
 
118
            firstName: "2"
 
119
            lastName: "B"
 
120
        }
 
121
    }
 
122
 
 
123
    Contact {
 
124
        id: contact3B
 
125
        Name {
 
126
            firstName: "3"
 
127
            lastName: "B"
 
128
        }
 
129
    }
 
130
 
 
131
    function initTestCase() {
 
132
        initTestForModel(model);
 
133
        waitForContactsChanged();
 
134
        // The wait is needed so the model is populated
 
135
        // (e.g. with garbage left from previous test runs)
 
136
        // before cleanup() is called.
 
137
        emptyContacts(model);
 
138
        saveContactsToModel();
 
139
        setupIdFilter();
 
140
    }
 
141
 
 
142
    function saveContactsToModel() {
 
143
        model.saveContact(contact1A);
 
144
        waitForContactsChanged();
 
145
        model.saveContact(contact2A);
 
146
        waitForContactsChanged();
 
147
        model.saveContact(contact3A);
 
148
        waitForContactsChanged();
 
149
        model.saveContact(contact1B);
 
150
        waitForContactsChanged();
 
151
        model.saveContact(contact2B);
 
152
        waitForContactsChanged();
 
153
        model.saveContact(contact3B);
 
154
        waitForContactsChanged();
 
155
        compare(model.contacts.length, 6);
 
156
    }
 
157
 
 
158
    function setupIdFilter() {
 
159
        idFilter_1B_2B_3B.ids = [findId("1", "B"), // contacts in model aren't guaranteed to be in the saving order, so find is needed
 
160
                                 findId("2", "B"),
 
161
                                 findId("3", "B")];
 
162
        idFilter_2A_3A_1B_2B.ids = [findId("2", "A"),
 
163
                                    findId("3", "A"),
 
164
                                    findId("1", "B"),
 
165
                                    findId("2", "B")];
 
166
    }
 
167
 
 
168
    function findId(firstName, lastName) {
 
169
        for (var i = 0; i < model.contacts.length; i++) {
 
170
            if (model.contacts[i].name.firstName == firstName &&
 
171
                    model.contacts[i].name.lastName == lastName)
 
172
                return model.contacts[i].contactId;
 
173
        }
 
174
    }
 
175
 
 
176
    function test_intersectionFilter_data() {
 
177
        return [{
 
178
                    tag: "Intersection of two detail filters",
 
179
                    filters: [detailFilter1, detailFilterB],
 
180
                    intersection: [contact1B]
 
181
                },
 
182
                {
 
183
                    tag: "Intersection of detail filter and id filter",
 
184
                    filters: [detailFilterB, idFilter_2A_3A_1B_2B],
 
185
                    intersection: [contact1B, contact2B]
 
186
                },
 
187
                {
 
188
                    tag: "Intersection of two id filters",
 
189
                    filters: [idFilter_1B_2B_3B, idFilter_2A_3A_1B_2B],
 
190
                    intersection: [contact1B, contact2B]
 
191
                },
 
192
                {
 
193
                    tag: "Intersection of two id filters and detail filter",
 
194
                    filters: [detailFilter1, idFilter_1B_2B_3B, idFilter_2A_3A_1B_2B],
 
195
                    intersection: [contact1B]
 
196
                },
 
197
                {
 
198
                    tag: "Intersection filter with empty filter set (manager: jsondb)",
 
199
                    manager: "jsondb",
 
200
                    filters: [],
 
201
                    intersection: [contact1A, contact2A, contact3A, contact1B, contact2B, contact3B]
 
202
                },
 
203
                {
 
204
                    tag: "Intersection filter with empty filter set (manager: memory)",
 
205
                    manager: "memory",
 
206
                    filters: [],
 
207
                    intersection: []
 
208
                },
 
209
                {
 
210
                    tag: "Intersection filter with a single detail filter",
 
211
                    filters: [detailFilter1],
 
212
                    intersection: [contact1A, contact1B]
 
213
                }
 
214
                ];
 
215
    }
 
216
 
 
217
    function test_intersectionFilter(data) {
 
218
        if (data.manager && data.manager !== model.manager)
 
219
            skip("Test not applicable to this manager");
 
220
        intersectionFilter.filters = data.filters;
 
221
        model.filter = intersectionFilter;
 
222
        waitForContactsChanged();
 
223
        compareContacts(model.contacts, data.intersection)
 
224
    }
 
225
 
 
226
    function compareContacts(set1, set2) {
 
227
        // Compare filtered set to expected set.
 
228
        // All contacts in sets are assumed to have unique firstName and lastName detail pairs.
 
229
        compare(set1.length, set2.length);
 
230
        for (var i = 0; i < set1.length; i++) {
 
231
            var match = false;
 
232
            for (var j = 0; j < set2.length; j++) {
 
233
                if (set1[i].name.firstName == set2[j].name.firstName &&
 
234
                    set1[i].name.lastName == set2[j].name.lastName)
 
235
                    match = true;
 
236
            }
 
237
            compare(match, true);
 
238
        }
 
239
    }
 
240
 
 
241
    function cleanup() {
 
242
        if (model.filter) {
 
243
            model.filter = null;
 
244
            waitForContactsChanged();
 
245
        }
 
246
    }
 
247
 
 
248
    function cleanupTestCase() {
 
249
        emptyContacts(model);
 
250
        finishTestForModel(model);
 
251
    }
 
252
 
 
253
}