-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GUI] add tests for global_prefs and log_report
- Loading branch information
Showing
3 changed files
with
128 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import pytest | ||
import os | ||
import tempfile | ||
|
||
from PyQt4.QtGui import QDialog | ||
from mock import Mock | ||
|
||
from mozregui import global_prefs | ||
|
||
|
||
@pytest.yield_fixture | ||
def write_default_conf(): | ||
default_file = global_prefs.DEFAULT_CONF_FNAME | ||
conf_file = tempfile.NamedTemporaryFile(delete=False) | ||
global_prefs.DEFAULT_CONF_FNAME = conf_file.name | ||
conf_file.close() | ||
|
||
def write_conf(text): | ||
with open(conf_file.name, 'w') as f: | ||
f.write(text) | ||
|
||
yield write_conf | ||
|
||
global_prefs.DEFAULT_CONF_FNAME = default_file | ||
os.unlink(conf_file.name) | ||
|
||
|
||
def test_change_prefs_dialog(write_default_conf, qtbot): | ||
write_default_conf(""" | ||
http-timeout = 32.1 | ||
persist-size-limit = 2.5 | ||
""") | ||
|
||
pref_dialog = global_prefs.ChangePrefsDialog() | ||
qtbot.add_widget(pref_dialog) | ||
pref_dialog.show() | ||
qtbot.waitForWindowShown(pref_dialog) | ||
|
||
# defaults are set | ||
assert str(pref_dialog.ui.persist.line_edit.text()) == '' | ||
assert pref_dialog.ui.http_timeout.value() == 32.1 | ||
assert pref_dialog.ui.persist_size_limit.value() == 2.5 | ||
|
||
# now let's change some values | ||
qtbot.keyClicks(pref_dialog.ui.persist.line_edit, "/path/to") | ||
|
||
# then save the prefs | ||
pref_dialog.save_prefs() | ||
|
||
# check they have been registered | ||
assert global_prefs.get_prefs().get('persist') == "/path/to" | ||
|
||
|
||
@pytest.mark.parametrize('dlg_result,saved', [ | ||
(QDialog.Accepted, True), | ||
(QDialog.Rejected, False), | ||
]) | ||
def test_change_prefs_dialog_saves_prefs(dlg_result, saved, mocker): | ||
Dlg = mocker.patch('mozregui.global_prefs.ChangePrefsDialog') | ||
dlg = Mock() | ||
Dlg.return_value = dlg | ||
dlg.exec_.return_value = dlg_result | ||
|
||
global_prefs.change_prefs_dialog() | ||
assert dlg.save_prefs.called == saved |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pytest | ||
import time | ||
|
||
from mozregui import log_report | ||
|
||
|
||
@pytest.fixture | ||
def log_view(qtbot): | ||
widget = log_report.LogView() | ||
qtbot.addWidget(widget) | ||
widget.log_model = log_report.LogModel() | ||
widget.log_model.log.connect(widget.on_log_received) | ||
widget.show() | ||
qtbot.waitForWindowShown(widget) | ||
return widget | ||
|
||
|
||
def test_log_report_report_log_line(log_view): | ||
# view is first empty | ||
assert str(log_view.toPlainText()) == '' | ||
assert log_view.blockCount() == 1 # 1 for an empty document | ||
|
||
# send a log line | ||
log_view.log_model({'message': 'my message', 'level': 'INFO', | ||
'time': time.time()}) | ||
|
||
assert log_view.blockCount() == 2 | ||
assert 'INFO : my message' in str(log_view.toPlainText()) | ||
|
||
|
||
def test_log_report_report_no_more_than_1000_lines(log_view): | ||
for i in range(1001): | ||
log_view.log_model({'message': str(i), 'level': 'INFO', | ||
'time': time.time()}) | ||
|
||
assert log_view.blockCount() == 1000 | ||
lines = str(log_view.toPlainText()).splitlines() | ||
assert len(lines) == 999 | ||
assert 'INFO : 1000' in lines[-1] | ||
assert 'INFO : 2' in lines[0] # 2 first lines were dropped |