diff --git a/onionshare_gui/tab/tab.py b/onionshare_gui/tab/tab.py index 53dc4ad5..4bc45ac9 100644 --- a/onionshare_gui/tab/tab.py +++ b/onionshare_gui/tab/tab.py @@ -142,6 +142,19 @@ class Tab(QtWidgets.QWidget): ) self.persistent_image_label.setFixedSize(20, 20) + # Create the close warning dialog -- the dialog widget needs to be in the constructor + # in order to test it + self.close_dialog = QtWidgets.QMessageBox() + self.close_dialog.setWindowTitle(strings._("gui_close_tab_warning_title")) + self.close_dialog.setIcon(QtWidgets.QMessageBox.Critical) + self.close_dialog.accept_button = self.close_dialog.addButton( + strings._("gui_close_tab_warning_close"), QtWidgets.QMessageBox.AcceptRole + ) + self.close_dialog.reject_button = self.close_dialog.addButton( + strings._("gui_close_tab_warning_cancel"), QtWidgets.QMessageBox.RejectRole + ) + self.close_dialog.setDefaultButton(self.close_dialog.reject_button) + def init(self, mode_settings=None): if mode_settings: # Load this tab @@ -538,21 +551,11 @@ class Tab(QtWidgets.QWidget): # Open the warning dialog self.common.log("Tab", "close_tab, opening warning dialog") - dialog = QtWidgets.QMessageBox() - dialog.setWindowTitle(strings._("gui_close_tab_warning_title")) - dialog.setText(dialog_text) - dialog.setIcon(QtWidgets.QMessageBox.Critical) - dialog.addButton( - strings._("gui_close_tab_warning_close"), QtWidgets.QMessageBox.YesRole - ) - cancel_button = dialog.addButton( - strings._("gui_close_tab_warning_cancel"), QtWidgets.QMessageBox.NoRole - ) - dialog.setDefaultButton(cancel_button) - reply = dialog.exec_() + self.close_dialog.setText(dialog_text) + self.close_dialog.exec_() # Close - if reply == 0: + if self.close_dialog.clickedButton() == self.close_dialog.accept_button: self.common.log("Tab", "close_tab", "close, closing tab") self.get_mode().stop_server() self.app.cleanup() diff --git a/tests2/test_tabs.py b/tests2/test_tabs.py index 8e8bf775..9606b33c 100644 --- a/tests2/test_tabs.py +++ b/tests2/test_tabs.py @@ -166,8 +166,6 @@ class TestTabs(unittest.TestCase): @pytest.mark.gui def test_010_close_share_tab_while_server_started_should_warn(self): """Closing a share mode tab when the server is running should throw a warning""" - pass - """ tab = self.gui.tabs.widget(0) # Share files @@ -197,34 +195,33 @@ class TestTabs(unittest.TestCase): tab.share_mode.server_status.STATUS_STARTED, ) + # Prepare to reject the dialog + QtCore.QTimer.singleShot(1000, tab.close_dialog.reject_button.click) + # Close tab QtTest.QTest.mouseClick( self.gui.tabs.tabBar().tabButton(0, QtWidgets.QTabBar.RightSide), QtCore.Qt.LeftButton, ) + QtTest.QTest.qWait(1000) - # The active window should now be a dialog - dialog = self.gui.qtapp.activeWindow() - self.assertEqual(type(dialog), QtWidgets.QMessageBox) - - # Reject it -- the share mode tab should still be open - dialog.reject() + # The tab should still be open self.assertFalse(tab.new_tab.isVisible()) self.assertTrue(tab.share_mode.isVisible()) + # Prepare to accept the dialog + QtCore.QTimer.singleShot(1000, tab.close_dialog.accept_button.click) + # Close tab QtTest.QTest.mouseClick( self.gui.tabs.tabBar().tabButton(0, QtWidgets.QTabBar.RightSide), QtCore.Qt.LeftButton, ) + QtTest.QTest.qWait(1000) - # This time accept it -- the share mode tab should be closed - dialog = self.gui.qtapp.activeWindow() - self.assertEqual(type(dialog), QtWidgets.QMessageBox) - dialog.accept() - + # The tab should be closed + # QtTest.QTest.qWait(5000) self.assertTrue(self.gui.tabs.widget(0).new_tab.isVisible()) - """ if __name__ == "__main__":