/tests/auto/qitemselectionmodel/tst_qitemselectionmodel.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 2713 lines · 2254 code · 329 blank · 130 comment · 91 complexity · 65f361859ee45d048fee8866e318933b MD5 · raw file

Large files are truncated click here to view the full file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the test suite of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include <QtTest/QtTest>
  42. #include <QtGui/QtGui>
  43. //TESTED_CLASS=
  44. //TESTED_FILES=
  45. class tst_QItemSelectionModel : public QObject
  46. {
  47. Q_OBJECT
  48. public:
  49. tst_QItemSelectionModel();
  50. virtual ~tst_QItemSelectionModel();
  51. public slots:
  52. void initTestCase();
  53. void cleanupTestCase();
  54. void init();
  55. private slots:
  56. void clear_data();
  57. void clear();
  58. void clearAndSelect();
  59. void toggleSelection();
  60. void select_data();
  61. void select();
  62. void persistentselections_data();
  63. void persistentselections();
  64. void resetModel();
  65. void removeRows_data();
  66. void removeRows();
  67. void removeColumns_data();
  68. void removeColumns();
  69. void modelLayoutChanged_data();
  70. void modelLayoutChanged();
  71. void selectedRows_data();
  72. void selectedRows();
  73. void selectedColumns_data();
  74. void selectedColumns();
  75. void setCurrentIndex();
  76. void splitOnInsert();
  77. void task196285_rowIntersectsSelection();
  78. void unselectable();
  79. void task220420_selectedIndexes();
  80. void task240734_layoutChanged();
  81. void merge_data();
  82. void merge();
  83. void task119433_isRowSelected();
  84. void task252069_rowIntersectsSelection();
  85. void task232634_childrenDeselectionSignal();
  86. void task260134_layoutChangedWithAllSelected();
  87. void QTBUG5671_layoutChangedWithAllSelected();
  88. void QTBUG2804_layoutChangedTreeSelection();
  89. void deselectRemovedMiddleRange();
  90. void rangeOperatorLessThan_data();
  91. void rangeOperatorLessThan();
  92. void testDifferentModels();
  93. void testValidRangesInSelectionsAfterReset();
  94. void testChainedSelectionClear();
  95. private:
  96. QAbstractItemModel *model;
  97. QItemSelectionModel *selection;
  98. };
  99. QDataStream &operator<<(QDataStream &, const QModelIndex &);
  100. QDataStream &operator>>(QDataStream &, QModelIndex &);
  101. QDataStream &operator<<(QDataStream &, const QModelIndexList &);
  102. QDataStream &operator>>(QDataStream &, QModelIndexList &);
  103. typedef QList<int> IntList;
  104. typedef QPair<int, int> IntPair;
  105. typedef QList<IntPair> PairList;
  106. Q_DECLARE_METATYPE(PairList)
  107. Q_DECLARE_METATYPE(QModelIndex)
  108. Q_DECLARE_METATYPE(QModelIndexList)
  109. Q_DECLARE_METATYPE(IntList)
  110. Q_DECLARE_METATYPE(QItemSelection)
  111. class QStreamHelper: public QAbstractItemModel
  112. {
  113. public:
  114. QStreamHelper() {}
  115. static QModelIndex create(int row = -1, int column = -1, void *data = 0)
  116. {
  117. QStreamHelper helper;
  118. return helper.QAbstractItemModel::createIndex(row, column, data);
  119. }
  120. QModelIndex index(int, int, const QModelIndex&) const
  121. { return QModelIndex(); }
  122. QModelIndex parent(const QModelIndex&) const
  123. { return QModelIndex(); }
  124. int rowCount(const QModelIndex & = QModelIndex()) const
  125. { return 0; }
  126. int columnCount(const QModelIndex & = QModelIndex()) const
  127. { return 0; }
  128. QVariant data(const QModelIndex &, int = Qt::DisplayRole) const
  129. { return QVariant(); }
  130. bool hasChildren(const QModelIndex &) const
  131. { return false; }
  132. };
  133. QDataStream &operator<<(QDataStream &s, const QModelIndex &input)
  134. {
  135. s << input.row()
  136. << input.column()
  137. << reinterpret_cast<qlonglong>(input.internalPointer());
  138. return s;
  139. }
  140. QDataStream &operator>>(QDataStream &s, QModelIndex &output)
  141. {
  142. int r, c;
  143. qlonglong ptr;
  144. s >> r;
  145. s >> c;
  146. s >> ptr;
  147. output = QStreamHelper::create(r, c, reinterpret_cast<void *>(ptr));
  148. return s;
  149. }
  150. QDataStream &operator<<(QDataStream &s, const QModelIndexList &input)
  151. {
  152. s << input.count();
  153. for (int i=0; i<input.count(); ++i)
  154. s << input.at(i);
  155. return s;
  156. }
  157. QDataStream &operator>>(QDataStream &s, QModelIndexList &output)
  158. {
  159. QModelIndex tmpIndex;
  160. int count;
  161. s >> count;
  162. for (int i=0; i<count; ++i) {
  163. s >> tmpIndex;
  164. output << tmpIndex;
  165. }
  166. return s;
  167. }
  168. tst_QItemSelectionModel::tst_QItemSelectionModel() : model(0), selection(0)
  169. {
  170. }
  171. tst_QItemSelectionModel::~tst_QItemSelectionModel()
  172. {
  173. }
  174. /*
  175. This test usually uses a model with a 5x5 table
  176. -------------------------------------------
  177. | 0,0 | 0,1 | 0,2 | 0,3 | 0,4 |
  178. -------------------------------------------
  179. | 1,0 | 1,1 | 1,2 | 1,3 | 1,4 |
  180. -------------------------------------------
  181. | 2,0 | 2,1 | 2,2 | 2,3 | 2,4 |
  182. -------------------------------------------
  183. | 3,0 | 3,1 | 3,2 | 3,3 | 3,4 |
  184. -------------------------------------------
  185. | 4,0 | 4,1 | 4,2 | 4,3 | 4,4 |
  186. -------------------------------------------
  187. ...that for each row has a children in a new 5x5 table ad infinitum.
  188. */
  189. void tst_QItemSelectionModel::initTestCase()
  190. {
  191. qRegisterMetaType<QItemSelection>("QItemSelection");
  192. model = new QStandardItemModel(5, 5);
  193. QModelIndex parent = model->index(0, 0, QModelIndex());
  194. model->insertRows(0, 5, parent);
  195. model->insertColumns(0, 5, parent);
  196. selection = new QItemSelectionModel(model);
  197. }
  198. void tst_QItemSelectionModel::cleanupTestCase()
  199. {
  200. delete selection;
  201. delete model;
  202. }
  203. void tst_QItemSelectionModel::init()
  204. {
  205. selection->clear();
  206. while (model->rowCount(QModelIndex()) > 5)
  207. model->removeRow(0, QModelIndex());
  208. while (model->rowCount(QModelIndex()) < 5)
  209. model->insertRow(0, QModelIndex());
  210. }
  211. void tst_QItemSelectionModel::clear_data()
  212. {
  213. QTest::addColumn<QModelIndexList>("indexList");
  214. QTest::addColumn<IntList>("commandList");
  215. {
  216. QModelIndexList index;
  217. IntList command;
  218. index << model->index(0, 0, QModelIndex());
  219. command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
  220. index << model->index(1, 0, QModelIndex());
  221. command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
  222. QTest::newRow("(0, 0) and (1, 0): Select|Rows")
  223. << index
  224. << command;
  225. }
  226. {
  227. QModelIndexList index;
  228. IntList command;
  229. index << model->index(0, 0, QModelIndex());
  230. command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
  231. index << model->index(0, 1, QModelIndex());
  232. command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
  233. QTest::newRow("(0, 0) and (1, 0): Select|Columns")
  234. << index
  235. << command;
  236. }
  237. {
  238. QModelIndexList index;
  239. IntList command;
  240. index << model->index(0, 0, QModelIndex());
  241. command << QItemSelectionModel::Select;
  242. index << model->index(1, 1, QModelIndex());
  243. command << QItemSelectionModel::Select;
  244. index << model->index(2, 2, QModelIndex());
  245. command << QItemSelectionModel::SelectCurrent;
  246. QTest::newRow("(0, 0), (1, 1) and (2, 2): Select, Select, SelectCurrent")
  247. << index
  248. << command;
  249. }
  250. {
  251. QModelIndexList index;
  252. IntList command;
  253. index << model->index(0, 0, QModelIndex());
  254. command << QItemSelectionModel::Select;
  255. index << model->index(1, 1, QModelIndex());
  256. command << QItemSelectionModel::Select;
  257. index << model->index(1, 1, QModelIndex());
  258. command << QItemSelectionModel::Toggle;
  259. QTest::newRow("(0, 0), (1, 1) and (1, 1): Select, Select, Toggle")
  260. << index
  261. << command;
  262. }
  263. {
  264. QModelIndexList index;
  265. IntList command;
  266. index << model->index(0, 0, model->index(0, 0, QModelIndex()));
  267. command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
  268. QTest::newRow("child (0, 0) of (0, 0): Select|Rows")
  269. << index
  270. << command;
  271. }
  272. }
  273. void tst_QItemSelectionModel::clear()
  274. {
  275. QFETCH(QModelIndexList, indexList);
  276. QFETCH(IntList, commandList);
  277. // do selections
  278. for (int i=0; i<indexList.count(); ++i) {
  279. selection->select(indexList.at(i), (QItemSelectionModel::SelectionFlags)commandList.at(i));
  280. }
  281. // test that we have selected items
  282. QVERIFY(!selection->selectedIndexes().isEmpty());
  283. selection->clear();
  284. // test that they were all cleared
  285. QVERIFY(selection->selectedIndexes().isEmpty());
  286. }
  287. void tst_QItemSelectionModel::clearAndSelect()
  288. {
  289. // populate selectionmodel
  290. selection->select(model->index(1, 1, QModelIndex()), QItemSelectionModel::Select);
  291. QCOMPARE(selection->selectedIndexes().count(), 1);
  292. QVERIFY(selection->hasSelection());
  293. // ClearAndSelect with empty selection
  294. QItemSelection emptySelection;
  295. selection->select(emptySelection, QItemSelectionModel::ClearAndSelect);
  296. // verify the selectionmodel is empty
  297. QVERIFY(selection->selectedIndexes().isEmpty());
  298. QVERIFY(selection->hasSelection()==false);
  299. }
  300. void tst_QItemSelectionModel::toggleSelection()
  301. {
  302. //test the toggle selection and checks whether selectedIndex
  303. //and hasSelection returns the correct value
  304. selection->clearSelection();
  305. QCOMPARE(selection->selectedIndexes().count(), 0);
  306. QVERIFY(selection->hasSelection()==false);
  307. QModelIndex index=model->index(1, 1, QModelIndex());
  308. // populate selectionmodel
  309. selection->select(index, QItemSelectionModel::Toggle);
  310. QCOMPARE(selection->selectedIndexes().count(), 1);
  311. QVERIFY(selection->hasSelection()==true);
  312. selection->select(index, QItemSelectionModel::Toggle);
  313. QCOMPARE(selection->selectedIndexes().count(), 0);
  314. QVERIFY(selection->hasSelection()==false);
  315. // populate selectionmodel with rows
  316. selection->select(index, QItemSelectionModel::Toggle | QItemSelectionModel::Rows);
  317. QCOMPARE(selection->selectedIndexes().count(), model->columnCount());
  318. QVERIFY(selection->hasSelection()==true);
  319. selection->select(index, QItemSelectionModel::Toggle | QItemSelectionModel::Rows);
  320. QCOMPARE(selection->selectedIndexes().count(), 0);
  321. QVERIFY(selection->hasSelection()==false);
  322. }
  323. void tst_QItemSelectionModel::select_data()
  324. {
  325. QTest::addColumn<QModelIndexList>("indexList");
  326. QTest::addColumn<bool>("useRanges");
  327. QTest::addColumn<IntList>("commandList");
  328. QTest::addColumn<QModelIndexList>("expectedList");
  329. {
  330. QModelIndexList index;
  331. QModelIndexList expected;
  332. IntList command;
  333. index << model->index(0, 0, QModelIndex());
  334. command << QItemSelectionModel::Select;
  335. expected << model->index(0, 0, QModelIndex());
  336. QTest::newRow("(0, 0): Select")
  337. << index
  338. << false
  339. << command
  340. << expected;
  341. }
  342. {
  343. QModelIndexList index;
  344. QModelIndexList expected;
  345. IntList command;
  346. index << model->index(0, 0, model->index(0, 0, QModelIndex()));
  347. command << QItemSelectionModel::Select;
  348. expected << model->index(0, 0, model->index(0, 0, QModelIndex()));
  349. QTest::newRow("child (0, 0) of (0, 0): Select")
  350. << index
  351. << false
  352. << command
  353. << expected;
  354. }
  355. {
  356. QModelIndexList index;
  357. QModelIndexList expected;
  358. IntList command;
  359. index << model->index(0, 0, QModelIndex());
  360. command << QItemSelectionModel::Deselect;
  361. QTest::newRow("(0, 0): Deselect")
  362. << index
  363. << false
  364. << command
  365. << expected;
  366. }
  367. {
  368. QModelIndexList index;
  369. QModelIndexList expected;
  370. IntList command;
  371. index << model->index(0, 0, QModelIndex());
  372. command << QItemSelectionModel::Toggle;
  373. expected << model->index(0, 0, QModelIndex());
  374. QTest::newRow("(0, 0): Toggle")
  375. << index
  376. << false
  377. << command
  378. << expected;
  379. }
  380. {
  381. QModelIndexList index;
  382. QModelIndexList expected;
  383. IntList command;
  384. index << model->index(0, 0, QModelIndex());
  385. command << QItemSelectionModel::Select;
  386. index << model->index(0, 0, QModelIndex());
  387. command << QItemSelectionModel::Toggle;
  388. QTest::newRow("(0, 0) and (0, 0): Select and Toggle")
  389. << index
  390. << false
  391. << command
  392. << expected;
  393. }
  394. {
  395. QModelIndexList index;
  396. QModelIndexList expected;
  397. IntList command;
  398. index << model->index(0, 0, QModelIndex());
  399. command << QItemSelectionModel::Select;
  400. index << model->index(0, 0, QModelIndex());
  401. command << QItemSelectionModel::Deselect;
  402. QTest::newRow("(0, 0) and (0, 0): Select and Deselect")
  403. << index
  404. << false
  405. << command
  406. << expected;
  407. }
  408. {
  409. QModelIndexList index;
  410. QModelIndexList expected;
  411. IntList command;
  412. index << model->index(0, 0, QModelIndex());
  413. command << QItemSelectionModel::Select;
  414. index << model->index(0, 0, model->index(0, 0, QModelIndex()));
  415. command << QItemSelectionModel::ClearAndSelect;
  416. expected << model->index(0, 0, model->index(0, 0, QModelIndex()));
  417. QTest::newRow("(0, 0) and child (0, 0) of (0, 0): Select and ClearAndSelect")
  418. << index
  419. << false
  420. << command
  421. << expected;
  422. }
  423. {
  424. QModelIndexList index;
  425. QModelIndexList expected;
  426. IntList command;
  427. index << model->index(0, 0, QModelIndex());
  428. index << model->index(4, 0, QModelIndex());
  429. command << QItemSelectionModel::Select;
  430. index << model->index(0, 1, QModelIndex());
  431. index << model->index(4, 1, QModelIndex());
  432. command << QItemSelectionModel::Select;
  433. index << model->index(0, 0, QModelIndex());
  434. index << model->index(4, 1, QModelIndex());
  435. command << QItemSelectionModel::Deselect;
  436. QTest::newRow("(0, 0 to 4, 0) and (0, 1 to 4, 1) and (0, 0 to 4, 1): Select and Select and Deselect")
  437. << index
  438. << true
  439. << command
  440. << expected;
  441. }
  442. {
  443. QModelIndexList index;
  444. QModelIndexList expected;
  445. IntList command;
  446. index << model->index(0, 0, QModelIndex());
  447. command << QItemSelectionModel::Select;
  448. index << model->index(4, 4, QModelIndex());
  449. command << QItemSelectionModel::Select;
  450. expected << model->index(0, 0, QModelIndex()) << model->index(4, 4, QModelIndex());
  451. QTest::newRow("(0, 0) and (4, 4): Select")
  452. << index
  453. << false
  454. << command
  455. << expected;
  456. }
  457. {
  458. QModelIndexList index;
  459. QModelIndexList expected;
  460. IntList command;
  461. index << model->index(0, 0, QModelIndex());
  462. command << QItemSelectionModel::Select;
  463. index << model->index(4, 4, QModelIndex());
  464. command << QItemSelectionModel::ClearAndSelect;
  465. expected << model->index(4, 4, QModelIndex());
  466. QTest::newRow("(0, 0) and (4, 4): Select and ClearAndSelect")
  467. << index
  468. << false
  469. << command
  470. << expected;
  471. }
  472. {
  473. QModelIndexList index;
  474. QModelIndexList expected;
  475. IntList command;
  476. index << model->index(0, 0, QModelIndex());
  477. command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
  478. index << model->index(4, 4, QModelIndex());
  479. command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
  480. expected << model->index(0, 0, QModelIndex())
  481. << model->index(0, 1, QModelIndex())
  482. << model->index(0, 2, QModelIndex())
  483. << model->index(0, 3, QModelIndex())
  484. << model->index(0, 4, QModelIndex())
  485. << model->index(4, 0, QModelIndex())
  486. << model->index(4, 1, QModelIndex())
  487. << model->index(4, 2, QModelIndex())
  488. << model->index(4, 3, QModelIndex())
  489. << model->index(4, 4, QModelIndex());
  490. QTest::newRow("(0, 0) and (4, 4): Select|Rows")
  491. << index
  492. << false
  493. << command
  494. << expected;
  495. }
  496. {
  497. QModelIndexList index;
  498. QModelIndexList expected;
  499. IntList command;
  500. index << model->index(0, 0, model->index(0, 0, QModelIndex()));
  501. command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
  502. index << model->index(4, 4, model->index(0, 0, QModelIndex()));
  503. command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
  504. QModelIndex parent = model->index(0, 0, QModelIndex());
  505. expected << model->index(0, 0, parent)
  506. << model->index(0, 1, parent)
  507. << model->index(0, 2, parent)
  508. << model->index(0, 3, parent)
  509. << model->index(0, 4, parent)
  510. << model->index(4, 0, parent)
  511. << model->index(4, 1, parent)
  512. << model->index(4, 2, parent)
  513. << model->index(4, 3, parent)
  514. << model->index(4, 4, parent);
  515. QTest::newRow("child (0, 0) and (4, 4) of (0, 0): Select|Rows")
  516. << index
  517. << false
  518. << command
  519. << expected;
  520. }
  521. {
  522. QModelIndexList index;
  523. QModelIndexList expected;
  524. IntList command;
  525. index << model->index(0, 0, QModelIndex());
  526. command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
  527. index << model->index(4, 4, QModelIndex());
  528. command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
  529. expected << model->index(0, 0, QModelIndex())
  530. << model->index(1, 0, QModelIndex())
  531. << model->index(2, 0, QModelIndex())
  532. << model->index(3, 0, QModelIndex())
  533. << model->index(4, 0, QModelIndex())
  534. << model->index(0, 4, QModelIndex())
  535. << model->index(1, 4, QModelIndex())
  536. << model->index(2, 4, QModelIndex())
  537. << model->index(3, 4, QModelIndex())
  538. << model->index(4, 4, QModelIndex());
  539. QTest::newRow("(0, 0) and (4, 4): Select|Columns")
  540. << index
  541. << false
  542. << command
  543. << expected;
  544. }
  545. {
  546. QModelIndexList index;
  547. QModelIndexList expected;
  548. IntList command;
  549. index << model->index(0, 0, model->index(0, 0, QModelIndex()));
  550. command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
  551. index << model->index(4, 4, model->index(0, 0, QModelIndex()));
  552. command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
  553. expected << model->index(0, 0, model->index(0, 0, QModelIndex()))
  554. << model->index(1, 0, model->index(0, 0, QModelIndex()))
  555. << model->index(2, 0, model->index(0, 0, QModelIndex()))
  556. << model->index(3, 0, model->index(0, 0, QModelIndex()))
  557. << model->index(4, 0, model->index(0, 0, QModelIndex()))
  558. << model->index(0, 4, model->index(0, 0, QModelIndex()))
  559. << model->index(1, 4, model->index(0, 0, QModelIndex()))
  560. << model->index(2, 4, model->index(0, 0, QModelIndex()))
  561. << model->index(3, 4, model->index(0, 0, QModelIndex()))
  562. << model->index(4, 4, model->index(0, 0, QModelIndex()));
  563. QTest::newRow("child (0, 0) and (4, 4) of (0, 0): Select|Columns")
  564. << index
  565. << false
  566. << command
  567. << expected;
  568. }
  569. {
  570. QModelIndexList index;
  571. QModelIndexList expected;
  572. IntList command;
  573. index << model->index(0, 0, QModelIndex());
  574. index << model->index(4, 0, QModelIndex());
  575. command << QItemSelectionModel::Select;
  576. expected << model->index(0, 0, QModelIndex())
  577. << model->index(1, 0, QModelIndex())
  578. << model->index(2, 0, QModelIndex())
  579. << model->index(3, 0, QModelIndex())
  580. << model->index(4, 0, QModelIndex());
  581. QTest::newRow("(0, 0 to 4, 0): Select")
  582. << index
  583. << true
  584. << command
  585. << expected;
  586. }
  587. /* ### FAILS
  588. {
  589. QModelIndexList index;
  590. QModelIndexList expected;
  591. IntList command;
  592. index << model->index(0, 0, QModelIndex());
  593. index << model->index(0, 0, model->index(0, 0, QModelIndex()));
  594. command << QItemSelectionModel::Select;
  595. QTest::newRow("(0, 0 to child 0, 0): Select")
  596. << index
  597. << true
  598. << command
  599. << expected;
  600. }
  601. */
  602. {
  603. QModelIndexList index;
  604. QModelIndexList expected;
  605. IntList command;
  606. index << model->index(0, 0, model->index(0, 0, QModelIndex()));
  607. index << model->index(0, 0, model->index(1, 0, QModelIndex()));
  608. command << QItemSelectionModel::Select;
  609. QTest::newRow("child (0, 0) of (0, 0) to child (0, 0) of (1, 0): Select")
  610. << index
  611. << true
  612. << command
  613. << expected;
  614. }
  615. {
  616. QModelIndexList index;
  617. QModelIndexList expected;
  618. IntList command;
  619. index << model->index(0, 0, QModelIndex());
  620. index << model->index(4, 4, QModelIndex());
  621. command << QItemSelectionModel::Select;
  622. expected << model->index(0, 0, QModelIndex())
  623. << model->index(0, 1, QModelIndex())
  624. << model->index(0, 2, QModelIndex())
  625. << model->index(0, 3, QModelIndex())
  626. << model->index(0, 4, QModelIndex())
  627. << model->index(1, 0, QModelIndex())
  628. << model->index(1, 1, QModelIndex())
  629. << model->index(1, 2, QModelIndex())
  630. << model->index(1, 3, QModelIndex())
  631. << model->index(1, 4, QModelIndex())
  632. << model->index(2, 0, QModelIndex())
  633. << model->index(2, 1, QModelIndex())
  634. << model->index(2, 2, QModelIndex())
  635. << model->index(2, 3, QModelIndex())
  636. << model->index(2, 4, QModelIndex())
  637. << model->index(3, 0, QModelIndex())
  638. << model->index(3, 1, QModelIndex())
  639. << model->index(3, 2, QModelIndex())
  640. << model->index(3, 3, QModelIndex())
  641. << model->index(3, 4, QModelIndex())
  642. << model->index(4, 0, QModelIndex())
  643. << model->index(4, 1, QModelIndex())
  644. << model->index(4, 2, QModelIndex())
  645. << model->index(4, 3, QModelIndex())
  646. << model->index(4, 4, QModelIndex());
  647. QTest::newRow("(0, 0 to 4, 4): Select")
  648. << index
  649. << true
  650. << command
  651. << expected;
  652. }
  653. {
  654. QModelIndexList index;
  655. QModelIndexList expected;
  656. IntList command;
  657. index << model->index(0, 0, QModelIndex());
  658. index << model->index(4, 0, QModelIndex());
  659. command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
  660. expected << model->index(0, 0, QModelIndex())
  661. << model->index(0, 1, QModelIndex())
  662. << model->index(0, 2, QModelIndex())
  663. << model->index(0, 3, QModelIndex())
  664. << model->index(0, 4, QModelIndex())
  665. << model->index(1, 0, QModelIndex())
  666. << model->index(1, 1, QModelIndex())
  667. << model->index(1, 2, QModelIndex())
  668. << model->index(1, 3, QModelIndex())
  669. << model->index(1, 4, QModelIndex())
  670. << model->index(2, 0, QModelIndex())
  671. << model->index(2, 1, QModelIndex())
  672. << model->index(2, 2, QModelIndex())
  673. << model->index(2, 3, QModelIndex())
  674. << model->index(2, 4, QModelIndex())
  675. << model->index(3, 0, QModelIndex())
  676. << model->index(3, 1, QModelIndex())
  677. << model->index(3, 2, QModelIndex())
  678. << model->index(3, 3, QModelIndex())
  679. << model->index(3, 4, QModelIndex())
  680. << model->index(4, 0, QModelIndex())
  681. << model->index(4, 1, QModelIndex())
  682. << model->index(4, 2, QModelIndex())
  683. << model->index(4, 3, QModelIndex())
  684. << model->index(4, 4, QModelIndex());
  685. QTest::newRow("(0, 0 to 4, 0): Select|Rows")
  686. << index
  687. << true
  688. << command
  689. << expected;
  690. }
  691. {
  692. QModelIndexList index;
  693. QModelIndexList expected;
  694. IntList command;
  695. index << model->index(0, 0, QModelIndex());
  696. index << model->index(0, 4, QModelIndex());
  697. command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
  698. expected << model->index(0, 0, QModelIndex())
  699. << model->index(0, 1, QModelIndex())
  700. << model->index(0, 2, QModelIndex())
  701. << model->index(0, 3, QModelIndex())
  702. << model->index(0, 4, QModelIndex())
  703. << model->index(1, 0, QModelIndex())
  704. << model->index(1, 1, QModelIndex())
  705. << model->index(1, 2, QModelIndex())
  706. << model->index(1, 3, QModelIndex())
  707. << model->index(1, 4, QModelIndex())
  708. << model->index(2, 0, QModelIndex())
  709. << model->index(2, 1, QModelIndex())
  710. << model->index(2, 2, QModelIndex())
  711. << model->index(2, 3, QModelIndex())
  712. << model->index(2, 4, QModelIndex())
  713. << model->index(3, 0, QModelIndex())
  714. << model->index(3, 1, QModelIndex())
  715. << model->index(3, 2, QModelIndex())
  716. << model->index(3, 3, QModelIndex())
  717. << model->index(3, 4, QModelIndex())
  718. << model->index(4, 0, QModelIndex())
  719. << model->index(4, 1, QModelIndex())
  720. << model->index(4, 2, QModelIndex())
  721. << model->index(4, 3, QModelIndex())
  722. << model->index(4, 4, QModelIndex());
  723. QTest::newRow("(0, 0 to 0, 4): Select|Columns")
  724. << index
  725. << true
  726. << command
  727. << expected;
  728. }
  729. {
  730. QModelIndexList index;
  731. QModelIndexList expected;
  732. IntList command;
  733. index << model->index(0, 0, QModelIndex());
  734. index << model->index(4, 4, QModelIndex());
  735. command << (QItemSelectionModel::Select | QItemSelectionModel::Rows);
  736. expected << model->index(0, 0, QModelIndex())
  737. << model->index(0, 1, QModelIndex())
  738. << model->index(0, 2, QModelIndex())
  739. << model->index(0, 3, QModelIndex())
  740. << model->index(0, 4, QModelIndex())
  741. << model->index(1, 0, QModelIndex())
  742. << model->index(1, 1, QModelIndex())
  743. << model->index(1, 2, QModelIndex())
  744. << model->index(1, 3, QModelIndex())
  745. << model->index(1, 4, QModelIndex())
  746. << model->index(2, 0, QModelIndex())
  747. << model->index(2, 1, QModelIndex())
  748. << model->index(2, 2, QModelIndex())
  749. << model->index(2, 3, QModelIndex())
  750. << model->index(2, 4, QModelIndex())
  751. << model->index(3, 0, QModelIndex())
  752. << model->index(3, 1, QModelIndex())
  753. << model->index(3, 2, QModelIndex())
  754. << model->index(3, 3, QModelIndex())
  755. << model->index(3, 4, QModelIndex())
  756. << model->index(4, 0, QModelIndex())
  757. << model->index(4, 1, QModelIndex())
  758. << model->index(4, 2, QModelIndex())
  759. << model->index(4, 3, QModelIndex())
  760. << model->index(4, 4, QModelIndex());
  761. QTest::newRow("(0, 0 to 4, 4): Select|Rows")
  762. << index
  763. << true
  764. << command
  765. << expected;
  766. }
  767. {
  768. QModelIndexList index;
  769. QModelIndexList expected;
  770. IntList command;
  771. index << model->index(0, 0, QModelIndex());
  772. index << model->index(4, 4, QModelIndex());
  773. command << (QItemSelectionModel::Select | QItemSelectionModel::Columns);
  774. expected << model->index(0, 0, QModelIndex())
  775. << model->index(0, 1, QModelIndex())
  776. << model->index(0, 2, QModelIndex())
  777. << model->index(0, 3, QModelIndex())
  778. << model->index(0, 4, QModelIndex())
  779. << model->index(1, 0, QModelIndex())
  780. << model->index(1, 1, QModelIndex())
  781. << model->index(1, 2, QModelIndex())
  782. << model->index(1, 3, QModelIndex())
  783. << model->index(1, 4, QModelIndex())
  784. << model->index(2, 0, QModelIndex())
  785. << model->index(2, 1, QModelIndex())
  786. << model->index(2, 2, QModelIndex())
  787. << model->index(2, 3, QModelIndex())
  788. << model->index(2, 4, QModelIndex())
  789. << model->index(3, 0, QModelIndex())
  790. << model->index(3, 1, QModelIndex())
  791. << model->index(3, 2, QModelIndex())
  792. << model->index(3, 3, QModelIndex())
  793. << model->index(3, 4, QModelIndex())
  794. << model->index(4, 0, QModelIndex())
  795. << model->index(4, 1, QModelIndex())
  796. << model->index(4, 2, QModelIndex())
  797. << model->index(4, 3, QModelIndex())
  798. << model->index(4, 4, QModelIndex());
  799. QTest::newRow("(0, 0 to 4, 4): Select|Columns")
  800. << index
  801. << true
  802. << command
  803. << expected;
  804. }
  805. {
  806. QModelIndexList index;
  807. QModelIndexList expected;
  808. IntList command;
  809. index << model->index(0, 2, QModelIndex());
  810. index << model->index(4, 2, QModelIndex());
  811. command << QItemSelectionModel::Select;
  812. index << model->index(2, 0, QModelIndex());
  813. index << model->index(2, 4, QModelIndex());
  814. command << QItemSelectionModel::Select;
  815. expected << model->index(0, 2, QModelIndex())
  816. << model->index(1, 2, QModelIndex())
  817. << model->index(2, 2, QModelIndex())
  818. << model->index(3, 2, QModelIndex())
  819. << model->index(4, 2, QModelIndex())
  820. << model->index(2, 0, QModelIndex())
  821. << model->index(2, 1, QModelIndex())
  822. << model->index(2, 3, QModelIndex())
  823. << model->index(2, 4, QModelIndex());
  824. QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select")
  825. << index
  826. << true
  827. << command
  828. << expected;
  829. }
  830. {
  831. QModelIndexList index;
  832. QModelIndexList expected;
  833. IntList command;
  834. index << model->index(0, 2, QModelIndex());
  835. index << model->index(4, 2, QModelIndex());
  836. command << QItemSelectionModel::Select;
  837. index << model->index(2, 0, QModelIndex());
  838. index << model->index(2, 4, QModelIndex());
  839. command << QItemSelectionModel::SelectCurrent;
  840. expected << model->index(2, 0, QModelIndex())
  841. << model->index(2, 1, QModelIndex())
  842. << model->index(2, 2, QModelIndex())
  843. << model->index(2, 3, QModelIndex())
  844. << model->index(2, 4, QModelIndex());
  845. QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select and SelectCurrent")
  846. << index
  847. << true
  848. << command
  849. << expected;
  850. }
  851. {
  852. QModelIndexList index;
  853. QModelIndexList expected;
  854. IntList command;
  855. index << model->index(0, 2, QModelIndex());
  856. index << model->index(4, 2, QModelIndex());
  857. command << QItemSelectionModel::Select;
  858. index << model->index(2, 0, QModelIndex());
  859. index << model->index(2, 4, QModelIndex());
  860. command << QItemSelectionModel::Toggle;
  861. expected << model->index(0, 2, QModelIndex())
  862. << model->index(1, 2, QModelIndex())
  863. << model->index(3, 2, QModelIndex())
  864. << model->index(4, 2, QModelIndex())
  865. << model->index(2, 0, QModelIndex())
  866. << model->index(2, 1, QModelIndex())
  867. << model->index(2, 3, QModelIndex())
  868. << model->index(2, 4, QModelIndex());
  869. QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select and Toggle")
  870. << index
  871. << true
  872. << command
  873. << expected;
  874. }
  875. {
  876. QModelIndexList index;
  877. QModelIndexList expected;
  878. IntList command;
  879. index << model->index(0, 2, QModelIndex());
  880. index << model->index(4, 2, QModelIndex());
  881. command << QItemSelectionModel::Select;
  882. index << model->index(2, 0, QModelIndex());
  883. index << model->index(2, 4, QModelIndex());
  884. command << QItemSelectionModel::Deselect;
  885. expected << model->index(0, 2, QModelIndex())
  886. << model->index(1, 2, QModelIndex())
  887. << model->index(3, 2, QModelIndex())
  888. << model->index(4, 2, QModelIndex());
  889. QTest::newRow("(0, 2 to 4, 2) and (2, 0 to 2, 4): Select and Deselect")
  890. << index
  891. << true
  892. << command
  893. << expected;
  894. }
  895. {
  896. QModelIndexList index;
  897. QModelIndexList expected;
  898. IntList command;
  899. index << model->index(0, 0, QModelIndex());
  900. index << model->index(2, 2, QModelIndex());
  901. command << QItemSelectionModel::Select;
  902. index << model->index(0, 0, QModelIndex());
  903. index << model->index(0, 0, QModelIndex());
  904. command << QItemSelectionModel::Toggle;
  905. expected << model->index(0, 1, QModelIndex())
  906. << model->index(0, 2, QModelIndex())
  907. << model->index(1, 0, QModelIndex())
  908. << model->index(1, 1, QModelIndex())
  909. << model->index(1, 2, QModelIndex())
  910. << model->index(2, 0, QModelIndex())
  911. << model->index(2, 1, QModelIndex())
  912. << model->index(2, 2, QModelIndex());
  913. QTest::newRow("(0, 0 to 2, 2) and (0, 0 to 0, 0): Select and Toggle at selection boundary")
  914. << index
  915. << true
  916. << command
  917. << expected;
  918. }
  919. {
  920. QModelIndexList index;
  921. QModelIndexList expected;
  922. IntList command;
  923. index << model->index(0, 0, QModelIndex());
  924. index << model->index(2, 2, QModelIndex());
  925. command << QItemSelectionModel::Select;
  926. index << model->index(0, 1, QModelIndex());
  927. index << model->index(0, 1, QModelIndex());
  928. command << QItemSelectionModel::Toggle;
  929. expected << model->index(0, 0, QModelIndex())
  930. << model->index(0, 2, QModelIndex())
  931. << model->index(1, 0, QModelIndex())
  932. << model->index(1, 1, QModelIndex())
  933. << model->index(1, 2, QModelIndex())
  934. << model->index(2, 0, QModelIndex())
  935. << model->index(2, 1, QModelIndex())
  936. << model->index(2, 2, QModelIndex());
  937. QTest::newRow("(0, 0 to 2, 2) and (0, 1 to 0, 1): Select and Toggle at selection boundary")
  938. << index
  939. << true
  940. << command
  941. << expected;
  942. }
  943. {
  944. QModelIndexList index;
  945. QModelIndexList expected;
  946. IntList command;
  947. index << model->index(0, 0, QModelIndex());
  948. index << model->index(2, 2, QModelIndex());
  949. command << QItemSelectionModel::Select;
  950. index << model->index(0, 2, QModelIndex());
  951. index << model->index(0, 2, QModelIndex());
  952. command << QItemSelectionModel::Toggle;
  953. expected << model->index(0, 0, QModelIndex())
  954. << model->index(0, 1, QModelIndex())
  955. << model->index(1, 0, QModelIndex())
  956. << model->index(1, 1, QModelIndex())
  957. << model->index(1, 2, QModelIndex())
  958. << model->index(2, 0, QModelIndex())
  959. << model->index(2, 1, QModelIndex())
  960. << model->index(2, 2, QModelIndex());
  961. QTest::newRow("(0, 0 to 2, 2) and (0, 2 to 0, 2): Select and Toggle at selection boundary")
  962. << index
  963. << true
  964. << command
  965. << expected;
  966. }
  967. {
  968. QModelIndexList index;
  969. QModelIndexList expected;
  970. IntList command;
  971. index << model->index(0, 0, QModelIndex());
  972. index << model->index(2, 2, QModelIndex());
  973. command << QItemSelectionModel::Select;
  974. index << model->index(1, 0, QModelIndex());
  975. index << model->index(1, 0, QModelIndex());
  976. command << QItemSelectionModel::Toggle;
  977. expected << model->index(0, 0, QModelIndex())
  978. << model->index(0, 1, QModelIndex())
  979. << model->index(0, 2, QModelIndex())
  980. << model->index(1, 1, QModelIndex())
  981. << model->index(1, 2, QModelIndex())
  982. << model->index(2, 0, QModelIndex())
  983. << model->index(2, 1, QModelIndex())
  984. << model->index(2, 2, QModelIndex());
  985. QTest::newRow("(0, 0 to 2, 2) and (1, 0 to 1, 0): Select and Toggle at selection boundary")
  986. << index
  987. << true
  988. << command
  989. << expected;
  990. }
  991. {
  992. QModelIndexList index;
  993. QModelIndexList expected;
  994. IntList command;
  995. index << model->index(0, 0, QModelIndex());
  996. index << model->index(2, 2, QModelIndex());
  997. command << QItemSelectionModel::Select;
  998. index << model->index(1, 1, QModelIndex());
  999. index << model->index(1, 1, QModelIndex());
  1000. command << QItemSelectionModel::Toggle;
  1001. expected << model->index(0, 0, QModelIndex())
  1002. << model->index(0, 1, QModelIndex())
  1003. << model->index(0, 2, QModelIndex())
  1004. << model->index(1, 0, QModelIndex())
  1005. << model->index(1, 2, QModelIndex())
  1006. << model->index(2, 0, QModelIndex())
  1007. << model->index(2, 1, QModelIndex())
  1008. << model->index(2, 2, QModelIndex());
  1009. QTest::newRow("(0, 0 to 2, 2) and (1, 1 to 1, 1): Select and Toggle at selection boundary")
  1010. << index
  1011. << true
  1012. << command
  1013. << expected;
  1014. }
  1015. {
  1016. QModelIndexList index;
  1017. QModelIndexList expected;
  1018. IntList command;
  1019. index << model->index(0, 0, QModelIndex());
  1020. index << model->index(2, 2, QModelIndex());
  1021. command << QItemSelectionModel::Select;
  1022. index << model->index(1, 2, QModelIndex());
  1023. index << model->index(1, 2, QModelIndex());
  1024. command << QItemSelectionModel::Toggle;
  1025. expected << model->index(0, 0, QModelIndex())
  1026. << model->index(0, 1, QModelIndex())
  1027. << model->index(0, 2, QModelIndex())
  1028. << model->index(1, 0, QModelIndex())
  1029. << model->index(1, 1, QModelIndex())
  1030. << model->index(2, 0, QModelIndex())
  1031. << model->index(2, 1, QModelIndex())
  1032. << model->index(2, 2, QModelIndex());
  1033. QTest::newRow("(0, 0 to 2, 2) and (1, 2 to 1, 2): Select and Toggle at selection boundary")
  1034. << index
  1035. << true
  1036. << command
  1037. << expected;
  1038. }
  1039. {
  1040. QModelIndexList index;
  1041. QModelIndexList expected;
  1042. IntList command;
  1043. index << model->index(0, 0, QModelIndex());
  1044. index << model->index(2, 2, QModelIndex());
  1045. command << QItemSelectionModel::Select;
  1046. index << model->index(2, 0, QModelIndex());
  1047. index << model->index(2, 0, QModelIndex());
  1048. command << QItemSelectionModel::Toggle;
  1049. expected << model->index(0, 0, QModelIndex())
  1050. << model->index(0, 1, QModelIndex())
  1051. << model->index(0, 2, QModelIndex())
  1052. << model->index(1, 0, QModelIndex())
  1053. << model->index(1, 1, QModelIndex())
  1054. << model->index(1, 2, QModelIndex())
  1055. << model->index(2, 1, QModelIndex())
  1056. << model->index(2, 2, QModelIndex());
  1057. QTest::newRow("(0, 0 to 2, 2) and (2, 0 to 2, 0): Select and Toggle at selection boundary")
  1058. << index
  1059. << true
  1060. << command
  1061. << expected;
  1062. }
  1063. {
  1064. QModelIndexList index;
  1065. QModelIndexList expected;
  1066. IntList command;
  1067. index << model->index(0, 0, QModelIndex());
  1068. index << model->index(2, 2, QModelIndex());
  1069. command << QItemSelectionModel::Select;
  1070. index << model->index(2, 1, QModelIndex());
  1071. index << model->index(2, 1, QModelIndex());
  1072. command << QItemSelectionModel::Toggle;
  1073. expected << model->index(0, 0, QModelIndex())
  1074. << model->index(0, 1, QModelIndex())
  1075. << model->index(0, 2, QModelIndex())
  1076. << model->index(1, 0, QModelIndex())
  1077. << model->index(1, 1, QModelIndex())
  1078. << model->index(1, 2, QModelIndex())
  1079. << model->index(2, 0, QModelIndex())
  1080. << model->index(2, 2, QModelIndex());
  1081. QTest::newRow("(0, 0 to 2, 2) and (2, 1 to 2, 1): Select and Toggle at selection boundary")
  1082. << index
  1083. << true
  1084. << command
  1085. << expected;
  1086. }
  1087. {
  1088. QModelIndexList index;
  1089. QModelIndexList expected;
  1090. IntList command;
  1091. index << model->index(0, 0, QModelIndex());
  1092. index << model->index(2, 2, QModelIndex());
  1093. command << QItemSelectionModel::Select;
  1094. index << model->index(2, 2, QModelIndex());
  1095. index << model->index(2, 2, QModelIndex());
  1096. command << QItemSelectionModel::Toggle;
  1097. expected << model->index(0, 0, QModelIndex())
  1098. << model->index(0, 1, QModelIndex())
  1099. << model->index(0, 2, QModelIndex())
  1100. << model->index(1, 0, QModelIndex())
  1101. << model->index(1, 1, QModelIndex())
  1102. << model->index(1, 2, QModelIndex())
  1103. << model->index(2, 0, QModelIndex())
  1104. << model->index(2, 1, QModelIndex());
  1105. QTest::newRow("(0, 0 to 2, 2) and (2, 2 to 2, 2): Select and Toggle at selection boundary")
  1106. << index
  1107. << true
  1108. << command
  1109. << expected;
  1110. }
  1111. {
  1112. QModelIndexList indexes;
  1113. IntList commands;
  1114. QModelIndexList expected;
  1115. indexes << model->index(0, 0, QModelIndex()) << model->index(0, 0, QModelIndex()) // press 0
  1116. << model->index(0, 0, QModelIndex()) << model->index(0, 0, QModelIndex()) // release 0
  1117. << model->index(1, 0, QModelIndex()) << model->index(1, 0, QModelIndex()) // press 1
  1118. << model->index(1, 0, QModelIndex()) << model->index(1, 0, QModelIndex()) // release 1
  1119. << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex()) // press 2
  1120. << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex()) // release 2
  1121. << model->index(3, 0, QModelIndex()) << model->index(3, 0, QModelIndex()) // press 3
  1122. << model->index(3, 0, QModelIndex()) << model->index(3, 0, QModelIndex()) // release 3
  1123. << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex()) // press 2 again
  1124. << model->index(2, 0, QModelIndex()) << model->index(2, 0, QModelIndex());// move 2
  1125. commands << (QItemSelectionModel::NoUpdate) // press 0
  1126. << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows) // release 0
  1127. << (QItemSelectionModel::NoUpdate) // press 1
  1128. << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows) // release 1
  1129. << (QItemSelectionModel::NoUpdate) // press 2
  1130. << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows) // release 2
  1131. << (QItemSelectionModel::NoUpdate) // press 3
  1132. << (QItemSelectionModel::Toggle|QItemSelectionModel::Rows) // release 3
  1133. << (QItemSelectionModel::NoUpdate) // press 2 again
  1134. << (QItemSelectionModel::Toggle/*Current*/|QItemSelectionModel::Rows);// move 2
  1135. expected << model->index(0, 0, QModelIndex())
  1136. << model->index(0, 1, QModelIndex())
  1137. << model->index(0, 2, QModelIndex())
  1138. << model->index(0, 3, QModelIndex())
  1139. << model->index(0, 4, QModelIndex())
  1140. << model->index(1, 0, QModelIndex())
  1141. << model->index(1, 1, QModelIndex())
  1142. << model->index(1, 2, QModelIndex())
  1143. << model->index(1, 3, QModelIndex())
  1144. << model->index(1, 4, QModelIndex())
  1145. /*
  1146. << model->index(2, 0, QModelIndex())
  1147. << model->index(2, 1, QModelIndex())
  1148. << model->index(2, 2, QModelIndex())
  1149. << model->index(2, 3, QModelIndex())
  1150. << model->index(2, 4, QModelIndex())
  1151. */
  1152. << model->index(3, 0, QModelIndex())
  1153. << model->index(3, 1, QModelIndex())
  1154. << model->index(3, 2, QModelIndex())
  1155. << model->index(3, 3, QModelIndex())
  1156. << model->index(3, 4, QModelIndex());
  1157. QTest::newRow("simulated treeview multiselection behavior")
  1158. << indexes
  1159. << true
  1160. << commands
  1161. << expected;
  1162. }
  1163. }
  1164. void tst_QItemSelectionModel::select()
  1165. {
  1166. QFETCH(QModelIndexList, indexList);
  1167. QFETCH(bool, useRanges);
  1168. QFETCH(IntList, commandList);
  1169. QFETCH(QModelIndexList, expectedList);
  1170. int lastCommand = 0;
  1171. // do selections
  1172. for (int i = 0; i<commandList.count(); ++i) {
  1173. if (useRanges) {
  1174. selection->select(QItemSelection(indexList.at(2*i), indexList.at(2*i+1)),
  1175. (QItemSelectionModel::SelectionFlags)commandList.at(i));
  1176. } else {
  1177. selection->select(indexList.at(i),
  1178. (QItemSelectionModel::SelectionFlags)commandList.at(i));
  1179. }
  1180. lastCommand = commandList.at(i);
  1181. }
  1182. QModelIndexList selectedList = selection->selectedIndexes();
  1183. QVERIFY(selection->hasSelection()!=selectedList.isEmpty());
  1184. // debug output
  1185. // for (int i=0; i<selectedList.count(); ++i)
  1186. // qDebug(QString("selected (%1, %2)")
  1187. // .arg(selectedList.at(i).row())
  1188. // .arg(selectedList.at(i).column()));
  1189. // test that the number of indices are as expected
  1190. QVERIFY2(selectedList.count() == expectedList.count(),
  1191. QString("expected indices: %1 actual indices: %2")
  1192. .arg(expectedList.count())
  1193. .arg(selectedList.count()).toLatin1());
  1194. // test existence of each index
  1195. for (int i=0; i<expectedList.count(); ++i) {
  1196. QVERIFY2(selectedList.contains(expectedList.at(i)),
  1197. QString("expected index(%1, %2) not found in selectedIndexes()")
  1198. .arg(expectedList.at(i).row())
  1199. .arg(expectedList.at(i).column()).toLatin1());
  1200. }
  1201. // test that isSelected agrees
  1202. for (int i=0; i<indexList.count(); ++i) {
  1203. QModelIndex idx = indexList.at(i);
  1204. QVERIFY2(selection->isSelected(idx) == selectedList.contains(idx),
  1205. QString("isSelected(index: %1, %2) does not match selectedIndexes()")
  1206. .arg(idx.row())
  1207. .arg(idx.column()).toLatin1());
  1208. }
  1209. //for now we assume Rows/Columns flag is the same for all commands, therefore we just check lastCommand
  1210. // test that isRowSelected agrees
  1211. if (lastCommand & QItemSelectionModel::Rows) {
  1212. for (int i=0; i<selectedList.count(); ++i)
  1213. QVERIFY2(selection->isRowSelected(selectedList.at(i).row(),
  1214. model->parent(selectedList.at(i))),
  1215. QString("isRowSelected(row: %1) does not match selectedIndexes()")
  1216. .arg(selectedList.at(i).row()).toLatin1());
  1217. }
  1218. // test that isColumnSelected agrees
  1219. if (lastC