forked from robotframework/RIDE
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest_treecontroller.py
194 lines (154 loc) · 7.19 KB
/
test_treecontroller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Copyright 2008-2015 Nokia Networks
# Copyright 2016- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import os
import pytest
DISPLAY = os.getenv('DISPLAY')
if not DISPLAY:
pytest.skip("Skipped because of missing DISPLAY", allow_module_level=True) # Avoid failing unit tests in system without X11
from robotide.robotapi import TestCase, TestCaseFile
from robotide.controller.ctrlcommands import ChangeTag
from robotide.controller.filecontrollers import TestCaseFileController
from robotide.controller.macrocontrollers import TestCaseController
from robotide.controller.tablecontrollers import TestCaseTableController
from robotide.controller.tags import Tag
from robotide.controller.ui.treecontroller import TreeController, _History, \
TestSelectionController
from utest.resources import UIUnitTestBase
class ActionRegistererMock(object):
def register_actions(self, action_collections):
self.action_collections = action_collections
def register_action(self, action):
pass
class TestTreeController(unittest.TestCase):
def test_register_tree_actions(self):
mocked_ar = ActionRegistererMock()
TreeController(None, mocked_ar, None, None).register_tree_actions()
self.assertEqual(["Go &Back", "Go &Forward"], [a.name for a in mocked_ar.action_collections])
class _BaseTreeControllerTest(object):
def setUp(self):
self.history = _History()
self.controller = TreeController(
self._tree_mock(), None, None, None, history=self.history)
self.controller.add_to_history("Top Suite")
def _tree_mock(self):
tree_mock = lambda: 0
self._tree_mock_items = []
tree_mock.SelectItem = lambda i: self._tree_mock_items.append(i)
return tree_mock
def _select_node(self, value):
self.controller.add_to_history(value)
def _go_back_and_return_selection(self):
self.controller.on_go_back(None)
return self._tree_mock_items[-1]
def _go_forward_and_return_selection(self):
self.controller.on_go_forward(None)
return self._tree_mock_items[-1]
class TestNavigationHistory(_BaseTreeControllerTest, unittest.TestCase):
def test_go_back_one_level(self):
self._select_node('Top Suite Fake UK 2')
self.assertEqual('Top Suite', self._go_back_and_return_selection())
def test_go_back_two_levels(self):
nodes = ['Top Suite Fake UK 1', 'Sub Suite 1', 'Sub Suite 1 Fake UK 0']
for name in nodes:
self._select_node(name)
nodes.reverse()
for name in nodes[1:]:
self.assertEqual(name, self._go_back_and_return_selection())
def test_it_is_not_possible_to_go_back_farther_than_history(self):
nodes = ['Top Suite Fake UK 1', 'Sub Suite 1', 'Sub Suite 1 Fake UK 0']
for name in nodes:
self._select_node(name)
nodes.reverse()
for name in nodes[1:] + ['Top Suite']:
self._go_back_and_assert_selection(name)
self._go_back_and_assert_selection('Top Suite')
def test_go_back_with_selecting_in_between(self):
nodes = ['Top Suite Fake UK 1', 'Sub Suite 1', 'Sub Suite 1 Fake UK 0']
for name in nodes:
self._select_node(name)
self._go_back_and_assert_selection('Sub Suite 1')
self._select_node('Sub Suite 2 Fake UK 0')
self._go_back_and_assert_selection('Sub Suite 1')
def test_go_forward(self):
nodes = ['Top Suite Fake UK 1', 'Sub Suite 1', 'Sub Suite 1 Fake UK 0']
for name in nodes:
self._select_node(name)
for _ in range(3):
self.controller.on_go_back(None)
for name in nodes:
self._go_forward_and_assert_selection(name)
def test_go_back_and_forward_between_suite_and_resource(self):
nodes = ['Top Suite Fake UK 0', 'Resource Keyword',
'Sub Suite 0 Fake UK 2']
for name in nodes:
self._select_node(name)
self._go_back_and_assert_selection('Resource Keyword')
self._go_back_and_assert_selection('Top Suite Fake UK 0')
self._go_forward_and_assert_selection('Resource Keyword')
self._go_forward_and_assert_selection('Sub Suite 0 Fake UK 2')
def _go_back_and_assert_selection(self, expected_selection):
assert self._go_back_and_return_selection() == expected_selection
def _go_forward_and_assert_selection(self, expected_selection):
assert (
self._go_forward_and_return_selection() == expected_selection)
class TestTestSelectionController(UIUnitTestBase):
def setUp(self):
super().setUp()
self._tsc = TestSelectionController()
def test_test_selection_is_empty_by_default(self):
self.assertTrue(self._tsc.is_empty())
def test_test_selection_is_not_empty_when_it_contains_a_test(self):
self._tsc.select(self._create_test())
self.assertFalse(self._tsc.is_empty())
def test_remove_invalid_cases_selection(self):
self._tsc.select(self._create_test())
new_test = self._create_test()
self._tsc.remove_invalid_cases_selection(new_test.datafile_controller)
self.assertFalse(self._tsc.is_empty())
def test_test_selection_is_empty_after_removing_same_test_from_there_even_when_it_is_not_the_same_object(self):
test = self._create_test()
self._tsc.select(test)
self._tsc.select(test, False)
self.assertTrue(self._tsc.is_empty())
def test_is_test_selected(self):
test = self._create_test()
self.assertFalse(self._tsc.is_test_selected(test))
self._tsc.select(test)
self.assertTrue(self._tsc.is_test_selected(test))
def test_adding_tag_to_selected_tests(self):
tests = [self._create_test('test%d' % i) for i in range(10)]
for t in tests:
self._tsc.select(t)
self._tsc.add_tag('foo')
for t in tests:
self.assertEqual([tag.name for tag in t.tags], ['foo'])
def test_adding_a_tag_to_test_with_a_default_tag(self):
test = self._create_test()
test.datafile_controller.default_tags.execute(
ChangeTag(Tag(None), 'default'))
assert [t.name for t in test.tags] == ['default']
self._tsc.select(test)
self._tsc.add_tag('custom')
self.assertEqual([t.name for t in test.tags], ['default', 'custom'])
def _create_test(self, name='test'):
suite = TestCaseFile(source='suite')
suite_controller = TestCaseFileController(suite)
parent = TestCaseTableController(
suite_controller, suite.testcase_table)
test = TestCase(parent=lambda: 0, name=name)
return TestCaseController(parent, test)
if __name__ == "__main__":
unittest.main()