frmrunform.pp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. unit frmrunform;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, js, web, hotreloadclient, browserTestRunner, fpcunitreport;
  6. Type
  7. { TTestRunForm }
  8. TTestRunForm = Class(TRunForm)
  9. private
  10. FElementID: String;
  11. BOutput : TJSHTMLInputElement;
  12. BRebuild : TJSHTMLInputElement;
  13. BRun : TJSHTMLInputElement;
  14. BuildID : TJSHTMLElement;
  15. FLastReq : TJSXMLHttpRequest;
  16. POutput: TJSElement;
  17. function RecompileClick(Event{%H-}: TJSMouseEvent): boolean;
  18. procedure CreatePanel(aParent: TJSElement);
  19. function DoReloadChange(Event{%H-}: TEventListenerEvent): boolean;
  20. function doResponse(Event{%H-}: TEventListenerEvent): boolean;
  21. function RunClick(aEvent{%H-}: TJSMouseEvent): boolean;
  22. function ToggleOutput(aEvent{%H-}: TJSMouseEvent): boolean;
  23. Public
  24. Constructor create(AOwner : TComponent); override;
  25. Procedure Initialize; override;
  26. // Where to place the control. Default fpcunit-control
  27. Property ElementID : String Read FElementID Write FElementID;
  28. end;
  29. implementation
  30. function TTestRunForm.doResponse(Event: TEventListenerEvent): boolean;
  31. Var
  32. Data : TJSObject;
  33. S : String;
  34. begin
  35. Result:=True;
  36. console.warn('Compile request response received');
  37. try
  38. Data:=TJSJSON.parseObject(FLastReq.responseText);
  39. if data['success'] then
  40. begin
  41. if isInteger(data['compileID']) then
  42. begin
  43. S:=String(data['compileID']);
  44. BuildID.innerText:=S;
  45. end;
  46. end
  47. else
  48. BuildID.innerText:='no build yet';
  49. except
  50. console.error('Compile request response received non-json response: '+FLastReq.ResponseText);
  51. end;
  52. FreeAndNil(FLastReq);
  53. end;
  54. function TTestRunForm.RunClick(aEvent: TJSMouseEvent): boolean;
  55. begin
  56. Result:=True;
  57. if Assigned(OnRun) then
  58. OnRun(Self);
  59. end;
  60. function TTestRunForm.ToggleOutput(aEvent: TJSMouseEvent): boolean;
  61. Var
  62. S : String;
  63. P : Integer;
  64. begin
  65. Result:=True;
  66. S:=POutput.className;
  67. P:=Pos(' in',S);
  68. if P>0 then
  69. S:=Copy(S,1,P-1)
  70. else
  71. S:=S+' in';
  72. POutput.className:=S;
  73. end;
  74. function TTestRunForm.RecompileClick(Event: TJSMouseEvent): boolean;
  75. Var
  76. Req : TJSXMLHttpRequest;
  77. begin
  78. Result:=True;
  79. console.info('Recompile requested');
  80. Req:=TJSXMLHttpRequest.new;
  81. Req.addEventListener('load',@DoResponse);
  82. Req.open('POST','$sys/compile');
  83. Req.send;
  84. FLastReq:=Req;
  85. end;
  86. Procedure TTestRunForm.CreatePanel(aParent : TJSElement);
  87. Var
  88. Panel : TJSElement;
  89. BG,D,PanelContent : TJSElement;
  90. CB : TJSHTMLInputElement;
  91. begin
  92. Panel:=aParent;
  93. // attrs are default array property...
  94. Panel['class']:='panel panel-default';
  95. // Innner panel
  96. PanelContent:=document.createElement('div');
  97. Panel.appendChild(PanelContent);
  98. PanelContent['class']:='panel-heading';
  99. BG:=document.createElement('div');
  100. BG['class']:='btn-group';
  101. PanelContent.appendChild(BG);
  102. // Run Button
  103. BRun:=TJSHTMLInputElement(document.createElement('button'));
  104. BRun['id']:=ElementID+'-BRun';
  105. BRun['type']:='button';
  106. BRun['class']:='btn btn-primary';
  107. BRun.appendChild(document.createTextNode('Re-Run'));
  108. BRun.onclick:=@RunClick;
  109. BG.appendChild(BRun);
  110. // Rebuild Button
  111. BRebuild:=TJSHTMLInputElement(document.createElement('button'));
  112. BRebuild['id']:=ElementID+'-BRebuild';
  113. BRebuild['type']:='button';
  114. BRebuild['class']:='btn btn-primary';
  115. BRebuild.appendChild(document.createTextNode('Recompile'));
  116. BRebuild.onclick:=@RecompileClick;
  117. BG.appendChild(BRebuild);
  118. // Output Button
  119. BOutput:=TJSHTMLInputElement(document.createElement('button'));
  120. BOutput['id']:=ElementID+'-BRebuild';
  121. BOutput['type']:='button';
  122. BOutput['class']:='btn btn-primary';
  123. BOutput.appendChild(document.createTextNode('Output'));
  124. BOutput.onclick:=@ToggleOutput;
  125. BG.appendChild(BOutput);
  126. // Span for build ID
  127. D:=Document.createElement('span');
  128. D.id:='buildlabel';
  129. D['style']:='margin-left: 15px; margin-right: 15px';
  130. D.appendChild(Document.createTextNode('Build ID:'));
  131. PanelContent.appendChild(D);
  132. BuildID:=TJSHTMLElement(document.createElement('span'));
  133. BuildID['id']:='recompileID';
  134. BuildID['class']:='badge';
  135. BuildID.appendChild(document.createTextNode('no build yet'));
  136. PanelContent.appendChild(BuildID);
  137. // Combo box to trigger onload
  138. D:=Document.createElement('label');
  139. D['class']:='checkbox-inline';
  140. D['style']:='margin-left: 15px';
  141. cb:=TJSHTMLInputElement(document.createElement('input'));
  142. cb['id']:='cbautoreload';
  143. cb['type']:='checkbox';
  144. cb.onchange:=@DoReloadChange;
  145. // Sync with hotreload
  146. cb.checked:=THotReload.getglobal.options.Reload;
  147. D.AppendChild(cb);
  148. D.appendChild(Document.createTextNode('Reload page on build/sync'));
  149. PanelContent.appendChild(D);
  150. // Collapsible panel For compiler output
  151. POutput:=document.createElement('div');
  152. POutput['class']:='panel-collapse collapse';
  153. D:=Document.createElement('div');
  154. D['class']:='panel-body';
  155. D['id']:=ElementID+'-output';
  156. // Tell it to hotreload.
  157. THotReload.getglobal.options.OverlayElementID:=ElementID+'-output';
  158. POutput.appendChild(D);
  159. Panel.appendChild(POutput);
  160. // Hint
  161. end;
  162. constructor TTestRunForm.Create(aOwner : TComponent);
  163. begin
  164. inherited;
  165. ElementID:='fpcunit-controller'
  166. end;
  167. procedure TTestRunForm.Initialize;
  168. Var
  169. Panel : TJSElement;
  170. begin
  171. inherited Initialize;
  172. THotReload.StartHotReload;
  173. Panel:=document.getElementById(Self.ElementID);
  174. if Panel<>Nil then
  175. CreatePanel(Panel);
  176. // Start hotreload
  177. end;
  178. function TTestRunForm.DoReloadChange(Event: TEventListenerEvent): boolean;
  179. begin
  180. Result:=True;
  181. THotReload.getglobal.options.Reload:=not THotReload.getglobal.options.Reload;
  182. end;
  183. end.