tests/auto/qabstractitemmodel/tst_qabstractitemmodel.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   110     void testMoveToDescendants();
   110     void testMoveToDescendants();
   111 
   111 
   112     void testMoveWithinOwnRange_data();
   112     void testMoveWithinOwnRange_data();
   113     void testMoveWithinOwnRange();
   113     void testMoveWithinOwnRange();
   114 
   114 
       
   115     void testMoveThroughProxy();
       
   116 
   115     void testReset();
   117     void testReset();
   116 
   118 
   117 
   119 
   118 private:
   120 private:
   119     DynamicTreeModel *m_model;
   121     DynamicTreeModel *m_model;
   863 void tst_QAbstractItemModel::testMoveSameParentDown_data()
   865 void tst_QAbstractItemModel::testMoveSameParentDown_data()
   864 {
   866 {
   865     QTest::addColumn<int>("startRow");
   867     QTest::addColumn<int>("startRow");
   866     QTest::addColumn<int>("endRow");
   868     QTest::addColumn<int>("endRow");
   867     QTest::addColumn<int>("destRow");
   869     QTest::addColumn<int>("destRow");
       
   870     // We can't put the actual parent index for the move in here because m_model is not defined until init() is run.
       
   871     QTest::addColumn<bool>("topLevel");
   868 
   872 
   869     // Move from the start to the middle
   873     // Move from the start to the middle
   870     QTest::newRow("move01") << 0 << 2 << 8;
   874     QTest::newRow("move01") << 0 << 2 << 8 << true;
   871     // Move from the start to the end
   875     // Move from the start to the end
   872     QTest::newRow("move02") << 0 << 2 << 10;
   876     QTest::newRow("move02") << 0 << 2 << 10 << true;
   873     // Move from the middle to the middle
   877     // Move from the middle to the middle
   874     QTest::newRow("move03") << 3 << 5 << 8;
   878     QTest::newRow("move03") << 3 << 5 << 8 << true;
   875     // Move from the middle to the end
   879     // Move from the middle to the end
   876     QTest::newRow("move04") << 3 << 5 << 10;
   880     QTest::newRow("move04") << 3 << 5 << 10 << true;
       
   881 
       
   882     QTest::newRow("move05") << 0 << 2 << 8 << false;
       
   883     QTest::newRow("move06") << 0 << 2 << 10 << false;
       
   884     QTest::newRow("move07") << 3 << 5 << 8 << false;
       
   885     QTest::newRow("move08") << 3 << 5 << 10 << false;
   877 }
   886 }
   878 
   887 
   879 void tst_QAbstractItemModel::testMoveSameParentDown()
   888 void tst_QAbstractItemModel::testMoveSameParentDown()
   880 {
   889 {
   881     QFETCH( int, startRow);
   890     QFETCH( int, startRow);
   882     QFETCH( int, endRow);
   891     QFETCH( int, endRow);
   883     QFETCH( int, destRow);
   892     QFETCH( int, destRow);
       
   893     QFETCH( bool, topLevel);
       
   894 
       
   895     QModelIndex moveParent = topLevel ? QModelIndex() : m_model->index(5, 0);
   884 
   896 
   885     QList<QPersistentModelIndex> persistentList;
   897     QList<QPersistentModelIndex> persistentList;
   886     QModelIndexList indexList;
   898     QModelIndexList indexList;
   887 
   899 
   888     for (int column = 0; column < m_model->columnCount(); ++column)
   900     for (int column = 0; column < m_model->columnCount(); ++column)
   911     QSignalSpy beforeSpy(m_model, SIGNAL(rowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int)));
   923     QSignalSpy beforeSpy(m_model, SIGNAL(rowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int)));
   912     QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)));
   924     QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)));
   913 
   925 
   914     ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this);
   926     ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this);
   915     moveCommand->setNumCols(4);
   927     moveCommand->setNumCols(4);
       
   928     if (!topLevel)
       
   929         moveCommand->setAncestorRowNumbers(QList<int>() << 5);
   916     moveCommand->setStartRow(startRow);
   930     moveCommand->setStartRow(startRow);
   917     moveCommand->setEndRow(endRow);
   931     moveCommand->setEndRow(endRow);
   918     moveCommand->setDestRow(destRow);
   932     moveCommand->setDestRow(destRow);
       
   933     if (!topLevel)
       
   934         moveCommand->setDestAncestors(QList<int>() << 5);
   919     moveCommand->doCommand();
   935     moveCommand->doCommand();
   920 
   936 
   921     QVariantList beforeSignal = beforeSpy.takeAt(0);
   937     QVariantList beforeSignal = beforeSpy.takeAt(0);
   922     QVariantList afterSignal = afterSpy.takeAt(0);
   938     QVariantList afterSignal = afterSpy.takeAt(0);
   923 
   939 
   924     QCOMPARE(beforeSignal.size(), 5);
   940     QCOMPARE(beforeSignal.size(), 5);
   925     QCOMPARE(beforeSignal.at(0).value<QModelIndex>(), QModelIndex());
   941     QCOMPARE(beforeSignal.at(0).value<QModelIndex>(), moveParent);
   926     QCOMPARE(beforeSignal.at(1).toInt(), startRow);
   942     QCOMPARE(beforeSignal.at(1).toInt(), startRow);
   927     QCOMPARE(beforeSignal.at(2).toInt(), endRow);
   943     QCOMPARE(beforeSignal.at(2).toInt(), endRow);
   928     QCOMPARE(beforeSignal.at(3).value<QModelIndex>(), QModelIndex());
   944     QCOMPARE(beforeSignal.at(3).value<QModelIndex>(), moveParent);
   929     QCOMPARE(beforeSignal.at(4).toInt(), destRow);
   945     QCOMPARE(beforeSignal.at(4).toInt(), destRow);
   930 
   946 
   931     QCOMPARE(afterSignal.size(), 5);
   947     QCOMPARE(afterSignal.size(), 5);
   932     QCOMPARE(afterSignal.at(0).value<QModelIndex>(), QModelIndex());
   948     QCOMPARE(afterSignal.at(0).value<QModelIndex>(), moveParent);
   933     QCOMPARE(afterSignal.at(1).toInt(), startRow);
   949     QCOMPARE(afterSignal.at(1).toInt(), startRow);
   934     QCOMPARE(afterSignal.at(2).toInt(), endRow);
   950     QCOMPARE(afterSignal.at(2).toInt(), endRow);
   935     QCOMPARE(afterSignal.at(3).value<QModelIndex>(), QModelIndex());
   951     QCOMPARE(afterSignal.at(3).value<QModelIndex>(), moveParent);
   936     QCOMPARE(afterSignal.at(4).toInt(), destRow);
   952     QCOMPARE(afterSignal.at(4).toInt(), destRow);
   937 
   953 
   938     for (int i = 0; i < indexList.size(); i++)
   954     for (int i = 0; i < indexList.size(); i++)
   939     {
   955     {
   940         QModelIndex idx = indexList.at(i);
   956         QModelIndex idx = indexList.at(i);
   941         QModelIndex persistentIndex = persistentList.at(i);
   957         QModelIndex persistentIndex = persistentList.at(i);
   942         if (idx.parent() == QModelIndex())
   958         if (idx.parent() == moveParent)
   943         {
   959         {
   944             int row = idx.row();
   960             int row = idx.row();
   945             if ( row >= startRow)
   961             if ( row >= startRow)
   946             {
   962             {
   947                 if (row <= endRow)
   963                 if (row <= endRow)
   974 void tst_QAbstractItemModel::testMoveSameParentUp_data()
   990 void tst_QAbstractItemModel::testMoveSameParentUp_data()
   975 {
   991 {
   976     QTest::addColumn<int>("startRow");
   992     QTest::addColumn<int>("startRow");
   977     QTest::addColumn<int>("endRow");
   993     QTest::addColumn<int>("endRow");
   978     QTest::addColumn<int>("destRow");
   994     QTest::addColumn<int>("destRow");
       
   995     QTest::addColumn<bool>("topLevel");
   979 
   996 
   980     // Move from the middle to the start
   997     // Move from the middle to the start
   981     QTest::newRow("move01") << 5 << 7 << 0;
   998     QTest::newRow("move01") << 5 << 7 << 0 << true;
   982     // Move from the end to the start
   999     // Move from the end to the start
   983     QTest::newRow("move02") << 8 << 9 << 0;
  1000     QTest::newRow("move02") << 8 << 9 << 0 << true;
   984     // Move from the middle to the middle
  1001     // Move from the middle to the middle
   985     QTest::newRow("move03") << 5 << 7 << 2;
  1002     QTest::newRow("move03") << 5 << 7 << 2 << true;
   986     // Move from the end to the middle
  1003     // Move from the end to the middle
   987     QTest::newRow("move04") << 8 << 9 << 5;
  1004     QTest::newRow("move04") << 8 << 9 << 5 << true;
       
  1005 
       
  1006     QTest::newRow("move05") << 5 << 7 << 0 << false;
       
  1007     QTest::newRow("move06") << 8 << 9 << 0 << false;
       
  1008     QTest::newRow("move07") << 5 << 7 << 2 << false;
       
  1009     QTest::newRow("move08") << 8 << 9 << 5 << false;
   988 }
  1010 }
   989 
  1011 
   990 void tst_QAbstractItemModel::testMoveSameParentUp()
  1012 void tst_QAbstractItemModel::testMoveSameParentUp()
   991 {
  1013 {
   992 
  1014 
   993     QFETCH( int, startRow);
  1015     QFETCH( int, startRow);
   994     QFETCH( int, endRow);
  1016     QFETCH( int, endRow);
   995     QFETCH( int, destRow);
  1017     QFETCH( int, destRow);
       
  1018     QFETCH( bool, topLevel);
       
  1019 
       
  1020     QModelIndex moveParent = topLevel ? QModelIndex() : m_model->index(5, 0);
   996 
  1021 
   997     QList<QPersistentModelIndex> persistentList;
  1022     QList<QPersistentModelIndex> persistentList;
   998     QModelIndexList indexList;
  1023     QModelIndexList indexList;
   999 
  1024 
  1000     for (int column = 0; column < m_model->columnCount(); ++column)
  1025     for (int column = 0; column < m_model->columnCount(); ++column)
  1024     QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)));
  1049     QSignalSpy afterSpy(m_model, SIGNAL(rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)));
  1025 
  1050 
  1026 
  1051 
  1027     ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this);
  1052     ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this);
  1028     moveCommand->setNumCols(4);
  1053     moveCommand->setNumCols(4);
       
  1054     if (!topLevel)
       
  1055         moveCommand->setAncestorRowNumbers(QList<int>() << 5);
  1029     moveCommand->setStartRow(startRow);
  1056     moveCommand->setStartRow(startRow);
  1030     moveCommand->setEndRow(endRow);
  1057     moveCommand->setEndRow(endRow);
  1031     moveCommand->setDestRow(destRow);
  1058     moveCommand->setDestRow(destRow);
       
  1059     if (!topLevel)
       
  1060         moveCommand->setDestAncestors(QList<int>() << 5);
  1032     moveCommand->doCommand();
  1061     moveCommand->doCommand();
  1033 
  1062 
  1034     QVariantList beforeSignal = beforeSpy.takeAt(0);
  1063     QVariantList beforeSignal = beforeSpy.takeAt(0);
  1035     QVariantList afterSignal = afterSpy.takeAt(0);
  1064     QVariantList afterSignal = afterSpy.takeAt(0);
  1036 
  1065 
  1037     QCOMPARE(beforeSignal.size(), 5);
  1066     QCOMPARE(beforeSignal.size(), 5);
  1038     QCOMPARE(beforeSignal.at(0).value<QModelIndex>(), QModelIndex());
  1067     QCOMPARE(beforeSignal.at(0).value<QModelIndex>(), moveParent);
  1039     QCOMPARE(beforeSignal.at(1).toInt(), startRow);
  1068     QCOMPARE(beforeSignal.at(1).toInt(), startRow);
  1040     QCOMPARE(beforeSignal.at(2).toInt(), endRow);
  1069     QCOMPARE(beforeSignal.at(2).toInt(), endRow);
  1041     QCOMPARE(beforeSignal.at(3).value<QModelIndex>(), QModelIndex());
  1070     QCOMPARE(beforeSignal.at(3).value<QModelIndex>(), moveParent);
  1042     QCOMPARE(beforeSignal.at(4).toInt(), destRow);
  1071     QCOMPARE(beforeSignal.at(4).toInt(), destRow);
  1043 
  1072 
  1044     QCOMPARE(afterSignal.size(), 5);
  1073     QCOMPARE(afterSignal.size(), 5);
  1045     QCOMPARE(afterSignal.at(0).value<QModelIndex>(), QModelIndex());
  1074     QCOMPARE(afterSignal.at(0).value<QModelIndex>(), moveParent);
  1046     QCOMPARE(afterSignal.at(1).toInt(), startRow);
  1075     QCOMPARE(afterSignal.at(1).toInt(), startRow);
  1047     QCOMPARE(afterSignal.at(2).toInt(), endRow);
  1076     QCOMPARE(afterSignal.at(2).toInt(), endRow);
  1048     QCOMPARE(afterSignal.at(3).value<QModelIndex>(), QModelIndex());
  1077     QCOMPARE(afterSignal.at(3).value<QModelIndex>(), moveParent);
  1049     QCOMPARE(afterSignal.at(4).toInt(), destRow);
  1078     QCOMPARE(afterSignal.at(4).toInt(), destRow);
  1050 
  1079 
  1051 
  1080 
  1052     for (int i = 0; i < indexList.size(); i++)
  1081     for (int i = 0; i < indexList.size(); i++)
  1053     {
  1082     {
  1054         QModelIndex idx = indexList.at(i);
  1083         QModelIndex idx = indexList.at(i);
  1055         QModelIndex persistentIndex = persistentList.at(i);
  1084         QModelIndex persistentIndex = persistentList.at(i);
  1056         if (idx.parent() == QModelIndex())
  1085         if (idx.parent() == moveParent)
  1057         {
  1086         {
  1058             int row = idx.row();
  1087             int row = idx.row();
  1059             if ( row >= destRow)
  1088             if ( row >= destRow)
  1060             {
  1089             {
  1061                 if (row < startRow)
  1090                 if (row < startRow)
  1081         } else
  1110         } else
  1082         {
  1111         {
  1083             QCOMPARE(idx, persistentIndex);
  1112             QCOMPARE(idx, persistentIndex);
  1084         }
  1113         }
  1085     }
  1114     }
       
  1115 }
       
  1116 
       
  1117 void tst_QAbstractItemModel::testMoveThroughProxy()
       
  1118 {
       
  1119     QSortFilterProxyModel *proxy = new QSortFilterProxyModel(this);
       
  1120     proxy->setSourceModel(m_model);
       
  1121 
       
  1122     QList<QPersistentModelIndex> persistentList;
       
  1123 
       
  1124     persistentList.append(proxy->index(0, 0));
       
  1125     persistentList.append(proxy->index(0, 0, proxy->mapFromSource(m_model->index(5, 0))));
       
  1126 
       
  1127     ModelMoveCommand *moveCommand = new ModelMoveCommand(m_model, this);
       
  1128     moveCommand->setNumCols(4);
       
  1129     moveCommand->setAncestorRowNumbers(QList<int>() << 5);
       
  1130     moveCommand->setStartRow(0);
       
  1131     moveCommand->setEndRow(0);
       
  1132     moveCommand->setDestRow(0);
       
  1133     moveCommand->doCommand();
  1086 }
  1134 }
  1087 
  1135 
  1088 void tst_QAbstractItemModel::testMoveToGrandParent_data()
  1136 void tst_QAbstractItemModel::testMoveToGrandParent_data()
  1089 {
  1137 {
  1090     QTest::addColumn<int>("startRow");
  1138     QTest::addColumn<int>("startRow");