1'''Test (selected) IDLE Edit menu items.
2
3Edit modules have their own test files files
4'''
5from test.test_support import requires
6import Tkinter as tk
7import unittest
8from idlelib import PyShell
9
10
11class PasteTest(unittest.TestCase):
12    '''Test pasting into widgets that allow pasting.
13
14    On X11, replacing selections requires tk fix.
15    '''
16
17    @classmethod
18    def setUpClass(cls):
19        requires('gui')
20        cls.root = root = tk.Tk()
21        root.withdraw()
22        PyShell.fix_x11_paste(root)
23        cls.text = tk.Text(root)
24        cls.entry = tk.Entry(root)
25        cls.spin = tk.Spinbox(root)
26        root.clipboard_clear()
27        root.clipboard_append('two')
28
29    @classmethod
30    def tearDownClass(cls):
31        del cls.text, cls.entry, cls.spin
32        cls.root.clipboard_clear()
33        cls.root.update_idletasks()
34        cls.root.update()
35        cls.root.destroy()
36        del cls.root
37
38    def test_paste_text_no_selection(self):
39        "Test pasting into text without a selection."
40        text = self.text
41        tag, ans = '', 'onetwo\n'
42        text.delete('1.0', 'end')
43        text.insert('1.0', 'one', tag)
44        text.event_generate('<<Paste>>')
45        self.assertEqual(text.get('1.0', 'end'), ans)
46
47    def test_paste_text_selection(self):
48        "Test pasting into text with a selection."
49        text = self.text
50        tag, ans = 'sel', 'two\n'
51        text.delete('1.0', 'end')
52        text.insert('1.0', 'one', tag)
53        text.event_generate('<<Paste>>')
54        self.assertEqual(text.get('1.0', 'end'), ans)
55
56    def test_paste_entry_no_selection(self):
57        "Test pasting into an entry without a selection."
58        # On 3.6, generated <<Paste>> fails without empty select range
59        # for 'no selection'.  Live widget works fine.
60        entry = self.entry
61        end, ans = 0, 'onetwo'
62        entry.delete(0, 'end')
63        entry.insert(0, 'one')
64        entry.select_range(0, end)  # see note
65        entry.event_generate('<<Paste>>')
66        self.assertEqual(entry.get(), ans)
67
68    def test_paste_entry_selection(self):
69        "Test pasting into an entry with a selection."
70        entry = self.entry
71        end, ans = 'end', 'two'
72        entry.delete(0, 'end')
73        entry.insert(0, 'one')
74        entry.select_range(0, end)
75        entry.event_generate('<<Paste>>')
76        self.assertEqual(entry.get(), ans)
77
78    def test_paste_spin_no_selection(self):
79        "Test pasting into a spinbox without a selection."
80        # See note above for entry.
81        spin = self.spin
82        end, ans = 0, 'onetwo'
83        spin.delete(0, 'end')
84        spin.insert(0, 'one')
85        spin.selection('range', 0, end)  # see note
86        spin.event_generate('<<Paste>>')
87        self.assertEqual(spin.get(), ans)
88
89    def test_paste_spin_selection(self):
90        "Test pasting into a spinbox with a selection."
91        spin = self.spin
92        end, ans = 'end', 'two'
93        spin.delete(0, 'end')
94        spin.insert(0, 'one')
95        spin.selection('range', 0, end)
96        spin.event_generate('<<Paste>>')
97        self.assertEqual(spin.get(), ans)
98
99
100if __name__ == '__main__':
101    unittest.main(verbosity=2)
102