@@ -235,39 +235,47 @@ def tearDownClass(cls):
235235 class DummyRMenu :
236236 def tk_popup (x , y ): pass
237237
238- def click (self , index ):
239- "Simulate a right click at the start of index; return the event."
240- x , y = self .text .bbox (index )[:2 ]
238+ def click (self , x = 0 , y = 0 ):
239+ """Simulate a right click at the (x, y) pixel of the text.
240+
241+ Return the index of the clicked character, as computed by the
242+ widget itself. It cannot be computed here, because the geometry
243+ of the text is unknown while its window is not mapped.
244+ """
245+ index = self .text .index (f'@{ x } ,{ y } ' )
241246 Event = namedtuple ('Event' , ['x' , 'y' , 'x_root' , 'y_root' ])
242247 event = Event (x , y , x_root = 0 , y_root = 0 )
243248 self .assertEqual (self .window .right_menu_event (event ), 'break' )
244- return event
249+ return index
245250
246251 def test_rclick_no_selection (self ):
247252 text = self .text
248253 insert (text , 'one two three' )
249- self .click ('1.8' )
254+ index = self .click ()
250255 self .assertEqual (text .tag_ranges ('sel' ), ())
251- self .assertEqual (text .index ('insert' ), '1.8' )
256+ self .assertEqual (text .index ('insert' ), index )
252257
253258 def test_rclick_outside_selection (self ):
254259 text = self .text
255260 insert (text , 'one two three' )
256- text .tag_add ('sel' , '1.0' , '1.3' )
257- text .mark_set ('insert' , '1.3' )
258- self .click ('1.8' )
261+ # The selection does not contain the clicked character.
262+ text .tag_add ('sel' , '1.5' , '1.8' )
263+ text .mark_set ('insert' , '1.8' )
264+ index = self .click ()
259265 self .assertEqual (text .tag_ranges ('sel' ), ())
260- self .assertEqual (text .index ('insert' ), '1.8' )
266+ self .assertEqual (text .index ('insert' ), index )
261267
262268 def test_rclick_inside_selection (self ):
263269 text = self .text
264270 insert (text , 'one two three' )
265- text .tag_add ('sel' , '1.4' , '1.7' )
266- text .mark_set ('insert' , '1.7' )
267- self .click ('1.5' )
268- self .assertEqual (text .index ('sel.first' ), '1.4' )
269- self .assertEqual (text .index ('sel.last' ), '1.7' )
270- self .assertEqual (text .index ('insert' ), '1.7' )
271+ # The selection contains the clicked character.
272+ index = text .index ('@0,0' )
273+ text .tag_add ('sel' , index , f'{ index } +3c' )
274+ text .mark_set ('insert' , 'end-1c' )
275+ self .click ()
276+ self .assertEqual (text .index ('sel.first' ), index )
277+ self .assertEqual (text .index ('sel.last' ), text .index (f'{ index } +3c' ))
278+ self .assertEqual (text .index ('insert' ), text .index ('end-1c' ))
271279
272280 def test_rmenu_check_copy (self ):
273281 text = self .text
0 commit comments