def test_get_font():
    """Tests the :class:`~_clockalarm.Notification.get_font` method with font given
    """
    notification = Notification("Test", font_family="helvetica", font_size=10)
    font = notification.get_font()
    assert isinstance(font, QFont)
    assert font.family() == "helvetica"
    assert font.pointSize() == 10
def test_get_color():
    """Tests the :class:`~_clockalarm.Notification.get_color` method.
    """
    notification = Notification("Test", color_hex="#ff5500")
    color = notification.get_color()

    assert isinstance(color, QColor)
    assert color.name() == "#ff5500"
def test_get_color_miss_arg(init_paths):
    """Tests the :class:`~_clockalarm.Notification.get_color` method without any
    color given.
    """
    notification = Notification("Test")
    color = notification.get_color()

    assert isinstance(color, QColor)
    assert color.name() == importExportUtils.get_default_config(
        "NOTIFICATION_COLOR_HEX")
def test_get_font_miss_two_arg(init_paths):
    """Tests the :class:`~_clockalarm.Notification.get_font` method without any
    font given.
    """
    notification = Notification("Test")
    font = notification.get_font()
    assert isinstance(font, QFont)
    assert font.family() == importExportUtils.get_default_config(
        "NOTIFICATION_FONT_FAMILY")
    assert font.pointSize() == importExportUtils.get_default_config(
        "NOTIFICATION_FONT_SIZE", "int")
def test_get_sound_corrupted():
    """Tests the :class:`~_clockalarm.Notification.get_sound` method without any
    sound or corrupted file given.
    """
    try:
        pygame.mixer.init()

        sound = Notification("Test", sound="corrupted.wave").get_sound()
        assert isinstance(sound, pygame.mixer.Sound)
        sound = Notification("Test").get_sound()
        assert isinstance(sound, pygame.mixer.Sound)
    except pygame.error as e:
        print("test_get_sound_corrupted {}".format(str(e)))
def test_nc_close_popup_nonexistent_popup(before):
    """Test the :class:`~_clockalarm.NotificationCenter` close_popup method.

    Trying to remove NotificationWidget object not on the list.

    """

    geometry = QRect(0, 0, 1920, 1080)
    nc = NotificationCenter.NotificationCenter(QRect(0, 0, 1920, 1080),
                                               FakeParent())
    nc.add_to_queue(Notification("test message"))

    with pytest.raises(KeyError):
        nc.close_popup(NotificationWidget(geometry, Notification("message")))
    assert len(nc._displayed_popups) == 1
        def button_clicked():
            dw = self.dialog_widget
            periodicity = QTime(0, 0).secsTo(dw.periodicity_edit.time())
            font_family = dw.font_family_edit.text()
            font_size = dw.font_size_edit.value()
            message = dw.alert_message_edit.text()
            if font_family == '':
                font_family = None
            if font_size <= 0:
                font_size = None
            if message == '':
                message = "Untitled simple alert"
            notification = Notification(message,
                                        font_family=font_family,
                                        font_size=font_size,
                                        color_hex=dw.color_edit.hex_color,
                                        sound=dw.sound_edit.sound_name)
            trigger_time = dw.date_time_edit.dateTime().toTime_t()

            try:
                self.app.alert_collection.edit(id_alert,
                                               notification=notification,
                                               trigger_time=trigger_time,
                                               periodicity=periodicity)
            except KeyError:
                logging.info("The alert to edit doesn't exist anymore")
            dw.close()
    def load_db(self):
        """Fill the AlertCollection with Alerts form the TinyDB database

        Exceptions:
            IOError: If a required parameter can't be found in the database. The database is probably corrupted.

        """
        self.alert_list = []  # clean the alert list
        for alert in self.db.all():
            try:
                notification = Notification(alert["message"],
                                            color_hex=alert["color_hex"],
                                            font_family=alert["font_family"],
                                            font_size=alert["font_size"],
                                            sound=alert["sound"])
                new_alert = SimpleAlert(alert["trigger_time"], notification)
            except KeyError as e:
                raise IOError('The alert database seems corrupted' + str(e))

            if "periodicity" in alert:  # periodicity can not appear in db
                new_alert.periodicity = alert["periodicity"]
            new_alert.id = alert.eid  # use the TinyDB object eid as alert id

            self.alert_list.append(new_alert)
            if self.parent:  # connect the alert to the GUI
                new_alert.timeout.connect(
                    self.parent.notification_center.add_to_queue)
        def button_clicked():
            dw = self.dialog_widget
            periodicity = QTime(0, 0).secsTo(dw.periodicity_edit.time())
            font_family = dw.font_family_edit.text()
            font_size = dw.font_size_edit.value()
            message = dw.alert_message_edit.text()
            if periodicity == 0:
                periodicity = None
            if font_family == '':
                font_family = None
            if font_size <= 0:
                font_size = None
            if message == '':
                message = "Untitled simple alert"
            notification = Notification(message,
                                        font_family=font_family,
                                        font_size=font_size,
                                        color_hex=dw.color_edit.hex_color,
                                        sound=dw.sound_edit.sound_name)
            new_alert = SimpleAlert(dw.date_time_edit.dateTime().toTime_t(),
                                    notification,
                                    periodicity=periodicity)

            self.app.alert_collection.add(new_alert)
            dw.close()
def test_notification_constructor():
    """Tests the :class:`~_clockalarm.Notification` constructor."""
    notification = Notification("Test")  # correct constructor
    assert notification.message == "Test"
    assert not notification.color_hex
    assert not notification.font_family
    assert not notification.font_size
    assert not notification.sound

    notification = Notification("Test", "#ffff00", None, None,
                                "test.wav")  # correct constructor
    assert notification.message == "Test"
    assert notification.color_hex == "#ffff00"
    assert not notification.font_family
    assert not notification.font_size
    assert notification.sound == "test.wav"
def test_nc_display_popup_value_error(before):
    """Test the :class:`~_clockalarm.NotificationCenter` display_popup method.

    Wrong arguments are passed to the method

    """

    nc = NotificationCenter.NotificationCenter(QRect(0, 0, 1920, 1080),
                                               FakeParent())
    with pytest.raises(ValueError):
        nc.display_popup(None, Notification("Test Message"))
    with pytest.raises(ValueError):
        nc.display_popup(QRect(0, 0, 1920, 1080), None)
    with pytest.raises(ValueError):
        nc.display_popup(0, Notification("Test Message"))
    with pytest.raises(ValueError):
        nc.display_popup(QRect(0, 0, 1920, 1080), 0)
def test_nc_refresh(before):
    """Test the :class:`~_clockalarm.NotificationCenter` refresh method."""

    nc = NotificationCenter.NotificationCenter(QRect(0, 0, 1920, 1080),
                                               FakeParent())
    for i in range(0, 6):
        nc.add_to_queue(Notification("test message " + str(i)))
    nc.refresh()
    assert len(nc._popup_queue) == 2
    assert len(nc._displayed_popups) == 4
def test_nc_display_popup(before):
    """Test the :class:`~_clockalarm.NotificationCenter` display_popup method."""

    nc = NotificationCenter.NotificationCenter(QRect(0, 0, 1920, 1080),
                                               FakeParent())
    for i in range(0, 4):
        nc.display_popup(QRect(0, 0, 1920, 1080),
                         Notification("Test Message" + str(i)))

    assert len(nc._displayed_popups) == 4
def test_get_sound():
    """Tests the :class:`~_clockalarm.Notification.get_sound` method
    """
    try:
        pygame.mixer.init()

        sound = Notification("Test", sound="floop.wave").get_sound()
        assert isinstance(sound, pygame.mixer.Sound)
    except pygame.error as e:
        print("test_get_sound: {}".format(str(e)))
def test_nc_display_popup_index_error(before):
    """Test the :class:`~_clockalarm.NotificationCenter` display_popup method.

    Too much popups are added to the display zone

    """

    nc = NotificationCenter.NotificationCenter(QRect(0, 0, 1920, 1080),
                                               FakeParent())
    with pytest.raises(IndexError):
        for i in range(0, 5):
            nc.display_popup(QRect(0, 0, 1920, 1080),
                             Notification("Test Message" + str(i)))
def test_nc_add_to_queue_multiple(before):
    """Test the :class:`~_clockalarm.NotificationCenter` add_to_queue method.

    After 6 notifications are added to the list, 2 elements in the queue and 4 popups displayed.

    """

    nc = NotificationCenter.NotificationCenter(QRect(0, 0, 1920, 1080),
                                               FakeParent)
    for i in range(0, 6):
        nc.add_to_queue(Notification("test message " + str(i)))

    assert len(nc._popup_queue) == 2
    assert len(nc._displayed_popups) == 4
def test_nc_close_popup(before):
    """Test the :class:`~_clockalarm.NotificationCenter` close_popup method.

    Test that the popup is correctly removed from the list when method is called

    """

    nc = NotificationCenter.NotificationCenter(QRect(0, 0, 1920, 1080),
                                               FakeParent())
    nc.add_to_queue(Notification("test message"))

    assert len(nc._displayed_popups) == 1
    popup = nc._displayed_popups[0]
    nc.close_popup(popup)
    assert len(nc._displayed_popups) == 0
def test_nc_add_to_queue(before):
    """Test the :class:`~_clockalarm.NotificationCenter` add_to_queue method.

    After one notification is added to the list, the queue is empty and one popup is displayed.

    """

    nc = NotificationCenter.NotificationCenter(QRect(0, 0, 1920, 1080),
                                               FakeParent)
    noti_1 = Notification("test message")
    nc.add_to_queue(noti_1)

    assert len(nc._popup_queue) == 0
    assert len(nc._displayed_popups) == 1
    assert nc._displayed_popups[0].notification.get_message() == "test message"
def test_nc_close_popup_wrong_arg(before):
    """Test the :class:`~_clockalarm.NotificationCenter` close_popup method.

    Trying to remove None or wrong NotificationWidget object

    """

    nc = NotificationCenter.NotificationCenter(QRect(0, 0, 1920, 1080),
                                               FakeParent())
    nc.add_to_queue(Notification("test message"))

    with pytest.raises(ValueError):
        nc.close_popup(None)
    with pytest.raises(ValueError):
        nc.close_popup(QRect)
    assert len(nc._displayed_popups) == 1
def test_get_font_miss_one_arg(init_paths):
    """Tests the :class:`~_clockalarm.Notification.get_font` method with one parameter missing
    """
    notification = Notification("Test", font_size=10)
    font = notification.get_font()
    assert isinstance(font, QFont)
    assert font.family() == importExportUtils.get_default_config(
        "NOTIFICATION_FONT_FAMILY")
    assert font.pointSize() == 10

    notification = Notification("Test", font_family="helvetica")
    font = notification.get_font()
    assert isinstance(font, QFont)
    assert font.family() == "helvetica"
    assert font.pointSize() == importExportUtils.get_default_config(
        "NOTIFICATION_FONT_SIZE", "int")
Exemple #21
0
import pytest

from _clockalarm import Notification
from _clockalarm.SimpleAlert import SimpleAlert

notification = Notification("Test")


@pytest.mark.test
def test_simple_alert_constructor():
    """Test the :class:`~_clockalarm.SimpleAlert` constructor."""
    global notification
    simplealert = SimpleAlert(10, notification)

    assert simplealert.trigger_time == 10
    assert not simplealert.periodicity


@pytest.mark.test
def test_get_periodicity():
    """Test the :class:`~_clockalarm.SimpleAlert.get_periodicity` method."""
    global notification
    simplealert = SimpleAlert(10, notification, 30)

    assert simplealert.get_periodicity() == 30


@pytest.mark.test
def test_get_notification():
    """Test the :class:`~_clockalarm.SimpleAlert.get_notification` method."""
    global notification
def test_get_message():
    """Tests the :class:`~_clockalarm.Notification.get_message` method."""
    notification = Notification("Test")

    assert notification.get_message() == "Test"
def test_notification_constructor_exception():
    """Tests the :class:`~_clockalarm.Notification` constructor."""
    with pytest.raises(ValueError):  # message is empty
        Notification("", "#ffff00", None, None, "test.wav")
    with pytest.raises(ValueError):  # message is None
        Notification(None, "#ffff00", None, None, "test.wav")