我正在测试一个带有CEF4Delphi组件的TFrame,但是在释放包含TFrame和相关CEF4Delphi组件的TForm时遇到了问题。
以下是最小示例
program MyTFrameExample;
{
Delphi DUnit Test Project
-------------------------
This project contains the DUnit test framework and the GUI/Console test runners.
Add "CONSOLE_TESTRUNNER" to the conditional defines entry in the project options
to use the console test runner. Otherwise the GUI test runner will be used by
default.
}
{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
madExcept,
madLinkDisAsm,
madListHardware,
madListProcesses,
madListModules,
DUnitTestRunner,
Vcl.Forms,
WinApi.Windows,
uCEFApplication,
SSFrame in '..\..\SS\SSFrame.pas' {SSFrm: TFrame},
TestSSFrame in '..\..\Test\TestSSFrame.pas';
{*.RES}
{$I cef.inc}
{$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE}
begin
GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.SingleProcess := True;
if GlobalCEFApp.StartMainProcess then
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
DUnitTestRunner.RunRegisteredTests;
Application.Run;
end;
GlobalCEFApp.Free;
end.
unit TestSSFrame;
{
Delphi DUnit Test Case
----------------------
This unit contains a skeleton test case class generated by the Test Case Wizard.
Modify the generated code to correctly setup and call the methods from the unit
being tested.
}
interface
uses
TestFramework,
System.SysUtils,
Vcl.Graphics,
uCEFChromium,
Winapi.Windows,
System.Variants,
uCEFInterfaces,
uCEFChromiumWindow,
Vcl.Dialogs,
Vcl.Controls,
uCEFWindowParent,
Vcl.Forms,
Winapi.Messages,
SSFrame,
System.Classes;
type
// Test methods for class TSSFrm
TestTSSFrm = class(TTestCase)
strict private
FSSFrm: TSSFrm;
FMainForm: TForm;
public
procedure SetUp; override;
procedure TearDown; override;
published
procedure TestChromium1AfterCreated;
end;
implementation
procedure TestTSSFrm.SetUp;
begin
Application.CreateForm(TForm, FMainForm);
FSSFrm := TSSFrm.Create(FMainForm);
FSSFrm.Parent := FMainForm;
FSSFrm.Visible := True;
FSSFrm.Align := alClient;
FSSFrm.Chromium1.CreateBrowser(FSSFrm.ChromiumWindow1);
if not (FSSFrm.ChromiumWindow1.CreateBrowser) then
FSSFrm.Timer1.Enabled := True;
while FSSFrm.Chromium1.Initialized = False do
begin
Application.ProcessMessages;
end;
FMainForm.Show;
end;
procedure TestTSSFrm.TearDown;
begin
FMainForm.Free;
[enter link description here][1] FMainForm := nil;
end;
procedure TestTSSFrm.TestChromium1AfterCreated;
var
a: Boolean;
begin
if (FMainForm.Components[0] is TSSFrm) then
begin
a := (FMainForm.Components[0] as TSSFrm).IsGoogleSearchPageCreated;
CheckTrue(a);
end;
end;
initialization
// Register any test cases with the test runner
RegisterTest(TestTSSFrm.Suite);
end.
unit SSFrame;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
uCEFWindowParent,
uCEFChromiumWindow,
uCEFInterfaces,
uCEFChromium,
Vcl.StdCtrls,
Vcl.ExtCtrls;
type
TSSFrm = class(TFrame)
Chromium1: TChromium;
ChromiumWindow1: TChromiumWindow;
Timer1: TTimer;
procedure Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
FIsGoogleSearchPageLoaded: Boolean;
FIsGoogleSearchPageCreated: Boolean;
public
{ Public declarations }
function IsGoogleSearchPageCreated: Boolean;
end;
implementation
{$R *.dfm}
procedure TSSFrm.Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser);
begin
FIsGoogleSearchPageLoaded := False;
FIsGoogleSearchPageCreated := True;
end;
function TSSFrm.IsGoogleSearchPageCreated: Boolean;
begin
Result := FIsGoogleSearchPageCreated;
end;
procedure TSSFrm.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
if not (ChromiumWindow1.CreateBrowser) and not (ChromiumWindow1.Initialized) then
Timer1.Enabled := True;
end;
end.
当我运行TestTSSFrm.TestChromium1AfterCreated时;测试通过,但是当关闭测试应用程序时,应用程序本身不会完全关闭,我必须手动从IDE重置它。如果我注释掉这些行
FMainForm.Free;
FMainForm := nil;
然后关闭测试应用程序没有问题,但我必须手动关闭表单。
我在哪里做错了?