FreeCAD
650 строк · 30.1 Кб
1# SPDX-License-Identifier: LGPL-2.1-or-later
2# ***************************************************************************
3# * *
4# * Copyright (c) 2022 FreeCAD Project Association *
5# * *
6# * This file is part of FreeCAD. *
7# * *
8# * FreeCAD is free software: you can redistribute it and/or modify it *
9# * under the terms of the GNU Lesser General Public License as *
10# * published by the Free Software Foundation, either version 2.1 of the *
11# * License, or (at your option) any later version. *
12# * *
13# * FreeCAD is distributed in the hope that it will be useful, but *
14# * WITHOUT ANY WARRANTY; without even the implied warranty of *
15# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16# * Lesser General Public License for more details. *
17# * *
18# * You should have received a copy of the GNU Lesser General Public *
19# * License along with FreeCAD. If not, see *
20# * <https://www.gnu.org/licenses/>. *
21# * *
22# ***************************************************************************
23
24import os25import tempfile26import unittest27import FreeCAD28
29from PySide import QtCore, QtWidgets30
31from addonmanager_installer_gui import AddonInstallerGUI, MacroInstallerGUI32
33from AddonManagerTest.gui.gui_mocks import DialogWatcher, DialogInteractor34from AddonManagerTest.app.mocks import MockAddon35
36translate = FreeCAD.Qt.translate37
38
39class TestInstallerGui(unittest.TestCase):40
41MODULE = "test_installer_gui" # file name without extension42
43def setUp(self):44self.addon_to_install = MockAddon()45self.installer_gui = AddonInstallerGUI(self.addon_to_install)46self.finalized_thread = False47
48def tearDown(self):49pass50
51def test_success_dialog(self):52# Pop the modal dialog and verify that it opens, and responds to an OK click53dialog_watcher = DialogWatcher(54translate("AddonsInstaller", "Success"),55QtWidgets.QDialogButtonBox.Ok,56)57self.installer_gui._installation_succeeded()58self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")59self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")60
61def test_failure_dialog(self):62# Pop the modal dialog and verify that it opens, and responds to a Cancel click63dialog_watcher = DialogWatcher(64translate("AddonsInstaller", "Installation Failed"),65QtWidgets.QDialogButtonBox.Cancel,66)67self.installer_gui._installation_failed(68self.addon_to_install, "Test of installation failure"69)70self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")71self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")72
73def test_no_python_dialog(self):74# Pop the modal dialog and verify that it opens, and responds to a No click75dialog_watcher = DialogWatcher(76translate("AddonsInstaller", "Cannot execute Python"),77QtWidgets.QDialogButtonBox.No,78)79self.installer_gui._report_no_python_exe()80self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")81self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")82
83def test_no_pip_dialog(self):84# Pop the modal dialog and verify that it opens, and responds to a No click85dialog_watcher = DialogWatcher(86translate("AddonsInstaller", "Cannot execute pip"),87QtWidgets.QDialogButtonBox.No,88)89self.installer_gui._report_no_pip("pip not actually run, this was a test")90self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")91self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")92
93def test_dependency_failure_dialog(self):94# Pop the modal dialog and verify that it opens, and responds to a No click95dialog_watcher = DialogWatcher(96translate("AddonsInstaller", "Package installation failed"),97QtWidgets.QDialogButtonBox.No,98)99self.installer_gui._report_dependency_failure(100"Unit test", "Nothing really failed, this is a test of the dialog box"101)102self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")103self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")104
105def test_install(self):106# Run the installation code and make sure it puts the directory in place107with tempfile.TemporaryDirectory() as temp_dir:108self.installer_gui.installer.installation_path = temp_dir109self.installer_gui.install() # This does not block110self.installer_gui.installer.success.disconnect(111self.installer_gui._installation_succeeded112)113self.installer_gui.installer.failure.disconnect(self.installer_gui._installation_failed)114while not self.installer_gui.worker_thread.isFinished():115QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 100)116self.assertTrue(117os.path.exists(os.path.join(temp_dir, "MockAddon")),118"Installed directory not found",119)120
121def test_handle_disallowed_python(self):122disallowed_packages = ["disallowed_package_name"]123dialog_watcher = DialogWatcher(124translate("AddonsInstaller", "Missing Requirement"),125QtWidgets.QDialogButtonBox.Cancel,126)127self.installer_gui._handle_disallowed_python(disallowed_packages)128self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")129self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")130
131def test_handle_disallowed_python_long_list(self):132"""A separate test for when there are MANY packages, which takes a separate code path."""133disallowed_packages = []134for i in range(50):135disallowed_packages.append(f"disallowed_package_name_{i}")136dialog_watcher = DialogWatcher(137translate("AddonsInstaller", "Missing Requirement"),138QtWidgets.QDialogButtonBox.Cancel,139)140self.installer_gui._handle_disallowed_python(disallowed_packages)141self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")142self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")143
144def test_report_missing_workbenches_single(self):145"""Test only missing one workbench"""146wbs = ["OneMissingWorkbench"]147dialog_watcher = DialogWatcher(148translate("AddonsInstaller", "Missing Requirement"),149QtWidgets.QDialogButtonBox.Cancel,150)151self.installer_gui._report_missing_workbenches(wbs)152self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")153self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")154
155def test_report_missing_workbenches_multiple(self):156"""Test only missing one workbench"""157wbs = ["FirstMissingWorkbench", "SecondMissingWorkbench"]158dialog_watcher = DialogWatcher(159translate("AddonsInstaller", "Missing Requirement"),160QtWidgets.QDialogButtonBox.Cancel,161)162self.installer_gui._report_missing_workbenches(wbs)163self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")164self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")165
166def test_resolve_dependencies_then_install(self):167class MissingDependenciesMock:168def __init__(self):169self.external_addons = ["addon_1", "addon_2"]170self.python_requires = ["py_req_1", "py_req_2"]171self.python_optional = ["py_opt_1", "py_opt_2"]172
173missing = MissingDependenciesMock()174dialog_watcher = DialogWatcher(175translate("DependencyResolutionDialog", "Resolve Dependencies"),176QtWidgets.QDialogButtonBox.Cancel,177)178self.installer_gui._resolve_dependencies_then_install(missing)179self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")180self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")181
182def test_check_python_version_bad(self):183class MissingDependenciesMock:184def __init__(self):185self.python_min_version = {"major": 3, "minor": 9999}186
187missing = MissingDependenciesMock()188dialog_watcher = DialogWatcher(189translate("AddonsInstaller", "Incompatible Python version"),190QtWidgets.QDialogButtonBox.Cancel,191)192stop_installing = self.installer_gui._check_python_version(missing)193self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")194self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")195self.assertTrue(stop_installing, "Failed to halt installation on bad Python version")196
197def test_check_python_version_good(self):198class MissingDependenciesMock:199def __init__(self):200self.python_min_version = {"major": 3, "minor": 0}201
202missing = MissingDependenciesMock()203stop_installing = self.installer_gui._check_python_version(missing)204self.assertFalse(stop_installing, "Failed to continue installation on good Python version")205
206def test_clean_up_optional(self):207class MissingDependenciesMock:208def __init__(self):209self.python_optional = [210"allowed_packages_1",211"allowed_packages_2",212"disallowed_package",213]214
215allowed_packages = ["allowed_packages_1", "allowed_packages_2"]216missing = MissingDependenciesMock()217self.installer_gui.installer.allowed_packages = set(allowed_packages)218self.installer_gui._clean_up_optional(missing)219self.assertTrue("allowed_packages_1" in missing.python_optional)220self.assertTrue("allowed_packages_2" in missing.python_optional)221self.assertFalse("disallowed_package" in missing.python_optional)222
223def intercept_run_dependency_installer(self, addons, python_requires, python_optional):224self.assertEqual(python_requires, ["py_req_1", "py_req_2"])225self.assertEqual(python_optional, ["py_opt_1", "py_opt_2"])226self.assertEqual(addons[0].name, "addon_1")227self.assertEqual(addons[1].name, "addon_2")228
229def test_dependency_dialog_yes_clicked(self):230class DialogMock:231class ListWidgetMock:232class ListWidgetItemMock:233def __init__(self, name):234self.name = name235
236def text(self):237return self.name238
239def checkState(self):240return QtCore.Qt.Checked241
242def __init__(self, items):243self.list = []244for item in items:245self.list.append(DialogMock.ListWidgetMock.ListWidgetItemMock(item))246
247def count(self):248return len(self.list)249
250def item(self, i):251return self.list[i]252
253def __init__(self):254self.listWidgetAddons = DialogMock.ListWidgetMock(["addon_1", "addon_2"])255self.listWidgetPythonRequired = DialogMock.ListWidgetMock(["py_req_1", "py_req_2"])256self.listWidgetPythonOptional = DialogMock.ListWidgetMock(["py_opt_1", "py_opt_2"])257
258class AddonMock:259def __init__(self, name):260self.name = name261
262self.installer_gui.dependency_dialog = DialogMock()263self.installer_gui.addons = [AddonMock("addon_1"), AddonMock("addon_2")]264self.installer_gui._run_dependency_installer = self.intercept_run_dependency_installer265self.installer_gui._dependency_dialog_yes_clicked()266
267
268class TestMacroInstallerGui(unittest.TestCase):269class MockMacroAddon:270class MockMacro:271def __init__(self):272self.install_called = False273self.install_result = (274True # External code can change to False to test failed install275)276self.name = "MockMacro"277self.filename = "mock_macro_no_real_file.FCMacro"278self.comment = "This is a mock macro for unit testing"279self.icon = None280self.xpm = None281
282def install(self):283self.install_called = True284return self.install_result285
286def __init__(self):287self.macro = TestMacroInstallerGui.MockMacroAddon.MockMacro()288self.name = self.macro.name289self.display_name = self.macro.name290
291class MockParameter:292"""Mock the parameter group to allow simplified behavior and introspection."""293
294def __init__(self):295self.params = {}296self.groups = {}297self.accessed_parameters = {} # Dict is param name: default value298
299types = ["Bool", "String", "Int", "UInt", "Float"]300for t in types:301setattr(self, f"Get{t}", self.get)302setattr(self, f"Set{t}", self.set)303setattr(self, f"Rem{t}", self.rem)304
305def get(self, p, default=None):306self.accessed_parameters[p] = default307if p in self.params:308return self.params[p]309else:310return default311
312def set(self, p, value):313self.params[p] = value314
315def rem(self, p):316if p in self.params:317self.params.erase(p)318
319def GetGroup(self, name):320if name not in self.groups:321self.groups[name] = TestMacroInstallerGui.MockParameter()322return self.groups[name]323
324def GetGroups(self):325return self.groups.keys()326
327class ToolbarIntercepter:328def __init__(self):329self.ask_for_toolbar_called = False330self.install_macro_to_toolbar_called = False331self.tb = None332self.custom_group = TestMacroInstallerGui.MockParameter()333self.custom_group.set("Name", "MockCustomToolbar")334
335def _ask_for_toolbar(self, _):336self.ask_for_toolbar_called = True337return self.custom_group338
339def _install_macro_to_toolbar(self, tb):340self.install_macro_to_toolbar_called = True341self.tb = tb342
343class InstallerInterceptor:344def __init__(self):345self.ccc_called = False346
347def _create_custom_command(348self,349toolbar,350filename,351menuText,352tooltipText,353whatsThisText,354statustipText,355pixmapText,356):357self.ccc_called = True358self.toolbar = toolbar359self.filename = filename360self.menuText = menuText361self.tooltipText = tooltipText362self.whatsThisText = whatsThisText363self.statustipText = statustipText364self.pixmapText = pixmapText365
366def setUp(self):367self.mock_macro = TestMacroInstallerGui.MockMacroAddon()368self.installer = MacroInstallerGUI(self.mock_macro)369self.installer.addon_params = TestMacroInstallerGui.MockParameter()370self.installer.toolbar_params = TestMacroInstallerGui.MockParameter()371
372def tearDown(self):373pass374
375def test_class_is_initialized(self):376"""Connecting to a signal does not throw"""377self.installer.finished.connect(lambda: None)378
379def test_ask_for_toolbar_no_dialog_default_exists(self):380self.installer.addon_params.set("alwaysAskForToolbar", False)381self.installer.addon_params.set("CustomToolbarName", "UnitTestCustomToolbar")382utct = self.installer.toolbar_params.GetGroup("UnitTestCustomToolbar")383utct.set("Name", "UnitTestCustomToolbar")384utct.set("Active", True)385result = self.installer._ask_for_toolbar([])386self.assertIsNotNone(result)387self.assertTrue(hasattr(result, "get"))388name = result.get("Name")389self.assertEqual(name, "UnitTestCustomToolbar")390
391def test_ask_for_toolbar_with_dialog_cancelled(self):392
393# First test: the user cancels the dialog394self.installer.addon_params.set("alwaysAskForToolbar", True)395dialog_watcher = DialogWatcher(396translate("select_toolbar_dialog", "Select Toolbar"),397QtWidgets.QDialogButtonBox.Cancel,398)399result = self.installer._ask_for_toolbar([])400self.assertIsNone(result)401
402def test_ask_for_toolbar_with_dialog_defaults(self):403
404# Second test: the user leaves the dialog at all default values, so:405# - The checkbox "Ask every time" is unchecked406# - The selected toolbar option is "Create new toolbar", which triggers a search for407# a new custom toolbar name by calling _create_new_custom_toolbar, which we mock.408fake_custom_toolbar_group = TestMacroInstallerGui.MockParameter()409fake_custom_toolbar_group.set("Name", "UnitTestCustomToolbar")410self.installer._create_new_custom_toolbar = lambda: fake_custom_toolbar_group411dialog_watcher = DialogWatcher(412translate("select_toolbar_dialog", "Select Toolbar"),413QtWidgets.QDialogButtonBox.Ok,414)415result = self.installer._ask_for_toolbar([])416self.assertIsNotNone(result)417self.assertTrue(hasattr(result, "get"))418name = result.get("Name")419self.assertEqual(name, "UnitTestCustomToolbar")420self.assertIn("alwaysAskForToolbar", self.installer.addon_params.params)421self.assertFalse(self.installer.addon_params.get("alwaysAskForToolbar", True))422self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")423
424def test_ask_for_toolbar_with_dialog_selection(self):425
426# Third test: the user selects a custom toolbar in the dialog, and checks the box to always427# ask.428dialog_interactor = DialogInteractor(429translate("select_toolbar_dialog", "Select Toolbar"),430self.interactor_selection_option_and_checkbox,431)432ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")433ut_tb_2 = self.installer.toolbar_params.GetGroup("UT_TB_2")434ut_tb_3 = self.installer.toolbar_params.GetGroup("UT_TB_3")435ut_tb_1.set("Name", "UT_TB_1")436ut_tb_2.set("Name", "UT_TB_2")437ut_tb_3.set("Name", "UT_TB_3")438result = self.installer._ask_for_toolbar(["UT_TB_1", "UT_TB_2", "UT_TB_3"])439self.assertIsNotNone(result)440self.assertTrue(hasattr(result, "get"))441name = result.get("Name")442self.assertEqual(name, "UT_TB_3")443self.assertIn("alwaysAskForToolbar", self.installer.addon_params.params)444self.assertTrue(self.installer.addon_params.get("alwaysAskForToolbar", False))445
446def interactor_selection_option_and_checkbox(self, parent):447
448boxes = parent.findChildren(QtWidgets.QComboBox)449self.assertEqual(len(boxes), 1) # Just to make sure...450box = boxes[0]451box.setCurrentIndex(box.count() - 2) # Select the last thing but one452
453checkboxes = parent.findChildren(QtWidgets.QCheckBox)454self.assertEqual(len(checkboxes), 1) # Just to make sure...455checkbox = checkboxes[0]456checkbox.setChecked(True)457
458parent.accept()459
460def test_macro_button_exists_no_command(self):461# Test 1: No command for this macro462self.installer._find_custom_command = lambda _: None463button_exists = self.installer._macro_button_exists()464self.assertFalse(button_exists)465
466def test_macro_button_exists_true(self):467# Test 2: Macro is in the list of buttons468ut_tb_1 = self.installer.toolbar_params.GetGroup("UnitTestCommand")469ut_tb_1.set("UnitTestCommand", "FreeCAD") # This is what the real thing looks like...470self.installer._find_custom_command = lambda _: "UnitTestCommand"471self.assertTrue(self.installer._macro_button_exists())472
473def test_macro_button_exists_false(self):474# Test 3: Macro is not in the list of buttons475self.installer._find_custom_command = lambda _: "UnitTestCommand"476self.assertFalse(self.installer._macro_button_exists())477
478def test_ask_to_install_toolbar_button_disabled(self):479self.installer.addon_params.SetBool("dontShowAddMacroButtonDialog", True)480self.installer._ask_to_install_toolbar_button()481# This should NOT block when dontShowAddMacroButtonDialog is True482
483def test_ask_to_install_toolbar_button_enabled_no(self):484self.installer.addon_params.SetBool("dontShowAddMacroButtonDialog", False)485dialog_watcher = DialogWatcher(486translate("toolbar_button", "Add button?"),487QtWidgets.QDialogButtonBox.No,488)489# Note: that dialog does not use a QButtonBox, so we can really only test its490# reject() signal, which is triggered by the DialogWatcher when it cannot find491# the button. In this case, failure to find that button is NOT an error.492self.installer._ask_to_install_toolbar_button() # Blocks until killed by watcher493self.assertTrue(dialog_watcher.dialog_found)494
495def test_get_toolbar_with_name_found(self):496ut_tb_1 = self.installer.toolbar_params.GetGroup("UnitTestToolbar")497ut_tb_1.set("Name", "Unit Test Toolbar")498ut_tb_1.set("UnitTestParam", True)499tb = self.installer._get_toolbar_with_name("Unit Test Toolbar")500self.assertIsNotNone(tb)501self.assertTrue(tb.get("UnitTestParam", False))502
503def test_get_toolbar_with_name_not_found(self):504ut_tb_1 = self.installer.toolbar_params.GetGroup("UnitTestToolbar")505ut_tb_1.set("Name", "Not the Unit Test Toolbar")506tb = self.installer._get_toolbar_with_name("Unit Test Toolbar")507self.assertIsNone(tb)508
509def test_create_new_custom_toolbar_no_existing(self):510tb = self.installer._create_new_custom_toolbar()511self.assertEqual(tb.get("Name", ""), "Auto-Created Macro Toolbar")512self.assertTrue(tb.get("Active", False), True)513
514def test_create_new_custom_toolbar_one_existing(self):515_ = self.installer._create_new_custom_toolbar()516tb = self.installer._create_new_custom_toolbar()517self.assertEqual(tb.get("Name", ""), "Auto-Created Macro Toolbar (2)")518self.assertTrue(tb.get("Active", False), True)519
520def test_check_for_toolbar_true(self):521ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")522ut_tb_1.set("Name", "UT_TB_1")523self.assertTrue(self.installer._check_for_toolbar("UT_TB_1"))524
525def test_check_for_toolbar_false(self):526ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")527ut_tb_1.set("Name", "UT_TB_1")528self.assertFalse(self.installer._check_for_toolbar("Not UT_TB_1"))529
530def test_install_toolbar_button_first_custom_toolbar(self):531tbi = TestMacroInstallerGui.ToolbarIntercepter()532self.installer._ask_for_toolbar = tbi._ask_for_toolbar533self.installer._install_macro_to_toolbar = tbi._install_macro_to_toolbar534self.installer._install_toolbar_button()535self.assertTrue(tbi.install_macro_to_toolbar_called)536self.assertFalse(tbi.ask_for_toolbar_called)537self.assertTrue("Custom_1" in self.installer.toolbar_params.GetGroups())538
539def test_install_toolbar_button_existing_custom_toolbar_1(self):540# There is an existing custom toolbar, and we should use it541tbi = TestMacroInstallerGui.ToolbarIntercepter()542self.installer._ask_for_toolbar = tbi._ask_for_toolbar543self.installer._install_macro_to_toolbar = tbi._install_macro_to_toolbar544ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")545ut_tb_1.set("Name", "UT_TB_1")546self.installer.addon_params.set("CustomToolbarName", "UT_TB_1")547self.installer._install_toolbar_button()548self.assertTrue(tbi.install_macro_to_toolbar_called)549self.assertFalse(tbi.ask_for_toolbar_called)550self.assertEqual(tbi.tb.get("Name", ""), "UT_TB_1")551
552def test_install_toolbar_button_existing_custom_toolbar_2(self):553# There are multiple existing custom toolbars, and we should use one of them554tbi = TestMacroInstallerGui.ToolbarIntercepter()555self.installer._ask_for_toolbar = tbi._ask_for_toolbar556self.installer._install_macro_to_toolbar = tbi._install_macro_to_toolbar557ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")558ut_tb_2 = self.installer.toolbar_params.GetGroup("UT_TB_2")559ut_tb_3 = self.installer.toolbar_params.GetGroup("UT_TB_3")560ut_tb_1.set("Name", "UT_TB_1")561ut_tb_2.set("Name", "UT_TB_2")562ut_tb_3.set("Name", "UT_TB_3")563self.installer.addon_params.set("CustomToolbarName", "UT_TB_3")564self.installer._install_toolbar_button()565self.assertTrue(tbi.install_macro_to_toolbar_called)566self.assertFalse(tbi.ask_for_toolbar_called)567self.assertEqual(tbi.tb.get("Name", ""), "UT_TB_3")568
569def test_install_toolbar_button_existing_custom_toolbar_3(self):570# There are multiple existing custom toolbars, but none of them match571tbi = TestMacroInstallerGui.ToolbarIntercepter()572self.installer._ask_for_toolbar = tbi._ask_for_toolbar573self.installer._install_macro_to_toolbar = tbi._install_macro_to_toolbar574ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")575ut_tb_2 = self.installer.toolbar_params.GetGroup("UT_TB_2")576ut_tb_3 = self.installer.toolbar_params.GetGroup("UT_TB_3")577ut_tb_1.set("Name", "UT_TB_1")578ut_tb_2.set("Name", "UT_TB_2")579ut_tb_3.set("Name", "UT_TB_3")580self.installer.addon_params.set("CustomToolbarName", "UT_TB_4")581self.installer._install_toolbar_button()582self.assertTrue(tbi.install_macro_to_toolbar_called)583self.assertTrue(tbi.ask_for_toolbar_called)584self.assertEqual(tbi.tb.get("Name", ""), "MockCustomToolbar")585
586def test_install_toolbar_button_existing_custom_toolbar_4(self):587# There are multiple existing custom toolbars, one of them matches, but we have set588# "alwaysAskForToolbar" to True589tbi = TestMacroInstallerGui.ToolbarIntercepter()590self.installer._ask_for_toolbar = tbi._ask_for_toolbar591self.installer._install_macro_to_toolbar = tbi._install_macro_to_toolbar592ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")593ut_tb_2 = self.installer.toolbar_params.GetGroup("UT_TB_2")594ut_tb_3 = self.installer.toolbar_params.GetGroup("UT_TB_3")595ut_tb_1.set("Name", "UT_TB_1")596ut_tb_2.set("Name", "UT_TB_2")597ut_tb_3.set("Name", "UT_TB_3")598self.installer.addon_params.set("CustomToolbarName", "UT_TB_3")599self.installer.addon_params.set("alwaysAskForToolbar", True)600self.installer._install_toolbar_button()601self.assertTrue(tbi.install_macro_to_toolbar_called)602self.assertTrue(tbi.ask_for_toolbar_called)603self.assertEqual(tbi.tb.get("Name", ""), "MockCustomToolbar")604
605def test_install_macro_to_toolbar_icon_abspath(self):606ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")607ut_tb_1.set("Name", "UT_TB_1")608ii = TestMacroInstallerGui.InstallerInterceptor()609self.installer._create_custom_command = ii._create_custom_command610with tempfile.NamedTemporaryFile() as ntf:611self.mock_macro.macro.icon = ntf.name612self.installer._install_macro_to_toolbar(ut_tb_1)613self.assertTrue(ii.ccc_called)614self.assertEqual(ii.pixmapText, ntf.name)615
616def test_install_macro_to_toolbar_icon_relpath(self):617ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")618ut_tb_1.set("Name", "UT_TB_1")619ii = TestMacroInstallerGui.InstallerInterceptor()620self.installer._create_custom_command = ii._create_custom_command621with tempfile.TemporaryDirectory() as td:622self.installer.macro_dir = td623self.mock_macro.macro.icon = "RelativeIconPath.png"624self.installer._install_macro_to_toolbar(ut_tb_1)625self.assertTrue(ii.ccc_called)626self.assertEqual(ii.pixmapText, os.path.join(td, "RelativeIconPath.png"))627
628def test_install_macro_to_toolbar_xpm(self):629ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")630ut_tb_1.set("Name", "UT_TB_1")631ii = TestMacroInstallerGui.InstallerInterceptor()632self.installer._create_custom_command = ii._create_custom_command633with tempfile.TemporaryDirectory() as td:634self.installer.macro_dir = td635self.mock_macro.macro.xpm = "Not really xpm data, don't try to use it!"636self.installer._install_macro_to_toolbar(ut_tb_1)637self.assertTrue(ii.ccc_called)638self.assertEqual(ii.pixmapText, os.path.join(td, "MockMacro_icon.xpm"))639self.assertTrue(os.path.exists(os.path.join(td, "MockMacro_icon.xpm")))640
641def test_install_macro_to_toolbar_no_icon(self):642ut_tb_1 = self.installer.toolbar_params.GetGroup("UT_TB_1")643ut_tb_1.set("Name", "UT_TB_1")644ii = TestMacroInstallerGui.InstallerInterceptor()645self.installer._create_custom_command = ii._create_custom_command646with tempfile.TemporaryDirectory() as td:647self.installer.macro_dir = td648self.installer._install_macro_to_toolbar(ut_tb_1)649self.assertTrue(ii.ccc_called)650self.assertIsNone(ii.pixmapText)651