File: TestHistoryManager.cc

package info (click to toggle)
j4-dmenu-desktop 3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,180 kB
  • sloc: cpp: 6,282; python: 473; sh: 81; makefile: 8
file content (233 lines) | stat: -rw-r--r-- 7,821 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// This file is part of j4-dmenu-desktop.
//
// j4-dmenu-desktop is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// j4-dmenu-desktop is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with j4-dmenu-desktop.  If not, see <http://www.gnu.org/licenses/>.
//

#include <catch2/catch_message.hpp>
#include <catch2/catch_test_macros.hpp>

#include <errno.h>
#include <exception>
#include <fcntl.h>
#include <functional>
#include <map>
#include <optional>
#include <stdexcept>
#include <string.h>
#include <string> // IWYU pragma: keep
#include <unistd.h>
// Both of these aren't used in this file.
// IWYU pragma: no_include <vector>
// IWYU pragma: no_include <utility>

#include "generated/tests_config.hh"

#include "AppManager.hh"
#include "FSUtils.hh"
#include "HistoryManager.hh"
#include "LocaleSuffixes.hh"

// This function checks that a and b have the same value pairs. If values of the
// same key are in a different order, this function still marks them equal.
template <typename Key, typename T, typename Compare>
static bool compare_maps(const std::multimap<Key, T, Compare> &a,
                         const std::multimap<Key, T, Compare> &b) {
    if (a.size() != b.size())
        return false;
    for (const auto &[key, value] : a) {
        auto begin = b.lower_bound(key), end = b.upper_bound(key);
        if (begin == b.end())
            return false;
        bool found = false;
        for (; begin != end; ++begin) {
            if (begin->second == value) {
                found = true;
                break;
            }
        }
        if (!found)
            return false;
    }
    return true;
}

TEST_CASE("Test loading history", "[History]") {
    std::optional<FSUtils::TempFile> tmpfile_container;
    try {
        tmpfile_container.emplace("j4dd-history-unit-test");
    } catch (std::runtime_error &e) {
        SKIP(e.what());
    }
    FSUtils::TempFile &tmpfile = *tmpfile_container;

    int origfd = open(TEST_FILES "history", O_RDONLY);
    if (origfd == -1) {
        SKIP("Couldn't open history file '" << TEST_FILES "history"
                                            << "': " << strerror(errno));
    }
    try {
        tmpfile.copy_from_fd(origfd);
    } catch (const std::exception &e) {
        close(origfd);
        SKIP("Couldn't copy file '" TEST_FILES "history' to '"
             << tmpfile.get_name() << ": " << e.what());
    }
    close(origfd);

    std::multimap<int, string, std::greater<int>> history = {
        {8, "Pinta"       },
        {8, "XScreenSaver"},
        {7, "Kdenlive"    },
        {1, "Thunderbird" },
    };

    std::multimap<int, string, std::greater<int>> history_modified = {
        {8, "Pinta"       },
        {8, "XScreenSaver"},
        {8, "Kdenlive"    },
        {1, "Thunderbird" },
    };

    std::multimap<int, string, std::greater<int>> history_added = {
        {8, "Pinta"       },
        {8, "XScreenSaver"},
        {8, "Kdenlive"    },
        {1, "Thunderbird" },
        {1, "Firefox"     },
    };

    {
        HistoryManager hist(tmpfile.get_name());
        REQUIRE(compare_maps(hist.view(), history));

        REQUIRE((tmpfile.compare_file(TEST_FILES "history-variant1") ||
                 tmpfile.compare_file(TEST_FILES "history-variant2")));

        hist.increment("Kdenlive");
        REQUIRE(compare_maps(hist.view(), history_modified));

        hist.increment("Firefox");
        REQUIRE(compare_maps(hist.view(), history_added));
    }
}

TEST_CASE("Test too new history", "[History]") {
    REQUIRE_THROWS(HistoryManager(TEST_FILES "too-new-history"));
}

TEST_CASE("Test bad history with empty entry", "[History]") {
    REQUIRE_THROWS(HistoryManager(TEST_FILES "bad-history"));
}

TEST_CASE("Test conversion from v0 to v1", "[History]") {
    std::optional<FSUtils::TempFile> tmpfile_container;
    try {
        tmpfile_container.emplace("j4dd-history-unit-test");
    } catch (std::runtime_error &e) {
        SKIP(e.what());
    }
    FSUtils::TempFile &tmpfile = *tmpfile_container;

    int origfd = open(TEST_FILES "old-history", O_RDONLY);
    if (origfd == -1) {
        SKIP("Couldn't open history file '" << TEST_FILES "old-history"
                                            << "': " << strerror(errno));
    }
    try {
        tmpfile.copy_from_fd(origfd);
    } catch (const std::exception &e) {
        close(origfd);
        SKIP("Couldn't copy file '" TEST_FILES "old-history' to '"
             << tmpfile.get_name() << ": " << e.what());
    }
    close(origfd);

    std::multimap<int, string, std::greater<int>> history = {
        {7, "Htop"                          },
        {7, "Process Viewer"                },
        {3, "Image Editor"                  },
        {3, "GNU Image Manipulation Program"},
        {1, "Eagle"                         },
    };

    {
        REQUIRE_THROWS_AS(HistoryManager(tmpfile.get_name()), v0_version_error);
        AppManager apps(
            {
                {TEST_FILES "applications/",
                 {
                     // These are actually important.
                     TEST_FILES "applications/eagle.desktop",
                     TEST_FILES "applications/gimp.desktop",
                     TEST_FILES "applications/htop.desktop",
                     // These are just filler desktop files.
                     TEST_FILES "applications/web.desktop",
                     TEST_FILES "applications/visible.desktop",
                 }}
        },
            {}, LocaleSuffixes("en_US"));
        HistoryManager hist =
            HistoryManager::convert_history_from_v0(tmpfile.get_name(), apps);
        REQUIRE(compare_maps(hist.view(), history));
        // XXX
    }
}

TEST_CASE("Test imperfect conversion from history v0 to v1", "[History]") {
    std::optional<FSUtils::TempFile> tmpfile_container;
    try {
        tmpfile_container.emplace("j4dd-history-unit-test");
    } catch (std::runtime_error &e) {
        SKIP(e.what());
    }
    FSUtils::TempFile &tmpfile = *tmpfile_container;

    int origfd = open(TEST_FILES "old-double-history", O_RDONLY);
    if (origfd == -1) {
        SKIP("Couldn't open history file '" << TEST_FILES "old-double-history"
                                            << "': " << strerror(errno));
    }
    try {
        tmpfile.copy_from_fd(origfd);
    } catch (const std::exception &e) {
        close(origfd);
        SKIP("Couldn't copy file '" TEST_FILES "old-double-history' to '"
             << tmpfile.get_name() << ": " << e.what());
    }
    close(origfd);

    std::multimap<int, string, std::greater<int>> history = {
        {3, "Eagle"},
    };

    {
        REQUIRE_THROWS_AS(HistoryManager(tmpfile.get_name()), v0_version_error);
        AppManager apps(
            {
                {TEST_FILES "applications/",
                 {
                     // This is actually important.
                     TEST_FILES "applications/doubleeagle.desktop",
                     // These are just filler desktop files.
                     TEST_FILES "applications/web.desktop",
                     TEST_FILES "applications/visible.desktop",
                 }}
        },
            {}, LocaleSuffixes("en_US"));
        HistoryManager hist =
            HistoryManager::convert_history_from_v0(tmpfile.get_name(), apps);
        REQUIRE(compare_maps(hist.view(), history));
    }
}