|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | from PySide6.QtCore import Qt |
2 | | -from PySide6.QtWidgets import QFileDialog |
| 4 | +from PySide6.QtWidgets import QDialog |
3 | 5 |
|
4 | 6 | from dlclivegui.gui.misc.weights_dialog import PoetWeightsDialog |
5 | 7 |
|
6 | 8 |
|
7 | | -def test_dialog_without_initial_path_disables_use_button( |
| 9 | +def test_dialog_shows_download_destination( |
| 10 | + qtbot, |
| 11 | +) -> None: |
| 12 | + dialog = PoetWeightsDialog() |
| 13 | + qtbot.addWidget(dialog) |
| 14 | + |
| 15 | + assert dialog.path_label.full_text == str(dialog._destination) |
| 16 | + assert dialog.path_label.text() |
| 17 | + assert dialog.path_label.textInteractionFlags() & Qt.TextInteractionFlag.TextSelectableByMouse |
| 18 | + assert dialog.status_label.text() == "Ready to download." |
| 19 | + assert dialog.progress_bar.value() == 0 |
| 20 | + assert dialog.download_button.isEnabled() |
| 21 | + assert dialog.close_button.isEnabled() |
| 22 | + |
| 23 | + |
| 24 | +def test_update_progress_updates_progress_and_status( |
8 | 25 | qtbot, |
9 | 26 | ) -> None: |
10 | 27 | dialog = PoetWeightsDialog() |
11 | 28 | qtbot.addWidget(dialog) |
12 | 29 |
|
13 | | - assert dialog.path_label.text() == "No weights selected" |
14 | | - assert not dialog.use_button.isEnabled() |
15 | | - assert not dialog.progress_bar.isVisible() |
| 30 | + dialog._update_progress(42) |
16 | 31 |
|
| 32 | + assert dialog.progress_bar.value() == 42 |
| 33 | + assert dialog.status_label.text() == "Downloading POET weights... 42%" |
17 | 34 |
|
18 | | -def test_dialog_accepts_valid_initial_path( |
| 35 | + |
| 36 | +def test_download_finished_records_completed_path( |
19 | 37 | qtbot, |
20 | 38 | tmp_path, |
21 | 39 | ) -> None: |
22 | 40 | path = tmp_path / "weights.pth" |
23 | 41 | path.touch() |
24 | 42 |
|
25 | | - dialog = PoetWeightsDialog(initial_path=str(path)) |
| 43 | + dialog = PoetWeightsDialog() |
26 | 44 | qtbot.addWidget(dialog) |
27 | 45 |
|
28 | | - assert dialog.path_label.text() == str(path) |
29 | | - assert dialog.use_button.isEnabled() |
| 46 | + dialog._download_finished(str(path)) |
| 47 | + |
| 48 | + assert dialog._completed_path == str(path) |
| 49 | + assert dialog.progress_bar.value() == 100 |
| 50 | + assert dialog.status_label.text() == ("Download complete. Returning to the main window...") |
30 | 51 |
|
31 | 52 |
|
32 | | -def test_use_selected_emits_path( |
| 53 | +def test_download_cleanup_emits_path_and_accepts( |
33 | 54 | qtbot, |
34 | 55 | tmp_path, |
35 | 56 | ) -> None: |
36 | 57 | path = tmp_path / "weights.pth" |
37 | 58 | path.touch() |
38 | 59 |
|
39 | | - dialog = PoetWeightsDialog(initial_path=str(path)) |
| 60 | + dialog = PoetWeightsDialog() |
40 | 61 | qtbot.addWidget(dialog) |
| 62 | + dialog._completed_path = str(path) |
41 | 63 |
|
42 | 64 | with qtbot.waitSignal( |
43 | | - dialog.weights_selected, |
| 65 | + dialog.weights_downloaded, |
44 | 66 | timeout=1000, |
45 | 67 | ) as blocker: |
46 | | - qtbot.mouseClick( |
47 | | - dialog.use_button, |
48 | | - Qt.MouseButton.LeftButton, |
49 | | - ) |
| 68 | + dialog._download_cleanup() |
50 | 69 |
|
51 | 70 | assert blocker.args == [str(path)] |
| 71 | + assert dialog.result() == QDialog.DialogCode.Accepted |
| 72 | + assert dialog._download_thread is None |
| 73 | + assert dialog._download_worker is None |
52 | 74 |
|
53 | 75 |
|
54 | | -def test_browse_sets_selected_path( |
| 76 | +def test_download_cleanup_after_failure_restores_controls( |
55 | 77 | qtbot, |
56 | | - monkeypatch, |
57 | | - tmp_path, |
58 | 78 | ) -> None: |
59 | | - path = tmp_path / "weights.pth" |
60 | | - path.touch() |
| 79 | + dialog = PoetWeightsDialog() |
| 80 | + qtbot.addWidget(dialog) |
61 | 81 |
|
62 | | - monkeypatch.setattr( |
63 | | - QFileDialog, |
64 | | - "getOpenFileName", |
65 | | - lambda *args, **kwargs: ( |
66 | | - str(path), |
67 | | - "POET weights (*.pt *.pth)", |
68 | | - ), |
69 | | - ) |
| 82 | + dialog.download_button.setEnabled(False) |
| 83 | + dialog.close_button.setEnabled(False) |
| 84 | + dialog._completed_path = None |
| 85 | + |
| 86 | + dialog._download_cleanup() |
| 87 | + |
| 88 | + assert dialog.download_button.isEnabled() |
| 89 | + assert dialog.close_button.isEnabled() |
70 | 90 |
|
| 91 | + |
| 92 | +def test_reject_is_ignored_while_download_is_active( |
| 93 | + qtbot, |
| 94 | +) -> None: |
71 | 95 | dialog = PoetWeightsDialog() |
72 | 96 | qtbot.addWidget(dialog) |
73 | 97 |
|
74 | | - dialog._browse() |
| 98 | + dialog._download_thread = object() |
| 99 | + |
| 100 | + dialog.reject() |
75 | 101 |
|
76 | | - assert dialog.path_label.text() == str(path) |
77 | | - assert dialog.use_button.isEnabled() |
| 102 | + assert dialog.result() == 0 |
78 | 103 |
|
79 | 104 |
|
80 | | -def test_download_cleanup_restores_controls( |
| 105 | +def test_reject_closes_when_idle( |
81 | 106 | qtbot, |
82 | | - tmp_path, |
83 | 107 | ) -> None: |
84 | | - path = tmp_path / "weights.pth" |
85 | | - path.touch() |
86 | | - |
87 | 108 | dialog = PoetWeightsDialog() |
88 | 109 | qtbot.addWidget(dialog) |
89 | 110 |
|
90 | | - dialog._set_selected_path(str(path)) |
91 | | - dialog.browse_button.setEnabled(False) |
92 | | - dialog.download_button.setEnabled(False) |
93 | | - |
94 | | - dialog._download_cleanup() |
| 111 | + dialog.reject() |
95 | 112 |
|
96 | | - assert dialog.browse_button.isEnabled() |
97 | | - assert dialog.download_button.isEnabled() |
98 | | - assert dialog.use_button.isEnabled() |
| 113 | + assert dialog.result() == QDialog.DialogCode.Rejected |
0 commit comments