| 1 |
/* |
| 2 |
* Copyright © 2022 Endless OS Foundation, LLC |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General |
| 17 |
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Author: Philip Withnall <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <gio/gio.h> |
| 23 |
#include <locale.h> |
| 24 |
|
| 25 |
|
| 26 |
static void |
| 27 |
test_basic_properties (void) |
| 28 |
{ |
| 29 |
GApplicationCommandLine *cl = NULL; |
| 30 |
const gchar * const arguments[] = { "arg1", "arg2", "arg3", NULL }; |
| 31 |
GVariantBuilder options_builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT); |
| 32 |
GVariantBuilder platform_data_builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT); |
| 33 |
gchar **argv = NULL; |
| 34 |
int argc = 0; |
| 35 |
GVariantDict *options_dict; |
| 36 |
GVariant *platform_data; |
| 37 |
GVariantDict *platform_data_dict = NULL; |
| 38 |
gboolean is_remote; |
| 39 |
|
| 40 |
/* Basic construction. */ |
| 41 |
g_variant_builder_add (&options_builder, "{sv}", "option1", g_variant_new_string ("value1")); |
| 42 |
g_variant_builder_add (&options_builder, "{sv}", "option2", g_variant_new_string ("value2")); |
| 43 |
|
| 44 |
g_variant_builder_add (&platform_data_builder, "{sv}", "data1", g_variant_new_string ("data-value1")); |
| 45 |
g_variant_builder_add (&platform_data_builder, "{sv}", "data2", g_variant_new_string ("data-value2")); |
| 46 |
|
| 47 |
cl = g_object_new (G_TYPE_APPLICATION_COMMAND_LINE, |
| 48 |
"arguments", g_variant_new_bytestring_array (arguments, -1), |
| 49 |
"options", g_variant_builder_end (&options_builder), |
| 50 |
"platform-data", g_variant_builder_end (&platform_data_builder), |
| 51 |
NULL); |
| 52 |
g_assert_nonnull (cl); |
| 53 |
|
| 54 |
/* Check the getters. */ |
| 55 |
argv = g_application_command_line_get_arguments (cl, &argc); |
| 56 |
g_assert_cmpint (argc, ==, 3); |
| 57 |
g_assert_cmpstrv (argv, arguments); |
| 58 |
g_clear_pointer (&argv, g_strfreev); |
| 59 |
|
| 60 |
options_dict = g_application_command_line_get_options_dict (cl); |
| 61 |
g_assert_nonnull (options_dict); |
| 62 |
g_assert_true (g_variant_dict_contains (options_dict, "option1")); |
| 63 |
g_assert_true (g_variant_dict_contains (options_dict, "option2")); |
| 64 |
|
| 65 |
g_assert_false (g_application_command_line_get_is_remote (cl)); |
| 66 |
|
| 67 |
platform_data = g_application_command_line_get_platform_data (cl); |
| 68 |
g_assert_nonnull (platform_data); |
| 69 |
platform_data_dict = g_variant_dict_new (platform_data); |
| 70 |
g_assert_true (g_variant_dict_contains (platform_data_dict, "data1")); |
| 71 |
g_assert_true (g_variant_dict_contains (platform_data_dict, "data2")); |
| 72 |
g_variant_dict_unref (platform_data_dict); |
| 73 |
g_variant_unref (platform_data); |
| 74 |
|
| 75 |
/* And g_object_get(). */ |
| 76 |
g_object_get (cl, "is-remote", &is_remote, NULL); |
| 77 |
g_assert_false (is_remote); |
| 78 |
|
| 79 |
/* exit status */ |
| 80 |
g_assert_cmpint (g_application_command_line_get_exit_status (cl), ==, 0); |
| 81 |
g_application_command_line_set_exit_status (cl, 1); |
| 82 |
g_assert_cmpint (g_application_command_line_get_exit_status (cl), ==, 1); |
| 83 |
|
| 84 |
g_application_command_line_done (cl); |
| 85 |
g_application_command_line_set_exit_status (cl, 2); |
| 86 |
g_assert_cmpint (g_application_command_line_get_exit_status (cl), ==, 1); |
| 87 |
|
| 88 |
g_clear_object (&cl); |
| 89 |
} |
| 90 |
|
| 91 |
int |
| 92 |
main (int argc, |
| 93 |
char *argv[]) |
| 94 |
{ |
| 95 |
setlocale (LC_ALL, ""); |
| 96 |
g_test_init (&argc, &argv, NULL); |
| 97 |
|
| 98 |
g_test_add_func ("/application-command-line/basic-properties", test_basic_properties); |
| 99 |
|
| 100 |
return g_test_run (); |
| 101 |
} |