preferences-test.h revision dda29597012dd9033f6bbf3334ab4d7af77a459b
6291N/A/** @file
6291N/A * @brief Unit tests for the Preferences object
6291N/A */
6291N/A/* Authors:
6291N/A * Krzysztof KosiƄski <tweenk.pl@gmail.com>
6291N/A *
6291N/A * This file is released into the public domain.
6291N/A */
6291N/A
6291N/A#include <cxxtest/TestSuite.h>
6291N/A#include "preferences.h"
6291N/A
6291N/A#include <glibmm/ustring.h>
6291N/A
6291N/A// test observer
6291N/Aclass TestObserver : public Inkscape::Preferences::Observer {
6291N/Apublic:
6291N/A TestObserver(Glib::ustring const &path) :
6291N/A Inkscape::Preferences::Observer(path),
6291N/A value(0) {}
6291N/A
6291N/A virtual void notify(Inkscape::Preferences::Entry const &val)
6291N/A {
6291N/A value = val.getInt();
6291N/A }
6291N/A int value;
6291N/A};
6291N/A
6291N/Aclass PreferencesTest : public CxxTest::TestSuite {
6291N/Apublic:
6291N/A void setUp() {
6291N/A prefs = Inkscape::Preferences::get();
6291N/A }
6291N/A void tearDown() {
6291N/A prefs = NULL;
6291N/A Inkscape::Preferences::unload();
6291N/A }
6291N/A
6291N/A void testStartingState()
6291N/A {
6291N/A TS_ASSERT(prefs != NULL);
6291N/A TS_ASSERT_EQUALS(prefs->isWritable(), false);
6291N/A }
6291N/A
6291N/A void testOverwrite()
6291N/A {
6291N/A prefs->setInt("/test/intvalue", 123);
6291N/A prefs->setInt("/test/intvalue", 321);
6291N/A TS_ASSERT_EQUALS(prefs->getInt("/test/intvalue"), 321);
6291N/A }
6291N/A
6291N/A void testDefaultReturn()
6291N/A {
6291N/A TS_ASSERT_EQUALS(prefs->getInt("/this/path/does/not/exist", 123), 123);
6291N/A }
6291N/A
6291N/A void testLimitedReturn()
6291N/A {
6291N/A prefs->setInt("/test/intvalue", 1000);
6291N/A
6291N/A // simple case
6291N/A TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 0, 500), 123);
6291N/A // the below may seem quirky but this behaviour is intended
6291N/A TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 1001, 5000), 123);
6291N/A // corner cases
6291N/A TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 0, 1000), 1000);
6291N/A TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 1000, 5000), 1000);
6291N/A }
6291N/A
6291N/A void testKeyObserverNotification()
6291N/A {
6291N/A Glib::ustring const path = "/some/random/path";
6291N/A TestObserver obs("/some/random");
6291N/A obs.value = 1;
6291N/A prefs->setInt(path, 5);
6291N/A TS_ASSERT_EQUALS(obs.value, 1); // no notifications sent before adding
6291N/A
6291N/A prefs->addObserver(obs);
6291N/A prefs->setInt(path, 10);
6291N/A TS_ASSERT_EQUALS(obs.value, 10);
6291N/A prefs->setInt("some/other/random/path", 10);
6291N/A TS_ASSERT_EQUALS(obs.value, 10); // value should not change
6291N/A
6291N/A prefs->removeObserver(obs);
6291N/A prefs->setInt(path, 15);
6291N/A TS_ASSERT_EQUALS(obs.value, 10); // no notifications sent after removal
6291N/A }
6291N/A
6291N/A void testEntryObserverNotification()
6291N/A {
6291N/A Glib::ustring const path = "/some/random/path";
6291N/A TestObserver obs(path);
6291N/A obs.value = 1;
6291N/A prefs->setInt(path, 5);
6291N/A TS_ASSERT_EQUALS(obs.value, 1); // no notifications sent before adding
6291N/A
6291N/A prefs->addObserver(obs);
6291N/A prefs->setInt(path, 10);
6291N/A TS_ASSERT_EQUALS(obs.value, 10);
6291N/A
6291N/A // test that filtering works properly
6291N/A prefs->setInt("/some/random/value", 1234);
6291N/A TS_ASSERT_EQUALS(obs.value, 10);
6291N/A prefs->setInt("/some/randomvalue", 1234);
6291N/A TS_ASSERT_EQUALS(obs.value, 10);
6291N/A prefs->setInt("/some/random/path2", 1234);
6291N/A TS_ASSERT_EQUALS(obs.value, 10);
6291N/A
6291N/A prefs->removeObserver(obs);
6291N/A prefs->setInt(path, 15);
6291N/A TS_ASSERT_EQUALS(obs.value, 10); // no notifications sent after removal
6291N/A }
6291N/A
6291N/A void testPreferencesEntryMethods()
6291N/A {
6291N/A prefs->setInt("/test/prefentry", 100);
6291N/A Inkscape::Preferences::Entry val = prefs->getEntry("/test/prefentry");
6291N/A TS_ASSERT(val.isValid());
6291N/A TS_ASSERT_EQUALS(val.getPath(), "/test/prefentry");
6291N/A TS_ASSERT_EQUALS(val.getEntryName(), "prefentry");
6291N/A TS_ASSERT_EQUALS(val.getInt(), 100);
6291N/A }
6291N/Aprivate:
6291N/A Inkscape::Preferences *prefs;
6291N/A};
6291N/A
6291N/A/*
6291N/A Local Variables:
6291N/A mode:c++
6291N/A c-file-style:"stroustrup"
6291N/A c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
6291N/A indent-tabs-mode:nil
6291N/A fill-column:99
6291N/A End:
6291N/A*/
6291N/A// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
6291N/A