~ubuntu-branches/ubuntu/jaunty/pida/jaunty

« back to all changes in this revision

Viewing changes to rat/testutil.py

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-08-01 13:08:56 UTC
  • mfrom: (0.1.2 etch) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060801130856-v92ktopgdxc8rv7q
Tags: 0.3.1-2ubuntu1
* Re-sync with Debian
* Remove bashisms from debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
__license__ = "MIT <http://www.opensource.org/licenses/mit-license.php>"
 
2
__author__ = "Tiago Cogumbreiro <cogumbreiro@users.sf.net>"
 
3
__copyright__ = "Copyright 2005, Tiago Cogumbreiro"
 
4
 
 
5
import unittest
 
6
import gtk
 
7
import gobject
 
8
from util import *
 
9
 
 
10
list_children = lambda *args, **kwargs: list(iterate_widget_children(*args, **kwargs))
 
11
list_parents = lambda *args, **kwargs: list(iterate_widget_parents(*args, **kwargs))
 
12
count_children = lambda *args, **kwargs: len(list_children(*args, **kwargs))
 
13
count_parents = lambda *args, **kwargs: len(list_parents(*args, **kwargs))
 
14
 
 
15
class TestWidgetIterators(unittest.TestCase):
 
16
    def test_count_children(self):
 
17
        container = gtk.VBox()
 
18
 
 
19
        # The number of children of the container starts at empty
 
20
        self.assertEqual(count_children(container), 0)
 
21
        # The number of children of the container starts at empty
 
22
        self.assertEqual(count_children(container, recurse_children=True), 0)
 
23
 
 
24
        container2 = gtk.VBox()
 
25
        # Add a sub-container to a container
 
26
        container.add(container2)
 
27
        # It affects the container where it was added
 
28
        
 
29
        self.assertEqual(count_children(container), 1)
 
30
        assert list_children(container)[0] is container2
 
31
        
 
32
        # It affects the container where it was added
 
33
        self.assertEqual(count_children(container, recurse_children=True), 1)
 
34
        assert list_children(container, recurse_children=True)[0] is container2
 
35
        
 
36
        # The number of children of the sub-container starts at empty
 
37
        self.assertEqual(count_children(container2), 0)
 
38
        # The number of children of the sub-container starts at empty
 
39
        self.assertEqual(count_children(container2, recurse_children=True), 0)
 
40
 
 
41
        # Adding a container in the sub-container
 
42
        lbl1 = gtk.Label()
 
43
        container2.add(lbl1)
 
44
        # It does not affect the children of main container
 
45
        self.assertEqual(count_children(container), 1)
 
46
        self.assertEqual(list_children(container), [container2])
 
47
        
 
48
        # It affects the list of all children (recurring)
 
49
        self.assertEqual(count_children(container, recurse_children=True), 2)
 
50
        self.assertEqual(list_children(container, recurse_children=True), [container2, lbl1])
 
51
 
 
52
        # It affects the container where it was added
 
53
        self.assertEqual(count_children(container2), 1)
 
54
        self.assertEqual(list_children(container2), [lbl1])
 
55
        
 
56
        # It affects the container where it was added
 
57
        self.assertEqual(count_children(container2, recurse_children=True), 1)
 
58
        self.assertEqual(list_children(container2, recurse_children=True), [lbl1])
 
59
 
 
60
        # Adding a child to the main container should not affect the count of elements
 
61
        lbl2 = gtk.Label()
 
62
        container.add(lbl2)
 
63
        self.assertEqual(count_children(container), 2)
 
64
        self.assertEqual(list_children(container), [container2, lbl2])
 
65
        
 
66
        self.assertEqual(count_children(container, recurse_children=True), 3)
 
67
        self.assertEqual(list_children(container, recurse_children=True), [container2, lbl1, lbl2])
 
68
 
 
69
        self.assertEqual(count_children(container2), 1)
 
70
        self.assertEqual(list_children(container2), [lbl1])
 
71
 
 
72
        self.assertEqual(count_children(container2, recurse_children=True), 1)
 
73
        self.assertEqual(list_children(container2, recurse_children=True), [lbl1])
 
74
 
 
75
    def test_iterate_parents(self):
 
76
        vbox1 = gtk.VBox()
 
77
        self.assertEqual(list_parents(vbox1),[])
 
78
        
 
79
        vbox2 = gtk.VBox()
 
80
        vbox1.add(vbox2)
 
81
        self.assertEqual(list_parents(vbox2),[vbox1])
 
82
 
 
83
        vbox3 = gtk.VBox()
 
84
        vbox1.add(vbox3)
 
85
        self.assertEqual(list_parents(vbox3),[vbox1])
 
86
 
 
87
        vbox4 = gtk.VBox()
 
88
        vbox2.add(vbox4)
 
89
        self.assertEqual(list_parents(vbox4),[vbox2, vbox1])
 
90
 
 
91
        vbox5 = gtk.VBox()
 
92
        vbox4.add(vbox5)
 
93
        self.assertEqual(list_parents(vbox5),[vbox4, vbox2, vbox1])
 
94
   
 
95
    def test_get_root_parent(self):
 
96
        # Adding widgets in different depths maintains the root widget
 
97
        vbox1 = gtk.VBox()
 
98
        self.assertEqual(get_root_parent(vbox1), None)
 
99
 
 
100
        vbox2 = gtk.VBox()
 
101
        vbox1.add(vbox2)
 
102
        self.assertEqual(get_root_parent(vbox2), vbox1)
 
103
 
 
104
        vbox3 = gtk.VBox()
 
105
        vbox2.add(vbox3)
 
106
        self.assertEqual(get_root_parent(vbox3), vbox1)
 
107
        self.assertEqual(get_root_parent(vbox2), vbox1)
 
108
 
 
109
    def test_find_child_widget(self):
 
110
        w1 = gtk.VBox()
 
111
        w1.set_name("w1")
 
112
        self.assertEqual(find_child_widget(w1, "w1"), w1)
 
113
        self.assertEqual(find_child_widget(w1, "foo"), None)
 
114
        
 
115
        w2 = gtk.VBox()
 
116
        w2.set_name("w2")
 
117
        w1.add(w2)
 
118
        self.assertEqual(find_child_widget(w1, "w2"), w2)
 
119
        
 
120
        w3 = gtk.VBox()
 
121
        w3.set_name("w3")
 
122
        w2.add(w3)
 
123
        self.assertEqual(find_child_widget(w1, "w3"), w3)
 
124
        self.assertEqual(find_child_widget(w2, "w3"), w3)
 
125
        self.assertEqual(find_child_widget(w3, "w3"), w3)
 
126
 
 
127
    def test_find_parent_widget(self):
 
128
        w1 = gtk.VBox()
 
129
        w1.set_name("w1")
 
130
        self.assertEqual(find_parent_widget(w1, "w1"), w1)
 
131
        self.assertEqual(find_parent_widget(w1, "w1", find_self=False), None)
 
132
        self.assertEqual(find_parent_widget(w1, "foo"), None)
 
133
        
 
134
        w2 = gtk.VBox()
 
135
        w2.set_name("w2")
 
136
        w1.add(w2)
 
137
        self.assertEqual(find_parent_widget(w2, "w1"), w1)
 
138
        
 
139
        w3 = gtk.VBox()
 
140
        w3.set_name("w3")
 
141
        w2.add(w3)
 
142
        self.assertEqual(find_parent_widget(w1, "w1"), w1)
 
143
        self.assertEqual(find_parent_widget(w1, "w1", find_self=False), None)
 
144
        self.assertEqual(find_parent_widget(w2, "w1"), w1)
 
145
        self.assertEqual(find_parent_widget(w3, "w1"), w1)
 
146
    
 
147
    def test_ListSpec(self):
 
148
        spec = ListSpec(("A", gobject.TYPE_STRING), ("B", gobject.TYPE_INT))
 
149
        self.assertEqual(spec.A, 0)
 
150
        self.assertEqual(spec.B, 1)
 
151
        
 
152
        store = spec.create_list_store()
 
153
        self.assertEqual(store.get_n_columns(), 2)
 
154
        self.assertEqual(store.get_column_type(spec.A), gobject.TYPE_STRING)
 
155
        self.assertEqual(store.get_column_type(spec.B), gobject.TYPE_INT)
 
156
        
 
157
        row = spec.to_tree_row({spec.A: "foo", spec.B: 1})
 
158
        self.assertEqual(row, ["foo", 1])
 
159
        store.append(row)
 
160
        
 
161
        row = store[0]
 
162
        self.assertEqual(row[spec.A], "foo")
 
163
        self.assertEqual(row[spec.B], 1)
 
164
 
 
165
 
 
166
def main():
 
167
    unittest.main()
 
168
 
 
169
if __name__ == '__main__':
 
170
    main()
 
 
b'\\ No newline at end of file'