Skip to content

Commit 75354ec

Browse files
fix: make dialog_file_browser_set_basic_options initialize all fields (#2756)
* fix: make `dialog_file_browser_set_basic_options` initialize all fields * fix(GH-2756): use alternative test for `test_dialog_file_browser_set_basic_options_should_init_all_fields` Co-authored-by: Aleksandr Kutuzov <[email protected]>
1 parent 168fa72 commit 75354ec

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

CODING_STYLE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Almost everything in flipper firmware is built around this concept.
4848
# C coding style
4949

5050
- Tab is 4 spaces
51-
- Use `fbt format` to reformat source code and check style guide before commit
51+
- Use `./fbt format` to reformat source code and check style guide before commit
5252

5353
## Naming
5454

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <dialogs/dialogs.h>
2+
3+
#include "../minunit.h"
4+
5+
MU_TEST(test_dialog_file_browser_set_basic_options_should_init_all_fields) {
6+
mu_assert(
7+
sizeof(DialogsFileBrowserOptions) == 28,
8+
"Changes to `DialogsFileBrowserOptions` should also be reflected in `dialog_file_browser_set_basic_options`");
9+
10+
DialogsFileBrowserOptions options;
11+
dialog_file_browser_set_basic_options(&options, ".fap", NULL);
12+
// note: this assertions can safely be changed, their primary purpose is to remind the maintainer
13+
// to update `dialog_file_browser_set_basic_options` by including all structure fields in it
14+
mu_assert_string_eq(".fap", options.extension);
15+
mu_assert_null(options.base_path);
16+
mu_assert(options.skip_assets, "`skip_assets` should default to `true");
17+
mu_assert(options.hide_dot_files, "`hide_dot_files` should default to `true");
18+
mu_assert_null(options.icon);
19+
mu_assert(options.hide_ext, "`hide_ext` should default to `true");
20+
mu_assert_null(options.item_loader_callback);
21+
mu_assert_null(options.item_loader_context);
22+
}
23+
24+
MU_TEST_SUITE(dialogs_file_browser_options) {
25+
MU_RUN_TEST(test_dialog_file_browser_set_basic_options_should_init_all_fields);
26+
}
27+
28+
int run_minunit_test_dialogs_file_browser_options() {
29+
MU_RUN_SUITE(dialogs_file_browser_options);
30+
31+
return MU_EXIT_CODE;
32+
}

applications/debug/unit_tests/test_index.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ int run_minunit_test_nfc();
2727
int run_minunit_test_bit_lib();
2828
int run_minunit_test_float_tools();
2929
int run_minunit_test_bt();
30+
int run_minunit_test_dialogs_file_browser_options();
3031

3132
typedef int (*UnitTestEntry)();
3233

@@ -55,6 +56,8 @@ const UnitTest unit_tests[] = {
5556
{.name = "bit_lib", .entry = run_minunit_test_bit_lib},
5657
{.name = "float_tools", .entry = run_minunit_test_float_tools},
5758
{.name = "bt", .entry = run_minunit_test_bt},
59+
{.name = "dialogs_file_browser_options",
60+
.entry = run_minunit_test_dialogs_file_browser_options},
5861
};
5962

6063
void minunit_print_progress() {

applications/services/dialogs/dialogs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ void dialog_file_browser_set_basic_options(
99
const char* extension,
1010
const Icon* icon) {
1111
options->extension = extension;
12+
options->base_path = NULL;
1213
options->skip_assets = true;
14+
options->hide_dot_files = true;
1315
options->icon = icon;
1416
options->hide_ext = true;
1517
options->item_loader_callback = NULL;
1618
options->item_loader_context = NULL;
17-
options->base_path = NULL;
1819
}
1920

2021
static DialogsApp* dialogs_app_alloc() {

applications/services/dialogs/dialogs.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ typedef struct DialogsApp DialogsApp;
1616
/****************** FILE BROWSER ******************/
1717

1818
/**
19-
* File browser dialog extra options
19+
* File browser dialog extra options.
20+
* This can be default-initialized using {@link dialog_file_browser_set_basic_options}.
2021
* @param extension file extension to be offered for selection
2122
* @param base_path root folder path for navigation with back key
2223
* @param skip_assets true - do not show assets folders
@@ -38,8 +39,10 @@ typedef struct {
3839
} DialogsFileBrowserOptions;
3940

4041
/**
41-
* Initialize file browser dialog options
42-
* and set default values
42+
* Initialize file browser dialog options and set default values.
43+
* This is guaranteed to initialize all fields
44+
* so it is safe to pass pointer to uninitialized {@code options}
45+
* and assume that the data behind it becomes fully initialized after the call.
4346
* @param options pointer to options structure
4447
* @param extension file extension to filter
4548
* @param icon file icon pointer, NULL for default icon

0 commit comments

Comments
 (0)