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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
|
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
import contextlib
import unittest
from traits.api import HasTraits, List
from traitsui.api import SetEditor, UItem, View
from traitsui.tests._tools import (
BaseTestMixin,
create_ui,
click_button,
is_control_enabled,
is_qt,
is_wx,
process_cascade_events,
requires_toolkit,
reraise_exceptions,
ToolkitName,
)
class ListModel(HasTraits):
value = List(["one", "two"])
def get_view(can_move_all=True, ordered=False):
return View(
UItem(
"value",
editor=SetEditor(
values=["one", "two", "three", "four"],
ordered=ordered,
can_move_all=can_move_all,
),
style="simple",
)
)
def get_list_items(list_widget):
"""Return a list of strings from the list widget."""
items = []
if is_wx():
for i in range(list_widget.GetCount()):
items.append(list_widget.GetString(i))
elif is_qt():
for i in range(list_widget.count()):
items.append(list_widget.item(i).text())
else:
raise unittest.SkipTest("Test not implemented for this toolkit")
return items
def click_on_item(editor, item_idx, in_used=False):
"""Simulate a click on an item in a specified list.
The function deselects all items in both used and unused lists, then
selects an item at index item_idx either in the used list (if
in_used=True) or in the unused list. Finally the function simulates a
click on the selected item.
"""
unused_list = editor._unused
used_list = editor._used
if is_wx():
import wx
# First deselect all items
for i in range(unused_list.GetCount()):
unused_list.Deselect(i)
for i in range(used_list.GetCount()):
used_list.Deselect(i)
# Select the item in the correct list
list_with_selection = used_list if in_used else unused_list
list_with_selection.SetSelection(item_idx)
event = wx.CommandEvent(
wx.EVT_LISTBOX.typeId, list_with_selection.GetId()
)
wx.PostEvent(editor.control, event)
elif is_qt():
for i in range(unused_list.count()):
status = (not in_used) and (item_idx == i)
unused_list.item(i).setSelected(status)
for i in range(used_list.count()):
status = (in_used) and (item_idx == i)
used_list.item(i).setSelected(status)
if in_used:
used_list.itemClicked.emit(used_list.item(item_idx))
else:
unused_list.itemClicked.emit(unused_list.item(item_idx))
else:
raise unittest.SkipTest("Test not implemented for this toolkit")
def double_click_on_item(editor, item_idx, in_used=False):
"""Simulate a double click on an item in a specified list.
The function deselects all items in both used and unused lists, then
selects an item at index item_idx either in the used list (if
in_used=True) or in the unused list. Finally the function simulates a
double click on the selected item.
"""
unused_list = editor._unused
used_list = editor._used
if is_wx():
import wx
# First deselect all items
for i in range(unused_list.GetCount()):
unused_list.Deselect(i)
for i in range(used_list.GetCount()):
used_list.Deselect(i)
# Select the item in the correct list
list_with_selection = used_list if in_used else unused_list
list_with_selection.SetSelection(item_idx)
event = wx.CommandEvent(
wx.EVT_LISTBOX_DCLICK.typeId, list_with_selection.GetId()
)
wx.PostEvent(editor.control, event)
elif is_qt():
for i in range(unused_list.count()):
status = (not in_used) and (item_idx == i)
unused_list.item(i).setSelected(status)
for i in range(used_list.count()):
status = (in_used) and (item_idx == i)
used_list.item(i).setSelected(status)
if in_used:
used_list.itemDoubleClicked.emit(used_list.item(item_idx))
else:
unused_list.itemDoubleClicked.emit(unused_list.item(item_idx))
else:
raise unittest.SkipTest("Test not implemented for this toolkit")
@requires_toolkit([ToolkitName.qt, ToolkitName.wx])
class TestSetEditorMapping(BaseTestMixin, unittest.TestCase):
def setUp(self):
BaseTestMixin.setUp(self)
def tearDown(self):
BaseTestMixin.tearDown(self)
@contextlib.contextmanager
def setup_ui(self, model, view):
with create_ui(model, dict(view=view)) as ui:
yield ui.get_editors("value")[0]
def test_simple_editor_mapping_values(self):
class IntListModel(HasTraits):
value = List()
set_editor_factory = SetEditor(
values=[0, 1], format_func=lambda v: str(bool(v)).upper()
)
formatted_view = View(
UItem(
"value",
editor=set_editor_factory,
style="simple",
)
)
with reraise_exceptions(), self.setup_ui(
IntListModel(), formatted_view
) as editor:
self.assertEqual(editor.names, ["FALSE", "TRUE"])
self.assertEqual(editor.mapping, {"FALSE": 0, "TRUE": 1})
self.assertEqual(editor.inverse_mapping, {0: "FALSE", 1: "TRUE"})
set_editor_factory.values = [1, 0]
self.assertEqual(editor.names, ["TRUE", "FALSE"])
self.assertEqual(editor.mapping, {"TRUE": 1, "FALSE": 0})
self.assertEqual(editor.inverse_mapping, {1: "TRUE", 0: "FALSE"})
def test_simple_editor_mapping_name(self):
class IntListModel(HasTraits):
value = List()
possible_values = List([0, 1])
formatted_view = View(
UItem(
'value',
editor=SetEditor(
name="object.possible_values",
format_func=lambda v: str(bool(v)).upper(),
),
style="simple",
)
)
model = IntListModel()
with reraise_exceptions(), self.setup_ui(
model, formatted_view
) as editor:
self.assertEqual(editor.names, ["FALSE", "TRUE"])
self.assertEqual(editor.mapping, {"FALSE": 0, "TRUE": 1})
self.assertEqual(editor.inverse_mapping, {0: "FALSE", 1: "TRUE"})
model.possible_values = [1, 0]
self.assertEqual(editor.names, ["TRUE", "FALSE"])
self.assertEqual(editor.mapping, {"TRUE": 1, "FALSE": 0})
self.assertEqual(editor.inverse_mapping, {1: "TRUE", 0: "FALSE"})
@requires_toolkit([ToolkitName.qt, ToolkitName.wx])
class TestSimpleSetEditor(BaseTestMixin, unittest.TestCase):
def setUp(self):
BaseTestMixin.setUp(self)
def tearDown(self):
BaseTestMixin.tearDown(self)
@contextlib.contextmanager
def setup_gui(self, model, view):
with create_ui(model, dict(view=view)) as ui:
yield ui.get_editors("value")[0]
def test_simple_set_editor_use_button(self):
# Initiate with non-alphabetical list
model = ListModel(value=["two", "one"])
with reraise_exceptions(), self.setup_gui(model, get_view()) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
# Used list is sorted alphabetically
self.assertEqual(get_list_items(editor._used), ["one", "two"])
click_on_item(editor, 1, in_used=False)
process_cascade_events()
self.assertTrue(is_control_enabled(editor._use))
self.assertFalse(is_control_enabled(editor._unuse))
click_button(editor._use)
process_cascade_events()
self.assertEqual(get_list_items(editor._unused), ["four"])
# Button inserts at the top
self.assertEqual(
get_list_items(editor._used), ["three", "one", "two"]
)
self.assertEqual(editor._get_selected_strings(editor._used), [])
def test_simple_set_editor_unuse_button(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view()
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
click_on_item(editor, 0, in_used=True)
process_cascade_events()
self.assertFalse(is_control_enabled(editor._use))
self.assertTrue(is_control_enabled(editor._unuse))
click_button(editor._unuse)
process_cascade_events()
# Button inserts at the top
self.assertEqual(
get_list_items(editor._unused), ["one", "four", "three"]
)
self.assertEqual(get_list_items(editor._used), ["two"])
def test_simple_set_editor_use_dclick(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view()
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
double_click_on_item(editor, 1, in_used=False)
process_cascade_events()
self.assertEqual(get_list_items(editor._unused), ["four"])
# Inserts at the top
self.assertEqual(
get_list_items(editor._used), ["three", "one", "two"]
)
self.assertEqual(editor._get_selected_strings(editor._used), [])
def test_simple_set_editor_unuse_dclick(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view()
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
double_click_on_item(editor, 0, in_used=True)
process_cascade_events()
# Inserts at the top
self.assertEqual(
get_list_items(editor._unused), ["one", "four", "three"]
)
self.assertEqual(get_list_items(editor._used), ["two"])
def test_simple_set_editor_use_all(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view()
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
click_on_item(editor, 1, in_used=False)
process_cascade_events()
self.assertTrue(is_control_enabled(editor._use_all))
self.assertFalse(is_control_enabled(editor._unuse_all))
click_button(editor._use_all)
process_cascade_events()
self.assertEqual(get_list_items(editor._unused), [])
# Button inserts at the end
self.assertEqual(
get_list_items(editor._used), ["one", "two", "four", "three"]
)
def test_simple_set_editor_unuse_all(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view()
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
click_on_item(editor, 0, in_used=True)
process_cascade_events()
self.assertFalse(is_control_enabled(editor._use_all))
self.assertTrue(is_control_enabled(editor._unuse_all))
click_button(editor._unuse_all)
process_cascade_events()
# Button inserts at the end
self.assertEqual(
get_list_items(editor._unused), ["four", "three", "one", "two"]
)
self.assertEqual(get_list_items(editor._used), [])
def test_simple_set_editor_move_up(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view(ordered=True)
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
click_on_item(editor, 1, in_used=True)
process_cascade_events()
self.assertTrue(is_control_enabled(editor._up))
self.assertFalse(is_control_enabled(editor._down))
click_button(editor._up)
process_cascade_events()
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["two", "one"])
def test_simple_set_editor_move_down(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view(ordered=True)
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
click_on_item(editor, 0, in_used=True)
process_cascade_events()
self.assertFalse(is_control_enabled(editor._up))
self.assertTrue(is_control_enabled(editor._down))
click_button(editor._down)
process_cascade_events()
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["two", "one"])
def test_simple_set_editor_use_all_button(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view()
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
click_on_item(editor, 1, in_used=False)
process_cascade_events()
self.assertTrue(is_control_enabled(editor._use_all))
self.assertFalse(is_control_enabled(editor._unuse_all))
click_button(editor._use_all)
process_cascade_events()
self.assertEqual(get_list_items(editor._unused), [])
# Button inserts at the end
self.assertEqual(
get_list_items(editor._used), ["one", "two", "four", "three"]
)
def test_simple_set_editor_unuse_all_button(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view()
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
click_on_item(editor, 0, in_used=True)
process_cascade_events()
self.assertFalse(is_control_enabled(editor._use_all))
self.assertTrue(is_control_enabled(editor._unuse_all))
click_button(editor._unuse_all)
process_cascade_events()
# Button inserts at the end
self.assertEqual(
get_list_items(editor._unused), ["four", "three", "one", "two"]
)
self.assertEqual(get_list_items(editor._used), [])
def test_simple_set_editor_default_selection_unused(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view()
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
click_button(editor._use)
process_cascade_events()
# Button inserts at the top
self.assertEqual(get_list_items(editor._unused), ["three"])
self.assertEqual(
get_list_items(editor._used), ["four", "one", "two"]
)
def test_simple_set_editor_default_selection_used(self):
# When all items are used, top used item is selected by default
list_edit = ListModel(value=["one", "two", "three", "four"])
with reraise_exceptions(), self.setup_gui(
list_edit, get_view()
) as editor:
self.assertEqual(get_list_items(editor._unused), [])
self.assertEqual(
get_list_items(editor._used), ["four", "one", "three", "two"]
)
click_button(editor._unuse)
process_cascade_events()
# Button inserts at the top
self.assertEqual(get_list_items(editor._unused), ["four"])
self.assertEqual(
get_list_items(editor._used), ["one", "three", "two"]
)
def test_simple_set_editor_deleted_valid_values(self):
editor_factory = SetEditor(values=["one", "two", "three", "four"])
view = View(
UItem(
"value",
editor=editor_factory,
style="simple",
)
)
list_edit = ListModel()
with reraise_exceptions(), self.setup_gui(list_edit, view) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
editor_factory.values = ["two", "three", "four"]
process_cascade_events()
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
# FIXME issue enthought/traitsui#840
if is_wx():
with self.assertRaises(AssertionError):
self.assertEqual(get_list_items(editor._used), ["two"])
self.assertEqual(get_list_items(editor._used), ["one", "two"])
else:
self.assertEqual(get_list_items(editor._used), ["two"])
self.assertEqual(list_edit.value, ["two"])
def test_simple_set_editor_use_ordered_selected(self):
# Initiate with non-alphabetical list
model = ListModel(value=["two", "one"])
with reraise_exceptions(), self.setup_gui(
model, get_view(ordered=True)
) as editor:
self.assertEqual(get_list_items(editor._unused), ["four", "three"])
# Used list maintains the order
self.assertEqual(get_list_items(editor._used), ["two", "one"])
click_on_item(editor, 1, in_used=False)
process_cascade_events()
self.assertTrue(is_control_enabled(editor._use))
self.assertFalse(is_control_enabled(editor._unuse))
click_button(editor._use)
process_cascade_events()
self.assertEqual(get_list_items(editor._unused), ["four"])
# Button inserts at the top
self.assertEqual(
get_list_items(editor._used), ["three", "two", "one"]
)
self.assertEqual(
editor._get_selected_strings(editor._used), ["three"]
)
def test_simple_set_editor_unordeder_button_existence(self):
with reraise_exceptions(), self.setup_gui(
ListModel(), get_view()
) as editor:
self.assertIsNone(editor._up)
self.assertIsNone(editor._down)
def test_simple_set_editor_cant_move_all_button_existence(self):
view = get_view(can_move_all=False)
with reraise_exceptions(), self.setup_gui(ListModel(), view) as editor:
self.assertIsNone(editor._use_all)
self.assertIsNone(editor._unuse_all)
|