beeref/tests/test_items.py

124 lines
4.7 KiB
Python
Raw Normal View History

2021-03-30 17:04:44 +00:00
from unittest.mock import patch, MagicMock, PropertyMock
2021-03-25 17:45:55 +00:00
2021-04-05 17:07:31 +00:00
from PyQt6 import QtCore, QtGui
2021-03-22 20:48:05 +00:00
from beeref.items import BeePixmapItem
2021-03-30 17:04:44 +00:00
from beeref.scene import BeeGraphicsScene
from .base import BeeTestCase
2021-03-22 20:48:05 +00:00
class BeePixmapItemTestCase(BeeTestCase):
def setUp(self):
self.scene = BeeGraphicsScene(None)
2021-04-03 14:53:19 +00:00
@patch('beeref.selection.SelectableMixin.init_selectable')
def test_init(self, selectable_mock):
2021-03-28 09:06:04 +00:00
item = BeePixmapItem(
QtGui.QImage(self.imgfilename3x3), self.imgfilename3x3)
assert item.save_id is None
2021-03-22 20:48:05 +00:00
assert item.width == 3
assert item.height == 3
2021-03-28 20:45:16 +00:00
assert item.scale() == 1
2021-03-28 09:06:04 +00:00
assert item.filename == self.imgfilename3x3
2021-04-03 14:53:19 +00:00
selectable_mock.assert_called_once()
2021-03-25 17:45:55 +00:00
def test_set_pos_center(self):
item = BeePixmapItem(QtGui.QImage())
with patch('beeref.items.BeePixmapItem.width',
new_callable=PropertyMock, return_value=200):
with patch('beeref.items.BeePixmapItem.height',
new_callable=PropertyMock, return_value=100):
2021-04-05 17:07:31 +00:00
item.set_pos_center(QtCore.QPointF(0, 0))
2021-03-25 17:45:55 +00:00
assert item.pos().x() == -100
assert item.pos().y() == -50
2021-03-30 17:04:44 +00:00
def test_set_pos_center_when_scaled(self):
item = BeePixmapItem(QtGui.QImage())
item.setScale(2)
with patch('beeref.items.BeePixmapItem.width',
new_callable=PropertyMock, return_value=200):
with patch('beeref.items.BeePixmapItem.height',
new_callable=PropertyMock, return_value=100):
item.set_pos_center(QtCore.QPointF(0, 0))
assert item.pos().x() == -200
assert item.pos().y() == -100
2021-04-06 12:37:48 +00:00
def test_set_pos_center_when_rotated(self):
item = BeePixmapItem(QtGui.QImage())
item.setRotation(90)
with patch('beeref.items.BeePixmapItem.width',
new_callable=PropertyMock, return_value=200):
with patch('beeref.items.BeePixmapItem.height',
new_callable=PropertyMock, return_value=100):
item.set_pos_center(QtCore.QPointF(0, 0))
assert item.pos().x() == 50
assert item.pos().y() == -100
2021-04-03 14:53:19 +00:00
def test_pixmap_to_bytes(self):
item = BeePixmapItem(QtGui.QImage(self.imgfilename3x3))
assert item.pixmap_to_bytes().startswith(b'\x89PNG')
def test_pixmap_from_bytes(self):
item = BeePixmapItem(QtGui.QImage())
with open(self.imgfilename3x3, 'rb') as f:
imgdata = f.read()
item.pixmap_from_bytes(imgdata)
assert item.width == 3
assert item.height == 3
def test_paint(self):
item = BeePixmapItem(QtGui.QImage())
item.pixmap = MagicMock(return_value='bee')
item.paint_selectable = MagicMock()
painter = MagicMock()
item.paint(painter, None, None)
item.paint_selectable.assert_called_once()
painter.drawPixmap.assert_called_with(0, 0, 'bee')
def test_has_selection_outline_when_not_selected(self):
item = BeePixmapItem(QtGui.QImage())
self.scene.addItem(item)
2021-04-03 14:53:19 +00:00
item.setSelected(False)
item.has_selection_outline() is False
2021-04-03 14:53:19 +00:00
def test_has_selection_outline_when_selected(self):
item = BeePixmapItem(QtGui.QImage())
self.scene.addItem(item)
2021-04-03 14:53:19 +00:00
item.setSelected(True)
item.has_selection_outline() is True
2021-04-03 14:53:19 +00:00
def test_has_selection_handles_when_not_selected(self):
item1 = BeePixmapItem(QtGui.QImage())
self.scene.addItem(item1)
2021-04-03 14:53:19 +00:00
item1.setSelected(False)
item2 = BeePixmapItem(QtGui.QImage())
self.scene.addItem(item2)
2021-04-03 14:53:19 +00:00
item2.setSelected(False)
item1.has_selection_handles() is False
2021-04-03 14:53:19 +00:00
def test_has_selection_handles_when_selected_single(self):
item1 = BeePixmapItem(QtGui.QImage())
self.scene.addItem(item1)
item1.setSelected(True)
2021-03-30 17:04:44 +00:00
item2 = BeePixmapItem(QtGui.QImage())
self.scene.addItem(item2)
2021-04-03 14:53:19 +00:00
item2.setSelected(False)
item1.has_selection_handles() is True
def test_has_selection_handles_when_selected_multi(self):
view = MagicMock(get_scale=MagicMock(return_value=1))
with patch('beeref.scene.BeeGraphicsScene.views',
return_value=[view]):
item1 = BeePixmapItem(QtGui.QImage())
self.scene.addItem(item1)
item1.setSelected(True)
item2 = BeePixmapItem(QtGui.QImage())
self.scene.addItem(item2)
item2.setSelected(True)
item1.has_selection_handles() is False
def test_selection_action_items(self):
item = BeePixmapItem(QtGui.QImage())
assert item.selection_action_items() == [item]