#!/usr/bin/python3

import sys
import unittest

# 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(unittest.TestCase):
    def testPluginsLoadProperly(self):
        should_load = ['ubi-language', 'ubi-timezone', 'ubi-console-setup',
                       'ubi-partman', 'ubi-usersetup']
        plugins = [x.filter_class.__module__ for x in self.frontend.pages]
        for x in should_load:
            self.assertIn(x, 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()
        self.assertEqual('ubi-partman', self.frontend.dbfilter.__module__)
        self.assertEqual('ubi-partman', self.frontend.dbfilter.ui.__module__)
        self.assertEqual('_Forward', self.frontend.next.get_label())

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

        c.allow_change_step(True)
        c.allow_go_forward(True)
        c.allow_go_backward(True)
        self.assertTrue(self.frontend.next.get_property('sensitive'))
        self.assertTrue(self.frontend.back.get_property('sensitive'))

    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):
#     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):
#     def setUp(self):
#         from ubiquity.frontend import noninteractive
#         self.ui = noninteractive
#         self.frontend = noninteractive.Wizard('ubuntu')


class GtkFrontendTests(CoreFrontendTests):
    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])
        self.assertIsNotNone(part_auto)
        seen = []
        while True:
            h = self.frontend.history[-1]
            self.assertNotIn(h, seen, 'already been to %s' % 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
        self.assertNotEqual(-1, 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
        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
        self.assertNotEqual(-1, idx)
        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)
        self.assertEqual(
            set(), common, 'Untranslated strings: %s' % ', '.join(common))


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