~ci-train-bot/ubuntu-ui-extras/ubuntu-ui-extras-ubuntu-zesty-2515

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
/*
 * Copyright (C) 2017 Canonical, Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mockbackend.h"

#include "backend/backend.h"
#include "models/jobmodel.h"

#include <QDebug>
#include <QObject>
#include <QSignalSpy>
#include <QTest>

class TestJobModel : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void init()
    {
        m_backend = new MockPrinterBackend;
        m_model = new JobModel(m_backend);
    }
    void cleanup()
    {
        QSignalSpy destroyedSpy(m_model, SIGNAL(destroyed(QObject*)));
        m_model->deleteLater();
        QTRY_COMPARE(destroyedSpy.count(), 1);
        delete m_backend;
    }
    void testInsert()
    {
        QSignalSpy countSpy(m_model, SIGNAL(countChanged()));
        QSignalSpy insertSpy(m_model, SIGNAL(rowsInserted(const QModelIndex&, int, int)));

        auto job = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend));
        m_backend->m_jobs << job;

        // Trigger update.
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);

        QCOMPARE(m_model->count(), 1);
        QCOMPARE(countSpy.count(), 1);
        QCOMPARE(insertSpy.count(), 1);
    }
    void testRemove()
    {
        // Add one.
        auto job = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend));
        m_backend->m_jobs << job;
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);

        QCOMPARE(m_model->count(), 1);

        /* Trigger another update, ignore the signal name here; there's a
        catchall handler in the model. */
        QSignalSpy removeSpy(m_model, SIGNAL(rowsRemoved(const QModelIndex&, int, int)));
        m_backend->m_jobs.clear();
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
        QCOMPARE(removeSpy.count(), 1);

        // Check item was removed
        QList<QVariant> args = removeSpy.at(0);
        QCOMPARE(args.at(1).toInt(), 0);
        QCOMPARE(args.at(2).toInt(), 0);
    }
    void testMove()
    {
        // Add two jobs.
        auto job1 = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend, 1));
        auto job2 = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend, 2));
        m_backend->m_jobs << job1 << job2;
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);

        m_backend->m_jobs.move(0, 1);
        // Triggers a move.
        QSignalSpy moveSpy(m_model, SIGNAL(rowsMoved(const QModelIndex&, int, int, const QModelIndex&, int)));
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
        QCOMPARE(moveSpy.count(), 1);
        QList<QVariant> args = moveSpy.at(0);
        QCOMPARE(args.at(1).toInt(), 1);
        QCOMPARE(args.at(2).toInt(), 1);
        QCOMPARE(args.at(4).toInt(), 0);
    }
    void testModify()
    {
        qDebug() << Q_FUNC_INFO << "Create jobBefore";

        auto jobBefore = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend, 1));

        qDebug() << Q_FUNC_INFO << "Add jobBefore to m_jobs";

        m_backend->m_jobs << jobBefore;

        qDebug() << Q_FUNC_INFO << "Emit mockJobCreated";

        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);

        qDebug() << Q_FUNC_INFO << "Model Count" << m_model->count();

        qDebug() << Q_FUNC_INFO << "Create jobAfter";

        auto jobAfter = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend, 1));

        qDebug() << Q_FUNC_INFO << "Set copies value";

        jobAfter->setCopies(100);

        qDebug() << Q_FUNC_INFO << "Replace jobBefore in m_jobs with jobAfter";

        m_backend->m_jobs.replace(0, jobAfter);

        qDebug() << Q_FUNC_INFO << "Setup spy";

        // Triggers a change.
        QSignalSpy changedSpy(m_model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)));

        qDebug() << Q_FUNC_INFO << "Emit mockJobCreated";

        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);

        qDebug() << Q_FUNC_INFO << "Compare spy count";

        QCOMPARE(changedSpy.count(), 1);

        qDebug() << Q_FUNC_INFO << "End";
    }

private:
    MockPrinterBackend *m_backend;
    JobModel *m_model;
};

QTEST_GUILESS_MAIN(TestJobModel)
#include "tst_jobmodel.moc"