summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test.py285
1 files changed, 254 insertions, 31 deletions
diff --git a/test/test.py b/test/test.py
index cb061ec..2e44a5a 100644
--- a/test/test.py
+++ b/test/test.py
@@ -1,36 +1,259 @@
1''' 1#!/usr/bin/env python
2 Class for the testing suite : 2#
3 - get the list of all test files 3# [SNIPPET_NAME: Pack Box]
4 - create a copy of them on start 4# [SNIPPET_CATEGORIES: PyGTK]
5 - remove the copy on end 5# [SNIPPET_DESCRIPTION: Handling packing]
6''' 6# [SNIPPET_DOCS: http://www.pygtk.org/docs/pygtk/class-gtkbox.html]
7 7
8import shutil 8# example packbox.py
9import os
10import glob
11import sys
12import tempfile
13import unittest
14sys.path.append('..')
15from lib import mat
16 9
17FILE_LIST = zip(glob.glob('clean*'), glob.glob('dirty*')) 10import pygtk
11pygtk.require('2.0')
12import gtk
13import sys, string
18 14
19class MATTest(unittest.TestCase): 15# Helper function that makes a new hbox filled with button-labels. Arguments
20 def setUp(self): 16# for the variables we're interested are passed in to this function. We do
21 '''create working copy of the clean and the dirty file in the TMP dir''' 17# not show the box, but do show everything inside.
22 self.file_list = []
23 _, self.tmpdir = tempfile.mkstemp()
24 18
25 for clean, dirty in FILE_LIST: 19def make_box(homogeneous, spacing, expand, fill, padding):
26 shutil.copy2(clean, self.tmpdir + clean)
27 shutil.copy2(dirty, self.tmpdir + dirty)
28 self.file_list.append((self.tmpdir + clean, self.tmpdir + dirty))
29 20
30 def tearDown(self): 21 # Create a new hbox with the appropriate homogeneous
31 '''Remove the tmp folder''' 22 # and spacing settings
32 for root, dirs, files in os.walk(self.tmpdir, topdown=False): 23 box = gtk.HBox(homogeneous, spacing)
33 for name in files: 24
34 mat.secure_remove(os.path.join(toor, name)) 25 # Create a series of buttons with the appropriate settings
35 for name in dirs: 26 button = gtk.Button("box.pack")
36 os.rmdir(os.path.join(root, name)) 27 box.pack_start(button, expand, fill, padding)
28 button.show()
29
30 button = gtk.Button("(button,")
31 box.pack_start(button, expand, fill, padding)
32 button.show()
33
34 # Create a button with the label depending on the value of
35 # expand.
36 if expand == True:
37 button = gtk.Button("True,")
38 else:
39 button = gtk.Button("False,")
40
41 box.pack_start(button, expand, fill, padding)
42 button.show()
43
44 # This is the same as the button creation for "expand"
45 # above, but uses the shorthand form.
46 button = gtk.Button(("False,", "True,")[fill==True])
47 box.pack_start(button, expand, fill, padding)
48 button.show()
49
50 padstr = "%d)" % padding
51
52 button = gtk.Button(padstr)
53 box.pack_start(button, expand, fill, padding)
54 button.show()
55 return box
56
57class PackBox1:
58 def delete_event(self, widget, event, data=None):
59 gtk.main_quit()
60 return False
61
62 def __init__(self, which):
63
64 # Create our window
65 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
66
67 # You should always remember to connect the delete_event signal
68 # to the main window. This is very important for proper intuitive
69 # behavior
70 self.window.connect("delete_event", self.delete_event)
71 self.window.set_border_width(10)
72
73 # We create a vertical box (vbox) to pack the horizontal boxes into.
74 # This allows us to stack the horizontal boxes filled with buttons one
75 # on top of the other in this vbox.
76 box1 = gtk.VBox(False, 0)
77
78 # which example to show. These correspond to the pictures above.
79 if which == 1:
80 # create a new label.
81 label = gtk.Label("HBox(False, 0)")
82
83 # Align the label to the left side. We'll discuss this method
84 # and others in the section on Widget Attributes.
85 label.set_alignment(0, 0)
86
87 # Pack the label into the vertical box (vbox box1). Remember that
88 # widgets added to a vbox will be packed one on top of the other in
89 # order.
90 box1.pack_start(label, False, False, 0)
91
92 # Show the label
93 label.show()
94
95 # Call our make box function - homogeneous = False, spacing = 0,
96 # expand = False, fill = False, padding = 0
97 box2 = make_box(False, 0, False, False, 0)
98 box1.pack_start(box2, False, False, 0)
99 box2.show()
100
101 # Call our make box function - homogeneous = False, spacing = 0,
102 # expand = True, fill = False, padding = 0
103 box2 = make_box(False, 0, True, False, 0)
104 box1.pack_start(box2, False, False, 0)
105 box2.show()
106
107 # Args are: homogeneous, spacing, expand, fill, padding
108 box2 = make_box(False, 0, True, True, 0)
109 box1.pack_start(box2, False, False, 0)
110 box2.show()
111
112 # Creates a separator, we'll learn more about these later,
113 # but they are quite simple.
114 separator = gtk.HSeparator()
115
116 # Pack the separator into the vbox. Remember each of these
117 # widgets is being packed into a vbox, so they'll be stacked
118 # vertically.
119 box1.pack_start(separator, False, True, 5)
120 separator.show()
121
122 # Create another new label, and show it.
123 label = gtk.Label("HBox(True, 0)")
124 label.set_alignment(0, 0)
125 box1.pack_start(label, False, False, 0)
126 label.show()
127
128 # Args are: homogeneous, spacing, expand, fill, padding
129 box2 = make_box(True, 0, True, False, 0)
130 box1.pack_start(box2, False, False, 0)
131 box2.show()
132
133 # Args are: homogeneous, spacing, expand, fill, padding
134 box2 = make_box(True, 0, True, True, 0)
135 box1.pack_start(box2, False, False, 0)
136 box2.show()
137
138 # Another new separator.
139 separator = gtk.HSeparator()
140 # The last 3 arguments to pack_start are:
141 # expand, fill, padding.
142 box1.pack_start(separator, False, True, 5)
143 separator.show()
144 elif which == 2:
145 # Create a new label, remember box1 is a vbox as created
146 # near the beginning of __init__()
147 label = gtk.Label("HBox(False, 10)")
148 label.set_alignment( 0, 0)
149 box1.pack_start(label, False, False, 0)
150 label.show()
151
152 # Args are: homogeneous, spacing, expand, fill, padding
153 box2 = make_box(False, 10, True, False, 0)
154 box1.pack_start(box2, False, False, 0)
155 box2.show()
156
157 # Args are: homogeneous, spacing, expand, fill, padding
158 box2 = make_box(False, 10, True, True, 0)
159 box1.pack_start(box2, False, False, 0)
160 box2.show()
161
162 separator = gtk.HSeparator()
163 # The last 3 arguments to pack_start are:
164 # expand, fill, padding.
165 box1.pack_start(separator, False, True, 5)
166 separator.show()
167
168 label = gtk.Label("HBox(False, 0)")
169 label.set_alignment(0, 0)
170 box1.pack_start(label, False, False, 0)
171 label.show()
172
173 # Args are: homogeneous, spacing, expand, fill, padding
174 box2 = make_box(False, 0, True, False, 10)
175 box1.pack_start(box2, False, False, 0)
176 box2.show()
177
178 # Args are: homogeneous, spacing, expand, fill, padding
179 box2 = make_box(False, 0, True, True, 10)
180 box1.pack_start(box2, False, False, 0)
181 box2.show()
182
183 separator = gtk.HSeparator()
184 # The last 3 arguments to pack_start are:
185 # expand, fill, padding.
186 box1.pack_start(separator, False, True, 5)
187 separator.show()
188
189 elif which == 3:
190
191 # This demonstrates the ability to use pack_end() to
192 # right justify widgets. First, we create a new box as before.
193 box2 = make_box(False, 0, False, False, 0)
194
195 # Create the label that will be put at the end.
196 label = gtk.Label("end")
197 # Pack it using pack_end(), so it is put on the right
198 # side of the hbox created in the make_box() call.
199 box2.pack_end(label, False, False, 0)
200 # Show the label.
201 label.show()
202
203 # Pack box2 into box1
204 box1.pack_start(box2, False, False, 0)
205 box2.show()
206
207 # A separator for the bottom.
208 separator = gtk.HSeparator()
209
210 # This explicitly sets the separator to 400 pixels wide by 5
211 # pixels high. This is so the hbox we created will also be 400
212 # pixels wide, and the "end" label will be separated from the
213 # other labels in the hbox. Otherwise, all the widgets in the
214 # hbox would be packed as close together as possible.
215 separator.set_size_request(400, 5)
216 # pack the separator into the vbox (box1) created near the start
217 # of __init__()
218 box1.pack_start(separator, False, True, 5)
219 separator.show()
220
221 # Create another new hbox.. remember we can use as many as we need!
222 quitbox = gtk.HBox(False, 0)
223
224 # Our quit button.
225 button = gtk.Button("Quit")
226
227 # Setup the signal to terminate the program when the button is clicked
228 button.connect("clicked", lambda w: gtk.main_quit())
229 # Pack the button into the quitbox.
230 # The last 3 arguments to pack_start are:
231 # expand, fill, padding.
232 quitbox.pack_start(button, True, False, 0)
233 # pack the quitbox into the vbox (box1)
234 box1.pack_start(quitbox, False, False, 0)
235
236 # Pack the vbox (box1) which now contains all our widgets, into the
237 # main window.
238 self.window.add(box1)
239
240 # And show everything left
241 button.show()
242 quitbox.show()
243
244 box1.show()
245 # Showing the window last so everything pops up at once.
246 self.window.show()
247
248def main():
249 # And of course, our main loop.
250 gtk.main()
251 # Control returns here when main_quit() is called
252 return 0
253
254if __name__ =="__main__":
255 if len(sys.argv) != 2:
256 sys.stderr.write("usage: packbox.py num, where num is 1, 2, or 3.\n")
257 sys.exit(1)
258 PackBox1(string.atoi(sys.argv[1]))
259 main()