summaryrefslogtreecommitdiff
path: root/test/listwidget.cpp
blob: 3a76fbc4c099febec748eb3817c5cde1b7866ebf (plain) (blame)
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
#include "listwidget.h"

#include "stflpp.h"
#include "utils.h"

#include "3rd-party/catch.hpp"
#include <cstdint>
#include <set>
#include <string>

using namespace newsboat;

const static std::string stflListForm =
	"vbox\n"
	"  list[list-name]\n"
	"    richtext:1\n"
	"    pos[list-name_pos]:0\n"
	"    offset[list-name_offset]:0";

std::string render_empty_line(std::uint32_t, std::uint32_t)
{
	return "";
}


TEST_CASE("invalidate_list_content() makes sure `position < num_lines`", "[ListWidget]")
{
	const std::uint32_t scrolloff = 0;
	Stfl::Form listForm(stflListForm);

	ListWidget listWidget("list-name", listForm, scrolloff);

	GIVEN("a ListWidget with 3 lines and position=2") {
		listWidget.invalidate_list_content(3, render_empty_line);
		listWidget.set_position(2);
		REQUIRE(listWidget.get_position() == 2);

		WHEN("the number of lines is reduced") {
			listWidget.invalidate_list_content(2, render_empty_line);

			THEN("the position is equal to the bottom item of the new list") {
				REQUIRE(listWidget.get_position() == 1);
			}
		}

		WHEN("all lines are removed") {
			listWidget.invalidate_list_content(0, render_empty_line);

			THEN("the position is changed to 0") {
				REQUIRE(listWidget.get_position() == 0);
			}
		}

		WHEN("a line is added") {
			listWidget.invalidate_list_content(4, render_empty_line);

			THEN("the position is not changed") {
				REQUIRE(listWidget.get_position() == 2);
			}
		}
	}
}

TEST_CASE("stfl_replace_list() makes sure the position is reset", "[ListWidget]")
{
	const std::uint32_t scrolloff = 0;
	Stfl::Form listForm(stflListForm);

	ListWidget listWidget("list-name", listForm, scrolloff);

	const std::string stflListPrototype =
		"{list[list-name] "
		"richtext:1 "
		"pos[list-name_pos]:0 "
		"offset[list-name_offset]:0 "
		"}";

	GIVEN("a ListWidget with 3 lines and position=2") {
		listWidget.invalidate_list_content(3, render_empty_line);
		listWidget.set_position(2);
		REQUIRE(listWidget.get_position() == 2);

		WHEN("the list is replaced") {
			listWidget.stfl_replace_list(stflListPrototype);

			THEN("the position is changed to 0") {
				REQUIRE(listWidget.get_position() == 0);
			}
		}
	}
}

TEST_CASE("invalidate_list_content() clears internal caches", "[ListWidget]")
{
	const std::uint32_t scrolloff = 0;
	Stfl::Form listForm(stflListForm);
	// Recalculate list dimensions
	listForm.run(-3);
	Stfl::reset();

	ListWidget listWidget("list-name", listForm, scrolloff);

	std::set<std::uint32_t> requested_lines;
	auto render_line = [&](std::uint32_t line, std::uint32_t) -> std::string {
		requested_lines.insert(line);
		return "";
	};

	GIVEN("a ListWidget with 3 lines") {
		listWidget.invalidate_list_content(3, render_empty_line);

		WHEN("the invalidate_list_content() is called") {
			requested_lines.clear();
			listWidget.invalidate_list_content(3, render_line);

			THEN("all lines are requested again") {
				REQUIRE(requested_lines.size() == 3);
				REQUIRE(requested_lines.count(0) == 1);
				REQUIRE(requested_lines.count(1) == 1);
				REQUIRE(requested_lines.count(2) == 1);
			}
		}
	}
}