~ubuntu-branches/ubuntu/trusty/pyalsaaudio/trusty

« back to all changes in this revision

Viewing changes to test.py

  • Committer: Bazaar Package Importer
  • Author(s): Artur Rona
  • Date: 2010-01-29 19:52:21 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100129195221-l6y4xmauznm77dl3
Tags: 0.5+svn36-1ubuntu1
* Merge from debian testing (LP: #514453, #331171), remaining changes:
  - Call setup.py install with --root= --install-layout=deb.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# These are internal test. They shouldn't fail, but they don't cover all
 
4
# of the ALSA API. Most of all PCM.read and PCM.write are missing.
 
5
# These need to be tested by playbacktest.py and recordtest.py
 
6
 
 
7
# In case of a problem, run these tests. If they fail, file a bug report on
 
8
# http://sourceforge.net/projects/pyalsaaudio
 
9
 
 
10
import unittest
1
11
import alsaaudio
2
 
import sys
3
 
if len(sys.argv) > 1: name = sys.argv[1]
4
 
else: name = "Master"
5
 
 
6
 
m = alsaaudio.Mixer(name)
7
 
 
 
12
 
 
13
 
 
14
# we can't test read and write well - these are tested otherwise
 
15
PCMMethods = [('pcmtype', None),
 
16
              ('pcmmode', None),
 
17
              ('cardname', None),
 
18
              ('setchannels', (2,)), 
 
19
              ('setrate', (44100,)),
 
20
              ('setformat', (alsaaudio.PCM_FORMAT_S8,)),
 
21
              ('setperiodsize', (320,))]
 
22
 
 
23
# A clever test would look at the Mixer capabilities and selectively run the
 
24
# omitted tests, but I am too tired for that.
 
25
 
 
26
MixerMethods = [('cardname', None),
 
27
                ('mixer', None),
 
28
                ('mixerid', None),
 
29
                ('switchcap', None),
 
30
                ('volumecap', None),
 
31
                ('getvolume', None),
 
32
                ('getrange', None),
 
33
                ('getenum', None),
 
34
#                ('getmute', None),
 
35
#                ('getrec', None),
 
36
#                ('setvolume', (60,)),
 
37
#                ('setmute', (0,))
 
38
#                ('setrec', (0')),
 
39
                ]
 
40
 
 
41
class MixerTest(unittest.TestCase):
 
42
    """Test Mixer objects"""
 
43
 
 
44
    def testMixer(self):
 
45
        """Open a Mixer on every card"""
 
46
 
 
47
        # Mixers are addressed by index, not name
 
48
        for i in range(len(alsaaudio.cards())):
 
49
            mixers = alsaaudio.mixers(i)
 
50
            for m in mixers:
 
51
                mixer = alsaaudio.Mixer(m, cardindex=i)
 
52
                mixer.close()
 
53
 
 
54
    def testMixerAll(self):
 
55
        "Run common Mixer methods on an open object"
 
56
 
 
57
        mixers = alsaaudio.mixers()
 
58
        mixer = alsaaudio.Mixer(mixers[0])
 
59
 
 
60
        for m, a in MixerMethods:
 
61
            f = alsaaudio.Mixer.__dict__[m]
 
62
            if a is None:
 
63
                f(mixer)
 
64
            else:
 
65
                f(mixer, *a)
 
66
 
 
67
        mixer.close()
 
68
 
 
69
    def testMixerClose(self):
 
70
        "Run common Mixer methods on a closed object and verify it raises an error"
 
71
 
 
72
        mixers = alsaaudio.mixers()
 
73
        mixer = alsaaudio.Mixer(mixers[0])
 
74
        mixer.close()
 
75
 
 
76
        for m, a in MixerMethods:
 
77
            f = alsaaudio.Mixer.__dict__[m]
 
78
            if a is None:
 
79
                self.assertRaises(alsaaudio.ALSAAudioError, f, mixer)
 
80
            else:
 
81
                self.assertRaises(alsaaudio.ALSAAudioError, f, mixer, *a)
 
82
 
 
83
class PCMTest(unittest.TestCase):
 
84
    """Test PCM objects"""
 
85
 
 
86
    def testPCM(self):
 
87
        "Open a PCM object on every card"
 
88
 
 
89
        for i in range(len(alsaaudio.cards())):
 
90
            pcm = alsaaudio.PCM(i)
 
91
            pcm.close()
 
92
 
 
93
    def testPCMAll(self):
 
94
        "Run all PCM methods on an open object"
 
95
 
 
96
        pcm = alsaaudio.PCM()
 
97
 
 
98
        for m, a in PCMMethods:
 
99
            f = alsaaudio.PCM.__dict__[m]
 
100
            if a is None:
 
101
                f(pcm)
 
102
            else:
 
103
                f(pcm, *a)
 
104
 
 
105
        pcm.close()
 
106
 
 
107
 
 
108
    def testPCMClose(self):
 
109
        "Run all PCM methods on a closed object and verify it raises an error"
 
110
 
 
111
        pcm = alsaaudio.PCM()
 
112
        pcm.close()
 
113
 
 
114
        for m, a in PCMMethods:
 
115
            f = alsaaudio.PCM.__dict__[m]
 
116
            if a is None:
 
117
                self.assertRaises(alsaaudio.ALSAAudioError, f, pcm)
 
118
            else:
 
119
                self.assertRaises(alsaaudio.ALSAAudioError, f, pcm, *a)
 
120
 
 
121
if __name__ == '__main__':
 
122
    unittest.main()