Пример #1
0
def test_on_clean_storing_with_name_in_path(caplog):
    print("test_on_clean_storing_with_name_in_path")

    testing_utils.dummy_gui(None)

    testing_utils.initialize_environment(gui_config={
        'HISTORY_ENABLED': False,
        'AUTO_BACKUP_ENABLED': False
    },
                                         gui_already_started=False)

    path_old_format = testing_utils.get_test_sm_path(
        os.path.join("unit_test_state_machines",
                     "id_to_name_plus_id_storage_format_test_do_not_update"))
    path_new_format = os.path.join(
        testing_utils.get_unique_temp_path(),
        "id_to_name_plus_id_storage_format_test_do_not_update")

    # gui imports better after initialization
    from rafcon.gui.models.state_machine import StateMachineModel

    shutil.copytree(path_old_format, path_new_format)
    from rafcon.core.storage import storage
    sm = storage.load_state_machine_from_path(path_new_format)
    check_state_recursively_if_state_scripts_are_valid(sm.root_state)
    sm.base_path = path_new_format
    sm_m = StateMachineModel(sm)
    try:
        on_save_activate(sm_m, logger)
        check_that_all_files_are_there(sm, with_print=False)
        check_id_and_name_plus_id_format(path_old_format, path_new_format,
                                         sm_m)
    finally:
        testing_utils.shutdown_environment(caplog=caplog,
                                           unpatch_threading=False)
Пример #2
0
def test_scoped_data(caplog):
    storage_path = testing_utils.get_unique_temp_path()

    sm = create_state_machine()

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    state_machine = StateMachine(sm_loaded.root_state)

    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(
        state_machine.state_machine_id)

    try:
        assert state_machine.root_state.output_data[
            "data_output_port1"] == 42.0
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
Пример #3
0
def save_state_machine(with_gui=True):
    import rafcon
    from rafcon.core.singleton import state_machine_execution_engine
    import rafcon.gui.singleton as gui_singleton
    from rafcon.core.storage import storage

    path = testing_utils.get_unique_temp_path()
    if with_gui:
        state_machine = call_gui_callback(create_models)
    else:
        state_machine = create_models()

    if with_gui:
        sm_model = rafcon.gui.singleton.state_machine_manager_model.state_machines[
            state_machine.state_machine_id]
        menubar_ctrl = gui_singleton.main_window_controller.get_controller(
            'menu_bar_controller')
        # sm_model.state_machine.base_path = path
        call_gui_callback(menubar_ctrl.on_save_as_activate, None, None, path)
        # call_gui_callback(menubar_ctrl.on_quit_activate, None)
        call_gui_callback(check_that_all_files_are_there,
                          state_machine,
                          with_print=False)
    else:
        storage.save_state_machine_to_path(state_machine,
                                           path,
                                           delete_old_state_machine=False)
        check_that_all_files_are_there(state_machine, with_print=False)
Пример #4
0
def test_hierarchy_save_load_test(caplog):
    storage_path = testing_utils.get_unique_temp_path()

    hierarchy_state = create_hierarchy_state()
    sm = StateMachine(hierarchy_state)

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    state_machine = StateMachine(sm_loaded.root_state)

    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(
        state_machine.state_machine_id)
    try:
        assert state_machine.root_state.output_data["output1"] == 52.0
        # 2 type error -> one child output port data type error and root state scoped data type error
        testing_utils.assert_logger_warnings_and_errors(caplog,
                                                        expected_errors=2)
    finally:
        testing_utils.test_multithreading_lock.release()
Пример #5
0
def test_default_values_of_data_ports(caplog):

    storage_path = testing_utils.get_unique_temp_path()
    print storage_path

    sm = create_state_machine()

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    root_state = sm_loaded.root_state

    state_machine = StateMachine(root_state)
    testing_utils.test_multithreading_lock.acquire()

    rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id)

    print root_state.output_data
    try:
        assert root_state.output_data["output_data_port1"] == "default_value"
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
Пример #6
0
def test_runtime_checks_for_data_port_data_types(caplog):

    storage_path = testing_utils.get_unique_temp_path()
    print storage_path

    sm = create_state_machine2()

    # test output port type check
    script_text = 'def execute(self, inputs, outputs, gvm):\n' \
                  '    outputs["output_data_port1"] = [1, 2, 3]\n' \
                  '    return 3'
    sm.get_state_by_path('FirstLevel1/ZeroLevel2').script_text = script_text

    # TODO comment (next 2 lines) in after final fix of tuple storing bug is applied and remove 3 line (4 storage check)
    # storage.save_state_machine_to_path(sm, storage_path)
    # sm_loaded = storage.load_state_machine_from_path(storage_path)
    sm_loaded = sm

    root_state = sm_loaded.root_state

    state_machine = StateMachine(root_state)
    testing_utils.test_multithreading_lock.acquire()

    rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id)
    # 6 errors -> IN ORDER output port-, root state scoped-, input port-, output port-, root state scoped- and
    # root state output port-data-type-errors
    testing_utils.shutdown_environment_only_core(caplog=caplog, expected_errors=6)
def test_concurrency_barrier_save_load(caplog):
    concurrency_barrier_state = create_concurrency_barrier_state()

    state_machine = StateMachine(concurrency_barrier_state)
    test_path = testing_utils.get_unique_temp_path()
    storage.save_state_machine_to_path(state_machine, test_path)
    sm_loaded = storage.load_state_machine_from_path(test_path)

    root_state = sm_loaded.root_state
    input_data = {"input_data_port1": 0.1, "input_data_port2": 0.1}
    output_data = {"output_data_port1": None}
    root_state.input_data = input_data
    root_state.output_data = output_data

    state_machine = StateMachine(root_state)
    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "var_x") == 10
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "var_y") == 20
        assert root_state.final_outcome.outcome_id == 4

        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            state_machine.state_machine_id)
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                     expected_warnings=0,
                                                     expected_errors=1)
def test_unchanged_storage_format(caplog):
    """This test ensures that the state machine storage format does not change in patch releases"""

    from rafcon.core.storage import storage
    from rafcon.gui.models.state_machine import StateMachineModel
    import rafcon

    path = get_backward_compatibility_state_machines_path()
    if not os.path.exists(path):
        logger.info(
            "test_unchanged_storage_format: the current python interpreter version is not supported"
        )
        return

    testing_utils.initialize_environment(
        gui_config={
            'HISTORY_ENABLED': False,
            'AUTO_BACKUP_ENABLED': False
        },
        libraries={
            'unit_test_state_machines':
            testing_utils.get_test_sm_path("unit_test_state_machines")
        },
        gui_already_started=False)
    try:
        current_rafcon_version = StrictVersion(rafcon.__version__).version
        current_minor = "{}.{}".format(current_rafcon_version[0],
                                       current_rafcon_version[1])
        for filename in os.listdir(path):
            if filename.startswith(current_minor):
                old_state_machine_path = os.path.join(path, filename)
                break
        else:
            assert False, "There is no state machine for the current RAFCON minor version {}".format(
                current_minor)

        state_machine = storage.load_state_machine_from_path(
            old_state_machine_path)
        state_machine_m = StateMachineModel(state_machine)
        new_state_machine_path = testing_utils.get_unique_temp_path()
        storage.save_state_machine_to_path(state_machine,
                                           new_state_machine_path, True, True)
        state_machine_m.store_meta_data(copy_path=new_state_machine_path)

        old_state_machine_hash = calculate_state_machine_hash(
            old_state_machine_path)
        new_state_machine_hash = calculate_state_machine_hash(
            new_state_machine_path)
        assert old_state_machine_hash.digest(
        ) == new_state_machine_hash.digest()
    except Exception:
        raise
    finally:
        testing_utils.shutdown_environment(caplog=caplog,
                                           unpatch_threading=False)
Пример #9
0
def shutdown_gui(duration_wait_for_gui):
    # wait for menu bar and main window controller
    menubar_ctrl = None
    time.sleep(2)
    while menubar_ctrl is None:
        time.sleep(duration_wait_for_gui)
        menubar_ctrl = rafcon.gui.singleton.main_window_controller.get_controller('menu_bar_controller') if \
            rafcon.gui.singleton.main_window_controller else None

    # quit rafcon by menu bar
    testing_utils.call_gui_callback(menubar_ctrl.on_save_as_activate, None,
                                    None, testing_utils.get_unique_temp_path())
    testing_utils.call_gui_callback(menubar_ctrl.on_stop_activate, None)
    testing_utils.call_gui_callback(menubar_ctrl.on_quit_activate, None)
Пример #10
0
def trigger_issue_586_reproduction_sequence():
    from os.path import join
    import rafcon.core.singleton
    import rafcon.gui.singleton
    sm_manager_model = rafcon.gui.singleton.state_machine_manager_model
    main_window_controller = rafcon.gui.singleton.main_window_controller
    menubar_ctrl = main_window_controller.get_controller('menu_bar_controller')
    current_sm_length = len(sm_manager_model.state_machines)
    assert current_sm_length == 0

    call_gui_callback(
        menubar_ctrl.on_open_activate, None, None,
        join(testing_utils.TEST_ASSETS_PATH, "unit_test_state_machines",
             "backward_step_barrier_test"))
    sm_m = sm_manager_model.state_machines.values()[0]
    assert sm_m.state_machine_id == sm_manager_model.selected_state_machine_id
    concurrent_decimate_state_m = sm_m.get_state_model_by_path("GLSUJY/OOECFM")

    # check start conditions overlapping ids
    state_ids = concurrent_decimate_state_m.states.keys()
    import rafcon.core.constants
    state_ids.remove(rafcon.core.constants.UNIQUE_DECIDER_STATE_ID)
    child_state_ids = concurrent_decimate_state_m.states.values(
    )[0].states.keys()
    for state_id in state_ids:
        assert all([
            child_id in child_state_ids for child_id in
            concurrent_decimate_state_m.states[state_id].states.keys()
        ])

    call_gui_callback(sm_m.selection.set, concurrent_decimate_state_m)
    call_gui_callback(menubar_ctrl.on_ungroup_state_activate, None, None)
    testing_utils.wait_for_gui()

    # ungroup all three child states which all have the same state ids as there child states plus data flows
    for state_id in state_ids:
        print "ungroup state:", state_id
        assert state_id in sm_m.root_state.states
        child_state_m = sm_m.get_state_model_by_path("GLSUJY/" + state_id)
        call_gui_callback(sm_m.selection.set, child_state_m)
        call_gui_callback(menubar_ctrl.on_ungroup_state_activate, None, None)

    # store and refresh selected
    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())
    call_gui_callback(menubar_ctrl.on_refresh_selected_activate, None, None)
Пример #11
0
def trigger_ungroup_signals():
    import rafcon.core.singleton
    import rafcon.gui.singleton as gui_singleton
    import rafcon.gui.helpers.state as gui_helper_state
    import rafcon.gui.helpers.state_machine as gui_helper_state_machine

    sm_manager_model = rafcon.gui.singleton.state_machine_manager_model
    main_window_controller = gui_singleton.main_window_controller
    menubar_ctrl = main_window_controller.get_controller('menu_bar_controller')

    state_machine = create_state_machine()
    first_sm_id = state_machine.state_machine_id
    call_gui_callback(
        rafcon.core.singleton.state_machine_manager.add_state_machine,
        state_machine)
    call_gui_callback(rafcon.core.singleton.state_machine_manager.__setattr__,
                      "active_state_machine_id", first_sm_id)

    call_gui_callback(main_window_controller.view['main_window'].grab_focus)
    call_gui_callback(sm_manager_model.__setattr__,
                      "selected_state_machine_id", first_sm_id)

    state_machines_ctrl = main_window_controller.get_controller(
        'state_machines_editor_ctrl')
    page_id = state_machines_ctrl.get_page_num(first_sm_id)
    page = state_machines_ctrl.view.notebook.get_nth_page(page_id)
    call_gui_callback(focus_graphical_editor_in_page, page)
    sm_m = sm_manager_model.get_selected_state_machine_model()
    assert sm_m
    call_gui_callback(gui_helper_state.change_state_type,
                      sm_m.get_state_model_by_path("ROOTSTATE/STATE3"),
                      gui_helper_state.BarrierConcurrencyState)

    call_gui_callback(sm_m.selection.set,
                      sm_m.get_state_model_by_path("ROOTSTATE/STATE3"))
    call_gui_callback(gui_helper_state_machine.ungroup_selected_state)

    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())
    call_gui_callback(menubar_ctrl.on_stop_activate, None)
def test_concurrency_preemption_save_load(caplog):
    testing_utils.test_multithreading_lock.acquire()

    storage_path = testing_utils.get_unique_temp_path()

    preemption_state_sm = create_preemption_state_machine()

    storage.save_state_machine_to_path(preemption_state_sm, storage_path)
    storage.load_state_machine_from_path(storage_path)

    rafcon.core.singleton.state_machine_manager.add_state_machine(
        preemption_state_sm)
    rafcon.core.singleton.state_machine_execution_engine.start(
        preemption_state_sm.state_machine_id)
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "preempted_state2_code") == "DF3LFXD34G"
        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            preemption_state_sm.state_machine_id)
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()
Пример #13
0
def test_transition_creation(caplog):

    storage_path = testing_utils.get_unique_temp_path()

    sm = create_state_machine()

    storage.save_state_machine_to_path(sm, storage_path)
    sm_loaded = storage.load_state_machine_from_path(storage_path)

    root_state = sm_loaded.root_state

    state_machine = StateMachine(root_state)
    testing_utils.test_multithreading_lock.acquire()
    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)
    rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
    rafcon.core.singleton.state_machine_execution_engine.start()
    rafcon.core.singleton.state_machine_execution_engine.join()
    try:
        testing_utils.assert_logger_warnings_and_errors(caplog)
    finally:
        testing_utils.test_multithreading_lock.release()

    rafcon.core.singleton.state_machine_manager.delete_all_state_machines()
Пример #14
0
def test_execution_log(caplog):
    try:
        testing_utils.initialize_environment_core(
            core_config={
                'EXECUTION_LOG_ENABLE':
                True,
                'EXECUTION_LOG_PATH':
                testing_utils.get_unique_temp_path() + '/test_execution_log'
            })

        state_machine = global_storage.load_state_machine_from_path(
            testing_utils.get_test_sm_path(
                os.path.join("unit_test_state_machines",
                             "execution_file_log_test")))

        rafcon.core.singleton.state_machine_manager.add_state_machine(
            state_machine)
        rafcon.core.singleton.state_machine_manager.active_state_machine_id = state_machine.state_machine_id
        rafcon.core.singleton.state_machine_execution_engine.start()
        rafcon.core.singleton.state_machine_execution_engine.join()

        import shelve
        import json
        ss = shelve.open(state_machine.get_last_execution_log_filename())

        assert len(ss) == 36

        start, next, concurrent, hierarchy, collapsed_items = log_helper.log_to_collapsed_structure(
            ss)

        prod1_id = [
            k for k, v in collapsed_items.items()
            if v['state_name'] == 'MakeProd1'
        ][0]
        prod1 = collapsed_items[prod1_id]
        assert prod1['scoped_data_ins']['product'] == 2
        assert prod1['scoped_data_outs']['product'] == 2
        assert prod1['outcome_name'] == 'success'

        prod2_id = [
            k for k, v in collapsed_items.items()
            if v['state_name'] == 'MakeProd2'
        ][0]
        prod2 = collapsed_items[prod2_id]
        assert prod2['data_ins']['input_1'] == 0
        assert prod2['data_outs']['output_1'] == 3
        assert prod2['scoped_data_ins']['product'] == 1
        assert prod2['scoped_data_outs']['product'] == 1

        start_states = [
            k for k, v in collapsed_items.items() if v['state_name'] == 'Start'
            and v['state_type'] == 'ExecutionState'
        ]
        assert len(
            start_states
        ) == 3  # Start state is executed three times until state machine is done
        start_id = start_states[0]
        start_item = collapsed_items[start_id]
        assert 'Starts the factory' in start_item['description']

        df = log_helper.log_to_DataFrame(ss)
        all_starts = df.groupby('state_name').get_group('Start')
        assert len(all_starts) == 3
        assert list(
            all_starts['outcome_name']) == ['success', 'success', 'done']

        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            state_machine.state_machine_id)
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog,
                                                     expected_warnings=0,
                                                     expected_errors=0)
Пример #15
0
def trigger_gui_signals(*args):
    """The function triggers and test basic functions of the menu bar.

    At the moment those functions are TESTED, SHOULD or are THOUGHT about:
    - TESTED menu-bar: New State Machine and save state machine (check in list and in menu) -> after save list updated
    - TESTED menu-bar: Open State Machine no library (check in list and in menu)
    - TESTED lib-tree: Open library State Machine (check in list and in menu)
    - THOUGHT sub-dialog-lib-tree: sub with library State Machine (check in list and in menu) - no update
    - THOUGHT sub-dialog-lib-tree: sub with library State Machine as template (check in list and in menu) - no update
    - SHOULD menu-bar: Save state machine with existing state machine no changes (check in list and in menu) - with update
    - TESTED menu-bar: Save changed state machine with existing state machine (check in list and in menu)
    - menu-bar: Save state machine as with existing state machine (check in list and in menu)
    - THOUGHT tool-bar: Save state machine with existing state machine no changes (check in list and in menu) - with update
    - THOUGHT tool-bar: Save changed state machine with existing state machine (check in list and in menu)
    - TESTED tool-bar: Save state machine as with existing state machine (check in list and in menu)
    - THOUGHT right-click menu: Save state as (check in list and in menu)
    - TESTED auto backup: recover state machine from backup functionality (check in list and in menu) - no update
    - TESTED menu bar: open state machine by recent opened sub-menu
    - TESTED menu bar: try to open state machine that is not there by recent opened sub-menu without fatal failure
    - THOUGHT menu-bar: refresh
    - THOUGHT tool-bar: refresh
    - THOUGHT tool-bar: refresh selected
    """
    # gui elements
    import rafcon.gui.singleton
    from rafcon.gui.controllers.main_window import MenuBarController
    from rafcon.gui.models.state_machine_manager import StateMachineManagerModel
    import rafcon.gui.helpers.state_machine as gui_helper_state_machine
    import rafcon.core.config
    from rafcon.core.states.library_state import LibraryState

    print "WT: ", threading.currentThread()
    sm_manager_model = rafcon.gui.singleton.state_machine_manager_model
    main_window_controller = rafcon.gui.singleton.main_window_controller
    menubar_ctrl = main_window_controller.get_controller('menu_bar_controller')
    global_runtime_config = rafcon.gui.singleton.global_runtime_config
    call_gui_callback(
        rafcon.core.singleton.state_machine_manager.add_state_machine,
        create_state_machine())
    assert isinstance(menubar_ctrl, MenuBarController)
    assert isinstance(sm_manager_model, StateMachineManagerModel)

    ####################
    # POSITIVE EXAMPLES -> supposed to be added to the recently opened state machines list
    ####################

    # menu-bar: New State Machine and save state machine (check in list and in menu) -> after save list updated
    call_gui_callback(testing_utils.wait_for_gui)
    current_sm_length = len(sm_manager_model.state_machines)
    first_sm_id = sm_manager_model.state_machines.keys()[0]
    call_gui_callback(menubar_ctrl.on_new_activate, None)
    call_gui_callback(sm_manager_model.__setattr__,
                      "selected_state_machine_id", first_sm_id)

    call_gui_callback(testing_utils.wait_for_gui)
    assert len(sm_manager_model.state_machines) == current_sm_length + 1

    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert sm_manager_model.state_machines[
        first_sm_id].state_machine.file_system_path == recently_opened_state_machines_paths[
            0]
    check_order_and_consistency_of_menu(menubar_ctrl)
    call_gui_callback(sm_manager_model.__setattr__,
                      "selected_state_machine_id", first_sm_id + 1)

    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())
    call_gui_callback(testing_utils.wait_for_gui)
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert sm_manager_model.state_machines[
        first_sm_id +
        1].state_machine.file_system_path == recently_opened_state_machines_paths[
            0]

    # menu-bar: Open State Machine no library (check in list and in menu)
    basic_turtle_sm_path = join(testing_utils.TUTORIAL_PATH,
                                "basic_turtle_demo_sm")
    call_gui_callback(menubar_ctrl.on_open_activate, None, None,
                      basic_turtle_sm_path)
    call_gui_callback(testing_utils.wait_for_gui)
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert basic_turtle_sm_path == recently_opened_state_machines_paths[0]
    check_order_and_consistency_of_menu(menubar_ctrl)

    # menu-bar: Open State Machine no library and re-save it somewhere (check in list and in menu)
    turtle_state_machine_m = sm_manager_model.get_selected_state_machine_model(
    )
    assert turtle_state_machine_m.state_machine.file_system_path == basic_turtle_sm_path

    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert turtle_state_machine_m.state_machine.file_system_path == recently_opened_state_machines_paths[
        0]
    check_order_and_consistency_of_menu(menubar_ctrl)

    # lib-tree: Open library State Machine (check in list and
    library_path = join("generic", "dialog")
    library_name = "Dialog [3 options]"
    library_os_path = rafcon.gui.singleton.library_manager.get_os_path_to_library(
        library_path, library_name)[0]
    call_gui_callback(menubar_ctrl.on_open_activate, None, None,
                      library_os_path)
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    call_gui_callback(testing_utils.wait_for_gui)
    lib_sm_m = sm_manager_model.get_selected_state_machine_model()
    assert library_os_path == recently_opened_state_machines_paths[0]
    assert lib_sm_m.state_machine.file_system_path == library_os_path
    check_order_and_consistency_of_menu(menubar_ctrl)

    call_gui_callback(sm_manager_model.__setattr__,
                      "selected_state_machine_id", first_sm_id)
    call_gui_callback(
        sm_manager_model.get_selected_state_machine_model().selection.set,
        sm_manager_model.get_selected_state_machine_model().root_state)
    call_gui_callback(menubar_ctrl.on_add_state_activate, None, None)
    call_gui_callback(menubar_ctrl.on_save_activate, None, None)
    assert sm_manager_model.state_machines[
        first_sm_id].state_machine.file_system_path == recently_opened_state_machines_paths[
            0]
    assert library_os_path == recently_opened_state_machines_paths[1]
    check_order_and_consistency_of_menu(menubar_ctrl)

    # open state machine by recent opened sub-menu in menu bar
    call_gui_callback(
        global_runtime_config.update_recently_opened_state_machines_with,
        sm_manager_model.state_machines[first_sm_id].state_machine)
    first_sm_path = sm_manager_model.state_machines[
        first_sm_id].state_machine.file_system_path
    call_gui_callback(testing_utils.wait_for_gui)
    assert first_sm_path in menubar_ctrl.view.sub_menu_open_recently.get_children(
    )[2].get_label()
    call_gui_callback(
        sm_manager_model.state_machine_manager.remove_state_machine,
        first_sm_id)
    call_gui_callback(
        menubar_ctrl.view.sub_menu_open_recently.get_children()[2].activate)
    call_gui_callback(testing_utils.wait_for_gui)
    reopen_first_sm_id = sm_manager_model.selected_state_machine_id
    assert sm_manager_model.state_machines[
        reopen_first_sm_id].state_machine.file_system_path == first_sm_path
    check_order_and_consistency_of_menu(menubar_ctrl)
    # clean check after every update and re-save of library (both paths are in -> after enforcing by marked dirty flag)
    # TODO think about to remove this enforcement by marked dirty flag
    # change name to set marked dirty flag and enforce update of recent opened
    call_gui_callback(lib_sm_m.state_machine.root_state.__setattr__, "name",
                      "Different")
    shutil.rmtree(sm_manager_model.state_machines[reopen_first_sm_id].
                  state_machine.file_system_path)
    call_gui_callback(sm_manager_model.__setattr__,
                      "selected_state_machine_id",
                      lib_sm_m.state_machine.state_machine_id)
    call_gui_callback(testing_utils.wait_for_gui)
    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())
    print recently_opened_state_machines_paths
    assert lib_sm_m.state_machine.file_system_path == recently_opened_state_machines_paths[
        0]
    print recently_opened_state_machines_paths, library_os_path, sm_manager_model.state_machines[
        reopen_first_sm_id].state_machine.file_system_path
    assert sm_manager_model.state_machines[
        reopen_first_sm_id].state_machine.file_system_path == recently_opened_state_machines_paths[
            1]
    call_gui_callback(
        global_runtime_config.clean_recently_opened_state_machines)
    assert not sm_manager_model.state_machines[
        reopen_first_sm_id].state_machine.file_system_path == recently_opened_state_machines_paths[
            1]
    assert library_os_path == recently_opened_state_machines_paths[1]
    check_order_and_consistency_of_menu(menubar_ctrl)

    ####################
    # NEGATIVE EXAMPLES -> supposed to not been added to the recently opened state machines list
    ####################
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')

    # if a LibraryState is created and insert no change should be in the recently opened state machine list
    call_gui_callback(menubar_ctrl.on_new_activate, None)
    lib_state = LibraryState(join("generic", "dialog"), "Dialog [3 options]",
                             "0.1", "Dialog [3 options]")
    call_gui_callback(
        gui_helper_state_machine.insert_state_into_selected_state, lib_state,
        True)
    assert recently_opened_state_machines_paths == global_runtime_config.get_config_value(
        'recently_opened_state_machines')

    # try to open state machine that is not there -> no fatal failure
    print "OPEN FAILURE CASE"
    call_gui_callback(
        global_runtime_config.update_recently_opened_state_machines_with,
        lib_sm_m.state_machine)
    lib_sm_path = lib_sm_m.state_machine.file_system_path
    shutil.rmtree(lib_sm_m.state_machine.file_system_path)
    lib_sm_before_remove = lib_sm_m.state_machine
    call_gui_callback(
        sm_manager_model.state_machine_manager.remove_state_machine,
        lib_sm_m.state_machine.state_machine_id)
    call_gui_callback(testing_utils.wait_for_gui)
    call_gui_callback(
        menubar_ctrl.view.sub_menu_open_recently.get_children()[2].activate)
    # was not open
    selected_sm_id = sm_manager_model.selected_state_machine_id
    assert not sm_manager_model.state_machines[
        selected_sm_id].state_machine.file_system_path == lib_sm_path
    # is still in and after clean removed
    assert lib_sm_path in menubar_ctrl.view.sub_menu_open_recently.get_children(
    )[2].get_label()
    call_gui_callback(
        global_runtime_config.clean_recently_opened_state_machines)
    assert lib_sm_path not in menubar_ctrl.view.sub_menu_open_recently.get_children(
    )[2].get_label()
    call_gui_callback(
        global_runtime_config.update_recently_opened_state_machines_with,
        lib_sm_before_remove)
    assert 'NOT_ACCESSIBLE' in menubar_ctrl.view.sub_menu_open_recently.get_children(
    )[2].get_label()

    # TODO maybe finally move this into the auto-backup or restore test module
    print "AUTO BACKUP TEST"
    number_of_open_sm = len(sm_manager_model.state_machines)
    backup_path = sm_manager_model.state_machines[
        reopen_first_sm_id].auto_backup.meta['last_backup']['file_system_path']
    from rafcon.gui.models import auto_backup
    call_gui_callback(auto_backup.recover_state_machine_from_backup,
                      backup_path)
    assert recently_opened_state_machines_paths == global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert number_of_open_sm == len(sm_manager_model.state_machines)
    call_gui_callback(
        sm_manager_model.state_machine_manager.remove_state_machine,
        reopen_first_sm_id)
    call_gui_callback(testing_utils.wait_for_gui)
    assert recently_opened_state_machines_paths == global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert number_of_open_sm == len(sm_manager_model.state_machines) + 1
    call_gui_callback(auto_backup.recover_state_machine_from_backup,
                      backup_path, None, None, True)
    assert recently_opened_state_machines_paths == global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert number_of_open_sm == len(sm_manager_model.state_machines)
    check_order_and_consistency_of_menu(menubar_ctrl)
Пример #16
0
def trigger_gui_signals_first_run(*args):
    """The function triggers the creation of different state machines that should be backup-ed.
    In another run those are restored and checked onto correctness.

    At the moment TESTED, SHOULD or are THOUGHT about to generate the following state machines:
    - TESTED new state machine without storage
    - TESTED new state machine with storage and no changes
    - TESTED new state machine with storage and changes
    - TESTED state machine loaded and no changes
    - TESTED state machine loaded and changes
    - TESTED library not changed
    - TESTED library changed
    - TESTED change tab position
    - SHOULD not stored state machine that was removed/moved before restart
    - SHOULD stored state machine and no changes that was removed/moved before restart
    - SHOULD stored state machine and no changes that was removed/moved before restart
    """
    import rafcon.gui.singleton
    from rafcon.core.states.hierarchy_state import HierarchyState
    from rafcon.gui.controllers.main_window import MenuBarController
    from rafcon.gui.models.state_machine_manager import StateMachineManagerModel
    from gui.widget.test_state_type_change import get_state_editor_ctrl_and_store_id_dict

    testing_utils.wait_for_gui()
    main_window_controller = rafcon.gui.singleton.main_window_controller
    sm_manager_model = rafcon.gui.singleton.state_machine_manager_model
    open_state_machines = args[0]
    library_manager = rafcon.gui.singleton.library_manager
    menubar_ctrl = main_window_controller.get_controller('menu_bar_controller')
    assert isinstance(menubar_ctrl, MenuBarController)
    assert isinstance(sm_manager_model, StateMachineManagerModel)

    def add_two_states_to_root_state_of_selected_state_machine():
        sm_m = sm_manager_model.get_selected_state_machine_model()
        current_number_states = len(sm_m.root_state.states)
        call_gui_callback(sm_m.selection.set, sm_m.root_state)
        call_gui_callback(menubar_ctrl.on_add_state_activate, None)
        call_gui_callback(menubar_ctrl.on_add_state_activate, None)
        assert len(sm_m.root_state.states) == current_number_states + 2
        assert sm_manager_model.get_selected_state_machine_model(
        ).state_machine.marked_dirty

    ####################
    # POSITIVE EXAMPLES -> supposed to be added to the open tabs list
    ####################

    # new state machine without storage
    state_machine = create_state_machine()
    call_gui_callback(sm_manager_model.state_machine_manager.add_state_machine,
                      state_machine)
    call_gui_callback(testing_utils.wait_for_gui)
    print(sm_manager_model.state_machines.keys())
    current_sm_id = list(sm_manager_model.state_machines.keys())[0]
    current_number_of_sm = len(sm_manager_model.state_machines)

    # new state machine with storage and no changes
    current_number_of_sm += 1
    current_sm_id += 1
    call_gui_callback(menubar_ctrl.on_new_activate, None)
    call_gui_callback(sm_manager_model.__setattr__,
                      'selected_state_machine_id', current_sm_id)
    assert len(sm_manager_model.state_machines) == current_number_of_sm
    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())

    # new state machine with storage and with changes
    current_number_of_sm += 1
    current_sm_id += 1
    call_gui_callback(menubar_ctrl.on_new_activate, None)
    call_gui_callback(sm_manager_model.__setattr__,
                      'selected_state_machine_id', current_sm_id)
    assert len(sm_manager_model.state_machines) == current_number_of_sm
    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())
    add_two_states_to_root_state_of_selected_state_machine()

    # state machine loaded and no changes
    current_number_of_sm += 1
    current_sm_id += 1
    basic_turtle_sm_path = join(testing_utils.TUTORIAL_PATH,
                                "basic_turtle_demo_sm")
    call_gui_callback(menubar_ctrl.on_open_activate, None, None,
                      basic_turtle_sm_path)
    call_gui_callback(sm_manager_model.__setattr__,
                      'selected_state_machine_id', current_sm_id)
    move_this_sm_id = sm_manager_model.selected_state_machine_id
    assert len(sm_manager_model.state_machines) == current_number_of_sm

    # state machine loaded and changes
    current_number_of_sm += 1
    current_sm_id += 1
    print("BUGS")
    basic_turtle_sm_path = join(testing_utils.TUTORIAL_PATH, "99_bugs")
    call_gui_callback(menubar_ctrl.on_open_activate, None, None,
                      basic_turtle_sm_path)
    call_gui_callback(testing_utils.wait_for_gui)
    assert len(sm_manager_model.state_machines) == current_number_of_sm
    assert sm_manager_model.get_selected_state_machine_model(
    ).state_machine.file_system_path == basic_turtle_sm_path
    add_two_states_to_root_state_of_selected_state_machine()

    # library not changed (needs state machine that has meta data already -> that should not be changed by opening)
    print("LIB no changes")
    library_os_path = library_manager.get_os_path_to_library(
        "turtle_libraries", "clear_field")[0]
    call_gui_callback(menubar_ctrl.on_open_activate, None, None,
                      library_os_path)
    call_gui_callback(testing_utils.wait_for_gui)
    # use artificial marked dirty to check for recovery of the flag
    assert not sm_manager_model.get_selected_state_machine_model(
    ).state_machine.marked_dirty
    sm_manager_model.get_selected_state_machine_model(
    ).state_machine._marked_dirty = True
    assert sm_manager_model.get_selected_state_machine_model(
    ).state_machine.marked_dirty

    # library with changes
    print("LIB with changes")
    library_os_path = library_manager.get_os_path_to_library(
        "turtle_libraries", "teleport_turtle")[0]
    call_gui_callback(menubar_ctrl.on_open_activate, None, None,
                      library_os_path)
    call_gui_callback(testing_utils.wait_for_gui)
    lib_sm_m = sm_manager_model.get_selected_state_machine_model()

    def do_type_change():
        [state_editor_ctrl, list_store_id_from_state_type_dict] = \
            get_state_editor_ctrl_and_store_id_dict(lib_sm_m, lib_sm_m.root_state, main_window_controller, 5., logger)
        # - do state type change
        state_type_row_id = list_store_id_from_state_type_dict[
            HierarchyState.__name__]
        state_editor_ctrl.get_controller('properties_ctrl').view[
            'type_combobox'].set_active(state_type_row_id)

    call_gui_callback(lib_sm_m.selection.set, [lib_sm_m.root_state])
    print(lib_sm_m.root_state)
    call_gui_callback(do_type_change)
    print(lib_sm_m.root_state)
    add_two_states_to_root_state_of_selected_state_machine()
    print(lib_sm_m.root_state)

    # change tab position
    state_machines_editor_ctrl = main_window_controller.get_controller(
        'state_machines_editor_ctrl')
    call_gui_callback(state_machines_editor_ctrl.rearrange_state_machines,
                      {move_this_sm_id: 0})

    # defined selection
    sm_m = sm_manager_model.state_machines[move_this_sm_id]
    call_gui_callback(sm_manager_model.__setattr__,
                      'selected_state_machine_id', move_this_sm_id)
    call_gui_callback(sm_m.selection.set,
                      list(sm_m.root_state.states.values())[0])
    print("last state machine:", sm_m.state_machine.file_system_path)

    ####################
    # NEGATIVE EXAMPLES -> supposed to not been added to the recently opened state machines list
    ####################

    # state machine that was removed/moved before restart -> result in second run

    ####################
    # collect open state machine data
    ####################
    call_gui_callback(prepare_tab_data_of_open_state_machines,
                      main_window_controller, sm_manager_model,
                      open_state_machines)

    ####################
    # shout down gui
    ####################
    call_gui_callback(
        menubar_ctrl.on_stop_activate,
        None)  # TODO why this is some how important for correct restore