1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** @file
3  * @brief Unit tests for the Preferences object
4  *//*
5  * Authors:
6  * see git history
7  *   Krzysztof Kosiński <tweenk.pl@gmail.com>
8  *
9  * Copyright (C) 2016 Authors
10  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11  */
12 
13 #include <cxxtest/TestSuite.h>
14 #include "preferences.h"
15 
16 #include <glibmm/ustring.h>
17 
18 // test observer
19 class TestObserver : public Inkscape::Preferences::Observer {
20 public:
TestObserver(Glib::ustring const & path)21     TestObserver(Glib::ustring const &path) :
22         Inkscape::Preferences::Observer(path),
23         value(0) {}
24 
notify(Inkscape::Preferences::Entry const & val)25     virtual void notify(Inkscape::Preferences::Entry const &val)
26     {
27         value = val.getInt();
28     }
29     int value;
30 };
31 
32 class PreferencesTest : public CxxTest::TestSuite {
33 public:
setUp()34     void setUp() {
35         prefs = Inkscape::Preferences::get();
36     }
tearDown()37     void tearDown() {
38         prefs = NULL;
39         Inkscape::Preferences::unload();
40     }
41 
testStartingState()42     void testStartingState()
43     {
44         TS_ASSERT_DIFFERS(prefs, static_cast<void*>(0));
45         TS_ASSERT_EQUALS(prefs->isWritable(), true);
46     }
47 
testOverwrite()48     void testOverwrite()
49     {
50         prefs->setInt("/test/intvalue", 123);
51         prefs->setInt("/test/intvalue", 321);
52         TS_ASSERT_EQUALS(prefs->getInt("/test/intvalue"), 321);
53     }
54 
testDefaultReturn()55     void testDefaultReturn()
56     {
57         TS_ASSERT_EQUALS(prefs->getInt("/this/path/does/not/exist", 123), 123);
58     }
59 
testLimitedReturn()60     void testLimitedReturn()
61     {
62         prefs->setInt("/test/intvalue", 1000);
63 
64         // simple case
65         TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 0, 500), 123);
66         // the below may seem quirky but this behaviour is intended
67         TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 1001, 5000), 123);
68         // corner cases
69         TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 0, 1000), 1000);
70         TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 1000, 5000), 1000);
71     }
72 
testKeyObserverNotification()73     void testKeyObserverNotification()
74     {
75         Glib::ustring const path = "/some/random/path";
76         TestObserver obs("/some/random");
77         obs.value = 1;
78         prefs->setInt(path, 5);
79         TS_ASSERT_EQUALS(obs.value, 1); // no notifications sent before adding
80 
81         prefs->addObserver(obs);
82         prefs->setInt(path, 10);
83         TS_ASSERT_EQUALS(obs.value, 10);
84         prefs->setInt("/some/other/random/path", 10);
85         TS_ASSERT_EQUALS(obs.value, 10); // value should not change
86 
87         prefs->removeObserver(obs);
88         prefs->setInt(path, 15);
89         TS_ASSERT_EQUALS(obs.value, 10); // no notifications sent after removal
90     }
91 
testEntryObserverNotification()92     void testEntryObserverNotification()
93     {
94         Glib::ustring const path = "/some/random/path";
95         TestObserver obs(path);
96         obs.value = 1;
97         prefs->setInt(path, 5);
98         TS_ASSERT_EQUALS(obs.value, 1); // no notifications sent before adding
99 
100         prefs->addObserver(obs);
101         prefs->setInt(path, 10);
102         TS_ASSERT_EQUALS(obs.value, 10);
103 
104         // test that filtering works properly
105         prefs->setInt("/some/random/value", 1234);
106         TS_ASSERT_EQUALS(obs.value, 10);
107         prefs->setInt("/some/randomvalue", 1234);
108         TS_ASSERT_EQUALS(obs.value, 10);
109         prefs->setInt("/some/random/path2", 1234);
110         TS_ASSERT_EQUALS(obs.value, 10);
111 
112         prefs->removeObserver(obs);
113         prefs->setInt(path, 15);
114         TS_ASSERT_EQUALS(obs.value, 10); // no notifications sent after removal
115     }
116 
testPreferencesEntryMethods()117     void testPreferencesEntryMethods()
118     {
119         prefs->setInt("/test/prefentry", 100);
120         Inkscape::Preferences::Entry val = prefs->getEntry("/test/prefentry");
121         TS_ASSERT(val.isValid());
122         TS_ASSERT_EQUALS(val.getPath(), "/test/prefentry");
123         TS_ASSERT_EQUALS(val.getEntryName(), "prefentry");
124         TS_ASSERT_EQUALS(val.getInt(), 100);
125     }
126 private:
127     Inkscape::Preferences *prefs;
128 };
129 
130 /*
131   Local Variables:
132   mode:c++
133   c-file-style:"stroustrup"
134   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
135   indent-tabs-mode:nil
136   fill-column:99
137   End:
138 */
139 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
140