我正在通过 DUnit 在我的应用程序中测试我使用CEF4Delphi创建的一些进程。
以下是重现该问题的 MCVE:
unit MyUnit;
interface
{$I cef.inc}
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
uCEFWindowParent,
uCEFChromiumWindow,
uCEFChromium,
Vcl.ExtCtrls,
Vcl.StdCtrls;
type
TForm1 = class(TForm)
ChromiumWindow1: TChromiumWindow;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure ChromiumWindow1AfterCreated(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FChromiumCreated: Boolean;
procedure WMMove(var aMessage: TWMMove); message WM_MOVE;
procedure WMMoving(var aMessage: TMessage); message WM_MOVING;
public
{ Public declarations }
function IsChromiumCreated: Boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ChromiumWindow1AfterCreated(Sender: TObject);
begin
ChromiumWindow1.LoadURL('https://www.google.com');
FChromiumCreated := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FChromiumCreated := False;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
if not (ChromiumWindow1.CreateBrowser) then
Timer1.Enabled := True;
end;
function TForm1.IsChromiumCreated: Boolean;
begin
Result := FChromiumCreated;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
if not (ChromiumWindow1.CreateBrowser) and not (ChromiumWindow1.Initialized) then
Timer1.Enabled := True
end;
procedure TForm1.WMMove(var aMessage: TWMMove);
begin
inherited;
if (ChromiumWindow1 <> nil) then
ChromiumWindow1.NotifyMoveOrResizeStarted;
end;
procedure TForm1.WMMoving(var aMessage: TMessage);
begin
inherited;
if (ChromiumWindow1 <> nil) then
ChromiumWindow1.NotifyMoveOrResizeStarted;
end;
end.
以下是测试用例:
unit TestMyTest;
{
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,
Vcl.Forms,
MyUnit,
System.Classes;
type
// Test methods for class TForm1
TestTForm1 = class(TTestCase)
strict private
FFormHolder: TForm;
FForm1: TForm1;
public
procedure SetUp; override;
procedure TearDown; override;
published
procedure TestFormActivate;
end;
implementation
procedure TestTForm1.SetUp;
begin
Application.Initialize;
FForm1 := TForm1.Create(nil);
Application.Run;
end;
procedure TestTForm1.TearDown;
begin
FForm1.Free;
FForm1 := nil;
end;
procedure TestTForm1.TestFormActivate;
begin
FForm1.Show;
CheckTrue(FForm1.IsChromiumCreated);
end;
initialization
// Register any test cases with the test runner
RegisterTest(TestTForm1.Suite);
end.
如果我使用 .Show,指令FChromiumCreated := True;
不执行,TChromium 不加载页面,测试返回 false。我不确定,但这可能是因为 TChromium 是异步初始化的,并且在执行测试时 TChromium 尚未完全初始化。
在这种情况下如何执行我的测试?
编辑 我已阅读此答案。就我而言,.Show 确实允许进入下一行测试,但似乎 TChromium 在那个阶段还没有完全初始化。我也尝试了tomazy的建议,但这也不起作用。