~ubuntu-branches/ubuntu/quantal/ubuntuone-control-panel/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# -*- coding: utf-8 -*-

# Authors: Alejandro J. Cura <alecu@canonical.com>
# Authors: Natalia B. Bidart <nataliabidart@canonical.com>
#
# Copyright 2010 Canonical Ltd.
#
# 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 the control panel backend DBus service."""

import dbus
import mocker

from twisted.internet import defer
from twisted.python.failure import Failure

from ubuntuone.controlpanel import dbus_service, backend
from ubuntuone.controlpanel import (DBUS_BUS_NAME, DBUS_PREFERENCES_PATH,
    DBUS_PREFERENCES_IFACE)
from ubuntuone.controlpanel.dbustests import TestCase


SAMPLE_ACCOUNT_INFO = {
    "quota_used": 12345,
    "quota_total": 54321,
    "type": "paying customer",
    "name": "john carlos",
    "email": "john@carlos.com",
}

SAMPLE_DEVICES_INFO = [
    {
        "device_token": "token-1",
        "name": "Ubuntu One @ darkstar",
        "date_added": "2008-12-06T18:15:38.0",
        "type": "computer",
        "configurable": True,
        "show_all_notifications": True,
        "limit_bandwidth": True,
        "max_upload_speed": 12345,
        "max_download_speed": 54321,
        "available_services": "files, contacts, music",
        "enabled_services": "files, music",
    },
    {
        "device_token": "token-2",
        "name": "Ubuntu One @ brightmoon",
        "date_added": "2010-09-22T20:45:38.0",
        "type": "computer",
        "configurable": False,
        "available_services": "files, contacts",
        "enabled_services": "files",
    },
]

SAMPLE_FOLDERS = [
    {u'path': u'/home/tester/Public',
     u'subscribed': True,
     u'suggested_path': u'~/Public',
     u'display_name': u'Public',
     u'type': backend.ControlBackend.FOLDER_TYPE,
     u'volume_id': u'9ea892f8-15fa-4201-bdbf-8de99fa5f588'},

    {u'path': u'/home/tester/Documents',
     u'subscribed': False,
     u'suggested_path': u'~/Documents',
     u'display_name': u'Documents',
     u'type': backend.ControlBackend.FOLDER_TYPE,
     u'volume_id': u'2db262f5-a151-4c19-969c-bb5ced753c61'},

    {u'path': u'/home/tester/Pictures/Photos',
     u'subscribed': True,
     u'suggested_path': u'~/Pictures/Photos',
     u'display_name': u'Pictures/Photos',
     u'type': backend.ControlBackend.FOLDER_TYPE,
     u'volume_id': u'1deb2874-3d28-46ae-9999-d5f48de9f460'},
]

SAMPLE_VOLUMES_INFO = [
    (u'', 1253698, SAMPLE_FOLDERS),
]

SAMPLE_REPLICATIONS_INFO = [
    {'replication_id': 'yadda', 'wait for it': 'awesome'},
    {'replication_id': 'yoda', 'something else': 'awesome'},
]


class DBusServiceMainTestCase(mocker.MockerTestCase):
    """Tests for the main function."""

    def test_dbus_service_main(self):
        """The main method starts the loop and hooks up to DBus."""
        rs_name = "ubuntuone.controlpanel.dbus_service.register_service"
        rs = self.mocker.replace(rs_name)
        rs()
        self.mocker.result(True)

        mainloop = "ubuntuone.controlpanel.dbus_service.GObject.MainLoop"
        mainloop = self.mocker.replace(mainloop)
        mainloop()
        loop = self.mocker.mock()
        self.mocker.result(loop)

        shutdown_func = self.mocker.mock()
        loop.quit  # pylint: disable=W0104
        self.mocker.result(shutdown_func)

        rml_name = "ubuntuone.controlpanel.dbus_service.run_mainloop"
        rml = self.mocker.replace(rml_name)
        rml(loop=loop)
        pb_name = "ubuntuone.controlpanel.dbus_service.publish_backend"
        pb = self.mocker.replace(pb_name)
        pb(shutdown_func=shutdown_func)
        self.mocker.replay()
        dbus_service.main()

    def test_dbus_service_cant_register(self):
        """The main method can't start the loop."""
        rs_name = "ubuntuone.controlpanel.dbus_service.register_service"
        rs = self.mocker.replace(rs_name)
        rs()
        self.mocker.result(False)
        self.mocker.replay()
        dbus_service.main()


class MockBackend(object):
    """A mock backend."""

    exception = None
    sample_status = {
        dbus_service.MSG_KEY: 'test me please',
        dbus_service.STATUS_KEY: dbus_service.FILE_SYNC_IDLE,
    }
    shutdown_func = None

    def __init__(self, shutdown_func=None):
        MockBackend.shutdown_func = shutdown_func
        self._status_changed_handler = []

    def _process(self, result):
        """Process the request with the given result."""
        if self.exception:
            # pylint: disable=E1102
            return defer.fail(self.exception(result))
        return defer.succeed(result)

    def _set_status_changed_handler(self, handler):
        """Set 'handler' to be called when file sync status changes."""
        if handler is not None:
            self._status_changed_handler.append(handler)
        else:
            self._status_changed_handler = []

    def _get_status_changed_handler(self):
        """Return the handler to be called when file sync status changes."""
        if len(self._status_changed_handler) > 0:
            result = self._status_changed_handler[-1]
        else:
            result = None
        return result

    status_changed_handler = property(_get_status_changed_handler,
                                      _set_status_changed_handler)

    def account_info(self):
        """Get the user account info."""
        return self._process(SAMPLE_ACCOUNT_INFO)

    def devices_info(self):
        """Get the user devices info."""
        return self._process(SAMPLE_DEVICES_INFO)

    def change_device_settings(self, token, settings):
        """Configure a given device."""
        return self._process(token)

    def remove_device(self, token):
        """Remove a device's tokens from the sso server."""
        return self._process(token)

    def file_sync_status(self):
        """Return the status of the file sync service."""
        return self._process(self.sample_status)

    def enable_files(self):
        """Enable files service."""
        return self._process(None)

    def disable_files(self):
        """Disable files service."""
        return self._process(None)

    def connect_files(self):
        """Connect files service."""
        return self._process(None)

    def disconnect_files(self):
        """Disconnect files service."""
        return self._process(None)

    def restart_files(self):
        """Restart the files service."""
        return self._process(None)

    def start_files(self):
        """Start the files service."""
        return self._process(None)

    def stop_files(self):
        """Stop the files service."""
        return self._process(None)

    def volumes_info(self):
        """Get the user volumes info."""
        return self._process(SAMPLE_VOLUMES_INFO)

    def change_volume_settings(self, volume_id, settings):
        """Configure a given volume."""
        return self._process(volume_id)

    def replications_info(self):
        """Start the replication exclusion service if needed.

        Return the replication info, which is a dictionary of (replication
        name, enabled).

        """
        return self._process(SAMPLE_REPLICATIONS_INFO)

    def change_replication_settings(self, replication_id, settings):
        """Configure a given replication."""
        return self._process(replication_id)

    def shutdown(self):
        """Stop this service."""
        self.shutdown_func()


class DBusServiceTestCase(TestCase):
    """Test for the DBus service."""

    timeout = 5

    def setUp(self):
        """Initialize each test run."""
        super(DBusServiceTestCase, self).setUp()
        dbus_service.init_mainloop()
        self._called = False

    def _set_called(self, *args, **kwargs):
        """Keep track of function calls, useful for monkeypatching."""
        self._called = (args, kwargs)

    def test_register_service(self):
        """The DBus service is successfully registered."""
        ret = dbus_service.register_service()
        self.assertTrue(ret)

    def test_cant_register_twice(self):
        """The DBus service can't register if it already did."""
        ret = dbus_service.register_service()
        self.assertTrue(ret)
        ret = dbus_service.register_service()
        self.assertFalse(ret)
    #pylint: disable=W0612
    test_cant_register_twice.skip = "Must run 2nd check in another process."

    def test_dbus_busname_created(self):
        """The DBus BusName is created."""
        busname = dbus_service.get_busname()
        self.assertEqual(busname.get_name(), DBUS_BUS_NAME)

    def test_error_handler_with_failure(self):
        """Ensure to build a string-string dict to pass to error signals."""
        error = dbus_service.Failure(TypeError('oh no!'))
        expected = dbus_service.failure_to_error_dict(error)

        result = dbus_service.error_handler(error)

        self.assertEqual(expected, result)

    def test_error_handler_with_exception(self):
        """Ensure to build a string-string dict to pass to error signals."""
        error = TypeError('oh no, no again!')
        expected = dbus_service.exception_to_error_dict(error)

        result = dbus_service.error_handler(error)

        self.assertEqual(expected, result)

    def test_error_handler_with_string_dict(self):
        """Ensure to build a string-string dict to pass to error signals."""
        expected = {'test': 'me'}

        result = dbus_service.error_handler(expected)

        self.assertEqual(expected, result)

    def test_error_handler_with_non_string_dict(self):
        """Ensure to build a string-string dict to pass to error signals."""
        expected = {'test': 0, 'qué?': None,
                    10: 'foo\xffbar', True: u'ñoño'}

        result = dbus_service.error_handler(expected)
        expected = dict(map(lambda x: x if isinstance(x, unicode) else
                                      str(x).decode('utf8', 'replace'), i)
                        for i in expected.iteritems())

        self.assertEqual(expected, result)

    def test_error_handler_default(self):
        """Ensure to build a string-string dict to pass to error signals."""
        msg = 'Got unexpected error argument %r' % None
        expected = {dbus_service.ERROR_TYPE: 'UnknownError',
                    dbus_service.ERROR_MESSAGE: msg}

        result = dbus_service.error_handler(None)

        self.assertEqual(expected, result)


class BaseTestCase(TestCase):
    """Base test case for the DBus service."""

    timeout = 3

    def setUp(self):
        super(BaseTestCase, self).setUp()
        dbus_service.init_mainloop()
        self.patch(dbus_service, 'ControlBackend', MockBackend)
        be = dbus_service.publish_backend()
        self.addCleanup(be.remove_from_connection)
        bus = dbus.SessionBus()
        obj = bus.get_object(bus_name=DBUS_BUS_NAME,
                             object_path=DBUS_PREFERENCES_PATH,
                             follow_name_owner_changes=True)
        self.backend = dbus.Interface(object=obj,
                                      dbus_interface=DBUS_PREFERENCES_IFACE)
        self.deferred = defer.Deferred()

    def tearDown(self):
        self.backend = None
        self.deferred = None
        super(BaseTestCase, self).tearDown()

    def got_error(self, *a):
        """Some error happened in the DBus call."""
        self.deferred.errback(*a)

    def ignore(self, *a):
        """Do nothing with the returned value."""

    def errback_on_error(self, f):
        """Call the given 'f' but errback self.deferred on any error."""

        def inner(*a, **kw):
            """Call the given 'f' but errback self.deferred on any error."""
            try:
                return f(*a, **kw)
            except Exception, e:  # pylint: disable=W0703
                self.deferred.errback(Failure(e))

        return inner

    def assert_correct_method_call(self, success_sig, error_sig, success_cb,
                                   method, *args):
        """Connect 'success_cb' with 'success_sig', and call 'method'.

        'error_sig' will be connected to the class' error handler.

        'success_cb' should fire 'self.deferred' on success. If 'success_cb'
        fails due to an failed assertion, the deferred will be errback'd.

        """
        success_cb = self.errback_on_error(success_cb)
        res = self.backend.connect_to_signal(success_sig, success_cb)
        self.addCleanup(res.remove)

        res = self.backend.connect_to_signal(error_sig, self.got_error)
        self.addCleanup(res.remove)

        method = self.errback_on_error(method)
        method(*args, reply_handler=self.ignore, error_handler=self.got_error)

        return self.deferred


class OperationsTestCase(BaseTestCase):
    """Test for the DBus service operations."""

    def test_account_info_returned(self):
        """The account info is successfully returned."""

        def got_signal(account_info):
            """The correct signal was fired."""
            self.assertIn("quota_used", account_info)
            self.assertIn("quota_total", account_info)
            self.assertIn("type", account_info)
            self.assertIn("name", account_info)
            self.assertIn("email", account_info)
            self.deferred.callback("success")

        args = ("AccountInfoReady", "AccountInfoError", got_signal,
                self.backend.account_info)
        return self.assert_correct_method_call(*args)

    def test_devices_info_returned(self):
        """The devices info is successfully returned."""

        def got_signal(devices_list):
            """The correct signal was fired."""
            for device_info in devices_list:
                self.assertIn("device_token", device_info)
                self.assertIn("name", device_info)
                self.assertIn("date_added", device_info)
                self.assertIn("type", device_info)
                self.assertIn("configurable", device_info)
                if bool(device_info["configurable"]):
                    self.assertIn("show_all_notifications", device_info)
                    self.assertIn("limit_bandwidth", device_info)
                    self.assertIn("max_upload_speed", device_info)
                    self.assertIn("max_download_speed", device_info)
                self.assertIn("available_services", device_info)
                self.assertIn("enabled_services", device_info)
            self.deferred.callback("success")

        args = ("DevicesInfoReady", "DevicesInfoError", got_signal,
                self.backend.devices_info)
        return self.assert_correct_method_call(*args)

    def test_change_device_settings(self):
        """The device settings are successfully changed."""
        sample_token = "token-1"

        def got_signal(token):
            """The correct token was received."""
            self.assertEqual(token, sample_token)
            self.deferred.callback("success")

        settings = {
            "enabled_services": "files, contacts",
        }
        args = ("DeviceSettingsChanged", "DeviceSettingsChangeError",
                got_signal, self.backend.change_device_settings,
                sample_token, settings)
        return self.assert_correct_method_call(*args)

    def test_remove_device(self):
        """The device is removed."""
        sample_token = "token-1"

        def got_signal(token):
            """The correct token was received."""
            self.assertEqual(token, sample_token)
            self.deferred.callback("success")

        args = ("DeviceRemoved", "DeviceRemovalError", got_signal,
                self.backend.remove_device, sample_token)
        return self.assert_correct_method_call(*args)

    def test_enable_files(self):
        """Enable files service."""

        def got_signal(*args):
            """The correct signal was received."""
            self.deferred.callback("success")

        args = ("FilesEnabled", "FilesEnableError", got_signal,
                self.backend.enable_files)
        return self.assert_correct_method_call(*args)

    def test_disable_files(self):
        """Disable files service."""

        def got_signal():
            """The correct signal was received."""
            self.deferred.callback("success")

        args = ("FilesDisabled", "FilesDisableError", got_signal,
                self.backend.disable_files)
        return self.assert_correct_method_call(*args)

    def test_connect_files(self):
        """Connect files service."""

        def got_signal():
            """The correct signal was received."""
            self.deferred.callback("success")

        args = ("FilesConnected", "FilesConnectError", got_signal,
                self.backend.connect_files)
        return self.assert_correct_method_call(*args)

    def test_disconnect_files(self):
        """Disconnect files service."""

        def got_signal():
            """The correct signal was received."""
            self.deferred.callback("success")

        args = ("FilesDisconnected", "FilesDisconnectError", got_signal,
                self.backend.disconnect_files)
        return self.assert_correct_method_call(*args)

    def test_restart_files(self):
        """Restart files service."""

        def got_signal():
            """The correct signal was received."""
            self.deferred.callback("success")

        args = ("FilesRestarted", "FilesRestartError", got_signal,
                self.backend.restart_files)
        return self.assert_correct_method_call(*args)

    def test_start_files(self):
        """Start files service."""

        def got_signal():
            """The correct signal was received."""
            self.deferred.callback("success")

        args = ("FilesStarted", "FilesStartError", got_signal,
                self.backend.start_files)
        return self.assert_correct_method_call(*args)

    def test_stop_files(self):
        """Stop files service."""

        def got_signal():
            """The correct signal was received."""
            self.deferred.callback("success")

        args = ("FilesStopped", "FilesStopError", got_signal,
                self.backend.stop_files)
        return self.assert_correct_method_call(*args)

    def test_volumes_info(self):
        """The volumes info is reported."""

        def got_signal(volumes):
            """The correct info was received."""

            expected = []
            f = lambda _: _
            for name, free_bytes, data in SAMPLE_VOLUMES_INFO:
                entry = (name, dbus_service.make_unicode(free_bytes),
                         dbus_service.transform_info(f)(data))
                expected.append(entry)

            self.assertEqual(volumes, expected)
            self.deferred.callback("success")

        args = ("VolumesInfoReady", "VolumesInfoError", got_signal,
                self.backend.volumes_info)
        return self.assert_correct_method_call(*args)

    def test_change_volume_settings(self):
        """The volume settings are successfully changed."""
        expected_volume_id = SAMPLE_FOLDERS[0]['volume_id']

        def got_signal(volume_id):
            """The correct volume was changed."""
            self.assertEqual(volume_id, expected_volume_id)
            self.deferred.callback("success")

        args = ("VolumeSettingsChanged", "VolumeSettingsChangeError",
                got_signal, self.backend.change_volume_settings,
                expected_volume_id, {'subscribed': ''})
        return self.assert_correct_method_call(*args)

    def test_replications_info(self):
        """The replications info is reported."""

        def got_signal(replications):
            """The correct info was received."""
            self.assertEqual(replications, SAMPLE_REPLICATIONS_INFO)
            self.deferred.callback("success")

        args = ("ReplicationsInfoReady", "ReplicationsInfoError", got_signal,
                self.backend.replications_info)
        return self.assert_correct_method_call(*args)

    def test_change_replication_settings(self):
        """The replication settings are successfully changed."""
        expected_replication_id = SAMPLE_REPLICATIONS_INFO[0]['replication_id']

        def got_signal(replication_id):
            """The correct replication was changed."""
            self.assertEqual(replication_id, expected_replication_id)
            self.deferred.callback("success")

        args = ("ReplicationSettingsChanged", "ReplicationSettingsChangeError",
                got_signal, self.backend.change_replication_settings,
                expected_replication_id, {'enabled': ''})
        return self.assert_correct_method_call(*args)


class OperationsErrorTestCase(OperationsTestCase):
    """Test for the DBus service operations when there is an error."""

    def setUp(self):
        super(OperationsErrorTestCase, self).setUp()
        self.patch(MockBackend, 'exception', AssertionError)

    def assert_correct_method_call(self, success_sig, error_sig, success_cb,
                                   method, *args):
        """Call parent instance swapping success_sig with error_sig.

        This is because we want to succeed the test when the error signal was
        received.

        """

        def got_error_signal(*a):
            """The error signal was received."""
            if len(a) == 1:
                error_dict = a[0]
            else:
                an_id, error_dict = a
                self.assertEqual(an_id, args[0])

            self.assertEqual(error_dict[dbus_service.ERROR_TYPE],
                             'AssertionError')
            self.deferred.callback("success")

        return super(OperationsErrorTestCase, self).assert_correct_method_call(
            error_sig, success_sig, got_error_signal, method, *args)


class OperationsAuthErrorTestCase(OperationsTestCase):
    """Test for the DBus service operations when UnauthorizedError happens."""

    def setUp(self):
        super(OperationsAuthErrorTestCase, self).setUp()
        self.patch(MockBackend, 'exception',
                   dbus_service.UnauthorizedError)

    def assert_correct_method_call(self, success_sig, error_sig, success_cb,
                                   method, *args):
        """Call parent instance expecting UnauthorizedError signal."""

        def inner_success_cb(*a):
            """The success signal was received."""
            if len(a) == 1:
                error_dict = a[0]
            else:
                an_id, error_dict = a
                self.assertEqual(an_id, args[0])

            self.assertEqual(error_dict[dbus_service.ERROR_TYPE],
                             'UnauthorizedError')
            self.deferred.callback('success')

        parent = super(OperationsAuthErrorTestCase, self)
        return parent.assert_correct_method_call(
            "UnauthorizedError", error_sig, inner_success_cb, method, *args)


class FileSyncTestCase(BaseTestCase):
    """Test for the DBus service when requesting file sync status."""

    def assert_correct_status_signal(self, status, sync_signal,
                                     error_signal="FileSyncStatusError",
                                     expected_msg=None):
        """The file sync status is reported properly."""
        MockBackend.sample_status[dbus_service.STATUS_KEY] = status
        if expected_msg is None:
            expected_msg = MockBackend.sample_status[dbus_service.MSG_KEY]

        def got_signal(msg):
            """The correct status was received."""
            self.assertEqual(msg, expected_msg)
            self.deferred.callback("success")

        args = (sync_signal, error_signal, got_signal,
                self.backend.file_sync_status)
        return self.assert_correct_method_call(*args)

    def test_file_sync_status_unknown(self):
        """The file sync status is reported properly."""
        msg = MockBackend.sample_status
        args = ('invalid-file-sync-status', "FileSyncStatusError",
                "FileSyncStatusIdle", msg)
        return self.assert_correct_status_signal(*args)

    def test_file_sync_status_error(self):
        """The file sync status is reported properly."""
        msg = MockBackend.sample_status[dbus_service.MSG_KEY]
        err_dict = {dbus_service.ERROR_TYPE: 'FileSyncStatusError',
                    dbus_service.ERROR_MESSAGE: msg}
        args = (dbus_service.FILE_SYNC_ERROR, "FileSyncStatusError",
                "FileSyncStatusIdle", err_dict)
        return self.assert_correct_status_signal(*args)

    def test_file_sync_status_disabled(self):
        """The file sync status is reported properly."""
        args = (dbus_service.FILE_SYNC_DISABLED, "FileSyncStatusDisabled")
        return self.assert_correct_status_signal(*args)

    def test_file_sync_status_starting(self):
        """The file sync status is reported properly."""
        args = (dbus_service.FILE_SYNC_STARTING, "FileSyncStatusStarting")
        return self.assert_correct_status_signal(*args)

    def test_file_sync_status_stopped(self):
        """The file sync status is reported properly."""
        args = (dbus_service.FILE_SYNC_STOPPED, "FileSyncStatusStopped")
        return self.assert_correct_status_signal(*args)

    def test_file_sync_status_disconnected(self):
        """The file sync status is reported properly."""
        args = (dbus_service.FILE_SYNC_DISCONNECTED,
                "FileSyncStatusDisconnected")
        return self.assert_correct_status_signal(*args)

    def test_file_sync_status_syncing(self):
        """The file sync status is reported properly."""
        args = (dbus_service.FILE_SYNC_SYNCING, "FileSyncStatusSyncing")
        return self.assert_correct_status_signal(*args)

    def test_file_sync_status_idle(self):
        """The file sync status is reported properly."""
        args = (dbus_service.FILE_SYNC_IDLE, "FileSyncStatusIdle")
        return self.assert_correct_status_signal(*args)

    def test_file_sync_status_changed(self):
        """The file sync status is reported every time status changed."""
        status = (
            dbus_service.FILE_SYNC_DISABLED,
            dbus_service.FILE_SYNC_DISCONNECTED,
            dbus_service.FILE_SYNC_ERROR,
            dbus_service.FILE_SYNC_IDLE,
            dbus_service.FILE_SYNC_STARTING,
            dbus_service.FILE_SYNC_SYNCING,
        )
        for arg in status:
            args = (arg, "FileSyncStatusChanged")
            return self.assert_correct_status_signal(*args, expected_msg=arg)

    def test_status_changed_handler(self):
        """The status changed handler is properly set."""
        be = MockBackend()
        dbus_service.ControlPanelBackend(backend=be)

        self.assertEqual(be.status_changed_handler, None)

    def test_status_changed_handler_after_status_requested(self):
        """The status changed handler is properly set."""
        be = MockBackend()
        cpbe = dbus_service.ControlPanelBackend(backend=be)
        cpbe.file_sync_status()

        self.assertEqual(be.status_changed_handler, cpbe.process_status)

    def test_status_changed_handler_after_status_requested_twice(self):
        """The status changed handler is properly set."""
        be = MockBackend()
        cpbe = dbus_service.ControlPanelBackend(backend=be)
        cpbe.file_sync_status()
        cpbe.file_sync_status()

        # pylint: disable=W0212
        self.assertEqual(be._status_changed_handler, [cpbe.process_status])


class ShutdownTestCase(BaseTestCase):
    """Test for the DBus service shurdown."""

    @defer.inlineCallbacks
    def test_shutdown(self):
        """The service can be shutdown."""
        called = []
        MockBackend.shutdown_func = lambda *a: called.append('shutdown')
        self.backend.shutdown(reply_handler=lambda: self.deferred.callback(1),
                              error_handler=self.got_error)
        yield self.deferred

        self.assertEqual(called, ['shutdown'])