#!/usr/bin/env python2.7

import unittest
import time
import dogtail.config
dogtail.config.config.logDebugToFile = False

class GtkDemoTest(unittest.TestCase):
    """
    TestCase subclass which handles bringing up and shutting down gtk-demo as a fixture.  Used for writing other test cases.
    """
    def setUp(self):
        import dogtail.utils
        self.pid = dogtail.utils.run('gtk-demo')
        self.app = dogtail.tree.root.application('gtk-demo')

    def tearDown(self):
        import os, signal
        os.kill(self.pid, signal.SIGKILL)
        # Sleep just enough to let the app actually die.
        # AT-SPI doesn't like being hammered too fast.
        time.sleep(0.5)

    def runDemo(self, demoName):
        """
        Click on the named demo within the gtk-demo app.
        """
        tree = self.app.child(roleName="tree table")
        tree.child(demoName).doActionNamed('activate')

class TestSlowdown(GtkDemoTest):
    def testInteractiveDialog(self):

        self.runDemo('Dialog and Message Boxes')
        wnd = self.app.window('Dialogs')

        for i in range(1, 5000):
            start_time = time.time()

            wnd.button('Interactive Dialog').doActionNamed('click')

            dlg = self.app.dialog('Interactive Dialog')
            dlg.button('OK').doActionNamed('click')

            end_time = time.time()
            print ("***** TIME %s: %s" % (i,  end_time - start_time ))

if __name__ == '__main__':
    unittest.main()
