ctkModelTesterTest2.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QAbstractItemModel>
  16. #include <QCoreApplication>
  17. #include <QStandardItemModel>
  18. #include <QModelIndex>
  19. // CTK includes
  20. #include "ctkModelTester.h"
  21. // STL includes
  22. #include <cstdlib>
  23. #include <iostream>
  24. //-----------------------------------------------------------------------------
  25. class QAbstractItemModelHelper : public QAbstractItemModel
  26. {
  27. public:
  28. virtual QModelIndex index(int, int, const QModelIndex&) const { return QModelIndex(); }
  29. virtual QModelIndex parent(const QModelIndex&) const { return QModelIndex(); }
  30. virtual int rowCount(const QModelIndex&) const { return 0; }
  31. virtual int columnCount(const QModelIndex&) const { return 0; }
  32. virtual QVariant data(const QModelIndex&, int) const { return QVariant(); }
  33. void emitInvalidHeaderDataChanged()
  34. { emit this->headerDataChanged(Qt::Vertical, 10, 10);}
  35. };
  36. //-----------------------------------------------------------------------------
  37. int ctkModelTesterTest2(int argc, char * argv [] )
  38. {
  39. QCoreApplication app(argc, argv);
  40. QObject * object = new QObject;
  41. ctkModelTester ctkTester( object );
  42. //---------------------------------------------------
  43. if ( ctkTester.model() != 0
  44. || ctkTester.throwOnError() != true
  45. || ctkTester.nestedInserts() != false
  46. || ctkTester.testDataEnabled() != true
  47. || ctkTester.verbose() != true)
  48. {
  49. std::cerr << "Line : " << __LINE__
  50. << " - Error in ctkModelTester::ctkModelTester" << std::endl;
  51. return EXIT_FAILURE;
  52. }
  53. ctkTester.setThrowOnError(false);
  54. ctkTester.setNestedInserts(true);
  55. ctkTester.setTestDataEnabled(false);
  56. ctkTester.setVerbose(false);
  57. if ( ctkTester.throwOnError() != false
  58. || ctkTester.nestedInserts() != true
  59. || ctkTester.testDataEnabled() != false
  60. || ctkTester.verbose() != false)
  61. {
  62. std::cerr << "Line : " << __LINE__
  63. << " - Error " << std::endl;
  64. return EXIT_FAILURE;
  65. }
  66. QModelIndex defaultModelIndex;
  67. ctkTester.testModelIndex(defaultModelIndex);
  68. ctkTester.setThrowOnError(true);
  69. ctkTester.setNestedInserts(false);
  70. ctkTester.setTestDataEnabled(true);
  71. ctkTester.setVerbose(true);
  72. ctkTester.testModel();
  73. QAbstractItemModelHelper helper;
  74. ctkTester.setModel(&helper);
  75. bool errorThrown = false;
  76. try
  77. {
  78. helper.emitInvalidHeaderDataChanged();
  79. }
  80. catch (...)
  81. {
  82. errorThrown = true;
  83. }
  84. if (!errorThrown)
  85. {
  86. std::cerr << "ThrowOnError failed" << std::endl;
  87. return EXIT_FAILURE;
  88. }
  89. QStandardItemModel model;
  90. ctkTester.setModel(&model);
  91. ctkTester.setModel(0);
  92. ctkTester.setModel(&model);
  93. ctkTester.testModelIndex(defaultModelIndex);
  94. return EXIT_SUCCESS;
  95. }