diff --git a/README.md b/README.md index 00dbaf7..c3dfacb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ - # freedesktop-icons -![crates.io-badge](https://img.shields.io/crates/v/freedesktop-icons) -![docrs-badge](https://img.shields.io/docsrs/freedesktop-icons) + # cosmic-freedesktop-icons +![crates.io-badge](https://img.shields.io/crates/v/cosmic-freedesktop-icons) +![docrs-badge](https://img.shields.io/docsrs/cosmic-freedesktop-icons) This crate provides a [freedesktop icon](https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html#implementation_notes) lookup implementation. @@ -15,7 +15,7 @@ with the default scale (`1`) and the default size (`24`). ```rust - use freedesktop_icons::lookup; + use cosmic_freedesktop_icons::lookup; let icon = lookup("firefox").find(); ``` @@ -25,7 +25,7 @@ If you have specific requirements for your lookup you can use the provided builder functions: ```rust - use freedesktop_icons::lookup; + use cosmic_freedesktop_icons::lookup; let icon = lookup("firefox") .with_size(48) @@ -39,7 +39,7 @@ you can use the internal cache to improve performance. ```rust - use freedesktop_icons::lookup; + use cosmic_freedesktop_icons::lookup; let icon = lookup("firefox") .with_size(48) @@ -47,4 +47,4 @@ .with_theme("Arc") .with_cache() .find(); -``` \ No newline at end of file +``` diff --git a/src/lib.rs b/src/lib.rs index 5218185..bb7bed8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,9 +11,9 @@ //! The following snippet get an icon from the default 'hicolor' theme //! with the default scale (`1`) and the default size (`24`). //! -//! ```rust +//! ```rust, no_run //! # fn main() { -//! use freedesktop_icons::lookup; +//! use cosmic_freedesktop_icons::lookup; //! //! let icon = lookup("firefox").find(); //! # } @@ -23,9 +23,9 @@ //! //! If you have specific requirements for your lookup you can use the provided builder functions: //! -//! ```rust +//! ```rust, no_run //! # fn main() { -//! use freedesktop_icons::lookup; +//! use cosmic_freedesktop_icons::lookup; //! //! let icon = lookup("firefox") //! .with_size(48) @@ -39,9 +39,9 @@ //! If your application is going to repeat the same icon lookups multiple times //! you can use the internal cache to improve performance. //! -//! ```rust +//! ```rust, no_run //! # fn main() { -//! use freedesktop_icons::lookup; +//! use cosmic_freedesktop_icons::lookup; //! //! let icon = lookup("firefox") //! .with_size(48) @@ -63,9 +63,9 @@ mod theme; /// Return the list of installed themes on the system /// /// ## Example -/// ```rust +/// ```rust, no_run /// # fn main() { -/// use freedesktop_icons::list_themes; +/// use cosmic_freedesktop_icons::list_themes; /// /// let themes: Vec<&str> = list_themes(); /// @@ -102,9 +102,9 @@ pub struct LookupBuilder<'a> { /// Build an icon lookup for the given icon name. /// /// ## Example -/// ```rust +/// ```rust, no_run /// # fn main() { -/// use freedesktop_icons::lookup; +/// use cosmic_freedesktop_icons::lookup; /// /// let icon = lookup("firefox").find(); /// # } @@ -116,9 +116,9 @@ impl<'a> LookupBuilder<'a> { /// Restrict the lookup to the given icon size. /// /// ## Example - /// ```rust + /// ```rust, no_run /// # fn main() { - /// use freedesktop_icons::lookup; + /// use cosmic_freedesktop_icons::lookup; /// /// let icon = lookup("firefox") /// .with_size(48) @@ -132,9 +132,9 @@ impl<'a> LookupBuilder<'a> { /// Restrict the lookup to the given scale. /// /// ## Example - /// ```rust + /// ```rust, no_run /// # fn main() { - /// use freedesktop_icons::lookup; + /// use cosmic_freedesktop_icons::lookup; /// /// let icon = lookup("firefox") /// .with_scale(2) @@ -147,9 +147,9 @@ impl<'a> LookupBuilder<'a> { /// Add the given theme to the current lookup : /// ## Example - /// ```rust + /// ```rust, no_run /// # fn main() { - /// use freedesktop_icons::lookup; + /// use cosmic_freedesktop_icons::lookup; /// /// let icon = lookup("firefox") /// .with_theme("Papirus") @@ -166,9 +166,9 @@ impl<'a> LookupBuilder<'a> { /// that repeat the same lookups, an application launcher for instance. /// /// ## Example - /// ```rust + /// ```rust, no_run /// # fn main() { - /// use freedesktop_icons::lookup; + /// use cosmic_freedesktop_icons::lookup; /// /// let icon = lookup("firefox") /// .with_scale(2) @@ -185,9 +185,9 @@ impl<'a> LookupBuilder<'a> { /// if you need a modifiable icon, to match a user theme for instance. /// /// ## Example - /// ```rust + /// ```rust, no_run /// # fn main() { - /// use freedesktop_icons::lookup; + /// use cosmic_freedesktop_icons::lookup; /// /// let icon = lookup("firefox") /// .force_svg() @@ -308,58 +308,81 @@ impl<'a> LookupBuilder<'a> { } } -// WARNING: these test are highly dependent on your installed icon-themes. -// If you want to run them, make sure you have 'Papirus' and 'Arc' icon-themes installed. #[cfg(test)] mod test { use crate::{lookup, CacheEntry, CACHE}; use speculoos::prelude::*; - use std::path::PathBuf; + use std::{ + env, + path::{Path, PathBuf}, + sync::LazyLock, + }; + + pub(super) static TEST_ASSETS_PATH: LazyLock = LazyLock::new(|| { + let data_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("test_assets"); + assert!( + data_dir.exists(), + "The `test_assets` folder should be in the package root" + ); + data_dir + }); + + /// Override the default search path(s) with a path we control. + /// + /// This grants us more control over tests rather than relying on the system having the + /// themes we need. + pub(super) fn set_fake_icons_path() { + env::set_var("XDG_DATA_DIRS", TEST_ASSETS_PATH.to_str().unwrap()); + } #[test] fn simple_lookup() { - let firefox = lookup("firefox").find(); + set_fake_icons_path(); + let browser = lookup("browser").find(); + let icon_path = TEST_ASSETS_PATH.join("icons/hicolor/scalable/apps/browser.svg"); asserting!("Lookup with no parameters should return an existing icon") - .that(&firefox) + .that(&browser) .is_some() - .is_equal_to(PathBuf::from( - "/usr/share/icons/hicolor/22x22/apps/firefox.png", - )); + .is_equal_to(icon_path); } #[test] fn theme_lookup() { - let firefox = lookup("firefox").with_theme("Papirus").find(); + set_fake_icons_path(); + let cosmic_fake = lookup("cosmic-fake").with_theme("cosmic-base").find(); + let icon_path = TEST_ASSETS_PATH.join("icons/cosmic-base/16x16/apps/cosmic-fake.svg"); asserting!("Lookup with no parameters should return an existing icon") - .that(&firefox) + .that(&cosmic_fake) .is_some() - .is_equal_to(PathBuf::from( - "/usr/share/icons/Papirus/24x24/apps/firefox.svg", - )); + .is_equal_to(icon_path); } #[test] fn should_fallback_to_parent_theme() { + set_fake_icons_path(); let icon = lookup("video-single-display-symbolic") - .with_theme("Arc") + .with_theme("cosmic-base-dark") .find(); - asserting!("Lookup for an icon in the Arc theme should find the icon in its parent") - .that(&icon) - .is_some() - .is_equal_to(PathBuf::from( - "/usr/share/icons/Adwaita/scalable/devices/video-single-display-symbolic.svg", - )); + let icon_path = TEST_ASSETS_PATH + .join("icons/cosmic-base/scalable/devices/video-single-display-symbolic.svg"); + asserting!( + "Lookup for an icon in the cosmic-dark theme should find the icon in its parent" + ) + .that(&icon) + .is_some() + .is_equal_to(icon_path); } #[test] - fn should_fallback_to_pixmaps_utlimately() { + fn should_fallback_to_pixmaps_ultimately() { + set_fake_icons_path(); let archlinux_logo = lookup("archlinux-logo") .with_size(16) .with_scale(1) - .with_theme("Papirus") + .with_theme("COSMIC") .find(); asserting!("When lookup fail in theme, icon should be found in '/usr/share/pixmaps'") @@ -370,22 +393,24 @@ mod test { #[test] fn compare_to_linincon_with_theme() { - let lin_wireshark = linicon::lookup_icon("wireshark") + set_fake_icons_path(); + let lin_cosmic_fake = linicon::lookup_icon("cosmic-fake") + .from_theme("cosmic-base") .next() .unwrap() .unwrap() .path; - let wireshark = lookup("wireshark") + let cosmic_fake = lookup("cosmic-fake") .with_size(16) .with_scale(1) - .with_theme("Papirus") + .with_theme("cosmic-base") .find(); asserting!("Given the same input parameter, lookup should output be the same as linincon") - .that(&wireshark) + .that(&cosmic_fake) .is_some() - .is_equal_to(lin_wireshark); + .is_equal_to(lin_cosmic_fake); } #[test] diff --git a/src/theme/mod.rs b/src/theme/mod.rs index a738f16..ff15109 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -214,13 +214,16 @@ impl Debug for Theme { #[cfg(test)] mod test { - use crate::THEMES; + use crate::{ + test::{set_fake_icons_path, TEST_ASSETS_PATH}, + THEMES, + }; use speculoos::prelude::*; - use std::path::PathBuf; #[test] fn get_one_icon() { - let themes = THEMES.get("Adwaita").unwrap(); + set_fake_icons_path(); + let themes = THEMES.get("cosmic-base-dark").unwrap(); println!( "{:?}", themes.iter().find_map(|t| t.try_get_icon_exact_size( @@ -233,24 +236,26 @@ mod test { } #[test] - fn should_get_png_first() { + fn should_get_svg_first() { + set_fake_icons_path(); let themes = THEMES.get("hicolor").unwrap(); let icon = themes .iter() - .find_map(|t| t.try_get_icon_exact_size("blueman", 24, 1, true)); - assert_that!(icon).is_some().is_equal_to(PathBuf::from( - "/usr/share/icons/hicolor/scalable/apps/blueman.svg", - )); + .find_map(|t| t.try_get_icon_exact_size("cosmic-fake-applet", 24, 1, true)); + + let icon_path = TEST_ASSETS_PATH.join("icons/hicolor/scalable/apps/cosmic-fake-applet.svg"); + assert_that!(icon).is_some().is_equal_to(icon_path); } #[test] - fn should_get_svg_first() { + fn should_get_png_first() { + set_fake_icons_path(); let themes = THEMES.get("hicolor").unwrap(); let icon = themes .iter() - .find_map(|t| t.try_get_icon_exact_size("blueman", 24, 1, false)); - assert_that!(icon).is_some().is_equal_to(PathBuf::from( - "/usr/share/icons/hicolor/22x22/apps/blueman.png", - )); + .find_map(|t| t.try_get_icon_exact_size("cosmic-cat-tracker", 22, 1, false)); + + let icon_path = TEST_ASSETS_PATH.join("icons/hicolor/22x22/apps/cosmic-cat-tracker.png"); + assert_that!(icon).is_some().is_equal_to(icon_path); } } diff --git a/src/theme/parse.rs b/src/theme/parse.rs index 22fa267..135330d 100644 --- a/src/theme/parse.rs +++ b/src/theme/parse.rs @@ -80,23 +80,18 @@ impl Theme { #[cfg(test)] mod test { - use crate::THEMES; + use crate::{test::set_fake_icons_path, THEMES}; use speculoos::prelude::*; #[test] fn should_get_theme_parents() { - for theme in THEMES.get("Arc").unwrap() { + set_fake_icons_path(); + for theme in THEMES.get("cosmic-base-dark").unwrap() { let parents = theme.inherits(); assert_that!(parents).does_not_contain("hicolor"); - assert_that!(parents).is_equal_to(vec![ - "Moka", - "Faba", - "elementary", - "Adwaita", - "gnome", - ]); + assert_that!(parents).is_equal_to(vec!["cosmic-base", "pop-os-base"]); } } } diff --git a/test_assets/icons/cosmic-base-dark/22x22/apps/bar.png b/test_assets/icons/cosmic-base-dark/22x22/apps/bar.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base-dark/22x22/apps/baz.png b/test_assets/icons/cosmic-base-dark/22x22/apps/baz.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base-dark/22x22/apps/browser.png b/test_assets/icons/cosmic-base-dark/22x22/apps/browser.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base-dark/22x22/apps/cosmic-cat-tracker.png b/test_assets/icons/cosmic-base-dark/22x22/apps/cosmic-cat-tracker.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base-dark/22x22/apps/cosmic-fake-applet.png b/test_assets/icons/cosmic-base-dark/22x22/apps/cosmic-fake-applet.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base-dark/22x22/apps/foo.png b/test_assets/icons/cosmic-base-dark/22x22/apps/foo.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base-dark/index.theme b/test_assets/icons/cosmic-base-dark/index.theme new file mode 100644 index 0000000..366be82 --- /dev/null +++ b/test_assets/icons/cosmic-base-dark/index.theme @@ -0,0 +1,30 @@ +[Icon Theme] +Name=COSMIC (Dark) +Comment=Fake theme for tests (dark) +Inherits=cosmic-base,pop-os-base,hicolor + +Directories=16x16/apps,22x22/apps,scalable/devices,symbolic/actions + +[16x16/apps] +Context=Applications +Size=16 +Type=Fixed + +[22x22/apps] +Context=Applications +Size=22 +Type=Fixed + +[scalable/devices] +Context=Devices +Size=16 +MinSize=16 +MaxSize=256 +Type=Scalable + +[symbolic/actions] +Context=Actions +Size=16 +MinSize=16 +MaxSize=512 +Type=Scalable diff --git a/test_assets/icons/cosmic-base-dark/symbolic/actions/edit-delete-symbolic.svg b/test_assets/icons/cosmic-base-dark/symbolic/actions/edit-delete-symbolic.svg new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base/16x16/apps/cosmic-fake.svg b/test_assets/icons/cosmic-base/16x16/apps/cosmic-fake.svg new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base/22x22/apps/bar.png b/test_assets/icons/cosmic-base/22x22/apps/bar.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base/22x22/apps/baz.png b/test_assets/icons/cosmic-base/22x22/apps/baz.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base/22x22/apps/browser.png b/test_assets/icons/cosmic-base/22x22/apps/browser.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base/22x22/apps/cosmic-cat-tracker.png b/test_assets/icons/cosmic-base/22x22/apps/cosmic-cat-tracker.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base/22x22/apps/cosmic-fake-applet.png b/test_assets/icons/cosmic-base/22x22/apps/cosmic-fake-applet.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base/22x22/apps/foo.png b/test_assets/icons/cosmic-base/22x22/apps/foo.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base/index.theme b/test_assets/icons/cosmic-base/index.theme new file mode 100644 index 0000000..a4ab35e --- /dev/null +++ b/test_assets/icons/cosmic-base/index.theme @@ -0,0 +1,30 @@ +[Icon Theme] +Name=COSMIC +Comment=Fake theme for tests (base) +Inherits=hicolor + +Directories=16x16/apps,22x22/apps,scalable/devices,symbolic/actions + +[16x16/apps] +Context=Applications +Size=16 +Type=Fixed + +[22x22/apps] +Context=Applications +Size=22 +Type=Fixed + +[scalable/devices] +Context=Devices +Size=16 +MinSize=16 +MaxSize=256 +Type=Scalable + +[symbolic/actions] +Context=Actions +Size=16 +MinSize=16 +MaxSize=512 +Type=Scalable diff --git a/test_assets/icons/cosmic-base/scalable/devices/video-single-display-symbolic.svg b/test_assets/icons/cosmic-base/scalable/devices/video-single-display-symbolic.svg new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/cosmic-base/symbolic/actions/edit-delete-symbolic.svg b/test_assets/icons/cosmic-base/symbolic/actions/edit-delete-symbolic.svg new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/hicolor/22x22/apps/bar.png b/test_assets/icons/hicolor/22x22/apps/bar.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/hicolor/22x22/apps/baz.png b/test_assets/icons/hicolor/22x22/apps/baz.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/hicolor/22x22/apps/browser.png b/test_assets/icons/hicolor/22x22/apps/browser.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/hicolor/22x22/apps/cosmic-cat-tracker.png b/test_assets/icons/hicolor/22x22/apps/cosmic-cat-tracker.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/hicolor/22x22/apps/cosmic-fake-applet.png b/test_assets/icons/hicolor/22x22/apps/cosmic-fake-applet.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/hicolor/22x22/apps/foo.png b/test_assets/icons/hicolor/22x22/apps/foo.png new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/hicolor/index.theme b/test_assets/icons/hicolor/index.theme new file mode 100644 index 0000000..0055865 --- /dev/null +++ b/test_assets/icons/hicolor/index.theme @@ -0,0 +1,22 @@ +[Icon Theme] +Name=Hicolor +Comment=Fake fallback +Hidden=True +Directories=16x16/apps,22x22/apps,scalable/apps + +[16x16/apps] +Context=Applications +Size=16 +Type=Fixed + +[22x22/apps] +Context=Applications +Size=22 +Type=Fixed + +[scalable/apps] +Context=Applications +Size=16 +MinSize=16 +MaxSize=256 +Type=Scalable diff --git a/test_assets/icons/hicolor/scalable/apps/browser.svg b/test_assets/icons/hicolor/scalable/apps/browser.svg new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/hicolor/scalable/apps/cosmic-cat-tracker.svg b/test_assets/icons/hicolor/scalable/apps/cosmic-cat-tracker.svg new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/hicolor/scalable/apps/cosmic-fake-applet.svg b/test_assets/icons/hicolor/scalable/apps/cosmic-fake-applet.svg new file mode 100644 index 0000000..e69de29 diff --git a/test_assets/icons/pop-os-base/index.theme b/test_assets/icons/pop-os-base/index.theme new file mode 100644 index 0000000..26ab5e3 --- /dev/null +++ b/test_assets/icons/pop-os-base/index.theme @@ -0,0 +1,9 @@ +[Icon Theme] +Name=Pop!_OS Base +Comment=Fake fallback +Directories=48x48/apps + +[48x48/apps] +Context=Applications +Size=48 +Type=Fixed