Merge branch 'tizen' into new_text
[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/public-api/dali-core.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   Character c = Text(std::string("t"))[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   Character c = Text(std::string("t"))[0];
411   font.AllGlyphsSupported(c);
412
413   DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
414   END_TEST;
415 }
416
417 int UtcFontGetMetrics(void)
418 {
419   TestApplication application;
420
421   tet_infoline("Testing Dali::Font::UtcFontGetMetrics()");
422
423   Font font = Font::New();
424   font.MeasureText(Text(std::string("Hello World"))); // Builds fake metrics in TestPlatformAbstraction.
425
426   float lineHeight = font.GetLineHeight();
427   float ascender = font.GetAscender();
428   float underlineThickness = font.GetUnderlineThickness();
429   float underlinePosition = font.GetUnderlinePosition();
430   Font::Metrics metrics = font.GetMetrics( Text("H")[0] );
431
432   // TODO VCC This TET case fails if there are some metrics cached.
433
434   DALI_TEST_EQUALS( lineHeight, 11.380209f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
435   DALI_TEST_EQUALS( ascender, 10.242188f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
436   DALI_TEST_EQUALS( underlineThickness, 2.276042f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
437   DALI_TEST_EQUALS( underlinePosition, 9.104167f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
438   DALI_TEST_EQUALS( metrics.GetAdvance(), 11.380209f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
439   DALI_TEST_EQUALS( metrics.GetBearing(), 10.242188f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
440   DALI_TEST_EQUALS( metrics.GetWidth(), 11.380209f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
441   END_TEST;
442 }
443
444 int UtcFontIsDefault(void)
445 {
446   TestApplication application;
447
448   tet_infoline("Testing Dali::Font::UtcFontIsDefault()");
449
450   FontParameters fontParams("FreeSans", "", PointSize(10.f));
451   Font font1 = Font::New(fontParams);
452
453   DALI_TEST_CHECK( !font1.IsDefaultSystemFont() );
454   DALI_TEST_CHECK( !font1.IsDefaultSystemSize() );
455
456   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateFontFamilyNameFunc ) );
457   application.GetPlatform().ResetTrace();
458
459   Font font2 = Font::New();
460
461   DALI_TEST_CHECK( !font2.IsDefaultSystemFont() );
462   DALI_TEST_CHECK( font2.IsDefaultSystemSize() );
463
464   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateFontFamilyNameFunc ) );
465   END_TEST;
466 }
467
468
469 int UtcFontGetInstalledFonts(void)
470 {
471   TestApplication application;
472
473   tet_infoline("Testing Dali::Font::GetInstalledFonts()");
474
475   // the default should only get installed and downloaded fonts
476   std::vector<std::string> fontList;
477
478   fontList = Font::GetInstalledFonts(Font::LIST_SYSTEM_FONTS);
479   DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateGetFontListFunc ) );
480   DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_SYSTEM_FONTS );
481
482   fontList = Font::GetInstalledFonts(Font::LIST_ALL_FONTS);
483   DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_ALL_FONTS );
484
485   fontList = Font::GetInstalledFonts(Font::LIST_APPLICATION_FONTS);
486   DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_APPLICATION_FONTS );
487
488   END_TEST;
489 }
490
491 int UtcFontMetricsDefaultConstructor(void)
492 {
493   TestApplication application;
494
495   tet_infoline("Testing UtcFontMetricsDefaultConstructor");
496
497   Font::Metrics metrics;
498
499   DALI_TEST_EQUALS( metrics.GetAdvance() , 0.f , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
500   DALI_TEST_EQUALS( metrics.GetBearing() , 0.f , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
501   DALI_TEST_EQUALS( metrics.GetWidth() , 0.f , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
502   DALI_TEST_EQUALS( metrics.GetHeight() , 0.f , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
503
504   END_TEST;
505 }
506
507 int UtcFontMetricsCopyConstructor(void)
508 {
509   TestApplication application;
510
511   tet_infoline("Testing UtcFontMetricsCopyConstructor");
512
513   Font font = Font::New();
514   font.MeasureText(Text(std::string("Hello World"))); // Builds fake metrics in TestPlatformAbstraction.
515   Font::Metrics metrics = font.GetMetrics( Text("H")[0] );
516   Font::Metrics metrics2( metrics );
517
518   DALI_TEST_EQUALS( metrics.GetAdvance() , metrics2.GetAdvance() , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
519   DALI_TEST_EQUALS( metrics.GetBearing() , metrics2.GetBearing(), Math::MACHINE_EPSILON_10000, TEST_LOCATION );
520   DALI_TEST_EQUALS( metrics.GetWidth() , metrics2.GetWidth() , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
521   DALI_TEST_EQUALS( metrics.GetHeight() , metrics2.GetHeight(), Math::MACHINE_EPSILON_10000, TEST_LOCATION );
522
523   END_TEST;
524 }
525
526 int UtcFontMetricsAssignmentOperator(void)
527 {
528   TestApplication application;
529
530   tet_infoline("Testing UtcFontMetricsAssignmentOperator");
531
532   Font font = Font::New();
533   font.MeasureText(Text(std::string("Hello World"))); // Builds fake metrics in TestPlatformAbstraction.
534   Font::Metrics metrics = font.GetMetrics( Text("H")[0] );
535   Font::Metrics metrics2;
536
537   metrics2 = metrics;
538
539   DALI_TEST_EQUALS( metrics.GetAdvance() , metrics2.GetAdvance() , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
540   DALI_TEST_EQUALS( metrics.GetBearing() , metrics2.GetBearing(), Math::MACHINE_EPSILON_10000, TEST_LOCATION );
541   DALI_TEST_EQUALS( metrics.GetWidth() , metrics2.GetWidth() , Math::MACHINE_EPSILON_10000, TEST_LOCATION );
542   DALI_TEST_EQUALS( metrics.GetHeight() , metrics2.GetHeight(), Math::MACHINE_EPSILON_10000, TEST_LOCATION );
543
544   END_TEST;
545 }