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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kurtis Nusbaum (klnusbaum@gmail.com) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include <QtGui>
00043 #include <QtTest/QtTest>
00044 #include "Teuchos_UnitTestHarness.hpp"
00045 #include "Optika_treemodel.hpp"
00046 #include "Optika_delegate.hpp"
00047 #include "Optika_treeview.hpp"
00048 #include <QApplication>
00049 #include <QSpinBox>
00050 #include "Optika_metawindow.hpp"
00051 
00052 namespace Optika{
00053 
00054 class ModalClicker : public QThread{
00055 public:
00056   void run();
00057 };
00058 
00059 void ModalClicker::run(){
00060   QWidget* modalDialog = QApplication::activeModalWidget();
00061   while(modalDialog == NULL){
00062     modalDialog = QApplication::activeModalWidget();
00063     msleep(100);
00064   }
00065   QTest::keyClick(QApplication::activeModalWidget(), Qt::Key_Return);
00066 }
00067 
00068 class OptikaGUITests: public QObject{
00069 Q_OBJECT
00070 private slots:
00071   void typeTest();
00072   void dependencyTests();
00073   void arrayEditorTest();
00074   void twoDEditorTest();
00075   void twoDSymmetryTest();
00076   void modelLoadTest();
00077   void validatorApplierTests();
00078   void settingsTest();
00079   void displayRoleTest();
00080   void cleanupTestCase();
00081 private:
00082   static inline QModelIndex getWidgetIndex(const QModelIndex& index);
00083   QObjectCleanupHandler cleaner;
00084   ModalClicker clicker;
00085 };
00086 
00087 void OptikaGUITests::cleanupTestCase(){
00088   cleaner.clear();
00089   if(clicker.isRunning()){
00090     clicker.terminate();
00091     clicker.wait();
00092   }
00093   QVERIFY(!clicker.isRunning());
00094 }
00095   
00096 
00097 
00098 //QModelIndex OptikaGUITests::getEntryIndex(
00099 
00100 #define GET_ENTRY_INDEX(\
00101   PL, \
00102   NAME, \
00103   MODEL) \
00104   RCP<ParameterEntry> NAME##Entry = PL->getEntryRCP( #NAME  ); \
00105   QVERIFY(nonnull( NAME##Entry )); \
00106   QModelIndex NAME##Index = MODEL->findParameterEntryIndex( NAME##Entry ); \
00107   QVERIFY( NAME##Index.isValid());
00108 
00109 #define VERIFY_PARAMETER_TYPE(PL, NAME, TYPE, MODEL) \
00110   GET_ENTRY_INDEX( PL , NAME , MODEL ) \
00111   QCOMPARE( MODEL->data( NAME##Index, Qt::DisplayRole).toString(), \
00112     QString::fromStdString( #NAME) );  \
00113   QModelIndex NAME##TypeIndex = NAME##Index.sibling(NAME##Index.row(),2); \
00114   QVERIFY( NAME##TypeIndex.isValid()); \
00115   QCOMPARE( MODEL->data( NAME##TypeIndex, Qt::DisplayRole).toString(), TYPE );
00116 
00117 
00118 void OptikaGUITests::typeTest(){
00119   cleaner.clear();
00120   RCP<ParameterList> My_List = 
00121     RCP<ParameterList>(new ParameterList);
00122 
00123   RCP<EnhancedNumberValidator<int> > intVali =
00124     Teuchos::rcp(new EnhancedNumberValidator<int>(0,2000,3));
00125 
00126   double *pointer = 0;
00127   Array<double*> doubleStarArray;
00128   Array<int> intArray;
00129   My_List->set("Doublepointer", pointer);
00130   My_List->set(
00131     "MaxIters", 
00132     1550, 
00133     "Determines the maximum number of iterations in the solver",
00134     intVali);
00135   My_List->set(
00136     "Tolerance", 1e-10, "The tolerance used for the convergence check");
00137   My_List->set("DoublePointerArray", doubleStarArray);
00138   My_List->set("IntArray", intArray);
00139   
00140   TreeModel* model = new TreeModel(My_List);
00141   cleaner.add(model);
00142 
00143   VERIFY_PARAMETER_TYPE(My_List, MaxIters, intId, model)
00144   VERIFY_PARAMETER_TYPE(My_List, Doublepointer, unrecognizedId, model)
00145   VERIFY_PARAMETER_TYPE(My_List, Tolerance, doubleId, model)
00146   VERIFY_PARAMETER_TYPE(My_List, DoublePointerArray, unrecognizedId, model)
00147   VERIFY_PARAMETER_TYPE(My_List, IntArray, arrayId + " " + intId, model);
00148   Delegate* delegate = new Delegate;
00149   cleaner.add(delegate);
00150 
00151   QStyleOptionViewItem genericStyleItem;
00152   QModelIndex widgetIndex = getWidgetIndex(MaxItersIndex);
00153   QSpinBox* intSpin = ((QSpinBox*)delegate->createEditor(0, genericStyleItem, widgetIndex));
00154   QCOMPARE(intSpin->maximum(), 2000);
00155   QCOMPARE(intSpin->minimum(),0);
00156   QCOMPARE(intSpin->singleStep(),3);
00157 
00158 
00159   cleaner.remove(model);
00160   cleaner.remove(delegate);
00161   delete model; 
00162   delete delegate;
00163 }
00164 
00165 
00166 void OptikaGUITests::arrayEditorTest(){
00167   Array<double> testArray(4,4.5);
00168   ParameterEntry testEntry(testArray);
00169   DoubleArrayWidget* testWidget = new DoubleArrayWidget("tester", doubleId, null);
00170   cleaner.add(testWidget);
00171 
00172   testWidget->initData(testArray);
00173   Array<double> retrievedArray = testWidget->getArrayFromWidgets();
00174   QVERIFY(testArray == retrievedArray);
00175 
00176   cleaner.remove(testWidget);
00177   delete testWidget;
00178 }
00179 
00180 void OptikaGUITests::twoDEditorTest(){
00181   TwoDArray<double> testArray(4,2,4.5);
00182   ParameterEntry testEntry(testArray);
00183   Double2DArrayWidget* testWidget = 
00184     new Double2DArrayWidget("tester", doubleId, null);
00185   cleaner.add(testWidget);
00186 
00187   testWidget->initData(testArray);
00188   testWidget->accept();
00189   TwoDArray<double> retrievedArray = testWidget->getData();
00190   QVERIFY(testArray == retrievedArray);
00191 
00192 
00193   cleaner.remove(testWidget);
00194   delete testWidget;
00195 
00196 }
00197 
00198 inline QModelIndex OptikaGUITests::getWidgetIndex(const QModelIndex& index){
00199   return index.sibling(index.row(),1);
00200 }
00201   
00202   
00203 #define VERIFY_HIDDEN_ROW(INDEX) \
00204   QVERIFY(treeView->isRowHidden(  INDEX.row(), INDEX.parent()));
00205 
00206 #define VERIFY_SHOWN_ROW(INDEX) \
00207   QVERIFY(!treeView->isRowHidden(  INDEX.row(), INDEX.parent()));
00208 
00209 void OptikaGUITests::dependencyTests(){
00210   cleaner.clear();
00211   RCP<DependencySheet> dependencySheet = rcp(new DependencySheet);
00212   RCP<ParameterList> validParameters = 
00213     getParametersFromXmlFile("deptests.xml", dependencySheet);
00214   TreeModel* model = new TreeModel(validParameters, dependencySheet);
00215   Delegate* delegate = new Delegate;
00216   TreeView* treeView = new TreeView(model, delegate);
00217   cleaner.add(model);
00218   cleaner.add(delegate);
00219   cleaner.add(treeView);
00220   QStyleOptionViewItem genericStyleItem;
00221 
00222   //Assert that the TreeModel has dependencies
00223   QVERIFY(model->hasDependencies());
00224   
00225 //Testing Bool visual dependency
00226   GET_ENTRY_INDEX(validParameters, Preconditioner, model)
00227   GET_ENTRY_INDEX(validParameters, ShowPrecs, model)
00228 
00229   VERIFY_HIDDEN_ROW(PreconditionerIndex)
00230   QModelIndex precWidgetIndex = getWidgetIndex(ShowPrecsIndex);
00231   QComboBox* precBox = (QComboBox*)delegate->createEditor(
00232     0, genericStyleItem, precWidgetIndex);
00233   precBox->setCurrentIndex(precBox->findText(Delegate::getBoolEditorTrue()));
00234   delegate->setModelData(precBox, model, precWidgetIndex);
00235   VERIFY_SHOWN_ROW(PreconditionerIndex)
00236 
00237 
00238 //StringVisualDependency testing
00239   GET_ENTRY_INDEX(validParameters, Favorite_Cheese, model)
00240   GET_ENTRY_INDEX(validParameters, Swiss_rating, model)
00241 
00242   VERIFY_HIDDEN_ROW(Swiss_ratingIndex)
00243   QModelIndex cheeseWidgetIndex = getWidgetIndex(Favorite_CheeseIndex);
00244   QComboBox* cheeseBox = (QComboBox*)delegate->createEditor(
00245     0,genericStyleItem, cheeseWidgetIndex);
00246   cheeseBox->setCurrentIndex(cheeseBox->findText("Swiss"));
00247   delegate->setModelData(cheeseBox, model, cheeseWidgetIndex);
00248   VERIFY_SHOWN_ROW(Swiss_ratingIndex)
00249  
00250 //Testing Number Visual Dependencies
00251   GET_ENTRY_INDEX(validParameters, Temp, model)
00252   GET_ENTRY_INDEX(validParameters, Num_ice_cubes, model)
00253   VERIFY_SHOWN_ROW(Num_ice_cubesIndex)
00254   QModelIndex tempWidgetIndex = getWidgetIndex(TempIndex);
00255   QLineEdit* tempLineEdit = (QLineEdit*)delegate->createEditor(
00256     0,genericStyleItem, tempWidgetIndex);
00257   tempLineEdit->setText("33.0");
00258   delegate->setModelData(tempLineEdit, model, tempWidgetIndex);
00259   VERIFY_HIDDEN_ROW(Num_ice_cubesIndex)
00260 
00261   //Test condition visual dependency
00262   GET_ENTRY_INDEX(validParameters, ParamA, model)
00263   GET_ENTRY_INDEX(validParameters, ParamB, model)
00264   GET_ENTRY_INDEX(validParameters, OptParam, model)
00265   VERIFY_SHOWN_ROW(OptParamIndex)
00266   QModelIndex paramAWidgetIndex = getWidgetIndex(ParamAIndex);
00267   QModelIndex paramBWidgetIndex = getWidgetIndex(ParamBIndex);
00268   QSpinBox* paramASpinner = (QSpinBox*)delegate->createEditor(
00269     0, genericStyleItem, paramAWidgetIndex);
00270   QSpinBox* paramBSpinner = (QSpinBox*)delegate->createEditor(
00271     0, genericStyleItem, paramBWidgetIndex);
00272   paramASpinner->setValue(0);
00273   delegate->setModelData(paramASpinner, model, paramAWidgetIndex);
00274   VERIFY_SHOWN_ROW(OptParamIndex)
00275   paramBSpinner->setValue(0);
00276   delegate->setModelData(paramBSpinner, model, paramBWidgetIndex);
00277   VERIFY_HIDDEN_ROW(OptParamIndex)
00278   paramBSpinner->setValue(1);
00279   delegate->setModelData(paramBSpinner, model, paramBWidgetIndex);
00280   VERIFY_SHOWN_ROW(OptParamIndex)
00281 
00282 
00283   //Test Number Array Length Dependency
00284   GET_ENTRY_INDEX(validParameters, NumBuckets, model)
00285   GET_ENTRY_INDEX(validParameters, AmtInBuckets, model)
00286   Array<double> bucketsArray = model->getArray<double>(AmtInBucketsIndex);
00287   QCOMPARE(bucketsArray.size(),(Array<double>::size_type)3);
00288   QModelIndex numBucketsWidgetIndex = getWidgetIndex(NumBucketsIndex);
00289   QSpinBox* numBucketsSpinner = (QSpinBox*)delegate->createEditor(
00290     0, genericStyleItem, numBucketsWidgetIndex);
00291   numBucketsSpinner->setValue(5);
00292   delegate->setModelData(numBucketsSpinner, model, numBucketsWidgetIndex);
00293   bucketsArray = model->getArray<double>(AmtInBucketsIndex);
00294   QCOMPARE(bucketsArray.size(),(Array<double>::size_type)5);
00295 
00296 
00297   //Testing for Bool ValidatorDependency
00298   GET_ENTRY_INDEX(validParameters, TempConst, model)
00299   GET_ENTRY_INDEX(validParameters, BoolTemp, model)
00300   QVERIFY(nonnull(model->getValidator(BoolTempIndex)));
00301   QModelIndex tempConstWidgetIndex = getWidgetIndex(TempConstIndex);
00302   QModelIndex boolTempWidgetIndex = getWidgetIndex(BoolTempIndex);
00303   QLineEdit* boolTempEdit = (QLineEdit*)delegate->createEditor(
00304     0, genericStyleItem, boolTempWidgetIndex);
00305   QCOMPARE(((QDoubleValidator*)boolTempEdit->validator())->bottom(), 0.0);
00306   QCOMPARE(((QDoubleValidator*)boolTempEdit->validator())->top(), 50.0);
00307   QComboBox* tempConstCombo = (QComboBox*)delegate->createEditor(
00308     0, genericStyleItem, tempConstWidgetIndex);
00309   tempConstCombo->setCurrentIndex(
00310     tempConstCombo->findText(Delegate::getBoolEditorFalse()));
00311   delegate->setModelData(tempConstCombo, model, tempConstWidgetIndex);
00312   QVERIFY(model->getValidator(BoolTempIndex).is_null());
00313   boolTempEdit = (QLineEdit*)delegate->createEditor(
00314     0, genericStyleItem, boolTempWidgetIndex);
00315   QCOMPARE(((QDoubleValidator*)boolTempEdit->validator())->bottom(), EnhancedNumberTraits<double>::min());
00316   QCOMPARE(((QDoubleValidator*)boolTempEdit->validator())->top(), EnhancedNumberTraits<double>::max());
00317 
00318 
00319   //StringValidatorDepenecy tests
00320   GET_ENTRY_INDEX(validParameters, FavFoodType, model)
00321   GET_ENTRY_INDEX(validParameters, FavFood, model)
00322   QVERIFY(nonnull(model->getValidator(FavFoodIndex)));
00323   QModelIndex favFoodWidgetIndex = getWidgetIndex(FavFoodIndex);
00324   QComboBox* favFoodCombo = (QComboBox*)delegate->createEditor(
00325     0, genericStyleItem, favFoodWidgetIndex);
00326   QCOMPARE(favFoodCombo->count(),3);
00327   QVERIFY(favFoodCombo->findText("American") != -1);
00328   QVERIFY(favFoodCombo->findText("Swiss") != -1);
00329   QVERIFY(favFoodCombo->findText("Pepperjack") != -1);
00330 
00331   //Change to chips and verify new valid values
00332   QSignalSpy badValSpy(model, SIGNAL(badValue(QModelIndex, QString)));
00333   QModelIndex favFoodTypeWidgetIndex = getWidgetIndex(FavFoodTypeIndex);
00334   QLineEdit* favFoodTypeEdit = (QLineEdit*)delegate->createEditor(
00335     0, genericStyleItem, favFoodTypeWidgetIndex);
00336   favFoodTypeEdit->setText("Chips");
00337   clicker.start(QThread::IdlePriority);
00338   delegate->setModelData(favFoodTypeEdit, model, favFoodTypeWidgetIndex);
00339   QCOMPARE(badValSpy.count(), 1);
00340   favFoodCombo = (QComboBox*)delegate->createEditor(
00341     0, genericStyleItem, favFoodWidgetIndex);
00342   QCOMPARE(favFoodCombo->count(),3);
00343   QVERIFY(favFoodCombo->findText("Lays") != -1);
00344   QVERIFY(favFoodCombo->findText("Ruffles") != -1);
00345   QVERIFY(favFoodCombo->findText("Pringles") != -1);
00346 
00347   //Change to blah and validate ther validator is now null
00348   favFoodTypeEdit->setText("blah");
00349   delegate->setModelData(favFoodTypeEdit, model, favFoodTypeWidgetIndex);
00350   QVERIFY(model->getValidator(FavFoodIndex).is_null());
00351 
00352   //Change Back to chips and verify that the valid vlues have returned.
00353   favFoodTypeEdit->setText("Chips");
00354   delegate->setModelData(favFoodTypeEdit, model, favFoodTypeWidgetIndex);
00355   favFoodCombo = (QComboBox*)delegate->createEditor(
00356     0, genericStyleItem, favFoodWidgetIndex);
00357   QCOMPARE(favFoodCombo->count(),3);
00358   QVERIFY(favFoodCombo->findText("Lays") != -1);
00359   QVERIFY(favFoodCombo->findText("Ruffles") != -1);
00360   QVERIFY(favFoodCombo->findText("Pringles") != -1);
00361 
00362 
00363   //Test RangeValidatorDependency
00364   GET_ENTRY_INDEX(validParameters, FondTemp, model)
00365   GET_ENTRY_INDEX(validParameters, FondFood, model)
00366   QVERIFY(nonnull(model->getValidator(FondFoodIndex)));
00367   QModelIndex fondFoodWidgetIndex = getWidgetIndex(FondFoodIndex);
00368   QComboBox* fondFoodCombo = (QComboBox*)delegate->createEditor(
00369     0, genericStyleItem, fondFoodWidgetIndex);
00370   QCOMPARE(fondFoodCombo->count(), 2);
00371   QVERIFY(fondFoodCombo->findText("Cheese") != -1);
00372   QVERIFY(fondFoodCombo->findText("Bread") != -1);
00373 
00374   QModelIndex fondTempWidgetIndex = getWidgetIndex(FondTempIndex);
00375   QLineEdit* fondTempEdit = (QLineEdit*)delegate->createEditor(
00376     0, genericStyleItem, fondTempWidgetIndex);
00377   fondTempEdit->setText("120.1");
00378   delegate->setModelData(fondTempEdit, model, fondTempWidgetIndex);
00379   fondFoodCombo = (QComboBox*)delegate->createEditor(
00380     0, genericStyleItem, fondFoodWidgetIndex);
00381   QCOMPARE(fondFoodCombo->count(), 2);
00382   QVERIFY(fondFoodCombo->findText("Chicken") != -1);
00383   QVERIFY(fondFoodCombo->findText("Beef") != -1);
00384 
00385   fondTempEdit->setText("180.1");
00386   delegate->setModelData(fondTempEdit, model, fondTempWidgetIndex);
00387   QVERIFY(model->getValidator(FondFoodIndex).is_null());
00388   QLineEdit* fondFoodLineEdit = (QLineEdit*)delegate->createEditor(
00389     0, genericStyleItem, fondFoodWidgetIndex);
00390   QVERIFY(fondFoodLineEdit != NULL);
00391   
00392 
00393   fondTempEdit->setText("90");
00394   delegate->setModelData(fondTempEdit, model, fondTempWidgetIndex);
00395   fondFoodCombo = (QComboBox*)delegate->createEditor(
00396     0, genericStyleItem, fondFoodWidgetIndex);
00397   QCOMPARE(fondFoodCombo->count(), 2);
00398   QVERIFY(fondFoodCombo->findText("Cheese") != -1);
00399   QVERIFY(fondFoodCombo->findText("Bread") != -1);
00400 
00401 
00402   //Test TwoDRowDependency
00403   GET_ENTRY_INDEX(validParameters, NumRows, model)
00404   GET_ENTRY_INDEX(validParameters, RowArray, model)
00405   QModelIndex numRowsWidgetIndex = getWidgetIndex(NumRowsIndex);
00406   QSpinBox* numRowSpin = (QSpinBox*)delegate->createEditor(
00407     0, genericStyleItem, numRowsWidgetIndex);
00408   numRowSpin->setValue(2);
00409   delegate->setModelData(numRowSpin, model, numRowsWidgetIndex);
00410   TwoDArray<double> rowArray = model->getTwoDArray<double>(RowArrayIndex);
00411   QCOMPARE(rowArray.getNumRows(), (TwoDArray<double>::size_type)2);
00412 
00413   //Test TwoDColDependency
00414   GET_ENTRY_INDEX(validParameters, NumCols, model)
00415   GET_ENTRY_INDEX(validParameters, ColArray, model)
00416   QModelIndex numColsWidgetIndex = getWidgetIndex(NumColsIndex);
00417   QSpinBox* numColSpin = (QSpinBox*)delegate->createEditor(
00418     0, genericStyleItem, numColsWidgetIndex);
00419   numColSpin->setValue(2);
00420   delegate->setModelData(numColSpin, model, numColsWidgetIndex);
00421   TwoDArray<double> colArray = model->getTwoDArray<double>(ColArrayIndex);
00422   QCOMPARE(colArray.getNumCols(), (TwoDArray<double>::size_type)2);
00423  
00424 
00425   
00426 
00427   cleaner.remove(model);
00428   cleaner.remove(treeView);
00429   cleaner.remove(delegate);
00430   delete model;
00431   delete treeView;
00432   delete delegate;
00433 }
00434   
00435 
00436 void OptikaGUITests::twoDSymmetryTest(){
00437   cleaner.clear();
00438   TwoDArray<double> testArray(4,4,4.5);
00439   testArray.setSymmetrical(true);
00440   Double2DArrayWidget* testWidget = 
00441     new Double2DArrayWidget("tester", doubleId, null);
00442   cleaner.add(testWidget);
00443 
00444   testWidget->initData(testArray);
00445   QGridLayout* layout = (QGridLayout*)testWidget->layout();
00446   QScrollArea* scrollArea = (QScrollArea*)(layout->itemAtPosition(0,0)->widget());
00447   QWidget* actualWidget = scrollArea->widget();
00448   QCOMPARE(((QGridLayout*)actualWidget->layout())->itemAtPosition(1,1), (QLayoutItem*)0);
00449 
00450   testWidget->accept();
00451   TwoDArray<double> retrievedArray = testWidget->getData();
00452   QVERIFY(testArray == retrievedArray);
00453 
00454 
00455   cleaner.remove(testWidget);
00456   delete testWidget;
00457 
00458 
00459 }
00460 
00461 void OptikaGUITests::modelLoadTest(){
00462   cleaner.clear();
00463   RCP<ParameterList> validParameters = 
00464     getParametersFromXmlFile("loadtest.xml");
00465   TreeModel* model = new TreeModel(validParameters);
00466   Delegate* delegate = new Delegate;
00467   TreeView* treeView = new TreeView(model, delegate);
00468   cleaner.add(model);
00469   cleaner.add(delegate);
00470   cleaner.add(treeView);
00471 
00472   QCOMPARE(model->getCurrentParameters()->get<int>("Steve"), 4);
00473   QCOMPARE(model->getCurrentParameters()->get<int>("Sam"), 4);
00474   QCOMPARE(model->getCurrentParameters()->sublist("Prec").get<int>("Sam"), 9);
00475   QCOMPARE(model->getCurrentParameters()->sublist("Prec").get<int>("Blah"), 1);
00476   model->readInput("loadtest.in.xml");
00477   QCOMPARE(model->getCurrentParameters()->get<int>("Steve"), 0);
00478   QCOMPARE(model->getCurrentParameters()->sublist("Prec").get<int>("Blah"), 80);
00479   QCOMPARE(model->getCurrentParameters()->sublist("Prec").get<int>("Sam"), 99);
00480   QCOMPARE(model->getCurrentParameters()->get<int>("Sam"), 50);
00481 
00482   cleaner.remove(model);
00483   cleaner.remove(treeView);
00484   cleaner.remove(delegate);
00485   delete model;
00486   delete treeView;
00487   delete delegate;
00488 
00489 }
00490 
00491 template<class T>
00492 void testingSpinBoxApply(
00493   const RCP<EnhancedNumberValidator<T> > validator,
00494   QAbstractSpinBox* spinner)
00495 {
00496   ValidatorApplier<T>::applyToSpinBox(validator, (QSpinBox*)spinner);
00497 }
00498 
00499 template<>
00500 void testingSpinBoxApply(
00501   const RCP<EnhancedNumberValidator<double> > validator,
00502   QAbstractSpinBox* spinner)
00503 {
00504   ValidatorApplier<double>::applyToSpinBox(validator, (QDoubleSpinBox*)spinner);
00505 }
00506 
00507 template<>
00508 void testingSpinBoxApply(
00509   const RCP<EnhancedNumberValidator<float> > validator,
00510   QAbstractSpinBox* spinner)
00511 {
00512   ValidatorApplier<float>::applyToSpinBox(validator, (QDoubleSpinBox*)spinner);
00513 }
00514 
00515 template<class T>
00516 QAbstractSpinBox* createDefaultSpinner(){
00517   return new QSpinBox();
00518 }
00519 
00520 template<>
00521 QAbstractSpinBox* createDefaultSpinner<float>(){
00522   return new QDoubleSpinBox();
00523 }
00524 
00525 template<>
00526 QAbstractSpinBox* createDefaultSpinner<double>(){
00527   return new QDoubleSpinBox();
00528 }
00529 
00530 template<class T>
00531 void assertSpinnerDefaults(QAbstractSpinBox *spinBox){
00532   QSpinBox* actualSpinner = (QSpinBox*)spinBox;
00533   QCOMPARE(
00534     Teuchos::as<T>(actualSpinner->singleStep()), 
00535     EnhancedNumberTraits<T>::defaultStep());
00536 }
00537 
00538 template<>
00539 void assertSpinnerDefaults<float>(QAbstractSpinBox *spinBox){
00540   QDoubleSpinBox* actualSpinner = (QDoubleSpinBox*)spinBox;
00541   QCOMPARE(
00542     actualSpinner->decimals(),
00543     Teuchos::as<int>(EnhancedNumberTraits<float>::defaultPrecision()));
00544   QCOMPARE(
00545     Teuchos::as<float>(actualSpinner->singleStep()),
00546      EnhancedNumberTraits<float>::defaultStep());
00547 }
00548 
00549 template<>
00550 void assertSpinnerDefaults<double>(QAbstractSpinBox *spinBox){
00551   QDoubleSpinBox* actualSpinner = (QDoubleSpinBox*)spinBox;
00552   QCOMPARE(
00553     actualSpinner->decimals(),
00554     Teuchos::as<int>(EnhancedNumberTraits<double>::defaultPrecision()));
00555   QCOMPARE(
00556     Teuchos::as<double>(actualSpinner->singleStep()),
00557      EnhancedNumberTraits<double>::defaultStep());
00558 }
00559 
00560 
00561 template<class T>
00562 void assertLineEditDetails(
00563   const QValidator* validator, 
00564   QString& val20, 
00565   QString& valneg1,
00566   int& pos)
00567 {
00568     QCOMPARE(validator->validate(val20,pos), QValidator::Invalid);
00569     QCOMPARE(validator->validate(valneg1,pos), QValidator::Invalid);
00570 }
00571 
00572 template<>
00573 void assertLineEditDetails<float>(
00574   const QValidator* validator, 
00575   QString& val20, 
00576   QString& valneg1,
00577   int& pos)
00578 {
00579   QCOMPARE(validator->validate(val20,pos), QValidator::Intermediate);
00580   QCOMPARE(validator->validate(valneg1,pos), QValidator::Invalid);
00581   const QDoubleValidator* doubleValidator = (QDoubleValidator*)validator;
00582   QCOMPARE(
00583     doubleValidator->decimals(), 
00584     Teuchos::as<int>(EnhancedNumberTraits<float>::defaultPrecision()));
00585 }
00586 
00587 template<>
00588 void assertLineEditDetails<double>(
00589   const QValidator* validator, 
00590   QString& val20, 
00591   QString& valneg1,
00592   int& pos)
00593 {
00594   QCOMPARE(validator->validate(val20,pos), QValidator::Intermediate);
00595   QCOMPARE(validator->validate(valneg1,pos), QValidator::Invalid);
00596   const QDoubleValidator* doubleValidator = (QDoubleValidator*)validator;
00597   QCOMPARE(
00598     doubleValidator->decimals(), 
00599     Teuchos::as<int>(EnhancedNumberTraits<double>::defaultPrecision()));
00600 }
00601 
00602 template<class T>
00603 void valApplyTestTemplate(){
00604   EnhancedNumberValidator<T> validator(Teuchos::as<T>(0), Teuchos::as<T>(10));
00605   RCP<EnhancedNumberValidator<T> > valPtr = rcpFromRef(validator);
00606 
00607   QString val10("10");
00608   QString val20("20");
00609   QString val5("5");
00610   QString val0("0");
00611   QString valneg1("-1");
00612 
00613 
00615   QAbstractSpinBox* spinner = createDefaultSpinner<T>();
00616   testingSpinBoxApply(valPtr, spinner);
00617   int pos=0;
00618   QCOMPARE(spinner->validate(val10,pos), QValidator::Acceptable);
00619   QCOMPARE(spinner->validate(val20,pos), QValidator::Invalid);
00620   QCOMPARE(spinner->validate(val5,pos), QValidator::Acceptable);
00621   QCOMPARE(spinner->validate(val0,pos), QValidator::Acceptable);
00622   QCOMPARE(spinner->validate(valneg1,pos), QValidator::Invalid);
00623   assertSpinnerDefaults<T>(spinner);
00624   delete spinner;
00625 
00627   QLineEdit lineEdit;
00628   ValidatorApplier<T>::applyToLineEdit(valPtr, &lineEdit);
00629   const QValidator* appliedValidator = lineEdit.validator();
00630   QCOMPARE(appliedValidator->validate(val10,pos), QValidator::Acceptable);
00631   QCOMPARE(appliedValidator->validate(val5,pos), QValidator::Acceptable);
00632   QCOMPARE(appliedValidator->validate(val0,pos), QValidator::Acceptable);
00633   assertLineEditDetails<T>(appliedValidator, val20, valneg1, pos);
00634 }
00635 
00636 void OptikaGUITests::validatorApplierTests(){
00637   valApplyTestTemplate<int>();
00638   valApplyTestTemplate<short>();
00639   valApplyTestTemplate<double>();
00640   valApplyTestTemplate<float>();
00641 }
00642 
00643 void OptikaGUITests::settingsTest(){
00644   RCP<ParameterList> validParameters = rcp(new ParameterList("Steve"));
00645   validParameters->set("Don't care", "don't care");
00646   MetaWindow* m1 = new MetaWindow(validParameters);
00647   m1->move(30,99);
00648   m1->resize(673,823);
00649   delete m1;
00650   MetaWindow* m2 = new MetaWindow(validParameters);
00651   QCOMPARE(m2->width(),673);
00652   QCOMPARE(m2->height(),823);
00653   QCOMPARE(m2->x(),30);
00654   QCOMPARE(m2->y(),99);
00655   delete m2;
00656 }
00657 
00658 void OptikaGUITests::displayRoleTest(){
00659   cleaner.clear();
00660   RCP<ParameterList> validParameters = 
00661     getParametersFromXmlFile("loadtest.xml");
00662   validParameters->set("2dtest", Teuchos::TwoDArray<int>(2,2,4));
00663   TreeModel* model = new TreeModel(validParameters);
00664   Delegate* delegate = new Delegate;
00665   TreeView* treeView = new TreeView(model, delegate);
00666   cleaner.add(model);
00667   cleaner.add(delegate);
00668   cleaner.add(treeView);
00669   RCP<const ParameterList> parameters =  model->getCurrentParameters();
00670 
00671   RCP<const ParameterEntry> steveParameter = parameters->getEntryRCP("Steve");
00672   QModelIndex steveIndex = model->findParameterEntryIndex(steveParameter);
00673   QVariant data = model->data(
00674     steveIndex.sibling(steveIndex.row(), 1), Qt::DisplayRole);
00675   QCOMPARE(data.toString(), QString("4"));
00676 
00677   RCP<const ParameterEntry> twodParameter = parameters->getEntryRCP("2dtest");
00678   QModelIndex twodIndex = model->findParameterEntryIndex(twodParameter);
00679   data = model->data(
00680     twodIndex.sibling(twodIndex.row(), 1), Qt::DisplayRole);
00681   QCOMPARE(data.toString(), QString("Click to view 2D Array"));
00682 
00683   RCP<const ParameterEntry> listParameter = parameters->getEntryRCP("Prec");
00684   QModelIndex listIndex = model->findParameterEntryIndex(listParameter);
00685   data = model->data(
00686     listIndex.sibling(listIndex.row(), 1), Qt::DisplayRole);
00687   QCOMPARE(data.toString(), QString(""));
00688 
00689   cleaner.remove(model);
00690   cleaner.remove(treeView);
00691   cleaner.remove(delegate);
00692   delete model;
00693   delete treeView;
00694   delete delegate;
00695 
00696 
00697 
00698 }
00699 
00700 } //namespace Optika
00701 
00702 QTEST_MAIN(Optika::OptikaGUITests)
00703 #include "GUI_UnitTests.moc"
00704 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines