File: test_ui_traits.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (899 lines) | stat: -rw-r--r-- 27,002 bytes parent folder | download
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
# (C) Copyright 2005-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 os
import unittest

# importlib.resources is new in Python 3.7, and importlib.resources.files is
# new in Python 3.9, so for Python < 3.9 we must rely on the 3rd party
# importlib_resources package.
try:
    from importlib.resources import files
except ImportError:
    from importlib_resources import files

try:
    import PIL.Image
except ImportError:
    PIL = None

from traits.api import DefaultValue, HasTraits, TraitError
from traits.testing.optional_dependencies import numpy as np, requires_numpy
from traits.testing.api import UnittestTools

from ..color import Color
from ..font import Font
from ..image_resource import ImageResource
from ..ui_traits import (
    Border,
    HasBorder,
    HasMargin,
    Image,
    Margin,
    PyfaceColor,
    PyfaceFont,
    image_resource_cache,
    image_bitmap_cache,
)


IMAGE_PATH = os.fspath(files("pyface.tests") / "images" / "core.png")


class ImageClass(HasTraits):

    image = Image


class ColorClass(HasTraits):

    color = PyfaceColor()


class FontClass(HasTraits):

    font = PyfaceFont()


class HasMarginClass(HasTraits):

    margin = HasMargin


class HasBorderClass(HasTraits):

    border = HasBorder


class TestImageTrait(unittest.TestCase, UnittestTools):
    def setUp(self):
        # clear all cached images
        image_resource_cache.clear()
        image_bitmap_cache.clear()
        # clear cached "not found" image
        ImageResource._image_not_found = None

    def test_defaults(self):
        image_class = ImageClass()

        self.assertIsNone(image_class.image)

    def test_init_local_image(self):
        from pyface.image_resource import ImageResource

        image_class = ImageClass(image=ImageResource("core.png"))

        self.assertIsInstance(image_class.image, ImageResource)
        self.assertEqual(image_class.image.name, "core.png")
        self.assertEqual(
            image_class.image.absolute_path, os.path.abspath(IMAGE_PATH)
        )

    def test_init_pyface_image(self):
        from pyface.image_resource import ImageResource

        image_class = ImageClass(image="about")
        image_class.image.create_image()

        self.assertIsInstance(image_class.image, ImageResource)
        self.assertEqual(image_class.image.name, "about")
        self.assertIsNone(image_class.image._image_not_found)
        self.assertIsNotNone(image_class.image._ref.data)

    def test_init_pyface_image_library(self):
        from pyface.image_resource import ImageResource

        image_class = ImageClass(image="@icons:dialog-warning")

        self.assertIsInstance(image_class.image, ImageResource)
        self.assertEqual(image_class.image.name, "dialog-warning.png")
        self.assertIsNone(image_class.image._image_not_found)
        self.assertEqual(
            image_class.image._ref.file_name, "dialog-warning.png"
        )
        self.assertEqual(image_class.image._ref.volume_name, "icons")

    @requires_numpy
    def test_init_array_image(self):
        from pyface.array_image import ArrayImage

        data = np.full((32, 64, 4), 0xee, dtype='uint8')
        image = ArrayImage(data)
        image_class = ImageClass(image=image)

        self.assertIsInstance(image_class.image, ArrayImage)
        self.assertTrue((image_class.image.data == data).all())

    @unittest.skipIf(PIL is None, "PIL/Pillow is not available")
    def test_init_pil_image(self):
        from pyface.pil_image import PILImage

        pil_image = PIL.Image.open(IMAGE_PATH)
        image = PILImage(pil_image)
        image_class = ImageClass(image=image)

        self.assertIsInstance(image_class.image, PILImage)


class TestMargin(unittest.TestCase):
    def test_defaults(self):
        margin = Margin()
        self.assertEqual(margin.top, 0)
        self.assertEqual(margin.bottom, 0)
        self.assertEqual(margin.left, 0)
        self.assertEqual(margin.right, 0)

    def test_init_one_arg(self):
        margin = Margin(4)
        self.assertEqual(margin.top, 4)
        self.assertEqual(margin.bottom, 4)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_init_two_args(self):
        margin = Margin(4, 2)
        self.assertEqual(margin.top, 2)
        self.assertEqual(margin.bottom, 2)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_init_four_args(self):
        margin = Margin(4, 2, 3, 1)
        self.assertEqual(margin.top, 3)
        self.assertEqual(margin.bottom, 1)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 2)


class TestPyfaceColor(unittest.TestCase):

    def test_init(self):
        trait = PyfaceColor()
        self.assertEqual(trait.default_value, (Color, (), {}))
        self.assertEqual(
            trait.default_value_type,
            DefaultValue.callable_and_args,
        )

    def test_init_name(self):
        trait = PyfaceColor("rebeccapurple")
        self.assertEqual(
            trait.default_value,
            (Color, (), {'rgba': (0.4, 0.2, 0.6, 1.0)})
        )

    def test_init_hex(self):
        trait = PyfaceColor("#663399ff")
        self.assertEqual(
            trait.default_value,
            (Color, (), {'rgba': (0.4, 0.2, 0.6, 1.0)})
        )

    def test_init_color(self):
        trait = PyfaceColor(Color(rgba=(0.4, 0.2, 0.6, 1.0)))
        self.assertEqual(
            trait.default_value,
            (Color, (), {'rgba': (0.4, 0.2, 0.6, 1.0)})
        )

    def test_init_tuple(self):
        trait = PyfaceColor((0.4, 0.2, 0.6, 1.0))
        self.assertEqual(
            trait.default_value,
            (Color, (), {'rgba': (0.4, 0.2, 0.6, 1.0)})
        )

    def test_init_list(self):
        trait = PyfaceColor([0.4, 0.2, 0.6, 1.0])
        self.assertEqual(
            trait.default_value,
            (Color, (), {'rgba': (0.4, 0.2, 0.6, 1.0)})
        )

    @requires_numpy
    def test_init_array(self):
        trait = PyfaceColor(np.array([0.4, 0.2, 0.6, 1.0]))
        self.assertEqual(
            trait.default_value,
            (Color, (), {'rgba': (0.4, 0.2, 0.6, 1.0)})
        )

    @requires_numpy
    def test_init_array_structured_dtype(self):
        """ Test if "typical" RGBA structured array value works. """
        arr = np.array(
            [(0.4, 0.2, 0.6, 1.0)],
            dtype=np.dtype([
                ('red', float),
                ('green', float),
                ('blue', float),
                ('alpha', float),
            ]),
        )
        trait = PyfaceColor(arr[0])
        self.assertEqual(
            trait.default_value,
            (Color, (), {'rgba': (0.4, 0.2, 0.6, 1.0)})
        )

    def test_init_invalid(self):
        with self.assertRaises(TraitError):
            PyfaceColor((0.4, 0.2))

    def test_validate_color(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        trait = PyfaceColor()
        validated = trait.validate(None, None, color)
        self.assertIs(
            validated, color
        )

    def test_validate_name(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        trait = PyfaceColor()
        validated = trait.validate(None, None, "rebeccapurple")
        self.assertEqual(
            validated, color
        )

    def test_validate_hex(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        trait = PyfaceColor()
        validated = trait.validate(None, None, "#663399ff")
        self.assertEqual(
            validated, color
        )

    def test_validate_tuple(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 0.8))
        trait = PyfaceColor()
        validated = trait.validate(None, None, (0.4, 0.2, 0.6, 0.8))
        self.assertEqual(
            validated, color
        )

    def test_validate_list(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 0.8))
        trait = PyfaceColor()
        validated = trait.validate(None, None, [0.4, 0.2, 0.6, 0.8])
        self.assertEqual(
            validated, color
        )

    def test_validate_rgb_list(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        trait = PyfaceColor()
        validated = trait.validate(None, None, [0.4, 0.2, 0.6])
        self.assertEqual(
            validated, color
        )

    def test_validate_bad_string(self):
        trait = PyfaceColor()
        with self.assertRaises(TraitError):
            trait.validate(None, None, "not a color")

    def test_validate_bad_object(self):
        trait = PyfaceColor()
        with self.assertRaises(TraitError):
            trait.validate(None, None, object())

    def test_info(self):
        trait = PyfaceColor()
        self.assertIsInstance(trait.info(), str)

    def test_default_trait(self):
        color_class = ColorClass()
        self.assertEqual(color_class.color, Color())

    def test_set_color(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        color_class = ColorClass(color=color)
        self.assertIs(color_class.color, color)

    def test_set_name(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        color_class = ColorClass(color="rebeccapurple")
        self.assertEqual(color_class.color, color)

    def test_set_hex(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        color_class = ColorClass(color="#663399ff")
        self.assertEqual(color_class.color, color)

    def test_set_tuple(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        color_class = ColorClass(color=(0.4, 0.2, 0.6, 1.0))
        self.assertEqual(color_class.color, color)

    def test_set_list(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        color_class = ColorClass(color=[0.4, 0.2, 0.6, 1.0])
        self.assertEqual(color_class.color, color)

    @requires_numpy
    def test_set_array(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        color_class = ColorClass(color=np.array([0.4, 0.2, 0.6, 1.0]))
        self.assertEqual(color_class.color, color)

    @requires_numpy
    def test_set_structured_dtype(self):
        color = Color(rgba=(0.4, 0.2, 0.6, 1.0))
        arr = np.array(
            [(0.4, 0.2, 0.6, 1.0)],
            dtype=np.dtype([
                ('red', float),
                ('green', float),
                ('blue', float),
                ('alpha', float),
            ]),
        )
        color_class = ColorClass(color=arr[0])
        self.assertEqual(color_class.color, color)


class TestPyfaceFont(unittest.TestCase):

    def test_init(self):
        trait = PyfaceFont()
        self.assertEqual(trait.default_value, (Font, (), {}))
        self.assertEqual(
            trait.default_value_type,
            DefaultValue.callable_and_args,
        )

    def test_init_empty_string(self):
        trait = PyfaceFont("")
        self.assertEqual(
            trait.default_value,
            (
                Font,
                (),
                {
                    'family': ["default"],
                    'size': 12.0,
                    'weight': "normal",
                    'stretch': 100,
                    'style': "normal",
                    'variants': set(),
                    'decorations': set(),
                },
            )
        )

    def test_init_typical_string(self):
        trait = PyfaceFont(
            "10 pt bold condensed italic underline Helvetica sans-serif")
        self.assertEqual(
            trait.default_value,
            (
                Font,
                (),
                {
                    'family': ["helvetica", "sans-serif"],
                    'size': 10.0,
                    'weight': "bold",
                    'stretch': 75.0,
                    'style': "italic",
                    'variants': set(),
                    'decorations': {"underline"},
                },
            )
        )

    def test_init_font(self):
        font = Font(
            family=["helvetica", "sans-serif"],
            size=10.0,
            weight="bold",
            stretch=75.0,
            style="italic",
            variants=set(),
            decorations={"underline"},
        )
        trait = PyfaceFont(font)
        self.assertEqual(
            trait.default_value,
            (
                Font,
                (),
                {
                    'family': ["helvetica", "sans-serif"],
                    'size': 10.0,
                    'weight': "bold",
                    'stretch': 75.0,
                    'style': "italic",
                    'variants': set(),
                    'decorations': {"underline"},
                },
            )
        )

    def test_init_invalid(self):
        with self.assertRaises(ValueError):
            PyfaceFont(0)

    def test_set_empty_string(self):
        font_class = FontClass()
        font_class.font = ""
        self.assertFontEqual(font_class.font, Font())

    def test_set_typical_string(self):
        font_class = FontClass()
        font_class.font = "10 pt bold condensed italic underline Helvetica sans-serif"  # noqa: E501
        self.assertFontEqual(
            font_class.font,
            Font(
                family=["helvetica", "sans-serif"],
                size=10.0,
                weight="bold",
                stretch=75.0,
                style="italic",
                variants=set(),
                decorations={"underline"},
            ),
        )

    def test_set_font(self):
        font_class = FontClass()
        font = Font(
            family=["helvetica", "sans-serif"],
            size=10.0,
            weight="bold",
            stretch=75.0,
            style="italic",
            variants=set(),
            decorations={"underline"},
        )
        font_class.font = font
        self.assertIs(font_class.font, font)

    def test_set_failure(self):
        font_class = FontClass()

        with self.assertRaises(TraitError):
            font_class.font = None

    def assertFontEqual(self, font1, font2):
        state1 = font1.trait_get(transient=lambda x: not x)
        state2 = font2.trait_get(transient=lambda x: not x)
        self.assertEqual(state1, state2)


class TestHasMargin(unittest.TestCase, UnittestTools):
    def test_defaults(self):
        has_margin = HasMarginClass()
        margin = has_margin.margin

        self.assertEqual(margin.top, 0)
        self.assertEqual(margin.bottom, 0)
        self.assertEqual(margin.left, 0)
        self.assertEqual(margin.right, 0)

    def test_unspecified_default(self):
        trait = HasMargin()
        trait.default_value_type = DefaultValue.unspecified

        (dvt, dv) = trait.get_default_value()

        self.assertEqual(dvt, DefaultValue.callable_and_args)
        self.assertEqual(
            dv, (Margin, (), {"top": 0, "bottom": 0, "left": 0, "right": 0})
        )

    def test_default_int(self):
        class HasMarginClass(HasTraits):

            margin = HasMargin(4)

        has_margin = HasMarginClass()
        margin = has_margin.margin

        self.assertEqual(margin.top, 4)
        self.assertEqual(margin.bottom, 4)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_default_one_tuple(self):
        class HasMarginClass(HasTraits):

            margin = HasMargin((4,))

        has_margin = HasMarginClass()
        margin = has_margin.margin

        self.assertEqual(margin.top, 4)
        self.assertEqual(margin.bottom, 4)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_default_two_tuple(self):
        class HasMarginClass(HasTraits):

            margin = HasMargin((4, 2))

        has_margin = HasMarginClass()
        margin = has_margin.margin

        self.assertEqual(margin.top, 2)
        self.assertEqual(margin.bottom, 2)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_default_four_tuple(self):
        class HasMarginClass(HasTraits):

            margin = HasMargin((4, 2, 3, 1))

        has_margin = HasMarginClass()
        margin = has_margin.margin

        self.assertEqual(margin.top, 3)
        self.assertEqual(margin.bottom, 1)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 2)

    def test_default_margin(self):
        m = Margin(left=4, right=2, top=3, bottom=1)

        class HasMarginClass(HasTraits):

            margin = HasMargin(m)

        has_margin = HasMarginClass()
        margin = has_margin.margin

        self.assertEqual(margin.top, 3)
        self.assertEqual(margin.bottom, 1)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 2)

    def test_init_int(self):
        has_margin = HasMarginClass(margin=4)
        margin = has_margin.margin

        self.assertEqual(margin.top, 4)
        self.assertEqual(margin.bottom, 4)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_init_one_tuple(self):
        has_margin = HasMarginClass(margin=(4,))
        margin = has_margin.margin

        self.assertEqual(margin.top, 4)
        self.assertEqual(margin.bottom, 4)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_init_two_tuple(self):
        has_margin = HasMarginClass(margin=(4, 2))
        margin = has_margin.margin

        self.assertEqual(margin.top, 2)
        self.assertEqual(margin.bottom, 2)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_init_four_tuple(self):
        has_margin = HasMarginClass(margin=(4, 2, 3, 1))
        margin = has_margin.margin

        self.assertEqual(margin.top, 3)
        self.assertEqual(margin.bottom, 1)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 2)

    def test_init_margin(self):
        margin = Margin()
        has_margin = HasMarginClass(margin=margin)

        self.assertEqual(has_margin.margin, margin)

    def test_set_int(self):
        has_margin = HasMarginClass()
        with self.assertTraitChanges(has_margin, "margin", 1):
            has_margin.margin = 4

        margin = has_margin.margin
        self.assertEqual(margin.top, 4)
        self.assertEqual(margin.bottom, 4)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_set_one_tuple(self):
        has_margin = HasMarginClass()
        with self.assertTraitChanges(has_margin, "margin", 1):
            has_margin.margin = (4,)

        margin = has_margin.margin

        self.assertEqual(margin.top, 4)
        self.assertEqual(margin.bottom, 4)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_set_two_tuple(self):
        has_margin = HasMarginClass()
        with self.assertTraitChanges(has_margin, "margin", 1):
            has_margin.margin = (4, 2)

        margin = has_margin.margin

        self.assertEqual(margin.top, 2)
        self.assertEqual(margin.bottom, 2)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 4)

    def test_set_four_tuple(self):
        has_margin = HasMarginClass()
        with self.assertTraitChanges(has_margin, "margin", 1):
            has_margin.margin = (4, 2, 3, 1)

        margin = has_margin.margin
        self.assertEqual(margin.top, 3)
        self.assertEqual(margin.bottom, 1)
        self.assertEqual(margin.left, 4)
        self.assertEqual(margin.right, 2)

    def test_set_margin(self):
        margin = Margin()
        has_margin = HasMarginClass()
        with self.assertTraitChanges(has_margin, "margin", 1):
            has_margin.margin = margin

        self.assertEqual(has_margin.margin, margin)

    def test_set_invalid(self):
        has_margin = HasMarginClass()
        with self.assertRaises(TraitError):
            has_margin.margin = (1, 2, 3)


class TestBorder(unittest.TestCase):
    def test_defaults(self):
        border = Border()
        self.assertEqual(border.top, 0)
        self.assertEqual(border.bottom, 0)
        self.assertEqual(border.left, 0)
        self.assertEqual(border.right, 0)

    def test_init_one_arg(self):
        border = Border(4)
        self.assertEqual(border.top, 4)
        self.assertEqual(border.bottom, 4)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_init_two_args(self):
        border = Border(4, 2)
        self.assertEqual(border.top, 2)
        self.assertEqual(border.bottom, 2)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_init_four_args(self):
        border = Border(4, 2, 3, 1)
        self.assertEqual(border.top, 3)
        self.assertEqual(border.bottom, 1)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 2)


class TestHasBorder(unittest.TestCase, UnittestTools):
    def test_defaults(self):
        has_border = HasBorderClass()
        border = has_border.border

        self.assertEqual(border.top, 0)
        self.assertEqual(border.bottom, 0)
        self.assertEqual(border.left, 0)
        self.assertEqual(border.right, 0)

    def test_unspecified_default(self):
        trait = HasBorder()
        trait.default_value_type = DefaultValue.unspecified

        (dvt, dv) = trait.get_default_value()

        self.assertEqual(dvt, DefaultValue.callable_and_args)
        self.assertEqual(
            dv, (Border, (), {"top": 0, "bottom": 0, "left": 0, "right": 0})
        )

    def test_default_int(self):
        class HasBorderClass(HasTraits):

            border = HasBorder(4)

        has_border = HasBorderClass()
        border = has_border.border

        self.assertEqual(border.top, 4)
        self.assertEqual(border.bottom, 4)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_default_one_tuple(self):
        class HasBorderClass(HasTraits):

            border = HasBorder((4,))

        has_border = HasBorderClass()
        border = has_border.border

        self.assertEqual(border.top, 4)
        self.assertEqual(border.bottom, 4)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_default_two_tuple(self):
        class HasBorderClass(HasTraits):

            border = HasBorder((4, 2))

        has_border = HasBorderClass()
        border = has_border.border

        self.assertEqual(border.top, 2)
        self.assertEqual(border.bottom, 2)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_default_four_tuple(self):
        class HasBorderClass(HasTraits):

            border = HasBorder((4, 2, 3, 1))

        has_border = HasBorderClass()
        border = has_border.border

        self.assertEqual(border.top, 3)
        self.assertEqual(border.bottom, 1)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 2)

    def test_default_border(self):
        m = Margin(left=4, right=2, top=3, bottom=1)

        class HasBorderClass(HasTraits):

            border = HasBorder(m)

        has_border = HasBorderClass()
        border = has_border.border

        self.assertEqual(border.top, 3)
        self.assertEqual(border.bottom, 1)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 2)

    def test_init_int(self):
        has_border = HasBorderClass(border=4)
        border = has_border.border

        self.assertEqual(border.top, 4)
        self.assertEqual(border.bottom, 4)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_init_one_tuple(self):
        has_border = HasBorderClass(border=(4,))
        border = has_border.border

        self.assertEqual(border.top, 4)
        self.assertEqual(border.bottom, 4)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_init_two_tuple(self):
        has_border = HasBorderClass(border=(4, 2))
        border = has_border.border

        self.assertEqual(border.top, 2)
        self.assertEqual(border.bottom, 2)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_init_four_tuple(self):
        has_border = HasBorderClass(border=(4, 2, 3, 1))
        border = has_border.border

        self.assertEqual(border.top, 3)
        self.assertEqual(border.bottom, 1)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 2)

    def test_init_border(self):
        border = Border()
        has_border = HasBorderClass(border=border)

        self.assertEqual(has_border.border, border)

    def test_set_int(self):
        has_border = HasBorderClass()
        with self.assertTraitChanges(has_border, "border", 1):
            has_border.border = 4

        border = has_border.border
        self.assertEqual(border.top, 4)
        self.assertEqual(border.bottom, 4)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_set_one_tuple(self):
        has_border = HasBorderClass()
        with self.assertTraitChanges(has_border, "border", 1):
            has_border.border = (4,)

        border = has_border.border

        self.assertEqual(border.top, 4)
        self.assertEqual(border.bottom, 4)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_set_two_tuple(self):
        has_border = HasBorderClass()
        with self.assertTraitChanges(has_border, "border", 1):
            has_border.border = (4, 2)

        border = has_border.border

        self.assertEqual(border.top, 2)
        self.assertEqual(border.bottom, 2)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 4)

    def test_set_four_tuple(self):
        has_border = HasBorderClass()
        with self.assertTraitChanges(has_border, "border", 1):
            has_border.border = (4, 2, 3, 1)

        border = has_border.border
        self.assertEqual(border.top, 3)
        self.assertEqual(border.bottom, 1)
        self.assertEqual(border.left, 4)
        self.assertEqual(border.right, 2)

    def test_set_border(self):
        border = Border()
        has_border = HasBorderClass()
        with self.assertTraitChanges(has_border, "border", 1):
            has_border.border = border

        self.assertEqual(has_border.border, border)

    def test_set_invalid(self):
        has_border = HasBorderClass()
        with self.assertRaises(TraitError):
            has_border.border = (1, 2, 3)