I am testing a few processes I have created with CEF4Delphi in my application via DUnit.
The following is a MCVE to reproduce the issue:
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.
and the following is the Test case:
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.
If I use .Show, the instruction FChromiumCreated := True;
is not executed, TChromium does not load the page and the test returns false.
I am not sure but this may be because TChromium is initialised asynchronously and when the test is performed TChromium is not initialised completely yet.
How can perform my test in this case?
Edit I have read this answer . In my case .Show does allow to progress to the next line of the test but it seems TChromium hasn't initialised completely at that stage. I also tried the suggestion from tomazy but that does not work either.