Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/project/tests/test_projectmodel.cpp
Show All 40 Lines | |||||
41 | { | 41 | { | ||
42 | Q_ASSERT(m); | 42 | Q_ASSERT(m); | ||
43 | qDebug() << QByteArray(depth*2, '-') << m->data(parent).toString(); | 43 | qDebug() << QByteArray(depth*2, '-') << m->data(parent).toString(); | ||
44 | for(int i=0; i<m->rowCount(parent); i++) { | 44 | for(int i=0; i<m->rowCount(parent); i++) { | ||
45 | debugItemModel(m, m->index(i, 0, parent), depth+1); | 45 | debugItemModel(m, m->index(i, 0, parent), depth+1); | ||
46 | } | 46 | } | ||
47 | } | 47 | } | ||
48 | 48 | | |||
49 | // Needed because util/Path.cpp asserts e.g. c: on Windows | ||||
50 | QString getTestRoot(QString const& currentPath) | ||||
51 | { | ||||
52 | #ifdef Q_OS_WIN | ||||
53 | QString testRoot = QStringLiteral("Z:/"); | ||||
54 | #else | ||||
55 | QString testRoot = QStringLiteral("/"); | ||||
56 | #endif | ||||
57 | return testRoot + currentPath; | ||||
58 | } | ||||
59 | | ||||
49 | void TestProjectModel::initTestCase() | 60 | void TestProjectModel::initTestCase() | ||
50 | { | 61 | { | ||
51 | AutoTestShell::init(); | 62 | AutoTestShell::init(); | ||
52 | TestCore::initialize(Core::NoUi); | 63 | TestCore::initialize(Core::NoUi); | ||
53 | 64 | | |||
54 | qRegisterMetaType<QModelIndex>("QModelIndex"); | 65 | qRegisterMetaType<QModelIndex>("QModelIndex"); | ||
55 | model = ICore::self()->projectController()->projectModel(); | 66 | model = ICore::self()->projectController()->projectModel(); | ||
56 | new ModelTest( model, this ); | 67 | new ModelTest( model, this ); | ||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 84 | { | |||
109 | QCOMPARE( model->data( idx ).toString(), expectedItemText ); | 120 | QCOMPARE( model->data( idx ).toString(), expectedItemText ); | ||
110 | QCOMPARE( model->indexFromItem( newitem ), idx ); | 121 | QCOMPARE( model->indexFromItem( newitem ), idx ); | ||
111 | QCOMPARE( model->pathFromIndex( idx ), expectedRelativeItemPath ); | 122 | QCOMPARE( model->pathFromIndex( idx ), expectedRelativeItemPath ); | ||
112 | QCOMPARE( model->pathToIndex( expectedRelativeItemPath ), idx ); | 123 | QCOMPARE( model->pathToIndex( expectedRelativeItemPath ), idx ); | ||
113 | } | 124 | } | ||
114 | 125 | | |||
115 | void TestProjectModel::testCreateFileSystemItems_data() | 126 | void TestProjectModel::testCreateFileSystemItems_data() | ||
116 | { | 127 | { | ||
128 | QString testRootDir = getTestRoot("rootdir"); | ||||
129 | QString testRootFile = getTestRoot("rootfile"); | ||||
117 | QTest::addColumn<int>( "itemType" ); | 130 | QTest::addColumn<int>( "itemType" ); | ||
118 | QTest::addColumn<Path>( "itemPath" ); | 131 | QTest::addColumn<Path>( "itemPath" ); | ||
119 | QTest::addColumn<Path>( "expectedItemPath" ); | 132 | QTest::addColumn<Path>( "expectedItemPath" ); | ||
120 | QTest::addColumn<QString>( "expectedItemText" ); | 133 | QTest::addColumn<QString>( "expectedItemText" ); | ||
121 | QTest::addColumn<QStringList>( "expectedRelativeItemPath" ); | 134 | QTest::addColumn<QStringList>( "expectedRelativeItemPath" ); | ||
122 | QTest::addColumn<int>( "expectedItemRow" ); | 135 | QTest::addColumn<int>( "expectedItemRow" ); | ||
123 | 136 | | |||
124 | QTest::newRow("RootFolder") | 137 | QTest::newRow("RootFolder") | ||
125 | << (int)ProjectBaseItem::Folder | 138 | << (int)ProjectBaseItem::Folder | ||
126 | << Path(QUrl::fromLocalFile(QStringLiteral("/rootdir"))) | 139 | << Path(QUrl::fromLocalFile(testRootDir)) | ||
127 | << Path(QUrl::fromLocalFile(QStringLiteral("/rootdir/"))) | 140 | << Path(QUrl::fromLocalFile(testRootDir)) | ||
128 | << QStringLiteral("rootdir") | 141 | << QStringLiteral("rootdir") | ||
129 | << ( QStringList() << QStringLiteral("rootdir") ) | 142 | << ( QStringList() << QStringLiteral("rootdir") ) | ||
130 | << 0; | 143 | << 0; | ||
131 | 144 | | |||
132 | QTest::newRow("RootBuildFolder") | 145 | QTest::newRow("RootBuildFolder") | ||
133 | << (int)ProjectBaseItem::BuildFolder | 146 | << (int)ProjectBaseItem::BuildFolder | ||
134 | << Path(QUrl::fromLocalFile(QStringLiteral("/rootdir"))) | 147 | << Path(QUrl::fromLocalFile(testRootDir)) | ||
135 | << Path(QUrl::fromLocalFile(QStringLiteral("/rootdir/"))) | 148 | << Path(QUrl::fromLocalFile(testRootDir)) | ||
136 | << QStringLiteral("rootdir") | 149 | << QStringLiteral("rootdir") | ||
137 | << ( QStringList() << QStringLiteral("rootdir") ) | 150 | << ( QStringList() << QStringLiteral("rootdir") ) | ||
138 | << 0; | 151 | << 0; | ||
139 | 152 | | |||
140 | QTest::newRow("RootFile") | 153 | QTest::newRow("RootFile") | ||
141 | << (int)ProjectBaseItem::File | 154 | << (int)ProjectBaseItem::File | ||
142 | << Path(QUrl::fromLocalFile(QStringLiteral("/rootfile"))) | 155 | << Path(QUrl::fromLocalFile(testRootFile)) | ||
143 | << Path(QUrl::fromLocalFile(QStringLiteral("/rootfile"))) | 156 | << Path(QUrl::fromLocalFile(testRootFile)) | ||
144 | << QStringLiteral("rootfile") | 157 | << QStringLiteral("rootfile") | ||
145 | << ( QStringList() << QStringLiteral("rootfile") ) | 158 | << ( QStringList() << QStringLiteral("rootfile") ) | ||
146 | << 0; | 159 | << 0; | ||
147 | 160 | | |||
148 | } | 161 | } | ||
149 | 162 | | |||
150 | void TestProjectModel::testCreateTargetItems() | 163 | void TestProjectModel::testCreateTargetItems() | ||
151 | { | 164 | { | ||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Line(s) | 210 | QTest::newRow("RootLibraryTarget") | |||
199 | << "libtarget" | 212 | << "libtarget" | ||
200 | << QStringLiteral("libtarget") | 213 | << QStringLiteral("libtarget") | ||
201 | << ( QStringList() << QStringLiteral("libtarget") ) | 214 | << ( QStringList() << QStringLiteral("libtarget") ) | ||
202 | << 0; | 215 | << 0; | ||
203 | } | 216 | } | ||
204 | 217 | | |||
205 | void TestProjectModel::testChangeWithProxyModel() | 218 | void TestProjectModel::testChangeWithProxyModel() | ||
206 | { | 219 | { | ||
220 | QString projectFolderPath = getTestRoot("folder1"); | ||||
221 | QString projectFilePath = getTestRoot("folder1/file1"); | ||||
207 | auto* proxy = new QSortFilterProxyModel( this ); | 222 | auto* proxy = new QSortFilterProxyModel( this ); | ||
208 | proxy->setSourceModel( model ); | 223 | proxy->setSourceModel( model ); | ||
209 | ProjectFolderItem* root = new ProjectFolderItem( nullptr, Path(QUrl::fromLocalFile(QStringLiteral("/folder1"))) ); | 224 | ProjectFolderItem* root = new ProjectFolderItem( nullptr, Path(QUrl::fromLocalFile(projectFolderPath)) ); | ||
210 | root->appendRow( new ProjectFileItem( nullptr, Path(QUrl::fromLocalFile(QStringLiteral("/folder1/file1"))) ) ); | 225 | root->appendRow( new ProjectFileItem( nullptr, Path(QUrl::fromLocalFile(projectFilePath)) ) ); | ||
211 | model->appendRow( root ); | 226 | model->appendRow( root ); | ||
212 | 227 | | |||
213 | QCOMPARE( model->rowCount(), 1 ); | 228 | QCOMPARE( model->rowCount(), 1 ); | ||
214 | QCOMPARE( proxy->rowCount(), 1 ); | 229 | QCOMPARE( proxy->rowCount(), 1 ); | ||
215 | 230 | | |||
216 | model->removeRow( 0 ); | 231 | model->removeRow( 0 ); | ||
217 | 232 | | |||
218 | QCOMPARE( model->rowCount(), 0 ); | 233 | QCOMPARE( model->rowCount(), 0 ); | ||
219 | QCOMPARE( proxy->rowCount(), 0 ); | 234 | QCOMPARE( proxy->rowCount(), 0 ); | ||
220 | } | 235 | } | ||
221 | 236 | | |||
222 | void TestProjectModel::testCreateSimpleHierarchy() | 237 | void TestProjectModel::testCreateSimpleHierarchy() | ||
223 | { | 238 | { | ||
224 | QString folderName = QStringLiteral("rootfolder"); | 239 | QString folderName = QStringLiteral("rootfolder"); | ||
225 | QString fileName = QStringLiteral("file"); | 240 | QString fileName = QStringLiteral("file"); | ||
226 | QString targetName = QStringLiteral("testtarged"); | 241 | QString targetName = QStringLiteral("testtarged"); | ||
227 | QString cppFileName = QStringLiteral("file.cpp"); | 242 | QString cppFileName = QStringLiteral("file.cpp"); | ||
228 | ProjectFolderItem* rootFolder = new ProjectFolderItem( nullptr, Path(QUrl::fromLocalFile("/"+folderName)) ); | 243 | ProjectFolderItem* rootFolder = new ProjectFolderItem( nullptr, Path(QUrl::fromLocalFile( getTestRoot(folderName) )) ); | ||
229 | QCOMPARE(rootFolder->baseName(), folderName); | 244 | QCOMPARE(rootFolder->baseName(), folderName); | ||
230 | auto* file = new ProjectFileItem( fileName, rootFolder ); | 245 | auto* file = new ProjectFileItem( fileName, rootFolder ); | ||
231 | QCOMPARE(file->baseName(), fileName); | 246 | QCOMPARE(file->baseName(), fileName); | ||
232 | auto* target = new ProjectTargetItem( nullptr, targetName ); | 247 | auto* target = new ProjectTargetItem( nullptr, targetName ); | ||
233 | rootFolder->appendRow( target ); | 248 | rootFolder->appendRow( target ); | ||
234 | ProjectFileItem* targetfile = new ProjectFileItem( nullptr, Path(rootFolder->path(), cppFileName), target ); | 249 | ProjectFileItem* targetfile = new ProjectFileItem( nullptr, Path(rootFolder->path(), cppFileName), target ); | ||
235 | 250 | | |||
236 | model->appendRow( rootFolder ); | 251 | model->appendRow( rootFolder ); | ||
Show All 30 Lines | |||||
267 | QVERIFY( rootFolder->hasFileOrFolder( fileName ) ); | 282 | QVERIFY( rootFolder->hasFileOrFolder( fileName ) ); | ||
268 | 283 | | |||
269 | delete rootFolder; | 284 | delete rootFolder; | ||
270 | QCOMPARE( model->rowCount(), 0 ); | 285 | QCOMPARE( model->rowCount(), 0 ); | ||
271 | } | 286 | } | ||
272 | 287 | | |||
273 | void TestProjectModel::testItemSanity() | 288 | void TestProjectModel::testItemSanity() | ||
274 | { | 289 | { | ||
290 | #ifdef Q_OS_WIN | ||||
291 | QString child3Path = QStringLiteral("file:///c:/bcd"); | ||||
292 | QString child4Path = QStringLiteral("file:///c:/abcd"); | ||||
293 | #else | ||||
294 | QString child3Path = QStringLiteral("file:///bcd"); | ||||
295 | QString child4Path = QStringLiteral("file:///abcd"); | ||||
296 | #endif | ||||
297 | QString newtestPath = getTestRoot("newtest"); | ||||
275 | ProjectBaseItem* parent = new ProjectBaseItem( nullptr, QStringLiteral("test") ); | 298 | ProjectBaseItem* parent = new ProjectBaseItem( nullptr, QStringLiteral("test") ); | ||
276 | ProjectBaseItem* child = new ProjectBaseItem( nullptr, QStringLiteral("test"), parent ); | 299 | ProjectBaseItem* child = new ProjectBaseItem( nullptr, QStringLiteral("test"), parent ); | ||
277 | ProjectBaseItem* child2 = new ProjectBaseItem( nullptr, QStringLiteral("ztest"), parent ); | 300 | ProjectBaseItem* child2 = new ProjectBaseItem( nullptr, QStringLiteral("ztest"), parent ); | ||
278 | ProjectFileItem* child3 = new ProjectFileItem( nullptr, Path(QUrl(QStringLiteral("file:///bcd"))), parent ); | 301 | ProjectFileItem* child3 = new ProjectFileItem( nullptr, Path(QUrl(child3Path)), parent ); | ||
279 | ProjectFileItem* child4 = new ProjectFileItem( nullptr, Path(QUrl(QStringLiteral("file:///abcd"))), parent ); | 302 | ProjectFileItem* child4 = new ProjectFileItem( nullptr, Path(QUrl(child4Path)), parent ); | ||
280 | 303 | | |||
281 | // Just some basic santiy checks on the API | 304 | // Just some basic santiy checks on the API | ||
282 | QCOMPARE( parent->child( 0 ), child ); | 305 | QCOMPARE( parent->child( 0 ), child ); | ||
283 | QCOMPARE( parent->row(), -1 ); | 306 | QCOMPARE( parent->row(), -1 ); | ||
284 | QVERIFY( !parent->child( -1 ) ); | 307 | QVERIFY( !parent->child( -1 ) ); | ||
285 | QVERIFY( !parent->file() ); | 308 | QVERIFY( !parent->file() ); | ||
286 | QVERIFY( !parent->folder() ); | 309 | QVERIFY( !parent->folder() ); | ||
287 | QVERIFY( !parent->project() ); | 310 | QVERIFY( !parent->project() ); | ||
288 | QVERIFY( !parent->child( parent->rowCount() ) ); | 311 | QVERIFY( !parent->child( parent->rowCount() ) ); | ||
289 | QCOMPARE( parent->iconName(), QString() ); | 312 | QCOMPARE( parent->iconName(), QString() ); | ||
290 | QCOMPARE( parent->index(), QModelIndex() ); | 313 | QCOMPARE( parent->index(), QModelIndex() ); | ||
291 | 314 | | |||
292 | QCOMPARE( child->type(), (int)ProjectBaseItem::BaseItem ); | 315 | QCOMPARE( child->type(), (int)ProjectBaseItem::BaseItem ); | ||
293 | 316 | | |||
294 | QCOMPARE( child->lessThan( child2 ), true ); | 317 | QCOMPARE( child->lessThan( child2 ), true ); | ||
295 | QCOMPARE( child3->lessThan( child4 ), false ); | 318 | QCOMPARE( child3->lessThan( child4 ), false ); | ||
296 | 319 | | |||
297 | // Check that model is properly emitting data-changes | 320 | // Check that model is properly emitting data-changes | ||
298 | model->appendRow( parent ); | 321 | model->appendRow( parent ); | ||
299 | QCOMPARE( parent->index(), model->index(0, 0, QModelIndex()) ); | 322 | QCOMPARE( parent->index(), model->index(0, 0, QModelIndex()) ); | ||
300 | QSignalSpy s( model, SIGNAL(dataChanged(QModelIndex,QModelIndex)) ); | 323 | QSignalSpy s( model, SIGNAL(dataChanged(QModelIndex,QModelIndex)) ); | ||
301 | parent->setPath( Path(QStringLiteral("/newtest")) ); | 324 | parent->setPath( Path(newtestPath) ); | ||
302 | QCOMPARE( s.count(), 1 ); | 325 | QCOMPARE( s.count(), 1 ); | ||
303 | QCOMPARE( model->data( parent->index() ).toString(), QStringLiteral("newtest") ); | 326 | QCOMPARE( model->data( parent->index() ).toString(), QStringLiteral("newtest") ); | ||
304 | 327 | | |||
305 | parent->removeRow( child->row() ); | 328 | parent->removeRow( child->row() ); | ||
306 | } | 329 | } | ||
307 | 330 | | |||
308 | void TestProjectModel::testTakeRow() | 331 | void TestProjectModel::testTakeRow() | ||
309 | { | 332 | { | ||
Show All 10 Lines | |||||
320 | parent->takeRow( child->row() ); | 343 | parent->takeRow( child->row() ); | ||
321 | 344 | | |||
322 | QCOMPARE( child->model(), static_cast<ProjectModel*>(nullptr) ); | 345 | QCOMPARE( child->model(), static_cast<ProjectModel*>(nullptr) ); | ||
323 | QCOMPARE( subchild->model(), static_cast<ProjectModel*>(nullptr) ); | 346 | QCOMPARE( subchild->model(), static_cast<ProjectModel*>(nullptr) ); | ||
324 | } | 347 | } | ||
325 | 348 | | |||
326 | void TestProjectModel::testRename() | 349 | void TestProjectModel::testRename() | ||
327 | { | 350 | { | ||
351 | QString projectFolderPath = getTestRoot("dummyprojectfolder"); | ||||
328 | QFETCH( int, itemType ); | 352 | QFETCH( int, itemType ); | ||
329 | QFETCH( QString, itemText ); | 353 | QFETCH( QString, itemText ); | ||
330 | QFETCH( QString, newName ); | 354 | QFETCH( QString, newName ); | ||
331 | QFETCH( bool, datachangesignal ); | 355 | QFETCH( bool, datachangesignal ); | ||
332 | QFETCH( QString, expectedItemText ); | 356 | QFETCH( QString, expectedItemText ); | ||
333 | QFETCH( int, expectedRenameCode ); | 357 | QFETCH( int, expectedRenameCode ); | ||
334 | 358 | | |||
335 | const Path projectFolder = Path(QUrl::fromLocalFile(QStringLiteral("/dummyprojectfolder"))); | 359 | const Path projectFolder = Path(QUrl::fromLocalFile(projectFolderPath)); | ||
336 | QScopedPointer<TestProject> proj(new TestProject()); | 360 | QScopedPointer<TestProject> proj(new TestProject()); | ||
337 | auto* rootItem = new ProjectFolderItem( proj.data(), projectFolder, nullptr); | 361 | auto* rootItem = new ProjectFolderItem( proj.data(), projectFolder, nullptr); | ||
338 | proj->setProjectItem( rootItem ); | 362 | proj->setProjectItem( rootItem ); | ||
339 | 363 | | |||
340 | new ProjectFileItem(QStringLiteral("existing"), rootItem); | 364 | new ProjectFileItem(QStringLiteral("existing"), rootItem); | ||
341 | 365 | | |||
342 | ProjectBaseItem* item = nullptr; | 366 | ProjectBaseItem* item = nullptr; | ||
343 | if( itemType == ProjectBaseItem::Target ) { | 367 | if( itemType == ProjectBaseItem::Target ) { | ||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Line(s) | 392 | { | |||
443 | << QStringLiteral("other/bash") | 467 | << QStringLiteral("other/bash") | ||
444 | << false | 468 | << false | ||
445 | << QStringLiteral("mynew") | 469 | << QStringLiteral("mynew") | ||
446 | << (int)ProjectBaseItem::InvalidNewName; | 470 | << (int)ProjectBaseItem::InvalidNewName; | ||
447 | } | 471 | } | ||
448 | 472 | | |||
449 | void TestProjectModel::testWithProject() | 473 | void TestProjectModel::testWithProject() | ||
450 | { | 474 | { | ||
475 | QString projectFolderPath = getTestRoot("dummyprojectfolder"); | ||||
451 | QScopedPointer<TestProject> proj(new TestProject()); | 476 | QScopedPointer<TestProject> proj(new TestProject()); | ||
452 | ProjectFolderItem* rootItem = new ProjectFolderItem( proj.data(), Path(QUrl::fromLocalFile(QStringLiteral("/dummyprojectfolder"))), nullptr); | 477 | ProjectFolderItem* rootItem = new ProjectFolderItem( proj.data(), Path(QUrl::fromLocalFile(projectFolderPath)), nullptr); | ||
453 | proj->setProjectItem( rootItem ); | 478 | proj->setProjectItem( rootItem ); | ||
454 | ProjectBaseItem* item = model->itemFromIndex( model->index( 0, 0 ) ); | 479 | ProjectBaseItem* item = model->itemFromIndex( model->index( 0, 0 ) ); | ||
455 | QCOMPARE( item, rootItem ); | 480 | QCOMPARE( item, rootItem ); | ||
456 | QCOMPARE( item->text(), proj->name() ); | 481 | QCOMPARE( item->text(), proj->name() ); | ||
457 | QCOMPARE( item->path(), proj->path() ); | 482 | QCOMPARE( item->path(), proj->path() ); | ||
458 | } | 483 | } | ||
459 | 484 | | |||
460 | void TestProjectModel::testItemsForPath() | 485 | void TestProjectModel::testItemsForPath() | ||
▲ Show 20 Lines • Show All 86 Lines • Show Last 20 Lines |