1from Foundation import *
2from PyObjCTools.TestSupport import *
3import Foundation
4
5try:
6    unicode
7except NameError:
8    unicode = str
9
10class TestHelper (NSObject):
11    def incFoo_(self, foo):
12        foo[0] += 1
13
14class TestNSUndoManager(TestCase):
15    def testUndoManager(self):
16        x = TestHelper.new()
17        m = NSUndoManager.new()
18        l = [ 0 ]
19
20        m.prepareWithInvocationTarget_(x).incFoo_(l)
21        m.undo()
22
23        self.assertEqual(l[0], 1)
24
25    def __del__(self, objc=objc):
26        objc.recycleAutoreleasePool()
27
28## Undo Integer test
29## From David Eppstein
30# test ability of int argument to pass through undo and then
31# be used as parameter to another routine expecting an int
32#
33# the actual routine I want to use is
34# NSTableView.editColumn_row_withEvent_select_
35# but that involves setting up a UI; instead use NSIndexSpecifier
36
37if hasattr(Foundation, 'NSIndexSpecifier'):
38    class TestUndoInt(TestCase):
39        class UndoInt(NSObject):
40            undo = NSUndoManager.alloc().init()
41            idx = NSIndexSpecifier.alloc().init()
42            idx.setIndex_(0)
43
44            def test_(self,i):
45                self.undo.prepareWithInvocationTarget_(self).test_(self.idx.index())
46                self.idx.setIndex_(i)
47
48        def testUndoInt(self):
49            # test that undo works
50            x = TestUndoInt.UndoInt.alloc().init()
51            x.test_(3)
52            assert(x.idx.index() == 3)
53            x.undo.undo()
54            assert(x.idx.index() == 0)
55## end Undo Integer test
56
57
58class TestSubclassingUndo(TestCase):
59    # Bugreport: 678759 Subclassing NSUndoManager fails
60
61    def testSubclass(self):
62        class UndoSubclass (NSUndoManager):
63            pass
64
65        x = TestHelper.new()
66        m = UndoSubclass.new()
67        l = [ 0 ]
68
69        m.prepareWithInvocationTarget_(x).incFoo_(l)
70        m.undo()
71
72        self.assertEqual(l[0], 1)
73
74    def testConstants(self):
75        self.assertIsInstance(NSUndoManagerCheckpointNotification, unicode)
76        self.assertIsInstance(NSUndoManagerWillUndoChangeNotification, unicode)
77        self.assertIsInstance(NSUndoManagerWillRedoChangeNotification, unicode)
78        self.assertIsInstance(NSUndoManagerDidUndoChangeNotification, unicode)
79        self.assertIsInstance(NSUndoManagerDidRedoChangeNotification, unicode)
80        self.assertIsInstance(NSUndoManagerDidOpenUndoGroupNotification, unicode)
81        self.assertIsInstance(NSUndoManagerWillCloseUndoGroupNotification, unicode)
82        self.assertEqual(NSUndoCloseGroupingRunLoopOrdering, 350000)
83
84    @min_os_level('10.7')
85    def testConstants10_7(self):
86        self.assertIsInstance(NSUndoManagerGroupIsDiscardableKey, unicode)
87        self.assertIsInstance(NSUndoManagerDidCloseUndoGroupNotification, unicode)
88
89    def testMethods(self):
90        self.assertResultIsBOOL(NSUndoManager.isUndoRegistrationEnabled)
91        self.assertResultIsBOOL(NSUndoManager.groupsByEvent)
92        self.assertArgIsBOOL(NSUndoManager.setGroupsByEvent_, 0)
93        self.assertResultIsBOOL(NSUndoManager.canUndo)
94        self.assertResultIsBOOL(NSUndoManager.canRedo)
95        self.assertResultIsBOOL(NSUndoManager.isUndoing)
96        self.assertResultIsBOOL(NSUndoManager.isRedoing)
97        self.assertArgIsSEL(NSUndoManager.registerUndoWithTarget_selector_object_, 1, b'v@:@')
98
99    @min_os_level('10.7')
100    def testMethods10_7(self):
101        self.assertArgIsBOOL(NSUndoManager.setActionIsDiscardable_, 0)
102        self.assertResultIsBOOL(NSUndoManager.undoActionIsDiscardable)
103        self.assertResultIsBOOL(NSUndoManager.redoActionIsDiscardable)
104
105if __name__ == '__main__':
106    main( )
107