utcnotifications.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. unit utcnotifications;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testutils, testregistry, system.notification;
  6. type
  7. { TTestNotification }
  8. TTestNotification= class(TTestCase)
  9. private
  10. FCenter: TNotificationCenter;
  11. FChannel: TChannel;
  12. FChannels: TChannels;
  13. FNotification: TNotification;
  14. FSender : TObject;
  15. FNotifiedNotification : TNotification;
  16. FNotificationName : String;
  17. FNotifyChannels: TChannels;
  18. FNotifyChannel: TChannel;
  19. FNumber : Integer;
  20. procedure DoCancelByname(Sender: TObject; aName: String);
  21. procedure DoChannelEvent(Sender: TObject; aChannel: TChannel);
  22. procedure DoGetChannels(Sender: TObject; aChannels: TChannels);
  23. procedure DoNamedChannelEvent(Sender: TObject; aName: String);
  24. procedure DoNotifiy(Sender: TObject);
  25. procedure DoGetIconNumber(Sender: TObject; var aNumber: Integer);
  26. procedure DoNotifyNotification(Sender: TObject; aNotification: TNotification);
  27. procedure DoSetBadgeNumber(Sender: TObject; const aNumber: Integer);
  28. protected
  29. procedure SetUp; override;
  30. procedure TearDown; override;
  31. property Notification : TNotification Read FNotification;
  32. Property Center : TNotificationCenter Read FCenter;
  33. Property Channels : TChannels Read FChannels;
  34. Property Channel : TChannel Read FChannel;
  35. published
  36. procedure TestHookUp;
  37. procedure TestAssign;
  38. procedure TestAssignChannel;
  39. procedure TestGetIconBadgeCount;
  40. procedure TestSetIconBadgeCount;
  41. procedure TestRequestPermission;
  42. procedure TestResetIconBadgeNumber;
  43. Procedure TestAuthorizationStatus;
  44. procedure TestCancelAll;
  45. procedure TestCancelByName;
  46. procedure TestGetAllChannels;
  47. procedure TestCreateChannel;
  48. procedure TestDeleteChannel;
  49. Procedure TestPresentNotification;
  50. Procedure TestScheduleNotification;
  51. end;
  52. implementation
  53. procedure TTestNotification.TestHookUp;
  54. begin
  55. AssertNotNull('Default',TEventedNotificationCenter.Instance);
  56. AssertNotNull('Center',Center);
  57. AssertNull('Sender',FSender);
  58. AssertNotNull('Channels',FChannels);
  59. AssertNotNull('Channel',FChannel);
  60. AssertTrue('TeventedNotificationCenter.Instance.OnGetIconBadgeNumber',TeventedNotificationCenter.Instance.OnGetIconBadgeNumber=Nil);
  61. AssertTrue('TeventedNotificationCenter.Instance.OnCancelAllNotifications',TeventedNotificationCenter.Instance.OnCancelAllNotifications=Nil);
  62. AssertTrue('TeventedNotificationCenter.Instance.OnCancelNotificationByName',TeventedNotificationCenter.Instance.OnCancelNotificationByName=Nil);
  63. AssertTrue('TeventedNotificationCenter.Instance.OnGetChannels',TeventedNotificationCenter.Instance.OnGetChannels=Nil);
  64. AssertTrue('TeventedNotificationCenter.Instance.OnCreateChannel',TeventedNotificationCenter.Instance.OnCreateChannel=Nil);
  65. AssertTrue('TeventedNotificationCenter.Instance.OnDeleteChannel',TeventedNotificationCenter.Instance.OnDeleteChannel=Nil);
  66. AssertTrue('TeventedNotificationCenter.Instance.OnPresentNotification',TeventedNotificationCenter.Instance.OnPresentNotification=Nil);
  67. AssertTrue('TeventedNotificationCenter.Instance.OnScheduleNotification',TeventedNotificationCenter.Instance.OnScheduleNotification=Nil);
  68. AssertTrue('TeventedNotificationCenter.Instance.OnRequestPermission',TeventedNotificationCenter.Instance.OnRequestPermission=Nil);
  69. AssertTrue('TeventedNotificationCenter.Instance.OnResetIconBadgeNumber',TeventedNotificationCenter.Instance.OnResetIconBadgeNumber=Nil);
  70. AssertTrue('TeventedNotificationCenter.Instance.OnCancelNotification',TeventedNotificationCenter.Instance.OnCancelNotification=Nil);
  71. AssertTrue('TeventedNotificationCenter.Instance.OnSetIconBadgeNumber',TeventedNotificationCenter.Instance.OnSetIconBadgeNumber=Nil);
  72. AssertTrue('TeventedNotificationCenter.Instance.AuthorizationStatus',TAuthorizationStatus.NotDetermined=TeventedNotificationCenter.Instance.AuthorizationStatus);
  73. end;
  74. procedure TTestNotification.TestAssign;
  75. var
  76. N : TNotification;
  77. begin
  78. With Notification do
  79. begin
  80. Name:='x';
  81. Title:='y';
  82. AlertBody:='z';
  83. AlertAction:='q';
  84. Number:=134;
  85. FireDate:=Date+1;
  86. EnableSound:=True;
  87. SoundName:='bigben';
  88. HasAction:=True;
  89. RepeatInterval:=TRepeatInterval.Year;
  90. ChannelId:='c4';
  91. end;
  92. N:=TEventedNotificationCenter.Instance.CreateNotification;
  93. try
  94. N.Assign(Notification);
  95. AssertEquals('Name',Notification.Name,N.Name);
  96. AssertEquals('Title',Notification.Title,N.Title);
  97. AssertEquals('AlertBody',Notification.AlertBody,N.AlertBody);
  98. AssertEquals('AlertAction',Notification.AlertAction,N.AlertAction);
  99. AssertEquals('Number',Notification.Number,N.Number);
  100. AssertEquals('Firedate',Notification.FireDate,N.FireDate);
  101. AssertEquals('EnableSound',Notification.EnableSound,N.EnableSOund);
  102. AssertEquals('SoundName',Notification.SoundName,N.SoundName);
  103. AssertEquals('HasAction',Notification.HasAction,N.HasAction);
  104. AssertTrue('RepeatInterval',Notification.RepeatInterval=N.RepeatInterval);
  105. AssertEquals('ChannelId',Notification.ChannelId,N.ChannelId);
  106. finally
  107. N.Free;
  108. end;
  109. end;
  110. procedure TTestNotification.TestAssignChannel;
  111. begin
  112. end;
  113. procedure TTestNotification.TestGetIconBadgeCount;
  114. begin
  115. TeventedNotificationCenter.Instance.OnGetIconBadgeNumber:=@DoGetIconNumber;
  116. AssertEquals('Correct number',12,FCenter.ApplicationIconBadgeNumber);
  117. end;
  118. procedure TTestNotification.TestSetIconBadgeCount;
  119. begin
  120. TeventedNotificationCenter.Instance.OnSetIconBadgeNumber:=@DoSetBadgeNumber;
  121. Center.ApplicationIconBadgeNumber:=16;
  122. AssertNotNull('Called',FSender);
  123. AssertEquals('Number',16,FNumber);
  124. end;
  125. procedure TTestNotification.TestRequestPermission;
  126. begin
  127. TEventedNotificationCenter.Instance.OnRequestPermission:=@DoNotifiy;
  128. Center.RequestPermission;
  129. AssertNotNull('Called',FSender);
  130. end;
  131. procedure TTestNotification.TestResetIconBadgeNumber;
  132. begin
  133. TeventedNotificationCenter.Instance.OnResetIconBadgeNumber:=@DoNotifiy;
  134. Center.ResetIconBadgeNumber;
  135. AssertNotNull('Called',FSender);
  136. end;
  137. procedure TTestNotification.TestAuthorizationStatus;
  138. begin
  139. TeventedNotificationCenter.Instance.AuthorizationStatus:=TAuthorizationStatus.NotDetermined;
  140. AssertTrue('Correct',TAuthorizationStatus.NotDetermined=Center.AuthorizationStatus);
  141. end;
  142. procedure TTestNotification.TestCancelAll;
  143. begin
  144. TeventedNotificationCenter.Instance.OnCancelAllNotifications:=@DoNotifiy;
  145. Center.CancelAll;
  146. AssertNotNull('Called',FSender);
  147. end;
  148. procedure TTestNotification.TestCancelByName;
  149. begin
  150. TeventedNotificationCenter.Instance.OnCancelNotificationByName:=@DoCancelByname;
  151. Center.CancelNotification('Name');
  152. AssertNotNull('Called',FSender);
  153. AssertEquals('Name','Name',FNotificationName);
  154. end;
  155. procedure TTestNotification.TestGetAllChannels;
  156. begin
  157. TeventedNotificationCenter.Instance.OnGetChannels:=@DoGetChannels;
  158. Center.GetAllChannels(Channels);
  159. AssertNotNull('Called',FSender);
  160. AssertSame('Channels',Channels,FNotifyChannels);
  161. end;
  162. procedure TTestNotification.TestCreateChannel;
  163. begin
  164. TeventedNotificationCenter.Instance.OnCreateChannel:=@DoChannelEvent;
  165. Center.CreateOrUpdateChannel(Channel);
  166. AssertNotNull('Called',FSender);
  167. AssertSame('Channel',Channel,FNotifyChannel);
  168. end;
  169. procedure TTestNotification.TestDeleteChannel;
  170. begin
  171. TeventedNotificationCenter.Instance.OnDeleteChannel:=@DoNamedChannelEvent;
  172. Center.DeleteChannel('Name');
  173. AssertNotNull('Called',FSender);
  174. AssertEquals('Name','Name',FNotificationName);
  175. end;
  176. procedure TTestNotification.TestPresentNotification;
  177. begin
  178. TeventedNotificationCenter.Instance.OnPresentNotification:=@DoNotifyNotification;
  179. Center.PresentNotification(Notification);
  180. AssertNotNull('Called',FSender);
  181. AssertSame('Notification',Notification,FNotifiedNotification);
  182. end;
  183. procedure TTestNotification.TestScheduleNotification;
  184. begin
  185. TeventedNotificationCenter.Instance.OnScheduleNotification:=@DoNotifyNotification;
  186. Center.ScheduleNotification(Notification);
  187. AssertNotNull('Called',FSender);
  188. AssertSame('Notification',Notification,FNotifiedNotification);
  189. end;
  190. procedure TTestNotification.DoNotifiy(Sender: TObject);
  191. begin
  192. FSender:=Sender;
  193. end;
  194. procedure TTestNotification.DoCancelByname(Sender: TObject; aName: String);
  195. begin
  196. FNotificationName:=aName;
  197. FSender:=Sender;
  198. end;
  199. procedure TTestNotification.DoChannelEvent(Sender: TObject; aChannel: TChannel);
  200. begin
  201. FSender:=Sender;
  202. FNotifyChannel:=aChannel
  203. end;
  204. procedure TTestNotification.DoGetChannels(Sender: TObject; aChannels: TChannels);
  205. begin
  206. FSender:=Sender;
  207. FNotifyChannels:=aChannels;
  208. end;
  209. procedure TTestNotification.DoNamedChannelEvent(Sender: TObject; aName: String);
  210. begin
  211. FSender:=Sender;
  212. FNotificationName:=aName;
  213. end;
  214. procedure TTestNotification.DoNotifyNotification(Sender: TObject; aNotification: TNotification);
  215. begin
  216. FSender:=Sender;
  217. FNotifiedNotification:=aNotification;
  218. end;
  219. procedure TTestNotification.DoSetBadgeNumber(Sender: TObject; const aNumber: Integer);
  220. begin
  221. FSender:=Sender;
  222. FNumber:=aNumber;
  223. end;
  224. procedure TTestNotification.DoGetIconNumber(Sender: TObject; var aNumber: Integer);
  225. begin
  226. aNumber:=12;
  227. end;
  228. procedure TTestNotification.SetUp;
  229. begin
  230. FNotification:=TEventedNotificationCenter.Instance.CreateNotification;
  231. FCenter:=TNotificationCenter.Create(Nil);
  232. FSender:=nil;
  233. FChannels:=TChannels.Create();
  234. FChannel:=TChannel.Create;
  235. FNotifiedNotification:=Nil;
  236. FNotificationName:='';
  237. FNotifyChannels:=Nil;
  238. FNumber:=0;
  239. TeventedNotificationCenter.Instance.OnGetIconBadgeNumber:=Nil;
  240. TeventedNotificationCenter.Instance.OnCancelAllNotifications:=Nil;
  241. TeventedNotificationCenter.Instance.OnCancelNotificationByName:=Nil;
  242. TeventedNotificationCenter.Instance.OnGetChannels:=Nil;
  243. TeventedNotificationCenter.Instance.OnCreateChannel:=Nil;
  244. TeventedNotificationCenter.Instance.OnDeleteChannel:=Nil;
  245. TeventedNotificationCenter.Instance.OnPresentNotification:=Nil;
  246. TeventedNotificationCenter.Instance.OnScheduleNotification:=Nil;
  247. TeventedNotificationCenter.Instance.OnRequestPermission:=Nil;
  248. TeventedNotificationCenter.Instance.OnResetIconBadgeNumber:=Nil;
  249. TeventedNotificationCenter.Instance.OnCancelNotification:=Nil;
  250. TeventedNotificationCenter.Instance.OnSetIconBadgeNumber:=Nil;
  251. TeventedNotificationCenter.Instance.AuthorizationStatus:=TAuthorizationStatus.NotDetermined;
  252. end;
  253. procedure TTestNotification.TearDown;
  254. begin
  255. FreeAndNil(FChannels);
  256. FreeAndNil(FChannel);
  257. FreeAndNil(FNotification);
  258. FreeAndNil(FCenter);
  259. FSender:=nil;
  260. end;
  261. initialization
  262. RegisterTest(TTestNotification);
  263. end.