#!/usr/bin/python

import unittest
import sys
# Make sure we're working in the source tree.
sys.path.insert(0, '.')

#class BaseFrontendTests(unittest.TestCase):
#    def setUp(self):
#        from ubiquity.frontend import base
#        self.frontend = base.BaseFrontend('ubiquity')
#
#    def tearDown(self):
#        self.frontend.stop_debconf()
        
class CoreFrontendTests:
    def testPluginsLoadProperly(self):
        should_load = ['ubi-language', 'ubi-timezone', 'ubi-console-setup',
                       'ubi-partman', 'ubi-usersetup', 'ubi-summary']
        plugins = [x.filter_class.__module__ for x in self.frontend.pages]
        for x in should_load:
            assert x in plugins, '%s not in %s' % (x, str(plugins))
    
    def testReturnToPartitioner(self):
        # bzr r3904, r3895
        self.frontend.installing = True
        self.frontend.next.set_label('Install')
        def nop(*args, **kwargs):
            pass
        self.frontend.switch_progress_windows = nop
        self.frontend.return_to_partitioning()
        assert self.frontend.dbfilter.__module__ == 'ubi-partman'
        assert self.frontend.dbfilter.ui.__module__ == 'ubi-partman'
        assert self.frontend.next.get_label() == '_Forward'

    def testPageNavigation(self):
        # bzr r4041, LP #556180
        c = self.ui.Controller(self.frontend)
        c.allow_change_step(False)
        assert self.frontend.next.get_property('sensitive') == False
        assert self.frontend.back.get_property('sensitive') == False
        
        c.allow_change_step(True)
        c.allow_go_forward(True)
        c.allow_go_backward(True)
        assert self.frontend.next.get_property('sensitive') == True
        assert self.frontend.back.get_property('sensitive') == True

    def tearDown(self):
        # Needed?
        self.frontend.quit_main_loop()
        self.frontend.stop_debconf()

# KDE applications run klauncher (kdelibs/kinit/klauncher_main.cpp), which
# tries to connect to the D-Bus session bus.  This wont work here as the system
# policy prevents one user from connecting to another's session bus.

#class KdeFrontendTests(CoreFrontendTests, unittest.TestCase):
#    def setUp(self):
#        from ubiquity.frontend import kde_ui
#        self.ui = kde_ui
#        self.frontend = kde_ui.Wizard('ubuntu')

# The noninteractive frontend needs access to /dev/console.

#class NoninteractiveFrontendTests(CoreFrontendTests, unittest.TestCase):
#    def setUp(self):
#        from ubiquity.frontend import noninteractive
#        self.ui = noninteractive
#        self.frontend = noninteractive.Wizard('ubuntu')

class GtkFrontendTests(CoreFrontendTests, unittest.TestCase):
    def setUp(self):
        from ubiquity.frontend import gtk_ui
        self.ui = gtk_ui
        self.frontend = gtk_ui.Wizard('ubuntu')

    def testSkippingPagesBackwards(self):
        # bzr r4055
        # Fix backing up past partitioning when manual partitioning was
        # selected (LP: #557210).
        part_auto = None
        for p in self.frontend.pages:
            self.frontend.add_history(p, p.widgets[0])
            # Advanced partitioning page.
            if p.filter_class.__module__ == 'ubi-partman':
                part_auto = (p, p.widgets[0])
                self.frontend.add_history(p, p.optional_widgets[0])
        assert part_auto is not None
        seen = []
        while True:
            h = self.frontend.history[-1]
            assert h not in seen, 'already been to %s' % str(h[1].name)
            seen.append(h)
            self.frontend.pop_history()
            if h[0].filter_class.__module__ == 'ubi-usersetup':
                self.frontend.add_history(*part_auto)
            else:
                h = self.frontend.history[-1]
                self.frontend.add_history(h[0], h[1])
            if len(self.frontend.history) == 1:
                break
    
    def testNoActiveItemRegionCombo(self):
        # bzr r4066, LP #521851
        idx = -1
        for page in self.frontend.pages:
            if page.module.NAME == 'timezone':
                idx = self.frontend.pages.index(page)
                break
        assert idx != -1
        ui = self.frontend.pages[idx].ui
        dbfilter = self.frontend.dbfilter
        dbfilter = self.frontend.pages[idx].filter_class(self, ui=ui)
        dbfilter.ui.controller.dbfilter = dbfilter
        dbfilter.ui.region_combo.set_active(-1)
        # Should return instead of raising
        # 'IndexError: could not find tree path'
        dbfilter.ui.on_region_combo_changed()
    
    def testPluginTranslate(self):
        # bzr r4061, LP #540929
        idx = -1
        for page in self.frontend.pages:
            if page.module.NAME == 'usersetup':
                idx = self.frontend.pages.index(page)
                break
        assert idx != -1
        self.frontend.pagesindex = idx
        ui = self.frontend.pages[idx].ui
        dbfilter = self.frontend.dbfilter
        dbfilter = self.frontend.pages[idx].filter_class(self, ui=ui)
        dbfilter.ui.controller.dbfilter = dbfilter
        
        self.frontend.translate_pages(reget=True)
        ui = dbfilter.ui
        widgets = (ui.username, ui.fullname, ui.password,
                   ui.verified_password, ui.hostname)
        cstrs = [x.inactive_message for x in widgets]
        self.frontend.locale = 'es'
        self.frontend.translate_pages(reget=True)
        esstrs = [x.inactive_message for x in widgets]
        common = set(cstrs) & set(esstrs)
        assert not common, 'Untranslated strings: %s' % ', '.join(common)

if __name__ == '__main__':
    unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
