From e81dcccacef3c241361a6bcf98e3038ec9bd53c0 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 10 Jul 2017 08:00:09 +0200 Subject: [PATCH] Add a test for a None currentWidget with backforward widget --- .../mainwindow/statusbar/backforward.py | 4 +++- tests/helpers/stubs.py | 5 ++++- .../mainwindow/statusbar/test_backforward.py | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/qutebrowser/mainwindow/statusbar/backforward.py b/qutebrowser/mainwindow/statusbar/backforward.py index 9ed18ffb0..233cb53f8 100644 --- a/qutebrowser/mainwindow/statusbar/backforward.py +++ b/qutebrowser/mainwindow/statusbar/backforward.py @@ -29,7 +29,9 @@ class Backforward(textbase.TextBase): def on_tab_cur_url_changed(self, tabs): """Called on URL changes.""" tab = tabs.currentWidget() - if tab is None: # pragma: no cover + if tab is None: + self.setText('') + self.hide() return text = '' if tab.history.can_go_back(): diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index dc49d8fec..368fe33e0 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -620,7 +620,10 @@ class TabbedBrowserStub(QObject): return self.current_index def currentWidget(self): - return self.tabs[self.currentIndex() - 1] + idx = self.currentIndex() + if idx == -1: + return None + return self.tabs[idx - 1] def tabopen(self, url): self.opened_url = url diff --git a/tests/unit/mainwindow/statusbar/test_backforward.py b/tests/unit/mainwindow/statusbar/test_backforward.py index bf37cd55f..f2dec3d3f 100644 --- a/tests/unit/mainwindow/statusbar/test_backforward.py +++ b/tests/unit/mainwindow/statusbar/test_backforward.py @@ -56,3 +56,21 @@ def test_backforward_widget(backforward_widget, stubs, backforward_widget.on_tab_cur_url_changed(tabbed_browser) assert backforward_widget.text() == '' assert not backforward_widget.isVisible() + + +def test_none_tab(backforward_widget, stubs, fake_web_tab): + """Make sure nothing crashes when passing None as tab.""" + tab = fake_web_tab(can_go_back=True, can_go_forward=True) + tabbed_browser = stubs.TabbedBrowserStub() + tabbed_browser.current_index = 1 + tabbed_browser.tabs = [tab] + backforward_widget.on_tab_cur_url_changed(tabbed_browser) + + assert backforward_widget.text() == '[<>]' + assert backforward_widget.isVisible() + + tabbed_browser.current_index = -1 + backforward_widget.on_tab_cur_url_changed(tabbed_browser) + + assert backforward_widget.text() == '' + assert not backforward_widget.isVisible()