.. -*-doctest-*-

====================================
Plone4Artists Audio Plone Functional
====================================

This functional test assumes a plone site has already been installed
and that we're ready to actually start dealing with audio files.

Set Up
======

Begin by setting up our Manager user.

    >>> uf = app.acl_users
    >>> uf._doAddUser('admin', 'admin', ['Manager'], [])

Next we install the product.

    >>> from p4a.ploneaudio import sitesetup
    >>> sitesetup.setup_portal(app.plone)

And of course we get the test browser.

    >>> import Products.Five.testbrowser
    >>> portal_url = portal.absolute_url()
    >>> browser = Products.Five.testbrowser.Browser()
    >>> browser.addHeader('Authorization', 'Basic admin:admin')

Smart Folders
-------------

Start by creating a new smart folder.

    >>> browser.open(portal_url)
    >>> addtopic = browser.getLink('Collection')
    >>> addtopic.click()
    >>> browser.getControl(name='title').value = 'Test Smart Audio Album'
    >>> browser.getControl('Save').click()

At this point we're inside an empty smart folder.  The first test here is to
*audio activate* this smart folder.  And change display to 'Album View'. It
is necessary to go through the 'Display' form as test browser cannot follow
the Javascript links in Plone.

    >>> browser.getLink('Audio Container').click()


Audio Folders
-------------

We begin by setting up a test folder.

    >>> browser.open(portal_url)
    >>> browser.getLink('Folder').click()
    >>> browser.getControl(name='title').value = 'Test Audio Album'
    >>> browser.getControl('Save').click()

At this point we're inside an empty folder.  The first test here is to
*audio activate* this folder.

    >>> browser.getLink('Audio Container').click()

Since we have a folder we will add new mp3 file. First create file object:

    >>> import Globals
    >>> import os
    >>> from StringIO import StringIO

    >>> maindir = Globals.package_home({'__name__': 'p4a.audio'})
    >>> samplesdir = os.path.join(maindir, 'tests', 'samples')


Creation of MP3 File
=======================

We get one of the example mp3's to use first.

    >>> mp3 = open(os.path.join(samplesdir, 'test-full.mp3'))

First we need to open folder view

    >>> browser.open(portal_url + '/test-audio-album')

Then we need to upload it to the form

   >>> browser.getLink('File').click()
   >>> browser.getControl(name='title').value = 'Test MP3 File'
   >>> browser.getControl(name='description').value = 'Test description'

   >>> upload_control = browser.getControl(name='file_file')
   >>> upload_file = upload_control.mech_control
   >>> upload_file.add_file(mp3,filename='test-full.mp3')

Finally save the form and close the file.

   >>> browser.getControl(name='form_submit').click()
   >>> mp3.close()
   

   >>> 'Changes saved.' in browser.contents
   True

Since we have uploaded mp3 file we should get all meta info.

    >>> '/test-full.mp3/viewimage?field=p4a.audio.interfaces:IAudio:audio_image' in browser.contents
    True

    >>> 'Rocky Burt' in browser.contents
    True

    >>> 'Emergencies All Around Us' in browser.contents
    True

    >>> '2006' in browser.contents
    True

    >>> 'Vocal' in browser.contents
    True

    >>> 'A test made to test the p4a.audio system' in browser.contents
    True

    >>> '62.1 kB' in browser.contents
    True

    >>> 'MPEG-1 Audio Layer 3 (audio/mpeg)' in browser.contents
    True

    >>> '128 Kbps' in browser.contents
    True

    >>> '44 Khz' in browser.contents
    True

    >>> '00:03 (mm:ss)' in browser.contents
    True


Edit MP3 file
====================
We assume that we have test-audio-album folder with mp3 file inside.
First we need to open the folder
    >>> browser.open(portal_url + '/test-audio-album')

Than we need the mp3 file and edit form

    >>> browser.getLink('Test of the Emercy Broadcast System').click()
    >>> browser.getLink('Edit').click()

Now we will change all fields

    >>> browser.getControl(name='form.title').value = 'First MP3 file with image'
    >>> browser.getControl(name='form.description').value = 'Description of MP3 file with image'
    >>> browser.getControl(name='form.artist').value = 'Burt Rocky'
    >>> browser.getControl(name='form.album').value = 'New Album Title'
    >>> browser.getControl(name='form.year').value = '2007'
    >>> browser.getControl(name='form.genre').value = ['37'] #Sound Clip
    >>> browser.getControl(name='form.file').mech_control.add_file(StringIO())
    >>> browser.getControl(name='form.audio_image').mech_control.add_file(StringIO())
    >>> browser.getControl(name='form.comment').value = 'A test made durring Sorrento Sprint.'
    >>> browser.getControl(name='form.actions.apply').click()

    >>> 'Successfully updated' in browser.contents
    True

Now the new metadata should be found

    >>> 'First MP3 file with image' in browser.contents
    True

    >>> 'Burt Rocky' in browser.contents
    True

    >>> 'Description of MP3 file with image' in browser.contents
    True

    >>> '2007' in browser.contents
    True

    >>> 'Sound Clip' in browser.contents
    True

    >>> 'A test made durring Sorrento Sprint.' in browser.contents
    True

    >>> '62.1 kB' in browser.contents
    True

    >>> 'MPEG-1 Audio Layer 3 (audio/mpeg)' in browser.contents
    True

    >>> '128 Kbps' in browser.contents
    True

    >>> '44 Khz' in browser.contents
    True

    >>> '00:03 (mm:ss)' in browser.contents
    True

Creation of OGG file
=======================

    >>> ogg = open(os.path.join(samplesdir, 'test-full.ogg'))

First we need to open folder view

    >>> browser.open(portal_url + '/test-audio-album')

Then we need to upload in to the form

    >>> browser.getLink(url='createObject?type_name=File').click()
    >>> editform = browser.getForm(name='edit_form')
    >>> editform.getControl(name='title').value = 'Test OGG File'
    >>> editform.getControl(name='description').value = 'Test description'

    >>> upload_control = editform.getControl(name='file_file')
    >>> upload_file = upload_control.mech_control
    >>> upload_file.add_file(ogg,filename='test-full.ogg')


Finally save the form and close the file.

    >>> browser.getControl(name='form_submit').click()
    >>> ogg.close()

Test if there is an Artist
    >>> 'Rocky Burt' in browser.contents
    True

Test if there is a Title
    >>> 'Emergencies All Around Us' in browser.contents
    True

Test if there is a Genre
    'Vocal' in browser.contents
    True

Test if there is the file metadata.
    >>> '60.5 kB' in browser.contents
    True

    >>> 'Ogg Vorbis (application/ogg)' in browser.contents
    True

    >>> '160 Kbps' in browser.contents
    True

    >>> '44 Khz' in browser.contents
    True

    >>> '00:03 (mm:ss)' in browser.contents
    True


Now we are going up to test-audio-album folder

    >>> browser.open(portal_url + '/test-audio-album')


Comments garbled / encoding issues: issue #21
==============================================

We assume that album folder is created and audio activated.
First we need to open folder view

    >>> browser.open(portal_url + '/test-audio-album')

Then we need to create and upload mp3 file.

    >>> u_mp3 = open(os.path.join(samplesdir, 'encode_test.mp3'))

Then we need to upload in to the form

    >>> browser.getLink(url='createObject?type_name=File').click()
    >>> browser.getControl(name='title').value = 'Test u_Mp3 File'
    >>> browser.getControl(name='description').value = 'Test description'

    >>> upload_control = browser.getControl(name='file_file')
    >>> upload_file = upload_control.mech_control
    >>> upload_file.add_file(u_mp3,filename='encode_test.mp3')

Finally save the form and close the file.

    >>> browser.getControl(name='form_submit').click()
    >>> u_mp3.close()

Now check file's title and comment

    >>> 'Arnar Jónsson' in browser.contents
    True

    >>> 'XARadio - 12spora útvarp' in browser.contents
    True

File with invalid genre: issue #38
==================================

If an MP3 file has an invalid genre, it should be handled gracefully.

We assume that album folder is created and audio activated.
First we need to open folder view

    >>> browser.open(portal_url + '/test-audio-album')

Then we need to create and upload mp3 file.

    >>> dummy_genre = open(os.path.join(samplesdir, 'test-full-dummy-genre.mp3'))

Then we need to upload in to the form

    >>> browser.getLink(url='createObject?type_name=File').click()
    >>> browser.getControl(name='title').value = 'Test Dummy Genre File'
    >>> browser.getControl(name='description').value = 'Test description'

    >>> upload_control = browser.getControl(name='file_file')
    >>> upload_file = upload_control.mech_control
    >>> upload_file.add_file(dummy_genre,filename='test-full-dummy-genre.mp3')

Finally save the form and close the file.

    >>> browser.getControl(name='form_submit').click()
    >>> dummy_genre.close()

Now check file's genre to make sure that it is empty.

    >>> '<dd class="audio-genre"></dd>' in browser.contents
    True


BTreeFolder Support
===================

First enable the adding of large plone folders via the portal_types

    >>> portal_types = app.plone.portal_types
    >>> portal_types['Large Plone Folder'].global_allow = True

Visit the homepage, add a large folder, and finally audio-activiate it.

    >>> browser.open(portal_url)
    >>> browser.getLink('Large Folder').click()
    >>> browser.getControl(name='title').value = 'Large Music Folder'
    >>> browser.getControl(name='form_submit').click()
    >>> browser.getLink(url='change_type?subtype=p4a.audio.').click()


Activating a folder as audio also activates video:  issue #28
==============================================================

Visit the test-audio-album folder and check whether there are
video-strings present.

    >>> browser.open(portal_url + '/test-audio-album')
    >>> 'Deactivate Video' in browser.contents
    False
    >>> 'video-config.html' in browser.contents
    False
