|
| 1 | +/*************************************************************************** |
| 2 | + testqgsvaluerelationwidgetwrapper.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : 21 07 2017 |
| 5 | + Copyright : (C) 2017 Paul Blottiere |
| 6 | + Email : paul dot blottiere at oslandia dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | + |
| 17 | +#include "qgstest.h" |
| 18 | +#include <QScrollBar> |
| 19 | + |
| 20 | +#include <editorwidgets/core/qgseditorwidgetregistry.h> |
| 21 | +#include <qgsapplication.h> |
| 22 | +#include <qgsproject.h> |
| 23 | +#include <qgsvectorlayer.h> |
| 24 | +#include "qgseditorwidgetwrapper.h" |
| 25 | +#include <editorwidgets/qgsvaluerelationwidgetwrapper.h> |
| 26 | +#include "qgsgui.h" |
| 27 | + |
| 28 | +class TestQgsValueRelationWidgetWrapper : public QObject |
| 29 | +{ |
| 30 | + Q_OBJECT |
| 31 | + public: |
| 32 | + TestQgsValueRelationWidgetWrapper() {} |
| 33 | + |
| 34 | + private slots: |
| 35 | + void initTestCase(); // will be called before the first testfunction is executed. |
| 36 | + void cleanupTestCase(); // will be called after the last testfunction was executed. |
| 37 | + void init(); // will be called before each testfunction is executed. |
| 38 | + void cleanup(); // will be called after every testfunction. |
| 39 | + |
| 40 | + void testScrollBarUnlocked(); |
| 41 | +}; |
| 42 | + |
| 43 | +void TestQgsValueRelationWidgetWrapper::initTestCase() |
| 44 | +{ |
| 45 | + QgsApplication::init(); |
| 46 | + QgsApplication::initQgis(); |
| 47 | + QgsGui::editorWidgetRegistry()->initEditors(); |
| 48 | +} |
| 49 | + |
| 50 | +void TestQgsValueRelationWidgetWrapper::cleanupTestCase() |
| 51 | +{ |
| 52 | + QgsApplication::exitQgis(); |
| 53 | +} |
| 54 | + |
| 55 | +void TestQgsValueRelationWidgetWrapper::init() |
| 56 | +{ |
| 57 | +} |
| 58 | + |
| 59 | +void TestQgsValueRelationWidgetWrapper::cleanup() |
| 60 | +{ |
| 61 | +} |
| 62 | + |
| 63 | +void TestQgsValueRelationWidgetWrapper::testScrollBarUnlocked() |
| 64 | +{ |
| 65 | + // create a vector layer |
| 66 | + QgsVectorLayer vl1( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=fk|:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) ); |
| 67 | + QgsProject::instance()->addMapLayer( &vl1, false, false ); |
| 68 | + |
| 69 | + // build a value relation widget wrapper |
| 70 | + QListWidget lw; |
| 71 | + QWidget editor; |
| 72 | + QgsValueRelationWidgetWrapper w( &vl1, 0, &editor, nullptr ); |
| 73 | + w.setEnabled( true ); |
| 74 | + w.initWidget( &lw ); |
| 75 | + |
| 76 | + // add an item virtually |
| 77 | + QListWidgetItem item; |
| 78 | + item.setText( "MyText" ); |
| 79 | + w.mListWidget->addItem( &item ); |
| 80 | + QCOMPARE( w.mListWidget->item( 0 )->text(), QString( "MyText" ) ); |
| 81 | + |
| 82 | + // when the widget wrapper is enabled, the container should be enabled |
| 83 | + // as well as items |
| 84 | + w.setEnabled( true ); |
| 85 | + |
| 86 | + QCOMPARE( w.widget()->isEnabled(), true ); |
| 87 | + |
| 88 | + bool itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled; |
| 89 | + QCOMPARE( itemEnabled, true ); |
| 90 | + |
| 91 | + // when the widget wrapper is disabled, the container should still be enabled |
| 92 | + // to keep the scrollbar available but items should be disabled to avoid |
| 93 | + // edition |
| 94 | + w.setEnabled( false ); |
| 95 | + |
| 96 | + itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled; |
| 97 | + QCOMPARE( itemEnabled, false ); |
| 98 | + |
| 99 | + QCOMPARE( w.widget()->isEnabled(), true ); |
| 100 | + |
| 101 | + // recheck after re-enabled |
| 102 | + w.setEnabled( true ); |
| 103 | + |
| 104 | + QCOMPARE( w.widget()->isEnabled(), true ); |
| 105 | + itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled; |
| 106 | + QCOMPARE( itemEnabled, true ); |
| 107 | +} |
| 108 | + |
| 109 | +QGSTEST_MAIN( TestQgsValueRelationWidgetWrapper ) |
| 110 | +#include "testqgsvaluerelationwidgetwrapper.moc" |
0 commit comments