|
1 | 1 | #!/usr/bin/env python |
2 | 2 | from pathlib import Path |
| 3 | +import shutil |
3 | 4 | from unittest import mock |
4 | 5 |
|
| 6 | +from PySide6 import QtCore |
5 | 7 | from PySide6.QtGui import QKeySequence |
6 | 8 | from PySide6.QtTest import QTest |
7 | 9 | from PySide6.QtWidgets import QApplication, QTreeWidgetItemIterator |
@@ -35,6 +37,10 @@ def setUp(self): |
35 | 37 |
|
36 | 38 | def destroyMW(self): |
37 | 39 | if self.mw: |
| 40 | + for i in range(self.mw.files.count()): |
| 41 | + widget = self.mw.files.widget(i) |
| 42 | + if widget is not None: |
| 43 | + widget.clear_windows(is_closing=True) |
38 | 44 | self.mw.close() |
39 | 45 | self.mw.deleteLater() |
40 | 46 |
|
@@ -216,3 +222,162 @@ def test_create_plot__numeric__tabular_sub_window_shortcut(self): |
216 | 222 | self.assertEqual(len(matrix_items), signals.columnHeader.model().columnCount() - 1) |
217 | 223 | for key in matrix_items.values(): |
218 | 224 | self.assertIn(key, signals.pgdf.df.keys()) |
| 225 | + |
| 226 | + |
| 227 | +class TestReplaceFile(TestBase): |
| 228 | + @safe_setup |
| 229 | + def setUp(self): |
| 230 | + super().setUp() |
| 231 | + # Create two copies of the test file so we have distinct paths |
| 232 | + self.measurement_file = str( |
| 233 | + shutil.copy(Path(TestBase.resource, "ASAP2_Demo_V171.mf4"), Path(self.test_workspace, "original.mf4")) |
| 234 | + ) |
| 235 | + self.replacement_file = str( |
| 236 | + shutil.copy(Path(TestBase.resource, "ASAP2_Demo_V171.mf4"), Path(self.test_workspace, "replacement.mf4")) |
| 237 | + ) |
| 238 | + self.mw = None |
| 239 | + |
| 240 | + def tearDown(self): |
| 241 | + if self.mw: |
| 242 | + for i in range(self.mw.files.count()): |
| 243 | + widget = self.mw.files.widget(i) |
| 244 | + if widget is not None: |
| 245 | + widget.clear_windows(is_closing=True) |
| 246 | + self.mw.close() |
| 247 | + self.mw.deleteLater() |
| 248 | + super().tearDown() |
| 249 | + |
| 250 | + def test_replace_file_preserves_tab_position(self): |
| 251 | + """ |
| 252 | + Test scope: |
| 253 | + Ensure that File > Replace replaces the current file tab in place. |
| 254 | + Events: |
| 255 | + - Open MainWindow with a measurement file |
| 256 | + - Trigger Replace via Ctrl+Shift+O with mocked file dialog |
| 257 | + Evaluate: |
| 258 | + - Tab count remains 1 |
| 259 | + - Tab label matches replacement file name |
| 260 | + - Tab tooltip matches replacement file path |
| 261 | + - FileWidget is valid |
| 262 | + """ |
| 263 | + self.mw = MainWindow(files=(self.measurement_file,)) |
| 264 | + self.mw.showNormal() |
| 265 | + self.processEvents(1) |
| 266 | + |
| 267 | + self.assertEqual(self.mw.files.count(), 1) |
| 268 | + self.assertEqual(self.mw.files.tabText(0), "original.mf4") |
| 269 | + |
| 270 | + with mock.patch("asammdf.gui.widgets.main.QtWidgets.QFileDialog.getOpenFileName") as mo_dialog: |
| 271 | + mo_dialog.return_value = self.replacement_file, None |
| 272 | + QTest.keySequence(self.mw, QKeySequence("Ctrl+Shift+O")) |
| 273 | + self.processEvents(1) |
| 274 | + |
| 275 | + # Tab count should still be 1 |
| 276 | + self.assertEqual(self.mw.files.count(), 1) |
| 277 | + # Tab label and tooltip should reflect the new file |
| 278 | + self.assertEqual(self.mw.files.tabText(0), "replacement.mf4") |
| 279 | + self.assertIn("replacement.mf4", self.mw.files.tabToolTip(0)) |
| 280 | + # Widget should be a valid FileWidget |
| 281 | + self.assertIsInstance(self.mw.files.widget(0), FileWidget) |
| 282 | + |
| 283 | + def test_replace_file_preserves_display_config(self): |
| 284 | + """ |
| 285 | + Test scope: |
| 286 | + Ensure that File > Replace preserves the display configuration (sub-windows) |
| 287 | + from the old file and applies it to the new file. |
| 288 | + Events: |
| 289 | + - Open MainWindow with a measurement file |
| 290 | + - Create a Plot sub-window with selected channels |
| 291 | + - Trigger Replace via Ctrl+Shift+O with mocked file dialog |
| 292 | + Evaluate: |
| 293 | + - New file has the same number of sub-windows |
| 294 | + - Sub-window type is Plot |
| 295 | + - Plot contains the same channels |
| 296 | + """ |
| 297 | + self.mw = MainWindow(files=(self.measurement_file,)) |
| 298 | + self.mw.showNormal() |
| 299 | + self.processEvents(1) |
| 300 | + |
| 301 | + file_widget = self.mw.files.widget(0) |
| 302 | + |
| 303 | + # Select some channels and create a Plot window |
| 304 | + channel_names = [] |
| 305 | + iterator = QTreeWidgetItemIterator(file_widget.channels_tree) |
| 306 | + count = 0 |
| 307 | + while item := iterator.value(): |
| 308 | + if item.parent() is not None and count < 3: |
| 309 | + item.setCheckState(0, QtCore.Qt.CheckState.Checked) |
| 310 | + channel_names.append(item.text(0)) |
| 311 | + count += 1 |
| 312 | + iterator += 1 |
| 313 | + |
| 314 | + self.assertGreater(len(channel_names), 0, "No channels found to select") |
| 315 | + |
| 316 | + # Create a Plot window directly (F2 shortcut is tested separately in TestShortcuts) |
| 317 | + file_widget._create_window(None, "Plot") |
| 318 | + self.processEvents(0.5) |
| 319 | + |
| 320 | + self.assertEqual(len(file_widget.mdi_area.subWindowList()), 1) |
| 321 | + |
| 322 | + with mock.patch("asammdf.gui.widgets.main.QtWidgets.QFileDialog.getOpenFileName") as mo_dialog: |
| 323 | + mo_dialog.return_value = self.replacement_file, None |
| 324 | + QTest.keySequence(self.mw, QKeySequence("Ctrl+Shift+O")) |
| 325 | + self.processEvents(1) |
| 326 | + |
| 327 | + new_widget = self.mw.files.widget(0) |
| 328 | + self.assertIsInstance(new_widget, FileWidget) |
| 329 | + # Verify sub-windows were recreated |
| 330 | + sub_windows = new_widget.mdi_area.subWindowList() |
| 331 | + self.assertEqual(len(sub_windows), 1) |
| 332 | + self.assertIsInstance(sub_windows[0].widget(), plot.Plot) |
| 333 | + |
| 334 | + def test_replace_file_no_op_when_no_file_open(self): |
| 335 | + """ |
| 336 | + Test scope: |
| 337 | + Ensure that Replace does nothing when no file tab is open. |
| 338 | + Events: |
| 339 | + - Open MainWindow with no files |
| 340 | + - Trigger Replace via Ctrl+Shift+O |
| 341 | + Evaluate: |
| 342 | + - Tab count remains 0 |
| 343 | + - No errors raised |
| 344 | + """ |
| 345 | + self.mw = MainWindow() |
| 346 | + self.mw.showNormal() |
| 347 | + self.processEvents(0.5) |
| 348 | + |
| 349 | + self.assertEqual(self.mw.files.count(), 0) |
| 350 | + |
| 351 | + with mock.patch("asammdf.gui.widgets.main.QtWidgets.QFileDialog.getOpenFileName") as mo_dialog: |
| 352 | + QTest.keySequence(self.mw, QKeySequence("Ctrl+Shift+O")) |
| 353 | + self.processEvents(0.5) |
| 354 | + # Dialog should never be shown |
| 355 | + mo_dialog.assert_not_called() |
| 356 | + |
| 357 | + self.assertEqual(self.mw.files.count(), 0) |
| 358 | + |
| 359 | + def test_replace_file_no_op_when_dialog_cancelled(self): |
| 360 | + """ |
| 361 | + Test scope: |
| 362 | + Ensure that Replace does nothing when the user cancels the file dialog. |
| 363 | + Events: |
| 364 | + - Open MainWindow with a measurement file |
| 365 | + - Trigger Replace but return empty string from dialog (user cancelled) |
| 366 | + Evaluate: |
| 367 | + - Original file tab is unchanged |
| 368 | + """ |
| 369 | + self.mw = MainWindow(files=(self.measurement_file,)) |
| 370 | + self.mw.showNormal() |
| 371 | + self.processEvents(1) |
| 372 | + |
| 373 | + self.assertEqual(self.mw.files.count(), 1) |
| 374 | + self.assertEqual(self.mw.files.tabText(0), "original.mf4") |
| 375 | + |
| 376 | + with mock.patch("asammdf.gui.widgets.main.QtWidgets.QFileDialog.getOpenFileName") as mo_dialog: |
| 377 | + mo_dialog.return_value = "", None |
| 378 | + QTest.keySequence(self.mw, QKeySequence("Ctrl+Shift+O")) |
| 379 | + self.processEvents(0.5) |
| 380 | + |
| 381 | + # Original tab should be unchanged |
| 382 | + self.assertEqual(self.mw.files.count(), 1) |
| 383 | + self.assertEqual(self.mw.files.tabText(0), "original.mf4") |
0 commit comments