summaryrefslogtreecommitdiff
path: root/src/unittest/testwebvi.py
blob: 2b53d605826369a8d6ca1d36a868966e6ed76c36 (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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-

# This file is part of vdr-webvideo-plugin.
#
# Copyright 2009-2012 Antti Ajanki <antti.ajanki@iki.fi>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Blackbox tests for each of thee supported video sites and webvicli.

Mainly useful for checking if the web sites have changed so much that
the XSLT templates don't match to them anymore. Requires network
connection because the tests automatically connect and navigate
through links on the video sites.
"""

import unittest
import sys
import re
import urllib
from urlparse import urlparse

sys.path.append('../webvicli')
sys.path.append('../libwebvi')
from webvicli import client, menu
import webvi.api
from webvi.constants import WebviConfig

class TestServiceModules(unittest.TestCase):

    # ========== Helper functions ==========

    def setUp(self):
        webvi.api.set_config(WebviConfig.TEMPLATE_PATH, '../../templates')
        self.client = client.WVClient([], {}, {}, False)

    def getLinks(self, menuobj):
        links = []
        for i in xrange(len(menuobj)):
            if isinstance(menuobj[i], menu.MenuItemLink):
                links.append(menuobj[i])
        return links

    def downloadMenuPage(self, reference, menuname):
        (status, statusmsg, menuobj) = self.client.getmenu(reference)
        self.assertEqual(status, 0, 'Unexpected status code %s (%s) in %s menu\nFailed ref was %s' % (status, statusmsg, menuname, reference))
        self.assertNotEqual(menuobj, None, 'Failed to get %s menu' % menuname)
        return menuobj

    def downloadAndExtractLinks(self, reference, minlinks, menuname):
        menuobj = self.downloadMenuPage(reference, menuname)
        links = self.getLinks(menuobj)
        self.assertTrue(len(links) >= minlinks, 'Too few links in %s menu' % menuname)
        return links

    def checkMediaUrl(self, reference):
        streamurl = self.client.get_stream_url(reference)
        self.assertNotEqual(streamurl, None, 'get_stream_url returned None')
        self.assertNotEqual(streamurl, '', 'get_stream_url returned empty string')

    def getServiceReference(self, templatedir):
        service = open(templatedir + '/service.xml').read()
        m = re.search(r'<ref>(.*)</ref>', service)
        self.assertNotEqual(m, None, 'no <ref> in service.xml')
        return m.group(1)

    def urlToWvtref(self, url):
        domain = urlparse(url).netloc.lower()
        if domain == '':
            return None

        return 'wvt:///%s/videopage.xsl?srcurl=%s' % (domain, urllib.quote(url, ''))

    def extractQueryParams(self, ref):
        res = {}
        params = {}
        splitref = ref.split('?', 1)
        if len(splitref) == 1:
            return (res, params)

        for param in splitref[1].split('&'):
            keyval = param.split('=', 1)
            key = keyval[0].lower()
            if len(keyval) == 2:
                val = keyval[1]

            if key == 'param':
                psplit = val.split(',', 1)
                pname = psplit[0].lower()
                if len(psplit) == 2:
                    pvalue = psplit[1]

                params[pname] = pvalue

            res[key] = urllib.unquote(val)
        return (res, params)

    # ========== Tests for supported websites ==========

    def testMainMenu(self):
        self.downloadAndExtractLinks('wvt:///?srcurl=mainmenu', 4, 'main')

    def testYoutube(self):
        # Category page
        ref = self.getServiceReference('../../templates/www.youtube.com')
        links = self.downloadAndExtractLinks(ref, 3, 'category')

        # Navigation page
        # The third one is the first "proper" category. The first and second are "Search" and "All"
        navigationref = links[2].ref
        links = self.downloadAndExtractLinks(navigationref, 2, 'navigation')

        # Video link
        videolink = links[0]
        self.assertNotEqual(videolink.stream, None, 'No media object in a video link')
        self.assertNotEqual(videolink.ref, None, 'No description page in a video link')
        self.checkMediaUrl(videolink.stream)

    def testYoutubeSearch(self):
        menuobj = self.downloadMenuPage('wvt:///www.youtube.com/search.xsl', 'search')
        self.assertTrue(len(menuobj) >= 4, 'Too few items in search menu')

        self.assertTrue(isinstance(menuobj[0], menu.MenuItemTextField))
        self.assertTrue(isinstance(menuobj[1], menu.MenuItemList))
        self.assertTrue(len(menuobj[1].items) >= 4)
        self.assertTrue(isinstance(menuobj[2], menu.MenuItemList))
        self.assertTrue(len(menuobj[2].items) >= 4)
        self.assertTrue(isinstance(menuobj[3], menu.MenuItemSubmitButton))

        # Query term
        menuobj[0].value = 'youtube'
        # Sort by: rating
        menuobj[1].current = 3
        # Uploaded: This month
        menuobj[2].current = 3

        resultref = menuobj[3].activate()
        self.assertNotEqual(resultref, None)
        self.downloadAndExtractLinks(resultref, 1, 'search result')

    def testGoogleSearch(self):
        ref = self.getServiceReference('../../templates/video.google.com')
        menuobj = self.downloadMenuPage(ref, 'search')

        self.assertTrue(isinstance(menuobj[0], menu.MenuItemTextField))
        self.assertTrue(isinstance(menuobj[-1], menu.MenuItemSubmitButton))

        # Query term
        menuobj[0].value = 'google'

        resultref = menuobj[-1].activate()
        self.assertNotEqual(resultref, None)
        self.downloadAndExtractLinks(resultref, 1, 'search result')

    def testMetacafe(self):
        # Category page
        ref = self.getServiceReference('../../templates/www.metacafe.com')
        links = self.downloadAndExtractLinks(ref, 3, 'category')

        # The first is "Search", the second is "Channels" and the
        # third is the first "proper" navigation.
        channelsref = links[1].ref
        navigationref = links[2].ref

        # Navigation page
        links = self.downloadAndExtractLinks(navigationref, 2, 'navigation')

        # Video link
        videolink = links[0]
        self.assertNotEqual(videolink.stream, None, 'No media object in a video link')
        self.assertNotEqual(videolink.ref, None, 'No description page in a video link')
        self.checkMediaUrl(videolink.stream)

        # User channels
        links = self.downloadAndExtractLinks(channelsref, 3, 'channel list')

    def testMetacafeSearch(self):
        menuobj = self.downloadMenuPage('wvt:///www.metacafe.com/search.xsl', 'search')
        self.assertTrue(len(menuobj) >= 4, 'Too few items in search menu')

        self.assertTrue(isinstance(menuobj[0], menu.MenuItemTextField))
        self.assertTrue(isinstance(menuobj[1], menu.MenuItemList))
        self.assertTrue(len(menuobj[1].items) == 3)
        self.assertTrue(isinstance(menuobj[2], menu.MenuItemList))
        self.assertTrue(len(menuobj[2].items) == 4)
        self.assertTrue(isinstance(menuobj[3], menu.MenuItemSubmitButton))

        # Query term
        menuobj[0].value = 'metacafe'
        # Sort by: most discussed
        menuobj[1].current = 2
        # Published: Anytime
        menuobj[2].current = 0

        resultref = menuobj[3].activate()
        self.assertNotEqual(resultref, None)
        self.downloadAndExtractLinks(resultref, 1, 'search result')

    def testYLEAreena(self):
        # Category page
        ref = self.getServiceReference('../../templates/areena.yle.fi')
        links = self.downloadAndExtractLinks(ref, 3, 'category')

        # The first is "Search", the second is "all tv", the rest are
        # navigation links.
        navigationref = links[1].ref

        links = self.downloadAndExtractLinks(navigationref, 2, 'navigation')

        videolink = None
        for link in links:
            if link.stream is not None:
                videolink = link
                break

        self.assertNotEqual(videolink, None, 'No media links')
        self.assertNotEqual(videolink.stream, None, 'No media object in video link')
        self.assertNotEqual(videolink.ref, None, 'No description in video link')

    def testYLEAreenaSearch(self):
        menuobj = self.downloadMenuPage('wvt:///areena.yle.fi/search.xsl', 'search')
        self.assertTrue(len(menuobj) >= 9, 'Too few items in search menu')

        self.assertTrue(isinstance(menuobj[0], menu.MenuItemTextField))
        self.assertTrue(isinstance(menuobj[1], menu.MenuItemList))
        self.assertTrue(isinstance(menuobj[2], menu.MenuItemList))
        self.assertTrue(isinstance(menuobj[3], menu.MenuItemList))
        self.assertTrue(isinstance(menuobj[4], menu.MenuItemList))
        self.assertTrue(isinstance(menuobj[5], menu.MenuItemList))
        self.assertTrue(isinstance(menuobj[6], menu.MenuItemList))
        self.assertTrue(isinstance(menuobj[7], menu.MenuItemList))
        self.assertTrue(isinstance(menuobj[8], menu.MenuItemSubmitButton))

        # Query term
        menuobj[0].value = 'yle'
        # Media: video
        menuobj[1].current = 1
        # Order: alphabetical
        menuobj[2].current = 1
        # Type: Episodes
        menuobj[3].current = 0
        # Channel: all
        menuobj[4].current = 0
        # Language: all
        menuobj[5].current = 0
        # Subtitles: all
        menuobj[6].current = 0
        # Only outside Finland: no
        menuobj[7].current = 0

        resultref = menuobj[8].activate()
        self.assertNotEqual(resultref, None)
        self.downloadAndExtractLinks(resultref, 1, 'search result')

    def testKatsomo(self):
        # Category page
        ref = self.getServiceReference('../../templates/katsomo.fi')
        links = self.downloadAndExtractLinks(ref, 2, 'category')

        # The first is "Search", the rest are navigation links.
        navigationref = links[1].ref

        # Navigation page
        links = self.downloadAndExtractLinks(navigationref, 1, 'navigation')

        # Program page
        links = self.downloadAndExtractLinks(links[0].ref, 1, 'program')

        # Video link
        # The first few links may be navigation links, but there
        # should be video links after them.
        foundVideo = False
        for link in links:
            if link.stream is not None:
                foundVideo = True

        self.assertTrue(foundVideo, 'No a video links in the program page')

    def testKatsomoSearch(self):
        menuobj = self.downloadMenuPage('wvt:///katsomo.fi/search.xsl', 'search')
        self.assertTrue(len(menuobj) >= 2, 'Too few items in search menu')

        self.assertTrue(isinstance(menuobj[0], menu.MenuItemTextField))
        self.assertTrue(isinstance(menuobj[1], menu.MenuItemSubmitButton))

        # Query term
        menuobj[0].value = 'mtv'

        resultref = menuobj[1].activate()
        self.assertNotEqual(resultref, None)
        self.downloadAndExtractLinks(resultref, 1, 'search result')

    def testRuutuFi(self):
        # Category page
        ref = self.getServiceReference('../../templates/www.ruutu.fi')
        links = self.downloadAndExtractLinks(ref, 3, 'category')

        seriesref = links[0].ref

        # Series page
        links = self.downloadAndExtractLinks(seriesref, 1, 'series')

        # Program page
        links = self.downloadAndExtractLinks(links[0].ref, 1, 'program')

        # Video link
        videolink = links[0]
        self.assertNotEqual(videolink.stream, None, 'No media object in a video link')
        self.assertNotEqual(videolink.ref, None, 'No description page in a video link')

        # Direct video page link
        queries, params = self.extractQueryParams(links[0].stream)
        self.assertTrue('srcurl' in queries, 'Required parameter missing in video link')
        videopageref = self.urlToWvtref(queries['srcurl'])
        self.checkMediaUrl(videopageref)

    # def testRuutuFiSearch(self):
    #     menuobj = self.downloadMenuPage('wvt:///www.ruutu.fi/search.xsl', 'search')
    #     self.assertTrue(len(menuobj) >= 2, 'Too few items in search menu')

    #     self.assertTrue(isinstance(menuobj[0], menu.MenuItemTextField))
    #     self.assertTrue(isinstance(menuobj[1], menu.MenuItemSubmitButton))

    #     # Query term
    #     menuobj[0].value = 'nelonen'

    #     resultref = menuobj[1].activate()
    #     self.assertNotEqual(resultref, None)
    #     self.downloadAndExtractLinks(resultref, 1, 'search result')


if __name__ == '__main__':
    testnames = sys.argv[1:]

    if not testnames:
        # Run all tests
        unittest.main()
    else:
        # Run test listed on the command line
        for test in testnames:
            suite = unittest.TestSuite()
            suite.addTest(TestServiceModules(test))
            unittest.TextTestRunner(verbosity=2).run(suite)