summaryrefslogtreecommitdiff
path: root/src/test/AVGAppTest.py
blob: 9178e4c98faa2240fab29c0bc3c0a10fb820591d (plain)
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# libavg - Media Playback Engine.
# Copyright (C) 2003-2014 Ulrich von Zadow
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Current versions can be found at www.libavg.de
#

import os
import time

import libavg
from libavg import avg, Point2D, player
import testcase

g_helper = player.getTestHelper()

TEST_RESOLUTION = (160, 120)

class TestAppBase(libavg.AVGApp):
    @classmethod
    def start(cls, **kwargs):
        with testcase.SuppressOutput():
            super(TestAppBase, cls).start(**kwargs)

    def requestStop(self, timeout=0):
        player.setTimeout(timeout, player.stop)

    def singleKeyPress(self, char):
        g_helper.fakeKeyEvent(avg.Event.KEY_DOWN, ord(char), ord(char), char, ord(char), 
                avg.KEYMOD_NONE)
        g_helper.fakeKeyEvent(avg.Event.KEY_UP, ord(char), ord(char), char, ord(char), 
                avg.KEYMOD_NONE)


class AVGAppTestCase(testcase.AVGTestCase):
    def testMinimal(self):
        class MinimalApp(TestAppBase):
            testInstance = self
            def init(self):
                self.testInstance.assert_(not player.isFullscreen())
                self.requestStop()

        if 'AVG_DEPLOY' in os.environ:
            del os.environ['AVG_DEPLOY']
        MinimalApp.start(resolution=TEST_RESOLUTION)
    
    def testAvgDeploy(self):
        class FullscreenApp(TestAppBase):
            testInstance = self
            def init(self):
                self.testInstance.assert_(player.isFullscreen())
                rootNodeSize = player.getRootNode().size
                self.testInstance.assertEqual(rootNodeSize, resolution)
                self.requestStop()
                
        resolution = player.getScreenResolution()
        os.environ['AVG_DEPLOY'] = '1'
        FullscreenApp.start(resolution=resolution)
        del os.environ['AVG_DEPLOY']

    def testDebugWindowSize(self):
        class DebugwindowApp(TestAppBase):
            testInstance = self
            def init(self):
                self.testInstance.assert_(not player.isFullscreen())
                rootNodeSize = player.getRootNode().size
                self.testInstance.assertEqual(rootNodeSize, TEST_RESOLUTION)
                
                # windowSize = player.getWindowResolution()
                # self.testInstance.assertEqual(windowSize, Point2D(TEST_RESOLUTION)/2)
                self.requestStop()
        
        DebugwindowApp.start(resolution=TEST_RESOLUTION,
                debugWindowSize=Point2D(TEST_RESOLUTION) / 2)
    
    def testScreenshot(self):
        if not(self._isCurrentDirWriteable()):
            self.skip("Current dir not writeable")
            return
            
        expectedFiles = ['screenshot-000.png', 'screenshot-001.png']

        def cleanup():
            for screenshotFile in expectedFiles[::-1]:
                if os.path.exists(screenshotFile):
                    os.unlink(screenshotFile)
            
        def checkCallback():
            for screenshotFile in expectedFiles[::-1]:
                if os.path.exists(screenshotFile):
                    avg.Bitmap(screenshotFile)
                else:
                    raise RuntimeError('Cannot find the expected '
                            'screenshot file %s' % screenshotFile)
            
            player.stop()
            
        class ScreenshotApp(TestAppBase):
            def init(self):
                self.singleKeyPress('s')
                self.singleKeyPress('s')
                self.timeStarted = time.time()
                self.timerId = player.subscribe(player.ON_FRAME, self.onFrame)
            
            def onFrame(self):
                if (os.path.exists(expectedFiles[-1]) or
                        time.time() - self.timeStarted > 1):
                    player.clearInterval(self.timerId)
                    checkCallback()
        
        cleanup()
        ScreenshotApp.start(resolution=TEST_RESOLUTION)
        cleanup()
    
    def testGraphs(self):
        class GraphsApp(TestAppBase):
            def init(self):
                self.enableGraphs()
            
            def enableGraphs(self):
                self.singleKeyPress('f')
                self.singleKeyPress('m')
                player.setTimeout(500, self.disableGraphs)
                
            def disableGraphs(self):
                self.singleKeyPress('m')
                self.singleKeyPress('f')
                self.requestStop()
        
        GraphsApp.start(resolution=TEST_RESOLUTION)
    
    def testToggleKeys(self):
        TOGGLE_KEYS = ['?', 't', 'e']
        class ToggleKeysApp(TestAppBase):
            def init(self):
                self.keys = TOGGLE_KEYS[:]
                player.setTimeout(0, self.nextKey)
            
            def nextKey(self):
                if not self.keys:
                    player.stop()
                else:
                    key = self.keys.pop()
                    self.singleKeyPress(key)
                    player.setTimeout(0, self.nextKey)
    
        ToggleKeysApp.start(resolution=TEST_RESOLUTION)
    
    def testFakeFullscreen(self):
        class FakeFullscreenApp(TestAppBase):
            fakeFullscreen = True
            def init(self):
                player.setTimeout(0, player.stop)
              
        resolution = player.getScreenResolution()
        if os.name == 'nt':
            FakeFullscreenApp.start(resolution=resolution)
        else:
            self.assertException(
                    lambda: FakeFullscreenApp.start(resolution=resolution))
        
def avgAppTestSuite(tests):
    availableTests = (
            'testMinimal',
            'testAvgDeploy',
            'testDebugWindowSize',
            'testScreenshot',
            'testGraphs',
            'testToggleKeys',
            'testFakeFullscreen',
    )
    return testcase.createAVGTestSuite(availableTests, AVGAppTestCase, tests)