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