py-sdl2 / sdl2 / test / video_test.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
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
import sys
from ctypes.util import find_library
from ctypes import c_int, byref, cast, POINTER, py_object
import unittest
from .util.testutils import interactive, doprint
from ..stdinc import SDL_FALSE, SDL_TRUE
from .. import video, rect, surface

if sys.version_info[0] >= 3:
    long = int

to_ctypes = lambda seq, dtype: (dtype * len(seq))(*seq)


def has_opengl_lib():
    for libname in("gl", "opengl", "opengl32"):
        path = find_library(libname)
        if path is not None:
            return True


def get_opengl_path():
    for libname in("gl", "opengl", "opengl32"):
        path = find_library(libname)
        if path is not None:
            return path


# TODO: mostly covers positive tests right now - fix this!
class SDLVideoTest(unittest.TestCase):
    __tags__ = ["sdl"]

    def setUp(self):
        video.SDL_VideoInit(None)

    def tearDown(self):
        video.SDL_VideoQuit()

    def test_SDL_WINDOWPOS_UNDEFINED_DISPLAY(self):
        for x in range(0xFFFF):
            self.assertEqual(video.SDL_WINDOWPOS_UNDEFINED_MASK | x,
                             video.SDL_WINDOWPOS_UNDEFINED_DISPLAY(x))
            self.assertEqual((video.SDL_WINDOWPOS_UNDEFINED_DISPLAY(x) &
                              video.SDL_WINDOWPOS_UNDEFINED_MASK),
                             video.SDL_WINDOWPOS_UNDEFINED_MASK)
            self.assertTrue(video.SDL_WINDOWPOS_UNDEFINED_DISPLAY(x) !=
                            video.SDL_WINDOWPOS_CENTERED_DISPLAY(x))

    def test_SDL_WINDOWPOS_ISUNDEFINED(self):
        self.assertTrue(video.SDL_WINDOWPOS_ISUNDEFINED
                        (video.SDL_WINDOWPOS_UNDEFINED))
        self.assertFalse(video.SDL_WINDOWPOS_ISUNDEFINED
                         (video.SDL_WINDOWPOS_CENTERED))
        for x in range(0xFFFF):
            self.assertTrue(video.SDL_WINDOWPOS_ISUNDEFINED
                            (video.SDL_WINDOWPOS_UNDEFINED_DISPLAY(x)))

    def test_SDL_WINDOWPOS_CENTERED_DISPLAY(self):
        for x in range(0xFFFF):
            self.assertEqual(video.SDL_WINDOWPOS_CENTERED_MASK | x,
                             video.SDL_WINDOWPOS_CENTERED_DISPLAY(x))
            self.assertEqual((video.SDL_WINDOWPOS_CENTERED_DISPLAY(x) &
                              video.SDL_WINDOWPOS_CENTERED_MASK),
                             video.SDL_WINDOWPOS_CENTERED_MASK)
            self.assertTrue(video.SDL_WINDOWPOS_CENTERED_DISPLAY(x) !=
                            video.SDL_WINDOWPOS_UNDEFINED_DISPLAY(x))

    def test_SDL_WINDOWPOS_ISCENTERED(self):
        self.assertTrue(video.SDL_WINDOWPOS_ISCENTERED
                        (video.SDL_WINDOWPOS_CENTERED))
        self.assertFalse(video.SDL_WINDOWPOS_ISCENTERED
                         (video.SDL_WINDOWPOS_UNDEFINED))
        for x in range(0xFFFF):
            self.assertTrue(video.SDL_WINDOWPOS_ISCENTERED
                            (video.SDL_WINDOWPOS_CENTERED_DISPLAY(x)))

    def test_SDL_DisplayMode(self):
        mode = video.SDL_DisplayMode()
        self.assertIsInstance(mode, video.SDL_DisplayMode)
        for fmt in range(0, 10):
            for w in range(0, 20):
                for h in range(0, 30):
                    for r in range(0, 40):
                        mode = video.SDL_DisplayMode(fmt, w, h, r)
                        self.assertIsInstance(mode, video.SDL_DisplayMode)
                        self.assertEqual(mode.format, fmt)
                        self.assertEqual(mode.w, w)
                        self.assertEqual(mode.h, h)
                        self.assertEqual(mode.refresh_rate, r)
        self.assertRaises(TypeError, video.SDL_DisplayMode, "Test")
        self.assertRaises(TypeError, video.SDL_DisplayMode, "Test", 10, 10, 10)
        self.assertRaises(TypeError, video.SDL_DisplayMode, 10, "Test", 10, 10)
        self.assertRaises(TypeError, video.SDL_DisplayMode, 10, 10, "Test", 10)
        self.assertRaises(TypeError, video.SDL_DisplayMode, 10, 10, 10, "Test")
        self.assertRaises(TypeError, video.SDL_DisplayMode, None)
        self.assertRaises(TypeError, video.SDL_DisplayMode, None, 10, 10, 10)
        self.assertRaises(TypeError, video.SDL_DisplayMode, 10, None, 10, 10)
        self.assertRaises(TypeError, video.SDL_DisplayMode, 10, 10, None, 10)
        self.assertRaises(TypeError, video.SDL_DisplayMode, 10, 10, 10, None)

    def test_SDL_DisplayMode__eq__(self):
        DMode = video.SDL_DisplayMode
        self.assertTrue(DMode() == DMode())
        self.assertTrue(DMode(10, 0, 0, 0) == DMode(10, 0, 0, 0))
        self.assertTrue(DMode(10, 10, 0, 0) == DMode(10, 10, 0, 0))
        self.assertTrue(DMode(10, 10, 10, 0) == DMode(10, 10, 10, 0))
        self.assertTrue(DMode(10, 10, 10, 10) == DMode(10, 10, 10, 10))
        self.assertTrue(DMode(0, 10, 0, 0) == DMode(0, 10, 0, 0))
        self.assertTrue(DMode(0, 0, 10, 0) == DMode(0, 0, 10, 0))
        self.assertTrue(DMode(0, 0, 0, 10) == DMode(0, 0, 0, 10))

        self.assertFalse(DMode() == DMode(10, 0, 0, 0))
        self.assertFalse(DMode(10, 0, 0, 0) == DMode(0, 0, 0, 0))
        self.assertFalse(DMode(10, 0, 0, 0) == DMode(0, 10, 0, 0))
        self.assertFalse(DMode(10, 0, 0, 0) == DMode(0, 0, 10, 0))
        self.assertFalse(DMode(10, 0, 0, 0) == DMode(0, 0, 0, 10))

    def test_SDL_DisplayMode__ne__(self):
        DMode = video.SDL_DisplayMode
        self.assertFalse(DMode() != DMode())
        self.assertFalse(DMode(10, 0, 0, 0) != DMode(10, 0, 0, 0))
        self.assertFalse(DMode(10, 10, 0, 0) != DMode(10, 10, 0, 0))
        self.assertFalse(DMode(10, 10, 10, 0) != DMode(10, 10, 10, 0))
        self.assertFalse(DMode(10, 10, 10, 10) != DMode(10, 10, 10, 10))
        self.assertFalse(DMode(0, 10, 0, 0) != DMode(0, 10, 0, 0))
        self.assertFalse(DMode(0, 0, 10, 0) != DMode(0, 0, 10, 0))
        self.assertFalse(DMode(0, 0, 0, 10) != DMode(0, 0, 0, 10))

        self.assertTrue(DMode() != DMode(10, 0, 0, 0))
        self.assertTrue(DMode(10, 0, 0, 0) != DMode(0, 0, 0, 0))
        self.assertTrue(DMode(10, 0, 0, 0) != DMode(0, 10, 0, 0))
        self.assertTrue(DMode(10, 0, 0, 0) != DMode(0, 0, 10, 0))
        self.assertTrue(DMode(10, 0, 0, 0) != DMode(0, 0, 0, 10))

    def test_SDL_Window(self):
        window = video.SDL_Window()
        self.assertIsInstance(window, video.SDL_Window)

    def test_SDL_GetNumVideoDrivers(self):
        numdrivers = video.SDL_GetNumVideoDrivers()
        self.assertGreaterEqual(numdrivers, 1)

    def test_SDL_GetVideoDriver(self):
        numdrivers = video.SDL_GetNumVideoDrivers()
        for i in range(numdrivers):
            name = video.SDL_GetVideoDriver(i)
            self.assertIn(type(name), (str, bytes))

    def test_SDL_GetCurrentVideoDriver(self):
        curdriver = video.SDL_GetCurrentVideoDriver()
        found = False
        numdrivers = video.SDL_GetNumVideoDrivers()
        for i in range(numdrivers):
            name = video.SDL_GetVideoDriver(i)
            if name == curdriver:
                found = True
                break
        self.assertTrue(found, "Current video driver not found")

    def test_SDL_GetNumVideoDisplays(self):
        numdisplays = video.SDL_GetNumVideoDisplays()
        self.assertGreaterEqual(numdisplays, 1)

    def test_SDL_GetNumDisplayModes(self):
        numdisplays = video.SDL_GetNumVideoDisplays()
        for index in range(numdisplays):
            modes = video.SDL_GetNumDisplayModes(index)
            self.assertGreaterEqual(modes, 1)

    def test_SDL_GetDisplayMode(self):
        numdisplays = video.SDL_GetNumVideoDisplays()
        for index in range(numdisplays):
            modes = video.SDL_GetNumDisplayModes(index)
            for mode in range(modes):
                dmode = video.SDL_DisplayMode()
                ret = video.SDL_GetDisplayMode(index, mode, byref(dmode))
                self.assertEqual(ret, 0)

    def test_SDL_GetCurrentDisplayMode(self):
        numdisplays = video.SDL_GetNumVideoDisplays()
        for index in range(numdisplays):
            dmode = video.SDL_DisplayMode()
            ret = video.SDL_GetCurrentDisplayMode(index, byref(dmode))
            self.assertEqual(ret, 0)

    def test_SDL_GetDesktopDisplayMode(self):
        numdisplays = video.SDL_GetNumVideoDisplays()
        for index in range(numdisplays):
            dmode = video.SDL_DisplayMode()
            ret = video.SDL_GetDesktopDisplayMode(index, byref(dmode))
            self.assertEqual(ret, 0)

    def test_SDL_GetClosestDisplayMode(self):
        numdisplays = video.SDL_GetNumVideoDisplays()
        for index in range(numdisplays):
            modes = video.SDL_GetNumDisplayModes(index)
            dmode = video.SDL_DisplayMode()
            for mode in range(modes):
                ret = video.SDL_GetDisplayMode(index, mode, byref(dmode))
                #self.assertIsInstance(dmode.contents, video.SDL_DisplayMode)
                self.assertEqual(ret, 0)
                cmode = video.SDL_DisplayMode(dmode.format,
                                              dmode.w - 1, dmode.h - 1,
                                              dmode.refresh_rate)
                closest = video.SDL_DisplayMode()
                video.SDL_GetClosestDisplayMode(index, cmode, byref(closest))
                self.assertEqual(closest, dmode)

    def test_SDL_VideoInit(self):
        video.SDL_VideoInit(None)
        video.SDL_VideoInit(None)
        video.SDL_VideoInit(None)
        video.SDL_VideoQuit(None)
        video.SDL_VideoInit(None)

    def test_SDL_VideoQuit(self):
        video.SDL_VideoQuit()
        video.SDL_VideoQuit()
        video.SDL_VideoQuit()
        video.SDL_VideoInit(None)

    def test_SDL_GetDisplayBounds(self):
        numdisplays = video.SDL_GetNumVideoDisplays()
        for index in range(numdisplays):
            bounds = rect.SDL_Rect()
            ret = video.SDL_GetDisplayBounds(index, byref(bounds))
            self.assertEqual(ret, 0)
            self.assertFalse(rect.SDL_RectEmpty(bounds))

    def test_screensaver(self):
        initial = video.SDL_IsScreenSaverEnabled()
        self.assertIn(initial, (SDL_FALSE, SDL_TRUE))

        video.SDL_EnableScreenSaver()
        self.assertEqual(video.SDL_IsScreenSaverEnabled(), SDL_TRUE)
        video.SDL_EnableScreenSaver()
        self.assertEqual(video.SDL_IsScreenSaverEnabled(), SDL_TRUE)
        video.SDL_DisableScreenSaver()
        self.assertEqual(video.SDL_IsScreenSaverEnabled(), SDL_FALSE)
        video.SDL_DisableScreenSaver()
        self.assertEqual(video.SDL_IsScreenSaverEnabled(), SDL_FALSE)
        video.SDL_EnableScreenSaver()
        self.assertEqual(video.SDL_IsScreenSaverEnabled(), SDL_TRUE)
        video.SDL_DisableScreenSaver()
        self.assertEqual(video.SDL_IsScreenSaverEnabled(), SDL_FALSE)

        if initial == SDL_TRUE:
            video.SDL_EnableScreenSaver()
        else:
            video.SDL_DisableScreenSaver()

    def test_SDL_CreateWindow(self):
        # Borderless to ensure that the size check works
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 10, 11, 12, 13, flag)
            self.assertIsInstance(window.contents, video.SDL_Window)
            px, py = c_int(), c_int()
            video.SDL_GetWindowPosition(window, byref(px), byref(py))
            self.assertEqual((px.value, py.value), (10, 11))
            video.SDL_GetWindowSize(window, byref(px), byref(py))
            self.assertEqual((px.value, py.value), (12, 13))
            self.assertEqual(video.SDL_GetWindowFlags(window) & flag, flag)
            self.assertEqual(video.SDL_GetWindowTitle(window), b"Test")
            video.SDL_DestroyWindow(window)
        # TODO

    def test_SDL_DestroyWindow(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, flag)
            # TODO: how to check for this in a meaningful way?
            video.SDL_DestroyWindow(window)

    @unittest.skip("not implemented")
    def test_SDL_CreateWindowFrom(self):
        pass

    def test_SDL_GetWindowDisplayIndex(self):
        numdisplays = video.SDL_GetNumVideoDisplays()
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 10, 11, 12, 13, flag)
            self.assertIsInstance(window.contents, video.SDL_Window)
            px, py = c_int(), c_int()
            video.SDL_GetWindowPosition(window, byref(px), byref(py))
            self.assertEqual((px.value, py.value), (10, 11))
            video.SDL_GetWindowSize(window, byref(px), byref(py))
            self.assertEqual((px.value, py.value), (12, 13))
            self.assertEqual(video.SDL_GetWindowFlags(window) & flag, flag)
            self.assertEqual(video.SDL_GetWindowTitle(window), b"Test")

            dindex = video.SDL_GetWindowDisplayIndex(window)
            self.assertTrue(0 <= dindex <= numdisplays,
                            "Invalid display index")
            video.SDL_DestroyWindow(window)
            #self.assertRaises(sdl.SDLError, video.SDL_GetWindowDisplay,
            #                  window)

    def test_SDL_GetWindowDisplayMode(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, flag)
            dmode = video.SDL_DisplayMode()
            ret = video.SDL_GetWindowDisplayMode(window, byref(dmode))
            self.assertEqual(ret, 0)
            video.SDL_DestroyWindow(window)
            #self.assertRaises(sdl.SDLError, video.SDL_GetWindowDisplayMode,
            #                  window)

    def test_SDL_SetWindowDisplayMode(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, flag)
            dindex = video.SDL_GetWindowDisplayIndex(window)
            dmode = video.SDL_DisplayMode()

            ret = video.SDL_GetCurrentDisplayMode(dindex, byref(dmode))
            self.assertEqual(ret, 0)

            video.SDL_SetWindowDisplayMode(window, dmode)
            wmode = video.SDL_DisplayMode()
            ret = video.SDL_GetWindowDisplayMode(window, byref(wmode))
            self.assertEqual(ret, 0)
            # TODO: refresh rates differ
            #self.assertEqual(dmode, wmode)

            video.SDL_DestroyWindow(window)
            #self.assertRaises(sdl.SDLError, video.SDL_SetWindowDisplayMode,
            #                  window, dmode)

    def test_SDL_GetWindowPixelFormat(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, flag)
            fmt = video.SDL_GetWindowPixelFormat(window)
            self.assertTrue(type(fmt) in(int, long))
            video.SDL_DestroyWindow(window)
            #self.assertRaises(sdl.SDLError, video.SDL_GetWindowPixelFormat,
            #                  window)

    def test_SDL_GetWindowID(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, flag)
            self.assertGreaterEqual(video.SDL_GetWindowID(window), 0)

            video.SDL_DestroyWindow(window)
            #self.assertRaises(sdl.SDLError, video.SDL_GetWindowID, window)

    def test_SDL_GetWindowFromID(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, flag)
            window2 = video.SDL_GetWindowFromID(video.SDL_GetWindowID(window))
            self.assertEqual(video.SDL_GetWindowID(window),
                             video.SDL_GetWindowID(window2))
            self.assertEqual(video.SDL_GetWindowTitle(window),
                             video.SDL_GetWindowTitle(window2))
            px1, py1, px2, py2 = c_int(), c_int(), c_int(), c_int()
            video.SDL_GetWindowPosition(window, byref(px1), byref(px2))
            video.SDL_GetWindowPosition(window2, byref(px2), byref(px2))
            self.assertEqual((px1.value, py1.value), (px2.value, py2.value))
            video.SDL_GetWindowSize(window, byref(px1), byref(px2))
            video.SDL_GetWindowSize(window2, byref(px2), byref(px2))
            self.assertEqual((px1.value, py1.value), (px2.value, py2.value))

    def test_SDL_GetWindowFlags(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN,
                 video.SDL_WINDOW_RESIZABLE)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, flag)
            wflags = video.SDL_GetWindowFlags(window)
            self.assertEqual((wflags & flag), flag)

    def test_SDL_GetSetWindowTitle(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, 0)
        self.assertEqual(video.SDL_GetWindowTitle(window), b"Test")
        video.SDL_SetWindowTitle(window, b"Hello there")
        self.assertEqual(video.SDL_GetWindowTitle(window), b"Hello there")
        video.SDL_DestroyWindow(window)
        #self.assertRaises(sdl.SDLError, video.SDL_GetWindowTitle, window)

    def test_SDL_SetWindowIcon(self):
        sf = surface.SDL_CreateRGBSurface(0, 16, 16, 16, 0xF000, 0x0F00,
                                          0x00F0, 0x000F)
        self.assertIsInstance(sf.contents, surface.SDL_Surface)
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, 0)
        video.SDL_SetWindowIcon(window, sf)

        #self.assertRaises((AttributeError, TypeError),
        #                  video.SDL_SetWindowIcon, None, None)
        #self.assertRaises((AttributeError, TypeError),
        #                  video.SDL_SetWindowIcon, window, None)
        #self.assertRaises((AttributeError, TypeError),
        #                  video.SDL_SetWindowIcon, None, sf)
        ##self.assertRaises((AttributeError, TypeError),
        #                  video.SDL_SetWindowIcon, window, "Test")
        #self.assertRaises((AttributeError, TypeError),
        #                  video.SDL_SetWindowIcon, window, 123456)

    @unittest.skipIf(hasattr(sys, "pypy_version_info"),
                     "PyPy can't create proper py_object() values")
    def test_SDL_GetSetWindowData(self):
        #TODO: fix this
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, 0)
        self.assertIsInstance(window.contents, video.SDL_Window)
        values = {b"text": py_object("Teststring"),
                  b"object": py_object(self),
                  b"list": py_object([1, 2, 3, 4]),
                  b"tuple": py_object(("a", 1, self))
                  }

        for k, v in values.items():
            retval = video.SDL_GetWindowData(window, k)
            self.assertFalse(retval)
            video.SDL_SetWindowData(window, k, v)
            retval = video.SDL_GetWindowData(window, k)
            self.assertEqual(retval.contents.value, v.value)
        video.SDL_DestroyWindow(window)

    def test_SDL_GetSetWindowPosition(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, 0)
        px, py = c_int(), c_int()
        video.SDL_GetWindowPosition(window, byref(px), byref(py))
        self.assertEqual((px.value, py.value), (10, 10))
        video.SDL_SetWindowPosition(window, 0, 0)
        video.SDL_GetWindowPosition(window, byref(px), byref(py))
        self.assertEqual((px.value, py.value), (0, 0))
        video.SDL_SetWindowPosition(window, 600, 900)
        video.SDL_GetWindowPosition(window, byref(px), byref(py))
        self.assertEqual((px.value, py.value), (600, 900))
        video.SDL_SetWindowPosition(window, -200, -10)
        video.SDL_GetWindowPosition(window, byref(px), byref(py))
        self.assertEqual((px.value, py.value), (-200, -10))
        video.SDL_DestroyWindow(window)

    def test_SDL_GetSetWindowSize(self):
        flags = video.SDL_WINDOW_BORDERLESS
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10, flags)
        sx, sy = c_int(), c_int()
        video.SDL_GetWindowSize(window, byref(sx), byref(sy))
        self.assertEqual((sx.value, sy.value), (10, 10))
        video.SDL_SetWindowSize(window, 1, 1)
        video.SDL_GetWindowSize(window, byref(sx), byref(sy))
        self.assertEqual((sx.value, sy.value), (1, 1))
        video.SDL_SetWindowSize(window, 600, 900)
        video.SDL_GetWindowSize(window, byref(sx), byref(sy))
        self.assertEqual((sx.value, sy.value), (600, 900))
        video.SDL_SetWindowSize(window, -200, -10)
        video.SDL_GetWindowSize(window, byref(sx), byref(sy))
        self.assertEqual((sx.value, sy.value), (600, 900))
        video.SDL_DestroyWindow(window)

    @interactive("Was the window shown?")
    def test_SDL_ShowWindow(self):
        window = video.SDL_CreateWindow(b"test_SDL_ShowWindow",
                                        200, 200, 200, 200, 0)
        video.SDL_ShowWindow(window)
        doprint("""Please check, if a window with the title
'test_SDL_ShowWindow' is shown""")
        video.SDL_DestroyWindow(window)

    @interactive("Did the window vanish from your sight and pop up again?")
    def test_SDL_HideWindow(self):
        window = video.SDL_CreateWindow(b"test_SDL_HideWindow",
                                        200, 200, 200, 200, 0)
        video.SDL_ShowWindow(window)
        doprint("""Please check, if a window with the title
'test_SDL_HideWindow' is shown""")
        video.SDL_HideWindow(window)
        doprint("Please check, that the window is not shown anymore")
        video.SDL_ShowWindow(window)
        doprint("Please check, if the window is shown again")
        video.SDL_DestroyWindow(window)

    @interactive("Did the window raise properly?")
    def test_SDL_RaiseWindow(self):
        window = video.SDL_CreateWindow(b"test_SDL_RaiseWindow",
                                        200, 200, 200, 200, 0)
        video.SDL_ShowWindow(window)
        doprint("""Please check, that a window with the title
'test_SDL_RaiseWindow' is shown""")
        doprint("Move another window on top of the window, so it is hidden")
        video.SDL_RaiseWindow(window)
        doprint("The window should be raised to the foreground now")
        video.SDL_DestroyWindow(window)

    @interactive("Was the window maximized?")
    def test_SDL_MaximizeWindow(self):
        window = video.SDL_CreateWindow(b"test_SDL_MaximizeWindow", 200, 200,
                                        200, 200, video.SDL_WINDOW_RESIZABLE)
        video.SDL_ShowWindow(window)
        doprint("""Please check, that a window with the title
'test_SDL_MaximizeWindow' is shown""")
        video.SDL_MaximizeWindow(window)
        doprint("Please check, if the window was maximized properly")
        video.SDL_DestroyWindow(window)

    @interactive("Was the window minimized?")
    def test_SDL_MinimizeWindow(self):
        window = video.SDL_CreateWindow(b"test_SDL_MinimizeWindow", 200, 200,
                                        200, 200, 0)
        video.SDL_ShowWindow(window)
        doprint("""Please check, that a window with the title
'test_SDL_MinimizeWindow' is shown""")
        video.SDL_MinimizeWindow(window)
        doprint("Please check, if the window was minimized properly")
        video.SDL_DestroyWindow(window)

    @interactive("Was the window maximized and restored properly?")
    def test_SDL_RestoreWindow(self):
        window = video.SDL_CreateWindow(b"test_SDL_RestoreWindow", 200, 200,
                                        200, 200, video.SDL_WINDOW_RESIZABLE)
        video.SDL_ShowWindow(window)
        doprint("""Please check, that a window with the title
'test_SDL_RestoreWindow' is shown""")
        video.SDL_MaximizeWindow(window)
        doprint("Please check, if the window was maximized properly")
        video.SDL_RestoreWindow(window)
        doprint("Please check, if the window was restored properly")
        video.SDL_DestroyWindow(window)

    def test_SDL_SetWindowFullscreen(self):
        # TODO: HIDDEN avoids flickering, but is this really a sufficient test?
        flags = (video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN,
                 video.SDL_WINDOW_RESIZABLE | video.SDL_WINDOW_MINIMIZED |
                 video.SDL_WINDOW_HIDDEN)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 0, 0, 1024, 768, flag)
            video.SDL_SetWindowFullscreen(window, True)
            flags = video.SDL_GetWindowFlags(window)
            self.assertEqual(flags & video.SDL_WINDOW_FULLSCREEN,
                             video.SDL_WINDOW_FULLSCREEN)
            video.SDL_SetWindowFullscreen(window, False)
            flags = video.SDL_GetWindowFlags(window)
            self.assertNotEqual(flags & video.SDL_WINDOW_FULLSCREEN,
                                video.SDL_WINDOW_FULLSCREEN)
            video.SDL_DestroyWindow(window)

    def test_SDL_GetWindowSurface(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN,
                 video.SDL_WINDOW_RESIZABLE | video.SDL_WINDOW_MINIMIZED)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 200, 200, 200, 200, flag)
            sf = video.SDL_GetWindowSurface(window)
            self.assertIsInstance(sf.contents, surface.SDL_Surface)
            video.SDL_DestroyWindow(window)
            #self.assertRaises(sdl.SDLError, video.SDL_GetWindowSurface,
            #                  window)

    def test_SDL_UpdateWindowSurface(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN,
                 video.SDL_WINDOW_RESIZABLE | video.SDL_WINDOW_MINIMIZED)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 200, 200, 200, 200, flag)
            video.SDL_UpdateWindowSurface(window)
            video.SDL_DestroyWindow(window)

    def test_SDL_UpdateWindowSurfaceRects(self):
        rectlist = (rect.SDL_Rect * 4)(rect.SDL_Rect(),
                                       rect.SDL_Rect(10, 10, 10, 10),
                                       rect.SDL_Rect(0, 0, 5, 4),
                                       rect.SDL_Rect(-5, -5, 6, 2))
        rptr = cast(rectlist, POINTER(rect.SDL_Rect))

        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN,
                 video.SDL_WINDOW_RESIZABLE | video.SDL_WINDOW_MINIMIZED)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 200, 200, 200, 200, flag)
            #self.assertRaises(sdl.SDLError,
            #                  video.SDL_UpdateWindowSurfaceRects,
            #                  window, rectlist)
            sf = surface.SDL_Surface()
            video.SDL_GetWindowSurface(window, byref(sf))
            ret = video.SDL_UpdateWindowSurfaceRects(window, rptr, 4)
            self.assertEqual(ret, 0)
            video.SDL_DestroyWindow(window)

    def test_SDL_GetSetWindowGrab(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN,
                 video.SDL_WINDOW_RESIZABLE | video.SDL_WINDOW_MINIMIZED)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 200, 200, 200, 200, flag)
            self.assertFalse(video.SDL_GetWindowGrab(window))
            video.SDL_SetWindowGrab(window, SDL_TRUE)
            self.assertEqual(video.SDL_GetWindowGrab(window), SDL_TRUE)
            video.SDL_SetWindowGrab(window, SDL_FALSE)
            self.assertEqual(video.SDL_GetWindowGrab(window), SDL_FALSE)

    def test_SDL_GetSetWindowBrightness(self):
        flags = (video.SDL_WINDOW_BORDERLESS,
                 video.SDL_WINDOW_BORDERLESS | video.SDL_WINDOW_HIDDEN,
                 video.SDL_WINDOW_RESIZABLE | video.SDL_WINDOW_MINIMIZED)
        for flag in flags:
            window = video.SDL_CreateWindow(b"Test", 200, 200, 200, 200, flag)
            orig = video.SDL_GetWindowBrightness(window)
            self.assertIsInstance(orig, float)
            # Go from 0.0, 0.1 ... to 3.0
            gammas = (x * 0.1 for x in range(0, 20))
            count = 0
            for b in gammas:
                ret = video.SDL_SetWindowBrightness(window, b)
                if ret == 0:
                    val = video.SDL_GetWindowBrightness(window)
                    self.assertAlmostEqual(val, b)
                    count += 1
            # At least one gamma(1.0) must have worked.
            self.assertTrue(count > 0)

    @unittest.skip("not implemented")
    def test_SDL_SetWindowGammaRamp(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_GetWindowGammaRamp(self):
        pass

    def test_SDL_GL_LoadUnloadLibrary(self):
        # Try the default library
        self.assertEquals(video.SDL_GL_LoadLibrary(None), 0)
        video.SDL_GL_UnloadLibrary()

        if has_opengl_lib():
            fpath = get_opengl_path().encode("utf-8")
            self.assertEqual(video.SDL_GL_LoadLibrary(fpath), 0)
            video.SDL_GL_UnloadLibrary()

        #self.assertRaises(sdl.SDLError, video.SDL_GL_LoadLibrary, "Test")
        #self.assertRaises(sdl.SDLError, video.SDL_GL_LoadLibrary, False)
        #self.assertRaises(sdl.SDLError, video.SDL_GL_LoadLibrary, 0)

    def test_SDL_GL_GetProcAddress(self):
        procaddr = video.SDL_GL_GetProcAddress(b"glGetString")
        self.assertIsNone(procaddr)

        self.assertEquals(video.SDL_GL_LoadLibrary(None), 0)

        # Behaviour is undefined as long as there is no window and context.
        window = video.SDL_CreateWindow(b"OpenGL", 10, 10, 10, 10,
                                     video.SDL_WINDOW_OPENGL)
        ctx = video.SDL_GL_CreateContext(window)

        procaddr = video.SDL_GL_GetProcAddress(b"glGetString")
        self.assertTrue(procaddr is not None and int(procaddr) != 0)

        procaddr = video.SDL_GL_GetProcAddress(b"glForWhackoPeople")
        self.assertIsNone(procaddr)

        video.SDL_GL_DeleteContext(ctx)
        video.SDL_DestroyWindow(window)
        video.SDL_GL_UnloadLibrary()

        procaddr = video.SDL_GL_GetProcAddress(b"glGetString")
        self.assertIsNone(procaddr)

    def test_SDL_GL_ExtensionSupported(self):
        self.assertFalse(video.SDL_GL_ExtensionSupported(b"GL_EXT_bgra"))

        self.assertEquals(video.SDL_GL_LoadLibrary(None), 0)
        window = video.SDL_CreateWindow(b"OpenGL", 10, 10, 10, 10,
                                     video.SDL_WINDOW_OPENGL)
        ctx = video.SDL_GL_CreateContext(window)

        self.assertTrue(video.SDL_GL_ExtensionSupported(b"GL_EXT_bgra"))

        video.SDL_GL_DeleteContext(ctx)
        video.SDL_DestroyWindow(window)
        video.SDL_GL_UnloadLibrary()

        self.assertFalse(video.SDL_GL_ExtensionSupported(b"GL_EXT_bgra"))

    def test_SDL_GL_GetSetAttribute(self):
        #self.assertRaises(sdl.SDLError, video.SDL_GL_GetAttribute,
        #                  video.SDL_GL_DEPTH_SIZE)
        #self.assertRaises(sdl.SDLError, video.SDL_GL_SetAttribute,
        #                  1455, 24)

        self.assertEquals(video.SDL_GL_LoadLibrary(None), 0)

        window = video.SDL_CreateWindow(b"OpenGL", 10, 10, 10, 10,
                                     video.SDL_WINDOW_OPENGL)
        ctx = video.SDL_GL_CreateContext(window)

        depth = c_int()
        video.SDL_GL_GetAttribute(video.SDL_GL_DEPTH_SIZE, byref(depth))

        video.SDL_GL_DeleteContext(ctx)
        video.SDL_DestroyWindow(window)

        newdepth = 24
        if depth == 8:
            newdepth = 16
        elif depth == 16:
            newdepth = 24
        elif depth == 24:
            newdepth = 16
        video.SDL_GL_SetAttribute(video.SDL_GL_DEPTH_SIZE, newdepth)

        window = video.SDL_CreateWindow(b"OpenGL", 10, 10, 10, 10,
                                     video.SDL_WINDOW_OPENGL)
        ctx = video.SDL_GL_CreateContext(window)

        val = c_int()
        video.SDL_GL_GetAttribute(video.SDL_GL_DEPTH_SIZE, byref(val))
        self.assertNotEqual(depth, val)
        self.assertEqual(val.value, newdepth)

        video.SDL_GL_DeleteContext(ctx)
        video.SDL_DestroyWindow(window)
        video.SDL_GL_UnloadLibrary()

    def test_SDL_GL_CreateDeleteContext(self):
        #self.assertRaises((AttributeError, TypeError),
        #                  video.SDL_GL_CreateContext, None)
        #self.assertRaises((AttributeError, TypeError),
        #                  video.SDL_GL_CreateContext, "Test")
        #self.assertRaises((AttributeError, TypeError),
        #                  video.SDL_GL_CreateContext, 1234)

        window = video.SDL_CreateWindow(b"No OpenGL", 10, 10, 10, 10,
                                     video.SDL_WINDOW_BORDERLESS)
        #self.assertRaises(sdl.SDLError, video.SDL_GL_CreateContext, window)
        video.SDL_DestroyWindow(window)

        self.assertEquals(video.SDL_GL_LoadLibrary(None), 0)
        window = video.SDL_CreateWindow(b"OpenGL", 10, 10, 10, 10,
                                     video.SDL_WINDOW_OPENGL)
        ctx = video.SDL_GL_CreateContext(window)

        video.SDL_GL_DeleteContext(ctx)
        video.SDL_DestroyWindow(window)

        window = video.SDL_CreateWindow(b"OpenGL", 10, 10, 10, 10,
                                     video.SDL_WINDOW_OPENGL)
        ctx = video.SDL_GL_CreateContext(window)
        video.SDL_DestroyWindow(window)
        video.SDL_GL_DeleteContext(ctx)

        video.SDL_GL_UnloadLibrary()

    def test_SDL_GL_MakeCurrent(self):
        self.assertEquals(video.SDL_GL_LoadLibrary(None), 0)

        #self.assertRaises((AttributeError, TypeError),
        #                  video.SDL_GL_MakeCurrent, None, None)

        window = video.SDL_CreateWindow(b"No OpenGL", 10, 10, 10, 10,
                                     video.SDL_WINDOW_BORDERLESS)
        #self.assertRaises(sdl.SDLError, video.SDL_GL_CreateContext, window)
        video.SDL_DestroyWindow(window)

        #self.assertRaises((AttributeError, TypeError),
        #                  video.SDL_GL_MakeCurrent, None, None)

        video.SDL_GL_UnloadLibrary()

    def test_SDL_GL_GetSetSwapInterval(self):
        #self.assertRaises(ValueError, video.SDL_GL_SetSwapInterval, None)
        #self.assertRaises(ValueError, video.SDL_GL_SetSwapInterval, "Test")
        #self.assertRaises(ValueError, video.SDL_GL_SetSwapInterval, 1234)

        # No current OpenGL context yet.
        # Might crash on certain platforms, since the internal state of
        # SDL2 does not support calling GL functions without having a
        # GL library loaded.
        # self.assertRaises(sdl.SDLError, video.SDL_GL_SetSwapInterval, 1)
        # self.assertRaises(sdl.SDLError, video.SDL_GL_SetSwapInterval, 0)

        self.assertEquals(video.SDL_GL_LoadLibrary(None), 0)
        window = video.SDL_CreateWindow(b"OpenGL", 10, 10, 10, 10,
                                        video.SDL_WINDOW_OPENGL)
        ctx = video.SDL_GL_CreateContext(window)
        video.SDL_GL_MakeCurrent(window, ctx)

        video.SDL_GL_SetSwapInterval(0)
        self.assertEqual(video.SDL_GL_GetSwapInterval(), 0)
        video.SDL_GL_SetSwapInterval(1)
        self.assertEqual(video.SDL_GL_GetSwapInterval(), 1)

        video.SDL_GL_DeleteContext(ctx)
        video.SDL_DestroyWindow(window)
        video.SDL_GL_UnloadLibrary()

    def test_SDL_GL_SwapWindow(self):
        self.assertEquals(video.SDL_GL_LoadLibrary(None), 0)
        window = video.SDL_CreateWindow(b"OpenGL", 10, 10, 10, 10,
                                        video.SDL_WINDOW_OPENGL)
        ctx = video.SDL_GL_CreateContext(window)
        video.SDL_GL_MakeCurrent(window, ctx)
        video.SDL_GL_SwapWindow(window)
        video.SDL_GL_SwapWindow(window)
        video.SDL_GL_SwapWindow(window)
        video.SDL_GL_SwapWindow(window)
        video.SDL_GL_DeleteContext(ctx)
        video.SDL_DestroyWindow(window)
        video.SDL_GL_UnloadLibrary()


if __name__ == '__main__':
    sys.exit(unittest.main())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.