====================================
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)
    >>> browser.getLink('Smart Folder').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.

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

Since the album/smart-folder is currently empty, the total length amount should
be zero.

    >>> 'Audio information unavailable' in browser.contents
    True

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('Activate Audio').click()

Since the album/folder is currently empty, the total length amount should
be zero.

    >>> 'Audio information unavailable' in browser.contents
    True

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 in 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

    >>> 'MP3 (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='_modify_form.file').value = ['0']
    >>> browser.getControl(name='_modify_form.audio_image').value = ['0']
    >>> 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

    >>> '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

    >>> 'MP3 (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('File').click()
    >>> browser.getControl(name='title').value = 'Test OGG 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(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 Year.
    >>> '2007' in browser.contents
    True

Test if there is a track number (TRACKNUMBER)
    >>> '01' in browser.contents
    True

The following tests are not working yet, because we need to add support for this metadata.

Test if there is a track total (TRACKTOTAL)
     '10' in browser.contents
    True

Test if there is a Comment (Ogg calls this DESCRIPTION)
    'Coming to you live from Gander, Newfoundland.' in browser.contents
    True
    
Test if there is a Genre
    'Vocal' in browser.contents
    True

Test if there is a Composer
    'Nate Aune' 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')

And check if we get actual data

    >>> '<dd>00:06 (mm:ss)</dd>' in browser.contents
    True

    >>> 'Test of the Emercy Broadcast System' in browser.contents
    True

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


Finally we should be able to open the file again

    >>> browser.getLink('Test of the Emercy Broadcast System').click()
    >>> 'Test of the Emercy Broadcast System' in browser.contents
    True


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('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('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

There's currently an annoying testbrowser bug that doesn't let us click
on links with spaces in them.  So we'll do a little renaming of the portal
type here.

    >>> self.loginAsPortalOwner()
    >>> portal_types.manage_renameObjects(['Large Plone Folder'], ['LargePloneFolder'])

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

    >>> browser.open(portal_url)
    >>> browser.getLink('Large Folder').click()

When you get the error: " When URLs have spaces in them, they're handled
correctly (before the bug was fixed, you'd get 'ValueError: too many values
to unpack')", this is because of a bug in zope.testbrowser.

    >>> browser.getControl(name='title').value = 'Large Music Folder'
    >>> browser.getControl(name='form_submit').click()
    >>> browser.getLink('Activate 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



 Activate - Deactivate: issue #20
=============================================

Visit the audio-activated folder. The view is currently the
default audio-view. We can check this by calling the getLayout()-method
on the folder:

    >>> browser.open(portal_url + '/test-audio-album')
    >>> music_folder = portal.contentValues(filter= {'portal_type': ('Folder',) } )[0]
    >>> music_folder.getLayout()
    'audio-container.html'

Then we deactivate the audio-folder. The layout should switch back
to the default folder-layout 'folder_listing'.

    >>> browser.getLink('Deactivate Audio').click()
    >>> music_folder.getLayout()
    'folder_listing'

Now we change default view to 'summary view',
this will set an 'layout'-attribute to the folder.

    >>> browser.getLink('Summary view').click()
    >>> music_folder.getLayout()
    'folder_summary_view'

Now we audio-activate the folder again. The new default audio-view should be
 immediately active.

    >>> browser.getLink('Activate Audio').click()
    >>> music_folder.getLayout()
    'audio-container.html'

This will change the layout-attribute on the folder to 'audio-container.html'.

    >>> music_folder.getLayout()
    'audio-container.html'

    >>> 'id="tracklisting"' in browser.contents
    True

    >>> 'title="Deactivate Audio">Deactivate Audio</a>' in browser.contents
    True

Now we audio-deactivate it again.

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