summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/default/src/console_cmd/cutoff.c
blob: 00ce40660fd68872a4d3132545fa3a8ba97b450d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <zephyr/shell/shell.h>
#include <zephyr/ztest.h>

#include "battery.h"
#include "console.h"
#include "ec_commands.h"
#include "hooks.h"
#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"

static void console_cmd_cutoff_after(void *unused)
{
	ARG_UNUSED(unused);
	set_ac_enabled(true);
	hook_notify(HOOK_AC_CHANGE);
	k_msleep(500);
}

ZTEST_SUITE(console_cmd_cutoff, drivers_predicate_post_main, NULL, NULL,
	    console_cmd_cutoff_after, NULL);

ZTEST_USER(console_cmd_cutoff, test_sb_cutoff)
{
	int rv = shell_execute_cmd(get_ec_shell(), "cutoff");

	zassert_equal(EC_RES_SUCCESS, rv, "Expected %d, but got %d",
		      EC_RES_SUCCESS, rv);
	zassert_true(battery_is_cut_off(), NULL);
}

ZTEST_USER(console_cmd_cutoff, test_invalid_arg1)
{
	int rv = shell_execute_cmd(get_ec_shell(), "cutoff bad_arg");

	zassert_equal(EC_ERROR_INVAL, rv, "Expected %d, but got %d",
		      EC_ERROR_INVAL, rv);
	zassert_false(battery_is_cut_off(), NULL);
}

ZTEST_USER(console_cmd_cutoff, test_at_shutdown)
{
	int rv = shell_execute_cmd(get_ec_shell(), "cutoff at-shutdown");

	zassert_equal(EC_RES_SUCCESS, rv, "Expected %d, but got %d",
		      EC_RES_SUCCESS, rv);
	zassert_false(battery_is_cut_off(), NULL);
	hook_notify(HOOK_CHIPSET_SHUTDOWN);
	zassert_true(WAIT_FOR(battery_is_cut_off(), 1500000, k_msleep(250)),
		     NULL);
}

ZTEST_USER(console_cmd_cutoff, test_clear_pending_shutdown)
{
	int rv = shell_execute_cmd(get_ec_shell(), "cutoff at-shutdown");

	zassume_true(extpower_is_present(), NULL);
	zassert_equal(EC_RES_SUCCESS, rv, "Expected %d, but got %d",
		      EC_RES_SUCCESS, rv);

	/* Triggering the AC_CHANGE hook will cancel the pending cutoff */
	hook_notify(HOOK_AC_CHANGE);

	/* The shutdown will no longer cutoff the battery */
	hook_notify(HOOK_CHIPSET_SHUTDOWN);
	zassert_false(WAIT_FOR(battery_is_cut_off(), 1500000, k_msleep(250)),
		      NULL);
}

ZTEST_USER(console_cmd_cutoff, test_ac_change_exits_cutoff)
{
	int rv;

	set_ac_enabled(false);

	rv = shell_execute_cmd(get_ec_shell(), "cutoff");
	zassert_equal(EC_RES_SUCCESS, rv, "Expected %d, but got %d",
		      EC_RES_SUCCESS, rv);

	set_ac_enabled(true);
	zassert_false(battery_is_cut_off(), NULL);
}