Debian Codegraph @

tests/code/testcases/testcastledownload.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestDownload" -*-
2 {
3 Copyright 2020-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Test CastleDownload unit. }
18 unit TestCastleDownload;
19
20 interface
21
22 uses
23 Classes, SysUtils, CastleTester;
24
25 type
26 TTestDownload = class(TCastleTestCase)
27 published
28 procedure TestLocalChars;
29 procedure TestTextReader;
30 procedure TestRegisteredProtocolNotCaseSensitive;
31 end;
32
33 implementation
34
35 uses CastleDownload, CastleClassUtils, CastleVectors, CastleStringUtils,
36 CastleFonts, CastleFilesUtils;
37
38 procedure TTestDownload.TestLocalChars;
39
40 { Test reading file using URL (through CGE function). }
41 procedure TestReading(const URL: String);
42 var
43 Stream: TStream;
44 S: String;
45 begin
46 Stream := Download(URL);
47 try
48 S := StreamToString(Stream);
49 AssertEquals('Testing.', Trim(S));
50 finally FreeAndNil(Stream) end;
51 end;
52
53 { Test reading file, whose URL is written inside another file, in UTF-8 encoding. }
54 procedure TestReadingThroughReference(const URL: String);
55 var
56 Stream: TStream;
57 ReferredURL: String;
58 begin
59 Stream := Download(URL);
60 try
61 ReferredURL := Trim(StreamToString(Stream));
62 TestReading(ReferredURL);
63 finally FreeAndNil(Stream) end;
64 end;
65
66 { Test reading font (as it goes through FreeType library). }
67 procedure TestReadingFont(const FontUrl: String);
68 var
69 MyNewFont: TCastleFont;
70 begin
71 MyNewFont := TCastleFont.Create(nil);
72 try
73 MyNewFont.Size := 20;
74 MyNewFont.AntiAliased := true;
75 MyNewFont.Url := FontUrl;
76 finally FreeAndNil(MyNewFont) end;
77 end;
78
79 begin
80 TestReading('castle-data:/local_chars/ascii_name.txt');
81 TestReading('castle-data:/local_chars/name with Polish chars ćma źrebak żmija wąż królik.txt');
82 TestReading('castle-data:/local_chars/name with Chinese chars 样例中文文本.txt');
83 TestReading('castle-data:/local_chars/样例中文文本/name with Chinese chars 样例中文文本.txt');
84 TestReading('castle-data:/local_chars/name with Russian chars образец русского текста.txt');
85 TestReading('castle-data:/local_chars/образец русского текста/name with Russian chars образец русского текста.txt');
86
87 TestReadingThroughReference('castle-data:/local_chars/reference to file with Chinese chars.txt');
88 TestReadingThroughReference('castle-data:/local_chars/reference to file with Russian chars.txt');
89 TestReadingThroughReference('castle-data:/local_chars/reference to file with Polish chars.txt');
90
91 { This would fail in Docker now, where we cannot create /.config/... }
92 {.$define TEST_CONFIG}
93 {$ifdef TEST_CONFIG}
94 StringToFile(ApplicationConfig('config_ascii.txt'), 'Testing save.');
95 StringToFile(ApplicationConfig('config with Chinese chars 样例中文文本.txt'), 'Testing save.');
96 StringToFile(ApplicationConfig('config with Polish chars ćma źrebak żmija wąż królik.txt'), 'Testing save.');
97 StringToFile(ApplicationConfig('config with Russian chars образец русского текста.txt'), 'Testing save.');
98 {$endif}
99
100 TestReadingFont('castle-data:/local_chars/DejaVuSans name with Russian chars образец русского текста.ttf');
101 end;
102
103 procedure TTestDownload.TestTextReader;
104
105 { Testcase based on example from
106 https://forum.castle-engine.io/t/setup-files-and-working-with-them/630/4
107 }
108
109 var
110 T: TTextReader;
111 X, Y, Z: Single;
112 V: TVector3;
113 begin
114 { using ReadSingle }
115 T := TTextReader.Create('castle-data:/test_text_reader.txt');
116 try
117 X := T.ReadSingle;
118 Y := T.ReadSingle;
119 Z := T.ReadSingle;
120 AssertSameValue(1, X);
121 AssertSameValue(2, Y);
122 AssertSameValue(3, Z);
123
124 X := T.ReadSingle;
125 Y := T.ReadSingle;
126 Z := T.ReadSingle;
127 AssertSameValue(4, X);
128 AssertSameValue(5, Y);
129 AssertSameValue(6, Z);
130
131 X := T.ReadSingle;
132 Y := T.ReadSingle;
133 Z := T.ReadSingle;
134 AssertSameValue(7, X);
135 AssertSameValue(8, Y);
136 AssertSameValue(9, Z);
137 finally FreeAndNil(T) end;
138
139 { alternative version using ReadVector3 }
140 T := TTextReader.Create('castle-data:/test_text_reader.txt');
141 try
142 V := T.ReadVector3;
143 AssertVectorEquals(Vector3(1, 2, 3), V);
144 V := T.ReadVector3;
145 AssertVectorEquals(Vector3(4, 5, 6), V);
146 V := T.ReadVector3;
147 AssertVectorEquals(Vector3(7, 8, 9), V);
148 finally FreeAndNil(T) end;
149
150 { alternative version using Readln + Vector3FromStr }
151 T := TTextReader.Create('castle-data:/test_text_reader.txt');
152 try
153 V := Vector3FromStr(T.Readln);
154 AssertVectorEquals(Vector3(1, 2, 3), V);
155 V := Vector3FromStr(T.Readln);
156 AssertVectorEquals(Vector3(4, 5, 6), V);
157 V := Vector3FromStr(T.Readln);
158 AssertVectorEquals(Vector3(7, 8, 9), V);
159 finally FreeAndNil(T) end;
160
161 { alternative version using Readln + DeFormat }
162 T := TTextReader.Create('castle-data:/test_text_reader.txt');
163 try
164 DeFormat(T.Readln, '%.single. %.single. %.single.', [@X, @Y, @Z]);
165 AssertSameValue(1, X);
166 AssertSameValue(2, Y);
167 AssertSameValue(3, Z);
168
169 DeFormat(T.Readln, '%.single. %.single. %.single.', [@X, @Y, @Z]);
170 AssertSameValue(4, X);
171 AssertSameValue(5, Y);
172 AssertSameValue(6, Z);
173
174 DeFormat(T.Readln, '%.single. %.single. %.single.', [@X, @Y, @Z]);
175 AssertSameValue(7, X);
176 AssertSameValue(8, Y);
177 AssertSameValue(9, Z);
178 finally FreeAndNil(T) end;
179 end;
180
181 procedure TTestDownload.TestRegisteredProtocolNotCaseSensitive;
182 begin
183 AssertFalse(RegisteredUrlProtocol('my-test-proto'));
184 AssertFalse(RegisteredUrlProtocol('MY-test-PROTO'));
185 AssertFalse(RegisteredUrlProtocol('my-Test-proto'));
186
187 RegisterUrlProtocol('my-Test-proto', nil, nil);
188
189 AssertTrue(RegisteredUrlProtocol('my-test-proto'));
190 AssertTrue(RegisteredUrlProtocol('MY-test-PROTO'));
191 AssertTrue(RegisteredUrlProtocol('my-Test-proto'));
192
193 UnregisterUrlProtocol('My-Test-protO'); // different case when registered, no problem
194
195 AssertFalse(RegisteredUrlProtocol('my-test-proto'));
196 AssertFalse(RegisteredUrlProtocol('MY-test-PROTO'));
197 AssertFalse(RegisteredUrlProtocol('my-Test-proto'));
198 end;
199
200 initialization
201 RegisterTest(TTestDownload);
202 end.