Migrating to new test framework
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Font.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <iostream>
18
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <dali/dali.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 void utc_dali_font_startup(void)
27 {
28   test_return_value = TET_UNDEF;
29 }
30
31 void utc_dali_font_cleanup(void)
32 {
33   test_return_value = TET_PASS;
34 }
35
36 namespace
37 {
38
39 const std::string FAMILY_NAME = "Arial";
40 const std::string STYLE = "Bold";
41 const unsigned int PIXEL_SIZE = 20;
42 const unsigned int POINT_SIZE = 11.f;
43
44 static Font CreateFont( PointSize size )
45 {
46   // Don't use a font which could be cached otherwise cached values will be used making measure text test to fail.
47   return Font::New(FontParameters("TET-FreeSans", "Book", size));
48 }
49
50 static Font CreateFont( PixelSize size )
51 {
52   // Don't use a font which could be cached otherwise cached values will be used making measure text test to fail.
53   return Font::New(FontParameters("TET-FreeSans", "Book", size));
54 }
55
56 static Font CreateFont( CapsHeight size )
57 {
58   // Don't use a font which could be cached otherwise cached values will be used making measure text test to fail.
59   return Font::New(FontParameters("TET-FreeSans", "Book", size));
60 }
61
62 } //anon namespace
63
64 int UtcDaliFontNew01(void)
65 {
66   TestApplication application;
67
68   tet_infoline("Testing Dali::Font::New() - with specific font family and  pixel size");
69
70   Font font;      // invoke default constructor (creates an empty handle)
71   font = CreateFont(PixelSize(25.0f));  // This does not call platform abstraction until some text is displayed or measured
72
73   DALI_TEST_CHECK(font);
74
75   tet_infoline("Testing Dali::Font::New() - with default font name and pixel size");
76
77   Font font2 = Font::New(FontParameters("", "", PixelSize(0.0f)));
78
79   DALI_TEST_CHECK(font2);
80
81   Font* ptrFont = new Font;
82   *ptrFont = Font::New(FontParameters("", "", PixelSize(0.0f)));
83   delete ptrFont;
84   END_TEST;
85 }
86
87 int UtcDaliFontNew02(void)
88 {
89   TestApplication application;
90
91   tet_infoline("Testing Dali::Font::New() - with specific font family and point size");
92
93   Font font = CreateFont(PointSize(8));
94
95   DALI_TEST_CHECK(font);
96
97   tet_infoline("Testing Dali::Font::New() - with default font family and point size");
98
99   Font font2;
100   font2 = Font::New(FontParameters("", "", PointSize(0)));
101
102   DALI_TEST_CHECK(font2);
103   END_TEST;
104 }
105
106 int UtcDaliFontNew03(void)
107 {
108   TestApplication application;
109
110   tet_infoline("Testing Dali::Font::New() - with specific font family and caps-height");
111
112   Font font = CreateFont(CapsHeight(8));
113
114   DALI_TEST_CHECK(font);
115   DALI_TEST_CHECK(font.GetPixelSize() > 8.0f);    // Pixel size should be bigger than requested CapsHeight
116
117   tet_infoline("Testing Dali::Font::New() - with default font family and point size");
118
119   Font font2 = Font::New(FontParameters("", "", CapsHeight(0)));
120
121   DALI_TEST_CHECK(font2);
122   END_TEST;
123 }
124
125 int UtcDaliFontNew04(void)
126 {
127   TestApplication application;
128
129   tet_infoline("Testing Dali::Font::New() - with wrong font family or font style");
130
131   Font font = Font::New(FontParameters("gfagag", "fgafgafga",PointSize(0)));
132
133   DALI_TEST_CHECK(font);
134   END_TEST;
135 }
136
137 int UtcDaliFontNew05(void)
138 {
139   TestApplication application;
140
141   tet_infoline("Testing Dali::Font::New() - with pixel size and weight");
142
143   PixelSize pixelSize(PIXEL_SIZE);
144   FontParameters fontParams(FAMILY_NAME, "", pixelSize);
145   Font font = Font::New( fontParams );
146
147   DALI_TEST_CHECK( font );
148   DALI_TEST_CHECK( font.GetName() == FAMILY_NAME );
149   DALI_TEST_CHECK( font.GetStyle().empty() );
150   END_TEST;
151 }
152
153 int UtcDaliFontNew06(void)
154 {
155   TestApplication application;
156
157   tet_infoline("Testing Dali::Font::New() - with caps height");
158
159   CapsHeight capsHeight(10.f);
160   FontParameters fontParams(FAMILY_NAME, "", capsHeight);
161   Font font = Font::New( fontParams );
162
163   DALI_TEST_CHECK( font );
164   DALI_TEST_CHECK( font.GetName() == FAMILY_NAME );
165   DALI_TEST_CHECK( font.GetStyle().empty() );
166   END_TEST;
167 }
168
169
170 int UtcDaliFontDownCast(void)
171 {
172   TestApplication application;
173   tet_infoline("Testing Dali::Font::DownCast()");
174
175   Font font = CreateFont(PixelSize(25.0f));
176
177   BaseHandle object(font);
178
179   Font font2 = Font::DownCast(object);
180   DALI_TEST_CHECK(font2);
181
182   Font font3 = DownCast< Font >(object);
183   DALI_TEST_CHECK(font3);
184
185   BaseHandle unInitializedObject;
186   Font font4 = Font::DownCast(unInitializedObject);
187   DALI_TEST_CHECK(!font4);
188
189   Font font5 = DownCast< Font >(unInitializedObject);
190   DALI_TEST_CHECK(!font5);
191   END_TEST;
192 }
193
194 int UtcDaliFontGetPixelSize(void)
195 {
196   TestApplication application;
197
198   tet_infoline("Testing Dali::Font::GetPixelSize()");
199
200   Font font = CreateFont(PixelSize(32));
201
202   DALI_TEST_CHECK(32 == font.GetPixelSize());
203   END_TEST;
204 }
205
206 int UtcDaliFontGetPointSize(void)
207 {
208   TestApplication application;
209
210   tet_infoline("Testing Dali::Font::GetPointSize)");
211
212   Font font = CreateFont(PointSize(8.0f));
213
214   DALI_TEST_EQUALS(8.f, font.GetPointSize(), Math::MACHINE_EPSILON_1000, TEST_LOCATION);
215   END_TEST;
216 }
217
218 int UtcDaliFontPointsToPixels(void)
219 {
220   TestApplication application;
221
222   tet_infoline("Testing Dali::Font::PointsToPixels)");
223
224   unsigned int points= Font::PointsToPixels( 12.0f );
225
226   DALI_TEST_CHECK(  points == 36  );
227   END_TEST;
228 }
229
230 int UtcFontMeasureTextWidth(void)
231 {
232   TestApplication application;
233
234   tet_infoline("Testing Dali::Font::MeasureTextWidth()");
235
236   TraceCallStack& trace = application.GetPlatform().GetTrace();
237   trace.Enable(true);
238
239   Font font = CreateFont(PointSize(8));
240
241   float width = font.MeasureTextWidth("test me", 24.0f);
242
243   // No cache
244
245   DALI_TEST_CHECK(trace.FindMethod("ReadMetricsFromCacheFile"));
246   DALI_TEST_CHECK(trace.FindMethod("WriteMetricsToCacheFile"));
247   DALI_TEST_CHECK(trace.FindMethod("GetGlyphData"));
248   trace.Reset();
249
250   DALI_TEST_EQUALS(width, 168.0f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
251   width = font.MeasureTextWidth(Text(std::string("test me")), 24.0f);
252
253   // Should now be cached in memory
254   DALI_TEST_CHECK( ! trace.FindMethod("ReadMetricsFromCacheFile"));
255   DALI_TEST_CHECK( ! trace.FindMethod("WriteMetricsToCacheFile"));
256   DALI_TEST_CHECK( ! trace.FindMethod("GetGlyphData"));
257
258   DALI_TEST_EQUALS(width, 168.0f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
259
260   Text text = Text(std::string("t"))[0];
261   Character c = text[0];
262   width = font.MeasureTextWidth(c, 24.0f);
263   DALI_TEST_EQUALS(width, 24.0f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
264
265   END_TEST;
266 }
267
268 int UtcFontMeasureTextHeight(void)
269 {
270   TestApplication application;
271
272   tet_infoline("Testing Dali::Font::MeasureTextHeight()");
273
274   TraceCallStack& trace = application.GetPlatform().GetTrace();
275   trace.Enable(true);
276
277   Font font = CreateFont(PointSize(8));
278
279   float height = font.MeasureTextHeight("test me", 48.0f);
280
281   DALI_TEST_CHECK(trace.FindMethod("ReadMetricsFromCacheFile"));
282   DALI_TEST_CHECK(trace.FindMethod("WriteMetricsToCacheFile"));
283   DALI_TEST_CHECK(trace.FindMethod("GetGlyphData"));
284   trace.Reset();
285
286   DALI_TEST_EQUALS(height, 6.8571f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
287
288   height = font.MeasureTextHeight(Text(std::string("test me")), 48.0f);
289
290   DALI_TEST_CHECK( ! trace.FindMethod("ReadMetricsFromCacheFile"));
291   DALI_TEST_CHECK( ! trace.FindMethod("WriteMetricsToCacheFile"));
292   DALI_TEST_CHECK( ! trace.FindMethod("GetGlyphData"));
293
294   DALI_TEST_EQUALS(height, 6.8571f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
295
296   Text text = Text(std::string("t"))[0];
297   Character c = text[0];
298   height = font.MeasureTextHeight(c, 24.0f);
299   DALI_TEST_EQUALS(height, 24.0f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
300
301   END_TEST;
302 }
303
304 int UtcFontMeasureText(void)
305 {
306   TestApplication application;
307
308   tet_infoline("Testing Dali::Font::MeasureText()");
309
310   TraceCallStack& trace = application.GetPlatform().GetTrace();
311   trace.Enable(true);
312
313   Font font = CreateFont(PointSize(8));
314
315   Vector3 size = font.MeasureText("test me");
316
317   DALI_TEST_CHECK(trace.FindMethod("ReadMetricsFromCacheFile"));
318   DALI_TEST_CHECK(trace.FindMethod("WriteMetricsToCacheFile"));
319   DALI_TEST_CHECK(trace.FindMethod("GetGlyphData"));
320   trace.Reset();
321
322   DALI_TEST_EQUALS(size.width, 53.1076f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
323   DALI_TEST_EQUALS(size.height, 7.5868f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
324
325   size = font.MeasureText(Text(std::string("test me")));
326
327   DALI_TEST_CHECK( ! trace.FindMethod("ReadMetricsFromCacheFile"));
328   DALI_TEST_CHECK( ! trace.FindMethod("WriteMetricsToCacheFile"));
329   DALI_TEST_CHECK( ! trace.FindMethod("GetGlyphData"));
330
331   DALI_TEST_EQUALS(size.width, 53.1076f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
332   DALI_TEST_EQUALS(size.height, 7.5868f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
333
334
335   Text text = Text(std::string("t"))[0];
336   Character c = text[0];
337   size = font.MeasureText(c);
338
339   // character size is square
340   DALI_TEST_EQUALS(size.width, 7.5868f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
341   DALI_TEST_EQUALS(size.height, 7.5868f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
342
343   END_TEST;
344 }
345
346 int UtcFontGetFamilyForText(void)
347 {
348   TestApplication application;
349
350   tet_infoline("Testing Dali::Font::GetFamilyForText()");
351
352   std::string latinFont = Font::GetFamilyForText("Hello world");
353   DALI_TEST_CHECK( latinFont.size() != 0 );
354
355   latinFont = Font::GetFamilyForText( Text(std::string("Hello world")) );
356   DALI_TEST_CHECK( latinFont.size() != 0 );
357
358   std::string asianFont = Font::GetFamilyForText("繁體中文");
359   DALI_TEST_CHECK( asianFont.size() != 0 );
360
361   asianFont = Font::GetFamilyForText(Text(std::string("繁體中文")));
362   DALI_TEST_CHECK( asianFont.size() != 0 );
363
364   Text text = Text(std::string("繁體中文"))[0];
365   Character c = text[0];
366
367   asianFont = Font::GetFamilyForText(c );
368   DALI_TEST_CHECK( asianFont.size() != 0 );
369
370   END_TEST;
371 }
372
373 int UtcFontGetFontLineHeightFromCapsHeight(void)
374 {
375   TestApplication application;
376
377   tet_infoline("Testing Dali::Font::GetLineHeightFromCapsHeight()");
378
379   CapsHeight capsHeight(10);
380   PixelSize pixelSize = Font::GetLineHeightFromCapsHeight("", "", capsHeight);
381   DALI_TEST_CHECK( capsHeight < pixelSize );
382
383   pixelSize = Font::GetLineHeightFromCapsHeight(Font::GetFamilyForText("Hello world"), "", capsHeight);
384   DALI_TEST_CHECK( capsHeight < pixelSize );
385
386   pixelSize = Font::GetLineHeightFromCapsHeight(Font::GetFamilyForText(Text(std::string("Hello world"))), "", capsHeight);
387   DALI_TEST_CHECK( capsHeight < pixelSize );
388   END_TEST;
389 }
390
391 int UtcFontAllGlyphsSupported(void)
392 {
393   TestApplication application;
394
395   tet_infoline("Testing Dali::Font::AllGlyphsSupported()");
396
397   Font font = Font::New();
398
399   font.AllGlyphsSupported("Hello World\n");
400
401   DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
402   application.GetPlatform().ResetTrace();
403
404   font.AllGlyphsSupported(Text(std::string("Hello World\n")));
405
406   DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
407
408   application.GetPlatform().ResetTrace();
409
410   Text text = Text(std::string("t"))[0];
411   Character c = text[0];
412   font.AllGlyphsSupported(c);
413
414   DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
415   END_TEST;
416 }
417
418 int UtcFontGetMetrics(void)
419 {
420   TestApplication application;
421
422   tet_infoline("Testing Dali::Font::UtcFontGetMetrics()");
423
424   Font font = Font::New();
425   font.MeasureText(Text(std::string("Hello World"))); // Builds fake metrics in TestPlatformAbstraction.
426
427   float lineHeight = font.GetLineHeight();
428   float ascender = font.GetAscender();
429   float underlineThickness = font.GetUnderlineThickness();
430   float underlinePosition = font.GetUnderlinePosition();
431   Font::Metrics metrics = font.GetMetrics( Text("H")[0] );
432
433   // TODO VCC This TET case fails if there are some metrics cached.
434
435   DALI_TEST_EQUALS( lineHeight, 11.380209f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
436   DALI_TEST_EQUALS( ascender, 10.242188f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
437   DALI_TEST_EQUALS( underlineThickness, 2.276042f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
438   DALI_TEST_EQUALS( underlinePosition, 9.104167f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
439   DALI_TEST_EQUALS( metrics.GetAdvance(), 11.380209f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
440   DALI_TEST_EQUALS( metrics.GetBearing(), 10.242188f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
441   DALI_TEST_EQUALS( metrics.GetWidth(), 11.380209f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
442   END_TEST;
443 }
444
445 int UtcFontIsDefault(void)
446 {
447   TestApplication application;
448
449   tet_infoline("Testing Dali::Font::UtcFontIsDefault()");
450
451   FontParameters fontParams("FreeSans", "", PointSize(10.f));
452   Font font1 = Font::New(fontParams);
453
454   DALI_TEST_CHECK( !font1.IsDefaultSystemFont() );
455   DALI_TEST_CHECK( !font1.IsDefaultSystemSize() );
456
457   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateFontFamilyNameFunc ) );
458   application.GetPlatform().ResetTrace();
459
460   Font font2 = Font::New();
461
462   DALI_TEST_CHECK( !font2.IsDefaultSystemFont() );
463   DALI_TEST_CHECK( font2.IsDefaultSystemSize() );
464
465   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateFontFamilyNameFunc ) );
466   END_TEST;
467 }
468
469
470 int UtcFontGetInstalledFonts(void)
471 {
472   TestApplication application;
473
474   tet_infoline("Testing Dali::Font::GetInstalledFonts()");
475
476   // the default should only get installed and downloaded fonts
477   std::vector<std::string> fontList;
478
479   fontList = Font::GetInstalledFonts(Font::LIST_SYSTEM_FONTS);
480   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateGetFontListFunc ) );
481   DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_SYSTEM_FONTS );
482
483   fontList = Font::GetInstalledFonts(Font::LIST_ALL_FONTS);
484   DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_ALL_FONTS );
485
486   fontList = Font::GetInstalledFonts(Font::LIST_APPLICATION_FONTS);
487   DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_APPLICATION_FONTS );
488
489   END_TEST;
490 }
491
492 int UtcFontMetricsDefaultConstructor(void)
493 {
494   TestApplication application;
495
496   tet_infoline("Testing UtcFontMetricsDefaultConstructor");
497
498   Font::Metrics metrics;
499
500   DALI_TEST_EQUALS( metrics.GetAdvance() , 0.f , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
501   DALI_TEST_EQUALS( metrics.GetBearing() , 0.f , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
502   DALI_TEST_EQUALS( metrics.GetWidth() , 0.f , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
503   DALI_TEST_EQUALS( metrics.GetHeight() , 0.f , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
504
505   END_TEST;
506 }
507
508 int UtcFontMetricsCopyConstructor(void)
509 {
510   TestApplication application;
511
512   tet_infoline("Testing UtcFontMetricsCopyConstructor");
513
514   Font font = Font::New();
515   font.MeasureText(Text(std::string("Hello World"))); // Builds fake metrics in TestPlatformAbstraction.
516   Font::Metrics metrics = font.GetMetrics( Text("H")[0] );
517   Font::Metrics metrics2( metrics );
518
519   DALI_TEST_EQUALS( metrics.GetAdvance() , metrics2.GetAdvance() , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
520   DALI_TEST_EQUALS( metrics.GetBearing() , metrics2.GetBearing(), Math::MACHINE_EPSILON_10000, TEST_LOCATION );
521   DALI_TEST_EQUALS( metrics.GetWidth() , metrics2.GetWidth() , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
522   DALI_TEST_EQUALS( metrics.GetHeight() , metrics2.GetHeight(), Math::MACHINE_EPSILON_10000, TEST_LOCATION );
523
524   END_TEST;
525 }
526
527 int UtcFontMetricsAssignmentOperator(void)
528 {
529   TestApplication application;
530
531   tet_infoline("Testing UtcFontMetricsAssignmentOperator");
532
533   Font font = Font::New();
534   font.MeasureText(Text(std::string("Hello World"))); // Builds fake metrics in TestPlatformAbstraction.
535   Font::Metrics metrics = font.GetMetrics( Text("H")[0] );
536   Font::Metrics metrics2;
537
538   metrics2 = metrics;
539
540   DALI_TEST_EQUALS( metrics.GetAdvance() , metrics2.GetAdvance() , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
541   DALI_TEST_EQUALS( metrics.GetBearing() , metrics2.GetBearing(), Math::MACHINE_EPSILON_10000, TEST_LOCATION );
542   DALI_TEST_EQUALS( metrics.GetWidth() , metrics2.GetWidth() , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
543   DALI_TEST_EQUALS( metrics.GetHeight() , metrics2.GetHeight(), Math::MACHINE_EPSILON_10000, TEST_LOCATION );
544
545   END_TEST;
546 }