#!/usr/bin/python

'''Run self tests of the Qt4 implementation.'''

# (c) 2008 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys, imp, os.path, unittest, time

from PyQt4.QtCore import *
from PyQt4.QtGui import *

root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))

# prefer modules in the local tree
sys.path.insert(0, root_dir)

from jockey.oslib import OSLib
import sandbox


class QtUITest(unittest.TestCase):
    def setUp(self):
        sys.argv = ['ui-test']
        self.ui = KDEUI()

    def test_download_progress(self):
        '''download progress dialog'''

        self.ui.ui_init()
        cur = 0
        total = 10000
        self.ui.ui_download_start('Running complete download', total)
        self.ui.ui_idle()
        while cur < total:
            cur += 4096
            if cur > total:
                cur = total
            # not finished yet
            self.assertEqual(self.ui.ui_download_progress(cur, total), False)
            self.ui.ui_idle()
            time.sleep(0.2)
        self.ui.ui_download_finish()

    def test_0_ui_notification(self):
        '''ui_notification()'''
        self.ui.ui_notification('NotificationTitle', 'NotificationText')
        self.ui.ui_idle()

if __name__ == '__main__':
    # set the global OSLib instance for all tests
    OSLib.inst = sandbox.TestOSLib()
    # get KDEUI
    qapp = QApplication(sys.argv)

    try:
        KDEUI = getattr(imp.load_source('mod_gtk_impl', os.path.join(root_dir,
                        'kde', 'jockey-kde')), 'KDEUI')

        # run tests
        unittest.main()
    finally:
        # clean up pyc
        os.unlink(os.path.join(root_dir, 'kde', 'jockey-kdec'))
