|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Copyright © QtAppUtils Project Contributors |
| 4 | +# https://github.com/jnsebgosselin/qtapputils |
| 5 | +# |
| 6 | +# This file is part of QtAppUtils. |
| 7 | +# Licensed under the terms of the MIT License. |
| 8 | +# ----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +""" |
| 11 | +Tests for the ConfDialog class. |
| 12 | +""" |
| 13 | +# ---- Third party imports |
| 14 | +import pytest |
| 15 | +from qtpy.QtCore import Qt |
| 16 | +from qtpy.QtWidgets import QWidget |
| 17 | + |
| 18 | +# ---- Local imports |
| 19 | +from qtapputils.widgets.configdialog import ConfDialog, ConfPage |
| 20 | +import qtawesome as qta |
| 21 | + |
| 22 | + |
| 23 | +# ============================================================================= |
| 24 | +# ---- Fixtures |
| 25 | +# ============================================================================= |
| 26 | +class ConfPageTest(ConfPage): |
| 27 | + |
| 28 | + def __init__(self, name, label, icon, conf): |
| 29 | + self.conf = conf |
| 30 | + self.value = None |
| 31 | + super().__init__(name, label, icon) |
| 32 | + |
| 33 | + def setup_page(self): |
| 34 | + pass |
| 35 | + |
| 36 | + def set_value(self, value): |
| 37 | + self.value = value |
| 38 | + self.sig_configs_changed.emit() |
| 39 | + |
| 40 | + def get_configs(self): |
| 41 | + return {self.name(): self.value} |
| 42 | + |
| 43 | + def get_configs_from_conf(self): |
| 44 | + return {self.name(): self.conf[self.name()]} |
| 45 | + |
| 46 | + def load_configs_from_conf(self): |
| 47 | + self.value = self.conf[self.name()] |
| 48 | + |
| 49 | + def save_configs_to_conf(self): |
| 50 | + self.conf[self.name()] = self.value |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture |
| 54 | +def conf(qtbot): |
| 55 | + """ |
| 56 | + A fixture to imitate Sardes conf system. |
| 57 | + """ |
| 58 | + return {'confpagetest1': 3, 'confpagetest2': 4} |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def configdialog(qtbot, conf): |
| 63 | + configdialog = ConfDialog(main=QWidget()) |
| 64 | + qtbot.addWidget(configdialog) |
| 65 | + configdialog.show() |
| 66 | + |
| 67 | + confpage_icon = qta.icon('fa5.image') |
| 68 | + |
| 69 | + configdialog.add_confpage(ConfPageTest( |
| 70 | + 'confpagetest1', 'ConfPageTest1', confpage_icon, conf)) |
| 71 | + configdialog.add_confpage(ConfPageTest( |
| 72 | + 'confpagetest2', 'ConfPageTest2', confpage_icon, conf)) |
| 73 | + |
| 74 | + assert not configdialog.apply_button.isEnabled() |
| 75 | + assert conf == {'confpagetest1': 3, 'confpagetest2': 4} |
| 76 | + assert configdialog.count() == 2 |
| 77 | + |
| 78 | + return configdialog |
| 79 | + |
| 80 | + |
| 81 | +# ============================================================================= |
| 82 | +# ---- Tests for the ConfDialog |
| 83 | +# ============================================================================= |
| 84 | +def test_configs_changed(configdialog, conf, qtbot): |
| 85 | + """ |
| 86 | + Test that the interface is updated as expected when the configs are |
| 87 | + changed in one or more configuration pages. |
| 88 | + """ |
| 89 | + confpage1 = configdialog.get_confpage('confpagetest1') |
| 90 | + |
| 91 | + # Change the value of the first page. |
| 92 | + confpage1.set_value(5) |
| 93 | + |
| 94 | + assert conf == {'confpagetest1': 3, 'confpagetest2': 4} |
| 95 | + assert confpage1.is_modified() |
| 96 | + assert configdialog.apply_button.isEnabled() |
| 97 | + |
| 98 | + # Revert the value of the first page to its original value. |
| 99 | + confpage1.set_value(3) |
| 100 | + |
| 101 | + assert conf == {'confpagetest1': 3, 'confpagetest2': 4} |
| 102 | + assert confpage1.is_modified() is False |
| 103 | + assert not configdialog.apply_button.isEnabled() |
| 104 | + |
| 105 | + |
| 106 | +def test_apply_setting_changes(configdialog, conf, qtbot): |
| 107 | + """ |
| 108 | + Test that applying setting changes is working as expected. |
| 109 | + """ |
| 110 | + confpage1 = configdialog.get_confpage('confpagetest1') |
| 111 | + confpage2 = configdialog.get_confpage('confpagetest2') |
| 112 | + |
| 113 | + # Change the value of the first and second page. |
| 114 | + confpage1.set_value(34) |
| 115 | + confpage2.set_value(25) |
| 116 | + |
| 117 | + assert conf == {'confpagetest1': 3, 'confpagetest2': 4} |
| 118 | + assert confpage1.is_modified() |
| 119 | + assert confpage2.is_modified() |
| 120 | + assert configdialog.apply_button.isEnabled() |
| 121 | + |
| 122 | + # Apply the changes. |
| 123 | + |
| 124 | + # Note that the OK button applies the changes and also close the |
| 125 | + # configurations dialog. |
| 126 | + qtbot.mouseClick(configdialog.ok_button, Qt.LeftButton) |
| 127 | + assert conf == {'confpagetest1': 34, 'confpagetest2': 25} |
| 128 | + assert confpage1.is_modified() is False |
| 129 | + assert confpage2.is_modified() is False |
| 130 | + assert not configdialog.apply_button.isEnabled() |
| 131 | + |
| 132 | + |
| 133 | +def test_cancel_setting_changes(configdialog, conf, qtbot): |
| 134 | + """ |
| 135 | + Test that cancelling setting changes is working as expected. |
| 136 | + """ |
| 137 | + confpage1 = configdialog.get_confpage('confpagetest1') |
| 138 | + confpage2 = configdialog.get_confpage('confpagetest2') |
| 139 | + |
| 140 | + # Change the value of the first and second page. |
| 141 | + confpage1.set_value(34) |
| 142 | + confpage2.set_value(25) |
| 143 | + |
| 144 | + assert conf == {'confpagetest1': 3, 'confpagetest2': 4} |
| 145 | + assert confpage1.is_modified() |
| 146 | + assert confpage2.is_modified() |
| 147 | + assert configdialog.apply_button.isEnabled() |
| 148 | + |
| 149 | + # Cancel the changes. |
| 150 | + qtbot.mouseClick(configdialog.cancel_button, Qt.LeftButton) |
| 151 | + assert conf == {'confpagetest1': 3, 'confpagetest2': 4} |
| 152 | + assert confpage1.is_modified() is False |
| 153 | + assert confpage2.is_modified() is False |
| 154 | + assert not configdialog.apply_button.isEnabled() |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == "__main__": |
| 158 | + pytest.main(['-x', __file__, '-v', '-rw']) |
0 commit comments