Page 1 of 1

CEF4Delphi and DUnit Test

Posted: Sat Jan 13, 2018 5:55 pm
by PioPio
Hello,

I am testing a few processes I have created in my application via DUnit (http://docwiki.embarcadero.com/RADStudi ... t_Overview).

The following is the skeleton of the source of the project I want to test:

Code: Select all

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 FormActivate(Sender: TObject);
  private
    { Private declarations }
    procedure WMMove(var aMessage: TWMMove); message WM_MOVE;
    procedure WMMoving(var aMessage: TMessage); message WM_MOVING;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ChromiumWindow1AfterCreated(Sender: TObject);
begin
  ChromiumWindow1.LoadURL('https://www.google.com');
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  if not (ChromiumWindow1.CreateBrowser) then
    Timer1.Enabled := True;
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 skeleton of the unit test:

Code: Select all

unit myTest;

interface

uses
  TestFramework,
  System.SysUtils,
  Vcl.Graphics,
  uCEFChromium,
  Winapi.Windows,
  System.Variants,
  uCEFChromiumWindow,
  Vcl.Dialogs,
  Vcl.Controls,
  uCEFWindowParent,
  Vcl.Forms,
  Winapi.Messages,
  System.Classes;

type
  // Test methods for class TForm1

  TestTForm1 = class(TTestCase)
  strict private
    FForm1: TForm1;
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestFormA;
  end;

implementation

procedure TestTForm1.SetUp;
begin
  Form1 := TForm1.Create(nil);
end;

procedure TestTForm1.TearDown;
begin
  FForm1.Free;
  FForm1 := nil;
end;

procedure TestTForm1.TestFormA;
begin
  FForm1.ShowModal;//Chromium shows correctly but because the form is modal the application does not perform the tests
  FForm1.Show;//The application seems to freeze

  {test to be performed are placed here}  
end;

initialization
  // Register any test cases with the test runner
  RegisterTest(TestTForm1.Suite);

end.
If I use .ShowModal, CEF4Delphi loads the page correctly but because of the modal state of FForm1 I cannot perform the tests after the line FForm1.ShowModal. If I use FForm1.Show I can progress to the next line but the page web is not loaded up, therefore, I cannot carry out the test either.

Why isn't the page loaded with .Show ? How can perform my test ?

Many thanks
Alberto

Re: CEF4Delphi and DUnit Test

Posted: Sun Jan 14, 2018 8:46 am
by salvadordf
Hi,

I don't have much experience with DUnit but TChromium is initialized asynchronously and it may be incompatible with DUnit.

Perhaps DUnit is destroying the form in the TearDown function before it's fully initialized.

Re: CEF4Delphi and DUnit Test

Posted: Sun Jan 14, 2018 4:18 pm
by PioPio
Hi Salvador,

I have posted the issue on StackOverflow and Craig Young found a solution. So far if is working.
https://stackoverflow.com/questions/482 ... -and-dunit

Thanks for pointing out in the right direction.

Alberto