diff --git a/meson.build b/meson.build index 21948f90fe016242f929e2383e826454b8c5953c..ea35b4c189d35fc9e4d4e70eea3150a8031387e2 100644 --- a/meson.build +++ b/meson.build @@ -5,6 +5,9 @@ project('chatty', 'c', version: '0.1.2', i18n = import('i18n') gnome = import('gnome') +top_inc = include_directories('.') +src_inc = include_directories('src') + config_h = configuration_data() config_h.set_quoted('GETTEXT_PACKAGE', 'purism-chatty') config_h.set_quoted('LOCALEDIR', join_paths(get_option('prefix'), get_option('localedir'))) @@ -85,6 +88,7 @@ libm_dep = cc.find_library('m') subdir('data') subdir('src') +subdir('tests') subdir('po') meson.add_install_script('build-aux/meson/postinstall.py') diff --git a/src/chatty-settings.c b/src/chatty-settings.c index d10eac10c5b946e188d3834ae6a204275bef93ce..923a2f4b5d4e021b4a144850c1fac00ab652042a 100644 --- a/src/chatty-settings.c +++ b/src/chatty-settings.c @@ -308,7 +308,10 @@ chatty_settings_get_default (void) static ChattySettings *self; if (!self) - self = g_object_new (CHATTY_TYPE_SETTINGS, NULL); + { + self = g_object_new (CHATTY_TYPE_SETTINGS, NULL); + g_object_add_weak_pointer (G_OBJECT (self), (gpointer *)&self); + } return self; } diff --git a/src/meson.build b/src/meson.build index e2cf79ced93589933f153097f927a4639d016f9c..8d3fc1261d4670b39a507246370eff223bec616f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -2,11 +2,14 @@ version_h = vcs_tag(command: ['git', 'rev-parse', 'HEAD'], input: 'version.h.in', output: 'version.h') +libsrc = [ + 'chatty-settings.c', +] + chatty_sources = [ 'main.c', 'chatty-application.c', 'chatty-window.c', - 'chatty-settings.c', 'chatty-dialogs.c', 'chatty-account.c', 'chatty-message-list.c', @@ -48,7 +51,14 @@ chatty_sources += gnome.compile_resources('chatty-resources', c_name: 'chatty' ) +libchatty = static_library( + 'libchatty', libsrc, + install: false, + dependencies: chatty_deps, +) + executable('chatty', chatty_sources, dependencies: chatty_deps, + link_with: libchatty, install: true, ) diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 0000000000000000000000000000000000000000..c59d227bf70d5f1b18f936019094be46f180fdcb --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,27 @@ +tests_inc = [ + top_inc, + src_inc, +] + +env = environment() +env.set('G_TEST_SRCDIR', meson.current_source_dir()) +env.set('G_TEST_BUILDDIR', meson.current_build_dir()) +env.set('GSETTINGS_SCHEMA_DIR', join_paths(meson.build_root(), 'data')) +env.set('MALLOC_CHECK_', '2') +# Guaranteed to be random: https://xkcd.com/221 +env.set('MALLOC_PERTURB_', '4') + +test_items = [ + 'settings', +] + +foreach item: test_items + t = executable( + item, + item + '.c', + include_directories: tests_inc, + link_with: libchatty, + dependencies: chatty_deps, + ) + test(item, t, env: env) +endforeach diff --git a/tests/settings.c b/tests/settings.c new file mode 100644 index 0000000000000000000000000000000000000000..766cc0fcc1bb4e4846071e804e9bc86b37529d69 --- /dev/null +++ b/tests/settings.c @@ -0,0 +1,152 @@ +/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */ +/* settings.c + * + * Copyright 2019 Purism SPC + * + * This program 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. + * + * This program 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 this program. If not, see . + * + * Author(s): + * Mohammed Sadiq + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#undef NDEBUG +#undef G_DISABLE_ASSERT +#undef G_DISABLE_CHECKS +#undef G_DISABLE_CAST_CHECKS +#undef G_LOG_DOMAIN + +#include + +#include "chatty-settings.h" + +static void +set_settings_bool (ChattySettings *settings, + gboolean value) +{ + g_assert_true (CHATTY_IS_SETTINGS (settings)); + g_object_set (settings, + "send-receipts", value, + "send-typing", value, + "show-offline-buddies", value, + "greyout-offline-buddies", value, + "blur-idle-buddies", value, + "indicate-unknown-contacts", value, + "return-sends-message", value, + NULL); +} + +static void +check_settings_true (ChattySettings *settings) +{ + g_assert_true (CHATTY_IS_SETTINGS (settings)); + + set_settings_bool (settings, TRUE); + + g_assert_true (chatty_settings_get_send_receipts (settings)); + g_assert_true (chatty_settings_get_send_typing (settings)); + g_assert_true (chatty_settings_get_show_offline_buddies (settings)); + g_assert_true (chatty_settings_get_greyout_offline_buddies (settings)); + g_assert_true (chatty_settings_get_blur_idle_buddies (settings)); + g_assert_true (chatty_settings_get_indicate_unkown_contacts (settings)); + g_assert_true (chatty_settings_get_return_sends_message (settings)); +} + +static void +check_settings_false (ChattySettings *settings) +{ + g_assert_true (CHATTY_IS_SETTINGS (settings)); + + set_settings_bool (settings, FALSE); + + g_assert_false (chatty_settings_get_send_receipts (settings)); + g_assert_false (chatty_settings_get_send_typing (settings)); + g_assert_false (chatty_settings_get_show_offline_buddies (settings)); + g_assert_false (chatty_settings_get_greyout_offline_buddies (settings)); + g_assert_false (chatty_settings_get_blur_idle_buddies (settings)); + g_assert_false (chatty_settings_get_indicate_unkown_contacts (settings)); + g_assert_false (chatty_settings_get_return_sends_message (settings)); +} + +static void +test_settings_all_bool (void) +{ + ChattySettings *settings; + + settings = chatty_settings_get_default (); + check_settings_true (settings); + g_object_unref (settings); + + /* + * Test again with a new settings to see + * if settings are saved to gsettings + */ + settings = chatty_settings_get_default (); + check_settings_true (settings); + g_object_unref (settings); + + settings = chatty_settings_get_default (); + check_settings_false (settings); + g_object_unref (settings); + + settings = chatty_settings_get_default (); + check_settings_false (settings); + g_object_unref (settings); +} + +static void +test_settings_first_start (void) +{ + ChattySettings *settings; + g_autoptr(GSettings) gsettings = NULL; + gboolean first_start; + + /* Reset the “first-start” settings */ + gsettings = g_settings_new ("sm.puri.Chatty"); + g_settings_reset (gsettings, "first-start"); + + settings = chatty_settings_get_default (); + g_assert_true (CHATTY_IS_SETTINGS (settings)); + g_object_get (settings, "first-start", &first_start, NULL); + g_assert_true (first_start); + g_assert_true (chatty_settings_get_first_start (settings)); + g_object_unref (settings); + + /* + * create a new object, and check again. Everytime the settings + * is finalized (ie, the program ends) “first-start” should be + * set. + */ + settings = chatty_settings_get_default (); + g_assert_true (CHATTY_IS_SETTINGS (settings)); + g_object_get (settings, "first-start", &first_start, NULL); + g_assert_false (first_start); + g_assert_false (chatty_settings_get_first_start (settings)); + g_object_unref (settings); +} + +int +main (int argc, + char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_setenv ("GSETTINGS_BACKEND", "memory", TRUE); + + g_test_add_func ("/settings/first_start", test_settings_first_start); + g_test_add_func ("/settings/all_bool", test_settings_all_bool); + + return g_test_run (); +}