1 /** @file
2 * @brief Unit tests for the Preferences object
3 */
4 /* Authors:
5 * Krzysztof KosiĆski <tweenk.pl@gmail.com>
6 *
7 * This file is released into the public domain.
8 */
10 #include <cxxtest/TestSuite.h>
11 #include "preferences.h"
13 #include <glibmm/ustring.h>
15 // test observer
16 class TestObserver : public Inkscape::Preferences::Observer {
17 public:
18 TestObserver(Glib::ustring const &path) :
19 Inkscape::Preferences::Observer(path),
20 value(0) {}
22 virtual void notify(Inkscape::Preferences::Entry const &val)
23 {
24 value = val.getInt();
25 }
26 int value;
27 };
29 class PreferencesTest : public CxxTest::TestSuite {
30 public:
31 void setUp() {
32 prefs = Inkscape::Preferences::get();
33 }
34 void tearDown() {
35 prefs = NULL;
36 Inkscape::Preferences::unload();
37 }
39 void testStartingState()
40 {
41 TS_ASSERT(prefs != NULL);
42 TS_ASSERT_EQUALS(prefs->isWritable(), false);
43 }
45 void testOverwrite()
46 {
47 prefs->setInt("/test/intvalue", 123);
48 prefs->setInt("/test/intvalue", 321);
49 TS_ASSERT_EQUALS(prefs->getInt("/test/intvalue"), 321);
50 }
52 void testDefaultReturn()
53 {
54 TS_ASSERT_EQUALS(prefs->getInt("/this/path/does/not/exist", 123), 123);
55 }
57 void testLimitedReturn()
58 {
59 prefs->setInt("/test/intvalue", 1000);
61 // simple case
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);
65 // corner cases
66 TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 0, 1000), 1000);
67 TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 1000, 5000), 1000);
68 }
70 void testKeyObserverNotification()
71 {
72 Glib::ustring const path = "/some/random/path";
73 TestObserver obs("/some/random");
74 obs.value = 1;
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
87 }
89 void testEntryObserverNotification()
90 {
91 Glib::ustring const path = "/some/random/path";
92 TestObserver obs(path);
93 obs.value = 1;
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
112 }
114 void testPreferencesEntryMethods()
115 {
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);
122 }
123 private:
124 Inkscape::Preferences *prefs;
125 };
127 /*
128 Local Variables:
129 mode:c++
130 c-file-style:"stroustrup"
131 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
132 indent-tabs-mode:nil
133 fill-column:99
134 End:
135 */
136 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :