[Python-checkins] r65990 - in sandbox/trunk/ttk-gsoc/src: 2.x/test/test_treeview.py 2.x/ttk.py 3.x/test/test_treeview.py 3.x/ttk.py

guilherme.polo python-checkins at python.org
Sat Aug 23 15:43:41 CEST 2008


Author: guilherme.polo
Date: Sat Aug 23 15:43:40 2008
New Revision: 65990

Log:
More treeview tests;
Added a notice about the tag_has method availability only in Tk 8.6


Modified:
   sandbox/trunk/ttk-gsoc/src/2.x/test/test_treeview.py
   sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
   sandbox/trunk/ttk-gsoc/src/3.x/test/test_treeview.py
   sandbox/trunk/ttk-gsoc/src/3.x/ttk.py

Modified: sandbox/trunk/ttk-gsoc/src/2.x/test/test_treeview.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/test/test_treeview.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/test/test_treeview.py	Sat Aug 23 15:43:40 2008
@@ -3,6 +3,7 @@
 import ttk
 
 import support
+from test_functions import MockTclObj, MockStateSpec
 
 class TreeviewTest(unittest.TestCase):
 
@@ -12,6 +13,13 @@
     def tearDown(self):
         self.tv.destroy()
 
+    def _simulate_mouse_click(self, x, y):
+        # Generate proper events (trying to act like an X server).
+        self.tv.event_generate('<Enter>', x=0, y=0)
+        self.tv.event_generate('<Motion>', x=x, y=y)
+        self.tv.event_generate('<ButtonPress-1>', x=x, y=y)
+        self.tv.event_generate('<ButtonRelease-1>', x=x, y=y)
+
 
     def test_bbox(self):
         self.tv.pack()
@@ -191,16 +199,13 @@
         self.failUnlessRaises(Tkinter.TclError, self.tv.heading, '#0',
             anchor=1)
 
+
     def test_heading_callback(self):
         def callback():
             success.append(True)
 
         def simulate_heading_click(x, y):
-            # Generate proper events (trying to act like an X server).
-            self.tv.event_generate('<Enter>', x=0, y=0)
-            self.tv.event_generate('<Motion>', x=x, y=y)
-            self.tv.event_generate('<ButtonPress-1>', x=x, y=y)
-            self.tv.event_generate('<ButtonRelease-1>', x=x, y=y)
+            self._simulate_mouse_click(x, y)
             self.tv.update_idletasks()
 
         self.tv.pack()
@@ -216,9 +221,174 @@
             self.fail("The command associated to the treeview heading wasn't "
                 "invoked.")
 
-        # XXX missing tests to check what happens when command is a string
-        # and some others
+        success = []
+        commands = self.tv.master._tclCommands
+        self.tv.heading('#0', command=self.tv.heading('#0', command=None))
+        self.failUnlessEqual(commands, self.tv.master._tclCommands)
+        simulate_heading_click(5, 5)
+        if not success:
+            self.fail("The command associated to the treeview heading wasn't "
+                "invoked.")
 
+        # XXX The following raises an error in a tcl interpreter, but not in
+        # Python
+        #self.tv.heading('#0', command='I dont exist')
+        #simulate_heading_click(5, 5)
+
+
+    def test_index(self):
+        # item 'what' doesn't exist
+        self.failUnlessRaises(Tkinter.TclError, self.tv.index, 'what')
+
+        self.failUnlessEqual(self.tv.index(''), 0)
+
+        item1 = self.tv.insert('', 'end')
+        item2 = self.tv.insert('', 'end')
+        c1 = self.tv.insert(item1, 'end')
+        c2 = self.tv.insert(item1, 'end')
+        self.failUnlessEqual(self.tv.index(item1), 0)
+        self.failUnlessEqual(self.tv.index(c1), 0)
+        self.failUnlessEqual(self.tv.index(c2), 1)
+        self.failUnlessEqual(self.tv.index(item2), 1)
+
+        self.tv.move(item2, '', 0)
+        self.failUnlessEqual(self.tv.index(item2), 0)
+        self.failUnlessEqual(self.tv.index(item1), 1)
+
+        # check that index still works even after its parent and siblings
+        # have been detached
+        self.tv.detach(item1)
+        self.failUnlessEqual(self.tv.index(c2), 1)
+        self.tv.detach(c1)
+        self.failUnlessEqual(self.tv.index(c2), 0)
+
+        # but it fails after item has been deleted
+        self.tv.delete(item1)
+        self.failUnlessRaises(Tkinter.TclError, self.tv.index, c2)
+
+
+    def test_insert_item(self):
+        # parent 'none' doesn't exist
+        self.failUnlessRaises(Tkinter.TclError, self.tv.insert, 'none', 'end')
+
+        # open values
+        self.failUnlessRaises(Tkinter.TclError, self.tv.insert, '', 'end',
+            open='')
+        self.failUnlessRaises(Tkinter.TclError, self.tv.insert, '', 'end',
+            open='please')
+        self.failIf(self.tv.delete(self.tv.insert('', 'end', open=True)))
+        self.failIf(self.tv.delete(self.tv.insert('', 'end', open=False)))
+
+        # invalid index
+        self.failUnlessRaises(Tkinter.TclError, self.tv.insert, '', 'middle')
+
+        # trying to duplicate item id is invalid
+        itemid = self.tv.insert('', 'end', 'first-item')
+        self.failUnlessEqual(itemid, 'first-item')
+        self.failUnlessRaises(Tkinter.TclError, self.tv.insert, '', 'end',
+            'first-item')
+        self.failUnlessRaises(Tkinter.TclError, self.tv.insert, '', 'end',
+            MockTclObj('first-item'))
+
+        # unicode values
+        value = u'\xe1ba'
+        item = self.tv.insert('', 'end', values=(value, ))
+        self.failUnlessEqual(self.tv.item(item, values=None), (value, ))
+
+        self.tv.item(item, values=list(self.tv.item(item, values=None)))
+        self.failUnlessEqual(self.tv.item(item, values=None), (value, ))
+
+        self.failUnless(isinstance(self.tv.item(item), dict))
+
+        # erase item values
+        self.tv.item(item, values='')
+        self.failIf(self.tv.item(item, values=None))
+
+        # item tags
+        item = self.tv.insert('', 'end', tags=[1, 2, value])
+        self.failUnlessEqual(self.tv.item(item, tags=None), ('1', '2', value))
+        self.tv.item(item, tags=[])
+        self.failIf(self.tv.item(item, tags=None))
+        self.tv.item(item, tags=(1, 2))
+        self.failUnlessEqual(self.tv.item(item, tags=None), ('1', '2'))
+
+        # values with spaces
+        item = self.tv.insert('', 'end', values=('a b c',
+            '%s %s' % (value, value)))
+        self.failUnlessEqual(self.tv.item(item, values=None),
+            ('a b c', '%s %s' % (value, value)))
+
+        # text
+        self.failUnlessEqual(self.tv.item(
+            self.tv.insert('', 'end', text="Label here"), text=None),
+            "Label here")
+        self.failUnlessEqual(self.tv.item(
+            self.tv.insert('', 'end', text=value), text=None),
+            value)
+
+
+    def test_set(self):
+        self.tv['columns'] = ['A', 'B']
+        item = self.tv.insert('', 'end', values=['a', 'b'])
+        self.failUnlessEqual(self.tv.set(item), {'A': 'a', 'B': 'b'})
+
+        self.tv.set(item, 'B', 'a')
+        self.failUnlessEqual(self.tv.item(item, values=None), ('a', 'a'))
+
+        self.tv['columns'] = ['B']
+        self.failUnlessEqual(self.tv.set(item), {'B': 'a'})
+
+        self.tv.set(item, 'B', 'b')
+        self.failUnlessEqual(self.tv.set(item, column='B'), 'b')
+        self.failUnlessEqual(self.tv.item(item, values=None), ('b', 'a'))
+
+        self.tv.set(item, 'B', 123)
+        self.failUnlessEqual(self.tv.set(item, 'B'), 123)
+        self.failUnlessEqual(self.tv.item(item, values=None), (123, 'a'))
+        self.failUnlessEqual(self.tv.set(item), {'B': 123})
+
+        # inexistant column
+        self.failUnlessRaises(Tkinter.TclError, self.tv.set, item, 'A')
+        self.failUnlessRaises(Tkinter.TclError, self.tv.set, item, 'A', 'b')
+
+        # inexistant item
+        self.failUnlessRaises(Tkinter.TclError, self.tv.set, 'notme')
+
+
+    def test_tag_bind(self):
+        def cb_test(event):
+            events.append(1)
+        def cb_test2(event):
+            events.append(2)
+        events = []
+        item1 = self.tv.insert('', 'end', tags=['call'])
+        item2 = self.tv.insert('', 'end', tags=['call'])
+        self.tv.tag_bind('call', '<ButtonPress-1>', cb_test)
+        self.tv.tag_bind('call', '<ButtonRelease-1>', cb_test2)
+
+        self.tv.pack()
+        self.tv.wait_visibility()
+        self.tv.update()
+
+        pos_y = set()
+        found = set()
+        for i in range(0, 100, 10):
+            if len(found) == 2: # item1 and item2 already found
+                break
+            item_id = self.tv.identify_row(i)
+            if item_id and item_id not in found:
+                pos_y.add(i)
+                found.add(item_id)
+
+        self.failUnlessEqual(len(pos_y), 2) # item1 and item2 y pos
+        for y in pos_y:
+            self._simulate_mouse_click(0, y)
+
+        # by now there should be 4 things in the events list, since each
+        # item had a bind for two events that were simulated above
+        self.failUnlessEqual(len(events), 4)
+        for evt in zip(events[::2], events[1::2]):
+            self.failUnlessEqual(evt, (1, 2))
 
 
 def test_main():

Modified: sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	Sat Aug 23 15:43:40 2008
@@ -1410,7 +1410,9 @@
     def tag_has(self, tagname, item=None):
         """If item is specified, returns 1 or 0 depending on whether the
         specified item has the given tagname. Otherwise, returns a list of
-        all items which have the specified tag."""
+        all items which have the specified tag.
+
+        * Availability: Tk 8.6"""
         return self.tk.call(self._w, "tag", "has", tagname, item)
 
 

Modified: sandbox/trunk/ttk-gsoc/src/3.x/test/test_treeview.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/test/test_treeview.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/test/test_treeview.py	Sat Aug 23 15:43:40 2008
@@ -3,6 +3,7 @@
 import ttk
 
 import support
+from test_functions import MockTclObj, MockStateSpec
 
 class TreeviewTest(unittest.TestCase):
 
@@ -12,6 +13,13 @@
     def tearDown(self):
         self.tv.destroy()
 
+    def _simulate_mouse_click(self, x, y):
+        # Generate proper events (trying to act like an X server).
+        self.tv.event_generate('<Enter>', x=0, y=0)
+        self.tv.event_generate('<Motion>', x=x, y=y)
+        self.tv.event_generate('<ButtonPress-1>', x=x, y=y)
+        self.tv.event_generate('<ButtonRelease-1>', x=x, y=y)
+
 
     def test_bbox(self):
         self.tv.pack()
@@ -191,16 +199,13 @@
         self.failUnlessRaises(tkinter.TclError, self.tv.heading, '#0',
             anchor=1)
 
+
     def test_heading_callback(self):
         def callback():
             success.append(True)
 
         def simulate_heading_click(x, y):
-            # Generate proper events (trying to act like an X server).
-            self.tv.event_generate('<Enter>', x=0, y=0)
-            self.tv.event_generate('<Motion>', x=x, y=y)
-            self.tv.event_generate('<ButtonPress-1>', x=x, y=y)
-            self.tv.event_generate('<ButtonRelease-1>', x=x, y=y)
+            self._simulate_mouse_click(x, y)
             self.tv.update_idletasks()
 
         self.tv.pack()
@@ -216,9 +221,174 @@
             self.fail("The command associated to the treeview heading wasn't "
                 "invoked.")
 
-        # XXX missing tests to check what happens when command is a string
-        # and some others
+        success = []
+        commands = self.tv.master._tclCommands
+        self.tv.heading('#0', command=self.tv.heading('#0', command=None))
+        self.failUnlessEqual(commands, self.tv.master._tclCommands)
+        simulate_heading_click(5, 5)
+        if not success:
+            self.fail("The command associated to the treeview heading wasn't "
+                "invoked.")
 
+        # XXX The following raises an error in a tcl interpreter, but not in
+        # Python
+        #self.tv.heading('#0', command='I dont exist')
+        #simulate_heading_click(5, 5)
+
+
+    def test_index(self):
+        # item 'what' doesn't exist
+        self.failUnlessRaises(tkinter.TclError, self.tv.index, 'what')
+
+        self.failUnlessEqual(self.tv.index(''), 0)
+
+        item1 = self.tv.insert('', 'end')
+        item2 = self.tv.insert('', 'end')
+        c1 = self.tv.insert(item1, 'end')
+        c2 = self.tv.insert(item1, 'end')
+        self.failUnlessEqual(self.tv.index(item1), 0)
+        self.failUnlessEqual(self.tv.index(c1), 0)
+        self.failUnlessEqual(self.tv.index(c2), 1)
+        self.failUnlessEqual(self.tv.index(item2), 1)
+
+        self.tv.move(item2, '', 0)
+        self.failUnlessEqual(self.tv.index(item2), 0)
+        self.failUnlessEqual(self.tv.index(item1), 1)
+
+        # check that index still works even after its parent and siblings
+        # have been detached
+        self.tv.detach(item1)
+        self.failUnlessEqual(self.tv.index(c2), 1)
+        self.tv.detach(c1)
+        self.failUnlessEqual(self.tv.index(c2), 0)
+
+        # but it fails after item has been deleted
+        self.tv.delete(item1)
+        self.failUnlessRaises(tkinter.TclError, self.tv.index, c2)
+
+
+    def test_insert_item(self):
+        # parent 'none' doesn't exist
+        self.failUnlessRaises(tkinter.TclError, self.tv.insert, 'none', 'end')
+
+        # open values
+        self.failUnlessRaises(tkinter.TclError, self.tv.insert, '', 'end',
+            open='')
+        self.failUnlessRaises(tkinter.TclError, self.tv.insert, '', 'end',
+            open='please')
+        self.failIf(self.tv.delete(self.tv.insert('', 'end', open=True)))
+        self.failIf(self.tv.delete(self.tv.insert('', 'end', open=False)))
+
+        # invalid index
+        self.failUnlessRaises(tkinter.TclError, self.tv.insert, '', 'middle')
+
+        # trying to duplicate item id is invalid
+        itemid = self.tv.insert('', 'end', 'first-item')
+        self.failUnlessEqual(itemid, 'first-item')
+        self.failUnlessRaises(tkinter.TclError, self.tv.insert, '', 'end',
+            'first-item')
+        self.failUnlessRaises(tkinter.TclError, self.tv.insert, '', 'end',
+            MockTclObj('first-item'))
+
+        # unicode values
+        value = '\xe1ba'
+        item = self.tv.insert('', 'end', values=(value, ))
+        self.failUnlessEqual(self.tv.item(item, values=None), (value, ))
+
+        self.tv.item(item, values=list(self.tv.item(item, values=None)))
+        self.failUnlessEqual(self.tv.item(item, values=None), (value, ))
+
+        self.failUnless(isinstance(self.tv.item(item), dict))
+
+        # erase item values
+        self.tv.item(item, values='')
+        self.failIf(self.tv.item(item, values=None))
+
+        # item tags
+        item = self.tv.insert('', 'end', tags=[1, 2, value])
+        self.failUnlessEqual(self.tv.item(item, tags=None), ('1', '2', value))
+        self.tv.item(item, tags=[])
+        self.failIf(self.tv.item(item, tags=None))
+        self.tv.item(item, tags=(1, 2))
+        self.failUnlessEqual(self.tv.item(item, tags=None), ('1', '2'))
+
+        # values with spaces
+        item = self.tv.insert('', 'end', values=('a b c',
+            '%s %s' % (value, value)))
+        self.failUnlessEqual(self.tv.item(item, values=None),
+            ('a b c', '%s %s' % (value, value)))
+
+        # text
+        self.failUnlessEqual(self.tv.item(
+            self.tv.insert('', 'end', text="Label here"), text=None),
+            "Label here")
+        self.failUnlessEqual(self.tv.item(
+            self.tv.insert('', 'end', text=value), text=None),
+            value)
+
+
+    def test_set(self):
+        self.tv['columns'] = ['A', 'B']
+        item = self.tv.insert('', 'end', values=['a', 'b'])
+        self.failUnlessEqual(self.tv.set(item), {'A': 'a', 'B': 'b'})
+
+        self.tv.set(item, 'B', 'a')
+        self.failUnlessEqual(self.tv.item(item, values=None), ('a', 'a'))
+
+        self.tv['columns'] = ['B']
+        self.failUnlessEqual(self.tv.set(item), {'B': 'a'})
+
+        self.tv.set(item, 'B', 'b')
+        self.failUnlessEqual(self.tv.set(item, column='B'), 'b')
+        self.failUnlessEqual(self.tv.item(item, values=None), ('b', 'a'))
+
+        self.tv.set(item, 'B', 123)
+        self.failUnlessEqual(self.tv.set(item, 'B'), 123)
+        self.failUnlessEqual(self.tv.item(item, values=None), (123, 'a'))
+        self.failUnlessEqual(self.tv.set(item), {'B': 123})
+
+        # inexistant column
+        self.failUnlessRaises(tkinter.TclError, self.tv.set, item, 'A')
+        self.failUnlessRaises(tkinter.TclError, self.tv.set, item, 'A', 'b')
+
+        # inexistant item
+        self.failUnlessRaises(tkinter.TclError, self.tv.set, 'notme')
+
+
+    def test_tag_bind(self):
+        def cb_test(event):
+            events.append(1)
+        def cb_test2(event):
+            events.append(2)
+        events = []
+        item1 = self.tv.insert('', 'end', tags=['call'])
+        item2 = self.tv.insert('', 'end', tags=['call'])
+        self.tv.tag_bind('call', '<ButtonPress-1>', cb_test)
+        self.tv.tag_bind('call', '<ButtonRelease-1>', cb_test2)
+
+        self.tv.pack()
+        self.tv.wait_visibility()
+        self.tv.update()
+
+        pos_y = set()
+        found = set()
+        for i in range(0, 100, 10):
+            if len(found) == 2: # item1 and item2 already found
+                break
+            item_id = self.tv.identify_row(i)
+            if item_id and item_id not in found:
+                pos_y.add(i)
+                found.add(item_id)
+
+        self.failUnlessEqual(len(pos_y), 2) # item1 and item2 y pos
+        for y in pos_y:
+            self._simulate_mouse_click(0, y)
+
+        # by now there should be 4 things in the events list, since each
+        # item had a bind for two events that were simulated above
+        self.failUnlessEqual(len(events), 4)
+        for evt in zip(events[::2], events[1::2]):
+            self.failUnlessEqual(evt, (1, 2))
 
 
 def test_main():

Modified: sandbox/trunk/ttk-gsoc/src/3.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	Sat Aug 23 15:43:40 2008
@@ -1410,7 +1410,9 @@
     def tag_has(self, tagname, item=None):
         """If item is specified, returns 1 or 0 depending on whether the
         specified item has the given tagname. Otherwise, returns a list of
-        all items which have the specified tag."""
+        all items which have the specified tag.
+        
+        * Availability: Tk 8.6"""
         return self.tk.call(self._w, "tag", "has", tagname, item)
 
 


More information about the Python-checkins mailing list