From 6197ad2082473d89c649b2ed168cb72c25cc698e Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 6 Jul 2023 16:01:48 -0700 Subject: [PATCH] libsysprof-gtk: add test to list files in UI --- src/libsysprof-gtk/tests/meson.build | 1 + src/libsysprof-gtk/tests/test-files.c | 208 +++++++++++++++++++ src/libsysprof-gtk/tests/test-files.ui | 56 +++++ src/libsysprof-gtk/tests/tests.gresource.xml | 1 + 4 files changed, 266 insertions(+) create mode 100644 src/libsysprof-gtk/tests/test-files.c create mode 100644 src/libsysprof-gtk/tests/test-files.ui diff --git a/src/libsysprof-gtk/tests/meson.build b/src/libsysprof-gtk/tests/meson.build index 7b488f04..106f9b0e 100644 --- a/src/libsysprof-gtk/tests/meson.build +++ b/src/libsysprof-gtk/tests/meson.build @@ -14,6 +14,7 @@ libsysprof_gtk_testsuite_c_args = [ libsysprof_gtk_testsuite = { 'test-callgraph' : {'skip': true}, 'test-charts' : {'skip': true}, + 'test-files' : {'skip': true}, 'test-processes' : {'skip': true}, 'test-tracks' : {'skip': true}, 'test-mark-chart' : {'skip': true}, diff --git a/src/libsysprof-gtk/tests/test-files.c b/src/libsysprof-gtk/tests/test-files.c new file mode 100644 index 00000000..b83c79ce --- /dev/null +++ b/src/libsysprof-gtk/tests/test-files.c @@ -0,0 +1,208 @@ +/* test-files.c + * + * Copyright 2023 Christian Hergert + * + * 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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "config.h" + +#include +#include +#include + +#include +#include + +static GMainLoop *main_loop; +static char *filename; +static const GOptionEntry entries[] = { + { 0 } +}; + +#define TEST_TYPE_FILES (test_files_get_type()) +G_DECLARE_FINAL_TYPE (TestFiles, test_files, TEST, FILES, AdwWindow) + +struct _TestFiles +{ + AdwWindow parent_instance; + + SysprofDocument *document; + SysprofSession *session; +}; + +G_DEFINE_FINAL_TYPE (TestFiles, test_files, ADW_TYPE_WINDOW) + +enum { + PROP_0, + PROP_DOCUMENT, + PROP_SESSION, + N_PROPS +}; + +static GParamSpec *properties [N_PROPS]; + +static void +test_files_set_document (TestFiles *self, + SysprofDocument *document) +{ + if (g_set_object (&self->document, document)) + { + g_clear_object (&self->session); + + self->session = sysprof_session_new (self->document); + + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DOCUMENT]); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SESSION]); + } +} + +static void +test_files_dispose (GObject *object) +{ + TestFiles *self = (TestFiles *)object; + + g_clear_object (&self->document); + g_clear_object (&self->session); + + G_OBJECT_CLASS (test_files_parent_class)->dispose (object); +} + +static void +test_files_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + TestFiles *self = TEST_FILES (object); + + switch (prop_id) + { + case PROP_DOCUMENT: + g_value_set_object (value, self->document); + break; + + case PROP_SESSION: + g_value_set_object (value, self->session); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +test_files_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + TestFiles *self = TEST_FILES (object); + + switch (prop_id) + { + case PROP_DOCUMENT: + test_files_set_document (self, g_value_get_object (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +test_files_class_init (TestFilesClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + object_class->dispose = test_files_dispose; + object_class->get_property = test_files_get_property; + object_class->set_property = test_files_set_property; + + properties [PROP_DOCUMENT] = + g_param_spec_object ("document", NULL, NULL, + SYSPROF_TYPE_DOCUMENT, + (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + properties [PROP_SESSION] = + g_param_spec_object ("session", NULL, NULL, + SYSPROF_TYPE_SESSION, + (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (object_class, N_PROPS, properties); + + gtk_widget_class_set_template_from_resource (widget_class, "/test-files.ui"); + + g_type_ensure (SYSPROF_TYPE_DOCUMENT_FILE); +} + +static void +test_files_init (TestFiles *self) +{ + gtk_widget_init_template (GTK_WIDGET (self)); +} + +int +main (int argc, + char *argv[]) +{ + g_autoptr(GOptionContext) context = g_option_context_new ("- test various files"); + g_autoptr(SysprofDocumentLoader) loader = NULL; + g_autoptr(SysprofDocument) document = NULL; + g_autoptr(GError) error = NULL; + GtkWindow *window; + + sysprof_clock_init (); + + gtk_init (); + adw_init (); + + g_option_context_add_main_entries (context, entries, NULL); + if (!g_option_context_parse (context, &argc, &argv, &error)) + { + g_printerr ("%s\n", error->message); + return 1; + } + + if (argc != 2) + { + g_print ("usage: %s [OPTIONS] CAPTURE_FILE\n", argv[0]); + return 1; + } + + filename = argv[1]; + + main_loop = g_main_loop_new (NULL, FALSE); + + loader = sysprof_document_loader_new (filename); + if (!(document = sysprof_document_loader_load (loader, NULL, &error))) + g_error ("Failed to load document: %s", error->message); + + window = g_object_new (TEST_TYPE_FILES, + "default-width", 800, + "default-height", 600, + "document", document, + NULL); + g_signal_connect_swapped (window, + "close-request", + G_CALLBACK (g_main_loop_quit), + main_loop); + gtk_window_present (window); + g_main_loop_run (main_loop); + + return 0; +} diff --git a/src/libsysprof-gtk/tests/test-files.ui b/src/libsysprof-gtk/tests/test-files.ui new file mode 100644 index 00000000..b85666fc --- /dev/null +++ b/src/libsysprof-gtk/tests/test-files.ui @@ -0,0 +1,56 @@ + + + + diff --git a/src/libsysprof-gtk/tests/tests.gresource.xml b/src/libsysprof-gtk/tests/tests.gresource.xml index fc414536..d3f1eb91 100644 --- a/src/libsysprof-gtk/tests/tests.gresource.xml +++ b/src/libsysprof-gtk/tests/tests.gresource.xml @@ -2,6 +2,7 @@ test-charts.ui + test-files.ui test-processes.ui test-tracks.ui