File indexing completed on 2024-05-12 05:40:53

0001 /***************************************************************************
0002  *   Copyright (C) 2015 by Renaud Guezennec                                *
0003  *   http://renaudguezennec.homelinux.org/accueil,3.html                   *
0004  *                                                                         *
0005  *   Rolisteam is free software; you can redistribute it and/or modify     *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include <QtTest/QtTest>
0021 
0022 #include <preferences/preferenceslistener.h>
0023 #include <preferences/preferencesmanager.h>
0024 
0025 class PreferencesTest : public QObject, public PreferencesListener
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     PreferencesTest();
0031     void preferencesHasChanged(const QString& pref) override;
0032 
0033 private slots:
0034     void testPreferenceRegisterValue();
0035     void initTestCase();
0036     void testNotOverridePreferenceValue();
0037     void testOverridePreferenceValue();
0038     void testLambdaFunction();
0039     void testListener();
0040     void cleanupTestCase();
0041 
0042 private:
0043     PreferencesManager* m_preferences;
0044     int m_count= 0;
0045 };
0046 
0047 PreferencesTest::PreferencesTest() {}
0048 
0049 void PreferencesTest::preferencesHasChanged(const QString& key)
0050 {
0051     QVERIFY(key == "keyListener");
0052     QVERIFY(m_preferences->value(key, 18).toInt() == 25);
0053 }
0054 
0055 void PreferencesTest::testPreferenceRegisterValue()
0056 {
0057     m_preferences->registerValue("key", 300);
0058 
0059     QVERIFY(m_preferences->value("key", 400) == 300);
0060 }
0061 void PreferencesTest::testNotOverridePreferenceValue()
0062 {
0063     m_preferences->registerValue("key1", 300, false);
0064     QVERIFY(m_preferences->value("key1", 400) == 300);
0065     m_preferences->registerValue("key1", 100, false);
0066     QVERIFY(m_preferences->value("key1", 400) == 300);
0067 }
0068 void PreferencesTest::testOverridePreferenceValue()
0069 {
0070     m_preferences->registerValue("key2", 300);
0071     QVERIFY(m_preferences->value("key2", 400) == 300);
0072     m_preferences->registerValue("key2", 100);
0073     QVERIFY(m_preferences->value("key2", 400) == 100);
0074 }
0075 void PreferencesTest::initTestCase()
0076 {
0077     m_preferences= PreferencesManager::getInstance();
0078 }
0079 
0080 void PreferencesTest::testLambdaFunction()
0081 {
0082     m_count= 0;
0083     m_preferences->registerValue("key", 300);
0084     auto func= [this](QVariant value) {
0085         QCOMPARE(value.toInt(), 25);
0086         m_count++;
0087     };
0088     m_preferences->registerLambda("key", func);
0089     m_preferences->registerValue("key", 25);
0090 
0091     QCOMPARE(m_count, 1);
0092 }
0093 
0094 void PreferencesTest::testListener()
0095 {
0096     m_count= 0;
0097     m_preferences->registerValue("keyListener", 800);
0098     m_preferences->registerListener("keyListener", this);
0099     m_preferences->registerValue("keyListener", 25);
0100 }
0101 
0102 void PreferencesTest::cleanupTestCase() {}
0103 
0104 QTEST_MAIN(PreferencesTest);
0105 
0106 #include "tst_preferencestest.moc"