Welcome to mirror list, hosted at ThFree Co, Russian Federation.

testsortedsharemodel.cpp « test - github.com/nextcloud/desktop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5619d7bb5b213e560a5e54463e0f264e99ff0574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (C) by Claudio Cambra <claudio.cambra@nextcloud.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */

#include "gui/filedetails/sortedsharemodel.h"

#include <QTest>
#include <QAbstractItemModelTester>
#include <QSignalSpy>

#include "sharetestutils.h"

using namespace OCC;

class TestSortedShareModel : public QObject
{
    Q_OBJECT

public slots:
    void addAllTestShares()
    {
        // Let's insert them in the opposite order we want from the model
        for (auto it = _expectedOrder.crbegin(); it != _expectedOrder.crend(); ++it) {
            helper.appendShareReplyData(*it);
        }
    }

private:
    ShareTestHelper helper;

    FakeShareDefinition _userADefinition;
    FakeShareDefinition _userBDefinition;
    FakeShareDefinition _groupADefinition;
    FakeShareDefinition _groupBDefinition;
    FakeShareDefinition _linkADefinition;
    FakeShareDefinition _linkBDefinition;
    FakeShareDefinition _emailADefinition;
    FakeShareDefinition _emailBDefinition;
    FakeShareDefinition _remoteADefinition;
    FakeShareDefinition _remoteBDefinition;
    FakeShareDefinition _roomADefinition;
    FakeShareDefinition _roomBDefinition;

    QVector<FakeShareDefinition> _expectedOrder;

    static constexpr auto _expectedShareCount = 12;

private slots:
    void initTestCase()
    {
        QSignalSpy helperSetupSucceeded(&helper, &ShareTestHelper::setupSucceeded);
        helper.setup();
        QCOMPARE(helperSetupSucceeded.count(), 1);

        const auto userAShareWith = QStringLiteral("user_a");
        const auto userAShareWithDisplayName = QStringLiteral("User A");
        _userADefinition = FakeShareDefinition(&helper, Share::TypeUser, userAShareWith, userAShareWithDisplayName);

        const auto userBShareWith = QStringLiteral("user_b");
        const auto userBShareWithDisplayName = QStringLiteral("User B");
        _userBDefinition = FakeShareDefinition(&helper, Share::TypeUser, userBShareWith, userBShareWithDisplayName);

        const auto groupAShareWith = QStringLiteral("group_a");
        const auto groupAShareWithDisplayName = QStringLiteral("Group A");
        _groupADefinition = FakeShareDefinition(&helper, Share::TypeGroup, groupAShareWith, groupAShareWithDisplayName);

        const auto groupBShareWith = QStringLiteral("group_b");
        const auto groupBShareWithDisplayName = QStringLiteral("Group B");
        _groupBDefinition = FakeShareDefinition(&helper, Share::TypeGroup, groupBShareWith, groupBShareWithDisplayName);

        const auto linkALabel = QStringLiteral("Link share label A");
        _linkADefinition = FakeShareDefinition(&helper, Share::TypeLink, {}, linkALabel);

        const auto linkBLabel = QStringLiteral("Link share label B");
        _linkBDefinition = FakeShareDefinition(&helper, Share::TypeLink, {}, linkBLabel);

        const auto emailAShareWith = QStringLiteral("email_a@nextcloud.com");
        const auto emailAShareWithDisplayName = QStringLiteral("email_a@nextcloud.com");
        _emailADefinition = FakeShareDefinition(&helper, Share::TypeEmail, emailAShareWith, emailAShareWithDisplayName);

        const auto emailBShareWith = QStringLiteral("email_b@nextcloud.com");
        const auto emailBShareWithDisplayName = QStringLiteral("email_b@nextcloud.com");
        _emailBDefinition = FakeShareDefinition(&helper, Share::TypeEmail, emailBShareWith, emailBShareWithDisplayName);

        const auto remoteAShareWith = QStringLiteral("remote_a");
        const auto remoteAShareWithDisplayName = QStringLiteral("Remote share A");
        _remoteADefinition = FakeShareDefinition(&helper, Share::TypeRemote, remoteAShareWith, remoteAShareWithDisplayName);

        const auto remoteBShareWith = QStringLiteral("remote_b");
        const auto remoteBShareWithDisplayName = QStringLiteral("Remote share B");
        _remoteBDefinition = FakeShareDefinition(&helper, Share::TypeRemote, remoteBShareWith, remoteBShareWithDisplayName);

        const auto roomAShareWith = QStringLiteral("room_a");
        const auto roomAShareWithDisplayName = QStringLiteral("Room A");
        _roomADefinition = FakeShareDefinition(&helper, Share::TypeRoom, roomAShareWith, roomAShareWithDisplayName);

        const auto roomBShareWith = QStringLiteral("room_b");
        const auto roomBShareWithDisplayName = QStringLiteral("Room B");
        _roomBDefinition = FakeShareDefinition(&helper, Share::TypeRoom, roomBShareWith, roomBShareWithDisplayName);

        _expectedOrder = {// Placeholder link shares always go first, followed by normal link shares.
                          _linkADefinition,
                          _linkBDefinition,
                          // For all other share types, we follow the Share::ShareType enum.
                          _userADefinition,
                          _userBDefinition,
                          _groupADefinition,
                          _groupBDefinition,
                          _emailADefinition,
                          _emailBDefinition,
                          _remoteADefinition,
                          _remoteBDefinition,
                          _roomADefinition,
                          _roomBDefinition};
    }

    void testSetModel()
    {
        helper.resetTestData();
        addAllTestShares();
        QCOMPARE(helper.shareCount(), _expectedShareCount);

        ShareModel model;
        QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
        model.setAccountState(helper.accountState.data());
        model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
        QVERIFY(sharesChanged.wait(5000));
        QCOMPARE(model.rowCount(), helper.shareCount());

        SortedShareModel sortedModel;
        QAbstractItemModelTester sortedModelTester(&sortedModel);
        QSignalSpy sortedModelReset(&sortedModel, &SortedShareModel::modelReset);
        QSignalSpy shareModelChanged(&sortedModel, &SortedShareModel::shareModelChanged);

        sortedModel.setShareModel(&model);
        QCOMPARE(shareModelChanged.count(), 1);
        QCOMPARE(sortedModelReset.count(), 1);
        QCOMPARE(sortedModel.rowCount(), model.rowCount());
        QCOMPARE(sortedModel.shareModel(), &model);
    }

    void testCorrectSort()
    {
        helper.resetTestData();
        addAllTestShares();
        QCOMPARE(helper.shareCount(), _expectedShareCount);

        ShareModel model;
        QSignalSpy sharesChanged(&model, &ShareModel::sharesChanged);
        model.setAccountState(helper.accountState.data());
        model.setLocalPath(helper.fakeFolder.localPath() + helper.testFileName);
        QVERIFY(sharesChanged.wait(5000));
        QCOMPARE(model.rowCount(), helper.shareCount());

        SortedShareModel sortedModel;
        QAbstractItemModelTester sortedModelTester(&sortedModel);
        QSignalSpy sortedModelReset(&sortedModel, &SortedShareModel::modelReset);

        sortedModel.setShareModel(&model);
        QCOMPARE(sortedModelReset.count(), 1);
        QCOMPARE(sortedModel.rowCount(), model.rowCount());

        for(auto i = 0; i < sortedModel.rowCount(); ++i) {
            const auto shareIndex = sortedModel.index(i, 0);
            const auto expectedShareDefinition = _expectedOrder.at(i);

            QCOMPARE(shareIndex.data(ShareModel::ShareTypeRole).toInt(), expectedShareDefinition.shareType);
            QCOMPARE(shareIndex.data(ShareModel::ShareIdRole).toString(), expectedShareDefinition.shareId);
        }
    }

};

QTEST_MAIN(TestSortedShareModel)
#include "testsortedsharemodel.moc"