uitest: start the switch to python's unittest

Change-Id: Ic6e0d99a567b7080928accb61ed036b1c25ec7ff
diff --git a/solenv/gbuild/UITest.mk b/solenv/gbuild/UITest.mk
index bbe76f6..dc6ea53 100644
--- a/solenv/gbuild/UITest.mk
+++ b/solenv/gbuild/UITest.mk
@@ -21,7 +21,7 @@ gb_UITest_EXECUTABLE_GDB := $(PYTHON_FOR_BUILD)
gb_UITest_DEPS :=
endif

gb_UITest_COMMAND := $(gb_UITest_EXECUTABLE) $(SRCDIR)/uitest/main.py
gb_UITest_COMMAND := $(gb_UITest_EXECUTABLE) $(SRCDIR)/uitest/test_main.py

.PHONY : $(call gb_UITest_get_clean_target,%)
$(call gb_UITest_get_clean_target,%) :
@@ -52,13 +52,12 @@ $(call gb_UITest_get_target,%) :| $(gb_UITest_DEPS)
		$(gb_UITest_COMMAND) \
		--soffice=path:$(INSTROOT)/$(LIBO_BIN_FOLDER)/soffice \
		--userdir=$(call gb_Helper_make_url,$(dir $(call gb_UITest_get_target,$*))user) \
		--file=$(SRCDIR)/uitest/$(strip $(MODULES)) \
		$(if $(gb_CppunitTest__interactive),, \
			> $@.log 2>&1 \
			|| ($(if $(value gb_CppunitTest_postprocess), \
					RET=$$?; \
					$(call gb_CppunitTest_postprocess,$(gb_UITest_EXECUTABLE_GDB),$@.core,$$RET) >> $@.log 2>&1;) \
				cat $@.log; $(gb_UITest_UNITTESTFAILED) Python $*))))
		--dir=$(SRCDIR)/uitest/$(strip $(MODULES)) \
		> $@.log 2>&1 \
		|| ($(if $(value gb_CppunitTest_postprocess), \
				RET=$$?; \
				$(call gb_CppunitTest_postprocess,$(gb_UITest_EXECUTABLE_GDB),$@.core,$$RET) >> $@.log 2>&1;) \
			cat $@.log; $(gb_UITest_UNITTESTFAILED) Python $*)))

# always use udkapi and URE services
define gb_UITest_UITest
diff --git a/uitest/UITestCase.py b/uitest/UITestCase.py
new file mode 100644
index 0000000..5628d9d
--- /dev/null
+++ b/uitest/UITestCase.py
@@ -0,0 +1,32 @@
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import unittest

from uitest_helper import UITest

from connection import PersistentConnection, OfficeConnection

class UITestCase(unittest.TestCase):

    def __init__(self, test_name, opts):
        unittest.TestCase.__init__(self, test_name)
        self.opts = opts

    def setUp(self):
        self.connection = PersistentConnection(self.opts)
        self.connection.setUp()
        self.xContext = self.connection.getContext()
        self.xUITest = self.xContext.ServiceManager.createInstanceWithContext(
                "org.libreoffice.uitest.UITest", self.xContext)

        self.ui_test = UITest(self.xUITest, self.xContext)

    def tearDown(self):
        self.connection.tearDown()

# vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/uitest/calc_tests/about_test.py b/uitest/calc_tests/about_test.py
index b512084..df90e7d 100644
--- a/uitest/calc_tests/about_test.py
+++ b/uitest/calc_tests/about_test.py
@@ -5,25 +5,23 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import time

from uitest_helper import UITest

def test_about_dlg(xContext):
    xUITest = xContext.ServiceManager.createInstanceWithContext(
            "org.libreoffice.uitest.UITest", xContext)
from UITestCase import UITestCase

    ui_test = UITest(xUITest, xContext)
class AboutDlgTest(UITestCase):

    ui_test.create_doc_in_start_center("calc")
    def test_about_dlg(self):

    ui_test.execute_dialog_through_command(".uno:About")
        self.ui_test.create_doc_in_start_center("calc")

    xAboutDlg = xUITest.getTopFocusWindow()
        self.ui_test.execute_dialog_through_command(".uno:About")

    xCloseBtn = xAboutDlg.getChild("close")
    xCloseBtn.executeAction("CLICK", tuple())
        xAboutDlg = self.xUITest.getTopFocusWindow()

    ui_test.close_doc()
        xCloseBtn = xAboutDlg.getChild("close")
        xCloseBtn.executeAction("CLICK", tuple())

        self.ui_test.close_doc()

# vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/uitest/calc_tests/calc_demo.txt b/uitest/calc_tests/calc_demo.txt
index 6ff4e22..1c2629e 100644
--- a/uitest/calc_tests/calc_demo.txt
+++ b/uitest/calc_tests/calc_demo.txt
@@ -2,4 +2,3 @@ calc_tests.create_range_name.create_range_name
calc_tests.create_range_name.create_local_range_name
calc_tests.function_wizard.open_function_wizard
calc_tests.gridwindow.input
calc_tests.about_test.test_about_dlg
diff --git a/uitest/calc_tests/create_chart.py b/uitest/calc_tests/create_chart.py
index 6ec7082..6db4d58 100644
--- a/uitest/calc_tests/create_chart.py
+++ b/uitest/calc_tests/create_chart.py
@@ -9,6 +9,8 @@ from uitest_helper import UITest

from helper import mkPropertyValues

from UITestCase import UITestCase

import time

try:
@@ -21,199 +23,171 @@ except ImportError:
    print("URE_BOOTSTRAP=file:///installation/opt/program/fundamentalrc")
    raise

def add_content_to_cell(gridwin, cell, content):
    selectProps = mkPropertyValues({"CELL": cell})
    gridwin.executeAction("SELECT", selectProps)
class CalcChartUIDemo(UITestCase):

    contentProps = mkPropertyValues({"TEXT": content})
    gridwin.executeAction("TYPE", contentProps)
    def add_content_to_cell(self, gridwin, cell, content):
        selectProps = mkPropertyValues({"CELL": cell})
        gridwin.executeAction("SELECT", selectProps)

        contentProps = mkPropertyValues({"TEXT": content})
        gridwin.executeAction("TYPE", contentProps)

def fill_spreadsheet(xUITest):
    xCalcDoc = xUITest.getTopFocusWindow()
    xGridWindow = xCalcDoc.getChild("grid_window")
    def fill_spreadsheet(self):
        xCalcDoc = self.xUITest.getTopFocusWindow()
        xGridWindow = xCalcDoc.getChild("grid_window")

    add_content_to_cell(xGridWindow, "A1", "col1")
    add_content_to_cell(xGridWindow, "B1", "col2")
    add_content_to_cell(xGridWindow, "C1", "col3")
    add_content_to_cell(xGridWindow, "A2", "1")
    add_content_to_cell(xGridWindow, "B2", "3")
    add_content_to_cell(xGridWindow, "C2", "5")
        self.add_content_to_cell(xGridWindow, "A1", "col1")
        self.add_content_to_cell(xGridWindow, "B1", "col2")
        self.add_content_to_cell(xGridWindow, "C1", "col3")
        self.add_content_to_cell(xGridWindow, "A2", "1")
        self.add_content_to_cell(xGridWindow, "B2", "3")
        self.add_content_to_cell(xGridWindow, "C2", "5")

    xGridWindow.executeAction("SELECT", mkPropertyValues({"RANGE": "A1:C2"}))
        xGridWindow.executeAction("SELECT", mkPropertyValues({"RANGE": "A1:C2"}))

def cancel_immediately(xContext):
    xUITest = xContext.ServiceManager.createInstanceWithContext(
            "org.libreoffice.uitest.UITest", xContext)
    def test_cancel_immediately(self):

    ui_test = UITest(xUITest, xContext)
        self.ui_test.create_doc_in_start_center("calc")

    ui_test.create_doc_in_start_center("calc")
        self.fill_spreadsheet()

    fill_spreadsheet(xUITest)
        self.ui_test.execute_dialog_through_command(".uno:InsertObjectChart")

    ui_test.execute_dialog_through_command(".uno:InsertObjectChart")
        xChartDlg = self.xUITest.getTopFocusWindow()

    xChartDlg = xUITest.getTopFocusWindow()
        xCancelBtn = xChartDlg.getChild("cancel")
        xCancelBtn.executeAction("CLICK", tuple())

    xCancelBtn = xChartDlg.getChild("cancel")
    xCancelBtn.executeAction("CLICK", tuple())
        self.ui_test.close_doc()

    ui_test.close_doc()
    def test_create_from_first_page(self):

def create_from_first_page(xContext):
    xUITest = xContext.ServiceManager.createInstanceWithContext(
            "org.libreoffice.uitest.UITest", xContext)
        self.ui_test.create_doc_in_start_center("calc")

    ui_test = UITest(xUITest, xContext)
        self.fill_spreadsheet()

    ui_test.create_doc_in_start_center("calc")
        self.ui_test.execute_dialog_through_command(".uno:InsertObjectChart")

    fill_spreadsheet(xUITest)
        xChartDlg = self.xUITest.getTopFocusWindow()
        print(xChartDlg.getChildren())
        time.sleep(2)

    ui_test.execute_dialog_through_command(".uno:InsertObjectChart")
        xOkBtn = xChartDlg.getChild("finish")
        xOkBtn.executeAction("CLICK", tuple())

    xChartDlg = xUITest.getTopFocusWindow()
    print(xChartDlg.getChildren())
    time.sleep(2)
        time.sleep(2)

    xOkBtn = xChartDlg.getChild("finish")
    xOkBtn.executeAction("CLICK", tuple())
        self.ui_test.close_doc()

    time.sleep(2)
    def test_create_from_second_page(self):

    ui_test.close_doc()
        self.ui_test.create_doc_in_start_center("calc")

def create_from_second_page(xContext):
    xUITest = xContext.ServiceManager.createInstanceWithContext(
            "org.libreoffice.uitest.UITest", xContext)
        self.fill_spreadsheet()

    ui_test = UITest(xUITest, xContext)
        self.ui_test.execute_dialog_through_command(".uno:InsertObjectChart")

    ui_test.create_doc_in_start_center("calc")
        xChartDlg = self.xUITest.getTopFocusWindow()
        print(xChartDlg.getChildren())
        time.sleep(2)

    fill_spreadsheet(xUITest)
        xNextBtn = xChartDlg.getChild("next")
        xNextBtn.executeAction("CLICK", tuple())

    ui_test.execute_dialog_through_command(".uno:InsertObjectChart")
        print(xChartDlg.getChildren())

    xChartDlg = xUITest.getTopFocusWindow()
    print(xChartDlg.getChildren())
    time.sleep(2)
        time.sleep(2)

    xNextBtn = xChartDlg.getChild("next")
    xNextBtn.executeAction("CLICK", tuple())
        xDataInRows = xChartDlg.getChild("RB_DATAROWS")
        xDataInRows.executeAction("CLICK", tuple())

    print(xChartDlg.getChildren())
        time.sleep(2)

    time.sleep(2)
        xDataInCols = xChartDlg.getChild("RB_DATACOLS")
        xDataInCols.executeAction("CLICK", tuple())

    xDataInRows = xChartDlg.getChild("RB_DATAROWS")
    xDataInRows.executeAction("CLICK", tuple())
        time.sleep(2)

    time.sleep(2)
        xCancelBtn = xChartDlg.getChild("finish")
        xCancelBtn.executeAction("CLICK", tuple())

    xDataInCols = xChartDlg.getChild("RB_DATACOLS")
    xDataInCols.executeAction("CLICK", tuple())

    time.sleep(2)

    xCancelBtn = xChartDlg.getChild("finish")
    xCancelBtn.executeAction("CLICK", tuple())

    time.sleep(5)

    ui_test.close_doc()

def deselect_chart(xContext):
    xUITest = xContext.ServiceManager.createInstanceWithContext(
            "org.libreoffice.uitest.UITest", xContext)

    ui_test = UITest(xUITest, xContext)

    ui_test.create_doc_in_start_center("calc")

    fill_spreadsheet(xUITest)
        time.sleep(5)

    xCalcDoc = xUITest.getTopFocusWindow()
    xGridWindow = xCalcDoc.getChild("grid_window")
        self.ui_test.close_doc()

    ui_test.execute_dialog_through_command(".uno:InsertObjectChart")
    def test_deselect_chart(self):
        self.ui_test.create_doc_in_start_center("calc")

    xChartDlg = xUITest.getTopFocusWindow()
        self.fill_spreadsheet()

    xNextBtn = xChartDlg.getChild("finish")
    xNextBtn.executeAction("CLICK", tuple())
        xCalcDoc = self.xUITest.getTopFocusWindow()
        xGridWindow = xCalcDoc.getChild("grid_window")

    xGridWindow.executeAction("DESELECT", mkPropertyValues({"OBJECT": ""}))
        self.ui_test.execute_dialog_through_command(".uno:InsertObjectChart")

    time.sleep(2)
        xChartDlg = self.xUITest.getTopFocusWindow()

    ui_test.close_doc()
        xNextBtn = xChartDlg.getChild("finish")
        xNextBtn.executeAction("CLICK", tuple())

def activate_chart(xContext):
    xUITest = xContext.ServiceManager.createInstanceWithContext(
            "org.libreoffice.uitest.UITest", xContext)
        xGridWindow.executeAction("DESELECT", mkPropertyValues({"OBJECT": ""}))

    ui_test = UITest(xUITest, xContext)
        time.sleep(2)

    ui_test.create_doc_in_start_center("calc")
        self.ui_test.close_doc()

    fill_spreadsheet(xUITest)
    def test_activate_chart(self):

    xCalcDoc = xUITest.getTopFocusWindow()
    xGridWindow = xCalcDoc.getChild("grid_window")
        self.ui_test.create_doc_in_start_center("calc")

    ui_test.execute_dialog_through_command(".uno:InsertObjectChart")
        self.fill_spreadsheet()

    xChartDlg = xUITest.getTopFocusWindow()
        xCalcDoc = self.xUITest.getTopFocusWindow()
        xGridWindow = xCalcDoc.getChild("grid_window")

    xNextBtn = xChartDlg.getChild("finish")
    xNextBtn.executeAction("CLICK", tuple())
        self.ui_test.execute_dialog_through_command(".uno:InsertObjectChart")

    xGridWindow.executeAction("DESELECT", mkPropertyValues({"OBJECT": ""}))
        xChartDlg = self.xUITest.getTopFocusWindow()

    time.sleep(2)
        xNextBtn = xChartDlg.getChild("finish")
        xNextBtn.executeAction("CLICK", tuple())

    xGridWindow.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
    xGridWindow.executeAction("ACTIVATE", tuple())
        xGridWindow.executeAction("DESELECT", mkPropertyValues({"OBJECT": ""}))

    time.sleep(2)
        time.sleep(2)

    xGridWindow.executeAction("DESELECT", mkPropertyValues({"OBJECT": ""}))
        xGridWindow.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
        xGridWindow.executeAction("ACTIVATE", tuple())

    ui_test.close_doc()
        time.sleep(2)

def select_chart_element(xContext):
    xUITest = xContext.ServiceManager.createInstanceWithContext(
            "org.libreoffice.uitest.UITest", xContext)
        xGridWindow.executeAction("DESELECT", mkPropertyValues({"OBJECT": ""}))

    ui_test = UITest(xUITest, xContext)
        self.ui_test.close_doc()

    ui_test.create_doc_in_start_center("calc")
    def select_chart_element(self):

    fill_spreadsheet(xUITest)
        self.ui_test.create_doc_in_start_center("calc")

    xCalcDoc = xUITest.getTopFocusWindow()
    xGridWindow = xCalcDoc.getChild("grid_window")
        self.fill_spreadsheet()

    ui_test.execute_dialog_through_command(".uno:InsertObjectChart")
        xCalcDoc = self.xUITest.getTopFocusWindow()
        xGridWindow = xCalcDoc.getChild("grid_window")

    xChartDlg = xUITest.getTopFocusWindow()
        self.ui_test.execute_dialog_through_command(".uno:InsertObjectChart")

    xNextBtn = xChartDlg.getChild("finish")
    xNextBtn.executeAction("CLICK", tuple())
        xChartDlg = self.xUITest.getTopFocusWindow()

    xGridWindow.executeAction("DESELECT", mkPropertyValues({"OBJECT": ""}))
        xNextBtn = xChartDlg.getChild("finish")
        xNextBtn.executeAction("CLICK", tuple())

    time.sleep(2)
        xGridWindow.executeAction("DESELECT", mkPropertyValues({"OBJECT": ""}))

    xGridWindow.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
    xGridWindow.executeAction("ACTIVATE", tuple())
        xGridWindow.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
        xGridWindow.executeAction("ACTIVATE", tuple())

    time.sleep(2)
    xCalcDoc = xUITest.getTopFocusWindow()
    print(xCalcDoc.getChildren())
    time.sleep(1)
    ui_test.close_doc()
        xCalcDoc = self.xUITest.getTopFocusWindow()
        print(xCalcDoc.getChildren())
        self.ui_test.close_doc()

# vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/uitest/test_main.py b/uitest/test_main.py
new file mode 100644
index 0000000..2a0fca1
--- /dev/null
+++ b/uitest/test_main.py
@@ -0,0 +1,101 @@
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import sys
import getopt
import os
import unittest
import calc_tests
import importlib
import importlib.machinery

from UITestCase import UITestCase

from connection import PersistentConnection, OfficeConnection

def parseArgs(argv):
    (optlist,args) = getopt.getopt(argv[1:], "hr",
            ["help", "soffice=", "userdir=", "dir="])
    return (dict(optlist), args)

def usage():
    message = """usage: {program} [option]... [task_file]..."
 -h | --help:      print usage information
 {connection_params}
 the 'task_file' parameters should be
  full absolute pathnames, not URLs."""
    print(message.format(program = os.path.basename(sys.argv[0]), \
        connection_params = OfficeConnection.getHelpText()))


def find_test_files(dir_path):
    valid_files = []
    for f in os.listdir(dir_path):
        file_path = os.path.join(dir_path, f)

        # don't go through the sub-directories
        if not os.path.isfile(file_path):
            continue

        # ignore any non .py files
        if not os.path.splitext(file_path)[1] == ".py":
            continue

        # ignore the __init__.py file
        # it is obviously not a test file
        if f is "__init__.py":
            continue

        valid_files.append(file_path)

    return valid_files

def get_classes_of_module(module):
    md = module.__dict__
    return [ md[c] for c in md if (
            isinstance(md[c], type) and md[c].__module__ == module.__name__ ) ]

def get_test_case_classes_of_module(module):
    classes = get_classes_of_module(module)
    return [ c for c in classes if issubclass(c, UITestCase) ]

def get_test_suite(opts):
    test_loader = unittest.TestLoader()
    test_suite = unittest.TestSuite()

    valid_test_files = find_test_files(opts['--dir'])
    for test_file in valid_test_files:
        module_name = os.path.splitext(os.path.split(test_file)[1])[0]
        loader = importlib.machinery.SourceFileLoader(module_name, test_file)
        mod = loader.load_module()
        classes = get_test_case_classes_of_module(mod)
        for c in classes:
            test_names = test_loader.getTestCaseNames(c)
            for test_name in test_names:
                obj = c(test_name, opts)
                test_suite.addTest(obj)

    return test_suite


if __name__ == '__main__':
    (opts,args) = parseArgs(sys.argv)
    if "-h" in opts or "--help" in opts:
        usage()
        sys.exit()
    elif not "--soffice" in opts:
        usage()
        sys.exit(1)
    elif not "--dir" in opts:
        usage()
        sys.exit()
    test_suite = get_test_suite(opts)
    print(test_suite)

    unittest.TextTestRunner().run(test_suite)

# vim:set shiftwidth=4 softtabstop=4 expandtab: */