2
* @brief Unit tests for the Preferences object
5
* Krzysztof Kosiński <tweenk.pl@gmail.com>
7
* This file is released into the public domain.
10
#include <cxxtest/TestSuite.h>
11
#include "preferences.h"
13
#include <glibmm/ustring.h>
16
class TestObserver : public Inkscape::Preferences::Observer {
18
TestObserver(Glib::ustring const &path) :
19
Inkscape::Preferences::Observer(path),
22
virtual void notify(Inkscape::Preferences::Entry const &val)
29
class PreferencesTest : public CxxTest::TestSuite {
32
prefs = Inkscape::Preferences::get();
36
Inkscape::Preferences::unload();
39
void testStartingState()
41
TS_ASSERT(prefs != NULL);
42
TS_ASSERT_EQUALS(prefs->isWritable(), false);
47
prefs->setInt("/test/intvalue", 123);
48
prefs->setInt("/test/intvalue", 321);
49
TS_ASSERT_EQUALS(prefs->getInt("/test/intvalue"), 321);
52
void testDefaultReturn()
54
TS_ASSERT_EQUALS(prefs->getInt("/this/path/does/not/exist", 123), 123);
57
void testLimitedReturn()
59
prefs->setInt("/test/intvalue", 1000);
62
TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 0, 500), 123);
63
// the below may seem quirky but this behaviour is intended
64
TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 1001, 5000), 123);
66
TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 0, 1000), 1000);
67
TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 1000, 5000), 1000);
70
void testKeyObserverNotification()
72
Glib::ustring const path = "/some/random/path";
73
TestObserver obs("/some/random");
75
prefs->setInt(path, 5);
76
TS_ASSERT_EQUALS(obs.value, 1); // no notifications sent before adding
78
prefs->addObserver(obs);
79
prefs->setInt(path, 10);
80
TS_ASSERT_EQUALS(obs.value, 10);
81
prefs->setInt("/some/other/random/path", 10);
82
TS_ASSERT_EQUALS(obs.value, 10); // value should not change
84
prefs->removeObserver(obs);
85
prefs->setInt(path, 15);
86
TS_ASSERT_EQUALS(obs.value, 10); // no notifications sent after removal
89
void testEntryObserverNotification()
91
Glib::ustring const path = "/some/random/path";
92
TestObserver obs(path);
94
prefs->setInt(path, 5);
95
TS_ASSERT_EQUALS(obs.value, 1); // no notifications sent before adding
97
prefs->addObserver(obs);
98
prefs->setInt(path, 10);
99
TS_ASSERT_EQUALS(obs.value, 10);
101
// test that filtering works properly
102
prefs->setInt("/some/random/value", 1234);
103
TS_ASSERT_EQUALS(obs.value, 10);
104
prefs->setInt("/some/randomvalue", 1234);
105
TS_ASSERT_EQUALS(obs.value, 10);
106
prefs->setInt("/some/random/path2", 1234);
107
TS_ASSERT_EQUALS(obs.value, 10);
109
prefs->removeObserver(obs);
110
prefs->setInt(path, 15);
111
TS_ASSERT_EQUALS(obs.value, 10); // no notifications sent after removal
114
void testPreferencesEntryMethods()
116
prefs->setInt("/test/prefentry", 100);
117
Inkscape::Preferences::Entry val = prefs->getEntry("/test/prefentry");
118
TS_ASSERT(val.isValid());
119
TS_ASSERT_EQUALS(val.getPath(), "/test/prefentry");
120
TS_ASSERT_EQUALS(val.getEntryName(), "prefentry");
121
TS_ASSERT_EQUALS(val.getInt(), 100);
124
Inkscape::Preferences *prefs;
130
c-file-style:"stroustrup"
131
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
136
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :