Logo Search packages:      
Sourcecode: magicicada version File versions  Download package

test_magicicada.py

# test_magicicada.py
#
# Author: Natalia Bidart <natalia.bidart@gmail.com>
#
# Copyright 2010 Chicharreros
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.

"""Tests for magicicada."""

from functools import wraps

import gobject
import gtk
import pango

from twisted.trial.unittest import TestCase

from magicicada import MagicicadaUI, CONTENT_QUEUE, META_QUEUE, syncdaemon
from magicicada.dbusiface import QueueData, FolderData, ShareData
from magicicada.helpers import NO_OP, humanize_bytes

def process_gtk_pendings():
    while gtk.events_pending(): gtk.main_iteration()

def close_dialog((dialog, test)):
    """Call the 'test', close 'dialog'."""
    try:
        process_gtk_pendings()
        test()
        process_gtk_pendings()
    finally:
        dialog.response(gtk.RESPONSE_CLOSE)
        process_gtk_pendings()
    return False # do not be called again


00048 class FakedSyncdaemon(object):
    """A faked syncdaemon."""

    def __init__(self):
        self.current_state = syncdaemon.State()
        self.meta_queue = []
        self.content_queue = []
        self.folders = []

        self.on_started_callback = NO_OP
        self.on_stopped_callback = NO_OP
        self.on_connected_callback = NO_OP
        self.on_disconnected_callback = NO_OP
        self.on_online_callback = NO_OP
        self.on_offline_callback = NO_OP
        self.status_changed_callback = NO_OP
        self.content_queue_changed_callback = NO_OP
        self.meta_queue_changed_callback = NO_OP
        self.shutdown = NO_OP

        self.start = lambda: setattr(self.current_state, 'is_started', True)
        self.quit = lambda: setattr(self.current_state, 'is_started', False)
        self.connect = lambda: setattr(self.current_state, 'is_connected', True)
        self.disconnect = \
                    lambda: setattr(self.current_state, 'is_connected', False)


00075 class MagicicadaUITestCase(TestCase):
    """UI test cases for Magicicada UI."""

00078     def setUp(self):
        """Init."""
        self.ui = MagicicadaUI(syncdaemon_class=FakedSyncdaemon)
        self._called = False
        self.set_called = lambda *args, **kwargs: setattr(self, '_called', True)

00084     def tearDown(self):
        """Cleanup."""
        self.ui.on_main_window_destroy(self.ui.main_window)
        self._called = False

00089     def do_start(self):
        """Simulate that start fully happened."""
        self.ui.on_start_clicked(self.ui.start)
        self.ui.on_started()

00094     def do_connect(self):
        """Simulate that connect fully happened."""
        self.do_start()
        self.ui.on_connect_clicked(self.ui.connect)
        self.ui.on_connected()

00100     def build_some_data(self, data_type, limit=5):
        """Build some data using named_tuple 'data_type'."""
        attrs = data_type._fields
        result = []
        for i in xrange(limit):
            kwargs = dict([(attr, '%s %i' % (attr, i)) for attr in attrs])
            result.append(data_type(**kwargs))
        return result

00109     def assert_store_correct(self, store, items):
        """Test that 'store' has 'items' as content."""
        msg = 'amount of rows for %s must be %s (got %s).'
        self.assertEqual(len(store), len(items),
                         msg % (store, len(items), len(store)))
        # assert rows content equal to items content
        tree_iter = store.get_iter_root()
        tmp = list(reversed(items))
        msg = "column %i ('%s') must be '%s' (got '%s' instead)"
        while tree_iter is not None:
            head = tmp.pop()
            for i, field in enumerate(head._fields):
                actual, = store.get(tree_iter, i)
                expected = getattr(head, field)
                if store.get_column_type(i).name == 'gboolean':
                    expected = bool(expected)
                self.assertEqual(expected, actual, msg % (i, field, expected, actual))

            tree_iter = store.iter_next(tree_iter)

00129     def assert_indicator_disabled(self, indicator):
        """Test that 'indicator' is not sensitive."""
        self.assertFalse(indicator.is_sensitive(), 'indicator is not sensitive')

00133     def assert_indicator_ready(self, indicator):
        """Test that 'indicator' is sensitive and green."""
        self.assertTrue(indicator.is_sensitive(), 'indicator is sensitive')
        expected = indicator.get_pixbuf() # a test on its own
        self.assertEqual(self.ui.active_indicator, expected,
                         'indicator is the correct pixbuf')

00140     def assert_indicator_loading(self, indicator):
        """Test that 'indicator' is sensitive and loading."""
        self.assertTrue(indicator.is_sensitive(), 'indicator is sensitive')
        expected = indicator.get_animation() # a test on its own
        self.assertEqual(self.ui.loading_animation, expected,
                         'indicator is the correct animation')


00148 class MagicicadaUIBasicTestCase(MagicicadaUITestCase):
    """UI test cases for basic state."""

00151     def test_init_creates_sd_instance(self):
        """SyncDaemon instance is created at creation time."""
        self.assertTrue(self.ui.sd is not None)
        self.assertTrue(isinstance(self.ui.sd, FakedSyncdaemon))

00156     def test_destroy_shutdowns_sd_instance(self):
        """SyncDaemon instance is shutdown at destroy time."""
        self.patch(self.ui.sd, 'shutdown', self.set_called)
        self.ui.on_main_window_destroy(self.ui.main_window)
        self.assertTrue(self._called,
                        'syncdaemon.shutdown must be called at destroy time.')

00163     def test_main_window_is_visible(self):
        """UI can be created and main_window is visible."""
        self.assertTrue(self.ui.widget_is_visible(self.ui.main_window))

00167     def test_windows_have_correct_icon(self):
        """Every window has the icon set."""
        for w in self.ui.windows:
            self.assertEqual(w.get_icon(), self.ui._icon)

00172     def test_start_connect_are_visible(self):
        """Start and Connect buttons are visible."""
        self.assertTrue(self.ui.widget_is_visible(self.ui.start))
        self.assertTrue(self.ui.start.is_sensitive())

        self.assertTrue(self.ui.widget_is_visible(self.ui.connect))
        self.assertFalse(self.ui.connect.is_sensitive())

00180     def test_stop_disconnect_are_not_visible(self):
        """Start and Connect buttons are visible."""
        self.assertFalse(self.ui.widget_is_visible(self.ui.stop))
        self.assertFalse(self.ui.widget_is_visible(self.ui.disconnect))

00185     def test_indicators_are_non_sensitive(self):
        """Test default sensitivity for indicators."""
        self.assertFalse(self.ui.is_started.is_sensitive())
        self.assertFalse(self.ui.is_connected.is_sensitive())
        self.assertFalse(self.ui.is_online.is_sensitive())

00191     def test_update_is_correct(self):
        """Update is called at startup."""
        self.patch(MagicicadaUI, 'update', self.set_called)
        self.ui = MagicicadaUI(syncdaemon_class=FakedSyncdaemon)
        self.assertTrue(self._called,
                        'update was called at startup.')


00199 class MagicicadaUIClickedTestCase(MagicicadaUITestCase):
    """UI test cases."""

00202     def test_on_start_clicked(self):
        """Test on_start_clicked."""
        self.ui.on_start_clicked(self.ui.start)

        self.assertTrue(self.ui.widget_is_visible(self.ui.start))
        self.assertFalse(self.ui.start.is_sensitive())
        self.assertFalse(self.ui.widget_is_visible(self.ui.stop))

        self.assert_indicator_loading(self.ui.is_started)
        self.assert_indicator_disabled(self.ui.is_connected)
        self.assert_indicator_disabled(self.ui.is_online)

00214     def test_on_start_clicked_starts_syncdaemon(self):
        """Test on_start_clicked."""
        self.patch(self.ui.sd, 'start', self.set_called)
        self.ui.on_start_clicked(self.ui.start)
        self.assertTrue(self._called, 'syncdaemon.start was called.')

00220     def test_on_connect_clicked(self):
        """Test on_connect_clicked."""
        self.do_start() # need to be started
        self.ui.on_connect_clicked(self.ui.connect)

        self.assertTrue(self.ui.widget_is_visible(self.ui.connect))
        self.assertFalse(self.ui.connect.is_sensitive())
        self.assertFalse(self.ui.widget_is_visible(self.ui.disconnect))

        self.assert_indicator_ready(self.ui.is_started)
        self.assert_indicator_loading(self.ui.is_connected)
        self.assert_indicator_disabled(self.ui.is_online)

00233     def test_on_connect_clicked_connects_syncdaemon(self):
        """Test on_connect_clicked."""
        self.patch(self.ui.sd, 'connect', self.set_called)
        self.ui.on_connect_clicked(self.ui.connect)
        self.assertTrue(self._called, 'syncdaemon.connect was called.')

00239     def test_on_stop_clicked(self):
        """Test on_stop_clicked."""
        self.do_start()
        assert not self.ui.widget_enabled(self.ui.disconnect)
        self.patch(self.ui, 'on_disconnect_clicked', self.set_called)
        self.ui.on_stop_clicked(self.ui.stop)

        self.assertFalse(self._called, 'on_disconnect_clicked was not called.')

        self.assertFalse(self.ui.widget_is_visible(self.ui.start))
        self.assertTrue(self.ui.widget_is_visible(self.ui.stop))
        self.assertFalse(self.ui.stop.is_sensitive())

        self.assertTrue(self.ui.widget_is_visible(self.ui.connect))
        self.assertFalse(self.ui.connect.is_sensitive())
        self.assertFalse(self.ui.widget_is_visible(self.ui.disconnect))

00256     def test_on_stop_clicked_if_connected(self):
        """Test on_stop_clicked."""
        self.do_connect()
        self.patch(self.ui, 'on_disconnect_clicked', self.set_called)
        self.ui.on_stop_clicked(self.ui.stop)

        self.assertTrue(self._called, 'on_disconnect_clicked was called.')

00264     def test_on_stop_clicked_stops_syncdaemon(self):
        """Test on_stop_clicked."""
        self.patch(self.ui.sd, 'quit', self.set_called)
        self.ui.on_stop_clicked(self.ui.stop)
        self.assertTrue(self._called, 'syncdaemon.quit was called.')

00270     def test_on_disconnect_clicked(self):
        """Test on_disconnect_clicked."""
        self.do_connect()
        self.ui.on_disconnect_clicked(self.ui.disconnect)

        self.assertFalse(self.ui.widget_is_visible(self.ui.connect))
        self.assertTrue(self.ui.widget_is_visible(self.ui.disconnect))
        self.assertFalse(self.ui.disconnect.is_sensitive())

00279     def test_on_disconnect_clicked_disconnects_syncdaemon(self):
        """Test on_disconnect_clicked."""
        self.patch(self.ui.sd, 'disconnect', self.set_called)
        self.ui.on_disconnect_clicked(self.ui.disconnect)
        self.assertTrue(self._called, 'syncdaemon.disconnect was called.')


00286 class MagicicadaUISystrayIconTestCase(MagicicadaUITestCase):
    """UI test cases for the systray icon."""

00289     def test_main_window_is_hid_when_icon_clicked(self):
        """Main window is hid when the systray icon is clicked."""
        self.ui.on_status_icon_activate(self.ui.status_icon)
        self.assertFalse(self.ui.widget_is_visible(self.ui.main_window),
                         'main_window should be invisible when icon clicked.')

00295     def test_main_window_is_shown_when_clicked_after_hidden(self):
        """Main window is shown when the icon is clicked after hidden."""
        self.ui.on_status_icon_activate(self.ui.status_icon) # hide
        self.ui.on_status_icon_activate(self.ui.status_icon) # show
        msg = 'main_window should be visible when icon clicked after hidden.'
        self.assertTrue(self.ui.widget_is_visible(self.ui.main_window), msg)


def skip_abstract_class(test):
    """If 'test' belongs to an abstract class, don't run it."""
    @wraps(test)
    def inner(klass):
        """Execute 'test' only if not in an abstract class."""
        if klass.name is not None:
            test(klass)
    return inner


00313 class _MagicicadaUIQueueTestCase(MagicicadaUITestCase):
    """Abstratc UI test cases for queue tree views."""

    name = None

00318     def setUp(self):
        """Init."""
        super(_MagicicadaUIQueueTestCase, self).setUp()
        if self.name is None:
            return
        self.sd_changed = getattr(self.ui.sd,
                                  '%s_queue_changed_callback' % self.name)
        self.ui_changed = getattr(self.ui,
                                  'on_%s_queue_changed' % self.name)
        self.set_sd_queue = lambda q: \
                            setattr(self.ui.sd, '%s_queue' % self.name, q)
        self.queue_store = getattr(self.ui, '%sq_store' % self.name)
        self.queue_view = getattr(self.ui, '%sq_view' % self.name)

00332     def build_some_data(self, limit=5):
        """Build some data to act as queue data."""
        kwargs = dict(data_type=QueueData, limit=limit)
        res = super(_MagicicadaUIQueueTestCase, self).build_some_data(**kwargs)
        # operation path share node
        return res

    @skip_abstract_class
00340     def test_callback_is_connected(self):
        """Queue changed callback is connected."""
        self.assertEqual(self.sd_changed, self.ui_changed,
                         '%s queue callback must be set' % self.name)

    @skip_abstract_class
00346     def test_model_is_binded(self):
        """List store is binded."""
        actual = self.queue_view.get_model()
        msg = 'model for view %s differs from %s'
        self.assertEqual(self.queue_store, actual,
                         msg % (self.name, self.queue_store))

    @skip_abstract_class
00354     def test_on_queue_changed_updates_view(self):
        """On queue changed the view is updated."""
        items = self.build_some_data()
        self.sd_changed(items)
        self.assert_store_correct(self.queue_store, items)

    @skip_abstract_class
00361     def test_on_queue_changed_handles_none(self):
        """On queue changed handles None as items."""
        self.sd_changed(None)
        self.assert_store_correct(self.queue_store, [])

    @skip_abstract_class
00367     def test_model_is_cleared_before_updating(self):
        """The model is cleared before upadting with a new set of data."""
        items = self.build_some_data()
        self.sd_changed(items)

        items = self.build_some_data()
        self.sd_changed(items)
        self.assertEqual(len(self.queue_store), len(items))

    @skip_abstract_class
00377     def test_view_is_enabled_if_disabled_on_changed(self):
        """The tree view is enabled on changed if it was disabled."""
        self.assertFalse(self.queue_view.is_sensitive(),
                         'Tree view must be disabled by default.')
        items = self.build_some_data()
        self.sd_changed(items)

        self.assertTrue(self.queue_view.is_sensitive(),
                        'Tree view must be enabled on changed.')

    @skip_abstract_class
00388     def test_update_is_correct_for_queue(self):
        """Correctly updates the queue state."""
        data = self.build_some_data()
        self.set_sd_queue(data)

        self.ui.update()

        self.assert_store_correct(self.queue_store, data)

    @skip_abstract_class
00398     def test_on_stopped_updates_queue(self):
        """On SD stoppped, the UI updates the queue state."""
        cb = 'on_%s_queue_changed' % self.name
        self.patch(self.ui, cb, self.set_called)
        self.ui.on_stopped()
        self.assertTrue(self._called,
                        '%s was called on_stopped.' % cb)

    @skip_abstract_class
00407     def test_label_with_row_count(self):
        """Queue label shows the row count."""
        label = '%sq_label' % self.name
        actual = getattr(self.ui, label).get_text()
        expected = '%s Queue (%i)' % (self.name.capitalize(),
                                      len(self.queue_store))
        msg = '%s should be %s (got %s instead)'
        self.assertEqual(expected, actual, msg % (label, expected, actual))

        items = self.build_some_data()
        self.sd_changed(items)
        self.assertEqual(expected, actual, msg % (label, expected, actual))

    @skip_abstract_class
00421     def test_queue_is_updated_on_started_and_on_stopped(self):
        """Queue store is updated on_started."""
        items = self.build_some_data()
        self.set_sd_queue(items)
        self.do_start()
        self.assert_store_correct(self.queue_store, items)

        items = items[:len(items)/2]
        self.set_sd_queue(items)
        self.ui.on_stop_clicked(self.ui.stop)
        self.ui.on_stopped()
        self.assert_store_correct(self.queue_store, items)


00435 class MagicicadaUIContentQueueTestCase(_MagicicadaUIQueueTestCase):
    """UI test cases for content queue view."""

    name = CONTENT_QUEUE


00441 class MagicicadaUIMetaQueueTestCase(_MagicicadaUIQueueTestCase):
    """UI test cases for meta queue view."""

    name = META_QUEUE


00447 class MagicicadaUIStatusTestCase(MagicicadaUITestCase):
    """UI test cases for the status label."""

00450     def setUp(self):
        """Init."""
        super(MagicicadaUIStatusTestCase, self).setUp()
        name = 'TEST'
        description = 'the status for testing'
        connection = 'Funny funny connection'
        queues = 'The queues are hilarious'
        self.kwargs = dict(name=name, description=description,
                           connection=connection, queues=queues)

00460     def assert_status_label_correct(self, name=None, description=None,
                                    connection=None, queues=None,
                                    expected=None):
        """Test that the status label is of the form name: description."""
        if expected is None:
            assert name is not None
            assert description is not None
            assert connection is not None
            assert queues is not None
            values = (name, description, queues, connection)
            expected = self.ui.STATUS_JOINER.join(values)

        actual = self.ui.status_label.get_text()
        msg = 'status label test must be "%s" (got "%s" instead).'
        self.assertEqual(expected, actual, msg % (expected, actual))

00476     def test_callback_is_connected(self):
        """Status callback is connected."""
        self.assertEqual(self.ui.sd.status_changed_callback,
                         self.ui.on_status_changed,
                         'status_changed callback must be set')

00482     def test_status_label_ellipsizes(self):
        """The status label ellipsizes."""
        expected = pango.ELLIPSIZE_END
        actual = self.ui.status_label.get_ellipsize()
        self.assertEqual(expected, actual, 'label ellipsizes is ELLIPSIZE_END.')

00488     def test_on_status_changed_updates_status_label(self):
        """On status changed, the status label is updated."""
        # usual case, all values are defined
        self.ui.on_status_changed(**self.kwargs)
        self.assert_status_label_correct(**self.kwargs)

00494     def test_on_status_changed_updates_status_label_even_on_weird_cases(self):
        """On status changed, the status label is updated."""
        keywords = ('name', 'description', 'queues', 'connection') # need order
        for attr in keywords:
            old_value = self.kwargs[attr]

            # some weird cases: attr is '' or None
            for value in ('', None):
                self.kwargs[attr] = value
                self.ui.on_status_changed(**self.kwargs)
                others = (self.kwargs[k] for k in keywords if k != attr)
                expected = self.ui.STATUS_JOINER.join(others)
                self.assert_status_label_correct(expected=expected)

            self.kwargs[attr] = old_value

00510     def test_update_is_correct_for_status_label(self):
        """Correctly updates the status label."""
        self.ui.sd.current_state._set(**self.kwargs)
        self.ui.update()
        self.assert_status_label_correct(**self.kwargs)

00516     def test_on_stopped_updates_status_label(self):
        """On SD stoppped, the UI updates the status label."""
        self.patch(self.ui, 'on_status_changed', self.set_called)
        self.ui.on_stopped()
        self.assertTrue(self._called,
                        'on_status_changed was called on_stopped.')

00523     def test_status_label_default_if_not_started(self):
        """Status label is the default if not started."""
        self.assert_status_label_correct(expected=self.ui.STATUS['initial'])

00527     def test_status_label_empty_if_started_and_no_name_nor_desc(self):
        """Status label is empty if started but no name and no description."""
        self.do_start()
        self.ui.on_status_changed(name=None, description=None)
        self.assert_status_label_correct(expected='')

00533     def test_status_label_is_updated_on_started_and_on_stopped(self):
        """Status label is updated on_started."""
        self.ui.sd.current_state._set(**self.kwargs)
        self.do_start()
        self.assert_status_label_correct(**self.kwargs)

        self.kwargs['name'] = 'CHANGED'
        self.ui.sd.current_state._set(**self.kwargs)

        self.ui.on_stop_clicked(self.ui.stop)
        self.ui.on_stopped()
        self.assert_status_label_correct(**self.kwargs)


00547 class MagicicadaUIConnectionTestCase(MagicicadaUITestCase):
    """UI test cases for connection buttons/indicators."""

00550     def assert_indicator_is_updated_correctly(self, indicator):
        """Test that correctly updates the 'indicator'."""
        cs = self.ui.sd.current_state
        for expected in (True, False):
            cs._set(**{indicator: expected})

            self.ui.update()

            self.assertEqual(self.ui.widget_enabled(self.ui.start),
                             not cs.is_started,
                             'start must be enabled if not started.')
            self.assertEqual(self.ui.widget_enabled(self.ui.stop),
                             cs.is_started,
                             'stop must be enabled if started.')

            if cs.is_started:
                actual = self.ui.widget_enabled(getattr(self.ui, indicator))
                self.assertEqual(expected, actual,
                                 '%s must be %s' % (indicator, expected))

                self.assertEqual(self.ui.widget_enabled(self.ui.connect),
                                 not cs.is_connected,
                                 'connect must be enabled if not connected.')
                self.assertEqual(self.ui.widget_enabled(self.ui.disconnect),
                                 cs.is_connected,
                                 'disconnect must be enabled if connected.')
            else:
                self.assertFalse(self.ui.connect.is_sensitive(),
                                 'connect must be disabled when %s' % cs)
                self.assertTrue(self.ui.widget_is_visible(self.ui.connect),
                                 'connect must be visible when %s' % cs)
                actual = self.ui.widget_enabled(getattr(self.ui, indicator))
                self.assertFalse(actual,
                                 '%s must be disabled' % (indicator,))

00585     def test_all_disabled_at_startup(self):
        """Indicators are all disabled at startup."""
        self.assert_indicator_disabled(self.ui.is_started)
        self.assert_indicator_disabled(self.ui.is_connected)
        self.assert_indicator_disabled(self.ui.is_online)

00591     def test_callback_are_connected(self):
        """Connection callbacks are connected."""
        for callback in ('on_started', 'on_stopped',
                         'on_connected', 'on_disconnected',
                         'on_online', 'on_offline'):
            sd_cb = getattr(self.ui.sd, '%s_callback' % callback)
            ui_cb = getattr(self.ui, callback)
            self.assertEqual(sd_cb, ui_cb,
                             '%s callback must be set' % callback)

00601     def test_on_started_is_correct(self):
        """On SD started, the UI enables connect and indicator."""
        self.do_start()

        self.assertTrue(self.ui.widget_enabled(self.ui.stop))
        self.assertTrue(self.ui.widget_enabled(self.ui.connect))
        self.assert_indicator_ready(self.ui.is_started)
        self.assert_indicator_disabled(self.ui.is_connected)
        self.assert_indicator_disabled(self.ui.is_online)

00611     def test_on_connected_is_correct(self):
        """On SD connected, the UI enables indicator."""
        self.do_connect()

        self.assertTrue(self.ui.widget_enabled(self.ui.disconnect))
        self.assert_indicator_ready(self.ui.is_started)
        self.assert_indicator_ready(self.ui.is_connected)
        self.assert_indicator_loading(self.ui.is_online)

00620     def test_on_online_is_correct(self):
        """On SD online, the UI enables indicator."""
        self.do_connect()

        self.ui.on_online()

        self.assert_indicator_ready(self.ui.is_started)
        self.assert_indicator_ready(self.ui.is_connected)
        self.assert_indicator_ready(self.ui.is_online)

00630     def test_on_stopped_is_correct(self):
        """On SD stopped, the UI disables stop and indicator."""
        self.do_start()
        self.ui.on_stop_clicked(self.ui.stop)

        self.ui.on_stopped()

        self.assertTrue(self.ui.start.is_sensitive())
        self.assertTrue(self.ui.widget_is_visible(self.ui.start))
        self.assert_indicator_disabled(self.ui.is_started)
        self.assert_indicator_disabled(self.ui.is_connected)
        self.assert_indicator_disabled(self.ui.is_online)

00643     def test_on_disconnected_is_correct(self):
        """On SD disconnected, the UI disables connect and indicator."""
        self.do_connect()
        self.ui.on_disconnect_clicked(self.ui.disconnect)

        self.ui.on_disconnected()

        self.assertTrue(self.ui.connect.is_sensitive())
        self.assert_indicator_ready(self.ui.is_started)
        self.assert_indicator_disabled(self.ui.is_connected)
        self.assert_indicator_disabled(self.ui.is_online)

00655     def test_on_offline_is_correct(self):
        """On SD offline, the UI disables indicator."""
        self.do_connect()

        self.ui.on_offline()

        self.assert_indicator_ready(self.ui.is_started)
        self.assert_indicator_ready(self.ui.is_connected)
        self.assert_indicator_disabled(self.ui.is_online)

00665     def test_update_is_correct_for_indicators_and_buttons(self):
        """Correctly updates the indicators and buttons state."""
        for i in ('is_started', 'is_connected', 'is_online'):
            self.assert_indicator_is_updated_correctly(i)


00671 class _MagicicadaUIVolumeTestCase(MagicicadaUITestCase):
    """Abstract UI test cases for volumes (folders/shares)."""

    name = None
    data_type = None

00677     def setUp(self):
        """Init."""
        super(_MagicicadaUIVolumeTestCase, self).setUp()
        if self.name is None:
            return
        self.volume = getattr(self.ui, self.name)
        self.volume_store = getattr(self.ui, '%s_store' % self.name)
        self.volume_dialog_name = '%s_dialog' % self.name
        self.volume_dialog = getattr(self.ui, self.volume_dialog_name)
        self.on_volume_clicked = getattr(self.ui, 'on_%s_clicked' % self.name)

00688     def build_some_data(self, limit=5):
        """Build some data to act as volume."""
        kwargs = dict(data_type=self.data_type, limit=limit)
        res = super(_MagicicadaUIVolumeTestCase, self).build_some_data(**kwargs)
        return res

00694     def assert_volume_availability(self, enabled):
        """Check volume availability according to 'enabled'."""
        self.assertTrue(self.ui.widget_is_visible(self.volume),
                        '%s should be visible' % self.name)
        sensitive = self.volume.is_sensitive()
        msg = '%s should %sbe sensitive'
        self.assertTrue(sensitive if enabled else not sensitive,
                        msg % (self.name, '' if enabled else 'not '))

    @skip_abstract_class
00704     def test_volume_are_disabled_until_started(self):
        """Folders and shares are disabled until online."""
        # disabled at startup
        self.assert_volume_availability(enabled=False)

        # enabled when started
        self.do_start()
        self.assert_volume_availability(enabled=True)

    @skip_abstract_class
00714     def test_volume_are_enabled_until_stopped(self):
        """Folders and shares are enabled until offline."""
        self.do_connect()
        self.assert_volume_availability(enabled=True)

        self.ui.on_online()
        self.assert_volume_availability(enabled=True)

        self.ui.on_offline()
        self.assert_volume_availability(enabled=True)

        self.ui.on_disconnect_clicked(self.ui.disconnect)
        self.ui.on_disconnected()
        self.assert_volume_availability(enabled=True)

        # disabled when stopped
        self.ui.on_stop_clicked(self.ui.stop)
        self.assert_volume_availability(enabled=False)
        self.ui.on_stopped()
        self.assert_volume_availability(enabled=False)

    @skip_abstract_class
00736     def test_volume_close_emits_response_close(self):
        """Test volume close button emits RESPONSE_CLOSE when clicked."""
        self.response = None
        def record_response(value):
            """Record the response received."""
            self.response = value
        self.patch(self.volume_dialog, 'response', record_response)

        volume_close = '%s_close' % self.name
        getattr(self.ui, volume_close).clicked()
        self.assertEqual(gtk.RESPONSE_CLOSE, self.response,
                         '%s should emit RESPONSE_CLOSE.' % volume_close)

    @skip_abstract_class
00750     def test_on_volume_clicked(self):
        """Test on_volume_clicked."""
        self.assertFalse(self.ui.widget_is_visible(self.volume_dialog),
                         '%s should not be visible.' % self.volume_dialog_name)

        def test():
            """Perform the test per se before closing the dialog."""
            self.assertTrue(self.ui.widget_is_visible(self.volume_dialog),
                            '%s should be visible.' % self.volume_dialog_name)
            self.assert_store_correct(self.volume_store, items)

        items = self.build_some_data()
        setattr(self.ui.sd, self.name, items)
        gobject.timeout_add(100, close_dialog,
                            (self.volume_dialog, test))
        self.on_volume_clicked(self.volume)

        # dialog was closed already
        self.assertFalse(self.ui.widget_is_visible(self.volume_dialog),
                         '%s should not be visible.' % self.volume_dialog_name)

    @skip_abstract_class
00772     def test_on_volume_clicked_twice(self):
        """Test on_volume_clicked twice."""

        def test():
            """Perform the test per se before closing the dialog."""
            self.assertTrue(self.ui.widget_is_visible(self.volume_dialog),
                            '%s should be visible.' % self.volume_dialog_name)
            self.assert_store_correct(self.volume_store, items)

        items = self.build_some_data()
        setattr(self.ui.sd, self.name, items)

        gobject.timeout_add(100, close_dialog,
                            (self.volume_dialog, test))
        self.on_volume_clicked(self.volume)

        gobject.timeout_add(100, close_dialog,
                            (self.volume_dialog, test))
        self.on_volume_clicked(self.volume)

    @skip_abstract_class
00793     def test_on_volume_clicked_handles_none(self):
        """On volume clicked handles None as items."""
        setattr(self.ui.sd, self.name, None)
        test = lambda: self.assert_store_correct(self.volume_store, [])
        gobject.timeout_add(100, close_dialog,
                            (self.volume_dialog, test))
        self.on_volume_clicked(self.volume)

    @skip_abstract_class
00802     def test_volume_dialog_props(self):
        """The volume dialog has correct properties."""
        size = self.volume_dialog.size_request()
        self.assertEquals((600, 300), size)

        self.assertTrue(self.volume_dialog.get_modal(),
                        '%s must be modal.' % self.volume_dialog_name)

        position = self.volume_dialog.get_property('window-position')
        self.assertEqual(gtk.WIN_POS_CENTER, position,
                        '%s must be centered.' % self.volume_dialog_name)

        actual = self.volume_dialog.get_title()
        expected = self.name.replace('_', ' ').capitalize()
        msg = '%s title must be %s (got %s instead)'
        self.assertEqual(expected, actual,
                         msg % (self.volume_dialog_name, expected, actual))

00820 class MagicicadaUIFoldersTestCase(_MagicicadaUIVolumeTestCase):
    """UI test cases for folders."""

    name = 'folders'
    data_type = FolderData # node path suggested_path subscribed volume


00827 class _MagicicadaUISharesTestCase(_MagicicadaUIVolumeTestCase):
    """UI test cases for shares_to_me."""

    data_type = ShareData # accepted access_level free_bytes name node_id
                          # other_username other_visible_name path volume_id

    @skip_abstract_class
00834     def test_bytes_are_humanized(self):
        """Free bytes are shown humanized."""
        attrs = self.data_type._fields
        kwargs = dict([(attr, str(attr)) for attr in attrs])
        kwargs['free_bytes'] = 10000
        item = self.data_type(**kwargs)
        setattr(self.ui.sd, self.name, [item])

        def test():
            """Perform the test per se before closing the dialog."""
            kwargs['free_bytes'] = humanize_bytes(kwargs['free_bytes'],
                                                  precision=2)
            item = self.data_type(**kwargs)
            self.assert_store_correct(self.volume_store, [item])

        gobject.timeout_add(100, close_dialog,
                            (self.volume_dialog, test))
        self.on_volume_clicked(self.volume)


00854 class MagicicadaUISharesToMeTestCase(_MagicicadaUISharesTestCase):
    """UI test cases for shares_to_me."""

    name = 'shares_to_me'


00860 class MagicicadaUISharesToOthersTestCase(_MagicicadaUISharesTestCase):
    """UI test cases for shares_to_others."""

    name = 'shares_to_others'


Generated by  Doxygen 1.6.0   Back to index