~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kplato/libs/models/tests/FlatProxyModelTester.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2010 Dag Andersen <danders@get2net.dk>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02110-1301, USA.
 
18
*/
 
19
#include "FlatProxyModelTester.h"
 
20
 
 
21
 
 
22
#include <QModelIndex>
 
23
#include <QStandardItemModel>
 
24
#include <QStandardItem>
 
25
 
 
26
#include <qtest_kde.h>
 
27
#include <kdebug.h>
 
28
 
 
29
namespace KPlato
 
30
{
 
31
 
 
32
void FlatProxyModelTester::emptyModel()
 
33
{
 
34
    QCOMPARE( m_flatmodel.columnCount(), 0 );
 
35
    QCOMPARE( m_flatmodel.rowCount(), 0 );
 
36
}
 
37
 
 
38
void FlatProxyModelTester::test()
 
39
{
 
40
    qDebug()<<"set 1 row, 1 column";
 
41
    m_standardmodel.setColumnCount( 1 );
 
42
    m_standardmodel.setRowCount( 1 );
 
43
 
 
44
    m_standardmodel.setHorizontalHeaderLabels( QStringList() << "Column 1" );
 
45
    m_flatmodel.setSourceModel( &m_standardmodel );
 
46
    
 
47
    QCOMPARE( m_flatmodel.columnCount(), 2 ); // it adds an extra column
 
48
    QCOMPARE( m_flatmodel.rowCount(), 1 );
 
49
    QCOMPARE( m_flatmodel.headerData( 0, Qt::Horizontal ), QVariant( "Column 1" ) );
 
50
    
 
51
    m_standardmodel.setData( m_standardmodel.index( 0, 0 ), "Index 0,0" );
 
52
 
 
53
    QModelIndex idx = m_flatmodel.index( 0, 0 );
 
54
    QVERIFY( idx.isValid() );
 
55
    qDebug()<<"Index 0,0:"<<idx.data();
 
56
 
 
57
    QCOMPARE( idx.data(), QVariant( "Index 0,0" ) );
 
58
    
 
59
    qDebug()<<"1 row, set 2 columns";
 
60
    m_standardmodel.setColumnCount( 2 );
 
61
    QCOMPARE( m_flatmodel.columnCount(), 3 ); // it adds an extra column
 
62
    m_flatmodel.setHeaderData( 1, Qt::Horizontal, "Column 2" );
 
63
    QCOMPARE( m_flatmodel.headerData( 1, Qt::Horizontal ), QVariant( "Column 2" ) );
 
64
    m_standardmodel.setData( m_standardmodel.index( 0, 1 ), "Index 0,1" );
 
65
    idx = m_flatmodel.index( 0, 1 );
 
66
    QVERIFY( idx.isValid() );
 
67
    qDebug()<<"Index 0,1:"<<idx.data();
 
68
    QCOMPARE( idx.data(), QVariant( "Index 0,1" ) );
 
69
 
 
70
    qDebug()<<"Set 2 rows, 2 columns";
 
71
    m_standardmodel.setRowCount( 2 );
 
72
    QCOMPARE( m_flatmodel.rowCount(), 2 );
 
73
    m_standardmodel.setData( m_standardmodel.index( 1, 0 ), "Index 1,0" );
 
74
    idx = m_flatmodel.index( 1, 0 );
 
75
    QVERIFY( idx.isValid() );
 
76
    qDebug()<<"Index 1,0:"<<idx.data();
 
77
    QCOMPARE( idx.data(), QVariant( "Index 1,0" ) );
 
78
 
 
79
    m_standardmodel.setData( m_standardmodel.index( 1, 1 ), "Index 1,1" );
 
80
    idx = m_flatmodel.index( 1, 1 );
 
81
    QVERIFY( idx.isValid() );
 
82
    qDebug()<<"Index 1,1:"<<idx.data();
 
83
    QCOMPARE( idx.data(), QVariant( "Index 1,1" ) );
 
84
    
 
85
    qDebug()<<"Add child on last index, adds a new row 1 in the flat model";
 
86
 
 
87
    // NOTE: m_standardmodel.insertRow( 0, m_standardmodel.index( 1, 0 ) );
 
88
    // does not work as there will be no columns and thus no valid indexes for this row
 
89
    QStandardItem *item = m_standardmodel.itemFromIndex( m_standardmodel.index( 1, 0 ) );
 
90
    QList<QStandardItem*> items;
 
91
    items << new QStandardItem( "Child last column 1" ) << new QStandardItem( "Child last column 2" );
 
92
    item->appendRow( items );
 
93
    QCOMPARE( m_flatmodel.rowCount(), 3 );
 
94
    idx = m_flatmodel.index( 2, 0 );
 
95
    QCOMPARE( idx.data(), QVariant( "Child last column 1" ) );
 
96
    idx = m_flatmodel.index( 2, 1 );
 
97
    QCOMPARE( idx.data(), QVariant( "Child last column 2" ) );
 
98
 
 
99
    qDebug()<<"add child on first index";
 
100
    
 
101
    item = m_standardmodel.itemFromIndex( m_standardmodel.index( 0, 0 ) );
 
102
    items.clear();
 
103
    items << new QStandardItem( "Child first column 1" ) << new QStandardItem( "Child first column 2" );
 
104
    item->appendRow( items );
 
105
    QCOMPARE( m_flatmodel.rowCount(), 4 );
 
106
    idx = m_flatmodel.index( 1, 0 );
 
107
    QCOMPARE( idx.data(), QVariant( "Child first column 1" ) );
 
108
    idx = m_flatmodel.index( 1, 1 );
 
109
    QCOMPARE( idx.data(), QVariant( "Child first column 2" ) );
 
110
 
 
111
    qDebug()<<"add row (2) on top level between first and last";
 
112
    
 
113
    item = m_standardmodel.invisibleRootItem();
 
114
    items.clear();
 
115
    items << new QStandardItem( "New index 1,0" ) << new QStandardItem( "New index 1,1" );
 
116
    item->insertRow( 1, items );
 
117
    QCOMPARE( m_flatmodel.rowCount(), 5 );
 
118
    idx = m_flatmodel.index( 2, 0 );
 
119
    QCOMPARE( idx.data(), QVariant( "New index 1,0" ) );
 
120
    idx = m_flatmodel.index( 2, 1 );
 
121
    QCOMPARE( idx.data(), QVariant( "New index 1,1" ) );
 
122
 
 
123
    qDebug()<<"Add child on middle index, adds row 3 to flat model";
 
124
    
 
125
    item = m_standardmodel.itemFromIndex( m_standardmodel.index( 1, 0 ) );
 
126
    items.clear();
 
127
    items << new QStandardItem( "Child middle column 1" ) << new QStandardItem( "Child middle column 2" );
 
128
    item->appendRow( items );
 
129
    QCOMPARE( m_flatmodel.rowCount(), 6 );
 
130
    idx = m_flatmodel.index( 3, 0 );
 
131
    QCOMPARE( idx.data().toString(), QVariant( "Child middle column 1" ).toString() );
 
132
    idx = m_flatmodel.index( 3, 1 );
 
133
    QCOMPARE( idx.data(), QVariant( "Child middle column 2" ) );
 
134
 
 
135
    qDebug()<<"Add child on middle index's child, adds row 4 to flat model";
 
136
    
 
137
    QModelIndex parent = m_standardmodel.index( 1, 0 );
 
138
    QCOMPARE( parent.data().toString(), QString( "New index 1,0" ) );
 
139
    idx = m_standardmodel.index( 0, 0, parent );
 
140
    QCOMPARE( idx.data().toString(), QString( "Child middle column 1" ) );
 
141
    item = m_standardmodel.itemFromIndex( idx );
 
142
    items.clear();
 
143
    items << new QStandardItem( "Child of middle child column 1" ) << new QStandardItem( "Child of middle child column 2" );
 
144
    item->appendRow( items );
 
145
    QCOMPARE( m_flatmodel.rowCount(), 7 );
 
146
    idx = m_flatmodel.index( 4, 0 );
 
147
    QCOMPARE( idx.data().toString(), QVariant( "Child of middle child column 1" ).toString() );
 
148
    idx = m_flatmodel.index( 4, 1 );
 
149
    QCOMPARE( idx.data(), QVariant( "Child of middle child column 2" ) );
 
150
 
 
151
    qDebug()<<"Add child on middle index at row 0, adds row 3 to flat model";
 
152
    
 
153
    idx = m_standardmodel.index( 1, 0 );
 
154
    QCOMPARE( idx.data().toString(), QString( "New index 1,0" ) );
 
155
    item = m_standardmodel.itemFromIndex( idx );
 
156
    items.clear();
 
157
    items << new QStandardItem( "Index 1,0 -> Index 0,0" ) << new QStandardItem( "Index 1,0 -> Index 0,1" );
 
158
    item->insertRow( 0, items );
 
159
    QCOMPARE( m_flatmodel.rowCount(), 8 );
 
160
    idx = m_flatmodel.index( 3, 0 );
 
161
    QCOMPARE( idx.data().toString(), QVariant( "Index 1,0 -> Index 0,0" ).toString() );
 
162
    idx = m_flatmodel.index( 3, 1 );
 
163
    QCOMPARE( idx.data(), QVariant( "Index 1,0 -> Index 0,1" ) );
 
164
 
 
165
    qDebug()<<"Add child on middle index at row 1, adds row 4 to flat model";
 
166
    
 
167
    idx = m_standardmodel.index( 1, 0 );
 
168
    QCOMPARE( idx.data().toString(), QString( "New index 1,0" ) );
 
169
    item = m_standardmodel.itemFromIndex( idx );
 
170
    items.clear();
 
171
    items << new QStandardItem( "Index 1,0 -> Index 1,0" ) << new QStandardItem( "Index 1,0 -> Index 1,1" );
 
172
    item->insertRow( 1, items );
 
173
    QCOMPARE( m_flatmodel.rowCount(), 9 );
 
174
    idx = m_flatmodel.index( 4, 0 );
 
175
    QCOMPARE( idx.data().toString(), QVariant( "Index 1,0 -> Index 1,0" ).toString() );
 
176
    idx = m_flatmodel.index( 4, 1 );
 
177
    QCOMPARE( idx.data(), QVariant( "Index 1,0 -> Index 1,1" ) );
 
178
 
 
179
    qDebug()<<"Add child on middle index at last row (4), adds row 8 to flat model";
 
180
    
 
181
    idx = m_standardmodel.index( 1, 0 );
 
182
    QCOMPARE( idx.data().toString(), QString( "New index 1,0" ) );
 
183
    item = m_standardmodel.itemFromIndex( idx );
 
184
    items.clear();
 
185
    items << new QStandardItem( "Index 1,0 -> Index 4,0" ) << new QStandardItem( "Index 1,0 -> Index 4,1" );
 
186
    item->insertRow( 3, items );
 
187
    QCOMPARE( m_standardmodel.rowCount( m_standardmodel.index( 1, 0 ) ), 4 );
 
188
    QCOMPARE( m_flatmodel.rowCount(), 10 );
 
189
    idx = m_flatmodel.index( 7, 0 );
 
190
    QCOMPARE( idx.data().toString(), QVariant( "Index 1,0 -> Index 4,0" ).toString() );
 
191
    idx = m_flatmodel.index( 7, 1 );
 
192
    QCOMPARE( idx.data(), QVariant( "Index 1,0 -> Index 4,1" ) );
 
193
}
 
194
 
 
195
} //namespace KPlato
 
196
 
 
197
QTEST_KDEMAIN_CORE( KPlato::FlatProxyModelTester )
 
198
 
 
199
#include "FlatProxyModelTester.moc"