From ea2e11ced83ce77129520bb4e0955476a107435a Mon Sep 17 00:00:00 2001 From: lakshyacis Date: Thu, 27 Feb 2020 20:16:43 +0530 Subject: [PATCH] Add quit function and code fixes --- .../test_addressgeneration.py | 4 +- src/graphicaltesting/test_addsubscription.py | 6 +- src/graphicaltesting/test_appstart.py | 1 + src/graphicaltesting/test_blackwhitelist.py | 11 +- src/graphicaltesting/test_chans.py | 3 + src/graphicaltesting/test_messagesend.py | 3 +- src/graphicaltesting/test_networkstatus.py | 5 +- src/graphicaltesting/test_quit.py | 101 ++++++++++++++++++ src/graphicaltesting/test_settingwindow.py | 5 +- src/graphicaltesting/testinitialization.py | 34 +++--- src/graphicaltesting/testloader.py | 1 + src/gtesting.py | 9 +- 12 files changed, 160 insertions(+), 23 deletions(-) create mode 100644 src/graphicaltesting/test_quit.py diff --git a/src/graphicaltesting/test_addressgeneration.py b/src/graphicaltesting/test_addressgeneration.py index 322b9e32..720eafc4 100644 --- a/src/graphicaltesting/test_addressgeneration.py +++ b/src/graphicaltesting/test_addressgeneration.py @@ -1,3 +1,4 @@ +"""Generate Address for tests""" from random import choice from string import ascii_lowercase @@ -83,6 +84,7 @@ class BitmessageTest_AddressGeneration(BitmessageTestCase): QTest.qWait(100) print( "\n Test Fail :--> Address Generatation Failed with passphrase" - " or Taking too much time to generate address \n") + " or Taking too much time to generate address \n" + ) self.assertTrue(False, " \n Test Fail :--> Address Generatation Failed with passphrase") return 0 diff --git a/src/graphicaltesting/test_addsubscription.py b/src/graphicaltesting/test_addsubscription.py index 1760db94..0901818b 100644 --- a/src/graphicaltesting/test_addsubscription.py +++ b/src/graphicaltesting/test_addsubscription.py @@ -1,3 +1,4 @@ +"""Add address in the subscription list""" from random import choice from string import ascii_lowercase @@ -12,6 +13,8 @@ from testloader import BitmessageTestCase class BitmessageTest_AddSubscription(BitmessageTestCase): + """Add address to list""" + def test_subscription(self): """Test for subscription functionality""" QTest.qWait(500) @@ -46,7 +49,8 @@ class BitmessageTest_AddSubscription(BitmessageTestCase): if shared.isAddressInMySubscriptionsList(address): print( "\n Test Fail :--> You cannot add the same address to your subscriptions twice." - " Perhaps rename the existing one if you want. \n") + " Perhaps rename the existing one if you want. \n" + ) QTest.qWait(500) return 0 self.myapp.addSubscription(address, label) diff --git a/src/graphicaltesting/test_appstart.py b/src/graphicaltesting/test_appstart.py index bcd023e5..fbb5f742 100644 --- a/src/graphicaltesting/test_appstart.py +++ b/src/graphicaltesting/test_appstart.py @@ -1,3 +1,4 @@ +"""Trigger dialog""" from PyQt4 import QtCore, QtGui from PyQt4.QtTest import QTest diff --git a/src/graphicaltesting/test_blackwhitelist.py b/src/graphicaltesting/test_blackwhitelist.py index 2750a36f..d08881be 100644 --- a/src/graphicaltesting/test_blackwhitelist.py +++ b/src/graphicaltesting/test_blackwhitelist.py @@ -1,3 +1,4 @@ +"""Tests for blackwhitelist""" from random import choice from string import ascii_lowercase @@ -39,6 +40,7 @@ class BitmessageTest_BlackandWhiteList(BitmessageTestCase): pass def checkblacklist(self, myapp): + """fill blacklist and whitelist fields""" # pylint: disable=too-many-statements QTest.qWait(1000) self.dialog.lineEditLabel.setText("") @@ -96,10 +98,13 @@ class BitmessageTest_BlackandWhiteList(BitmessageTestCase): QTest.qWait(100) print( "\n Test Fail :--> You cannot add the same address to your list twice." - " Perhaps rename the existing one if you want. \n") + " Perhaps rename the existing one if you want. \n" + ) self.assertTrue( - False, "\n Test Fail :--> You cannot add the same address to your list twice." - " Perhaps rename the existing one if you want.") + False, + "\n Test Fail :--> You cannot add the same address to your list twice." + " Perhaps rename the existing one if you want.", + ) return 0 else: QTest.qWait(100) diff --git a/src/graphicaltesting/test_chans.py b/src/graphicaltesting/test_chans.py index 6ccbe346..959dc530 100644 --- a/src/graphicaltesting/test_chans.py +++ b/src/graphicaltesting/test_chans.py @@ -1,9 +1,12 @@ +"""Tests for changs Tab""" from PyQt4.QtTest import QTest from testloader import BitmessageTestCase class BitmessageTest_ChansTest(BitmessageTestCase): + """Switch to chans and test""" + def test_chans(self): """Switch to chans window and test""" QTest.qWait(1200) diff --git a/src/graphicaltesting/test_messagesend.py b/src/graphicaltesting/test_messagesend.py index f5471dc8..b9a0b6d7 100644 --- a/src/graphicaltesting/test_messagesend.py +++ b/src/graphicaltesting/test_messagesend.py @@ -1,3 +1,4 @@ +"""Test for message send""" import random from random import choice from string import ascii_lowercase @@ -23,7 +24,7 @@ class BitmessageTest_MessageTesting(BitmessageTestCase): self.myapp.ui.comboBoxSendFrom.setCurrentIndex( random.randrange(1, len(BMConfigParser().addresses()) + 1) ) - QTest.qWait(800) + QTest.qWait(1000) rand_address = choice(BMConfigParser().addresses()) random_address = "" for x in range(len(rand_address)): diff --git a/src/graphicaltesting/test_networkstatus.py b/src/graphicaltesting/test_networkstatus.py index fe15f578..d3dbb35a 100644 --- a/src/graphicaltesting/test_networkstatus.py +++ b/src/graphicaltesting/test_networkstatus.py @@ -1,11 +1,14 @@ +"""Test for network window""" from PyQt4.QtTest import QTest from testloader import BitmessageTestCase class BitmessageTest_NetworkTest(BitmessageTestCase): + """Switch to network tab and test""" + def test_network(self): - """Switch to network window and test""" + """Switch to network window""" QTest.qWait(1000) self.myapp.ui.tabWidget.setCurrentWidget(self.myapp.ui.networkstatus) QTest.qWait(1200) diff --git a/src/graphicaltesting/test_quit.py b/src/graphicaltesting/test_quit.py new file mode 100644 index 00000000..329149e8 --- /dev/null +++ b/src/graphicaltesting/test_quit.py @@ -0,0 +1,101 @@ +"""Quits the application""" +import time + +from PyQt4 import QtCore, QtGui + +import bitmessageqt.sound +import shared +import shutdown +from bitmessageqt import settingsmixin +from bmconfigparser import BMConfigParser +from debug import logger +from network.stats import pendingDownload, pendingUpload +from proofofwork import getPowType +from testloader import BitmessageTestCase +from tr import _translate + + +class BitmessageTest_QuitTest(BitmessageTestCase): + """Quit the bitmessage qt application""" + + def test_quitapplication(self): + """wait for pow and shutdown the application""" + if self.myapp.quitAccepted and not self.myapp.wait: + return + + self.myapp.show() + self.myapp.raise_() + self.myapp.activateWindow() + + waitForPow = True + waitForConnection = False + waitForSync = False + if getPowType() == "python" and (bitmessageqt.powQueueSize() > 0 or pendingUpload() > 0): + waitForPow = False + if pendingDownload() > 0: + self.myapp.wait = waitForSync = True + if shared.statusIconColor == "red" and not BMConfigParser().safeGetBoolean( + "bitmessagesettings", "dontconnect" + ): + waitForConnection = True + self.myapp.wait = waitForSync = True + self.myapp.quitAccepted = True + self.myapp.updateStatusBar(_translate("MainWindow", "Shutting down PyBitmessage... %1%").arg(0)) + if waitForConnection: + self.myapp.updateStatusBar(_translate("MainWindow", "Waiting for network connection...")) + while shared.statusIconColor == "red": + time.sleep(0.5) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000) + if waitForSync: + self.myapp.updateStatusBar(_translate("MainWindow", "Waiting for finishing synchronisation...")) + while pendingDownload() > 0: + time.sleep(0.5) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000) + if waitForPow: + maxWorkerQueue = 0 + curWorkerQueue = bitmessageqt.powQueueSize() + while curWorkerQueue > 0: + curWorkerQueue = bitmessageqt.powQueueSize() + if curWorkerQueue > maxWorkerQueue: + maxWorkerQueue = curWorkerQueue + if curWorkerQueue > 0: + self.myapp.updateStatusBar( + _translate("MainWindow", "Waiting for PoW to finish... %1%").arg( + 50 * (maxWorkerQueue - curWorkerQueue) / maxWorkerQueue + ) + ) + time.sleep(0.5) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000) + self.myapp.updateStatusBar(_translate("MainWindow", "Shutting down Pybitmessage... %1%").arg(50)) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000) + if maxWorkerQueue > 0: + time.sleep(0.5) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000) + self.myapp.updateStatusBar(_translate("MainWindow", "Waiting for objects to be sent... %1%").arg(50)) + maxPendingUpload = max(1, pendingUpload()) + while pendingUpload() > 1: + self.myapp.updateStatusBar( + _translate("MainWindow", "Waiting for objects to be sent... %1%").arg( + int(50 + 20 * (pendingUpload() / maxPendingUpload)) + ) + ) + time.sleep(0.5) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000) + self.myapp.updateStatusBar(_translate("MainWindow", "Saving settings... %1%").arg(70)) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000) + self.myapp.saveSettings() + for attr, obj in self.myapp.ui.__dict__.iteritems(): + if hasattr(obj, "__class__") and isinstance(obj, settingsmixin.SettingsMixin): + saveMethod = getattr(obj, "saveSettings", None) + if callable(saveMethod): + obj.saveSettings() + self.myapp.updateStatusBar(_translate("MainWindow", "Shutting down core... %1%").arg(80)) + QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 1000) + shutdown.doCleanShutdown() + self.myapp.updateStatusBar(_translate("MainWindow", "Stopping notifications... %1%").arg(90)) + self.myapp.tray.hide() + self.myapp.updateStatusBar(_translate("MainWindow", "Shutdown imminent... %1%").arg(100)) + logger.info("Shutdown complete") + QtGui.qApp.closeAllWindows() diff --git a/src/graphicaltesting/test_settingwindow.py b/src/graphicaltesting/test_settingwindow.py index 4b8c0974..cd2ed89a 100644 --- a/src/graphicaltesting/test_settingwindow.py +++ b/src/graphicaltesting/test_settingwindow.py @@ -1,3 +1,4 @@ +"""Tests for setting window""" import random from random import choice from string import ascii_lowercase @@ -11,6 +12,8 @@ from testloader import BitmessageTestCase class BitmessageTest_SettingWindowTest(BitmessageTestCase): + """Switch to setting tab and test""" + def test_settingwindow(self): """Triggers the setting window""" self.myapp.ui.tabWidget.setCurrentWidget(self.myapp.ui.inbox) @@ -56,7 +59,7 @@ class BitmessageTest_SettingWindowTest(BitmessageTestCase): dialog.languageComboBox.setStyleSheet("QComboBox {background-color: #FF5733; color: white;}") QTest.qWait(50) dialog.languageComboBox.setStyleSheet("") - dialog.languageComboBox.setCurrentIndex(6) + dialog.languageComboBox.setCurrentIndex(5) QTest.qWait(1000) ok_btn = dialog.buttonBox.button(QtGui.QDialogButtonBox.Ok) QTest.mouseClick(ok_btn, Qt.LeftButton) diff --git a/src/graphicaltesting/testinitialization.py b/src/graphicaltesting/testinitialization.py index 0e1f3523..02cef716 100644 --- a/src/graphicaltesting/testinitialization.py +++ b/src/graphicaltesting/testinitialization.py @@ -1,3 +1,4 @@ +"""Trigger Test""" import unittest import test_addressgeneration @@ -7,24 +8,29 @@ import test_chans import test_messagesend import test_networkstatus import test_settingwindow +import test_quit from testloader import BitmessageTestCase def test_initialize(myapp): """Test Loader""" suite = unittest.TestSuite() - suite.addTest(BitmessageTestCase.bitmessage_testloader( - test_addressgeneration.BitmessageTest_AddressGeneration, myapp=myapp)) - suite.addTest(BitmessageTestCase.bitmessage_testloader( - test_messagesend.BitmessageTest_MessageTesting, myapp=myapp)) - suite.addTest(BitmessageTestCase.bitmessage_testloader( - test_addsubscription.BitmessageTest_AddSubscription, myapp=myapp)) - suite.addTest(BitmessageTestCase.bitmessage_testloader( - test_networkstatus.BitmessageTest_NetworkTest, myapp=myapp)) - suite.addTest(BitmessageTestCase.bitmessage_testloader( - test_blackwhitelist.BitmessageTest_BlackandWhiteList, myapp=myapp)) - suite.addTest(BitmessageTestCase.bitmessage_testloader( - test_chans.BitmessageTest_ChansTest, myapp=myapp)) - suite.addTest(BitmessageTestCase.bitmessage_testloader( - test_settingwindow.BitmessageTest_SettingWindowTest, myapp=myapp)) + suite.addTest( + BitmessageTestCase.bitmessage_testloader(test_addressgeneration.BitmessageTest_AddressGeneration, myapp=myapp) + ) + suite.addTest( + BitmessageTestCase.bitmessage_testloader(test_messagesend.BitmessageTest_MessageTesting, myapp=myapp) + ) + suite.addTest( + BitmessageTestCase.bitmessage_testloader(test_addsubscription.BitmessageTest_AddSubscription, myapp=myapp) + ) + suite.addTest(BitmessageTestCase.bitmessage_testloader(test_networkstatus.BitmessageTest_NetworkTest, myapp=myapp)) + suite.addTest( + BitmessageTestCase.bitmessage_testloader(test_blackwhitelist.BitmessageTest_BlackandWhiteList, myapp=myapp) + ) + suite.addTest(BitmessageTestCase.bitmessage_testloader(test_chans.BitmessageTest_ChansTest, myapp=myapp)) + suite.addTest( + BitmessageTestCase.bitmessage_testloader(test_settingwindow.BitmessageTest_SettingWindowTest, myapp=myapp) + ) + suite.addTest(BitmessageTestCase.bitmessage_testloader(test_quit.BitmessageTest_QuitTest, myapp=myapp)) unittest.TextTestRunner().run(suite) diff --git a/src/graphicaltesting/testloader.py b/src/graphicaltesting/testloader.py index 6a9b4f09..8d21dd6b 100644 --- a/src/graphicaltesting/testloader.py +++ b/src/graphicaltesting/testloader.py @@ -1,3 +1,4 @@ +"""Load Test""" import unittest diff --git a/src/gtesting.py b/src/gtesting.py index db4b2c11..56099b97 100644 --- a/src/gtesting.py +++ b/src/gtesting.py @@ -1,6 +1,13 @@ -import state +import os +import shutil if __name__ == "__main__": + APPNAME = "PyBitmessage" + if os.path.isdir(os.path.expanduser(os.path.join("~", ".config/" + APPNAME + "/"))): + shutil.rmtree(os.path.expanduser(os.path.join("~", ".config/" + APPNAME + "/"))) + else: + pass + import state state.qttesting = True print(" --------------------------------- Graphical Qt Testing --------------------------------- ") from bitmessagemain import main