summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/default/src/charge_state_prevent_power_on.c
blob: ce47ff804285466ab4f1699d0d9e784a54e5e0ec (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
87
88
89
90
91
/* 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 "charge_state.h"
#include "charge_state_v2.h"
#include "test/drivers/test_state.h"

#include <zephyr/ztest.h>

/* Test external variable defined in charge_state_v2 */
extern int charge_prevent_power_on_automatic_power_on;

struct charge_state_prevent_power_on_fixture {
	struct charge_state_data charge_state_backup;
	int automatic_power_on;
};

static void *setup(void)
{
	static struct charge_state_prevent_power_on_fixture fixture;

	return &fixture;
}

static void before(void *f)
{
	struct charge_state_prevent_power_on_fixture *fixture = f;

	/* Backup the current state */
	fixture->charge_state_backup = *charge_get_status();
	fixture->automatic_power_on =
		charge_prevent_power_on_automatic_power_on;

	/* Reset the automatic_power_on global */
	charge_prevent_power_on_automatic_power_on = 1;
}

static void after(void *f)
{
	struct charge_state_prevent_power_on_fixture *fixture = f;

	/* Restore the state from 'before' */
	*charge_get_status() = fixture->charge_state_backup;
	charge_prevent_power_on_automatic_power_on =
		fixture->automatic_power_on;
}

ZTEST_SUITE(charge_state_prevent_power_on, drivers_predicate_post_main, setup,
	    before, after, NULL);

ZTEST(charge_state_prevent_power_on, test_allow_power_on)
{
	struct batt_params *params = &charge_get_status()->batt;

	/* Force a call to refresh the battery parameters */
	params->is_present = BP_NOT_SURE;
	/* Set the charge state to be high enough */
	params->state_of_charge =
		CONFIG_PLATFORM_EC_CHARGER_MIN_BAT_PCT_FOR_POWER_ON;

	/* Verify that we can power on when the power button was pressed */
	zassert_false(charge_prevent_power_on(true));
}

ZTEST(charge_state_prevent_power_on, test_low_charge)
{
	struct batt_params *params = &charge_get_status()->batt;

	/* Force a low charge state */
	params->state_of_charge =
		CONFIG_PLATFORM_EC_CHARGER_MIN_BAT_PCT_FOR_POWER_ON - 1;

	/* Verify that we cannot power on during an automatic power-on */
	zassert_true(charge_prevent_power_on(false));
}

ZTEST(charge_state_prevent_power_on, test_consuming_full_input_current)
{
	struct batt_params *params = &charge_get_status()->batt;

	params->state_of_charge = 50;
	zassert_true(charge_is_consuming_full_input_current());

	params->state_of_charge = 0;
	zassert_false(charge_is_consuming_full_input_current());

	params->state_of_charge = 100;
	zassert_false(charge_is_consuming_full_input_current());
}