Skip to content

Commit

Permalink
Fixed stability of GUI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devos50 committed Jul 24, 2016
1 parent d264a84 commit 95d7956
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 22 deletions.
1 change: 1 addition & 0 deletions Tribler/Test/GUI/FakeApi/endpoints/settings_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def render_GET(self, request):
"seeding_mode": "ratio",
"seeding_time": 60,
"seeding_ratio": 2.0,
"saveas": "bla",
},
"multichain": {
"enabled": True,
Expand Down
24 changes: 16 additions & 8 deletions Tribler/Test/GUI/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@

from PyQt5.QtWidgets import QApplication, QListWidget, QTreeWidget

import TriblerGUI
from Tribler.Core.Utilities.network_utils import get_random_port

import TriblerGUI.core_manager as core_manager
rand_port = get_random_port()
core_manager.START_FAKE_API = True
core_manager.FAKE_API_PORT = rand_port

import TriblerGUI.tribler_request_manager as request_mgr
request_mgr.API_PORT = rand_port

# We also need to add the TriblerGUI directory to the path. This should be done before the
# window is imported, otherwise, it cannot find the UI widgets.
sys.path.append(os.path.dirname(TriblerGUI.__file__))
import TriblerGUI

from TriblerGUI.home_recommended_item import HomeRecommendedChannelItem, HomeRecommendedTorrentItem
from TriblerGUI.loading_list_item import LoadingListItem
from TriblerGUI.tribler_window import TriblerWindow

os.environ['VLC_PLUGIN_PATH'] = '/Applications/VLC.app/Contents/MacOS/plugins'

app = QApplication(sys.argv)
window = TriblerWindow()
window = TriblerWindow(api_port=rand_port)
QTest.qWaitForWindowExposed(window)

sys.excepthook = sys.__excepthook__
Expand Down Expand Up @@ -64,7 +68,11 @@ def screenshot(self, widget, name=None):
if name is not None:
img_name = 'screenshot_%s.jpg' % name

pixmap.save(os.path.join(os.path.dirname(TriblerGUI.__file__), 'screenshots', img_name))
screenshots_dir = os.path.join(os.path.dirname(TriblerGUI.__file__), 'screenshots')
if not os.path.exists(screenshots_dir):
os.mkdir(screenshots_dir)

pixmap.save(os.path.join(screenshots_dir, img_name))

def wait_for_list_populated(self, list, num_items=1, timeout=10):
for _ in range(0, timeout * 1000, 100):
Expand Down
9 changes: 6 additions & 3 deletions TriblerGUI/core_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@

class CoreManager(object):

def __init__(self):
def __init__(self, api_port):
environment = QProcessEnvironment.systemEnvironment()

environment.insert("base_path", get_base_path())
if not is_frozen():
environment.insert("base_path", os.path.join(get_base_path(), ".."))

self.api_port = api_port

self.core_process = QProcess()
self.core_process.setProcessEnvironment(environment)
self.core_process.readyReadStandardOutput.connect(self.on_ready_read_stdout)
self.core_process.readyReadStandardError.connect(self.on_ready_read_stderr)
self.core_process.finished.connect(self.on_finished)
self.events_manager = EventRequestManager()
self.events_manager = EventRequestManager(api_port)

self.shutting_down = False

def start(self):
core_script_path = os.path.join(get_base_path(), 'scripts', 'start_core.py')
if START_FAKE_API:
self.core_process.start("python %s/scripts/start_fake_core.py" % os.path.dirname(TriblerGUI.__file__))
self.core_process.start("python %s/scripts/start_fake_core.py %d" %
(os.path.dirname(TriblerGUI.__file__), self.api_port))
else:
self.core_process.start("python %s -n tribler" % core_script_path)

Expand Down
3 changes: 3 additions & 0 deletions TriblerGUI/dialogs/dialogcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def paintEvent(self, event):
self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)

def on_main_window_resize(self):
if not self.parentWidget():
return

self.setFixedSize(self.parentWidget().size())
self.dialog_widget.setFixedWidth(self.width() - 100)
self.dialog_widget.move(QPoint(self.geometry().center().x() - self.dialog_widget.geometry().width() / 2,
Expand Down
4 changes: 2 additions & 2 deletions TriblerGUI/event_request_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class EventRequestManager(QNetworkAccessManager):
discovered_channel = pyqtSignal(object)
discovered_torrent = pyqtSignal(object)

def __init__(self):
def __init__(self, api_port):
QNetworkAccessManager.__init__(self)
url = QUrl("http://localhost:8085/events")
url = QUrl("http://localhost:%d/events" % api_port)
self.request = QNetworkRequest(url)
self.failed_attempts = 0
self.connect_timer = QTimer()
Expand Down
11 changes: 7 additions & 4 deletions TriblerGUI/scripts/start_fake_core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging
import os
import sys
import Tribler

os.chdir(os.path.join(os.path.dirname(Tribler.__file__), "Test", "GUI", "FakeApi"))
if len(sys.argv) < 2:
exit()

os.chdir(os.path.join(os.path.dirname(__file__), "..", "..", "Tribler", "Test", "GUI", "FakeApi"))
sys.path.insert(0, os.path.abspath(os.getcwd()))

from twisted.internet import reactor
Expand All @@ -26,6 +28,7 @@ def generate_tribler_data():
generate_tribler_data()

site = Site(RootEndpoint())
logger.info("Starting fake Tribler API on port 8085")
reactor.listenTCP(8085, site)
port = int(sys.argv[1])
logger.info("Starting fake Tribler API on port %d" % port)
reactor.listenTCP(port, site)
reactor.run()
10 changes: 7 additions & 3 deletions TriblerGUI/tribler_request_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest


API_PORT = 8085


class TriblerRequestManager(QNetworkAccessManager):
"""
This class is responsible for all the requests made to the Tribler REST API.
"""

base_url = "http://localhost:8085/"

received_json = pyqtSignal(object, int)
received_file = pyqtSignal(str, object)

def __init__(self):
QNetworkAccessManager.__init__(self)
self.base_url = "http://localhost:%d/" % API_PORT

def send_file(self, endpoint, read_callback, file):
"""
From http://stackoverflow.com/questions/7922015/qnetworkaccessmanager-posting-files-via-http
Expand Down
5 changes: 3 additions & 2 deletions TriblerGUI/tribler_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ def on_exception(self, *exc_info):
self.feedback_dialog_is_open = True
result = dialog.exec_()

def __init__(self):
def __init__(self, api_port=8085):
super(TriblerWindow, self).__init__()

self.api_port = api_port
self.navigation_stack = []
self.feedback_dialog_is_open = False
self.tribler_started = False
self.tribler_settings = None
self.core_manager = CoreManager()
self.core_manager = CoreManager(self.api_port)

sys.excepthook = self.on_exception

Expand Down

0 comments on commit 95d7956

Please sign in to comment.