~centralelyon2010/inkscape/imagelinks2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/** @file
 * @brief Unit tests for the Preferences object
 */
/* Authors:
 *   Krzysztof KosiƄski <tweenk.pl@gmail.com>
 *
 * This file is released into the public domain.
 */

#include <cxxtest/TestSuite.h>
#include "preferences.h"

#include <glibmm/ustring.h>

// test observer
class TestObserver : public Inkscape::Preferences::Observer {
public:
    TestObserver(Glib::ustring const &path) :
        Inkscape::Preferences::Observer(path),
        value(0) {}
    
    virtual void notify(Inkscape::Preferences::Entry const &val)
    {
        value = val.getInt();
    }
    int value;
};

class PreferencesTest : public CxxTest::TestSuite {
public:
    void setUp() {
        prefs = Inkscape::Preferences::get();
    }
    void tearDown() {
        prefs = NULL;
        Inkscape::Preferences::unload();
    }
    
    void testStartingState()
    {
        TS_ASSERT(prefs != NULL);
        TS_ASSERT_EQUALS(prefs->isWritable(), false);
    }
    
    void testOverwrite()
    {
        prefs->setInt("/test/intvalue", 123);
        prefs->setInt("/test/intvalue", 321);
        TS_ASSERT_EQUALS(prefs->getInt("/test/intvalue"), 321);
    }
    
    void testDefaultReturn()
    {
        TS_ASSERT_EQUALS(prefs->getInt("/this/path/does/not/exist", 123), 123);
    }
    
    void testLimitedReturn()
    {
        prefs->setInt("/test/intvalue", 1000);
        
        // simple case
        TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 0, 500), 123);
        // the below may seem quirky but this behaviour is intended
        TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 1001, 5000), 123);
        // corner cases
        TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 0, 1000), 1000);
        TS_ASSERT_EQUALS(prefs->getIntLimited("/test/intvalue", 123, 1000, 5000), 1000);
    }
    
    void testKeyObserverNotification()
    {
        Glib::ustring const path = "/some/random/path";
        TestObserver obs("/some/random");
        obs.value = 1;
        prefs->setInt(path, 5);
        TS_ASSERT_EQUALS(obs.value, 1); // no notifications sent before adding
        
        prefs->addObserver(obs);
        prefs->setInt(path, 10);
        TS_ASSERT_EQUALS(obs.value, 10);
        prefs->setInt("/some/other/random/path", 10);
        TS_ASSERT_EQUALS(obs.value, 10); // value should not change
        
        prefs->removeObserver(obs);
        prefs->setInt(path, 15);
        TS_ASSERT_EQUALS(obs.value, 10); // no notifications sent after removal
    }
    
    void testEntryObserverNotification()
    {
        Glib::ustring const path = "/some/random/path";
        TestObserver obs(path);
        obs.value = 1;
        prefs->setInt(path, 5);
        TS_ASSERT_EQUALS(obs.value, 1); // no notifications sent before adding
        
        prefs->addObserver(obs);
        prefs->setInt(path, 10);
        TS_ASSERT_EQUALS(obs.value, 10);
        
        // test that filtering works properly
        prefs->setInt("/some/random/value", 1234);
        TS_ASSERT_EQUALS(obs.value, 10);
        prefs->setInt("/some/randomvalue", 1234);
        TS_ASSERT_EQUALS(obs.value, 10);
        prefs->setInt("/some/random/path2", 1234);
        TS_ASSERT_EQUALS(obs.value, 10);
        
        prefs->removeObserver(obs);
        prefs->setInt(path, 15);
        TS_ASSERT_EQUALS(obs.value, 10); // no notifications sent after removal
    }
    
    void testPreferencesEntryMethods()
    {
        prefs->setInt("/test/prefentry", 100);
        Inkscape::Preferences::Entry val = prefs->getEntry("/test/prefentry");
        TS_ASSERT(val.isValid());
        TS_ASSERT_EQUALS(val.getPath(), "/test/prefentry");
        TS_ASSERT_EQUALS(val.getEntryName(), "prefentry");
        TS_ASSERT_EQUALS(val.getInt(), 100);
    }
private:
    Inkscape::Preferences *prefs;
};

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :