mirror of
https://github.com/ZoiteChat/zoitechat.git
synced 2026-03-18 11:40:18 +00:00
feat: centralize theming in theme-manager (palette/tokens, CSS, dark-mode, setup UI), add tests + win32/meson wiring
This commit is contained in:
236
src/fe-gtk/theme/tests/test-theme-access-routing.c
Normal file
236
src/fe-gtk/theme/tests/test-theme-access-routing.c
Normal file
@@ -0,0 +1,236 @@
|
||||
#include <math.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include "../theme-access.h"
|
||||
#include "../theme-manager.h"
|
||||
#include "../theme-runtime.h"
|
||||
#include "../../xtext-color.h"
|
||||
#include "../../../common/zoitechat.h"
|
||||
|
||||
struct session *current_sess;
|
||||
struct session *current_tab;
|
||||
struct session *lastact_sess;
|
||||
struct zoitechatprefs prefs;
|
||||
|
||||
static gboolean stub_dark_active;
|
||||
static ThemeSemanticToken stub_last_color_token;
|
||||
static int stub_runtime_get_color_calls;
|
||||
static int stub_runtime_widget_calls;
|
||||
static int stub_runtime_xtext_calls;
|
||||
static size_t stub_runtime_xtext_last_len;
|
||||
|
||||
static GdkRGBA stub_light_colors[THEME_TOKEN_COUNT];
|
||||
static GdkRGBA stub_dark_colors[THEME_TOKEN_COUNT];
|
||||
|
||||
void
|
||||
setup_apply_real (const ThemeChangedEvent *event)
|
||||
{
|
||||
(void) event;
|
||||
}
|
||||
|
||||
gboolean
|
||||
fe_dark_mode_is_enabled_for (unsigned int mode)
|
||||
{
|
||||
return mode == ZOITECHAT_DARK_MODE_DARK;
|
||||
}
|
||||
|
||||
gboolean
|
||||
theme_runtime_apply_mode (unsigned int mode, gboolean *dark_active)
|
||||
{
|
||||
if (mode == ZOITECHAT_DARK_MODE_DARK)
|
||||
stub_dark_active = TRUE;
|
||||
if (mode == ZOITECHAT_DARK_MODE_LIGHT)
|
||||
stub_dark_active = FALSE;
|
||||
if (dark_active)
|
||||
*dark_active = stub_dark_active;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
theme_runtime_load (void)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
theme_runtime_save (void)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
theme_runtime_user_set_color (ThemeSemanticToken token, const GdkRGBA *col)
|
||||
{
|
||||
(void) token;
|
||||
(void) col;
|
||||
}
|
||||
|
||||
void
|
||||
theme_runtime_dark_set_color (ThemeSemanticToken token, const GdkRGBA *col)
|
||||
{
|
||||
(void) token;
|
||||
(void) col;
|
||||
}
|
||||
|
||||
gboolean
|
||||
theme_runtime_get_color (ThemeSemanticToken token, GdkRGBA *out_rgba)
|
||||
{
|
||||
g_assert_nonnull (out_rgba);
|
||||
stub_runtime_get_color_calls++;
|
||||
stub_last_color_token = token;
|
||||
*out_rgba = stub_dark_active ? stub_dark_colors[token] : stub_light_colors[token];
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void
|
||||
theme_runtime_get_widget_style_values (ThemeWidgetStyleValues *out_values)
|
||||
{
|
||||
stub_runtime_widget_calls++;
|
||||
gdk_rgba_parse (&out_values->background, "#010203");
|
||||
gdk_rgba_parse (&out_values->foreground, "#fdfcfa");
|
||||
}
|
||||
|
||||
void
|
||||
theme_runtime_get_xtext_colors (XTextColor *palette, size_t palette_len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
stub_runtime_xtext_calls++;
|
||||
stub_runtime_xtext_last_len = palette_len;
|
||||
for (i = 0; i < palette_len; i++)
|
||||
{
|
||||
palette[i].red = (unsigned short) (i + 1);
|
||||
palette[i].green = (unsigned short) (i + 2);
|
||||
palette[i].blue = (unsigned short) (i + 3);
|
||||
}
|
||||
}
|
||||
|
||||
gboolean
|
||||
theme_runtime_is_dark_active (void)
|
||||
{
|
||||
return stub_dark_active;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
rgba_equal (const GdkRGBA *a, const GdkRGBA *b)
|
||||
{
|
||||
return a->red == b->red && a->green == b->green && a->blue == b->blue && a->alpha == b->alpha;
|
||||
}
|
||||
|
||||
static void
|
||||
reset_stubs (void)
|
||||
{
|
||||
size_t i;
|
||||
char light[32];
|
||||
char dark[32];
|
||||
|
||||
stub_dark_active = FALSE;
|
||||
stub_last_color_token = THEME_TOKEN_MIRC_0;
|
||||
stub_runtime_get_color_calls = 0;
|
||||
stub_runtime_widget_calls = 0;
|
||||
stub_runtime_xtext_calls = 0;
|
||||
stub_runtime_xtext_last_len = 0;
|
||||
for (i = 0; i < THEME_TOKEN_COUNT; i++)
|
||||
{
|
||||
g_snprintf (light, sizeof (light), "#%02x%02x%02x", (unsigned int) (i + 1), 0x11, 0x22);
|
||||
g_snprintf (dark, sizeof (dark), "#%02x%02x%02x", (unsigned int) (i + 1), 0xaa, 0xbb);
|
||||
g_assert_true (gdk_rgba_parse (&stub_light_colors[i], light));
|
||||
g_assert_true (gdk_rgba_parse (&stub_dark_colors[i], dark));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_access_semantic_token_routes_directly (void)
|
||||
{
|
||||
ThemeSemanticToken token;
|
||||
GdkRGBA color;
|
||||
size_t i;
|
||||
|
||||
reset_stubs ();
|
||||
for (i = 0; i < theme_palette_token_def_count (); i++)
|
||||
{
|
||||
const ThemePaletteTokenDef *def = theme_palette_token_def_at (i);
|
||||
|
||||
g_assert_nonnull (def);
|
||||
token = def->token;
|
||||
g_assert_true (theme_get_color (token, &color));
|
||||
g_assert_cmpint (stub_last_color_token, ==, token);
|
||||
g_assert_true (rgba_equal (&color, &stub_light_colors[token]));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_access_token_routes_without_legacy_accessor (void)
|
||||
{
|
||||
ThemeSemanticToken token = THEME_TOKEN_MIRC_0;
|
||||
GdkRGBA color;
|
||||
size_t i;
|
||||
|
||||
reset_stubs ();
|
||||
for (i = 0; i < theme_palette_token_def_count (); i++)
|
||||
{
|
||||
const ThemePaletteTokenDef *def = theme_palette_token_def_at (i);
|
||||
|
||||
g_assert_nonnull (def);
|
||||
g_assert_true (theme_palette_legacy_index_to_token (def->legacy_index, &token));
|
||||
g_assert_true (theme_get_color (token, &color));
|
||||
g_assert_cmpint (stub_last_color_token, ==, token);
|
||||
g_assert_true (rgba_equal (&color, &stub_light_colors[token]));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_access_xtext_palette_forwarding (void)
|
||||
{
|
||||
XTextColor palette[4] = { 0 };
|
||||
|
||||
reset_stubs ();
|
||||
theme_get_xtext_colors (palette, G_N_ELEMENTS (palette));
|
||||
g_assert_cmpint (stub_runtime_xtext_calls, ==, 1);
|
||||
g_assert_cmpuint (stub_runtime_xtext_last_len, ==, G_N_ELEMENTS (palette));
|
||||
g_assert_cmpuint (palette[0].red, ==, 1);
|
||||
g_assert_cmpuint (palette[1].green, ==, 3);
|
||||
g_assert_cmpuint (palette[3].blue, ==, 6);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_access_widget_style_forwarding (void)
|
||||
{
|
||||
ThemeWidgetStyleValues values;
|
||||
|
||||
reset_stubs ();
|
||||
theme_get_widget_style_values (&values);
|
||||
g_assert_cmpint (stub_runtime_widget_calls, ==, 1);
|
||||
g_assert_true (fabs (values.background.red - (0x01 / 255.0)) < 0.0001);
|
||||
g_assert_true (fabs (values.foreground.green - (0xfc / 255.0)) < 0.0001);
|
||||
}
|
||||
|
||||
static void
|
||||
test_access_dark_light_switch_affects_token_consumers (void)
|
||||
{
|
||||
ThemeSemanticToken token;
|
||||
GdkRGBA light;
|
||||
GdkRGBA dark;
|
||||
reset_stubs ();
|
||||
token = THEME_TOKEN_TEXT_FOREGROUND;
|
||||
g_assert_true (theme_runtime_apply_mode (ZOITECHAT_DARK_MODE_LIGHT, NULL));
|
||||
g_assert_true (theme_get_color (token, &light));
|
||||
g_assert_true (rgba_equal (&light, &stub_light_colors[token]));
|
||||
|
||||
g_assert_true (theme_runtime_apply_mode (ZOITECHAT_DARK_MODE_DARK, NULL));
|
||||
g_assert_true (theme_get_color (token, &dark));
|
||||
g_assert_true (rgba_equal (&dark, &stub_dark_colors[token]));
|
||||
g_assert_false (rgba_equal (&light, &dark));
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
g_test_add_func ("/theme/access/semantic_token_routes_directly", test_access_semantic_token_routes_directly);
|
||||
g_test_add_func ("/theme/access/token_routes_without_legacy_accessor", test_access_token_routes_without_legacy_accessor);
|
||||
g_test_add_func ("/theme/access/xtext_palette_forwarding", test_access_xtext_palette_forwarding);
|
||||
g_test_add_func ("/theme/access/widget_style_forwarding", test_access_widget_style_forwarding);
|
||||
g_test_add_func ("/theme/access/dark_light_switch_affects_token_consumers",
|
||||
test_access_dark_light_switch_affects_token_consumers);
|
||||
return g_test_run ();
|
||||
}
|
||||
122
src/fe-gtk/theme/tests/test-theme-application-input-style.c
Normal file
122
src/fe-gtk/theme/tests/test-theme-application-input-style.c
Normal file
@@ -0,0 +1,122 @@
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include "../theme-application.h"
|
||||
#include "../../maingui.h"
|
||||
#include "../../../common/zoitechat.h"
|
||||
#include "../../../common/zoitechatc.h"
|
||||
|
||||
struct session *current_sess;
|
||||
struct session *current_tab;
|
||||
struct session *lastact_sess;
|
||||
struct zoitechatprefs prefs;
|
||||
InputStyle *input_style;
|
||||
|
||||
static gboolean css_enabled;
|
||||
static PangoFontDescription *css_font_desc;
|
||||
static int css_reload_calls;
|
||||
static int message_calls;
|
||||
|
||||
void
|
||||
fe_message (char *msg, int flags)
|
||||
{
|
||||
(void) msg;
|
||||
(void) flags;
|
||||
message_calls++;
|
||||
}
|
||||
|
||||
void
|
||||
theme_css_reload_input_style (gboolean enabled, const PangoFontDescription *font_desc)
|
||||
{
|
||||
css_enabled = enabled;
|
||||
if (css_font_desc)
|
||||
pango_font_description_free (css_font_desc);
|
||||
css_font_desc = font_desc ? pango_font_description_copy (font_desc) : NULL;
|
||||
css_reload_calls++;
|
||||
}
|
||||
|
||||
void
|
||||
theme_runtime_load (void)
|
||||
{
|
||||
}
|
||||
|
||||
gboolean
|
||||
theme_runtime_apply_mode (unsigned int mode, gboolean *palette_changed)
|
||||
{
|
||||
(void) mode;
|
||||
if (palette_changed)
|
||||
*palette_changed = FALSE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void
|
||||
reset_state (void)
|
||||
{
|
||||
if (css_font_desc)
|
||||
{
|
||||
pango_font_description_free (css_font_desc);
|
||||
css_font_desc = NULL;
|
||||
}
|
||||
if (input_style)
|
||||
{
|
||||
if (input_style->font_desc)
|
||||
pango_font_description_free (input_style->font_desc);
|
||||
g_free (input_style);
|
||||
input_style = NULL;
|
||||
}
|
||||
css_enabled = FALSE;
|
||||
css_reload_calls = 0;
|
||||
message_calls = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
test_invalid_font_falls_back_to_sans_11 (void)
|
||||
{
|
||||
InputStyle *style;
|
||||
|
||||
reset_state ();
|
||||
g_strlcpy (prefs.hex_text_font, "Sans", sizeof (prefs.hex_text_font));
|
||||
prefs.hex_gui_input_style = TRUE;
|
||||
|
||||
style = theme_application_update_input_style (NULL);
|
||||
g_assert_nonnull (style);
|
||||
g_assert_nonnull (style->font_desc);
|
||||
g_assert_cmpstr (pango_font_description_get_family (style->font_desc), ==, "sans");
|
||||
g_assert_cmpint (pango_font_description_get_size (style->font_desc), ==, 11 * PANGO_SCALE);
|
||||
g_assert_cmpint (message_calls, ==, 1);
|
||||
g_assert_cmpint (css_reload_calls, ==, 1);
|
||||
g_assert_true (css_enabled);
|
||||
g_assert_nonnull (css_font_desc);
|
||||
|
||||
if (style->font_desc)
|
||||
pango_font_description_free (style->font_desc);
|
||||
g_free (style);
|
||||
}
|
||||
|
||||
static void
|
||||
test_style_toggle_routes_enabled_flag (void)
|
||||
{
|
||||
reset_state ();
|
||||
g_strlcpy (prefs.hex_text_font, "sans 13", sizeof (prefs.hex_text_font));
|
||||
prefs.hex_gui_input_style = FALSE;
|
||||
input_style = NULL;
|
||||
|
||||
theme_application_reload_input_style ();
|
||||
g_assert_nonnull (input_style);
|
||||
g_assert_cmpint (css_reload_calls, ==, 1);
|
||||
g_assert_false (css_enabled);
|
||||
g_assert_nonnull (css_font_desc);
|
||||
g_assert_cmpstr (pango_font_description_get_family (css_font_desc), ==, "sans");
|
||||
g_assert_cmpint (pango_font_description_get_size (css_font_desc), ==, 13 * PANGO_SCALE);
|
||||
g_assert_cmpint (message_calls, ==, 0);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
g_test_add_func ("/theme/application/input_style/invalid_font_fallback",
|
||||
test_invalid_font_falls_back_to_sans_11);
|
||||
g_test_add_func ("/theme/application/input_style/style_toggle_routes_enabled_flag",
|
||||
test_style_toggle_routes_enabled_flag);
|
||||
return g_test_run ();
|
||||
}
|
||||
229
src/fe-gtk/theme/tests/test-theme-manager-auto-refresh.c
Normal file
229
src/fe-gtk/theme/tests/test-theme-manager-auto-refresh.c
Normal file
@@ -0,0 +1,229 @@
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include "../theme-manager.h"
|
||||
#include "../../fe-gtk.h"
|
||||
#include "../../../common/zoitechat.h"
|
||||
#include "../../../common/zoitechatc.h"
|
||||
|
||||
struct session *current_sess;
|
||||
struct session *current_tab;
|
||||
struct session *lastact_sess;
|
||||
struct zoitechatprefs prefs;
|
||||
|
||||
static gboolean stub_apply_mode_palette_changed;
|
||||
static gboolean stub_system_prefers_dark;
|
||||
static int auto_state_calls;
|
||||
static gboolean last_auto_state;
|
||||
static int listener_calls;
|
||||
static ThemeChangedEvent last_event;
|
||||
static int idle_add_calls;
|
||||
static guint next_idle_source_id = 33;
|
||||
|
||||
void setup_apply_real (const ThemeChangedEvent *event)
|
||||
{
|
||||
(void) event;
|
||||
}
|
||||
|
||||
gboolean fe_dark_mode_is_enabled_for (unsigned int mode)
|
||||
{
|
||||
return mode == ZOITECHAT_DARK_MODE_DARK;
|
||||
}
|
||||
|
||||
void fe_set_auto_dark_mode_state (gboolean enabled)
|
||||
{
|
||||
auto_state_calls++;
|
||||
last_auto_state = enabled;
|
||||
}
|
||||
|
||||
gboolean fe_win32_high_contrast_is_enabled (void)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
gboolean fe_win32_try_get_system_dark (gboolean *enabled)
|
||||
{
|
||||
(void) enabled;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void zoitechat_set_theme_post_apply_callback (zoitechat_theme_post_apply_callback callback)
|
||||
{
|
||||
(void) callback;
|
||||
}
|
||||
|
||||
gboolean theme_policy_is_dark_mode_active (unsigned int mode)
|
||||
{
|
||||
return mode == ZOITECHAT_DARK_MODE_DARK;
|
||||
}
|
||||
|
||||
gboolean theme_policy_system_prefers_dark (void)
|
||||
{
|
||||
return stub_system_prefers_dark;
|
||||
}
|
||||
|
||||
gboolean theme_application_apply_mode (unsigned int mode, gboolean *palette_changed)
|
||||
{
|
||||
(void) mode;
|
||||
if (palette_changed)
|
||||
*palette_changed = stub_apply_mode_palette_changed;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void theme_application_reload_input_style (void)
|
||||
{
|
||||
}
|
||||
|
||||
void theme_runtime_dark_set_color (ThemeSemanticToken token, const GdkRGBA *color)
|
||||
{
|
||||
(void) token;
|
||||
(void) color;
|
||||
}
|
||||
|
||||
void theme_runtime_user_set_color (ThemeSemanticToken token, const GdkRGBA *color)
|
||||
{
|
||||
(void) token;
|
||||
(void) color;
|
||||
}
|
||||
|
||||
gboolean theme_runtime_apply_mode (unsigned int mode, gboolean *dark_active)
|
||||
{
|
||||
(void) mode;
|
||||
(void) dark_active;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void theme_css_reload_input_style (gboolean enabled, const PangoFontDescription *font_desc)
|
||||
{
|
||||
(void) enabled;
|
||||
(void) font_desc;
|
||||
}
|
||||
|
||||
void theme_css_apply_palette_widget (GtkWidget *widget, const GdkRGBA *bg, const GdkRGBA *fg,
|
||||
const PangoFontDescription *font_desc)
|
||||
{
|
||||
(void) widget;
|
||||
(void) bg;
|
||||
(void) fg;
|
||||
(void) font_desc;
|
||||
}
|
||||
|
||||
void theme_runtime_load (void)
|
||||
{
|
||||
}
|
||||
|
||||
void theme_runtime_save (void)
|
||||
{
|
||||
}
|
||||
|
||||
gboolean theme_runtime_is_dark_active (void)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void gtkutil_apply_palette (GtkWidget *widget, const GdkRGBA *background, const GdkRGBA *foreground,
|
||||
const PangoFontDescription *font_desc)
|
||||
{
|
||||
(void) widget;
|
||||
(void) background;
|
||||
(void) foreground;
|
||||
(void) font_desc;
|
||||
}
|
||||
|
||||
void theme_get_widget_style_values (ThemeWidgetStyleValues *out_values)
|
||||
{
|
||||
gdk_rgba_parse (&out_values->background, "#101010");
|
||||
gdk_rgba_parse (&out_values->foreground, "#f0f0f0");
|
||||
}
|
||||
|
||||
void fe_win32_apply_native_titlebar (GtkWidget *window, gboolean dark)
|
||||
{
|
||||
(void) window;
|
||||
(void) dark;
|
||||
}
|
||||
|
||||
static void
|
||||
auto_listener (const ThemeChangedEvent *event, gpointer userdata)
|
||||
{
|
||||
(void) userdata;
|
||||
listener_calls++;
|
||||
last_event = *event;
|
||||
}
|
||||
|
||||
static guint
|
||||
immediate_idle_add (GSourceFunc function, gpointer data)
|
||||
{
|
||||
idle_add_calls++;
|
||||
function (data);
|
||||
return next_idle_source_id++;
|
||||
}
|
||||
|
||||
static void
|
||||
reset_state (void)
|
||||
{
|
||||
stub_apply_mode_palette_changed = FALSE;
|
||||
stub_system_prefers_dark = FALSE;
|
||||
auto_state_calls = 0;
|
||||
last_auto_state = FALSE;
|
||||
listener_calls = 0;
|
||||
idle_add_calls = 0;
|
||||
next_idle_source_id = 33;
|
||||
}
|
||||
|
||||
static void
|
||||
test_auto_refresh_dispatches_mode_palette_and_style_reasons (void)
|
||||
{
|
||||
guint listener_id;
|
||||
|
||||
reset_state ();
|
||||
prefs.hex_gui_dark_mode = ZOITECHAT_DARK_MODE_AUTO;
|
||||
stub_apply_mode_palette_changed = TRUE;
|
||||
stub_system_prefers_dark = TRUE;
|
||||
listener_id = theme_listener_register ("auto.refresh", auto_listener, NULL);
|
||||
theme_manager_set_idle_add_func (immediate_idle_add);
|
||||
|
||||
theme_manager_refresh_auto_mode ();
|
||||
|
||||
g_assert_cmpint (idle_add_calls, ==, 1);
|
||||
g_assert_cmpint (auto_state_calls, ==, 2);
|
||||
g_assert_true (last_auto_state);
|
||||
g_assert_cmpint (listener_calls, ==, 1);
|
||||
g_assert_true (theme_changed_event_has_reason (&last_event, THEME_CHANGED_REASON_PALETTE));
|
||||
g_assert_true (theme_changed_event_has_reason (&last_event, THEME_CHANGED_REASON_WIDGET_STYLE));
|
||||
g_assert_true (theme_changed_event_has_reason (&last_event, THEME_CHANGED_REASON_USERLIST));
|
||||
g_assert_true (theme_changed_event_has_reason (&last_event, THEME_CHANGED_REASON_MODE));
|
||||
|
||||
theme_manager_set_idle_add_func (NULL);
|
||||
theme_listener_unregister (listener_id);
|
||||
}
|
||||
|
||||
static void
|
||||
test_auto_refresh_ignores_non_auto_mode (void)
|
||||
{
|
||||
guint listener_id;
|
||||
|
||||
reset_state ();
|
||||
prefs.hex_gui_dark_mode = ZOITECHAT_DARK_MODE_DARK;
|
||||
stub_apply_mode_palette_changed = TRUE;
|
||||
listener_id = theme_listener_register ("auto.nonauto", auto_listener, NULL);
|
||||
theme_manager_set_idle_add_func (immediate_idle_add);
|
||||
|
||||
theme_manager_refresh_auto_mode ();
|
||||
|
||||
g_assert_cmpint (idle_add_calls, ==, 1);
|
||||
g_assert_cmpint (auto_state_calls, ==, 0);
|
||||
g_assert_cmpint (listener_calls, ==, 0);
|
||||
|
||||
theme_manager_set_idle_add_func (NULL);
|
||||
theme_listener_unregister (listener_id);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
g_test_add_func ("/theme/manager/auto_refresh_dispatches_mode_palette_and_style_reasons",
|
||||
test_auto_refresh_dispatches_mode_palette_and_style_reasons);
|
||||
g_test_add_func ("/theme/manager/auto_refresh_ignores_non_auto_mode",
|
||||
test_auto_refresh_ignores_non_auto_mode);
|
||||
return g_test_run ();
|
||||
}
|
||||
273
src/fe-gtk/theme/tests/test-theme-manager-dispatch-routing.c
Normal file
273
src/fe-gtk/theme/tests/test-theme-manager-dispatch-routing.c
Normal file
@@ -0,0 +1,273 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../theme-manager.h"
|
||||
#include "../../fe-gtk.h"
|
||||
#include "../../../common/zoitechat.h"
|
||||
#include "../../../common/zoitechatc.h"
|
||||
|
||||
struct session *current_sess;
|
||||
struct session *current_tab;
|
||||
struct session *lastact_sess;
|
||||
struct zoitechatprefs prefs;
|
||||
|
||||
static int window_refresh_calls;
|
||||
static int widget_style_calls;
|
||||
static int palette_reapply_calls;
|
||||
static int unmatched_listener_calls;
|
||||
|
||||
void setup_apply_real (const ThemeChangedEvent *event)
|
||||
{
|
||||
(void) event;
|
||||
}
|
||||
|
||||
gboolean fe_dark_mode_is_enabled_for (unsigned int mode)
|
||||
{
|
||||
return mode == ZOITECHAT_DARK_MODE_DARK;
|
||||
}
|
||||
|
||||
void fe_set_auto_dark_mode_state (gboolean enabled)
|
||||
{
|
||||
(void) enabled;
|
||||
}
|
||||
|
||||
gboolean fe_win32_high_contrast_is_enabled (void)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
gboolean fe_win32_try_get_system_dark (gboolean *enabled)
|
||||
{
|
||||
(void) enabled;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void zoitechat_set_theme_post_apply_callback (zoitechat_theme_post_apply_callback callback)
|
||||
{
|
||||
(void) callback;
|
||||
}
|
||||
|
||||
gboolean theme_policy_is_dark_mode_active (unsigned int mode)
|
||||
{
|
||||
return mode == ZOITECHAT_DARK_MODE_DARK;
|
||||
}
|
||||
|
||||
gboolean theme_policy_system_prefers_dark (void)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
gboolean theme_application_apply_mode (unsigned int mode, gboolean *palette_changed)
|
||||
{
|
||||
(void) mode;
|
||||
if (palette_changed)
|
||||
*palette_changed = FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void theme_application_reload_input_style (void)
|
||||
{
|
||||
}
|
||||
|
||||
void theme_runtime_dark_set_color (ThemeSemanticToken token, const GdkRGBA *color)
|
||||
{
|
||||
(void) token;
|
||||
(void) color;
|
||||
}
|
||||
|
||||
void theme_runtime_user_set_color (ThemeSemanticToken token, const GdkRGBA *color)
|
||||
{
|
||||
(void) token;
|
||||
(void) color;
|
||||
}
|
||||
|
||||
gboolean theme_runtime_apply_mode (unsigned int mode, gboolean *dark_active)
|
||||
{
|
||||
(void) mode;
|
||||
(void) dark_active;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void theme_css_reload_input_style (gboolean enabled, const PangoFontDescription *font_desc)
|
||||
{
|
||||
(void) enabled;
|
||||
(void) font_desc;
|
||||
}
|
||||
|
||||
void theme_css_apply_palette_widget (GtkWidget *widget, const GdkRGBA *bg, const GdkRGBA *fg,
|
||||
const PangoFontDescription *font_desc)
|
||||
{
|
||||
(void) widget;
|
||||
(void) bg;
|
||||
(void) fg;
|
||||
(void) font_desc;
|
||||
}
|
||||
|
||||
void theme_runtime_load (void)
|
||||
{
|
||||
}
|
||||
|
||||
void theme_runtime_save (void)
|
||||
{
|
||||
}
|
||||
|
||||
gboolean theme_runtime_is_dark_active (void)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void gtkutil_apply_palette (GtkWidget *widget, const GdkRGBA *background, const GdkRGBA *foreground,
|
||||
const PangoFontDescription *font_desc)
|
||||
{
|
||||
(void) widget;
|
||||
(void) background;
|
||||
(void) foreground;
|
||||
(void) font_desc;
|
||||
}
|
||||
|
||||
void theme_get_widget_style_values (ThemeWidgetStyleValues *out_values)
|
||||
{
|
||||
gdk_rgba_parse (&out_values->background, "#101010");
|
||||
gdk_rgba_parse (&out_values->foreground, "#f0f0f0");
|
||||
}
|
||||
|
||||
void fe_win32_apply_native_titlebar (GtkWidget *window, gboolean dark)
|
||||
{
|
||||
(void) window;
|
||||
(void) dark;
|
||||
}
|
||||
|
||||
static void
|
||||
window_refresh_listener (const ThemeChangedEvent *event, gpointer userdata)
|
||||
{
|
||||
(void) userdata;
|
||||
if (theme_changed_event_has_reason (event, THEME_CHANGED_REASON_PALETTE))
|
||||
palette_reapply_calls++;
|
||||
if (theme_changed_event_has_reason (event, THEME_CHANGED_REASON_PALETTE) ||
|
||||
theme_changed_event_has_reason (event, THEME_CHANGED_REASON_WIDGET_STYLE))
|
||||
window_refresh_calls++;
|
||||
}
|
||||
|
||||
static void
|
||||
widget_style_listener (const ThemeChangedEvent *event, gpointer userdata)
|
||||
{
|
||||
(void) userdata;
|
||||
if (theme_changed_event_has_reason (event, THEME_CHANGED_REASON_WIDGET_STYLE))
|
||||
widget_style_calls++;
|
||||
}
|
||||
|
||||
static void
|
||||
unmatched_reason_listener (const ThemeChangedEvent *event, gpointer userdata)
|
||||
{
|
||||
(void) userdata;
|
||||
if (theme_changed_event_has_reason (event, THEME_CHANGED_REASON_IDENTD))
|
||||
unmatched_listener_calls++;
|
||||
}
|
||||
|
||||
static void
|
||||
reset_counters (void)
|
||||
{
|
||||
window_refresh_calls = 0;
|
||||
widget_style_calls = 0;
|
||||
palette_reapply_calls = 0;
|
||||
unmatched_listener_calls = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
test_dispatch_filters_reasons_across_multiple_subscribers (void)
|
||||
{
|
||||
guint listener_window;
|
||||
guint listener_widget;
|
||||
guint listener_unmatched;
|
||||
|
||||
reset_counters ();
|
||||
listener_window = theme_listener_register ("refresh.window", window_refresh_listener, NULL);
|
||||
listener_widget = theme_listener_register ("refresh.widget", widget_style_listener, NULL);
|
||||
listener_unmatched = theme_listener_register ("refresh.unmatched", unmatched_reason_listener, NULL);
|
||||
|
||||
theme_manager_dispatch_changed (THEME_CHANGED_REASON_PALETTE);
|
||||
g_assert_cmpint (window_refresh_calls, ==, 1);
|
||||
g_assert_cmpint (palette_reapply_calls, ==, 1);
|
||||
g_assert_cmpint (widget_style_calls, ==, 0);
|
||||
g_assert_cmpint (unmatched_listener_calls, ==, 0);
|
||||
|
||||
theme_manager_dispatch_changed (THEME_CHANGED_REASON_WIDGET_STYLE | THEME_CHANGED_REASON_USERLIST);
|
||||
g_assert_cmpint (window_refresh_calls, ==, 2);
|
||||
g_assert_cmpint (palette_reapply_calls, ==, 1);
|
||||
g_assert_cmpint (widget_style_calls, ==, 1);
|
||||
g_assert_cmpint (unmatched_listener_calls, ==, 0);
|
||||
|
||||
theme_manager_dispatch_changed (THEME_CHANGED_REASON_LAYOUT);
|
||||
g_assert_cmpint (window_refresh_calls, ==, 2);
|
||||
g_assert_cmpint (palette_reapply_calls, ==, 1);
|
||||
g_assert_cmpint (widget_style_calls, ==, 1);
|
||||
g_assert_cmpint (unmatched_listener_calls, ==, 0);
|
||||
|
||||
theme_listener_unregister (listener_unmatched);
|
||||
theme_listener_unregister (listener_widget);
|
||||
theme_listener_unregister (listener_window);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_preferences_change_synthesizes_theme_reasons (void)
|
||||
{
|
||||
struct zoitechatprefs old_prefs = { 0 };
|
||||
struct zoitechatprefs new_prefs = { 0 };
|
||||
ThemeChangedEvent event;
|
||||
gboolean color_change = TRUE;
|
||||
|
||||
prefs.hex_gui_dark_mode = ZOITECHAT_DARK_MODE_DARK;
|
||||
old_prefs.hex_gui_dark_mode = prefs.hex_gui_dark_mode;
|
||||
new_prefs.hex_gui_dark_mode = prefs.hex_gui_dark_mode;
|
||||
strcpy (old_prefs.hex_text_background, "old.png");
|
||||
strcpy (new_prefs.hex_text_background, "new.png");
|
||||
old_prefs.hex_gui_tab_dots = 0;
|
||||
new_prefs.hex_gui_tab_dots = 1;
|
||||
old_prefs.hex_identd_port = 113;
|
||||
new_prefs.hex_identd_port = 114;
|
||||
old_prefs.hex_gui_ulist_color = 0;
|
||||
new_prefs.hex_gui_ulist_color = 1;
|
||||
|
||||
event = theme_manager_on_preferences_changed (&old_prefs, &new_prefs, prefs.hex_gui_dark_mode, &color_change);
|
||||
|
||||
g_assert_true (theme_changed_event_has_reason (&event, THEME_CHANGED_REASON_PIXMAP));
|
||||
g_assert_true (theme_changed_event_has_reason (&event, THEME_CHANGED_REASON_LAYOUT));
|
||||
g_assert_true (theme_changed_event_has_reason (&event, THEME_CHANGED_REASON_IDENTD));
|
||||
g_assert_true (theme_changed_event_has_reason (&event, THEME_CHANGED_REASON_USERLIST));
|
||||
g_assert_true (theme_changed_event_has_reason (&event, THEME_CHANGED_REASON_WIDGET_STYLE));
|
||||
}
|
||||
|
||||
static void
|
||||
test_preferences_change_omits_reasons_without_differences (void)
|
||||
{
|
||||
struct zoitechatprefs old_prefs = { 0 };
|
||||
struct zoitechatprefs new_prefs = { 0 };
|
||||
ThemeChangedEvent event;
|
||||
gboolean color_change = FALSE;
|
||||
|
||||
prefs.hex_gui_dark_mode = ZOITECHAT_DARK_MODE_DARK;
|
||||
old_prefs.hex_gui_dark_mode = prefs.hex_gui_dark_mode;
|
||||
new_prefs.hex_gui_dark_mode = prefs.hex_gui_dark_mode;
|
||||
|
||||
event = theme_manager_on_preferences_changed (&old_prefs, &new_prefs, prefs.hex_gui_dark_mode, &color_change);
|
||||
|
||||
g_assert_false (theme_changed_event_has_reason (&event, THEME_CHANGED_REASON_PIXMAP));
|
||||
g_assert_false (theme_changed_event_has_reason (&event, THEME_CHANGED_REASON_LAYOUT));
|
||||
g_assert_false (theme_changed_event_has_reason (&event, THEME_CHANGED_REASON_IDENTD));
|
||||
g_assert_false (theme_changed_event_has_reason (&event, THEME_CHANGED_REASON_USERLIST));
|
||||
g_assert_false (theme_changed_event_has_reason (&event, THEME_CHANGED_REASON_WIDGET_STYLE));
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
g_test_add_func ("/theme/manager/dispatch_filters_reasons_across_multiple_subscribers",
|
||||
test_dispatch_filters_reasons_across_multiple_subscribers);
|
||||
g_test_add_func ("/theme/manager/preferences_change_synthesizes_theme_reasons",
|
||||
test_preferences_change_synthesizes_theme_reasons);
|
||||
g_test_add_func ("/theme/manager/preferences_change_omits_reasons_without_differences",
|
||||
test_preferences_change_omits_reasons_without_differences);
|
||||
return g_test_run ();
|
||||
}
|
||||
368
src/fe-gtk/theme/tests/test-theme-manager-policy.c
Normal file
368
src/fe-gtk/theme/tests/test-theme-manager-policy.c
Normal file
@@ -0,0 +1,368 @@
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include "../theme-palette.h"
|
||||
#include "../theme-manager.h"
|
||||
#include "../../fe-gtk.h"
|
||||
#include "../../../common/zoitechat.h"
|
||||
#include "../../../common/zoitechatc.h"
|
||||
|
||||
struct session *current_sess;
|
||||
struct session *current_tab;
|
||||
struct session *lastact_sess;
|
||||
struct zoitechatprefs prefs;
|
||||
|
||||
static gboolean stub_policy_dark;
|
||||
static unsigned int stub_policy_mode;
|
||||
static gboolean stub_apply_mode_result;
|
||||
static gboolean stub_apply_mode_palette_changed;
|
||||
static int stub_dark_set_calls;
|
||||
static int stub_user_set_calls;
|
||||
static int stub_apply_mode_calls;
|
||||
static int stub_reload_style_calls;
|
||||
static ThemeSemanticToken stub_last_dark_token;
|
||||
static ThemeSemanticToken stub_last_user_token;
|
||||
|
||||
static int listener_a_calls;
|
||||
static int listener_b_calls;
|
||||
static ThemeChangedEvent listener_last_event;
|
||||
|
||||
void setup_apply_real (const ThemeChangedEvent *event)
|
||||
{
|
||||
(void) event;
|
||||
}
|
||||
|
||||
gboolean fe_dark_mode_is_enabled_for (unsigned int mode)
|
||||
{
|
||||
return mode == ZOITECHAT_DARK_MODE_DARK;
|
||||
}
|
||||
|
||||
void fe_set_auto_dark_mode_state (gboolean enabled)
|
||||
{
|
||||
(void) enabled;
|
||||
}
|
||||
|
||||
gboolean fe_win32_high_contrast_is_enabled (void)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
gboolean fe_win32_try_get_system_dark (gboolean *enabled)
|
||||
{
|
||||
(void) enabled;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void zoitechat_set_theme_post_apply_callback (zoitechat_theme_post_apply_callback callback)
|
||||
{
|
||||
(void) callback;
|
||||
}
|
||||
|
||||
gboolean theme_policy_is_dark_mode_active (unsigned int mode)
|
||||
{
|
||||
stub_policy_mode = mode;
|
||||
return stub_policy_dark;
|
||||
}
|
||||
|
||||
gboolean theme_application_apply_mode (unsigned int mode, gboolean *palette_changed)
|
||||
{
|
||||
(void) mode;
|
||||
if (palette_changed)
|
||||
*palette_changed = stub_apply_mode_palette_changed;
|
||||
return stub_apply_mode_result;
|
||||
}
|
||||
|
||||
void theme_runtime_dark_set_color (ThemeSemanticToken token, const GdkRGBA *color)
|
||||
{
|
||||
(void) color;
|
||||
stub_dark_set_calls++;
|
||||
stub_last_dark_token = token;
|
||||
}
|
||||
|
||||
void theme_runtime_user_set_color (ThemeSemanticToken token, const GdkRGBA *color)
|
||||
{
|
||||
(void) color;
|
||||
stub_user_set_calls++;
|
||||
stub_last_user_token = token;
|
||||
}
|
||||
|
||||
gboolean theme_runtime_apply_mode (unsigned int mode, gboolean *dark_active)
|
||||
{
|
||||
(void) mode;
|
||||
(void) dark_active;
|
||||
stub_apply_mode_calls++;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void theme_application_reload_input_style (void)
|
||||
{
|
||||
stub_reload_style_calls++;
|
||||
}
|
||||
|
||||
|
||||
void theme_css_reload_input_style (gboolean enabled, const PangoFontDescription *font_desc)
|
||||
{
|
||||
(void) enabled;
|
||||
(void) font_desc;
|
||||
}
|
||||
|
||||
void theme_css_apply_palette_widget (GtkWidget *widget, const GdkRGBA *bg, const GdkRGBA *fg,
|
||||
const PangoFontDescription *font_desc)
|
||||
{
|
||||
(void) widget;
|
||||
(void) bg;
|
||||
(void) fg;
|
||||
(void) font_desc;
|
||||
}
|
||||
|
||||
void theme_runtime_load (void)
|
||||
{
|
||||
}
|
||||
|
||||
void theme_runtime_save (void)
|
||||
{
|
||||
}
|
||||
|
||||
gboolean theme_runtime_is_dark_active (void)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
gboolean theme_policy_system_prefers_dark (void)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void gtkutil_apply_palette (GtkWidget *widget, const GdkRGBA *background, const GdkRGBA *foreground,
|
||||
const PangoFontDescription *font_desc)
|
||||
{
|
||||
(void) widget;
|
||||
(void) background;
|
||||
(void) foreground;
|
||||
(void) font_desc;
|
||||
}
|
||||
|
||||
void theme_get_widget_style_values (ThemeWidgetStyleValues *out_values)
|
||||
{
|
||||
gdk_rgba_parse (&out_values->background, "#101010");
|
||||
gdk_rgba_parse (&out_values->foreground, "#f0f0f0");
|
||||
}
|
||||
|
||||
void fe_win32_apply_native_titlebar (GtkWidget *window, gboolean dark)
|
||||
{
|
||||
(void) window;
|
||||
(void) dark;
|
||||
}
|
||||
|
||||
static void
|
||||
listener_a (const ThemeChangedEvent *event, gpointer userdata)
|
||||
{
|
||||
(void) userdata;
|
||||
listener_a_calls++;
|
||||
listener_last_event = *event;
|
||||
}
|
||||
|
||||
static void
|
||||
listener_b (const ThemeChangedEvent *event, gpointer userdata)
|
||||
{
|
||||
(void) userdata;
|
||||
(void) event;
|
||||
listener_b_calls++;
|
||||
}
|
||||
|
||||
static void
|
||||
reset_manager_stubs (void)
|
||||
{
|
||||
stub_policy_dark = FALSE;
|
||||
stub_policy_mode = 999;
|
||||
stub_apply_mode_result = TRUE;
|
||||
stub_apply_mode_palette_changed = FALSE;
|
||||
stub_dark_set_calls = 0;
|
||||
stub_user_set_calls = 0;
|
||||
stub_apply_mode_calls = 0;
|
||||
stub_reload_style_calls = 0;
|
||||
stub_last_dark_token = -1;
|
||||
stub_last_user_token = -1;
|
||||
listener_a_calls = 0;
|
||||
listener_b_calls = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
test_token_roundtrip (void)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < theme_palette_token_def_count (); i++)
|
||||
{
|
||||
const ThemePaletteTokenDef *def = theme_palette_token_def_at (i);
|
||||
int legacy_idx = -1;
|
||||
ThemeSemanticToken token = THEME_TOKEN_MIRC_0;
|
||||
|
||||
g_assert_nonnull (def);
|
||||
g_assert_true (theme_palette_token_to_legacy_index (def->token, &legacy_idx));
|
||||
g_assert_cmpint (legacy_idx, ==, def->legacy_index);
|
||||
g_assert_true (theme_palette_legacy_index_to_token (legacy_idx, &token));
|
||||
g_assert_cmpint (token, ==, def->token);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_policy_mode_resolution (void)
|
||||
{
|
||||
g_assert_false (theme_policy_is_dark_mode_active (ZOITECHAT_DARK_MODE_LIGHT));
|
||||
g_assert_true (theme_policy_is_dark_mode_active (ZOITECHAT_DARK_MODE_DARK));
|
||||
}
|
||||
|
||||
static void
|
||||
test_manager_set_token_color_routes_by_mode (void)
|
||||
{
|
||||
GdkRGBA color = { 0.1, 0.2, 0.3, 1.0 };
|
||||
gboolean palette_changed = FALSE;
|
||||
|
||||
reset_manager_stubs ();
|
||||
stub_policy_dark = FALSE;
|
||||
theme_manager_set_token_color (ZOITECHAT_DARK_MODE_LIGHT, THEME_TOKEN_MIRC_2, &color, &palette_changed);
|
||||
g_assert_cmpint (stub_policy_mode, ==, ZOITECHAT_DARK_MODE_LIGHT);
|
||||
g_assert_cmpint (stub_user_set_calls, ==, 1);
|
||||
g_assert_cmpint (stub_dark_set_calls, ==, 0);
|
||||
g_assert_cmpint (stub_apply_mode_calls, ==, 1);
|
||||
g_assert_cmpint (stub_reload_style_calls, ==, 1);
|
||||
g_assert_true (palette_changed);
|
||||
|
||||
reset_manager_stubs ();
|
||||
stub_policy_dark = TRUE;
|
||||
theme_manager_set_token_color (ZOITECHAT_DARK_MODE_DARK, THEME_TOKEN_MIRC_2, &color, &palette_changed);
|
||||
g_assert_cmpint (stub_policy_mode, ==, ZOITECHAT_DARK_MODE_DARK);
|
||||
g_assert_cmpint (stub_user_set_calls, ==, 0);
|
||||
g_assert_cmpint (stub_dark_set_calls, ==, 1);
|
||||
|
||||
reset_manager_stubs ();
|
||||
stub_policy_dark = TRUE;
|
||||
theme_manager_set_token_color (ZOITECHAT_DARK_MODE_AUTO, THEME_TOKEN_MIRC_2, &color, &palette_changed);
|
||||
g_assert_cmpint (stub_policy_mode, ==, ZOITECHAT_DARK_MODE_AUTO);
|
||||
g_assert_cmpint (stub_user_set_calls, ==, 0);
|
||||
g_assert_cmpint (stub_dark_set_calls, ==, 1);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_manager_set_token_color_routes_setup_indexes (void)
|
||||
{
|
||||
GdkRGBA color = { 0.7, 0.3, 0.2, 1.0 };
|
||||
gboolean palette_changed = FALSE;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < theme_palette_token_def_count (); i++)
|
||||
{
|
||||
const ThemePaletteTokenDef *def = theme_palette_token_def_at (i);
|
||||
|
||||
g_assert_nonnull (def);
|
||||
reset_manager_stubs ();
|
||||
stub_policy_dark = FALSE;
|
||||
palette_changed = FALSE;
|
||||
theme_manager_set_token_color (ZOITECHAT_DARK_MODE_LIGHT, def->token, &color, &palette_changed);
|
||||
g_assert_cmpint (stub_user_set_calls, ==, 1);
|
||||
g_assert_cmpint (stub_last_user_token, ==, def->token);
|
||||
g_assert_cmpint (stub_dark_set_calls, ==, 0);
|
||||
g_assert_true (palette_changed);
|
||||
|
||||
reset_manager_stubs ();
|
||||
stub_policy_dark = TRUE;
|
||||
palette_changed = FALSE;
|
||||
theme_manager_set_token_color (ZOITECHAT_DARK_MODE_DARK, def->token, &color, &palette_changed);
|
||||
g_assert_cmpint (stub_dark_set_calls, ==, 1);
|
||||
g_assert_cmpint (stub_last_dark_token, ==, def->token);
|
||||
g_assert_cmpint (stub_user_set_calls, ==, 0);
|
||||
g_assert_true (palette_changed);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_manager_listener_registration_dispatch_and_unregister (void)
|
||||
{
|
||||
guint id_a;
|
||||
guint id_b;
|
||||
|
||||
reset_manager_stubs ();
|
||||
id_a = theme_listener_register ("test.a", listener_a, NULL);
|
||||
id_b = theme_listener_register ("test.b", listener_b, NULL);
|
||||
g_assert_cmpuint (id_a, >, 0);
|
||||
g_assert_cmpuint (id_b, >, 0);
|
||||
|
||||
theme_manager_dispatch_changed (THEME_CHANGED_REASON_PIXMAP | THEME_CHANGED_REASON_USERLIST | THEME_CHANGED_REASON_IDENTD | THEME_CHANGED_REASON_WIDGET_STYLE);
|
||||
g_assert_cmpint (listener_a_calls, ==, 1);
|
||||
g_assert_cmpint (listener_b_calls, ==, 1);
|
||||
g_assert_true (theme_changed_event_has_reason (&listener_last_event, THEME_CHANGED_REASON_PIXMAP));
|
||||
g_assert_true (theme_changed_event_has_reason (&listener_last_event, THEME_CHANGED_REASON_USERLIST));
|
||||
g_assert_false (theme_changed_event_has_reason (&listener_last_event, THEME_CHANGED_REASON_LAYOUT));
|
||||
g_assert_true (theme_changed_event_has_reason (&listener_last_event, THEME_CHANGED_REASON_IDENTD));
|
||||
g_assert_true (theme_changed_event_has_reason (&listener_last_event, THEME_CHANGED_REASON_WIDGET_STYLE));
|
||||
|
||||
theme_listener_unregister (id_a);
|
||||
theme_manager_dispatch_changed (THEME_CHANGED_REASON_PIXMAP | THEME_CHANGED_REASON_LAYOUT | THEME_CHANGED_REASON_WIDGET_STYLE);
|
||||
g_assert_cmpint (listener_a_calls, ==, 1);
|
||||
g_assert_cmpint (listener_b_calls, ==, 2);
|
||||
|
||||
theme_listener_unregister (id_b);
|
||||
}
|
||||
|
||||
static void
|
||||
test_manager_window_attach_detach_idempotence (void)
|
||||
{
|
||||
GtkWidget *window;
|
||||
gulong *first_handler_ptr;
|
||||
gulong first_handler_id;
|
||||
gulong *second_handler_ptr;
|
||||
gulong second_handler_id;
|
||||
|
||||
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
||||
g_assert_nonnull (window);
|
||||
|
||||
theme_manager_attach_window (window);
|
||||
first_handler_ptr = g_object_get_data (G_OBJECT (window), "theme-manager-window-destroy-handler");
|
||||
g_assert_nonnull (first_handler_ptr);
|
||||
first_handler_id = *first_handler_ptr;
|
||||
g_assert_cmpuint (first_handler_id, >, 0);
|
||||
g_assert_true (g_signal_handler_is_connected (G_OBJECT (window), first_handler_id));
|
||||
|
||||
theme_manager_attach_window (window);
|
||||
second_handler_ptr = g_object_get_data (G_OBJECT (window), "theme-manager-window-destroy-handler");
|
||||
g_assert_nonnull (second_handler_ptr);
|
||||
g_assert_true (first_handler_ptr == second_handler_ptr);
|
||||
g_assert_cmpuint (*second_handler_ptr, ==, first_handler_id);
|
||||
|
||||
theme_manager_detach_window (window);
|
||||
g_assert_null (g_object_get_data (G_OBJECT (window), "theme-manager-window-destroy-handler"));
|
||||
g_assert_false (g_signal_handler_is_connected (G_OBJECT (window), first_handler_id));
|
||||
|
||||
theme_manager_detach_window (window);
|
||||
g_assert_null (g_object_get_data (G_OBJECT (window), "theme-manager-window-destroy-handler"));
|
||||
|
||||
theme_manager_attach_window (window);
|
||||
second_handler_ptr = g_object_get_data (G_OBJECT (window), "theme-manager-window-destroy-handler");
|
||||
g_assert_nonnull (second_handler_ptr);
|
||||
second_handler_id = *second_handler_ptr;
|
||||
g_assert_cmpuint (second_handler_id, >, 0);
|
||||
g_assert_true (g_signal_handler_is_connected (G_OBJECT (window), second_handler_id));
|
||||
|
||||
theme_manager_detach_window (window);
|
||||
g_assert_null (g_object_get_data (G_OBJECT (window), "theme-manager-window-destroy-handler"));
|
||||
g_assert_false (g_signal_handler_is_connected (G_OBJECT (window), second_handler_id));
|
||||
|
||||
gtk_widget_destroy (window);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
g_test_add_func ("/theme/palette/token_roundtrip", test_token_roundtrip);
|
||||
g_test_add_func ("/theme/policy/mode_resolution", test_policy_mode_resolution);
|
||||
g_test_add_func ("/theme/manager/set_token_color_routes_by_mode", test_manager_set_token_color_routes_by_mode);
|
||||
g_test_add_func ("/theme/manager/set_token_color_routes_setup_indexes",
|
||||
test_manager_set_token_color_routes_setup_indexes);
|
||||
g_test_add_func ("/theme/manager/listener_registration_dispatch_and_unregister",
|
||||
test_manager_listener_registration_dispatch_and_unregister);
|
||||
g_test_add_func ("/theme/manager/window_attach_detach_idempotence",
|
||||
test_manager_window_attach_detach_idempotence);
|
||||
return g_test_run ();
|
||||
}
|
||||
281
src/fe-gtk/theme/tests/test-theme-runtime-persistence.c
Normal file
281
src/fe-gtk/theme/tests/test-theme-runtime-persistence.c
Normal file
@@ -0,0 +1,281 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <glib/gstdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../theme-runtime.h"
|
||||
#include "../../../common/zoitechat.h"
|
||||
#include "../../../common/zoitechatc.h"
|
||||
|
||||
struct session *current_sess;
|
||||
struct session *current_tab;
|
||||
struct session *lastact_sess;
|
||||
struct zoitechatprefs prefs;
|
||||
|
||||
static char *test_home_dir;
|
||||
|
||||
static gboolean
|
||||
read_line_value (const char *cfg, const char *key, char *out, gsize out_len)
|
||||
{
|
||||
char *pattern;
|
||||
char *pos;
|
||||
char *line_end;
|
||||
gsize value_len;
|
||||
|
||||
pattern = g_strdup_printf ("%s = ", key);
|
||||
pos = g_strstr_len (cfg, -1, pattern);
|
||||
g_free (pattern);
|
||||
if (!pos)
|
||||
return FALSE;
|
||||
|
||||
pos = strchr (pos, '=');
|
||||
if (!pos)
|
||||
return FALSE;
|
||||
pos++;
|
||||
while (*pos == ' ')
|
||||
pos++;
|
||||
|
||||
line_end = strchr (pos, '\n');
|
||||
if (!line_end)
|
||||
line_end = pos + strlen (pos);
|
||||
value_len = (gsize) (line_end - pos);
|
||||
if (value_len + 1 > out_len)
|
||||
return FALSE;
|
||||
memcpy (out, pos, value_len);
|
||||
out[value_len] = '\0';
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int
|
||||
cfg_get_color (char *cfg, char *var, guint16 *r, guint16 *g, guint16 *b)
|
||||
{
|
||||
char value[128];
|
||||
|
||||
if (!read_line_value (cfg, var, value, sizeof (value)))
|
||||
return 0;
|
||||
if (sscanf (value, "%04hx %04hx %04hx", r, g, b) != 3)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
cfg_get_int (char *cfg, char *var)
|
||||
{
|
||||
char value[128];
|
||||
|
||||
if (!read_line_value (cfg, var, value, sizeof (value)))
|
||||
return 0;
|
||||
return atoi (value);
|
||||
}
|
||||
|
||||
int
|
||||
cfg_put_color (int fh, guint16 r, guint16 g, guint16 b, char *var)
|
||||
{
|
||||
char line[256];
|
||||
int len;
|
||||
|
||||
len = g_snprintf (line, sizeof line, "%s = %04hx %04hx %04hx\n", var, r, g, b);
|
||||
if (len < 0)
|
||||
return 0;
|
||||
return write (fh, line, (size_t) len) == len;
|
||||
}
|
||||
|
||||
int
|
||||
cfg_put_int (int fh, int value, char *var)
|
||||
{
|
||||
char line[128];
|
||||
int len;
|
||||
|
||||
len = g_snprintf (line, sizeof line, "%s = %d\n", var, value);
|
||||
if (len < 0)
|
||||
return 0;
|
||||
return write (fh, line, (size_t) len) == len;
|
||||
}
|
||||
|
||||
int
|
||||
zoitechat_open_file (const char *file, int flags, int mode, int xof_flags)
|
||||
{
|
||||
char *path;
|
||||
int fd;
|
||||
|
||||
(void) xof_flags;
|
||||
path = g_build_filename (test_home_dir, file, NULL);
|
||||
fd = g_open (path, flags, mode);
|
||||
g_free (path);
|
||||
return fd;
|
||||
}
|
||||
|
||||
gboolean
|
||||
fe_dark_mode_is_enabled_for (unsigned int mode)
|
||||
{
|
||||
return mode == ZOITECHAT_DARK_MODE_DARK;
|
||||
}
|
||||
|
||||
gboolean
|
||||
theme_policy_system_prefers_dark (void)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
theme_policy_is_dark_mode_active (unsigned int mode)
|
||||
{
|
||||
return mode == ZOITECHAT_DARK_MODE_DARK;
|
||||
}
|
||||
|
||||
static void
|
||||
setup_temp_home (void)
|
||||
{
|
||||
if (test_home_dir)
|
||||
return;
|
||||
test_home_dir = g_dir_make_tmp ("zoitechat-theme-tests-XXXXXX", NULL);
|
||||
g_assert_nonnull (test_home_dir);
|
||||
}
|
||||
|
||||
static char *
|
||||
read_colors_conf (void)
|
||||
{
|
||||
char *path;
|
||||
char *content = NULL;
|
||||
gsize length = 0;
|
||||
gboolean ok;
|
||||
|
||||
path = g_build_filename (test_home_dir, "colors.conf", NULL);
|
||||
ok = g_file_get_contents (path, &content, &length, NULL);
|
||||
g_free (path);
|
||||
g_assert_true (ok);
|
||||
g_assert_cmpuint (length, >, 0);
|
||||
return content;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
colors_equal (const GdkRGBA *a, const GdkRGBA *b)
|
||||
{
|
||||
return a->red == b->red && a->green == b->green && a->blue == b->blue;
|
||||
}
|
||||
|
||||
static void
|
||||
apply_ui_color_edit (unsigned int mode, ThemeSemanticToken token, const char *hex)
|
||||
{
|
||||
GdkRGBA color;
|
||||
|
||||
g_assert_true (gdk_rgba_parse (&color, hex));
|
||||
if (theme_policy_is_dark_mode_active (mode))
|
||||
theme_runtime_dark_set_color (token, &color);
|
||||
else
|
||||
theme_runtime_user_set_color (token, &color);
|
||||
theme_runtime_apply_mode (mode, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
test_persistence_roundtrip_light_and_dark (void)
|
||||
{
|
||||
GdkRGBA light_color;
|
||||
GdkRGBA dark_color;
|
||||
GdkRGBA loaded;
|
||||
char *cfg;
|
||||
|
||||
setup_temp_home ();
|
||||
theme_runtime_load ();
|
||||
|
||||
gdk_rgba_parse (&light_color, "#123456");
|
||||
theme_runtime_user_set_color (THEME_TOKEN_MIRC_0, &light_color);
|
||||
theme_runtime_apply_dark_mode (FALSE);
|
||||
|
||||
theme_runtime_apply_dark_mode (TRUE);
|
||||
gdk_rgba_parse (&dark_color, "#abcdef");
|
||||
theme_runtime_dark_set_color (THEME_TOKEN_MIRC_0, &dark_color);
|
||||
|
||||
theme_runtime_save ();
|
||||
cfg = read_colors_conf ();
|
||||
g_assert_nonnull (g_strstr_len (cfg, -1, "theme.mode.light.token.mirc_0"));
|
||||
g_assert_nonnull (g_strstr_len (cfg, -1, "theme.mode.dark.token.mirc_0"));
|
||||
g_assert_null (g_strstr_len (cfg, -1, "color_0 = "));
|
||||
g_assert_null (g_strstr_len (cfg, -1, "dark_color_0 = "));
|
||||
g_free (cfg);
|
||||
|
||||
theme_runtime_load ();
|
||||
theme_runtime_apply_dark_mode (FALSE);
|
||||
g_assert_true (theme_runtime_get_color (THEME_TOKEN_MIRC_0, &loaded));
|
||||
g_assert_true (colors_equal (&light_color, &loaded));
|
||||
|
||||
theme_runtime_apply_dark_mode (TRUE);
|
||||
g_assert_true (theme_runtime_get_color (THEME_TOKEN_MIRC_0, &loaded));
|
||||
g_assert_true (colors_equal (&dark_color, &loaded));
|
||||
}
|
||||
|
||||
static void
|
||||
test_loads_legacy_color_keys_via_migration_loader (void)
|
||||
{
|
||||
char *path;
|
||||
const char *legacy_cfg =
|
||||
"color_0 = 1111 2222 3333\n"
|
||||
"dark_color_0 = aaaa bbbb cccc\n";
|
||||
GdkRGBA loaded;
|
||||
GdkRGBA light_expected;
|
||||
GdkRGBA dark_expected;
|
||||
gboolean ok;
|
||||
|
||||
setup_temp_home ();
|
||||
path = g_build_filename (test_home_dir, "colors.conf", NULL);
|
||||
ok = g_file_set_contents (path, legacy_cfg, -1, NULL);
|
||||
g_free (path);
|
||||
g_assert_true (ok);
|
||||
|
||||
theme_runtime_load ();
|
||||
|
||||
gdk_rgba_parse (&light_expected, "#111122223333");
|
||||
gdk_rgba_parse (&dark_expected, "#aaaabbbbcccc");
|
||||
|
||||
theme_runtime_apply_dark_mode (FALSE);
|
||||
g_assert_true (theme_runtime_get_color (THEME_TOKEN_MIRC_0, &loaded));
|
||||
g_assert_true (colors_equal (&loaded, &light_expected));
|
||||
|
||||
theme_runtime_apply_dark_mode (TRUE);
|
||||
g_assert_true (theme_runtime_get_color (THEME_TOKEN_MIRC_0, &loaded));
|
||||
g_assert_true (colors_equal (&loaded, &dark_expected));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_ui_edits_persist_without_legacy_array_mutation (void)
|
||||
{
|
||||
GdkRGBA light_loaded;
|
||||
GdkRGBA dark_loaded;
|
||||
GdkRGBA light_expected;
|
||||
GdkRGBA dark_expected;
|
||||
|
||||
setup_temp_home ();
|
||||
theme_runtime_load ();
|
||||
|
||||
apply_ui_color_edit (ZOITECHAT_DARK_MODE_LIGHT, THEME_TOKEN_SELECTION_FOREGROUND, "#224466");
|
||||
apply_ui_color_edit (ZOITECHAT_DARK_MODE_DARK, THEME_TOKEN_SELECTION_FOREGROUND, "#88aacc");
|
||||
theme_runtime_save ();
|
||||
|
||||
theme_runtime_load ();
|
||||
theme_runtime_apply_mode (ZOITECHAT_DARK_MODE_LIGHT, NULL);
|
||||
g_assert_true (theme_runtime_get_color (THEME_TOKEN_SELECTION_FOREGROUND, &light_loaded));
|
||||
g_assert_true (gdk_rgba_parse (&light_expected, "#224466"));
|
||||
g_assert_true (colors_equal (&light_loaded, &light_expected));
|
||||
|
||||
theme_runtime_apply_mode (ZOITECHAT_DARK_MODE_DARK, NULL);
|
||||
g_assert_true (theme_runtime_get_color (THEME_TOKEN_SELECTION_FOREGROUND, &dark_loaded));
|
||||
g_assert_true (gdk_rgba_parse (&dark_expected, "#88aacc"));
|
||||
g_assert_true (colors_equal (&dark_loaded, &dark_expected));
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
g_test_add_func ("/theme/runtime/persistence_roundtrip_light_and_dark",
|
||||
test_persistence_roundtrip_light_and_dark);
|
||||
g_test_add_func ("/theme/runtime/loads_legacy_color_keys_via_migration_loader",
|
||||
test_loads_legacy_color_keys_via_migration_loader);
|
||||
g_test_add_func ("/theme/runtime/ui_edits_persist_without_legacy_array_mutation",
|
||||
test_ui_edits_persist_without_legacy_array_mutation);
|
||||
return g_test_run ();
|
||||
}
|
||||
Reference in New Issue
Block a user