From 86d052babad543ebf1da6b16a9b38bdd8fb815de Mon Sep 17 00:00:00 2001 From: janezd Date: Sat, 17 Oct 2020 18:32:52 +0200 Subject: [PATCH] customizableplot.available_font_families: Fix for non-existent default family --- .../visualize/utils/customizableplot.py | 7 +++- .../utils/tests/test_customizableplot.py | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 Orange/widgets/visualize/utils/tests/test_customizableplot.py diff --git a/Orange/widgets/visualize/utils/customizableplot.py b/Orange/widgets/visualize/utils/customizableplot.py index afdc0ecfad1..f9655a26801 100644 --- a/Orange/widgets/visualize/utils/customizableplot.py +++ b/Orange/widgets/visualize/utils/customizableplot.py @@ -28,8 +28,11 @@ def available_font_families() -> List: if not QApplication.instance(): _ = QApplication(sys.argv) fonts = QFontDatabase().families() - default = fonts.pop(fonts.index(default_font_family())) + default = default_font_family() + defaults = [default] + if default in fonts: + fonts.remove(default) guessed_name = default.split()[0] i = 0 @@ -263,7 +266,7 @@ def update_axis(axis, **settings): self.getAxis(axis), settings[self.TITLE_LABEL]) self.FONT_FAMILY_SETTING: SettingsType = { # pylint: disable=invalid-name - Updater.FONT_FAMILY_LABEL: (available_font_families(), QFont().family()), + Updater.FONT_FAMILY_LABEL: (available_font_families(), default_font_family()), } self.FONT_SETTING: SettingsType = { # pylint: disable=invalid-name diff --git a/Orange/widgets/visualize/utils/tests/test_customizableplot.py b/Orange/widgets/visualize/utils/tests/test_customizableplot.py new file mode 100644 index 00000000000..5863ced5fa3 --- /dev/null +++ b/Orange/widgets/visualize/utils/tests/test_customizableplot.py @@ -0,0 +1,38 @@ +import unittest +from unittest.mock import patch, Mock + +from Orange.widgets.visualize.utils import customizableplot + + +class TestFonts(unittest.TestCase): + def test_available_font_families(self): + with patch.object(customizableplot, "QFont") as font, \ + patch.object(customizableplot, "QFontDatabase") as db: + font.return_value = Mock() + font.return_value.family = Mock(return_value="mock regular") + + db.return_value = Mock() + db.return_value.families = Mock( + return_value=["a", ".d", "e", ".b", "mock regular", "c"]) + self.assertEqual(customizableplot.available_font_families(), + ["mock regular", "", "a", ".b", "c", ".d", "e"]) + + db.return_value = Mock() + db.return_value.families = Mock( + return_value=["a", ".d", "e", ".b", "mock regular", + "mock bold", "mock italic", "c", "mock semi"]) + self.assertEqual(customizableplot.available_font_families(), + ["mock regular", "mock bold", "mock italic", + "mock semi", "", + "a", ".b", "c", ".d", "e"]) + + # It seems it's possible that default font family does not exist + # (see gh-5036) + db.return_value.families.return_value = ["a", ".d", "e", ".b", "c"] + self.assertEqual(customizableplot.available_font_families(), + ["mock regular", "", "a", ".b", "c", ".d", "e"]) + self.assertIn(customizableplot.default_font_family(), + customizableplot.available_font_families()) + +if __name__ == "__main__": + unittest.main()