2014-04-23 22:29:32 +00:00
|
|
|
unit fonttests;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
{ Font tests
|
|
|
|
This unit tests writing out to and reading back from files.
|
|
|
|
}
|
|
|
|
|
|
|
|
uses
|
|
|
|
// Not using Lazarus package as the user may be working with multiple versions
|
|
|
|
// Instead, add .. to unit search path
|
|
|
|
Classes, SysUtils, fpcunit, testregistry,
|
2014-04-24 22:31:01 +00:00
|
|
|
fpspreadsheet, xlsbiff2, xlsbiff5, xlsbiff8 {and a project requirement for lclbase for utf8 handling},
|
2014-04-23 22:29:32 +00:00
|
|
|
testsutility;
|
|
|
|
|
|
|
|
var
|
|
|
|
// Norm to test against - list of font sizes that should occur in spreadsheet
|
|
|
|
SollSizes: array[0..12] of single; //"Soll" is a German word in Dutch accountancy jargon meaning "normative value to check against". There ;)
|
|
|
|
SollStyles: array[0..15] of TsFontStyles;
|
|
|
|
|
|
|
|
// Initializes Soll*/normative variables.
|
|
|
|
// Useful in test setup procedures to make sure the norm is correct.
|
|
|
|
procedure InitSollSizes;
|
|
|
|
procedure InitSollStyles;
|
|
|
|
|
|
|
|
type
|
|
|
|
{ TSpreadWriteReadFontTests }
|
2014-04-24 09:25:31 +00:00
|
|
|
// Write to xls/xml file and read back
|
2014-04-23 22:29:32 +00:00
|
|
|
TSpreadWriteReadFontTests = class(TTestCase)
|
|
|
|
private
|
|
|
|
protected
|
|
|
|
// Set up expected values:
|
|
|
|
procedure SetUp; override;
|
|
|
|
procedure TearDown; override;
|
2014-04-25 09:32:34 +00:00
|
|
|
procedure TestWriteReadBold(AFormat: TsSpreadsheetFormat);
|
2014-04-24 22:31:01 +00:00
|
|
|
procedure TestWriteReadFont(AFormat: TsSpreadsheetFormat; AFontName: String);
|
2014-04-25 09:02:10 +00:00
|
|
|
|
2014-04-23 22:29:32 +00:00
|
|
|
published
|
2014-04-24 22:31:01 +00:00
|
|
|
// BIFF2 test cases
|
2014-04-25 09:32:34 +00:00
|
|
|
procedure TestWriteReadBoldBIFF2;
|
2014-04-24 22:31:01 +00:00
|
|
|
procedure TestWriteReadFontBIFF2_Arial;
|
|
|
|
procedure TestWriteReadFontBIFF2_TimesNewRoman;
|
|
|
|
procedure TestWriteReadFontBIFF2_CourierNew;
|
|
|
|
|
|
|
|
// BIFF8 test cases
|
2014-04-25 09:32:34 +00:00
|
|
|
procedure TestWriteReadBoldBIFF8;
|
2014-04-24 22:31:01 +00:00
|
|
|
procedure TestWriteReadFontBIFF8_Arial;
|
|
|
|
procedure TestWriteReadFontBIFF8_TimesNewRoman;
|
|
|
|
procedure TestWriteReadFontBIFF8_CourierNew;
|
2014-04-23 22:29:32 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
TypInfo;
|
|
|
|
|
|
|
|
const
|
|
|
|
FontSheet = 'Font';
|
|
|
|
|
|
|
|
// When adding tests, add values to this array
|
|
|
|
// and increase array size in variable declaration
|
|
|
|
procedure InitSollSizes;
|
|
|
|
begin
|
|
|
|
// Set up norm - MUST match spreadsheet cells exactly
|
|
|
|
SollSizes[0]:=8.0;
|
|
|
|
SollSizes[1]:=9.0;
|
|
|
|
SollSizes[2]:=10.0;
|
|
|
|
SollSizes[3]:=11.0;
|
|
|
|
SollSizes[4]:=12.0;
|
|
|
|
SollSizes[5]:=13.0;
|
|
|
|
SollSizes[6]:=14.0;
|
|
|
|
SollSizes[7]:=16.0;
|
|
|
|
SollSizes[8]:=18.0;
|
|
|
|
SollSizes[9]:=20.0;
|
|
|
|
SollSizes[10]:=24.0;
|
|
|
|
SollSizes[11]:=32.0;
|
|
|
|
SollSizes[12]:=48.0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure InitSollStyles;
|
|
|
|
begin
|
|
|
|
SollStyles[0] := [];
|
|
|
|
SollStyles[1] := [fssBold];
|
|
|
|
SolLStyles[2] := [fssItalic];
|
|
|
|
SollStyles[3] := [fssBold, fssItalic];
|
|
|
|
SollStyles[4] := [fssUnderline];
|
|
|
|
SollStyles[5] := [fssUnderline, fssBold];
|
|
|
|
SollStyles[6] := [fssUnderline, fssItalic];
|
|
|
|
SollStyles[7] := [fssUnderline, fssBold, fssItalic];
|
|
|
|
SollStyles[8] := [fssStrikeout];
|
|
|
|
SollStyles[9] := [fssStrikeout, fssBold];
|
|
|
|
SolLStyles[10] := [fssStrikeout, fssItalic];
|
|
|
|
SollStyles[11] := [fssStrikeout, fssBold, fssItalic];
|
|
|
|
SollStyles[12] := [fssStrikeout, fssUnderline];
|
|
|
|
SollStyles[13] := [fssStrikeout, fssUnderline, fssBold];
|
|
|
|
SollStyles[14] := [fssStrikeout, fssUnderline, fssItalic];
|
|
|
|
SollStyles[15] := [fssStrikeout, fssUnderline, fssBold, fssItalic];
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TSpreadWriteReadFontTests }
|
|
|
|
|
|
|
|
procedure TSpreadWriteReadFontTests.SetUp;
|
|
|
|
begin
|
|
|
|
inherited SetUp;
|
|
|
|
InitSollSizes;
|
|
|
|
InitSollStyles;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadWriteReadFontTests.TearDown;
|
|
|
|
begin
|
|
|
|
inherited TearDown;
|
|
|
|
end;
|
|
|
|
|
2014-04-25 09:32:34 +00:00
|
|
|
procedure TSpreadWriteReadFontTests.TestWriteReadBold(AFormat: TsSpreadsheetFormat);
|
|
|
|
var
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
row, col: Integer;
|
|
|
|
MyCell: PCell;
|
|
|
|
TempFile: string; //write xls/xml to this file and read back from it
|
|
|
|
currValue: String;
|
|
|
|
expectedValue: String;
|
|
|
|
begin
|
|
|
|
TempFile:=GetTempFileName;
|
|
|
|
{// Not needed: use workbook.writetofile with overwrite=true
|
|
|
|
if fileexists(TempFile) then
|
|
|
|
DeleteFile(TempFile);
|
|
|
|
}
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
|
|
|
MyWorkSheet:= MyWorkBook.AddWorksheet(FontSheet);
|
|
|
|
|
|
|
|
// Write out a cell without "bold"formatting style
|
|
|
|
row := 0;
|
|
|
|
col := 0;
|
|
|
|
MyWorksheet.WriteUTF8Text(row, col, 'not bold');
|
|
|
|
MyCell := MyWorksheet.FindCell(row, col);
|
|
|
|
if MyCell = nil then
|
|
|
|
fail('Error in test code. Failed to get cell.');
|
|
|
|
CheckEquals(uffBold in MyCell^.UsedFormattingFields, false,
|
|
|
|
'Test unsaved bold attribute, cell '+CellNotation(MyWorksheet,Row,Col));
|
|
|
|
|
|
|
|
// Write out a cell with "bold"formatting style
|
|
|
|
inc(row);
|
|
|
|
MyWorksheet.WriteUTF8Text(row, col, 'bold');
|
|
|
|
MyWorksheet.WriteUsedFormatting(row, col, [uffBold]);
|
|
|
|
MyCell := MyWorksheet.FindCell(row, col);
|
|
|
|
if MyCell = nil then
|
|
|
|
fail('Error in test code. Failded to get cell.');
|
|
|
|
CheckEquals(uffBold in MyCell^.UsedFormattingFields, true,
|
|
|
|
'Test unsaved bold attribute, cell '+CellNotation(MyWorksheet,Row, Col));
|
|
|
|
|
|
|
|
MyWorkBook.WriteToFile(TempFile, AFormat, true);
|
|
|
|
MyWorkbook.Free;
|
|
|
|
|
|
|
|
// Open the spreadsheet, as biff8
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
|
|
|
MyWorkbook.ReadFromFile(TempFile, AFormat);
|
|
|
|
if AFormat = sfExcel2 then
|
|
|
|
MyWorksheet := MyWorkbook.GetFirstWorksheet // only 1 sheet for BIFF2
|
|
|
|
else
|
|
|
|
MyWorksheet := GetWorksheetByName(MyWorkBook, FontSheet);
|
|
|
|
if MyWorksheet=nil then
|
|
|
|
fail('Error in test code. Failed to get named worksheet');
|
|
|
|
|
|
|
|
// Try to read cell without "bold"
|
|
|
|
row := 0;
|
|
|
|
col := 0;
|
|
|
|
MyCell := MyWorksheet.FindCell(row, col);
|
|
|
|
if MyCell = nil then
|
|
|
|
fail('Error in test code. Failed to get cell.');
|
|
|
|
CheckEquals(uffBold in MyCell^.UsedFormattingFields, false,
|
|
|
|
'Test saved bold attribute, cell '+CellNotation(MyWorksheet,row,col));
|
|
|
|
|
|
|
|
// Try to read cell with "bold"
|
|
|
|
inc(row);
|
|
|
|
MyCell := MyWorksheet.FindCell(row, col);
|
|
|
|
if MyCell = nil then
|
|
|
|
fail('Error in test code. Failed to get cell.');
|
|
|
|
CheckEquals(uffBold in MyCell^.UsedFormattingFields, true,
|
|
|
|
'Test saved bold attribute, cell '+CellNotation(MyWorksheet,row,col));
|
|
|
|
|
|
|
|
MyWorkbook.Free;
|
|
|
|
DeleteFile(TempFile);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadWriteReadFontTests.TestWriteReadBoldBIFF2;
|
|
|
|
begin
|
|
|
|
TestWriteReadBold(sfExcel2);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadWriteReadFontTests.TestWriteReadBoldBIFF8;
|
|
|
|
begin
|
|
|
|
TestWriteReadBold(sfExcel8);
|
|
|
|
end;
|
|
|
|
|
2014-04-24 22:31:01 +00:00
|
|
|
procedure TSpreadWriteReadFontTests.TestWriteReadFont(AFormat: TsSpreadsheetFormat;
|
|
|
|
AFontName: String);
|
2014-04-23 22:29:32 +00:00
|
|
|
var
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
row, col: Integer;
|
|
|
|
MyCell: PCell;
|
|
|
|
TempFile: string; //write xls/xml to this file and read back from it
|
|
|
|
cellText: String;
|
|
|
|
font: TsFont;
|
|
|
|
currValue: String;
|
|
|
|
expectedValue: String;
|
2014-04-25 09:02:10 +00:00
|
|
|
counter: Integer;
|
2014-04-23 22:29:32 +00:00
|
|
|
begin
|
|
|
|
TempFile:=GetTempFileName;
|
|
|
|
{// Not needed: use workbook.writetofile with overwrite=true
|
|
|
|
if fileexists(TempFile) then
|
|
|
|
DeleteFile(TempFile);
|
|
|
|
}
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
|
|
|
MyWorkSheet:= MyWorkBook.AddWorksheet(FontSheet);
|
|
|
|
|
|
|
|
// Write out all font styles at various sizes
|
|
|
|
for row := 0 to High(SollSizes) do begin
|
|
|
|
for col := 0 to High(SollStyles) do begin
|
|
|
|
cellText := Format('%s, %.1f-pt', [AFontName, SollSizes[row]]);
|
|
|
|
MyWorksheet.WriteUTF8Text(row, col, celltext);
|
|
|
|
MyWorksheet.WriteFont(row, col, AFontName, SollSizes[row], SollStyles[col], scBlack);
|
|
|
|
|
|
|
|
MyCell := MyWorksheet.FindCell(row, col);
|
|
|
|
if MyCell = nil then
|
|
|
|
fail('Error in test code. Failed to get cell.');
|
|
|
|
font := MyWorkbook.GetFont(MyCell^.FontIndex);
|
|
|
|
CheckEquals(SollSizes[row], font.Size,
|
|
|
|
'Test unsaved font size, cell ' + CellNotation(MyWorksheet,0,0));
|
|
|
|
currValue := GetEnumName(TypeInfo(TsFontStyles), byte(font.Style));
|
|
|
|
expectedValue := GetEnumName(TypeInfo(TsFontStyles), byte(SollStyles[col]));
|
|
|
|
CheckEquals(currValue, expectedValue,
|
|
|
|
'Test unsaved font style, cell ' + CellNotation(MyWorksheet,0,0));
|
|
|
|
end;
|
|
|
|
end;
|
2014-04-24 22:31:01 +00:00
|
|
|
MyWorkBook.WriteToFile(TempFile, AFormat, true);
|
2014-04-23 22:29:32 +00:00
|
|
|
MyWorkbook.Free;
|
|
|
|
|
|
|
|
// Open the spreadsheet, as biff8
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
2014-04-24 22:31:01 +00:00
|
|
|
MyWorkbook.ReadFromFile(TempFile, AFormat);
|
|
|
|
if AFormat = sfExcel2 then
|
|
|
|
MyWorksheet := MyWorkbook.GetFirstWorksheet // only 1 sheet for BIFF2
|
|
|
|
else
|
|
|
|
MyWorksheet := GetWorksheetByName(MyWorkBook, FontSheet);
|
2014-04-23 22:29:32 +00:00
|
|
|
if MyWorksheet=nil then
|
|
|
|
fail('Error in test code. Failed to get named worksheet');
|
2014-04-25 09:02:10 +00:00
|
|
|
counter := 0;
|
2014-04-23 22:29:32 +00:00
|
|
|
for row := 0 to MyWorksheet.GetLastRowNumber do
|
|
|
|
for col := 0 to MyWorksheet.GetLastColNumber do begin
|
2014-04-25 09:02:10 +00:00
|
|
|
if (AFormat = sfExcel2) and (counter = 4) then
|
|
|
|
break; // Excel 2 allows only 4 fonts
|
2014-04-23 22:29:32 +00:00
|
|
|
MyCell := MyWorksheet.FindCell(row, col);
|
|
|
|
if MyCell = nil then
|
|
|
|
fail('Error in test code. Failed to get cell.');
|
|
|
|
font := MyWorkbook.GetFont(MyCell^.FontIndex);
|
2014-04-24 22:31:01 +00:00
|
|
|
if abs(SollSizes[row] - font.Size) > 1e-6 then // safe-guard against rounding errors
|
|
|
|
CheckEquals(SollSizes[row], font.Size,
|
|
|
|
'Test saved font size, cell '+CellNotation(MyWorksheet,Row,Col));
|
2014-04-23 22:29:32 +00:00
|
|
|
currValue := GetEnumName(TypeInfo(TsFontStyles), byte(font.Style));
|
|
|
|
expectedValue := GetEnumName(TypeInfo(TsFontStyles), byte(SollStyles[col]));
|
|
|
|
CheckEquals(currValue, expectedValue,
|
|
|
|
'Test unsaved font style, cell ' + CellNotation(MyWorksheet,0,0));
|
2014-04-25 09:02:10 +00:00
|
|
|
inc(counter);
|
2014-04-23 22:29:32 +00:00
|
|
|
end;
|
|
|
|
MyWorkbook.Free;
|
|
|
|
|
|
|
|
DeleteFile(TempFile);
|
|
|
|
end;
|
2014-04-25 09:02:10 +00:00
|
|
|
|
2014-04-24 22:31:01 +00:00
|
|
|
procedure TSpreadWriteReadFontTests.TestWriteReadFontBIFF2_Arial;
|
|
|
|
begin
|
|
|
|
TestWriteReadFont(sfExcel2, 'Arial');
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadWriteReadFontTests.TestWriteReadFontBIFF2_TimesNewRoman;
|
|
|
|
begin
|
|
|
|
TestWriteReadFont(sfExcel2, 'TimesNewRoman');
|
|
|
|
end;
|
2014-04-23 22:29:32 +00:00
|
|
|
|
2014-04-24 22:31:01 +00:00
|
|
|
procedure TSpreadWriteReadFontTests.TestWriteReadFontBIFF2_CourierNew;
|
2014-04-23 22:29:32 +00:00
|
|
|
begin
|
2014-04-24 22:31:01 +00:00
|
|
|
TestWriteReadFont(sfExcel2, 'CourierNew');
|
2014-04-23 22:29:32 +00:00
|
|
|
end;
|
|
|
|
|
2014-04-24 22:31:01 +00:00
|
|
|
procedure TSpreadWriteReadFontTests.TestWriteReadFontBIFF8_Arial;
|
2014-04-23 22:29:32 +00:00
|
|
|
begin
|
2014-04-24 22:31:01 +00:00
|
|
|
TestWriteReadFont(sfExcel8, 'Arial');
|
2014-04-23 22:29:32 +00:00
|
|
|
end;
|
|
|
|
|
2014-04-24 22:31:01 +00:00
|
|
|
procedure TSpreadWriteReadFontTests.TestWriteReadFontBIFF8_TimesNewRoman;
|
2014-04-23 22:29:32 +00:00
|
|
|
begin
|
2014-04-24 22:31:01 +00:00
|
|
|
TestWriteReadFont(sfExcel8, 'TimesNewRoman');
|
2014-04-23 22:29:32 +00:00
|
|
|
end;
|
|
|
|
|
2014-04-24 22:31:01 +00:00
|
|
|
procedure TSpreadWriteReadFontTests.TestWriteReadFontBIFF8_CourierNew;
|
|
|
|
begin
|
|
|
|
TestWriteReadFont(sfExcel8, 'CourierNew');
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2014-04-23 22:29:32 +00:00
|
|
|
initialization
|
|
|
|
RegisterTest(TSpreadWriteReadFontTests);
|
|
|
|
|
|
|
|
end.
|
|
|
|
|