~ralsina/ubuntuone-client/fix_712674

« back to all changes in this revision

Viewing changes to tests/status/test_aggregator.py

  • Committer: Tarmac
  • Author(s): eric.casteleijn at canonical
  • Date: 2011-02-10 18:23:32 UTC
  • mfrom: (843.2.8 lp-702055)
  • Revision ID: tarmac-20110210182332-u1tx2s9jiwpob5z5
This adds notifications and messaging on new incoming shares and UDFs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from ubuntuone.devtools.handlers import MementoHandler
26
26
from ubuntuone.status import aggregator
27
27
from ubuntuone.status.notification import AbstractNotification
 
28
from ubuntuone.status.messaging import AbstractMessaging
28
29
from ubuntuone.syncdaemon import status_listener
29
30
from ubuntuone.syncdaemon.volume_manager import Share, UDF
30
31
 
144
145
            notification]
145
146
        if new_icon is not None:
146
147
            icon = new_icon
147
 
                # we store it as a new notification, to ease testing
 
148
        # we store it as a new notification, to ease testing
148
149
        notification_params = (new_title, new_message, icon, append)
149
150
        self.notifications_shown.append(notification_params)
150
151
 
160
161
    return get_instance
161
162
 
162
163
 
163
 
class FakeMessaging(object):
 
164
class FakeMessaging(AbstractMessaging):
164
165
    """A fake messaging class."""
165
166
 
166
 
    def __init__(self, application_name="fake app"):
 
167
    def __init__(self):  # pylint: disable=W0231
167
168
        self.messages_shown = {}
168
 
        self.application_name = application_name
 
169
        self.messages_updated = {}
169
170
 
170
 
    def show_message(self, sender, callback, message_time=None,
 
171
    # pylint: disable=R0913
 
172
    def show_message(self, sender, callback=None, message_time=None,
171
173
                     message_count=None, icon=None):
172
174
        """Show a message to the user."""
 
175
        if message_count and sender in self.messages_shown:
 
176
            self.update_count(sender, message_count)
173
177
        self.messages_shown[sender] = (
174
178
            callback, message_time, message_count, icon)
 
179
    # pylint: enable=R0913
175
180
 
176
 
    def update_count(self, sender, new_count):
 
181
    def update_count(self, sender, add_count):
177
182
        """Update the count for an existing indicator."""
178
 
        callback, message_time, _, icon = self.messages_shown[sender]
179
 
        self.messages_shown[sender] = (
180
 
            callback, message_time, new_count, icon)
 
183
        self.messages_updated[sender] = (sender, add_count)
181
184
 
182
185
 
183
186
class FakeStatusAggregator(object):
184
187
    """A fake status aggregator."""
185
188
 
186
 
    def __init__(self, clock):
 
189
    def __init__(self, clock):          # pylint: disable=W0613
187
190
        """Initialize this instance."""
188
191
        self.discovered = 0
189
192
        self.completed = 0
523
526
 
524
527
    def __init__(self, clock):
525
528
        """Initialize this fake instance."""
526
 
        self.interesting_events = []
527
529
        self.queued_commands = set()
528
530
 
529
 
    def interesting_event(self, event):
530
 
        """A new interesting event was found."""
531
 
        self.interesting_events.append(event)
532
 
 
533
531
    def queue_done(self):
534
532
        """The queue completed all operations."""
535
533
        self.queued_commands.clear()
554
552
    def setUp(self):
555
553
        """Initialize this test instance."""
556
554
        self.patch(aggregator, "StatusAggregator", FakeAggregator)
 
555
        self.patch(aggregator, "Notification", FakeNotificationSingleton())
 
556
        self.patch(aggregator, "Messaging", FakeMessaging)
557
557
        self.fakefsm = None
558
558
        self.fakevm = FakeVolumeManager()
559
559
        self.status_frontend = aggregator.StatusFrontend()
560
560
        self.listener = status_listener.StatusListener(self.fakefsm,
561
561
                                                       self.fakevm,
562
562
                                                       self.status_frontend)
 
563
 
563
564
    def test_file_published(self):
564
565
        """A file published event is processed."""
565
566
        share_id = "fake share id"
566
567
        node_id = "fake node id"
567
568
        is_public = True
568
569
        public_url = "http://fake_public/url"
569
 
 
570
570
        self.listener.handle_AQ_CHANGE_PUBLIC_ACCESS_OK(share_id, node_id,
571
571
                                                        is_public, public_url)
572
 
        event = self.status_frontend.aggregator.interesting_events[0]
573
 
        self.assertIsInstance(event, aggregator.FilePublishingStatus)
 
572
        self.assertEqual(
 
573
            1, len(self.status_frontend.notification.notifications_shown))
 
574
        self.assertEqual(
 
575
            (aggregator.UBUNTUONE_TITLE,
 
576
             'A file was just made public at http://fake_public/url', None,
 
577
             False),
 
578
            self.status_frontend.notification.notifications_shown[0])
574
579
 
575
580
    def test_file_unpublished(self):
576
581
        """A file unpublished event is processed."""
581
586
 
582
587
        self.listener.handle_AQ_CHANGE_PUBLIC_ACCESS_OK(share_id, node_id,
583
588
                                                        is_public, public_url)
584
 
        event = self.status_frontend.aggregator.interesting_events[0]
585
 
        self.assertIsInstance(event, aggregator.FileUnpublishingStatus)
 
589
        self.assertEqual(
 
590
            1, len(self.status_frontend.notification.notifications_shown))
 
591
        self.assertEqual(
 
592
            (aggregator.UBUNTUONE_TITLE, 'A file is no longer published', None,
 
593
             False),
 
594
            self.status_frontend.notification.notifications_shown[0])
586
595
 
587
596
    def test_download_started(self):
588
597
        """A download was added to the queue."""
644
653
    def test_new_share_available(self):
645
654
        """A new share is available for subscription."""
646
655
        SHARE_ID = "fake share id"
647
 
        share = Share(volume_id=SHARE_ID)
 
656
        FAKE_SENDER = 'Mom'
 
657
        share = Share(volume_id=SHARE_ID, other_visible_name=FAKE_SENDER)
648
658
        self.fakevm.volumes[SHARE_ID] = share
649
659
        self.listener.handle_VM_SHARE_CREATED(SHARE_ID)
650
 
        event = self.status_frontend.aggregator.interesting_events[0]
651
 
        self.assertIsInstance(event, aggregator.ShareAvailableStatus)
 
660
        self.assertEqual(
 
661
            1, len(self.status_frontend.notification.notifications_shown))
 
662
        self.assertEqual(
 
663
            (aggregator.UBUNTUONE_TITLE,
 
664
             'New cloud folder available: <%s> shared by <%s>' % (
 
665
                 'None', FAKE_SENDER), None, False),
 
666
            self.status_frontend.notification.notifications_shown[0])
 
667
        msg = self.status_frontend.messaging.messages_shown[FAKE_SENDER]
 
668
        # msg did not receive a time argument
 
669
        self.assertEqual(None, msg[1])
 
670
        # msg did not receive a count argument
 
671
        self.assertEqual(None, msg[2])
652
672
 
653
673
    def test_new_udf_available(self):
654
674
        """A new udf is available for subscription."""
655
675
        udf = UDF()
656
676
        self.listener.handle_VM_UDF_CREATED(udf)
657
 
        event = self.status_frontend.aggregator.interesting_events[0]
658
 
        self.assertIsInstance(event, aggregator.UDFAvailableStatus)
 
677
        self.assertEqual(
 
678
            1, len(self.status_frontend.notification.notifications_shown))
 
679
        self.assertEqual(
 
680
            (aggregator.UBUNTUONE_TITLE, 'New cloud folder available: None',
 
681
             None, False),
 
682
            self.status_frontend.notification.notifications_shown[0])
 
683
        self.assertEqual(
 
684
            1, len(self.status_frontend.messaging.messages_shown))
 
685
        self.assertEqual(
 
686
            0, len(self.status_frontend.messaging.messages_updated))
 
687
        msg = self.status_frontend.messaging.messages_shown[
 
688
            aggregator.NEW_UDFS_SENDER]
 
689
        # msg did not receive a time argument
 
690
        self.assertEqual(None, msg[1])
 
691
        # msg did receive a count argument
 
692
        self.assertEqual(1, msg[2])
 
693
 
 
694
    def test_two_new_udfs_available(self):
 
695
        """A new udf is available for subscription."""
 
696
        udf1 = UDF()
 
697
        self.listener.handle_VM_UDF_CREATED(udf1)
 
698
        udf2 = UDF()
 
699
        self.listener.handle_VM_UDF_CREATED(udf2)
 
700
        self.assertEqual(
 
701
            2, len(self.status_frontend.notification.notifications_shown))
 
702
        self.assertEqual(
 
703
            (aggregator.UBUNTUONE_TITLE, 'New cloud folder available: None',
 
704
             None, False),
 
705
            self.status_frontend.notification.notifications_shown[0])
 
706
        self.assertEqual(
 
707
            (aggregator.UBUNTUONE_TITLE, 'New cloud folder available: None',
 
708
             None, False),
 
709
            self.status_frontend.notification.notifications_shown[1])
 
710
        self.assertEqual(
 
711
            1, len(self.status_frontend.messaging.messages_shown))
 
712
        self.assertEqual(
 
713
            1, len(self.status_frontend.messaging.messages_updated))
 
714
        msg = self.status_frontend.messaging.messages_shown[
 
715
            aggregator.NEW_UDFS_SENDER]
 
716
        # msg did not receive a time argument
 
717
        self.assertEqual(None, msg[1])
 
718
        # msg did receive a count argument
 
719
        self.assertEqual(1, msg[2])
 
720
        self.assertEqual(
 
721
            (aggregator.NEW_UDFS_SENDER, 1),
 
722
            self.status_frontend.messaging.messages_updated[
 
723
                aggregator.NEW_UDFS_SENDER])
659
724
 
660
725
 
661
726
class StatusEventTestCase(TestCase):
662
 
 
663
727
    """Test the status event class and children."""
664
728
 
665
729
    CLASS = aggregator.StatusEvent
832
896
        self.assertEqual(self.aggregator.done_counter, 0)
833
897
        self.assertEqual(len(self.aggregator.files_uploading), 0)
834
898
        self.assertEqual(len(self.aggregator.files_downloading), 0)
835
 
        self.assertEqual(len(self.aggregator.interesting_events), 0)
836
899
 
837
900
    def assertMiscCommandQueued(self, fc):
838
901
        """Assert that some command was queued."""