test_settings.py 5.23 KB
Newer Older
Benjamin Jakimow's avatar
tbd.  
Benjamin Jakimow committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# -*- coding: utf-8 -*-

"""
***************************************************************************
    
    ---------------------
    Date                 : 30.11.2017
    Copyright            : (C) 2017 by Benjamin Jakimow
    Email                : benjamin jakimow at geo dot hu-berlin dot de
***************************************************************************
*                                                                         *
*   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.                                   *
*                                                                         *
***************************************************************************
"""
# noinspection PyPep8Naming
20
import unittest
Benjamin Jakimow's avatar
Benjamin Jakimow committed
21
import json
22 23
import tempfile
import xmlrunner
Benjamin Jakimow's avatar
tbd.  
Benjamin Jakimow committed
24

25 26
from qgis.core import QgsReadWriteContext, QgsTextFormat

27
from eotimeseriesviewer.tests import EOTSVTestCase
Benjamin Jakimow's avatar
tbd.  
Benjamin Jakimow committed
28

29
from eotimeseriesviewer.settings import *
Benjamin Jakimow's avatar
Benjamin Jakimow committed
30 31


32
class TestSettings(EOTSVTestCase):
Benjamin Jakimow's avatar
tbd.  
Benjamin Jakimow committed
33 34

    def test_Dialog(self):
35
        allValues = values()
Benjamin Jakimow's avatar
tbd.  
Benjamin Jakimow committed
36 37

        d = SettingsDialog()
38
        self.assertIsInstance(d, SettingsDialog)
Benjamin Jakimow's avatar
tbd.  
Benjamin Jakimow committed
39

40
        specs = value(Keys.SensorSpecs)
41 42 43
        defaults = defaultValues()
        self.assertIsInstance(defaults, dict)

44 45 46 47
        dialogValues = d.values()
        for k in Keys:
            a, b = allValues[k], dialogValues[k]
            if not a is None:
48 49 50 51
                if isinstance(a, QgsTextFormat):
                    self.assertIsInstance(b, QgsTextFormat)
                    a = a.toMimeData().text()
                    b = b.toMimeData().text()
52
                self.assertEqual(a, b, msg='Dialog returns {} instead {} for settings key {}'.format(a, b, k))
53

54 55 56
        defaultMapColor = dialogValues[Keys.MapBackgroundColor]
        dialogValues[Keys.MapBackgroundColor] = QColor('yellow')
        d.setValues(dialogValues)
57 58 59 60
        self.assertTrue(d.mCanvasColorButton.color() == QColor('yellow'))
        d.onAccept()

        d = SettingsDialog()
61 62 63 64
        dialogValues = d.values()
        self.assertTrue(dialogValues[Keys.MapBackgroundColor] == QColor('yellow'))
        dialogValues[Keys.MapBackgroundColor] = defaultMapColor
        setValues(dialogValues)
65 66

        d = SettingsDialog()
67 68
        dialogValues = d.values()
        self.assertTrue(dialogValues[Keys.MapBackgroundColor] == defaultMapColor)
69

Benjamin Jakimow's avatar
Benjamin Jakimow committed
70
        self.showGui(d)
Benjamin Jakimow's avatar
tbd.  
Benjamin Jakimow committed
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    def test_SensorMatching(self):

        f0 = SensorMatching.PX_DIMS

        self.assertTrue(bool(f0 & SensorMatching.PX_DIMS))
        self.assertFalse(bool(f0 & SensorMatching.WL))
        f1 = SensorMatching.PX_DIMS | SensorMatching.WL
        self.assertTrue(bool(f1 & SensorMatching.WL))
        self.assertFalse(bool(f1 & SensorMatching.NAME))

        for f in [f0, f1]:
            name = SensorMatching.name(f1)
            tooltip = SensorMatching.tooltip(f)
            self.assertIsInstance(name, str)
            self.assertIsInstance(tooltip, str)
            self.assertTrue(len(name) > 0)
            self.assertTrue(len(tooltip) > 0)

90 91 92 93 94 95 96
    def test_SensorModel(self):

        tb = QTableView()
        m = SensorSettingsTableModel()
        tb.setModel(m)
        tb.show()

Benjamin Jakimow's avatar
Benjamin Jakimow committed
97
        self.showGui(tb)
Benjamin Jakimow's avatar
tbd.  
Benjamin Jakimow committed
98

99 100 101
    def test_MapTextFormat(self):

        key = Keys.MapTextFormat
Benjamin Jakimow's avatar
Benjamin Jakimow committed
102
        format0 = defaultValues()[key]
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        format1 = defaultValues()[key]
        self.assertIsInstance(format1, QgsTextFormat)
        color1 = QColor('yellow')
        format1.setColor(color1)

        doc = QDomDocument()
        doc.appendChild(format1.writeXml(doc, QgsReadWriteContext()))
        docBA = doc.toByteArray()
        docStr = doc.toString()

        doc2 = QDomDocument()
        doc2.setContent(docBA)
        format2 = QgsTextFormat()
        format2.readXml(doc2.documentElement(), QgsReadWriteContext())
        color2 = format2.color()
        self.assertEqual(color1, color2)

        QS = QSettings('TEST')
        QS.setValue(key.value, doc.toByteArray())

        docBA3 = QS.value(key.value)
        self.assertIsInstance(docBA3, QByteArray)
        doc3 = QDomDocument()
        doc3.setContent(docBA3)
        format3 = QgsTextFormat()
        format3.readXml(doc3.documentElement(), QgsReadWriteContext())
        self.assertEqual(color1, format3.color())

        setValue(key, format1)
        self.assertEqual(format1.color(), value(key).color())

        s = ""

136 137 138 139 140 141 142 143 144 145 146 147 148 149
    def test_saveAndRestoreSensorNames(self):

        from example.Images import Img_2014_01_15_LC82270652014015LGN00_BOA
        from eotimeseriesviewer.timeseries import TimeSeriesSource, SensorInstrument, sensorIDtoProperties
        tss = TimeSeriesSource.create(Img_2014_01_15_LC82270652014015LGN00_BOA)
        self.assertIsInstance(tss, TimeSeriesSource)
        sensorID = tss.sid()

        jsonDict = json.loads(sensorID)
        assert isinstance(jsonDict, dict)

        for k in ['nb', 'px_size_x', 'px_size_y', 'dt', 'wl', 'wlu', 'name']:
            self.assertTrue(k in jsonDict.keys())

Benjamin Jakimow's avatar
Benjamin Jakimow committed
150
        # removed: should be done by project settings
151

Benjamin Jakimow's avatar
Benjamin Jakimow committed
152

Benjamin Jakimow's avatar
tbd.  
Benjamin Jakimow committed
153
if __name__ == "__main__":
154 155
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'), buffer=False)
    exit(0)