Optika GUI Toolik Version of the Day
GUI_UnitTests.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //         Optika: A Tool For Developing Parameter Obtaining GUIs
00005 //                Copyright (2009) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, with Sandia Corporation, the 
00008 // U.S. Government retains certain rights in this software.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Kurtis Nusbaum (klnusbaum@gmail.com) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include <QtGui>
00030 #include <QtTest/QtTest>
00031 #include "Teuchos_UnitTestHarness.hpp"
00032 #include "Optika_treemodel.hpp"
00033 #include "Optika_delegate.hpp"
00034 #include "Optika_treeview.hpp"
00035 #include <QApplication>
00036 #include <QSpinBox>
00037 #include "Optika_metawindow.hpp"
00038 
00039 namespace Optika{
00040 
00041 class ModalClicker : public QThread{
00042 public:
00043   void run();
00044 };
00045 
00046 void ModalClicker::run(){
00047   QWidget* modalDialog = QApplication::activeModalWidget();
00048   while(modalDialog == NULL){
00049     modalDialog = QApplication::activeModalWidget();
00050     msleep(100);
00051   }
00052   QTest::keyClick(QApplication::activeModalWidget(), Qt::Key_Return);
00053 }
00054 
00055 class OptikaGUITests: public QObject{
00056 Q_OBJECT
00057 private slots:
00058   void typeTest();
00059   void dependencyTests();
00060   void arrayEditorTest();
00061   void twoDEditorTest();
00062   void twoDSymmetryTest();
00063   void modelLoadTest();
00064   void validatorApplierTests();
00065   void settingsTest();
00066   void displayRoleTest();
00067   void cleanupTestCase();
00068 private:
00069   static inline QModelIndex getWidgetIndex(const QModelIndex& index);
00070   QObjectCleanupHandler cleaner;
00071   ModalClicker clicker;
00072 };
00073 
00074 void OptikaGUITests::cleanupTestCase(){
00075   cleaner.clear();
00076   if(clicker.isRunning()){
00077     clicker.terminate();
00078     clicker.wait();
00079   }
00080   QVERIFY(!clicker.isRunning());
00081 }
00082   
00083 
00084 
00085 //QModelIndex OptikaGUITests::getEntryIndex(
00086 
00087 #define GET_ENTRY_INDEX(\
00088   PL, \
00089   NAME, \
00090   MODEL) \
00091   RCP<ParameterEntry> NAME##Entry = PL->getEntryRCP( #NAME  ); \
00092   QVERIFY(nonnull( NAME##Entry )); \
00093   QModelIndex NAME##Index = MODEL->findParameterEntryIndex( NAME##Entry ); \
00094   QVERIFY( NAME##Index.isValid());
00095 
00096 #define VERIFY_PARAMETER_TYPE(PL, NAME, TYPE, MODEL) \
00097   GET_ENTRY_INDEX( PL , NAME , MODEL ) \
00098   QCOMPARE( MODEL->data( NAME##Index, Qt::DisplayRole).toString(), \
00099     QString::fromStdString( #NAME) );  \
00100   QModelIndex NAME##TypeIndex = NAME##Index.sibling(NAME##Index.row(),2); \
00101   QVERIFY( NAME##TypeIndex.isValid()); \
00102   QCOMPARE( MODEL->data( NAME##TypeIndex, Qt::DisplayRole).toString(), TYPE );
00103 
00104 
00105 void OptikaGUITests::typeTest(){
00106   cleaner.clear();
00107   RCP<ParameterList> My_List = 
00108     RCP<ParameterList>(new ParameterList);
00109 
00110   RCP<EnhancedNumberValidator<int> > intVali =
00111     Teuchos::rcp(new EnhancedNumberValidator<int>(0,2000,3));
00112 
00113   double *pointer = 0;
00114   Array<double*> doubleStarArray;
00115   Array<int> intArray;
00116   My_List->set("Doublepointer", pointer);
00117   My_List->set(
00118     "MaxIters", 
00119     1550, 
00120     "Determines the maximum number of iterations in the solver",
00121     intVali);
00122   My_List->set(
00123     "Tolerance", 1e-10, "The tolerance used for the convergence check");
00124   My_List->set("DoublePointerArray", doubleStarArray);
00125   My_List->set("IntArray", intArray);
00126   
00127   TreeModel* model = new TreeModel(My_List);
00128   cleaner.add(model);
00129 
00130   VERIFY_PARAMETER_TYPE(My_List, MaxIters, intId, model)
00131   VERIFY_PARAMETER_TYPE(My_List, Doublepointer, unrecognizedId, model)
00132   VERIFY_PARAMETER_TYPE(My_List, Tolerance, doubleId, model)
00133   VERIFY_PARAMETER_TYPE(My_List, DoublePointerArray, unrecognizedId, model)
00134   VERIFY_PARAMETER_TYPE(My_List, IntArray, arrayId + " " + intId, model);
00135   Delegate* delegate = new Delegate;
00136   cleaner.add(delegate);
00137 
00138   QStyleOptionViewItem genericStyleItem;
00139   QModelIndex widgetIndex = getWidgetIndex(MaxItersIndex);
00140   QSpinBox* intSpin = ((QSpinBox*)delegate->createEditor(0, genericStyleItem, widgetIndex));
00141   QCOMPARE(intSpin->maximum(), 2000);
00142   QCOMPARE(intSpin->minimum(),0);
00143   QCOMPARE(intSpin->singleStep(),3);
00144 
00145 
00146   cleaner.remove(model);
00147   cleaner.remove(delegate);
00148   delete model; 
00149   delete delegate;
00150 }
00151 
00152 
00153 void OptikaGUITests::arrayEditorTest(){
00154   Array<double> testArray(4,4.5);
00155   ParameterEntry testEntry(testArray);
00156   DoubleArrayWidget* testWidget = new DoubleArrayWidget("tester", doubleId, null);
00157   cleaner.add(testWidget);
00158 
00159   testWidget->initData(testArray);
00160   Array<double> retrievedArray = testWidget->getArrayFromWidgets();
00161   QVERIFY(testArray == retrievedArray);
00162 
00163   cleaner.remove(testWidget);
00164   delete testWidget;
00165 }
00166 
00167 void OptikaGUITests::twoDEditorTest(){
00168   TwoDArray<double> testArray(4,2,4.5);
00169   ParameterEntry testEntry(testArray);
00170   Double2DArrayWidget* testWidget = 
00171     new Double2DArrayWidget("tester", doubleId, null);
00172   cleaner.add(testWidget);
00173 
00174   testWidget->initData(testArray);
00175   testWidget->accept();
00176   TwoDArray<double> retrievedArray = testWidget->getData();
00177   QVERIFY(testArray == retrievedArray);
00178 
00179 
00180   cleaner.remove(testWidget);
00181   delete testWidget;
00182 
00183 }
00184 
00185 inline QModelIndex OptikaGUITests::getWidgetIndex(const QModelIndex& index){
00186   return index.sibling(index.row(),1);
00187 }
00188   
00189   
00190 #define VERIFY_HIDDEN_ROW(INDEX) \
00191   QVERIFY(treeView->isRowHidden(  INDEX.row(), INDEX.parent()));
00192 
00193 #define VERIFY_SHOWN_ROW(INDEX) \
00194   QVERIFY(!treeView->isRowHidden(  INDEX.row(), INDEX.parent()));
00195 
00196 void OptikaGUITests::dependencyTests(){
00197   cleaner.clear();
00198   RCP<DependencySheet> dependencySheet = rcp(new DependencySheet);
00199   RCP<ParameterList> validParameters = 
00200     getParametersFromXmlFile("deptests.xml", dependencySheet);
00201   TreeModel* model = new TreeModel(validParameters, dependencySheet);
00202   Delegate* delegate = new Delegate;
00203   TreeView* treeView = new TreeView(model, delegate);
00204   cleaner.add(model);
00205   cleaner.add(delegate);
00206   cleaner.add(treeView);
00207   QStyleOptionViewItem genericStyleItem;
00208 
00209   //Assert that the TreeModel has dependencies
00210   QVERIFY(model->hasDependencies());
00211   
00212 //Testing Bool visual dependency
00213   GET_ENTRY_INDEX(validParameters, Preconditioner, model)
00214   GET_ENTRY_INDEX(validParameters, ShowPrecs, model)
00215 
00216   VERIFY_HIDDEN_ROW(PreconditionerIndex)
00217   QModelIndex precWidgetIndex = getWidgetIndex(ShowPrecsIndex);
00218   QComboBox* precBox = (QComboBox*)delegate->createEditor(
00219     0, genericStyleItem, precWidgetIndex);
00220   precBox->setCurrentIndex(precBox->findText(Delegate::getBoolEditorTrue()));
00221   delegate->setModelData(precBox, model, precWidgetIndex);
00222   VERIFY_SHOWN_ROW(PreconditionerIndex)
00223 
00224 
00225 //StringVisualDependency testing
00226   GET_ENTRY_INDEX(validParameters, Favorite_Cheese, model)
00227   GET_ENTRY_INDEX(validParameters, Swiss_rating, model)
00228 
00229   VERIFY_HIDDEN_ROW(Swiss_ratingIndex)
00230   QModelIndex cheeseWidgetIndex = getWidgetIndex(Favorite_CheeseIndex);
00231   QComboBox* cheeseBox = (QComboBox*)delegate->createEditor(
00232     0,genericStyleItem, cheeseWidgetIndex);
00233   cheeseBox->setCurrentIndex(cheeseBox->findText("Swiss"));
00234   delegate->setModelData(cheeseBox, model, cheeseWidgetIndex);
00235   VERIFY_SHOWN_ROW(Swiss_ratingIndex)
00236  
00237 //Testing Number Visual Dependencies
00238   GET_ENTRY_INDEX(validParameters, Temp, model)
00239   GET_ENTRY_INDEX(validParameters, Num_ice_cubes, model)
00240   VERIFY_SHOWN_ROW(Num_ice_cubesIndex)
00241   QModelIndex tempWidgetIndex = getWidgetIndex(TempIndex);
00242   QLineEdit* tempLineEdit = (QLineEdit*)delegate->createEditor(
00243     0,genericStyleItem, tempWidgetIndex);
00244   tempLineEdit->setText("33.0");
00245   delegate->setModelData(tempLineEdit, model, tempWidgetIndex);
00246   VERIFY_HIDDEN_ROW(Num_ice_cubesIndex)
00247 
00248   //Test condition visual dependency
00249   GET_ENTRY_INDEX(validParameters, ParamA, model)
00250   GET_ENTRY_INDEX(validParameters, ParamB, model)
00251   GET_ENTRY_INDEX(validParameters, OptParam, model)
00252   VERIFY_SHOWN_ROW(OptParamIndex)
00253   QModelIndex paramAWidgetIndex = getWidgetIndex(ParamAIndex);
00254   QModelIndex paramBWidgetIndex = getWidgetIndex(ParamBIndex);
00255   QSpinBox* paramASpinner = (QSpinBox*)delegate->createEditor(
00256     0, genericStyleItem, paramAWidgetIndex);
00257   QSpinBox* paramBSpinner = (QSpinBox*)delegate->createEditor(
00258     0, genericStyleItem, paramBWidgetIndex);
00259   paramASpinner->setValue(0);
00260   delegate->setModelData(paramASpinner, model, paramAWidgetIndex);
00261   VERIFY_SHOWN_ROW(OptParamIndex)
00262   paramBSpinner->setValue(0);
00263   delegate->setModelData(paramBSpinner, model, paramBWidgetIndex);
00264   VERIFY_HIDDEN_ROW(OptParamIndex)
00265   paramBSpinner->setValue(1);
00266   delegate->setModelData(paramBSpinner, model, paramBWidgetIndex);
00267   VERIFY_SHOWN_ROW(OptParamIndex)
00268 
00269 
00270   //Test Number Array Length Dependency
00271   GET_ENTRY_INDEX(validParameters, NumBuckets, model)
00272   GET_ENTRY_INDEX(validParameters, AmtInBuckets, model)
00273   Array<double> bucketsArray = model->getArray<double>(AmtInBucketsIndex);
00274   QCOMPARE(bucketsArray.size(),(Array<double>::size_type)3);
00275   QModelIndex numBucketsWidgetIndex = getWidgetIndex(NumBucketsIndex);
00276   QSpinBox* numBucketsSpinner = (QSpinBox*)delegate->createEditor(
00277     0, genericStyleItem, numBucketsWidgetIndex);
00278   numBucketsSpinner->setValue(5);
00279   delegate->setModelData(numBucketsSpinner, model, numBucketsWidgetIndex);
00280   bucketsArray = model->getArray<double>(AmtInBucketsIndex);
00281   QCOMPARE(bucketsArray.size(),(Array<double>::size_type)5);
00282 
00283 
00284   //Testing for Bool ValidatorDependency
00285   GET_ENTRY_INDEX(validParameters, TempConst, model)
00286   GET_ENTRY_INDEX(validParameters, BoolTemp, model)
00287   QVERIFY(nonnull(model->getValidator(BoolTempIndex)));
00288   QModelIndex tempConstWidgetIndex = getWidgetIndex(TempConstIndex);
00289   QModelIndex boolTempWidgetIndex = getWidgetIndex(BoolTempIndex);
00290   QLineEdit* boolTempEdit = (QLineEdit*)delegate->createEditor(
00291     0, genericStyleItem, boolTempWidgetIndex);
00292   QCOMPARE(((QDoubleValidator*)boolTempEdit->validator())->bottom(), 0.0);
00293   QCOMPARE(((QDoubleValidator*)boolTempEdit->validator())->top(), 50.0);
00294   QComboBox* tempConstCombo = (QComboBox*)delegate->createEditor(
00295     0, genericStyleItem, tempConstWidgetIndex);
00296   tempConstCombo->setCurrentIndex(
00297     tempConstCombo->findText(Delegate::getBoolEditorFalse()));
00298   delegate->setModelData(tempConstCombo, model, tempConstWidgetIndex);
00299   QVERIFY(model->getValidator(BoolTempIndex).is_null());
00300   boolTempEdit = (QLineEdit*)delegate->createEditor(
00301     0, genericStyleItem, boolTempWidgetIndex);
00302   QCOMPARE(((QDoubleValidator*)boolTempEdit->validator())->bottom(), EnhancedNumberTraits<double>::min());
00303   QCOMPARE(((QDoubleValidator*)boolTempEdit->validator())->top(), EnhancedNumberTraits<double>::max());
00304 
00305 
00306   //StringValidatorDepenecy tests
00307   GET_ENTRY_INDEX(validParameters, FavFoodType, model)
00308   GET_ENTRY_INDEX(validParameters, FavFood, model)
00309   QVERIFY(nonnull(model->getValidator(FavFoodIndex)));
00310   QModelIndex favFoodWidgetIndex = getWidgetIndex(FavFoodIndex);
00311   QComboBox* favFoodCombo = (QComboBox*)delegate->createEditor(
00312     0, genericStyleItem, favFoodWidgetIndex);
00313   QCOMPARE(favFoodCombo->count(),3);
00314   QVERIFY(favFoodCombo->findText("American") != -1);
00315   QVERIFY(favFoodCombo->findText("Swiss") != -1);
00316   QVERIFY(favFoodCombo->findText("Pepperjack") != -1);
00317 
00318   //Change to chips and verify new valid values
00319   QSignalSpy badValSpy(model, SIGNAL(badValue(QModelIndex, QString)));
00320   QModelIndex favFoodTypeWidgetIndex = getWidgetIndex(FavFoodTypeIndex);
00321   QLineEdit* favFoodTypeEdit = (QLineEdit*)delegate->createEditor(
00322     0, genericStyleItem, favFoodTypeWidgetIndex);
00323   favFoodTypeEdit->setText("Chips");
00324   clicker.start(QThread::IdlePriority);
00325   delegate->setModelData(favFoodTypeEdit, model, favFoodTypeWidgetIndex);
00326   QCOMPARE(badValSpy.count(), 1);
00327   favFoodCombo = (QComboBox*)delegate->createEditor(
00328     0, genericStyleItem, favFoodWidgetIndex);
00329   QCOMPARE(favFoodCombo->count(),3);
00330   QVERIFY(favFoodCombo->findText("Lays") != -1);
00331   QVERIFY(favFoodCombo->findText("Ruffles") != -1);
00332   QVERIFY(favFoodCombo->findText("Pringles") != -1);
00333 
00334   //Change to blah and validate ther validator is now null
00335   favFoodTypeEdit->setText("blah");
00336   delegate->setModelData(favFoodTypeEdit, model, favFoodTypeWidgetIndex);
00337   QVERIFY(model->getValidator(FavFoodIndex).is_null());
00338 
00339   //Change Back to chips and verify that the valid vlues have returned.
00340   favFoodTypeEdit->setText("Chips");
00341   delegate->setModelData(favFoodTypeEdit, model, favFoodTypeWidgetIndex);
00342   favFoodCombo = (QComboBox*)delegate->createEditor(
00343     0, genericStyleItem, favFoodWidgetIndex);
00344   QCOMPARE(favFoodCombo->count(),3);
00345   QVERIFY(favFoodCombo->findText("Lays") != -1);
00346   QVERIFY(favFoodCombo->findText("Ruffles") != -1);
00347   QVERIFY(favFoodCombo->findText("Pringles") != -1);
00348 
00349 
00350   //Test RangeValidatorDependency
00351   GET_ENTRY_INDEX(validParameters, FondTemp, model)
00352   GET_ENTRY_INDEX(validParameters, FondFood, model)
00353   QVERIFY(nonnull(model->getValidator(FondFoodIndex)));
00354   QModelIndex fondFoodWidgetIndex = getWidgetIndex(FondFoodIndex);
00355   QComboBox* fondFoodCombo = (QComboBox*)delegate->createEditor(
00356     0, genericStyleItem, fondFoodWidgetIndex);
00357   QCOMPARE(fondFoodCombo->count(), 2);
00358   QVERIFY(fondFoodCombo->findText("Cheese") != -1);
00359   QVERIFY(fondFoodCombo->findText("Bread") != -1);
00360 
00361   QModelIndex fondTempWidgetIndex = getWidgetIndex(FondTempIndex);
00362   QLineEdit* fondTempEdit = (QLineEdit*)delegate->createEditor(
00363     0, genericStyleItem, fondTempWidgetIndex);
00364   fondTempEdit->setText("120.1");
00365   delegate->setModelData(fondTempEdit, model, fondTempWidgetIndex);
00366   fondFoodCombo = (QComboBox*)delegate->createEditor(
00367     0, genericStyleItem, fondFoodWidgetIndex);
00368   QCOMPARE(fondFoodCombo->count(), 2);
00369   QVERIFY(fondFoodCombo->findText("Chicken") != -1);
00370   QVERIFY(fondFoodCombo->findText("Beef") != -1);
00371 
00372   fondTempEdit->setText("180.1");
00373   delegate->setModelData(fondTempEdit, model, fondTempWidgetIndex);
00374   QVERIFY(model->getValidator(FondFoodIndex).is_null());
00375   QLineEdit* fondFoodLineEdit = (QLineEdit*)delegate->createEditor(
00376     0, genericStyleItem, fondFoodWidgetIndex);
00377   QVERIFY(fondFoodLineEdit != NULL);
00378   
00379 
00380   fondTempEdit->setText("90");
00381   delegate->setModelData(fondTempEdit, model, fondTempWidgetIndex);
00382   fondFoodCombo = (QComboBox*)delegate->createEditor(
00383     0, genericStyleItem, fondFoodWidgetIndex);
00384   QCOMPARE(fondFoodCombo->count(), 2);
00385   QVERIFY(fondFoodCombo->findText("Cheese") != -1);
00386   QVERIFY(fondFoodCombo->findText("Bread") != -1);
00387 
00388 
00389   //Test TwoDRowDependency
00390   GET_ENTRY_INDEX(validParameters, NumRows, model)
00391   GET_ENTRY_INDEX(validParameters, RowArray, model)
00392   QModelIndex numRowsWidgetIndex = getWidgetIndex(NumRowsIndex);
00393   QSpinBox* numRowSpin = (QSpinBox*)delegate->createEditor(
00394     0, genericStyleItem, numRowsWidgetIndex);
00395   numRowSpin->setValue(2);
00396   delegate->setModelData(numRowSpin, model, numRowsWidgetIndex);
00397   TwoDArray<double> rowArray = model->getTwoDArray<double>(RowArrayIndex);
00398   QCOMPARE(rowArray.getNumRows(), (TwoDArray<double>::size_type)2);
00399 
00400   //Test TwoDColDependency
00401   GET_ENTRY_INDEX(validParameters, NumCols, model)
00402   GET_ENTRY_INDEX(validParameters, ColArray, model)
00403   QModelIndex numColsWidgetIndex = getWidgetIndex(NumColsIndex);
00404   QSpinBox* numColSpin = (QSpinBox*)delegate->createEditor(
00405     0, genericStyleItem, numColsWidgetIndex);
00406   numColSpin->setValue(2);
00407   delegate->setModelData(numColSpin, model, numColsWidgetIndex);
00408   TwoDArray<double> colArray = model->getTwoDArray<double>(ColArrayIndex);
00409   QCOMPARE(colArray.getNumCols(), (TwoDArray<double>::size_type)2);
00410  
00411 
00412   
00413 
00414   cleaner.remove(model);
00415   cleaner.remove(treeView);
00416   cleaner.remove(delegate);
00417   delete model;
00418   delete treeView;
00419   delete delegate;
00420 }
00421   
00422 
00423 void OptikaGUITests::twoDSymmetryTest(){
00424   cleaner.clear();
00425   TwoDArray<double> testArray(4,4,4.5);
00426   testArray.setSymmetrical(true);
00427   Double2DArrayWidget* testWidget = 
00428     new Double2DArrayWidget("tester", doubleId, null);
00429   cleaner.add(testWidget);
00430 
00431   testWidget->initData(testArray);
00432   QGridLayout* layout = (QGridLayout*)testWidget->layout();
00433   QScrollArea* scrollArea = (QScrollArea*)(layout->itemAtPosition(0,0)->widget());
00434   QWidget* actualWidget = scrollArea->widget();
00435   QCOMPARE(((QGridLayout*)actualWidget->layout())->itemAtPosition(1,1), (QLayoutItem*)0);
00436 
00437   testWidget->accept();
00438   TwoDArray<double> retrievedArray = testWidget->getData();
00439   QVERIFY(testArray == retrievedArray);
00440 
00441 
00442   cleaner.remove(testWidget);
00443   delete testWidget;
00444 
00445 
00446 }
00447 
00448 void OptikaGUITests::modelLoadTest(){
00449   cleaner.clear();
00450   RCP<ParameterList> validParameters = 
00451     getParametersFromXmlFile("loadtest.xml");
00452   TreeModel* model = new TreeModel(validParameters);
00453   Delegate* delegate = new Delegate;
00454   TreeView* treeView = new TreeView(model, delegate);
00455   cleaner.add(model);
00456   cleaner.add(delegate);
00457   cleaner.add(treeView);
00458 
00459   QCOMPARE(model->getCurrentParameters()->get<int>("Steve"), 4);
00460   QCOMPARE(model->getCurrentParameters()->get<int>("Sam"), 4);
00461   QCOMPARE(model->getCurrentParameters()->sublist("Prec").get<int>("Sam"), 9);
00462   QCOMPARE(model->getCurrentParameters()->sublist("Prec").get<int>("Blah"), 1);
00463   model->readInput("loadtest.in.xml");
00464   QCOMPARE(model->getCurrentParameters()->get<int>("Steve"), 0);
00465   QCOMPARE(model->getCurrentParameters()->sublist("Prec").get<int>("Blah"), 80);
00466   QCOMPARE(model->getCurrentParameters()->sublist("Prec").get<int>("Sam"), 99);
00467   QCOMPARE(model->getCurrentParameters()->get<int>("Sam"), 50);
00468 
00469   cleaner.remove(model);
00470   cleaner.remove(treeView);
00471   cleaner.remove(delegate);
00472   delete model;
00473   delete treeView;
00474   delete delegate;
00475 
00476 }
00477 
00478 template<class T>
00479 void testingSpinBoxApply(
00480   const RCP<EnhancedNumberValidator<T> > validator,
00481   QAbstractSpinBox* spinner)
00482 {
00483   ValidatorApplier<T>::applyToSpinBox(validator, (QSpinBox*)spinner);
00484 }
00485 
00486 template<>
00487 void testingSpinBoxApply(
00488   const RCP<EnhancedNumberValidator<double> > validator,
00489   QAbstractSpinBox* spinner)
00490 {
00491   ValidatorApplier<double>::applyToSpinBox(validator, (QDoubleSpinBox*)spinner);
00492 }
00493 
00494 template<>
00495 void testingSpinBoxApply(
00496   const RCP<EnhancedNumberValidator<float> > validator,
00497   QAbstractSpinBox* spinner)
00498 {
00499   ValidatorApplier<float>::applyToSpinBox(validator, (QDoubleSpinBox*)spinner);
00500 }
00501 
00502 template<class T>
00503 void valApplyTestTemplate(){
00504   EnhancedNumberValidator<T> validator(Teuchos::as<T>(0), Teuchos::as<T>(10));
00505   RCP<EnhancedNumberValidator<T> > valPtr = rcpFromRef(validator);
00506   std::string myType = Teuchos::TypeNameTraits<T>::name();
00507   bool isFloatingType = 
00508     myType == Teuchos::TypeNameTraits<double>::name() ||
00509     myType == Teuchos::TypeNameTraits<float>::name();
00510 
00511   QString val10("10");
00512   QString val20("20");
00513   QString val5("5");
00514   QString val0("0");
00515   QString valneg1("-1");
00516 
00517 
00519   QAbstractSpinBox* spinner = NULL;
00520   if(isFloatingType)
00521   {
00522     spinner = new QDoubleSpinBox();
00523   }
00524   else{
00525     spinner = new QSpinBox();
00526   }
00527   testingSpinBoxApply(valPtr, spinner);
00528   int pos=0;
00529   QCOMPARE(spinner->validate(val10,pos), QValidator::Acceptable);
00530   QCOMPARE(spinner->validate(val20,pos), QValidator::Invalid);
00531   QCOMPARE(spinner->validate(val5,pos), QValidator::Acceptable);
00532   QCOMPARE(spinner->validate(val0,pos), QValidator::Acceptable);
00533   QCOMPARE(spinner->validate(valneg1,pos), QValidator::Invalid);
00534   if(isFloatingType){
00535     QDoubleSpinBox* actualSpinner = (QDoubleSpinBox*)spinner;
00536     QCOMPARE(actualSpinner->decimals(), Teuchos::as<int>(EnhancedNumberTraits<T>::defaultPrecision()));
00537     QCOMPARE(Teuchos::as<T>(actualSpinner->singleStep()), EnhancedNumberTraits<T>::defaultStep());
00538   }
00539   if(isFloatingType){
00540     QSpinBox* actualSpinner = (QSpinBox*)spinner;
00541     QCOMPARE(Teuchos::as<T>(actualSpinner->singleStep()), EnhancedNumberTraits<T>::defaultStep());
00542   }
00543   delete spinner;
00544 
00546   QLineEdit lineEdit;
00547   ValidatorApplier<T>::applyToLineEdit(valPtr, &lineEdit);
00548   const QValidator* appliedValidator = lineEdit.validator();
00549   QCOMPARE(appliedValidator->validate(val10,pos), QValidator::Acceptable);
00550   QCOMPARE(appliedValidator->validate(val5,pos), QValidator::Acceptable);
00551   QCOMPARE(appliedValidator->validate(val0,pos), QValidator::Acceptable);
00552   if(isFloatingType){
00553     QCOMPARE(appliedValidator->validate(val20,pos), QValidator::Intermediate);
00554     QCOMPARE(appliedValidator->validate(valneg1,pos), QValidator::Invalid);
00555     const QDoubleValidator* doubleValidator = (QDoubleValidator*)appliedValidator;
00556     QCOMPARE(doubleValidator->decimals(), Teuchos::as<int>(EnhancedNumberTraits<T>::defaultPrecision()));
00557   }
00558   else{
00559     QCOMPARE(appliedValidator->validate(val20,pos), QValidator::Invalid);
00560     QCOMPARE(appliedValidator->validate(valneg1,pos), QValidator::Invalid);
00561   }
00562 }
00563 
00564 void OptikaGUITests::validatorApplierTests(){
00565   valApplyTestTemplate<int>();
00566   valApplyTestTemplate<short>();
00567   valApplyTestTemplate<double>();
00568   valApplyTestTemplate<float>();
00569 }
00570 
00571 void OptikaGUITests::settingsTest(){
00572   RCP<ParameterList> validParameters = rcp(new ParameterList("Steve"));
00573   validParameters->set("Don't care", "don't care");
00574   MetaWindow* m1 = new MetaWindow(validParameters);
00575   m1->move(30,99);
00576   m1->resize(673,823);
00577   delete m1;
00578   MetaWindow* m2 = new MetaWindow(validParameters);
00579   QCOMPARE(m2->width(),673);
00580   QCOMPARE(m2->height(),823);
00581   QCOMPARE(m2->x(),30);
00582   QCOMPARE(m2->y(),99);
00583   delete m2;
00584 }
00585 
00586 void OptikaGUITests::displayRoleTest(){
00587   cleaner.clear();
00588   RCP<ParameterList> validParameters = 
00589     getParametersFromXmlFile("loadtest.xml");
00590   validParameters->set("2dtest", Teuchos::TwoDArray<int>(2,2,4));
00591   TreeModel* model = new TreeModel(validParameters);
00592   Delegate* delegate = new Delegate;
00593   TreeView* treeView = new TreeView(model, delegate);
00594   cleaner.add(model);
00595   cleaner.add(delegate);
00596   cleaner.add(treeView);
00597   RCP<const ParameterList> parameters =  model->getCurrentParameters();
00598 
00599   RCP<const ParameterEntry> steveParameter = parameters->getEntryRCP("Steve");
00600   QModelIndex steveIndex = model->findParameterEntryIndex(steveParameter);
00601   QVariant data = model->data(
00602     steveIndex.sibling(steveIndex.row(), 1), Qt::DisplayRole);
00603   QCOMPARE(data.toString(), QString("4"));
00604 
00605   RCP<const ParameterEntry> twodParameter = parameters->getEntryRCP("2dtest");
00606   QModelIndex twodIndex = model->findParameterEntryIndex(twodParameter);
00607   data = model->data(
00608     twodIndex.sibling(twodIndex.row(), 1), Qt::DisplayRole);
00609   QCOMPARE(data.toString(), QString("Click to view 2D Array"));
00610 
00611   RCP<const ParameterEntry> listParameter = parameters->getEntryRCP("Prec");
00612   QModelIndex listIndex = model->findParameterEntryIndex(listParameter);
00613   data = model->data(
00614     listIndex.sibling(listIndex.row(), 1), Qt::DisplayRole);
00615   QCOMPARE(data.toString(), QString(""));
00616 
00617   cleaner.remove(model);
00618   cleaner.remove(treeView);
00619   cleaner.remove(delegate);
00620   delete model;
00621   delete treeView;
00622   delete delegate;
00623 
00624 
00625 
00626 }
00627 
00628 } //namespace Optika
00629 
00630 QTEST_MAIN(Optika::OptikaGUITests)
00631 #include "GUI_UnitTests.moc"
00632 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines