File: settings.c

package info (click to toggle)
chatty 0.8.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,180 kB
  • sloc: ansic: 57,069; sql: 5,122; xml: 158; cpp: 39; makefile: 26; sh: 11; lisp: 8; javascript: 6
file content (161 lines) | stat: -rw-r--r-- 5,355 bytes parent folder | download | duplicates (2)
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
/* -*- 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 <http://www.gnu.org/licenses/>.
 *
 * Author(s):
 *   Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * 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 <glib.h>

#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,
                "message-carbons", value,
                "send-typing", value,
                "strip-url-tracking-id", value,
                "strip-url-tracking-id-dialog", value,
                "blur-idle-buddies", value,
                "indicate-unknown-contacts", value,
                "clear-out-stuck-sms", value,
                "return-sends-message", value,
                "render-attachments", value,
                NULL);
}

/* set_settings(), check_settings_true() and check_settings_false()
 * should stay in sync */
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_message_carbons (settings));
  g_assert_true (chatty_settings_get_send_typing (settings));
  g_assert_true (chatty_settings_get_strip_url_tracking_ids (settings));
  g_assert_true (chatty_settings_get_strip_url_tracking_ids_dialog (settings));
  g_assert_true (chatty_settings_get_blur_idle_buddies (settings));
  g_assert_true (chatty_settings_get_indicate_unknown_contacts (settings));
  g_assert_true (chatty_settings_get_clear_out_stuck_sms (settings));
  g_assert_true (chatty_settings_get_return_sends_message (settings));
  g_assert_true (chatty_settings_get_render_attachments (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_message_carbons (settings));
  g_assert_false (chatty_settings_get_send_typing (settings));
  g_assert_false (chatty_settings_get_strip_url_tracking_ids (settings));
  g_assert_false (chatty_settings_get_strip_url_tracking_ids_dialog (settings));
  g_assert_false (chatty_settings_get_blur_idle_buddies (settings));
  g_assert_false (chatty_settings_get_indicate_unknown_contacts (settings));
  g_assert_false (chatty_settings_get_clear_out_stuck_sms (settings));
  g_assert_false (chatty_settings_get_return_sends_message (settings));
  g_assert_false (chatty_settings_get_render_attachments (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_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 ();
}