File: UI_test_cpp.cpp

package info (click to toggle)
fff 1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,968 kB
  • sloc: cpp: 18,525; ansic: 6,971; ruby: 534; makefile: 223; sh: 17
file content (136 lines) | stat: -rw-r--r-- 3,330 bytes parent folder | download
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
extern "C"{
#include "UI.h"
#include "SYSTEM.h"
#include "DISPLAY.h"
}
#include <gtest/gtest.h>


#include "../../fff.h"
DEFINE_FFF_GLOBALS;

/* SYSTEM.h */
FAKE_VOID_FUNC(SYSTEM_register_irq, irq_func_t, unsigned int);
/* DISPLAY.h */
FAKE_VOID_FUNC(DISPLAY_init);
FAKE_VOID_FUNC(DISPLAY_clear);
FAKE_VOID_FUNC(DISPLAY_output, char *);
FAKE_VALUE_FUNC(unsigned int, DISPLAY_get_line_capacity);
FAKE_VALUE_FUNC(unsigned int, DISPLAY_get_line_insert_index);

FAKE_VOID_FUNC(button_press_cbk);


class UITests : public testing::Test
{
public:

	void SetUp()
	{
		// Register resets
	    RESET_FAKE(SYSTEM_register_irq);

	    RESET_FAKE(DISPLAY_init)
	    RESET_FAKE(DISPLAY_clear)
	    RESET_FAKE(DISPLAY_output)
	    RESET_FAKE(DISPLAY_get_line_capacity)
	    RESET_FAKE(DISPLAY_get_line_insert_index);

	    RESET_FAKE(button_press_cbk);

		FFF_RESET_HISTORY();
		// non default init
		DISPLAY_get_line_capacity_fake.return_val = 2;
	}
};



/* Tests go here */
TEST_F(UITests, init_will_initialise_display)
{
	UI_init();
	ASSERT_EQ(DISPLAY_init_fake.call_count, 1);
}

TEST_F(UITests, init_will_register_interrupt_gpio2)
{
	UI_init();
    ASSERT_EQ(SYSTEM_register_irq_fake.call_count, 1);
    ASSERT_EQ((void *)SYSTEM_register_irq_fake.arg0_val, (void *)UI_button_irq_handler);
    ASSERT_EQ(SYSTEM_register_irq_fake.arg1_val, IRQ_GPIO_2);
}

TEST_F(UITests, when_no_irq_then_missed_irq_counter_zero)
{
    ASSERT_EQ(UI_get_missed_irqs(), 0);
}

TEST_F(UITests, when_one_irq_and_no_handler_then_missed_irq_counter_one)
{
	UI_button_irq_handler();
    ASSERT_EQ(UI_get_missed_irqs(), 1);
}

TEST_F(UITests, when_one_irq_and_valid_callback_then_missed_irq_counter_zero)
{
	UI_init();
	UI_register_button_cbk(button_press_cbk);
	UI_button_irq_handler();
    ASSERT_EQ(UI_get_missed_irqs(), 0);
}

TEST_F(UITests, when_one_irq_and_valid_callback_then_callback_called)
{
	UI_register_button_cbk(button_press_cbk);
	UI_button_irq_handler();
	ASSERT_EQ(button_press_cbk_fake.call_count, 1);
}

TEST_F(UITests, write_line_outputs_lines_to_display)
{
	char msg[] = "helloworld";
	UI_write_line(msg);
	ASSERT_EQ(DISPLAY_output_fake.call_count, 1);
	ASSERT_EQ(strncmp(DISPLAY_output_fake.arg0_val, msg, 26), 0);
}

TEST_F(UITests, when_no_empty_lines_write_line_clears_screen_and_outputs_lines_to_display)
{
	DISPLAY_get_line_insert_index_fake.return_val = 2;
	char msg[] = "helloworld";

	UI_write_line(msg);

	ASSERT_EQ(DISPLAY_clear_fake.call_count, 1);
	ASSERT_EQ(DISPLAY_output_fake.call_count, 1);
	// Check the order of the calls:  Don't care about the first two:
	// DISPLAY_get_line_capacity and DISPLAY_get_line_insert_index
	ASSERT_EQ(fff.call_history_idx, 4);
	ASSERT_EQ(fff.call_history[2], (void *) DISPLAY_clear);
	ASSERT_EQ(fff.call_history[3], (void *) DISPLAY_output);
}

TEST_F(UITests, when_empty_lines_write_line_doesnt_clear_screen)
{
	// given
	DISPLAY_get_line_insert_index_fake.return_val = 1;
	char msg[] = "helloworld";
	// when
	UI_write_line(msg);
	// then
	ASSERT_EQ(DISPLAY_clear_fake.call_count, 0);
}

TEST_F(UITests, when_string_longer_than_26_then_truncated_string_output)
{
	// given
	char input[] = "abcdefghijklmnopqrstuvwxyz0123456789";
	char expected[] = "abcdefghijklmnopqrstuvwxyz";
	// when
	UI_write_line(input);
	// then
	ASSERT_EQ(strncmp(expected, DISPLAY_output_fake.arg0_val, 37), 0);
}