1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 
3 /*
4  * Copyright (C) 2017 Red Hat, Inc.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "config.h"
21 
22 #include "tests/monitor-config-migration-unit-tests.h"
23 
24 #include <glib.h>
25 #include <gio/gio.h>
26 
27 #include "backends/meta-backend-private.h"
28 #include "backends/meta-monitor-config-manager.h"
29 #include "backends/meta-monitor-config-store.h"
30 #include "backends/meta-monitor-manager-private.h"
31 #include "backends/meta-monitor-config-migration.h"
32 #include "tests/monitor-test-utils.h"
33 
34 static void
test_migration(const char * old_config,const char * new_config)35 test_migration (const char *old_config,
36                 const char *new_config)
37 {
38   MetaBackend *backend = meta_get_backend ();
39   MetaMonitorManager *monitor_manager =
40     meta_backend_get_monitor_manager (backend);
41   MetaMonitorConfigManager *config_manager = monitor_manager->config_manager;
42   MetaMonitorConfigStore *config_store =
43     meta_monitor_config_manager_get_store (config_manager);
44   GError *error = NULL;
45   const char *old_config_path;
46   g_autoptr (GFile) old_config_file = NULL;
47   g_autofree char *migrated_path = NULL;
48   const char *expected_path;
49   g_autofree char *migrated_data = NULL;
50   g_autofree char *expected_data = NULL;
51   g_autoptr (GFile) migrated_file = NULL;
52 
53   migrated_path = g_build_filename (g_get_tmp_dir (),
54                                     "test-migrated-monitors.xml",
55                                     NULL);
56   if (!meta_monitor_config_store_set_custom (config_store, "/dev/null",
57                                              migrated_path,
58                                              &error))
59     g_error ("Failed to set custom config store: %s", error->message);
60 
61   old_config_path = g_test_get_filename (G_TEST_DIST, "tests", "migration",
62                                          old_config, NULL);
63   old_config_file = g_file_new_for_path (old_config_path);
64   if (!meta_migrate_old_monitors_config (config_store,
65                                          old_config_file,
66                                          &error))
67     g_error ("Failed to migrate config: %s", error->message);
68 
69   expected_path = g_test_get_filename (G_TEST_DIST, "tests", "migration",
70                                        new_config, NULL);
71 
72   expected_data = read_file (expected_path);
73   migrated_data = read_file (migrated_path);
74 
75   g_assert_nonnull (expected_data);
76   g_assert_nonnull (migrated_data);
77 
78   g_assert_cmpstr (expected_data, ==, migrated_data);
79 
80   migrated_file = g_file_new_for_path (migrated_path);
81   if (!g_file_delete (migrated_file, NULL, &error))
82     g_error ("Failed to remove test data output file: %s", error->message);
83 }
84 
85 static void
meta_test_monitor_config_migration_basic(void)86 meta_test_monitor_config_migration_basic (void)
87 {
88   test_migration ("basic-old.xml", "basic-new.xml");
89 }
90 
91 static void
meta_test_monitor_config_migration_rotated(void)92 meta_test_monitor_config_migration_rotated (void)
93 {
94   test_migration ("rotated-old.xml", "rotated-new.xml");
95 }
96 
97 static void
meta_test_monitor_config_migration_tiled(void)98 meta_test_monitor_config_migration_tiled (void)
99 {
100   test_migration ("tiled-old.xml", "tiled-new.xml");
101 }
102 
103 static void
meta_test_monitor_config_migration_first_rotated(void)104 meta_test_monitor_config_migration_first_rotated (void)
105 {
106   test_migration ("first-rotated-old.xml", "first-rotated-new.xml");
107 }
108 
109 static void
meta_test_monitor_config_migration_oneoff(void)110 meta_test_monitor_config_migration_oneoff (void)
111 {
112   test_migration ("oneoff-old.xml", "oneoff-new.xml");
113 }
114 
115 static void
meta_test_monitor_config_migration_wiggle(void)116 meta_test_monitor_config_migration_wiggle (void)
117 {
118   test_migration ("wiggle-old.xml", "wiggle-new.xml");
119 }
120 
121 void
init_monitor_config_migration_tests(void)122 init_monitor_config_migration_tests (void)
123 {
124   g_test_add_func ("/backends/monitor-config-migration/basic",
125                    meta_test_monitor_config_migration_basic);
126   g_test_add_func ("/backends/monitor-config-migration/rotated",
127                    meta_test_monitor_config_migration_rotated);
128   g_test_add_func ("/backends/monitor-config-migration/tiled",
129                    meta_test_monitor_config_migration_tiled);
130   g_test_add_func ("/backends/monitor-config-migration/first-rotated",
131                    meta_test_monitor_config_migration_first_rotated);
132   g_test_add_func ("/backends/monitor-config-migration/oneoff",
133                    meta_test_monitor_config_migration_oneoff);
134   g_test_add_func ("/backends/monitor-config-migration/wiggle",
135                    meta_test_monitor_config_migration_wiggle);
136 }
137