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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
#include <gtest/gtest.h>
#include <XdgUtils/DesktopEntry/DesktopEntry.h>
using namespace XdgUtils::DesktopEntry;
class TestDesktopEntry : public ::testing::Test {
protected:
const std::string exampleDesktopEntry = "[Desktop Entry]\n"
"Version=1.0\n"
"Type=Application\n"
"Name=Foo Viewer\n"
"Comment=The best viewer for Foo objects available!\n"
"TryExec=fooview\n"
"Exec=fooview %F\n"
"Icon=fooview\n"
"MimeType=image/x-foo;\n"
"Actions=Gallery;Create;\n"
"\n"
"[Desktop Action Gallery]\n"
"Exec=fooview --gallery\n"
"Name=Browse Gallery\n"
"\n"
"[Desktop Action Create]\n"
"Exec=fooview --create-new\n"
"Name=Create a new Foo!\n"
"Icon=fooview-new\n";
};
TEST_F(TestDesktopEntry, create) {
DesktopEntry a;
auto entryPath = "Desktop Entry/Name";
auto entryValue = "A";
a.set(entryPath, entryValue);
// Copy constructor
DesktopEntry b = a;
ASSERT_EQ(a.get(entryPath), b.get(entryPath));
ASSERT_EQ(a, b);
b.set(entryPath, "b");
ASSERT_NE(a, b);
}
TEST_F(TestDesktopEntry, readWrite) {
DesktopEntry entry;
std::stringstream in(exampleDesktopEntry);
in >> entry;
std::stringstream out;
out << entry;
ASSERT_EQ(in.str(), out.str());
}
TEST_F(TestDesktopEntry, paths) {
DesktopEntry entry{
"[Desktop Entry]\n"
"Version=1.0\n"
"Type=Application\n"
"Name=Foo Viewer\n"
"Comment=The best viewer for Foo objects available!\n"
"TryExec=fooview\n"
"Exec=fooview %F\n"
"Icon=fooview\n"
};
std::vector<std::string> paths = entry.paths();
std::vector<std::string> expectedPaths = {
"Desktop Entry",
"Desktop Entry/Comment",
"Desktop Entry/Exec",
"Desktop Entry/Icon",
"Desktop Entry/Name",
"Desktop Entry/TryExec",
"Desktop Entry/Type",
"Desktop Entry/Version",
};
ASSERT_EQ(paths, expectedPaths);
}
TEST_F(TestDesktopEntry, get) {
const std::string s = "[Desktop Entry]\n"
"Version=1.0\n"
"Type=Application\n"
"Name=Foo Viewer\n"
"Comment=The best viewer for Foo objects available!\n"
"TryExec=fooview\n"
"Exec=fooview %F\n"
"Icon=fooview\n"
"MimeType=image/x-foo;\n"
"Actions=Gallery;Create;\n"
"\n"
"[Desktop Action Gallery]\n"
"Exec=fooview --gallery\n"
"Name=Browse Gallery\n"
"\n"
"[Desktop Action Create]\n"
"Exec=fooview --create-new\n"
"Name=Create a new Foo!\n"
"Icon=fooview-new\n";
DesktopEntry entry;
std::stringstream in(s);
in >> entry;
ASSERT_EQ(entry.get("Desktop Entry/Name"), "Foo Viewer");
ASSERT_EQ(entry.get("Desktop Action Gallery/Name"), "Browse Gallery");
ASSERT_EQ(entry.get("Desktop Action Create/Name"), "Create a new Foo!");
}
TEST_F(TestDesktopEntry, set) {
DesktopEntry entry;
entry.set("A/B", "C");
std::stringstream out;
out << entry;
std::string expected = "[A]\nB=C";
ASSERT_EQ(out.str(), expected);
}
TEST_F(TestDesktopEntry, edit) {
DesktopEntry entry;
std::stringstream in(exampleDesktopEntry);
in >> entry;
entry.set("Spam", "");
ASSERT_EQ(entry.get("Spam"), "Spam");
entry.set("Spam/Spam", "Food");
ASSERT_EQ(entry.get("Spam/Spam"), "Food");
entry.set("Spam2/Spam", "Food");
ASSERT_EQ(entry.get("Spam2/Spam"), "Food");
entry.set("Spam2/Spam", "");
ASSERT_EQ(entry.get("Spam2/Spam"), "");
entry.set("Desktop Entry/Name", "New Foo Viewer");
ASSERT_EQ(entry.get("Desktop Entry/Name"), "New Foo Viewer");
entry.set("Desktop Action Gallery/Name", "Browse The Gallery");
ASSERT_EQ(entry.get("Desktop Action Gallery/Name"), "Browse The Gallery");
entry.set("Desktop Action Create/Name", "Create Foo!");
ASSERT_EQ(entry.get("Desktop Action Create/Name"), "Create Foo!");
entry.set("Desktop Action Create/Name[es_ES]", "Crear Foo");
ASSERT_EQ(entry.get("Desktop Action Create/Name[es_ES]"), "Crear Foo");
}
TEST_F(TestDesktopEntry, exists) {
DesktopEntry entry;
std::stringstream in(exampleDesktopEntry);
in >> entry;
ASSERT_TRUE(entry.exists("Desktop Action Gallery/Name"));
ASSERT_FALSE(entry.exists("Desktop Action Gallery/Nam"));
}
TEST_F(TestDesktopEntry, removeGroup) {
DesktopEntry entry;
std::string entryStr = {"[G1]\nName=1\n[g2]\nName=2\n"};
std::stringstream in(entryStr);
in >> entry;
ASSERT_TRUE(entry.exists("G1"));
entry.remove("G1");
ASSERT_FALSE(entry.exists("G1"));
std::stringstream out;
out << entry;
ASSERT_EQ(out.str(), "[g2]\nName=2\n");
}
TEST_F(TestDesktopEntry, removeEntry) {
DesktopEntry entry;
std::string entryStr = {"[G1]\nName=1\nIcon=1\n[g2]\nName=2\n"};
std::stringstream in(entryStr);
in >> entry;
ASSERT_TRUE(entry.exists("G1/Icon"));
entry.remove("G1/Icon");
ASSERT_FALSE(entry.exists("G1/Icon"));
std::stringstream out;
out << entry;
ASSERT_EQ(out.str(), "[G1]\nName=1\n[g2]\nName=2\n");
}
TEST_F(TestDesktopEntry, arraySubscriptOperatorAccessGroup) {
DesktopEntry entry;
entry["Group"] = "Group";
std::stringstream out;
out << entry;
ASSERT_EQ(out.str(), "[Group]\n");
}
TEST_F(TestDesktopEntry, arraySubscriptOperatorAccessEntry) {
DesktopEntry entry;
entry["Group/Key"] = "My App";
ASSERT_EQ(static_cast<std::string>(entry["Group/Key"]), "My App");
std::stringstream out;
out << entry;
ASSERT_EQ(out.str(), "[Group]\n"
"Key=My App");
}
TEST_F(TestDesktopEntry, copy) {
DesktopEntry a;
a["g/k"] = "v";
// Assert that values are copied
DesktopEntry b = a;
ASSERT_EQ(static_cast<std::string>(b["g/k"]), "v");
// Assert that b inner data pointers are different from the a ones
b["g/k"] = "v1";
ASSERT_EQ(static_cast<std::string>(a["g/k"]), "v");
ASSERT_EQ(static_cast<std::string>(b["g/k"]), "v1");
}
TEST_F(TestDesktopEntry, move) {
DesktopEntry a;
a["g/k"] = "v";
// Assert that values are copied
DesktopEntry b = std::move(a);
ASSERT_EQ(static_cast<std::string>(b["g/k"]), "v");
}
|