Browse Source

Add test to ctkSettings

Add comments to ctkSettings.h
Julien Finet 14 years ago
parent
commit
479980f442

+ 2 - 0
Libs/Widgets/Testing/Cpp/CMakeLists.txt

@@ -23,6 +23,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkRangeSliderTest1.cpp
   ctkRangeWidgetTest1.cpp
   ctkDateRangeWidgetTest1.cpp
+  ctkSettingsTest1.cpp
   ctkSliderWidgetTest1.cpp
   ctkTreeComboBoxTest1.cpp
   ctkWorkflowWidgetTest1.cpp
@@ -87,6 +88,7 @@ SIMPLE_TEST( ctkMenuButtonTest1 )
 SIMPLE_TEST( ctkRangeSliderTest1 )
 SIMPLE_TEST( ctkRangeWidgetTest1 )
 SIMPLE_TEST( ctkDateRangeWidgetTest1 )
+SIMPLE_TEST( ctkSettingsTest1 )
 SIMPLE_TEST( ctkSliderWidgetTest1 )
 SIMPLE_TEST( ctkTreeComboBoxTest1 )
 SIMPLE_TEST( ctkWorkflowWidgetTest1 )

+ 97 - 0
Libs/Widgets/Testing/Cpp/ctkSettingsTest1.cpp

@@ -0,0 +1,97 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QDialog>
+#include <QMainWindow>
+#include <QTimer>
+
+// CTK includes
+#include "ctkSettings.h"
+
+// STD includes
+#include <stdlib.h>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkSettingsTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+  
+  QMainWindow mainWindow(0);
+  mainWindow.show();
+  
+  mainWindow.move(123, 123);
+  mainWindow.resize(640, 470);
+  ctkSettings settings("CTK", "CTK", 0);
+  settings.saveState(mainWindow,"");
+  mainWindow.move(100, 100);
+  mainWindow.resize(300, 200);
+  settings.saveState(mainWindow, "key");
+  
+  settings.restoreState("", mainWindow);
+  if (mainWindow.pos() != QPoint(123,123) ||
+      mainWindow.size() != QSize(640, 470))
+    {
+    std::cerr << "ctkSettings::restoreState failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  settings.restoreState("key", mainWindow);
+  if (mainWindow.pos() != QPoint(100,100) ||
+      mainWindow.size() != QSize(300, 200))
+    {
+    std::cerr << "ctkSettings::restoreState failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  QDialog dialog(0);
+  dialog.show();
+  dialog.move(456, 456);
+  dialog.resize(320, 222);
+
+  settings.saveState(dialog,"key2");
+  dialog.move(50, 50);
+  dialog.resize(432, 743);
+  settings.saveState(dialog, "key3");
+  
+  settings.restoreState("key2", dialog);
+  if (dialog.pos() != QPoint(456,456) ||
+      dialog.size() != QSize(320, 222))
+    {
+    std::cerr << "ctkSettings::restoreState failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  settings.restoreState("key3", dialog);
+  if (dialog.pos() != QPoint(50,50) ||
+      dialog.size() != QSize(432, 743))
+    {
+    std::cerr << "ctkSettings::restoreState failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+    
+    if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}
+

+ 10 - 2
Libs/Widgets/ctkSettings.h

@@ -76,11 +76,19 @@ public:
     const QString& organization,
     const QString& application,
     QObject* p);
-    
+
+  /// Saves the position, size and layout of the QMainWindow
   void saveState(const QMainWindow& window, const QString& key);
+
+  /// Saves the position and size of the QDialog
   void saveState(const QDialog& dialog, const QString& key);
-  
+
+  /// Restore the position, size and layout of the QMainWindow
+  /// for a given key
   void restoreState(const QString& key, QMainWindow& window);
+  
+  /// Saves the position, size and layout of the QMainWindow
+  /// for a given key
   void restoreState(const QString& key, QDialog& dialog);
 
   /// Calling this method will cause the modified signal to be emited.