user_notify/platform_impl/
mock.rs1use std::collections::HashMap;
6
7use async_trait::async_trait;
8use tokio::sync::RwLock;
9
10use crate::{NotificationBuilder, NotificationHandle, NotificationManager};
11
12#[derive(Debug, Clone)]
14pub struct NotificationHandleMock {
15 id: String,
16 user_info: HashMap<String, String>,
17}
18
19impl NotificationHandle for NotificationHandleMock {
20 fn close(&self) -> Result<(), crate::Error> {
21 log::info!("called close notification handle {self:?}");
22 Ok(())
23 }
24
25 fn get_id(&self) -> String {
26 self.id.clone()
27 }
28
29 fn get_user_info(&self) -> &HashMap<String, String> {
30 &self.user_info
31 }
32}
33
34#[derive(Debug, Default)]
38pub struct NotificationManagerMock {
39 active_notifications: RwLock<Vec<NotificationHandleMock>>,
40}
41
42impl NotificationManagerMock {
43 pub fn new() -> Self {
45 Self::default()
46 }
47
48 async fn add_notification(&self, notification: NotificationHandleMock) {
49 self.active_notifications.write().await.push(notification);
50 }
51}
52
53#[async_trait]
54impl NotificationManager for NotificationManagerMock {
55 async fn get_notification_permission_state(&self) -> Result<bool, crate::Error> {
56 log::info!("NotificationManagerMock::get_notification_permission_state");
57 Ok(true)
58 }
59
60 async fn first_time_ask_for_notification_permission(&self) -> Result<bool, crate::Error> {
61 log::info!("NotificationManagerMock::first_time_ask_for_notification_permission");
62 Ok(true)
63 }
64
65 fn register(
66 &self,
67 _handler_callback: Box<dyn Fn(crate::NotificationResponse) + Send + Sync + 'static>,
68 categories: Vec<crate::NotificationCategory>,
69 ) -> Result<(), crate::Error> {
70 log::info!("NotificationManagerMock::register {categories:?}");
71 Ok(())
72 }
73
74 fn remove_all_delivered_notifications(&self) -> Result<(), crate::Error> {
75 let mut active_notifications = self.active_notifications.try_write()?;
76 let removed_notifiactions = active_notifications.drain(..);
77 log::info!(
78 "NotificationManagerMock::remove_all_delivered_notifications -> removed the following notifications {removed_notifiactions:?}"
79 );
80 Ok(())
81 }
82
83 fn remove_delivered_notifications(&self, ids: Vec<&str>) -> Result<(), crate::Error> {
84 let mut active_notifications = self.active_notifications.try_write()?;
85 let all_notifications = active_notifications.drain(..);
86 let mut kept = Vec::new();
87 let mut removed = Vec::new();
88 for n in all_notifications {
89 if ids.contains(&n.id.as_str()) {
90 removed.push(n);
91 } else {
92 kept.push(n);
93 }
94 }
95 active_notifications.append(&mut kept);
96
97 log::info!(
98 "NotificationManagerMock::remove_delivered_notifications -> removed the following notifications {removed:?}"
99 );
100 Ok(())
101 }
102
103 async fn get_active_notifications(
104 &self,
105 ) -> Result<Vec<Box<dyn NotificationHandle>>, crate::Error> {
106 let active_notifications = self.active_notifications.read().await;
107 log::info!(
108 "NotificationManagerMock::get_active_notifications - the active notifications are: {active_notifications:?}"
109 );
110 Ok(active_notifications
111 .clone()
112 .into_iter()
113 .map(|n| Box::new(n) as Box<dyn NotificationHandle>)
114 .collect())
115 }
116
117 async fn send_notification(
118 &self,
119 builder: NotificationBuilder,
120 ) -> Result<Box<dyn NotificationHandle>, crate::Error> {
121 log::info!("show notification {self:?}");
122 let id = uuid::Uuid::new_v4().to_string();
123
124 let handle = NotificationHandleMock {
125 id,
126 user_info: builder.user_info.unwrap_or_default(),
127 };
128
129 self.add_notification(handle.clone()).await;
130 Ok(Box::new(handle) as Box<dyn NotificationHandle>)
131 }
132}