Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

CEF4Delphi and DUnit Test

Post Reply
PioPio
Posts: 42
Joined: Sun Nov 05, 2017 10:25 pm

CEF4Delphi and DUnit Test

Post 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
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CEF4Delphi and DUnit Test

Post 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.
PioPio
Posts: 42
Joined: Sun Nov 05, 2017 10:25 pm

Re: CEF4Delphi and DUnit Test

Post 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
Post Reply