Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
237 lines (203 sloc) 6.82 KB
/*
* Copyright (C) 2003-2006 Benjamin C Meyer (ben at meyerhome dot net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <QtTest/QtTest>
#include <QtGui/QtGui>
#include <qautoconfig.h>
#include <qconfigdialog.h>
class TestWidget : public QWidget {
Q_OBJECT
public:
TestWidget(QWidget *parent=0);
QCheckBox *checkBox;
QSpinBox *spinBox;
QComboBox *comboBox;
};
TestWidget::TestWidget(QWidget *parent) : QWidget(parent) {
checkBox = new QCheckBox(this);
checkBox->setObjectName("checkBox");
checkBox->setChecked(false);
spinBox = new QSpinBox(this);
spinBox->setObjectName("spinbox");
spinBox->setRange(0,10);
spinBox->setValue(0);
comboBox = new QComboBox(this);
comboBox->setObjectName("combobox");
comboBox->addItem("Number 1");
comboBox->addItem("Number 2");
comboBox->setCurrentIndex(0);
}
class tst_QAutoConfig : public QObject {
Q_OBJECT
public:
tst_QAutoConfig();
~tst_QAutoConfig();
public slots:
void init();
void cleanup();
private slots:
void addWidget();
void ignoreSubWidget();
void namePrefix();
void saveSettings();
void resetSettings();
void reloadSettings();
void customWidgetSignal();
void customUserProperty();
};
tst_QAutoConfig::tst_QAutoConfig()
{
}
tst_QAutoConfig::~tst_QAutoConfig()
{
}
void tst_QAutoConfig::init()
{
}
void tst_QAutoConfig::cleanup()
{
QSettings settings;
QStringList groups = settings.childGroups();
for (int i = 0; i < groups.size(); ++i) {
settings.beginGroup(groups.at(i));
QStringList keys = settings.childKeys();
for (int j = 0; j < keys.size(); ++j)
settings.remove(keys.at(j));
settings.endGroup();
}
}
void tst_QAutoConfig::addWidget()
{
QAutoConfig qautoconfig;
// don't crash
qautoconfig.addWidget(0, "General");
TestWidget testWidget;
qautoconfig.addWidget(&testWidget, "General");
// This will assert
//qautoconfig.addWidget(&testWidget, "General");
}
void tst_QAutoConfig::ignoreSubWidget()
{
QAutoConfig qautoconfig;
TestWidget testWidget;
// Adding the same widget should
qautoconfig.addWidget(&testWidget, "General");
qautoconfig.ignoreSubWidget(testWidget.checkBox);
QCOMPARE(qautoconfig.retrieveSettings(), false);
QSignalSpy spy(&qautoconfig, SIGNAL(settingsChanged()));
qautoconfig.resetSettings();
testWidget.checkBox->setChecked(true);
QCOMPARE(qautoconfig.saveSettings(), false);
QCOMPARE(spy.count(), 0);
// reload and reset
}
void tst_QAutoConfig::namePrefix()
{
QAutoConfig qautoconfig;
QCOMPARE(qautoconfig.namePrefix(), QString(""));
QString newPrefix = "kcfg_";
qautoconfig.setNamePrefix(newPrefix);
QCOMPARE(qautoconfig.namePrefix(), newPrefix);
TestWidget testWidget;
qautoconfig.addWidget(&testWidget, "General");
QCOMPARE(qautoconfig.retrieveSettings(), false);
QCOMPARE(qautoconfig.hasChanged(), false);
testWidget.checkBox->setChecked(true);
QCOMPARE(qautoconfig.hasChanged(), false);
}
void tst_QAutoConfig::saveSettings()
{
QSettings qsettings;
QAutoConfig qautoconfig(&qsettings);
TestWidget testWidget;
qautoconfig.addWidget(&testWidget, "General");
QCOMPARE(qautoconfig.retrieveSettings(true), false);
QCOMPARE(qautoconfig.isDefault(), true);
QSignalSpy spy(&qautoconfig, SIGNAL(settingsChanged()));
QSignalSpy spyWidget(&qautoconfig, SIGNAL(settingsChanged()));
QSignalSpy spyModified(&qautoconfig, SIGNAL(widgetModified()));
QCOMPARE(qautoconfig.saveSettings(), false);
QCOMPARE(qautoconfig.hasChanged(), false);
testWidget.checkBox->setChecked(true);
QCOMPARE(qautoconfig.hasChanged(), true);
QCOMPARE(qautoconfig.saveSettings(), true);
QCOMPARE(spy.count(), 1);
QCOMPARE(spyWidget.count(), 1);
QCOMPARE(spyModified.count(), 1);
QCOMPARE(qautoconfig.hasChanged(), false);
QCOMPARE(qautoconfig.isDefault(), false);
}
void tst_QAutoConfig::resetSettings()
{
QSettings qsettings;
QAutoConfig qautoconfig(&qsettings);
TestWidget testWidget;
qautoconfig.addWidget(&testWidget, "General");
QCOMPARE(qautoconfig.retrieveSettings(), false);
QSignalSpy spyModified(&qautoconfig, SIGNAL(widgetModified()));
QCOMPARE(qautoconfig.isDefault(), true);
testWidget.checkBox->setChecked(true);
QCOMPARE(spyModified.count(), 0);
QCOMPARE(qautoconfig.isDefault(), false);
qautoconfig.resetSettings();
QCOMPARE(qautoconfig.isDefault(), true);
}
void tst_QAutoConfig::reloadSettings()
{
QSettings qsettings;
QAutoConfig qautoconfig(&qsettings);
TestWidget testWidget;
qautoconfig.addWidget(&testWidget, "General");
QCOMPARE(qautoconfig.retrieveSettings(), false);
testWidget.checkBox->setChecked(true);
QCOMPARE(qautoconfig.isDefault(), false);
QCOMPARE(qautoconfig.hasChanged(), true);
qautoconfig.reloadSettings();
QCOMPARE(testWidget.checkBox->isChecked(), false);
QCOMPARE(qautoconfig.hasChanged(), false);
QCOMPARE(qautoconfig.isDefault(), true);
}
void tst_QAutoConfig::customWidgetSignal()
{
QSettings qsettings;
QAutoConfig qautoconfig(&qsettings);
TestWidget testWidget;
qautoconfig.addWidget(&testWidget, "General");
qautoconfig.addWidgetChangedSignal("QComboBox", SIGNAL(destroyed(QObject *)));
QSignalSpy spyModified(&qautoconfig, SIGNAL(widgetModified()));
QCOMPARE(qautoconfig.retrieveSettings(), false);
testWidget.comboBox->setCurrentIndex(1);
testWidget.comboBox->setItemText(0, "changed!");
QCOMPARE(spyModified.count(), 0);
}
void tst_QAutoConfig::customUserProperty()
{
QSettings qsettings;
QAutoConfig qautoconfig(&qsettings);
TestWidget testWidget;
qautoconfig.addWidget(&testWidget, "General");
qautoconfig.setUserProperty( "QComboBox", "currentText" );
QCOMPARE(qautoconfig.retrieveSettings(), false);
testWidget.comboBox->setCurrentIndex(1);
QCOMPARE(qautoconfig.saveSettings(), true);
QSettings settings;
settings.beginGroup("General");
QCOMPARE(settings.value("combobox").toString(), QString("Number 2"));
}
QTEST_MAIN(tst_QAutoConfig)
#include "tst_qautoconfig.moc"
Something went wrong with that request. Please try again.