feat: implement Open With functionality for file chooser integration

This commit is contained in:
claudi 2026-03-10 13:16:27 +01:00
parent 939c2f896f
commit a261de3460
2 changed files with 200 additions and 0 deletions

View file

@ -149,3 +149,74 @@ class TestMainWindowURLWhitelist:
# web_view should have allowed_urls configured
assert window.web_view.allowed_urls == sample_config.allowed_urls
class TestMainWindowOpenWith:
"""Test Open With chooser behavior."""
def test_open_with_app_chooser_windows(self, qtbot, sample_config):
"""Windows should use ShellExecuteW with the openas verb."""
window = MainWindow(sample_config)
qtbot.addWidget(window)
test_file = sample_config.allowed_roots[0] / "open_with_test.txt"
test_file.write_text("test")
with patch("webdrop_bridge.ui.main_window.sys.platform", "win32"):
with patch("ctypes.windll.shell32.ShellExecuteW", return_value=33) as mock_shell:
assert window._open_with_app_chooser(str(test_file)) is True
mock_shell.assert_called_once_with(
None,
"openas",
str(test_file),
None,
None,
1,
)
def test_open_with_app_chooser_windows_shellexecute_failure(self, qtbot, sample_config):
"""Windows should fall back to OpenAs_RunDLL when ShellExecuteW fails."""
window = MainWindow(sample_config)
qtbot.addWidget(window)
test_file = sample_config.allowed_roots[0] / "open_with_fallback.txt"
test_file.write_text("test")
with patch("webdrop_bridge.ui.main_window.sys.platform", "win32"):
with patch("ctypes.windll.shell32.ShellExecuteW", return_value=31):
with patch("webdrop_bridge.ui.main_window.subprocess.Popen") as mock_popen:
assert window._open_with_app_chooser(str(test_file)) is True
mock_popen.assert_called_once_with(
["rundll32.exe", "shell32.dll,OpenAs_RunDLL", str(test_file)]
)
def test_open_with_app_chooser_missing_file(self, qtbot, sample_config):
"""Missing files should fail before platform-specific invocation."""
window = MainWindow(sample_config)
qtbot.addWidget(window)
with patch("webdrop_bridge.ui.main_window.sys.platform", "win32"):
assert window._open_with_app_chooser("C:/tmp/does_not_exist.txt") is False
def test_open_with_app_chooser_macos_success(self, qtbot, sample_config):
"""macOS should return True when osascript exits successfully."""
window = MainWindow(sample_config)
qtbot.addWidget(window)
test_file = sample_config.allowed_roots[0] / "open_with_macos.txt"
test_file.write_text("test")
class _Result:
returncode = 0
with patch("webdrop_bridge.ui.main_window.sys.platform", "darwin"):
with patch("webdrop_bridge.ui.main_window.subprocess.run", return_value=_Result()):
assert window._open_with_app_chooser(str(test_file)) is True
def test_open_with_app_chooser_unsupported_platform(self, qtbot, sample_config):
"""Unsupported platforms should return False."""
window = MainWindow(sample_config)
qtbot.addWidget(window)
with patch("webdrop_bridge.ui.main_window.sys.platform", "linux"):
assert window._open_with_app_chooser("/tmp/test.txt") is False