Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
import asyncio

if os.name == "nt": # If on windows
if os.name == "nt": # If on windows
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

from qtpy.QtWidgets import QApplication
Expand Down
3 changes: 1 addition & 2 deletions opencodeblocks/graphics/blocks/codeblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ def init_run_button(self):

def run_code(self):
"""Run the code in the block"""
code = self.source_editor.text()
self.source = code
self.source = self.source_editor.text()
# Create a worker to handle execution
worker = Worker(self.source_editor.kernel, self.source)
worker.signals.stdout.connect(self.handle_stdout)
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/blocks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# OpenCodeBlock an open-source tool for modular visual programing in python
# Copyright (C) 2021 Mathïs FEDERICO <https://www.gnu.org/licenses/>

"""
Integration tests for blocks behavior.
"""
73 changes: 73 additions & 0 deletions tests/integration/blocks/test_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# OpenCodeBlock an open-source tool for modular visual programing in python
# Copyright (C) 2021 Mathïs FEDERICO <https://www.gnu.org/licenses/>

"""
Integration tests for the OCBBlocks.
"""

import pytest
import pyautogui
from pytestqt.qtbot import QtBot

from PyQt5.QtCore import QPointF

from opencodeblocks.graphics.blocks.codeblock import OCBBlock
from opencodeblocks.graphics.window import OCBWindow
from opencodeblocks.graphics.widget import OCBWidget

from tests.integration.utils import apply_function_inapp, CheckingQueue


class TestBlocks:

@pytest.fixture(autouse=True)
def setup(self):
""" Setup reused variables. """
self.window = OCBWindow()
self.ocb_widget = OCBWidget()
self.subwindow = self.window.mdiArea.addSubWindow(self.ocb_widget)
self.subwindow.show()
self.block = OCBBlock(title="Testing block")

def test_create_blocks(self, qtbot: QtBot):
""" can be added to the scene. """
self.ocb_widget.scene.addItem(self.block)

def test_move_blocks(self, qtbot: QtBot):
""" can be dragged around with the mouse. """
self.ocb_widget.scene.addItem(self.block)

def testing_drag(msgQueue: CheckingQueue):
expected_move_amount = [70, -30]
pos_block = QPointF(self.block.pos().x(), self.block.pos().y())

pos_block.setX(
pos_block.x() + self.block.title_height + self.block.edge_size
)
pos_block.setY(pos_block.y() + self.block.title_height/2)

pos_block = self.ocb_widget.view.mapFromScene(pos_block)
pos_block = self.ocb_widget.view.mapToGlobal(pos_block)

pyautogui.moveTo(pos_block.x(), pos_block.y())
pyautogui.mouseDown(button="left")

iterations = 5
for i in range(iterations+1):
pyautogui.moveTo(
pos_block.x() + expected_move_amount[0] * i / iterations,
pos_block.y() + expected_move_amount[1] * i / iterations
)

pyautogui.mouseUp(button="left")

move_amount = [self.block.pos().x(), self.block.pos().y()]
# rectify because the scene can be zoomed :
move_amount[0] = move_amount[0] * self.ocb_widget.view.zoom
move_amount[1] = move_amount[1] * self.ocb_widget.view.zoom

msgQueue.check_equal(
move_amount, expected_move_amount, "Block moved by the correct amound")
msgQueue.stop()

apply_function_inapp(self.window, testing_drag)
68 changes: 68 additions & 0 deletions tests/integration/blocks/test_codeblock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# OpenCodeBlock an open-source tool for modular visual programing in python
# Copyright (C) 2021 Mathïs FEDERICO <https://www.gnu.org/licenses/>

"""
Integration tests for the OCBCodeBlocks.
"""

import pyautogui
import pytest
from pytestqt.qtbot import QtBot

from PyQt5.QtCore import QPointF

from opencodeblocks.graphics.blocks.codeblock import OCBCodeBlock
from opencodeblocks.graphics.window import OCBWindow
from opencodeblocks.graphics.widget import OCBWidget

from tests.integration.utils import apply_function_inapp, CheckingQueue


class TestCodeBlocks:

@pytest.fixture(autouse=True)
def setup(self):
""" Setup reused variables. """
self.window = OCBWindow()
self.ocb_widget = OCBWidget()
self.subwindow = self.window.mdiArea.addSubWindow(self.ocb_widget)
self.subwindow.show()

def test_run_python(self, qtbot: QtBot):
""" run source code when run button is pressed. """

# Add a block with the source to the window
EXPRESSION = "3 + 5 * 2"
SOURCE_TEST = f'''print({EXPRESSION})'''
expected_result = str(3 + 5 * 2)

test_block = OCBCodeBlock(title="CodeBlock test", source=SOURCE_TEST)
self.ocb_widget.scene.addItem(test_block)

def testing_run(msgQueue: CheckingQueue):

msgQueue.check_equal(test_block.stdout.strip(), "")

pos_run_button = test_block.run_button.pos()
pos_run_button = QPointF(
pos_run_button.x() + test_block.run_button.width() / 2,
pos_run_button.y() + test_block.run_button.height() / 2,
)
pos_run_button = self.ocb_widget.view.mapFromScene(pos_run_button)
pos_run_button = self.ocb_widget.view.mapToGlobal(pos_run_button)

# Run the block by pressung the run button
pyautogui.moveTo(pos_run_button.x(), pos_run_button.y())
pyautogui.mouseDown(button="left")
pyautogui.mouseUp(button="left")

# qtbot.mouseMove(test_block.run_button)
# qtbot.mousePress(test_block.run_button,
# Qt.MouseButton.LeftButton, delay=1)
# qtbot.mouseRelease(test_block.run_button, Qt.MouseButton.LeftButton)

# When the execution becomes non-blocking for the UI, a refactor will be needed here.
msgQueue.check_equal(test_block.stdout.strip(), expected_result)
msgQueue.stop()

apply_function_inapp(self.window, testing_run)
144 changes: 0 additions & 144 deletions tests/integration/test_blocks.py

This file was deleted.

52 changes: 52 additions & 0 deletions tests/integration/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# OpenCodeBlock an open-source tool for modular visual programing in python
# Copyright (C) 2021 Mathïs FEDERICO <https://www.gnu.org/licenses/>

"""
Utilities functions for integration testing.
"""

import os
import asyncio
from typing import Callable

import threading
from queue import Queue
from qtpy.QtWidgets import QApplication
import pytest_check as check

from opencodeblocks.graphics.window import OCBWindow

STOP_MSG = "stop"
CHECK_MSG = "check"


class CheckingQueue(Queue):

def check_equal(self, a, b, msg=""):
self.put([CHECK_MSG, a, b, msg])

def stop(self):
self.put([STOP_MSG])


def apply_function_inapp(window: OCBWindow, run_func: Callable):

if os.name == "nt": # If on windows
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

QApplication.processEvents()
msgQueue = CheckingQueue()
t = threading.Thread(target=run_func, args=(msgQueue,))
t.start()

stop = False
while not stop:
QApplication.processEvents()
if not msgQueue.empty():
msg = msgQueue.get()
if msg[0] == CHECK_MSG:
check.equal(msg[1], msg[2], msg[3])
elif msg[0] == STOP_MSG:
stop = True
t.join()
window.close()