From 991396fc849e4394432a44f5d6b8dbf2bb4459b7 Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Sat, 23 Jul 2022 23:22:34 +0200 Subject: [PATCH] Initial test boilerplate with Config path and values test --- src/configModel/configModel.vala | 6 +++ src/meson.build | 17 ++++++ src/tests/config_test.json | 38 ++++++++++++++ src/tests/tests.vala | 88 ++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 src/tests/config_test.json create mode 100644 src/tests/tests.vala diff --git a/src/configModel/configModel.vala b/src/configModel/configModel.vala index 3b97f268..fd5e2a2d 100644 --- a/src/configModel/configModel.vala +++ b/src/configModel/configModel.vala @@ -223,6 +223,12 @@ namespace SwayNotificationCenter { private static ConfigModel _instance; private static string _path = ""; + public string path { + get { + return _path; + } + } + /** Get the static singleton */ public static unowned ConfigModel instance { get { diff --git a/src/meson.build b/src/meson.build index 3ab09baa..f2368506 100644 --- a/src/meson.build +++ b/src/meson.build @@ -80,6 +80,23 @@ app_sources += gnome.compile_resources('sway_notification_center-resources', c_name: 'sway_notification_center' ) +# Tests +test_sources = [] +foreach item : app_sources + if item not in ['main.vala'] + test_sources += item + endif +endforeach +test('swaync-tests', + executable( + 'swaync-tests', + ['tests/tests.vala', test_sources], + vala_args: args, + dependencies: app_deps, + install: false, + ), + timeout: 180) + executable('swaync', app_sources, vala_args: args, diff --git a/src/tests/config_test.json b/src/tests/config_test.json new file mode 100644 index 00000000..728818bd --- /dev/null +++ b/src/tests/config_test.json @@ -0,0 +1,38 @@ +{ + "$schema": "/etc/xdg/swaync/configSchema.json", + "positionX": "center", + "positionY": "bottom", + "control-center-margin-top": 12, + "control-center-margin-bottom": 10, + "control-center-margin-right": 40, + "control-center-margin-left": 8, + "notification-icon-size": 83, + "notification-body-image-height": 140, + "notification-body-image-width": 210, + "timeout": 19, + "timeout-low": 59, + "timeout-critical": 1, + "fit-to-screen": false, + "control-center-width": 800, + "control-center-height": 800, + "notification-window-width": 50, + "keyboard-shortcuts": false, + "image-visibility": "never", + "transition-time": 20, + "hide-on-clear": true, + "hide-on-action": false, + "script-fail-notify": false, + "scripts": { + "example-script": { + "exec": "echo 'Do something...'", + "urgency": "Normal" + } + }, + "notification-visibility": { + "example-name": { + "state": "muted", + "urgency": "Low", + "app-name": "Spotify" + } + } +} diff --git a/src/tests/tests.vala b/src/tests/tests.vala new file mode 100644 index 00000000..c0231d8f --- /dev/null +++ b/src/tests/tests.vala @@ -0,0 +1,88 @@ +namespace SwayNotificationCenter { + static SwayncDaemon swaync_daemon; + static string ? style_path; + static string ? config_path; + + static Settings self_settings; + + public int main (string[] args) { + Gtk.init (ref args); + Test.init (ref args); + + Functions.init (); + + self_settings = new Settings ("org.erikreider.swaync"); + + style_path = "./src/tests/style_test.json"; + config_path = "./src/tests/config_test.json"; + + Test.add_func ("/ConfigModel/Test_paths", test_config_paths); + + Test.add_func ("/ConfigModel/Verify_custom_test_values", + test_config_values); + + return Test.run (); + } + + private static inline void test_config_paths () { + // Test invalid paths would result in the default path + const string[] PATHS = { "~/@", "/@", "./@", "", null }; + foreach (var path in PATHS) { + assert_false (path == Functions.get_config_path (path)); + } + + // Test valid paths + // Relative path + const string TEST_PATH1 = "./src/tests/config_test.json"; + assert_true (TEST_PATH1 == Functions.get_config_path (TEST_PATH1)); + + // Home relative path ("~/...") + string test_path2 = Path.build_filename ( + Environment.get_current_dir (), + TEST_PATH1); + string test_rel_path2 = Path.build_filename ( + Environment.get_current_dir ().replace ( + Environment.get_home_dir (), "~"), + TEST_PATH1); + assert_true (test_path2 == Functions.get_config_path (test_rel_path2)); + + // Absolute path + string test_path3 = Path.build_filename ( + Environment.get_current_dir (), + TEST_PATH1); + assert_true (test_path3 == Functions.get_config_path (test_path3)); + } + + private static inline void test_config_values () { + ConfigModel.init (config_path); + unowned ConfigModel i = ConfigModel.instance; + // Position + assert_true (i.positionX == PositionX.CENTER); + assert_true (i.positionY == PositionY.BOTTOM); + // Margins + assert_true (i.control_center_margin_top == 12); + assert_true (i.control_center_margin_bottom == 10); + assert_true (i.control_center_margin_right == 40); + assert_true (i.control_center_margin_left == 8); + // Notification values + assert_true (i.notification_icon_size == 83); + print ("H: %i\n", i.notification_body_image_height); + assert_true (i.notification_body_image_height == 140); + assert_true (i.notification_body_image_width == 210); + // Timeouts + assert_true (i.timeout == 19); + assert_true (i.timeout_low == 59); + assert_true (i.timeout_critical == 1); + // Control Center + assert_true (i.control_center_width == 800); + assert_true (i.control_center_height == 800); + // Misc + assert_true (i.notification_window_width == 50); + assert_true (i.keyboard_shortcuts == false); + assert_true (i.image_visibility == ImageVisibility.NEVER); + assert_true (i.transition_time == 20); + assert_true (i.hide_on_clear == true); + assert_true (i.hide_on_action == false); + assert_true (i.script_fail_notify == false); + } +}