Skip to content

Commit

Permalink
Add unit tests for QgsRasterBandComboBox
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 9, 2017
1 parent c9bfb9f commit e4a94bc
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/gui/raster/qgsrasterbandcombobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ QgsRasterBandComboBox::QgsRasterBandComboBox( QWidget *parent )
{
emit bandChanged( currentIndex() >= 0 ? currentData().toInt() : -1 );
} );
setEnabled( false );
}

QgsRasterLayer *QgsRasterBandComboBox::layer() const
Expand All @@ -41,6 +42,8 @@ void QgsRasterBandComboBox::setLayer( QgsMapLayer *layer )
{
QgsRasterLayer *rl = qobject_cast< QgsRasterLayer * >( layer );
mLayer = rl;
int oldBand = currentBand();

blockSignals( true );
clear();

Expand All @@ -63,7 +66,7 @@ void QgsRasterBandComboBox::setLayer( QgsMapLayer *layer )
setEnabled( count() > 1 );

if ( count() > 0 )
setCurrentIndex( 0 );
setCurrentIndex( findData( oldBand ) >= 0 ? findData( oldBand ) : 0 );

blockSignals( false );
emit bandChanged( currentIndex() >= 0 ? currentData().toInt() : -1 );
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ ADD_PYTHON_TEST(PyQgsPointDisplacementRenderer test_qgspointdisplacementrenderer
ADD_PYTHON_TEST(PyQgsProjectionSelectionWidgets test_qgsprojectionselectionwidgets.py)
ADD_PYTHON_TEST(PyQgsRange test_qgsrange.py)
ADD_PYTHON_TEST(PyQgsRangeWidgets test_qgsrangewidgets.py)
ADD_PYTHON_TEST(PyQgsRasterBandComboBox test_qgsrasterbandcombobox.py)
ADD_PYTHON_TEST(PyQgsRasterFileWriter test_qgsrasterfilewriter.py)
ADD_PYTHON_TEST(PyQgsRasterFileWriterTask test_qgsrasterfilewritertask.py)
ADD_PYTHON_TEST(PyQgsRasterLayer test_qgsrasterlayer.py)
Expand Down
127 changes: 127 additions & 0 deletions tests/src/python/test_qgsrasterbandcombobox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsRasterBandComboBox.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '09/05/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA
import os

from qgis.core import QgsRasterLayer
from qgis.gui import QgsRasterBandComboBox
from qgis.testing import start_app, unittest
from qgis.PyQt.QtCore import QFileInfo
from qgis.PyQt.QtTest import QSignalSpy
from utilities import unitTestDataPath

start_app()


class TestQgsRasterBandComboBox(unittest.TestCase):

def testNoLayer(self):
"""
Test widget with no layer
"""

combo = QgsRasterBandComboBox()
self.assertFalse(combo.layer())
self.assertEqual(combo.currentBand(), -1)
self.assertFalse(combo.isEnabled())

combo.setShowNotSetOption(True)
self.assertEqual(combo.currentBand(), -1)
self.assertFalse(combo.isEnabled())

combo.setBand(11111)
self.assertEqual(combo.currentBand(), -1)
combo.setBand(-11111)
self.assertEqual(combo.currentBand(), -1)

def testOneBandRaster(self):
path = os.path.join(unitTestDataPath('raster'),
'band1_float32_noct_epsg4326.tif')
info = QFileInfo(path)
base_name = info.baseName()
layer = QgsRasterLayer(path, base_name)
self.assertTrue(layer)

combo = QgsRasterBandComboBox()
combo.setLayer(layer)
self.assertEqual(combo.layer(), layer)
self.assertEqual(combo.currentBand(), 1)
# only one choice, should be disabled
self.assertEqual(combo.count(), 1)
self.assertFalse(combo.isEnabled())

combo.setShowNotSetOption(True)
self.assertEqual(combo.currentBand(), 1)
self.assertEqual(combo.count(), 2)
self.assertTrue(combo.isEnabled())
combo.setBand(-1)
self.assertEqual(combo.currentBand(), -1)
combo.setBand(1)
self.assertEqual(combo.currentBand(), 1)

combo.setShowNotSetOption(False)
self.assertEqual(combo.currentBand(), 1)
self.assertEqual(combo.count(), 1)
self.assertFalse(combo.isEnabled())

def testMultiBandRaster(self):
path = os.path.join(unitTestDataPath('raster'),
'band3_float32_noct_epsg4326.tif')
info = QFileInfo(path)
base_name = info.baseName()
layer = QgsRasterLayer(path, base_name)
self.assertTrue(layer)

combo = QgsRasterBandComboBox()
combo.setLayer(layer)
self.assertEqual(combo.layer(), layer)
self.assertEqual(combo.currentBand(), 1)
self.assertEqual(combo.count(), 3)
self.assertTrue(combo.isEnabled())
combo.setBand(2)
self.assertEqual(combo.currentBand(), 2)

combo.setShowNotSetOption(True)
self.assertEqual(combo.currentBand(), 2)
self.assertEqual(combo.count(), 4)
self.assertTrue(combo.isEnabled())

combo.setShowNotSetOption(False)
self.assertEqual(combo.currentBand(), 2)
self.assertEqual(combo.count(), 3)
self.assertTrue(combo.isEnabled())

def testSignals(self):
path = os.path.join(unitTestDataPath('raster'),
'band3_float32_noct_epsg4326.tif')
info = QFileInfo(path)
base_name = info.baseName()
layer = QgsRasterLayer(path, base_name)
self.assertTrue(layer)

combo = QgsRasterBandComboBox()
combo.setLayer(layer)

signal_spy = QSignalSpy(combo.bandChanged)
combo.setBand(2)
self.assertEqual(len(signal_spy), 1)
self.assertEqual(signal_spy[0][0], 2)
combo.setBand(3)
self.assertEqual(len(signal_spy), 2)
self.assertEqual(signal_spy[1][0], 3)


if __name__ == '__main__':
unittest.main()

0 comments on commit e4a94bc

Please sign in to comment.