strawberry/app/tests/test_autostart.cpp
trvon 1c5ca2b3cd
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Read the autostart test file without istreambuf_iterator
The autostart test used std::istreambuf_iterator, which source
invariant R1 forbids; use read_small_file_or_empty. The previous commit
was pushed with source_invariants failing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 21:00:19 -06:00

73 lines
3.2 KiB
C++

// "Start at login": the XDG autostart entry and the per-session hint.
#include "../autostart.hpp"
#include "strawberry/proc_read.hpp"
#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <unistd.h>
namespace fs = std::filesystem;
using namespace strawberry::app;
int main() {
const fs::path root = fs::temp_directory_path() / ("sb-autostart-" + std::to_string(::getpid()));
fs::remove_all(root);
AutostartEnv env;
env.home = (root / "home").string();
env.current_desktop = "GNOME";
// Location: $XDG_CONFIG_HOME when absolute, else ~/.config.
assert(autostart_file(env) == env.home + "/.config/autostart/strawberry-tray.desktop");
env.xdg_config_home = (root / "cfg").string();
assert(autostart_file(env) == (root / "cfg").string() + "/autostart/strawberry-tray.desktop");
env.xdg_config_home = "relative/ignored";
assert(autostart_file(env) == env.home + "/.config/autostart/strawberry-tray.desktop");
// Enable / disable round trip.
std::string error;
assert(!autostart_enabled(env));
assert(!set_autostart(env, true, "strawberry-tray", error) && !error.empty()); // needs an absolute path
assert(set_autostart(env, true, "/usr/local/bin/strawberry-tray", error));
assert(autostart_enabled(env));
{
const std::string text = strawberry::read_small_file_or_empty(autostart_file(env));
assert(text.find("Exec=/usr/local/bin/strawberry-tray\n") != std::string::npos);
assert(text.find("Type=Application") != std::string::npos);
}
assert(set_autostart(env, false, "", error));
assert(!autostart_enabled(env) && !fs::exists(autostart_file(env)));
assert(set_autostart(env, false, "", error)); // disabling twice is fine
// An entry disabled by the desktop (Hidden=true) counts as off.
fs::create_directories(fs::path(autostart_file(env)).parent_path());
std::ofstream(autostart_file(env)) << "[Desktop Entry]\nExec=/x\nHidden=true\n";
assert(!autostart_enabled(env));
// Paths with spaces are quoted per the desktop entry spec.
assert(autostart_entry("/opt/my apps/strawberry-tray").find("Exec=\"/opt/my apps/strawberry-tray\"") != std::string::npos);
// Session support: desktops that run autostart need no hint; sway and
// friends get the exact line to add; the systemd autostart target counts.
assert(session_runs_xdg_autostart(env) && session_hint(env, "/usr/local/bin/strawberry-tray").empty());
env.current_desktop = "sway:wlroots";
assert(!session_runs_xdg_autostart(env));
assert(session_hint(env, "/usr/local/bin/strawberry-tray").find("exec /usr/local/bin/strawberry-tray") != std::string::npos);
env.current_desktop = "Hyprland";
assert(session_hint(env, "/usr/bin/strawberry-tray").find("exec-once = /usr/bin/strawberry-tray") != std::string::npos);
env.current_desktop = "sway";
env.autostart_target_active = true; // e.g. uwsm
assert(session_hint(env, "/x").empty());
env.autostart_target_active = false;
env.current_desktop = "KDE";
assert(session_runs_xdg_autostart(env));
fs::remove_all(root);
std::cout << "autostart: PASSED" << std::endl;
return 0;
}