Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new QgsSettingsEntryFlag and tests for enum and flag
- Loading branch information
Showing
5 changed files
with
296 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/*************************************************************************** | ||
testqgssettingsentry.cpp | ||
-------------------------------------- | ||
Date : 01.04.2021 | ||
Copyright : (C) 2021 by Damiano Lombardi | ||
Email : damiano@opengis.ch | ||
*************************************************************************** | ||
* * | ||
* 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. * | ||
* * | ||
***************************************************************************/ | ||
#include <QObject> | ||
|
||
|
||
#include "qgssettings.h" | ||
#include "qgssettingsentry.h" | ||
#include "qgsunittypes.h" | ||
#include "qgsmaplayerproxymodel.h" | ||
#include "qgstest.h" | ||
|
||
|
||
/** | ||
* \ingroup UnitTests | ||
* This is a unit test for the operations on curve geometries | ||
*/ | ||
class TestQgsSettingsEntry : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
private slots: | ||
void enumValue(); | ||
void flagValue(); | ||
}; | ||
|
||
|
||
void TestQgsSettingsEntry::enumValue() | ||
{ | ||
QString settingsKey( "qgis/testing/my_enum_value_for_units" ); | ||
|
||
// Make sure the setting is not existing | ||
QgsSettings().remove( settingsKey, QgsSettings::NoSection ); | ||
|
||
QgsSettingsEntryEnum settingsEntryEnum( settingsKey, QgsSettings::NoSection, QgsUnitTypes::LayoutMeters, "Layout unit" ); | ||
|
||
// Check default value | ||
QCOMPARE( settingsEntryEnum.defaultValue(), QgsUnitTypes::LayoutMeters ); | ||
|
||
// Check set value | ||
{ | ||
bool success = settingsEntryEnum.setValue( QgsUnitTypes::LayoutFeet ); | ||
QCOMPARE( success, true ); | ||
QgsUnitTypes::LayoutUnit qgsSettingsValue = QgsSettings().enumValue( settingsKey, QgsUnitTypes::LayoutMeters, QgsSettings::NoSection ); | ||
QCOMPARE( qgsSettingsValue, QgsUnitTypes::LayoutFeet ); | ||
} | ||
|
||
// Check get value | ||
QgsSettings().setEnumValue( settingsKey, QgsUnitTypes::LayoutPicas, QgsSettings::NoSection ); | ||
QCOMPARE( settingsEntryEnum.value(), QgsUnitTypes::LayoutPicas ); | ||
|
||
// Check settings type | ||
QCOMPARE( settingsEntryEnum.settingsType(), QgsSettingsEntryBase::Enum ); | ||
|
||
// assign to inexisting value | ||
{ | ||
bool success = settingsEntryEnum.setValue( -1 ); | ||
QCOMPARE( success, false ); | ||
|
||
// Current value should not have changed | ||
QgsUnitTypes::LayoutUnit qgsSettingsValue = QgsSettings().enumValue( settingsKey, QgsUnitTypes::LayoutMeters, QgsSettings::NoSection ); | ||
QCOMPARE( qgsSettingsValue, QgsUnitTypes::LayoutPicas ); | ||
} | ||
|
||
// check that value is stored as string | ||
QCOMPARE( settingsEntryEnum.valueAsVariant().toString(), QMetaEnum::fromType<QgsUnitTypes::LayoutUnit>().key( QgsUnitTypes::LayoutPicas ) ); | ||
|
||
// auto conversion of old settings (int to str) | ||
QSettings().setValue( settingsKey, static_cast<int>( QgsUnitTypes::LayoutCentimeters ) ); | ||
QCOMPARE( settingsEntryEnum.valueAsVariant().toInt(), QgsUnitTypes::LayoutCentimeters ); | ||
QCOMPARE( settingsEntryEnum.value(), QgsUnitTypes::LayoutCentimeters ); | ||
} | ||
|
||
void TestQgsSettingsEntry::flagValue() | ||
{ | ||
QString settingsKey( "qgis/testing/my_flag_value_for_units" ); | ||
QgsMapLayerProxyModel::Filters pointAndLine = QgsMapLayerProxyModel::Filters( QgsMapLayerProxyModel::PointLayer | QgsMapLayerProxyModel::LineLayer ); | ||
QgsMapLayerProxyModel::Filters pointAndPolygon = QgsMapLayerProxyModel::Filters( QgsMapLayerProxyModel::PointLayer | QgsMapLayerProxyModel::PolygonLayer ); | ||
QgsMapLayerProxyModel::Filters hasGeometry = QgsMapLayerProxyModel::Filters( QgsMapLayerProxyModel::HasGeometry ); | ||
|
||
// Make sure the setting is not existing | ||
QgsSettings().remove( settingsKey, QgsSettings::NoSection ); | ||
|
||
QgsSettingsEntryFlag settingsEntryFlag( settingsKey, QgsSettings::NoSection, pointAndLine, "Filters" ); | ||
|
||
// Check default value | ||
QCOMPARE( settingsEntryFlag.defaultValue(), pointAndLine ); | ||
|
||
// Check set value | ||
{ | ||
bool success = settingsEntryFlag.setValue( hasGeometry ); | ||
QCOMPARE( success, true ); | ||
QgsMapLayerProxyModel::Filters qgsSettingsValue = QgsSettings().flagValue( settingsKey, pointAndLine, QgsSettings::NoSection ); | ||
QCOMPARE( qgsSettingsValue, hasGeometry ); | ||
} | ||
|
||
// Check get value | ||
QgsSettings().setFlagValue( settingsKey, pointAndLine, QgsSettings::NoSection ); | ||
QCOMPARE( settingsEntryFlag.value(), pointAndLine ); | ||
|
||
// Check settings type | ||
QCOMPARE( settingsEntryFlag.settingsType(), QgsSettingsEntryBase::Flag ); | ||
|
||
// check that value is stored as string | ||
QCOMPARE( settingsEntryFlag.valueAsVariant().toByteArray(), QMetaEnum::fromType<QgsMapLayerProxyModel::Filters>().valueToKeys( pointAndLine ) ); | ||
|
||
// auto conversion of old settings (int to str) | ||
QSettings().setValue( settingsKey, static_cast<int>( pointAndPolygon ) ); | ||
QCOMPARE( settingsEntryFlag.valueAsVariant().toInt(), pointAndPolygon ); | ||
QCOMPARE( settingsEntryFlag.value(), pointAndPolygon ); | ||
} | ||
|
||
|
||
QGSTEST_MAIN( TestQgsSettingsEntry ) | ||
#include "testqgssettingsentry.moc" |