~mvo/ubuntu-sso-client/strawman-lp711413

« back to all changes in this revision

Viewing changes to ubuntu_sso/qt/tests/test_proxy_dialog.py

  • Committer: Tarmac
  • Author(s): Diego Sarmentero, Roberto Alsina, Natalia B. Bidart, manuel.delapena at canonical, Manuel de la Pena
  • Date: 2012-03-27 19:33:27 UTC
  • mfrom: (831.1.2 stable-3-0-update-2.99.91.1)
  • Revision ID: tarmac-20120327193327-rgwi1z1cbfxxrx9d
Tags: release-2_99_91_1
[ Diego Sarmentero <diego.sarmentero@canonical.com> ]
  - Converting to unicode some data returned by webclient in
    QByteArray format (LP: #961315).
  - Adding ubuntu_sso.qt.main module to the setup script (LP: #965377).
  - Fixed the backend getting stuck installing the qtreactor before
    the qt process is executed and running the application in the
    twisted process (This fix affects only Windows OS) (LP: #961511).

[ Roberto Alsina <roberto.alsina@canonical.com> ]
   - Forced white background (LP: #961346).

[ Manuel de la Pena <manuel.delapena@canonical.com> ]
  - Fixed dialog on window by using show so that the QEventLoop does
    not block the execution of the main loop of the reactor which did
    not allow the deferreds to be fired (LP: #960481).

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# with this program.  If not, see <http://www.gnu.org/licenses/>.
16
16
"""Test the proxy UI."""
17
17
 
18
 
from twisted.internet import defer
 
18
import qt4reactor
 
19
 
 
20
from PyQt4.QtCore import QObject, pyqtSignal
 
21
from twisted.internet import defer, reactor
19
22
from twisted.trial.unittest import TestCase
20
23
 
21
24
from ubuntu_sso.qt import proxy_dialog
301
304
        self.retry = retry
302
305
 
303
306
 
 
307
class FakeCredsDialog(QObject):
 
308
    """A fake dialog."""
 
309
 
 
310
    finished = pyqtSignal(int)
 
311
 
 
312
    def __init__(self, called, code):
 
313
        """Create a new instance."""
 
314
        super(FakeCredsDialog, self).__init__()
 
315
        self.domain = None
 
316
        self.retry = None
 
317
        self.called = called
 
318
        self.return_code = code
 
319
 
 
320
    def __call__(self, domain, retry):
 
321
        """Fake init."""
 
322
        self.domain = domain
 
323
        self.retry = retry
 
324
        return self
 
325
 
 
326
    def show(self):
 
327
        """Show the dialog."""
 
328
        self.called.append(('show',))
 
329
 
 
330
    def exec_(self):
 
331
        """Show the dialog."""
 
332
        self.called.append(('exec_',))
 
333
        return self.return_code
 
334
 
 
335
 
 
336
class FakeQApplication(object):
 
337
    """A fake QApplication."""
 
338
 
 
339
    test_instance = None
 
340
 
 
341
    def __init__(self, called):
 
342
        """Create a new instance."""
 
343
        self.called = called
 
344
        self.args = None
 
345
        self.code = None
 
346
        FakeQApplication.test_instance = self
 
347
 
 
348
    def __call__(self, args):
 
349
        """Fake construntor."""
 
350
        self.args = args
 
351
 
 
352
    @classmethod
 
353
    def instance(cls):
 
354
        """Return the instance."""
 
355
        return FakeQApplication.test_instance
 
356
 
 
357
    def exit(self, code):
 
358
        """Exit the app."""
 
359
        self.called.append(('exit', code))
 
360
        self.code = code
 
361
 
 
362
 
304
363
class MainTest(TestCase):
305
364
    """Test the main method."""
306
365
 
310
369
        yield super(MainTest, self).setUp()
311
370
        self.called = []
312
371
 
 
372
        self.return_code = 0
 
373
        self.dialog = FakeCredsDialog(self.called, self.return_code)
 
374
        self.patch(proxy_dialog, 'ProxyCredsDialog', self.dialog)
 
375
 
313
376
        self.args = FakeArgs(domain='domain', retry=False)
314
377
        self.patch(proxy_dialog, 'parse_args', lambda: self.args)
315
378
 
316
 
        self.return_code = 0
317
 
        self.patch(proxy_dialog.ProxyCredsDialog, 'exec_', lambda x:
318
 
                self.return_code)
319
379
        self.exit_code = None
320
380
 
 
381
        self.app = FakeQApplication(self.called)
 
382
 
 
383
        self.patch(proxy_dialog, 'QApplication', self.app)
 
384
 
 
385
    def _set_old_platform(self, platform):
 
386
        """Set back the platform."""
 
387
        proxy_dialog.sys.platform = platform
 
388
 
 
389
    def _assert_basic_start(self):
 
390
        """Assert the common code is actually ran."""
 
391
        self.assertEqual(self.args.domain, self.dialog.domain)
 
392
        self.assertEqual(self.args.retry, self.dialog.retry)
 
393
 
 
394
    def test_linux_main(self):
 
395
        """Test the main method on linux."""
 
396
        old_platform = proxy_dialog.sys.platform
 
397
        proxy_dialog.sys.platform = 'linux'
 
398
        self.addCleanup(self._set_old_platform, old_platform)
 
399
 
321
400
        def fake_exit(code):
322
 
            """Fake the sys.exit."""
 
401
            """Fake sys exit."""
323
402
            self.called.append(('exit', code))
324
 
            self.exit_code = code
325
403
 
326
404
        self.patch(proxy_dialog.sys, 'exit', fake_exit)
327
405
 
328
 
        def fake_application(args):
329
 
            """Fake a QApplication."""
330
 
            self.called.append(('QApplication', args))
331
 
 
332
 
        self.patch(proxy_dialog, 'QApplication', fake_application)
333
 
 
334
 
    def test_main(self):
335
 
        """Test the main method."""
336
 
        proxy_dialog.main()
337
 
        self.assertIn('QApplication', self.called[0])
 
406
        proxy_dialog.main()
 
407
 
 
408
        self._assert_basic_start()
 
409
        self.assertIn(('exec_',), self.called)
 
410
        self.assertIn(('exit', self.return_code), self.called)
 
411
 
 
412
    def test_windows_main(self):
 
413
        """Test the main method on windows."""
 
414
        old_platform = proxy_dialog.sys.platform
 
415
        proxy_dialog.sys.platform = 'win32'
 
416
        self.addCleanup(self._set_old_platform, old_platform)
 
417
 
 
418
        def fake_install():
 
419
            """Fake the reactor installation."""
 
420
            self.called.append(('install',))
 
421
 
 
422
        self.patch(qt4reactor, 'install', fake_install)
 
423
 
 
424
        def fake_start():
 
425
            """Fake reactor.start."""
 
426
            self.called.append(('run',))
 
427
 
 
428
        self.patch(reactor, 'run', fake_start)
 
429
 
 
430
        def fake_exit(code):
 
431
            """Fake the exit method."""
 
432
            self.called.append(('exit_code', code))
 
433
 
 
434
        self.patch(proxy_dialog, 'exit_code', fake_exit)
 
435
 
 
436
        proxy_dialog.main()
 
437
 
 
438
        self._assert_basic_start()
 
439
        self.assertIn(('show',), self.called)
 
440
        self.assertIn(('run',), self.called)
 
441
 
 
442
        self.dialog.finished.emit(self.return_code)
 
443
        self.assertIn(('exit_code', self.return_code), self.called)
 
444
 
 
445
    def test_exit_code_windows(self):
 
446
        """"Test the exit code function."""
 
447
        old_platform = proxy_dialog.sys.platform
 
448
        proxy_dialog.sys.platform = 'win32'
 
449
        self.addCleanup(self._set_old_platform, old_platform)
 
450
 
 
451
        def fake_stop():
 
452
            """Fake reactor.start."""
 
453
            self.called.append(('stop',))
 
454
 
 
455
        self.patch(reactor, 'stop', fake_stop)
 
456
 
 
457
        proxy_dialog.exit_code(self.return_code)
 
458
        self.assertIn(('stop',), self.called)
338
459
        self.assertIn(('exit', self.return_code), self.called)