]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
test: dm: add button_cmd test
authorCaleb Connolly <caleb.connolly@linaro.org>
Tue, 19 Mar 2024 13:24:42 +0000 (13:24 +0000)
committerTom Rini <trini@konsulko.com>
Wed, 20 Mar 2024 21:13:05 +0000 (17:13 -0400)
Add a test for the button_cmd feature. This validates that commands can
be mapped to two buttons, that the correct command runs based on which
button is pressed, that only 1 command is run, and that no command runs
if button_cmd_0_name is wrong or unset.

Additionally, fix a potential uninitialised variable use caught by these
tests, the btn variable in get_button_cmd() is assumed to be null if
button_get_by_label() fails, but it's actually used uninitialised in
that case.

CONFIG_BUTTON is now enabled automatically and was removed when running
save_defconfig.

Fixes: e761035b6423 ("boot: add support for button commands")
Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
common/button_cmd.c
configs/sandbox64_defconfig
configs/sandbox_defconfig
configs/sandbox_flattree_defconfig
test/dm/button.c

index b6a8434d6f29258ea75f5891bee93ca2310782c7..8642c26735ccb06e31eeb861261b3e8d3cfa93d7 100644 (file)
@@ -33,7 +33,7 @@ struct button_cmd {
 static int get_button_cmd(int n, struct button_cmd *cmd)
 {
        const char *cmd_str;
-       struct udevice *btn;
+       struct udevice *btn = NULL;
        char buf[24];
 
        snprintf(buf, sizeof(buf), "button_cmd_%d_name", n);
index 3be9a00a85755959acd9358bc930fd4f0a9f988d..a62faf772482db5a83dea8eb2841cf5742d2d60e 100644 (file)
@@ -11,6 +11,7 @@ CONFIG_SANDBOX64=y
 CONFIG_DEBUG_UART=y
 CONFIG_SYS_MEMTEST_START=0x00100000
 CONFIG_SYS_MEMTEST_END=0x00101000
+CONFIG_BUTTON_CMD=y
 CONFIG_FIT=y
 CONFIG_FIT_SIGNATURE=y
 CONFIG_FIT_VERBOSE=y
index 4ad10363e91bb4aa7a3f80caef1e6bf467f85762..93b52f2de5cf1b8372680aef1cf7a068bcc25aad 100644 (file)
@@ -10,6 +10,7 @@ CONFIG_PCI=y
 CONFIG_DEBUG_UART=y
 CONFIG_SYS_MEMTEST_START=0x00100000
 CONFIG_SYS_MEMTEST_END=0x00101000
+CONFIG_BUTTON_CMD=y
 CONFIG_FIT=y
 CONFIG_FIT_RSASSA_PSS=y
 CONFIG_FIT_CIPHER=y
index 0390186275276d64418ec717fd0049adf935f0e5..6bf8874e722eda127da024cb966436f66465f378 100644 (file)
@@ -8,6 +8,7 @@ CONFIG_PCI=y
 CONFIG_DEBUG_UART=y
 CONFIG_SYS_MEMTEST_START=0x00100000
 CONFIG_SYS_MEMTEST_END=0x00101000
+CONFIG_BUTTON_CMD=y
 CONFIG_FIT=y
 CONFIG_FIT_SIGNATURE=y
 CONFIG_FIT_VERBOSE=y
index 3318668df25abc8b6c6b4a91da0d02fd915657c1..830d96fbef345c5cdc4cff5294e0181d9e43217a 100644 (file)
@@ -131,3 +131,99 @@ static int dm_test_button_keys_adc(struct unit_test_state *uts)
        return 0;
 }
 DM_TEST(dm_test_button_keys_adc, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+/* Test of the button uclass using the button_gpio driver */
+static int dm_test_button_cmd(struct unit_test_state *uts)
+{
+       struct udevice *btn1_dev, *btn2_dev, *gpio;
+       const char *envstr;
+
+#define BTN1_GPIO 3
+#define BTN2_GPIO 4
+#define BTN1_PASS_VAR "test_button_cmds_0"
+#define BTN2_PASS_VAR "test_button_cmds_1"
+
+       /*
+        * Buttons 1 and 2 are connected to gpio_a gpios 3 and 4 respectively.
+        * set the GPIOs to known values and then check that the appropriate
+        * commands are run when invoking process_button_cmds().
+        */
+       ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &btn1_dev));
+       ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &btn2_dev));
+       ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
+
+       /*
+        * Map a command to button 1 and check that it process_button_cmds()
+        * runs it if called with button 1 pressed.
+        */
+       ut_assertok(env_set("button_cmd_0_name", "button1"));
+       ut_assertok(env_set("button_cmd_0", "env set " BTN1_PASS_VAR " PASS"));
+       ut_assertok(sandbox_gpio_set_value(gpio, BTN1_GPIO, 1));
+       /* Sanity check that the button is actually pressed */
+       ut_asserteq(BUTTON_ON, button_get_state(btn1_dev));
+       process_button_cmds();
+       ut_assertnonnull((envstr = env_get(BTN1_PASS_VAR)));
+       ut_asserteq_str(envstr, "PASS");
+
+       /* Clear result */
+       ut_assertok(env_set(BTN1_PASS_VAR, NULL));
+
+       /*
+        * Map a command for button 2, press it, check that only the command
+        * for button 1 runs because it comes first and is also pressed.
+        */
+       ut_assertok(env_set("button_cmd_1_name", "button2"));
+       ut_assertok(env_set("button_cmd_1", "env set " BTN2_PASS_VAR " PASS"));
+       ut_assertok(sandbox_gpio_set_value(gpio, BTN2_GPIO, 1));
+       ut_asserteq(BUTTON_ON, button_get_state(btn2_dev));
+       process_button_cmds();
+       /* Check that button 1 triggered again */
+       ut_assertnonnull((envstr = env_get(BTN1_PASS_VAR)));
+       ut_asserteq_str(envstr, "PASS");
+       /* And button 2 didn't */
+       ut_assertnull(env_get(BTN2_PASS_VAR));
+
+       /* Clear result */
+       ut_assertok(env_set(BTN1_PASS_VAR, NULL));
+
+       /*
+        * Release button 1 and check that the command for button 2 is run
+        */
+       ut_assertok(sandbox_gpio_set_value(gpio, BTN1_GPIO, 0));
+       process_button_cmds();
+       ut_assertnull(env_get(BTN1_PASS_VAR));
+       /* Check that the command for button 2 ran */
+       ut_assertnonnull((envstr = env_get(BTN2_PASS_VAR)));
+       ut_asserteq_str(envstr, "PASS");
+
+       /* Clear result */
+       ut_assertok(env_set(BTN2_PASS_VAR, NULL));
+
+       /*
+        * Unset "button_cmd_0_name" and check that no commands run even
+        * with both buttons pressed.
+        */
+       ut_assertok(env_set("button_cmd_0_name", NULL));
+       /* Press button 1 (button 2 is already pressed )*/
+       ut_assertok(sandbox_gpio_set_value(gpio, BTN1_GPIO, 1));
+       ut_asserteq(BUTTON_ON, button_get_state(btn1_dev));
+       process_button_cmds();
+       ut_assertnull(env_get(BTN1_PASS_VAR));
+       ut_assertnull(env_get(BTN2_PASS_VAR));
+
+       /*
+        * Check that no command is run if the button name is wrong.
+        */
+       ut_assertok(env_set("button_cmd_0_name", "invalid_button"));
+       process_button_cmds();
+       ut_assertnull(env_get(BTN1_PASS_VAR));
+       ut_assertnull(env_get(BTN2_PASS_VAR));
+
+#undef BTN1_PASS_VAR
+#undef BTN2_PASS_VAR
+#undef BTN1_GPIO
+#undef BTN2_GPIO
+
+       return 0;
+}
+DM_TEST(dm_test_button_cmd, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);