Test_LibraryLoader.dpr 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. program Test_LibraryLoader;
  26. {$I Tests.inc}
  27. uses
  28. SysUtils,
  29. Classes,
  30. libsagui,
  31. BrookLibraryLoader;
  32. type
  33. TEventHolder = class
  34. private
  35. FSender: TObject;
  36. FPassed: Boolean;
  37. public
  38. procedure DoNotifyEvent(ASender: TObject);
  39. property Sender: TObject read FSender write FSender;
  40. property Passed: Boolean read FPassed write FPassed;
  41. end;
  42. procedure TEventHolder.DoNotifyEvent(ASender: TObject);
  43. begin
  44. FPassed := ASender = FSender;
  45. end;
  46. procedure Test_LibraryLoaderCreate;
  47. var
  48. LL: TBrookLibraryLoader;
  49. C: TComponent;
  50. begin
  51. C := TComponent.Create(nil);
  52. LL := TBrookLibraryLoader.Create(C);
  53. try
  54. Assert(LL.Owner = C);
  55. Assert(LL.LibraryName = SG_LIB_NAME);
  56. finally
  57. LL.Destroy;
  58. C.Destroy;
  59. end;
  60. end;
  61. procedure Test_LibraryLoaderLoad;
  62. begin
  63. SgLib.Unload;
  64. Assert(SgLib.Handle = NilHandle);
  65. TBrookLibraryLoader.Load(SG_LIB_NAME);
  66. Assert(SgLib.Handle <> NilHandle);
  67. SgLib.Unload;
  68. Assert(SgLib.Handle = NilHandle);
  69. TBrookLibraryLoader.Load;
  70. Assert(SgLib.Handle <> NilHandle);
  71. end;
  72. procedure Test_LibraryLoaderUnload;
  73. begin
  74. SgLib.Load(SG_LIB_NAME);
  75. Assert(SgLib.Handle <> NilHandle);
  76. TBrookLibraryLoader.Unload;
  77. Assert(SgLib.Handle = NilHandle);
  78. end;
  79. procedure Test_LibraryLoaderIsLoaded;
  80. begin
  81. Assert(not TBrookLibraryLoader.IsLoaded);
  82. TBrookLibraryLoader.Load;
  83. Assert(TBrookLibraryLoader.IsLoaded);
  84. TBrookLibraryLoader.Unload;
  85. Assert(not TBrookLibraryLoader.IsLoaded);
  86. end;
  87. procedure Test_LibraryLoaderOpen;
  88. var
  89. LL: TBrookLibraryLoader;
  90. begin
  91. LL := TBrookLibraryLoader.Create(nil);
  92. try
  93. Assert(not LL.Active);
  94. LL.Open;
  95. Assert(LL.Active);
  96. finally
  97. LL.Destroy;
  98. end;
  99. end;
  100. procedure Test_LibraryLoaderClose;
  101. var
  102. LL: TBrookLibraryLoader;
  103. begin
  104. LL := TBrookLibraryLoader.Create(nil);
  105. try
  106. LL.Open;
  107. Assert(LL.Active);
  108. LL.Close;
  109. Assert(not LL.Active);
  110. finally
  111. LL.Destroy;
  112. end;
  113. end;
  114. procedure Test_LibraryLoaderActive;
  115. var
  116. LL: TBrookLibraryLoader;
  117. begin
  118. LL := TBrookLibraryLoader.Create(nil);
  119. try
  120. Assert(not LL.Active);
  121. LL.Active := not LL.Active;
  122. Assert(LL.Active);
  123. finally
  124. LL.Destroy;
  125. end;
  126. end;
  127. procedure Test_LibraryLoaderLibraryName;
  128. var
  129. LL: TBrookLibraryLoader;
  130. begin
  131. LL := TBrookLibraryLoader.Create(nil);
  132. try
  133. Assert(LL.LibraryName = LL.LIB_NAME);
  134. LL.LibraryName := '';
  135. Assert(LL.LibraryName = LL.LIB_NAME);
  136. LL.LibraryName := 'test';
  137. Assert(LL.LibraryName = 'test');
  138. finally
  139. LL.Destroy;
  140. end;
  141. end;
  142. procedure Test_LibraryLoaderVersion;
  143. var
  144. LL: TBrookLibraryLoader;
  145. begin
  146. LL := TBrookLibraryLoader.Create(nil);
  147. try
  148. Assert(LL.Version = '');
  149. LL.Open;
  150. Assert(LL.Version = Format('%d.%d.%d', [SG_VERSION_MAJOR, SG_VERSION_MINOR,
  151. SG_VERSION_PATCH]));
  152. finally
  153. LL.Destroy;
  154. end;
  155. end;
  156. procedure Test_LibraryLoaderOnLoad;
  157. var
  158. LL: TBrookLibraryLoader;
  159. EH: TEventHolder;
  160. begin
  161. LL := TBrookLibraryLoader.Create(nil);
  162. EH := TEventHolder.Create;
  163. try
  164. LL.OnLoad := EH.DoNotifyEvent;
  165. EH.Sender := LL;
  166. EH.Passed := False;
  167. LL.Open;
  168. Assert(EH.Passed);
  169. EH.Passed := False;
  170. LL.Open;
  171. Assert(not EH.Passed);
  172. finally
  173. LL.Destroy;
  174. EH.Destroy;
  175. end;
  176. end;
  177. procedure Test_LibraryLoaderOnUnload;
  178. var
  179. LL: TBrookLibraryLoader;
  180. EH: TEventHolder;
  181. begin
  182. LL := TBrookLibraryLoader.Create(nil);
  183. EH := TEventHolder.Create;
  184. try
  185. LL.Open;
  186. LL.OnUnload := EH.DoNotifyEvent;
  187. EH.Sender := LL;
  188. EH.Passed := False;
  189. LL.Close;
  190. Assert(EH.Passed);
  191. EH.Passed := False;
  192. LL.Close;
  193. Assert(not EH.Passed);
  194. finally
  195. LL.Destroy;
  196. EH.Destroy;
  197. end;
  198. end;
  199. begin
  200. {$IF (NOT DEFINED(FPC)) AND DEFINED(DEBUG)}
  201. ReportMemoryLeaksOnShutdown := True;
  202. {$ENDIF}
  203. Test_LibraryLoaderCreate;
  204. // Test_LibraryLoaderDestroy - not required
  205. Test_LibraryLoaderLoad;
  206. Test_LibraryLoaderUnload;
  207. Test_LibraryLoaderIsLoaded;
  208. Test_LibraryLoaderOpen;
  209. Test_LibraryLoaderClose;
  210. // Test_LibraryLoaderDefineProperties - not required
  211. Test_LibraryLoaderActive;
  212. Test_LibraryLoaderLibraryName;
  213. Test_LibraryLoaderVersion;
  214. Test_LibraryLoaderOnLoad;
  215. Test_LibraryLoaderOnUnload;
  216. end.