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

123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
1
/*
2
 * Copyright (C) 2017 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
17
#include "mockbackend.h"
18
19
#include "backend/backend.h"
20
#include "models/jobmodel.h"
21
22
#include <QDebug>
23
#include <QObject>
24
#include <QSignalSpy>
25
#include <QTest>
26
27
class TestJobModel : public QObject
28
{
29
    Q_OBJECT
30
private Q_SLOTS:
31
    void init()
32
    {
33
        m_backend = new MockPrinterBackend;
34
        m_model = new JobModel(m_backend);
35
    }
36
    void cleanup()
37
    {
38
        QSignalSpy destroyedSpy(m_model, SIGNAL(destroyed(QObject*)));
39
        m_model->deleteLater();
40
        QTRY_COMPARE(destroyedSpy.count(), 1);
41
        delete m_backend;
42
    }
43
    void testInsert()
44
    {
45
        QSignalSpy countSpy(m_model, SIGNAL(countChanged()));
46
        QSignalSpy insertSpy(m_model, SIGNAL(rowsInserted(const QModelIndex&, int, int)));
47
48
        auto job = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend));
49
        m_backend->m_jobs << job;
50
51
        // Trigger update.
52
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
53
54
        QCOMPARE(m_model->count(), 1);
55
        QCOMPARE(countSpy.count(), 1);
56
        QCOMPARE(insertSpy.count(), 1);
57
    }
58
    void testRemove()
59
    {
60
        // Add one.
61
        auto job = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend));
62
        m_backend->m_jobs << job;
63
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
64
65
        QCOMPARE(m_model->count(), 1);
66
67
        /* Trigger another update, ignore the signal name here; there's a
68
        catchall handler in the model. */
69
        QSignalSpy removeSpy(m_model, SIGNAL(rowsRemoved(const QModelIndex&, int, int)));
70
        m_backend->m_jobs.clear();
71
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
72
        QCOMPARE(removeSpy.count(), 1);
73
74
        // Check item was removed
75
        QList<QVariant> args = removeSpy.at(0);
76
        QCOMPARE(args.at(1).toInt(), 0);
77
        QCOMPARE(args.at(2).toInt(), 0);
78
    }
79
    void testMove()
80
    {
81
        // Add two jobs.
82
        auto job1 = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend, 1));
83
        auto job2 = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend, 2));
84
        m_backend->m_jobs << job1 << job2;
85
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
86
87
        m_backend->m_jobs.move(0, 1);
88
        // Triggers a move.
89
        QSignalSpy moveSpy(m_model, SIGNAL(rowsMoved(const QModelIndex&, int, int, const QModelIndex&, int)));
90
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
91
        QCOMPARE(moveSpy.count(), 1);
92
        QList<QVariant> args = moveSpy.at(0);
93
        QCOMPARE(args.at(1).toInt(), 1);
94
        QCOMPARE(args.at(2).toInt(), 1);
95
        QCOMPARE(args.at(4).toInt(), 0);
96
    }
97
    void testModify()
98
    {
134.1.4 by Andrew Hayzen
* Add more tracing :-)
99
        qDebug() << Q_FUNC_INFO << "Create jobBefore";
100
123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
101
        auto jobBefore = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend, 1));
102
134.1.4 by Andrew Hayzen
* Add more tracing :-)
103
        qDebug() << Q_FUNC_INFO << "Add jobBefore to m_jobs";
104
123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
105
        m_backend->m_jobs << jobBefore;
134.1.4 by Andrew Hayzen
* Add more tracing :-)
106
107
        qDebug() << Q_FUNC_INFO << "Emit mockJobCreated";
108
123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
109
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
110
134.1.4 by Andrew Hayzen
* Add more tracing :-)
111
        qDebug() << Q_FUNC_INFO << "Model Count" << m_model->count();
112
113
        qDebug() << Q_FUNC_INFO << "Create jobAfter";
114
123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
115
        auto jobAfter = QSharedPointer<PrinterJob>(new PrinterJob("test-printer", m_backend, 1));
134.1.4 by Andrew Hayzen
* Add more tracing :-)
116
117
        qDebug() << Q_FUNC_INFO << "Set copies value";
118
123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
119
        jobAfter->setCopies(100);
134.1.4 by Andrew Hayzen
* Add more tracing :-)
120
121
        qDebug() << Q_FUNC_INFO << "Replace jobBefore in m_jobs with jobAfter";
122
123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
123
        m_backend->m_jobs.replace(0, jobAfter);
124
134.1.4 by Andrew Hayzen
* Add more tracing :-)
125
        qDebug() << Q_FUNC_INFO << "Setup spy";
126
123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
127
        // Triggers a change.
128
        QSignalSpy changedSpy(m_model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)));
134.1.4 by Andrew Hayzen
* Add more tracing :-)
129
130
        qDebug() << Q_FUNC_INFO << "Emit mockJobCreated";
131
123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
132
        m_backend->mockJobCreated("", "", "", 1, "", true, 100, 1, "", "", 1);
134.1.4 by Andrew Hayzen
* Add more tracing :-)
133
134
        qDebug() << Q_FUNC_INFO << "Compare spy count";
135
123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
136
        QCOMPARE(changedSpy.count(), 1);
134.1.4 by Andrew Hayzen
* Add more tracing :-)
137
138
        qDebug() << Q_FUNC_INFO << "End";
123 by Andrew Hayzen
* Add printer-components from ubuntu-settings-components to ubuntu-ui-extras (original branch https://code.launchpad.net/~phablet-team/ubuntu-settings-components/printer-components)
139
    }
140
141
private:
142
    MockPrinterBackend *m_backend;
143
    JobModel *m_model;
144
};
145
146
QTEST_GUILESS_MAIN(TestJobModel)
147
#include "tst_jobmodel.moc"