1 /***************************************************************************
2     testqgsspinbox.cpp
3      --------------------------------------
4     Date                 : December 2014
5     Copyright            : (C) 2014 Nyall Dawson
6     Email                : nyall dot dawson at gmail 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 
19 #include <editorwidgets/qgsspinbox.h>
20 
21 class TestQgsSpinBox: public QObject
22 {
23     Q_OBJECT
24   private slots:
25     void initTestCase(); // will be called before the first testfunction is executed.
26     void cleanupTestCase(); // will be called after the last testfunction was executed.
27     void init(); // will be called before each testfunction is executed.
28     void cleanup(); // will be called after every testfunction.
29 
30     void clear();
31     void expression();
32 
33   private:
34 
35 };
36 
initTestCase()37 void TestQgsSpinBox::initTestCase()
38 {
39 
40 }
41 
cleanupTestCase()42 void TestQgsSpinBox::cleanupTestCase()
43 {
44 }
45 
init()46 void TestQgsSpinBox::init()
47 {
48 }
49 
cleanup()50 void TestQgsSpinBox::cleanup()
51 {
52 }
53 
clear()54 void TestQgsSpinBox::clear()
55 {
56   QgsSpinBox *spinBox = new QgsSpinBox();
57   spinBox->setMaximum( 10 );
58   spinBox->setMinimum( 1 );
59   spinBox->setValue( 5 );
60   spinBox->setClearValueMode( QgsSpinBox::MinimumValue );
61   spinBox->clear();
62   QCOMPARE( spinBox->value(), 1 );
63   QCOMPARE( spinBox->clearValue(), 1 );
64   spinBox->setClearValueMode( QgsSpinBox::MaximumValue );
65   spinBox->clear();
66   QCOMPARE( spinBox->value(), 10 );
67   QCOMPARE( spinBox->clearValue(), 10 );
68   spinBox->setClearValue( 7 );
69   spinBox->clear();
70   QCOMPARE( spinBox->value(), 7 );
71   QCOMPARE( spinBox->clearValue(), 7 );
72   delete spinBox;
73 }
74 
expression()75 void TestQgsSpinBox::expression()
76 {
77   QgsSpinBox *spinBox = new QgsSpinBox();
78   spinBox->setMinimum( -10 );
79   spinBox->setMaximum( 10 );
80   spinBox->setValue( 1 );
81   spinBox->setExpressionsEnabled( false );
82   QCOMPARE( spinBox->valueFromText( QString( "5" ) ), 5 );
83   QCOMPARE( spinBox->valueFromText( QString( "5+2" ) ), -10 );
84   spinBox->setExpressionsEnabled( true );
85   QCOMPARE( spinBox->valueFromText( QString( "5" ) ), 5 );
86   QCOMPARE( spinBox->valueFromText( QString( "5+2" ) ), 7 );
87   spinBox->setClearValue( 3 );
88   spinBox->setShowClearButton( true );
89   QCOMPARE( spinBox->valueFromText( QString() ), 3 ); //clearing should set to clearValue
90   spinBox->setShowClearButton( false );
91   spinBox->setValue( 8 );
92   QCOMPARE( spinBox->valueFromText( QString() ), 8 ); //if no clear button, clearing should set to previous value
93   spinBox->setShowClearButton( true );
94   spinBox->setValue( 4 );
95   QCOMPARE( spinBox->valueFromText( QString( "5/" ) ), 4 ); //invalid expression should reset to previous value
96 
97   //suffix tests
98   spinBox->setSuffix( QStringLiteral( "mm" ) );
99   spinBox->setExpressionsEnabled( false );
100   QCOMPARE( spinBox->valueFromText( QString( "5mm" ) ), 5 );
101   QCOMPARE( spinBox->valueFromText( QString( "5+2mm" ) ), -10 );
102   spinBox->setExpressionsEnabled( true );
103   QCOMPARE( spinBox->valueFromText( QString( "5 mm" ) ), 5 );
104   QCOMPARE( spinBox->valueFromText( QString( "5+2 mm" ) ), 7 );
105   QCOMPARE( spinBox->valueFromText( QString( "5mm" ) ), 5 );
106   QCOMPARE( spinBox->valueFromText( QString( "5+2mm" ) ), 7 );
107   spinBox->setClearValue( 3 );
108   QCOMPARE( spinBox->valueFromText( QString( "mm" ) ), 3 ); //clearing should set to clearValue
109   QCOMPARE( spinBox->valueFromText( QString() ), 3 );
110   spinBox->setValue( 4 );
111   QCOMPARE( spinBox->valueFromText( QString( "5/mm" ) ), 4 ); //invalid expression should reset to previous value
112 
113   //prefix tests
114   spinBox->setSuffix( QString() );
115   spinBox->setPrefix( QStringLiteral( "mm" ) );
116   spinBox->setExpressionsEnabled( false );
117   QCOMPARE( spinBox->valueFromText( QString( "mm5" ) ), 5 );
118   QCOMPARE( spinBox->valueFromText( QString( "mm5+2" ) ), -10 );
119   spinBox->setExpressionsEnabled( true );
120   QCOMPARE( spinBox->valueFromText( QString( "mm 5" ) ), 5 );
121   QCOMPARE( spinBox->valueFromText( QString( "mm 5+2" ) ), 7 );
122   QCOMPARE( spinBox->valueFromText( QString( "mm5" ) ), 5 );
123   QCOMPARE( spinBox->valueFromText( QString( "mm5+2" ) ), 7 );
124   spinBox->setClearValue( 3 );
125   QCOMPARE( spinBox->valueFromText( QString( "mm" ) ), 3 ); //clearing should set to clearValue
126   QCOMPARE( spinBox->valueFromText( QString() ), 3 );
127   spinBox->setValue( 4 );
128   QCOMPARE( spinBox->valueFromText( QString( "mm5/" ) ), 4 ); //invalid expression should reset to previous value
129 
130   //both suffix and prefix
131   spinBox->setSuffix( QStringLiteral( "ll" ) );
132   spinBox->setPrefix( QStringLiteral( "mm" ) );
133   spinBox->setExpressionsEnabled( true );
134   QCOMPARE( spinBox->valueFromText( QString( "mm 5 ll" ) ), 5 );
135   QCOMPARE( spinBox->valueFromText( QString( "mm 5+2 ll" ) ), 7 );
136   QCOMPARE( spinBox->valueFromText( QString( "mm5ll" ) ), 5 );
137   QCOMPARE( spinBox->valueFromText( QString( "mm5+2ll" ) ), 7 );
138   spinBox->setClearValue( 3 );
139   QCOMPARE( spinBox->valueFromText( QString( "mmll" ) ), 3 ); //clearing should set to clearValue
140   QCOMPARE( spinBox->valueFromText( QString() ), 3 );
141   spinBox->setValue( 4 );
142   QCOMPARE( spinBox->valueFromText( QString( "mm5/ll" ) ), 4 ); //invalid expression should reset to previous value
143 
144   delete spinBox;
145 }
146 
147 QGSTEST_MAIN( TestQgsSpinBox )
148 #include "testqgsspinbox.moc"
149