<commit_before>#! /usr/bin/env python
# This Python file uses the following encoding: utf-8

#
# Code generator for python ctypes bindings for VLC
# Copyright (C) 2009 the VideoLAN team
# $Id: $
#
# Authors: <NAME> <contact at o<EMAIL>ub<EMAIL>>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
#

"""Unittest module.
"""

import logging
logger = logging.getLogger(__name__)

import ctypes
import re
import os
import unittest

try:
    import urllib.parse as urllib # python3
except ImportError:
    import urllib # python2

try:
    from pathlib import Path
except ImportError:
    Path = None

try:
    import vlc
except ImportError:
    import generated.vlc as vlc

try:
    from generator import generate
except ImportError:
    generate = None
except SyntaxError:
    generate = None

SAMPLE = os.path.join(os.path.dirname(__file__), 'samples/sample.mp4')
print ("Checking " + vlc.__file__)


class TestAuxMethods(unittest.TestCase):
    if Path is not None:
        def test_try_fspath_path_like_object(self):
            test_object = Path('test', 'path')
            result = vlc.try_fspath(test_object)
            self.assertEqual(result, os.path.join('test', 'path'))

    def test_try_fspath_str_object(self):
        test_object = os.path.join('test', 'path')
        result = vlc.try_fspath(test_object)
        self.assertEqual(result, os.path.join('test', 'path'))


class TestVLCAPI(unittest.TestCase):
    #def setUp(self):
    #    self.seq = range(10)
    #self.assert_(element in self.seq)

    # We check enum definitions against hardcoded values. In case of
    # failure, check that the reason is not a change in the .h
    # definitions.
    def test_enum_event_type(self):
        self.assertEqual(vlc.EventType.MediaStateChanged.value, 5)

    def test_enum_meta(self):
        self.assertEqual(vlc.Meta.Description.value, 6)

    def test_enum_state(self):
        self.assertEqual(vlc.State.Playing.value, 3)

    def test_enum_playback_mode(self):
        self.assertEqual(vlc.PlaybackMode.repeat.value, 2)

    def test_enum_marquee_int_option(self):
        self.assertEqual(vlc.VideoMarqueeOption.Size.value, 6)

    if hasattr(vlc, 'AudioOutputDeviceTypes'):
        def test_enum_output_device_type(self):
            self.assertEqual(vlc.AudioOutputDeviceTypes._2F2R.value, 4)

    def test_enum_output_channel(self):
        self.assertEqual(vlc.AudioOutputChannel.Dolbys.value, 5)

    # Basic libvlc tests
    def test_instance_creation(self):
        i=vlc.Instance()
        self.assertTrue(i)

    def test_libvlc_media(self):
        mrl='/tmp/foo.avi'
        i=vlc.Instance()
        m=i.media_new(mrl)
        self.assertEqual(m.get_mrl(), 'file://' + mrl)

    def test_wrapper_media(self):
        mrl = '/tmp/foo.avi'
        m = vlc.Media(mrl)
        self.assertEqual(m.get_mrl(), 'file://' + mrl)

    def test_wrapper_medialist(self):
        mrl1 = '/tmp/foo.avi'
        mrl2 = '/tmp/bar.avi'
        l = vlc.MediaList( [mrl1, mrl2] )
        self.assertEqual(l[1].get_mrl(), 'file://' + mrl2)

    def test_libvlc_player(self):
        mrl='/tmp/foo.avi'
        i=vlc.Instance()
        p=i.media_player_new(mrl)
        self.assertEqual(p.get_media().get_mrl(), 'file://' + mrl)

    def test_libvlc_none_object(self):
        i=vlc.Instance()
        p=i.media_player_new()
        p.set_media(None)
        self.assertEqual(p.get_media(), None)

    def test_libvlc_player_state(self):
        mrl='/tmp/foo.avi'
        i=vlc.Instance()
        p=i.media_player_new(mrl)
        self.assertEqual(p.get_state(), vlc.State.NothingSpecial)

    # Test that the VLC bindings can handle special characters in the filenames
    def test_libvlc_player_special_chars(self):
        mrl = u'/tmp/Test 韓 Korean.mp4'
        i = vlc.Instance()
        m = i.media_new(mrl)
        url_encoded_mrl = urllib.quote(mrl.encode('utf-8'))
        self.assertEqual(m.get_mrl(), 'file://' + url_encoded_mrl)

    def test_libvlc_video_new_viewpoint(self):
        vp = vlc.libvlc_video_new_viewpoint()
        vp.contents.yaw = 2
        self.assertEqual(vp.contents.yaw, 2)

    def no_test_callback(self):

        @vlc.CallbackDecorators.LogCb
        def log_handler(instance, log_level, ctx, fmt, va_list):
            try:
                module, _file, _line = vlc.libvlc_log_get_context(ctx)
            except TypeError:
                print("vlc.libvlc_log_get_context(ctx)")

        instance = vlc.Instance('--vout dummy --aout dummy')
        instance.log_set(log_handler, None)
        player = instance.media_player_new()

    def test_equalizer(self):
        val = 9.5
        eq = vlc.AudioEqualizer()
        self.assertEqual(eq.get_amp_at_index(0), 0)
        eq.set_amp_at_index(val, 1)
        self.assertEqual(eq.get_amp_at_index(1), val)

    def test_tracks_get(self):
        self.assertTrue(os.path.exists(SAMPLE))
        m = vlc.Media(SAMPLE)
        m.parse()
        # Audiotrack is the second one
        audiotrack = list(m.tracks_get())[1]
        self.assertEqual(audiotrack.language, b"eng")
        self.assertEqual(audiotrack.description, b"Stereo")
        self.assertEqual(audiotrack.bitrate, 83051)
        self.assertEqual(m.get_duration(), 5568)

    def test_meta_get(self):
        self.assertTrue(os.path.exists(SAMPLE))
        m = vlc.Media(SAMPLE)
        m.parse()
        self.assertEqual(m.get_meta(vlc.Meta.Title), 'Title')
        self.assertEqual(m.get_meta(vlc.Meta.Artist), 'Artist')
        self.assertEqual(m.get_meta(vlc.Meta.Description), 'Comment')
        self.assertEqual(m.get_meta(vlc.Meta.Album), 'Album')
        self.assertEqual(m.get_meta(vlc.Meta.AlbumArtist), 'Album Artist')
        self.assertEqual(m.get_meta(vlc.Meta.Date), '2013')<commit_msg>python: adapt test to 4.x API<commit_after>#! /usr/bin/env python
# This Python file uses the following encoding: utf-8

#
# Code generator for python ctypes bindings for VLC
# Copyright (C) 2009 the VideoLAN team
# $Id: $
#
# Authors: <NAME> <contact at <EMAIL>>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
#

"""Unittest module.
"""

import logging
logger = logging.getLogger(__name__)

import ctypes
import re
import os
import unittest

try:
    import urllib.parse as urllib # python3
except ImportError:
    import urllib # python2

try:
    from pathlib import Path
except ImportError:
    Path = None

try:
    import vlc
except ImportError:
    import generated.vlc as vlc

try:
    from generator import generate
except ImportError:
    generate = None
except SyntaxError:
    generate = None

SAMPLE = os.path.join(os.path.dirname(__file__), 'samples/sample.mp4')
print ("Checking " + vlc.__file__)


class TestAuxMethods(unittest.TestCase):
    if Path is not None:
        def test_try_fspath_path_like_object(self):
            test_object = Path('test', 'path')
            result = vlc.try_fspath(test_object)
            self.assertEqual(result, os.path.join('test', 'path'))

    def test_try_fspath_str_object(self):
        test_object = os.path.join('test', 'path')
        result = vlc.try_fspath(test_object)
        self.assertEqual(result, os.path.join('test', 'path'))


class TestVLCAPI(unittest.TestCase):
    #def setUp(self):
    #    self.seq = range(10)
    #self.assert_(element in self.seq)

    # We check enum definitions against hardcoded values. In case of
    # failure, check that the reason is not a change in the .h
    # definitions.
    def test_enum_event_type(self):
        self.assertEqual(vlc.EventType.MediaStateChanged.value, 5)

    def test_enum_meta(self):
        self.assertEqual(vlc.Meta.Description.value, 6)

    def test_enum_state(self):
        self.assertEqual(vlc.State.Playing.value, 3)

    def test_enum_playback_mode(self):
        self.assertEqual(vlc.PlaybackMode.repeat.value, 2)

    def test_enum_marquee_int_option(self):
        self.assertEqual(vlc.VideoMarqueeOption.Size.value, 6)

    if hasattr(vlc, 'AudioOutputDeviceTypes'):
        def test_enum_output_device_type(self):
            self.assertEqual(vlc.AudioOutputDeviceTypes._2F2R.value, 4)

    def test_enum_output_channel(self):
        self.assertEqual(vlc.AudioOutputChannel.Dolbys.value, 5)

    # Basic libvlc tests
    def test_instance_creation(self):
        i=vlc.Instance()
        self.assertTrue(i)

    def test_libvlc_media(self):
        mrl='/tmp/foo.avi'
        i=vlc.Instance()
        m=i.media_new(mrl)
        self.assertEqual(m.get_mrl(), 'file://' + mrl)

    def test_wrapper_media(self):
        mrl = '/tmp/foo.avi'
        m = vlc.Media(mrl)
        self.assertEqual(m.get_mrl(), 'file://' + mrl)

    def test_wrapper_medialist(self):
        mrl1 = '/tmp/foo.avi'
        mrl2 = '/tmp/bar.avi'
        l = vlc.MediaList( [mrl1, mrl2] )
        self.assertEqual(l[1].get_mrl(), 'file://' + mrl2)

    def test_libvlc_player(self):
        mrl='/tmp/foo.avi'
        i=vlc.Instance()
        p=i.media_player_new(mrl)
        self.assertEqual(p.get_media().get_mrl(), 'file://' + mrl)

    def test_libvlc_none_object(self):
        i=vlc.Instance()
        p=i.media_player_new()
        p.set_media(None)
        self.assertEqual(p.get_media(), None)

    def test_libvlc_player_state(self):
        mrl='/tmp/foo.avi'
        i=vlc.Instance()
        p=i.media_player_new(mrl)
        self.assertEqual(p.get_state(), vlc.State.NothingSpecial)

    # Test that the VLC bindings can handle special characters in the filenames
    def test_libvlc_player_special_chars(self):
        mrl = u'/tmp/Test 韓 Korean.mp4'
        i = vlc.Instance()
        m = i.media_new(mrl)
        url_encoded_mrl = urllib.quote(mrl.encode('utf-8'))
        self.assertEqual(m.get_mrl(), 'file://' + url_encoded_mrl)

    def test_libvlc_video_new_viewpoint(self):
        vp = vlc.libvlc_video_new_viewpoint()
        vp.contents.f_yaw = 2
        self.assertEqual(vp.contents.f_yaw, 2)

    def no_test_callback(self):

        @vlc.CallbackDecorators.LogCb
        def log_handler(instance, log_level, ctx, fmt, va_list):
            try:
                module, _file, _line = vlc.libvlc_log_get_context(ctx)
            except TypeError:
                print("vlc.libvlc_log_get_context(ctx)")

        instance = vlc.Instance('--vout dummy --aout dummy')
        instance.log_set(log_handler, None)
        player = instance.media_player_new()

    def test_equalizer(self):
        val = 9.5
        eq = vlc.AudioEqualizer()
        self.assertEqual(eq.get_amp_at_index(0), 0)
        eq.set_amp_at_index(val, 1)
        self.assertEqual(eq.get_amp_at_index(1), val)

    def test_tracks_get(self):
        self.assertTrue(os.path.exists(SAMPLE))
        m = vlc.Media(SAMPLE)
        m.parse()
        # Audiotrack is the second one
        audiotrack = list(m.tracks_get())[1]
        self.assertEqual(audiotrack.i_original_fourcc, 0x6134706d)
        self.assertEqual(m.get_duration(), 5568)

    def test_meta_get(self):
        self.assertTrue(os.path.exists(SAMPLE))
        m = vlc.Media(SAMPLE)
        m.parse()
        self.assertEqual(m.get_meta(vlc.Meta.Title), 'Title')
        self.assertEqual(m.get_meta(vlc.Meta.Artist), 'Artist')
        self.assertEqual(m.get_meta(vlc.Meta.Description), 'Comment')
        self.assertEqual(m.get_meta(vlc.Meta.Album), 'Album')
        self.assertEqual(m.get_meta(vlc.Meta.AlbumArtist), 'Album Artist')
        self.assertEqual(m.get_meta(vlc.Meta.Date), '2013')
{'fraction_non_alphanumeric': 0.11295003529688603, 'fraction_numerical': 0.009883128088477527, 'mean_word_length': 2.970725630644659, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1622329', 'n_tokens_mistral': 4365, 'n_tokens_neox': 4198, 'n_words': 936}