Skip to content

Commit c6530b6

Browse files
test: add unit tests for headless file dialog mocks and stubs
1 parent f680f16 commit c6530b6

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ else() # NOT EMSCRIPTEN, ANDROID, or IOS
790790
tests/ui/test_screen.cpp
791791
tests/ui/test_pedal_board_chain.cpp
792792
tests/ui/test_pedal_board_menu.cpp
793+
tests/ui/test_file_dialog.cpp
793794
tests/ui/test_pedal_widget_knobs.cpp
794795
tests/unit/test_session_manager.cpp
795796
tests/unit/test_cli.cpp

tests/ui/test_file_dialog.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/**
2+
* @file test_file_dialog.cpp
3+
* @brief Tests for file dialog headless mock paths.
4+
*
5+
* Under AMPLITRON_HEADLESS the native dialog functions are replaced by
6+
* controllable mocks. These tests exercise every reachable code path
7+
* in file_dialog_native_open.cpp (the mock implementation), as well as
8+
* the stub implementations in file_dialog_native.cpp (save) and
9+
* file_dialog_native_folder.cpp (folder).
10+
*/
11+
12+
#include <string>
13+
14+
#include "gui/dialogs/file_dialog.h"
15+
#include "test_framework.h"
16+
17+
using namespace Amplitron;
18+
using namespace TestFramework;
19+
20+
// Forward-declare the mock setter (defined in file_dialog_native_open.cpp
21+
// under AMPLITRON_HEADLESS).
22+
namespace Amplitron {
23+
void set_mock_open_dialog_path(const std::string& path);
24+
}
25+
26+
// =============================================================================
27+
// show_open_dialog — headless mock
28+
// =============================================================================
29+
30+
TEST(file_dialog_open_returns_empty_by_default) {
31+
// Without setting a mock path, the dialog must return "".
32+
Amplitron::set_mock_open_dialog_path("");
33+
std::string result = show_open_dialog("Open File", "WAV Audio", "wav");
34+
ASSERT_EQ(result, "");
35+
}
36+
37+
TEST(file_dialog_open_returns_mock_path) {
38+
// After setting a mock path, show_open_dialog must return it verbatim.
39+
Amplitron::set_mock_open_dialog_path("/tmp/test_file.wav");
40+
std::string result = show_open_dialog("Open File", "WAV Audio", "wav");
41+
ASSERT_EQ(result, "/tmp/test_file.wav");
42+
}
43+
44+
TEST(file_dialog_open_ignores_title_and_filter_params) {
45+
// The mock path must be returned regardless of the title/filter arguments.
46+
Amplitron::set_mock_open_dialog_path("my_ir.wav");
47+
ASSERT_EQ(show_open_dialog("Load Impulse Response", "WAV Audio", "wav"), "my_ir.wav");
48+
ASSERT_EQ(show_open_dialog("Select File", "All Files", "*"), "my_ir.wav");
49+
ASSERT_EQ(show_open_dialog("", "", ""), "my_ir.wav");
50+
}
51+
52+
TEST(file_dialog_open_can_be_reset) {
53+
// Set a path, verify it is returned, reset, verify empty.
54+
Amplitron::set_mock_open_dialog_path("first.wav");
55+
ASSERT_EQ(show_open_dialog(), "first.wav");
56+
57+
Amplitron::set_mock_open_dialog_path("");
58+
ASSERT_EQ(show_open_dialog(), "");
59+
}
60+
61+
TEST(file_dialog_open_can_be_overwritten) {
62+
// Setting a new mock path overwrites the previous one.
63+
Amplitron::set_mock_open_dialog_path("a.wav");
64+
ASSERT_EQ(show_open_dialog(), "a.wav");
65+
66+
Amplitron::set_mock_open_dialog_path("b.wav");
67+
ASSERT_EQ(show_open_dialog(), "b.wav");
68+
}
69+
70+
TEST(file_dialog_open_default_params) {
71+
// Call with default arguments (title="Open File", desc="WAV Audio",
72+
// ext="wav").
73+
Amplitron::set_mock_open_dialog_path("default_args.wav");
74+
ASSERT_EQ(show_open_dialog(), "default_args.wav");
75+
}
76+
77+
TEST(file_dialog_open_unicode_path) {
78+
// The mock must handle paths with unicode characters.
79+
const std::string unicode_path = "/tmp/日本語ファイル.wav";
80+
Amplitron::set_mock_open_dialog_path(unicode_path);
81+
ASSERT_EQ(show_open_dialog("Open", "WAV", "wav"), unicode_path);
82+
}
83+
84+
TEST(file_dialog_open_path_with_spaces) {
85+
// The mock must handle paths containing spaces.
86+
const std::string spaced_path = "/my folder/my file.wav";
87+
Amplitron::set_mock_open_dialog_path(spaced_path);
88+
ASSERT_EQ(show_open_dialog("Open", "WAV", "wav"), spaced_path);
89+
}
90+
91+
TEST(file_dialog_open_path_with_special_chars) {
92+
// The mock must handle paths with special characters (quotes,
93+
// backslashes).
94+
const std::string special_path = R"(C:\Users\test\"file".wav)";
95+
Amplitron::set_mock_open_dialog_path(special_path);
96+
ASSERT_EQ(show_open_dialog("Open", "WAV", "wav"), special_path);
97+
}
98+
99+
TEST(file_dialog_open_consecutive_calls_same_result) {
100+
// Multiple consecutive calls without resetting should return the same mock
101+
// path.
102+
Amplitron::set_mock_open_dialog_path("stable.wav");
103+
ASSERT_EQ(show_open_dialog(), "stable.wav");
104+
ASSERT_EQ(show_open_dialog(), "stable.wav");
105+
ASSERT_EQ(show_open_dialog(), "stable.wav");
106+
}
107+
108+
// =============================================================================
109+
// show_save_dialog — headless stub (always returns "")
110+
// =============================================================================
111+
112+
TEST(file_dialog_save_returns_empty_in_headless) {
113+
std::string result = show_save_dialog("recording.wav", "WAV Audio", "wav");
114+
ASSERT_EQ(result, "");
115+
}
116+
117+
TEST(file_dialog_save_default_params) {
118+
// Call with default arguments.
119+
ASSERT_EQ(show_save_dialog(), "");
120+
}
121+
122+
TEST(file_dialog_save_custom_params) {
123+
// Custom arguments should still return "" in headless mode.
124+
ASSERT_EQ(show_save_dialog("my_preset.json", "Preset Files", "json"), "");
125+
}
126+
127+
// =============================================================================
128+
// show_folder_dialog — headless stub (always returns "")
129+
// =============================================================================
130+
131+
TEST(file_dialog_folder_returns_empty_in_headless) {
132+
std::string result = show_folder_dialog("Select Folder");
133+
ASSERT_EQ(result, "");
134+
}
135+
136+
TEST(file_dialog_folder_default_params) {
137+
// Call with default argument.
138+
ASSERT_EQ(show_folder_dialog(), "");
139+
}
140+
141+
TEST(file_dialog_folder_custom_title) { ASSERT_EQ(show_folder_dialog("Choose IR Directory"), ""); }

0 commit comments

Comments
 (0)