Forráskód Böngészése

Add test to ctkDynamicSpacer

Julien Finet 14 éve
szülő
commit
1d831c51dd

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

@@ -17,6 +17,8 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkDirectoryButtonTest1.cpp
   ctkDoubleRangeSliderTest1.cpp
   ctkDoubleSliderTest1.cpp
+  ctkDynamicSpacerTest1.cpp
+  ctkDynamicSpacerTest2.cpp
   ctkFileDialogTest1.cpp
   ctkFittedTextBrowserTest1.cpp
   ctkMatrixWidgetTest1.cpp
@@ -84,6 +86,8 @@ SIMPLE_TEST( ctkCoordinatesWidgetTest1 )
 SIMPLE_TEST( ctkDirectoryButtonTest1 )
 SIMPLE_TEST( ctkDoubleRangeSliderTest1 )
 SIMPLE_TEST( ctkDoubleSliderTest1 )
+SIMPLE_TEST( ctkDynamicSpacerTest1 )
+SIMPLE_TEST( ctkDynamicSpacerTest2 )
 SIMPLE_TEST( ctkFileDialogTest1 )
 SIMPLE_TEST( ctkFittedTextBrowserTest1 )
 SIMPLE_TEST( ctkMatrixWidgetTest1 )

+ 127 - 0
Libs/Widgets/Testing/Cpp/ctkDynamicSpacerTest1.cpp

@@ -0,0 +1,127 @@
+/*=========================================================================
+
+  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 <QPushButton>
+#include <QSignalSpy>
+#include <QStyle>
+#include <QTimer>
+#include <QVBoxLayout>
+
+// CTK includes
+#include "ctkDynamicSpacer.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkDynamicSpacerTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+  
+  QWidget topLevel;
+  ctkDynamicSpacer* spacer1 = new ctkDynamicSpacer;
+  QPushButton* button= new QPushButton;
+  button->setCheckable(true);
+  ctkDynamicSpacer* spacer2 = new ctkDynamicSpacer;
+  
+  QVBoxLayout* layout = new QVBoxLayout;
+  layout->addWidget(spacer1);
+  layout->addWidget(button);
+  layout->addWidget(spacer2);
+  topLevel.setLayout(layout);
+  
+  if (spacer1->activeSizePolicy() != 
+      QSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred))
+    {
+    std::cerr << "ctkDynamicSpacer: wrong default values" << std::endl;
+    return EXIT_FAILURE;
+    }
+  spacer1->setActiveSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+  if (spacer1->activeSizePolicy() != 
+      QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding))
+    {
+    std::cerr << "ctkDynamicSpacer::setActiveSizePolicy failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  spacer1->setInactiveSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
+  if (spacer1->inactiveSizePolicy() != 
+      QSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum))
+    {
+    std::cerr << "ctkDynamicSpacer::setInactiveSizePolicy failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  spacer2->setInactiveSizePolicy(spacer1->inactiveSizePolicy());
+  spacer2->setActiveSizePolicy(spacer1->activeSizePolicy());
+  
+  QObject::connect(button, SIGNAL(toggled(bool)), spacer1, SLOT(setActive(bool)));
+  QObject::connect(button, SIGNAL(toggled(bool)), spacer2, SLOT(setInactive(bool)));
+
+  spacer1->setPalette(QPalette(Qt::red));
+  spacer2->setPalette(QPalette(Qt::blue));
+  spacer1->setAutoFillBackground(true);
+  spacer2->setAutoFillBackground(true);
+
+  topLevel.resize(50, 300);
+  topLevel.show();
+  QApplication::processEvents();
+  
+  // both inactive, they don't grow
+  if (spacer1->height() != 0 || spacer2->height() != 0)
+    {
+    std::cerr << "ctkDynamicSpacer failed 1: " << spacer1->height() << " "
+              << spacer2->height() << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  button->toggle();
+  // need to repaint the widgets to query their size
+  QApplication::processEvents();
+
+  if (spacer1->height() <= 0 || spacer2->height() != 0)
+    {
+    std::cerr << "ctkDynamicSpacer failed 2: " << spacer1->height() << " "
+              << spacer2->height() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  button->toggle();
+  // need to repaint the widgets to query their size
+  QApplication::processEvents();
+
+  if (spacer1->height() != 0 || spacer2->height() <= 0)
+    {
+    std::cerr << "ctkDynamicSpacer failed 3: " << spacer1->height() << " "
+              << spacer2->height() << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}
+

+ 79 - 0
Libs/Widgets/Testing/Cpp/ctkDynamicSpacerTest2.cpp

@@ -0,0 +1,79 @@
+/*=========================================================================
+
+  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 <QStyle>
+#include <QTimer>
+#include <QTreeView>
+#include <QVBoxLayout>
+
+// CTK includes
+#include "ctkCollapsibleButton.h"
+#include "ctkDynamicSpacer.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkDynamicSpacerTest2(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+  
+  QWidget topLevel;
+  ctkCollapsibleButton* button = new ctkCollapsibleButton("Collapsible button");
+  QTreeView* view = new QTreeView;
+  QVBoxLayout* collapsibleLayout = new QVBoxLayout;
+  collapsibleLayout->addWidget(view);
+  button->setLayout(collapsibleLayout);
+
+  ctkDynamicSpacer* spacer = new ctkDynamicSpacer;
+  
+  QVBoxLayout* layout = new QVBoxLayout;
+  layout->addWidget(button);
+  layout->addWidget(spacer);
+  topLevel.setLayout(layout);
+  
+  view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+  spacer->setInactiveSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
+  spacer->setActiveSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+  
+  QObject::connect(button, SIGNAL(contentsCollapsed(bool)),
+                   spacer, SLOT(setActive(bool)));
+  spacer->setPalette(QPalette(Qt::red));
+  spacer->setAutoFillBackground(true);
+
+  topLevel.show();
+  
+  if (spacer->height() != 0)
+    {
+    std::cerr << "ctkDynamicSpacer failed: " << spacer->height() << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}
+