1#  Copyright (c) 2005-2020, Enthought, Inc.
2#  All rights reserved.
3#
4#  This software is provided without warranty under the terms of the BSD
5#  license included in LICENSE.txt and may be redistributed only
6#  under the conditions described in the aforementioned license.  The license
7#  is also available online at http://www.enthought.com/licenses/BSD.txt
8#
9#  Thanks for using Enthought open source!
10#
11
12import unittest
13
14from traitsui.testing.tester.target_registry import (
15    TargetRegistry,
16)
17from traitsui.testing.tester.exceptions import (
18    InteractionNotSupported,
19    LocationNotSupported,
20)
21
22
23class TestInteractionRegistry(unittest.TestCase):
24
25    def test_registry_empty(self):
26        registry = TargetRegistry()
27        with self.assertRaises(InteractionNotSupported) as exception_context:
28            registry.get_handler(None, None)
29
30        self.assertEqual(
31            str(exception_context.exception),
32            "No handler is found for target None with interaction None. "
33            "Supported these: []",
34        )
35
36    def test_register_editor_with_action(self):
37        registry = TargetRegistry()
38
39        class SpecificEditor:
40            pass
41
42        class UserAction:
43            pass
44
45        def handler(wrapper, interaction):
46            pass
47
48        # when
49        registry.register_interaction(
50            target_class=SpecificEditor,
51            interaction_class=UserAction,
52            handler=handler,
53        )
54
55        # then
56        actual = registry.get_handler(SpecificEditor, UserAction)
57        self.assertIs(actual, handler)
58
59    def test_action_not_supported_report_supported_action(self):
60        # Test raise InteractionNotSupported contains information about what
61        # actions are supported.
62
63        class SpecificEditor:
64            pass
65
66        class SpecificEditor2:
67            pass
68
69        class UserAction:
70            pass
71
72        class UserAction2:
73            pass
74
75        class UserAction3:
76            pass
77
78        def handler(wrapper, interaction):
79            pass
80
81        registry = TargetRegistry()
82        registry.register_interaction(SpecificEditor, UserAction, handler)
83        registry.register_interaction(SpecificEditor2, UserAction2, handler)
84        registry.register_interaction(SpecificEditor2, UserAction3, handler)
85
86        with self.assertRaises(InteractionNotSupported) as exception_context:
87            registry.get_handler(SpecificEditor2, None)
88
89        self.assertIn(UserAction2, exception_context.exception.supported)
90        self.assertIn(UserAction3, exception_context.exception.supported)
91        self.assertNotIn(UserAction, exception_context.exception.supported)
92
93    def test_error_conflict(self):
94        # Test the same target + interaction type cannot be registered twice.
95
96        class SpecificEditor:
97            pass
98
99        class UserAction:
100            pass
101
102        def handler(wrapper, interaction):
103            pass
104
105        registry = TargetRegistry()
106        registry.register_interaction(SpecificEditor, UserAction, handler)
107
108        with self.assertRaises(ValueError):
109            registry.register_interaction(SpecificEditor, UserAction, handler)
110
111    def test_error_get_interaction_doc(self):
112        # The registry is empty
113        registry = TargetRegistry()
114        with self.assertRaises(InteractionNotSupported):
115            registry.get_interaction_doc(float, int)
116
117    def test_get_default_interaction_doc(self):
118
119        class Action:
120            """Some action."""
121            pass
122
123        def handler(wrapper, interaction):
124            pass
125
126        registry = TargetRegistry()
127        registry.register_interaction(
128            target_class=float,
129            interaction_class=Action,
130            handler=handler,
131        )
132
133        actual = registry.get_interaction_doc(
134            target_class=float, interaction_class=Action,
135        )
136        self.assertEqual(actual, "Some action.")
137
138
139class TestLocationRegistry(unittest.TestCase):
140
141    def test_location_registry_empty(self):
142        registry = TargetRegistry()
143        with self.assertRaises(LocationNotSupported) as exception_context:
144            registry.get_solver(None, None)
145
146        self.assertEqual(exception_context.exception.supported, [])
147        self.assertEqual(
148            str(exception_context.exception),
149            "Location None is not supported for None. Supported these: []",
150        )
151
152    def test_register_location(self):
153
154        def solver(wrapper, location):
155            return 1
156
157        registry = TargetRegistry()
158        registry.register_location(
159            target_class=float, locator_class=str, solver=solver)
160
161        self.assertIs(registry.get_solver(float, str), solver)
162
163    def test_register_location_report_existing(self):
164
165        def solver(wrapper, location):
166            return 1
167
168        registry = TargetRegistry()
169        registry.register_location(
170            target_class=float, locator_class=str, solver=solver)
171
172        with self.assertRaises(LocationNotSupported) as exception_context:
173            registry.get_solver(float, None)
174
175        self.assertEqual(exception_context.exception.supported, [str])
176
177    def test_get_location_help_default(self):
178
179        class Locator:
180            """ Some default documentation."""
181            pass
182
183        registry = TargetRegistry()
184        registry.register_location(
185            target_class=float,
186            locator_class=Locator,
187            solver=lambda w, l: 1,
188        )
189
190        help_text = registry.get_location_doc(
191            target_class=float, locator_class=Locator,
192        )
193        self.assertEqual(help_text, "Some default documentation.")
194
195    def test_error_get_interaction_doc(self):
196        # The registry is empty
197        registry = TargetRegistry()
198        with self.assertRaises(LocationNotSupported):
199            registry.get_location_doc(float, int)
200