summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/common_charger/src/test_common_charger_mocked.c
blob: 060f0cf74817aa78c8621700eb6953729e97790d (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* 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 <string.h>
#include <zephyr/fff.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
#include <zephyr/ztest_assert.h>

#include "charger.h"
#include "test/drivers/charger_utils.h"
#include "test/drivers/test_state.h"

/* This test suite only works if the chg_chips array is not const. */
BUILD_ASSERT(IS_ENABLED(CONFIG_PLATFORM_EC_CHARGER_RUNTIME_CONFIG),
	     "chg_chips array cannot be const.");

/** Index of the charger chip we are overriding / working with. */
#define CHG_NUM (0)

/* FFF fakes for driver functions. These get assigned to members of the
 * charger_drv struct
 */
FAKE_VALUE_FUNC(enum ec_error_list, enable_otg_power, int, int);
FAKE_VALUE_FUNC(enum ec_error_list, set_otg_current_voltage, int, int, int);
FAKE_VALUE_FUNC(int, is_sourcing_otg_power, int, int);
FAKE_VALUE_FUNC(enum ec_error_list, get_actual_current, int, int *);

struct common_charger_mocked_driver_fixture {
	/* The original driver pointer that gets restored after the tests */
	const struct charger_drv *saved_driver_ptr;
	/* Mock driver that gets substituted */
	struct charger_drv mock_driver;
};

ZTEST(common_charger_mocked_driver, test_charger_enable_otg_power__invalid)
{
	/* charger number out of bounds */
	zassert_equal(EC_ERROR_INVAL, charger_enable_otg_power(-1, 0));
	zassert_equal(EC_ERROR_INVAL, charger_enable_otg_power(INT_MAX, 0));
}

ZTEST(common_charger_mocked_driver, test_charger_enable_otg_power__unimpl)
{
	/* enable_otg_power is NULL */
	zassert_equal(EC_ERROR_UNIMPLEMENTED,
		      charger_enable_otg_power(CHG_NUM, 1));
}

ZTEST_F(common_charger_mocked_driver, test_charger_enable_otg_power)
{
	fixture->mock_driver.enable_otg_power = enable_otg_power;
	enable_otg_power_fake.return_val = 123;

	zassert_equal(enable_otg_power_fake.return_val,
		      charger_enable_otg_power(CHG_NUM, 1));

	zassert_equal(1, enable_otg_power_fake.call_count);
	zassert_equal(CHG_NUM, enable_otg_power_fake.arg0_history[0]);
	zassert_equal(1, enable_otg_power_fake.arg1_history[0]);
}

ZTEST(common_charger_mocked_driver,
      test_charger_set_otg_current_voltage__invalid)
{
	/* charger number out of bounds */
	zassert_equal(EC_ERROR_INVAL,
		      charger_set_otg_current_voltage(-1, 0, 0));
	zassert_equal(EC_ERROR_INVAL,
		      charger_set_otg_current_voltage(INT_MAX, 0, 0));
}

ZTEST(common_charger_mocked_driver,
      test_charger_set_otg_current_voltage__unimpl)
{
	/* set_otg_current_voltage is NULL */
	zassert_equal(EC_ERROR_UNIMPLEMENTED,
		      charger_set_otg_current_voltage(CHG_NUM, 0, 0));
}

ZTEST_F(common_charger_mocked_driver, test_charger_set_otg_current_voltage)
{
	fixture->mock_driver.set_otg_current_voltage = set_otg_current_voltage;
	set_otg_current_voltage_fake.return_val = 123;

	zassert_equal(set_otg_current_voltage_fake.return_val,
		      charger_set_otg_current_voltage(CHG_NUM, 10, 20));

	zassert_equal(1, set_otg_current_voltage_fake.call_count);
	zassert_equal(CHG_NUM, set_otg_current_voltage_fake.arg0_history[0]);
	zassert_equal(10, set_otg_current_voltage_fake.arg1_history[0]);
	zassert_equal(20, set_otg_current_voltage_fake.arg2_history[0]);
}

ZTEST(common_charger_mocked_driver, test_charger_is_sourcing_otg_power__invalid)
{
	/* is_sourcing_otg_power is NULL */
	zassert_equal(0, charger_is_sourcing_otg_power(0));
}

ZTEST_F(common_charger_mocked_driver, test_charger_is_sourcing_otg_power)
{
	fixture->mock_driver.is_sourcing_otg_power = is_sourcing_otg_power;
	is_sourcing_otg_power_fake.return_val = 123;

	zassert_equal(is_sourcing_otg_power_fake.return_val,
		      charger_is_sourcing_otg_power(0));

	zassert_equal(1, is_sourcing_otg_power_fake.call_count);
}

ZTEST(common_charger_mocked_driver, test_charger_get_actual_current__invalid)
{
	/* charger number out of bounds */
	zassert_equal(EC_ERROR_INVAL, charger_get_actual_current(-1, NULL));
	zassert_equal(EC_ERROR_INVAL,
		      charger_get_actual_current(INT_MAX, NULL));
}

ZTEST(common_charger_mocked_driver, test_charger_get_actual_current__unimpl)
{
	/* get_actual_current is NULL */
	zassert_equal(EC_ERROR_UNIMPLEMENTED,
		      charger_get_actual_current(CHG_NUM, NULL));
}

/**
 * @brief Custom fake for get_actual_current that can write to the output param
 */
static enum ec_error_list get_actual_current_custom_fake(int chgnum,
							 int *current)
{
	ARG_UNUSED(chgnum);

	*current = 1000;

	return EC_SUCCESS;
}

ZTEST_F(common_charger_mocked_driver, test_charger_get_actual_current)
{
	int current;

	fixture->mock_driver.get_actual_current = get_actual_current;
	get_actual_current_fake.custom_fake = get_actual_current_custom_fake;

	zassert_equal(EC_SUCCESS,
		      charger_get_actual_current(CHG_NUM, &current));

	zassert_equal(1, get_actual_current_fake.call_count);
	zassert_equal(CHG_NUM, get_actual_current_fake.arg0_history[0]);
	zassert_equal(1000, current);
}

static void *setup(void)
{
	static struct common_charger_mocked_driver_fixture f;

	zassert_true(board_get_charger_chip_count() > 0,
		     "Need at least one charger chip present.");

	/* Back up the current charger driver and substitute our own */
	f.saved_driver_ptr = chg_chips[CHG_NUM].drv;
	chg_chips[CHG_NUM].drv = &f.mock_driver;

	return &f;
}

static void reset(void *data)
{
	struct common_charger_mocked_driver_fixture *f = data;

	/* Reset the mock driver's function pointer table. Each tests adds these
	 * as-needed
	 */
	f->mock_driver = (struct charger_drv){ 0 };

	/* Reset fakes */
	RESET_FAKE(enable_otg_power);
	RESET_FAKE(set_otg_current_voltage);
	RESET_FAKE(is_sourcing_otg_power);
	RESET_FAKE(get_actual_current);
}

static void teardown(void *data)
{
	struct common_charger_mocked_driver_fixture *f = data;

	/* Restore the original driver */
	chg_chips[CHG_NUM].drv = f->saved_driver_ptr;
}

ZTEST_SUITE(common_charger_mocked_driver, drivers_predicate_post_main, setup,
	    reset, reset, teardown);