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