2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/dali-core.h>
23 #include <dali-test-suite-utils.h>
27 void utc_dali_font_startup(void)
29 test_return_value = TET_UNDEF;
32 void utc_dali_font_cleanup(void)
34 test_return_value = TET_PASS;
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;
45 static Font CreateFont( PointSize size )
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));
51 static Font CreateFont( PixelSize size )
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));
57 static Font CreateFont( CapsHeight size )
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));
65 int UtcDaliFontNew01(void)
67 TestApplication application;
69 tet_infoline("Testing Dali::Font::New() - with specific font family and pixel size");
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
74 DALI_TEST_CHECK(font);
76 tet_infoline("Testing Dali::Font::New() - with default font name and pixel size");
78 Font font2 = Font::New(FontParameters("", "", PixelSize(0.0f)));
80 DALI_TEST_CHECK(font2);
82 Font* ptrFont = new Font;
83 *ptrFont = Font::New(FontParameters("", "", PixelSize(0.0f)));
88 int UtcDaliFontNew02(void)
90 TestApplication application;
92 tet_infoline("Testing Dali::Font::New() - with specific font family and point size");
94 Font font = CreateFont(PointSize(8));
96 DALI_TEST_CHECK(font);
98 tet_infoline("Testing Dali::Font::New() - with default font family and point size");
101 font2 = Font::New(FontParameters("", "", PointSize(0)));
103 DALI_TEST_CHECK(font2);
107 int UtcDaliFontNew03(void)
109 TestApplication application;
111 tet_infoline("Testing Dali::Font::New() - with specific font family and caps-height");
113 Font font = CreateFont(CapsHeight(8));
115 DALI_TEST_CHECK(font);
116 DALI_TEST_CHECK(font.GetPixelSize() > 8.0f); // Pixel size should be bigger than requested CapsHeight
118 tet_infoline("Testing Dali::Font::New() - with default font family and point size");
120 Font font2 = Font::New(FontParameters("", "", CapsHeight(0)));
122 DALI_TEST_CHECK(font2);
126 int UtcDaliFontNew04(void)
128 TestApplication application;
130 tet_infoline("Testing Dali::Font::New() - with wrong font family or font style");
132 Font font = Font::New(FontParameters("gfagag", "fgafgafga",PointSize(0)));
134 DALI_TEST_CHECK(font);
138 int UtcDaliFontNew05(void)
140 TestApplication application;
142 tet_infoline("Testing Dali::Font::New() - with pixel size and weight");
144 PixelSize pixelSize(PIXEL_SIZE);
145 FontParameters fontParams(FAMILY_NAME, "", pixelSize);
146 Font font = Font::New( fontParams );
148 DALI_TEST_CHECK( font );
149 DALI_TEST_CHECK( font.GetName() == FAMILY_NAME );
150 DALI_TEST_CHECK( font.GetStyle().empty() );
154 int UtcDaliFontNew06(void)
156 TestApplication application;
158 tet_infoline("Testing Dali::Font::New() - with caps height");
160 CapsHeight capsHeight(10.f);
161 FontParameters fontParams(FAMILY_NAME, "", capsHeight);
162 Font font = Font::New( fontParams );
164 DALI_TEST_CHECK( font );
165 DALI_TEST_CHECK( font.GetName() == FAMILY_NAME );
166 DALI_TEST_CHECK( font.GetStyle().empty() );
171 int UtcDaliFontDownCast(void)
173 TestApplication application;
174 tet_infoline("Testing Dali::Font::DownCast()");
176 Font font = CreateFont(PixelSize(25.0f));
178 BaseHandle object(font);
180 Font font2 = Font::DownCast(object);
181 DALI_TEST_CHECK(font2);
183 Font font3 = DownCast< Font >(object);
184 DALI_TEST_CHECK(font3);
186 BaseHandle unInitializedObject;
187 Font font4 = Font::DownCast(unInitializedObject);
188 DALI_TEST_CHECK(!font4);
190 Font font5 = DownCast< Font >(unInitializedObject);
191 DALI_TEST_CHECK(!font5);
195 int UtcDaliFontGetPixelSize(void)
197 TestApplication application;
199 tet_infoline("Testing Dali::Font::GetPixelSize()");
201 Font font = CreateFont(PixelSize(32));
203 DALI_TEST_CHECK(32 == font.GetPixelSize());
207 int UtcDaliFontGetPointSize(void)
209 TestApplication application;
211 tet_infoline("Testing Dali::Font::GetPointSize)");
213 Font font = CreateFont(PointSize(8.0f));
215 DALI_TEST_EQUALS(8.f, font.GetPointSize(), Math::MACHINE_EPSILON_1000, TEST_LOCATION);
219 int UtcDaliFontPointsToPixels(void)
221 TestApplication application;
223 tet_infoline("Testing Dali::Font::PointsToPixels)");
225 unsigned int points= Font::PointsToPixels( 12.0f );
227 DALI_TEST_CHECK( points == 36 );
231 int UtcFontMeasureTextWidth(void)
233 TestApplication application;
235 tet_infoline("Testing Dali::Font::MeasureTextWidth()");
237 TraceCallStack& trace = application.GetPlatform().GetTrace();
240 Font font = CreateFont(PointSize(8));
242 float width = font.MeasureTextWidth("test me", 24.0f);
246 DALI_TEST_CHECK(trace.FindMethod("ReadMetricsFromCacheFile"));
247 DALI_TEST_CHECK(trace.FindMethod("WriteMetricsToCacheFile"));
248 DALI_TEST_CHECK(trace.FindMethod("GetGlyphData"));
251 DALI_TEST_EQUALS(width, 168.0f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
252 width = font.MeasureTextWidth(Text(std::string("test me")), 24.0f);
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"));
259 DALI_TEST_EQUALS(width, 168.0f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
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);
269 int UtcFontMeasureTextHeight(void)
271 TestApplication application;
273 tet_infoline("Testing Dali::Font::MeasureTextHeight()");
275 TraceCallStack& trace = application.GetPlatform().GetTrace();
278 Font font = CreateFont(PointSize(8));
280 float height = font.MeasureTextHeight("test me", 48.0f);
282 DALI_TEST_CHECK(trace.FindMethod("ReadMetricsFromCacheFile"));
283 DALI_TEST_CHECK(trace.FindMethod("WriteMetricsToCacheFile"));
284 DALI_TEST_CHECK(trace.FindMethod("GetGlyphData"));
287 DALI_TEST_EQUALS(height, 6.8571f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
289 height = font.MeasureTextHeight(Text(std::string("test me")), 48.0f);
291 DALI_TEST_CHECK( ! trace.FindMethod("ReadMetricsFromCacheFile"));
292 DALI_TEST_CHECK( ! trace.FindMethod("WriteMetricsToCacheFile"));
293 DALI_TEST_CHECK( ! trace.FindMethod("GetGlyphData"));
295 DALI_TEST_EQUALS(height, 6.8571f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
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);
305 int UtcFontMeasureText(void)
307 TestApplication application;
309 tet_infoline("Testing Dali::Font::MeasureText()");
311 TraceCallStack& trace = application.GetPlatform().GetTrace();
314 Font font = CreateFont(PointSize(8));
316 Vector3 size = font.MeasureText("test me");
318 DALI_TEST_CHECK(trace.FindMethod("ReadMetricsFromCacheFile"));
319 DALI_TEST_CHECK(trace.FindMethod("WriteMetricsToCacheFile"));
320 DALI_TEST_CHECK(trace.FindMethod("GetGlyphData"));
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);
326 size = font.MeasureText(Text(std::string("test me")));
328 DALI_TEST_CHECK( ! trace.FindMethod("ReadMetricsFromCacheFile"));
329 DALI_TEST_CHECK( ! trace.FindMethod("WriteMetricsToCacheFile"));
330 DALI_TEST_CHECK( ! trace.FindMethod("GetGlyphData"));
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);
336 Character c = Text(std::string("t"))[0];
337 size = font.MeasureText(c);
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);
346 int UtcFontGetFamilyForText(void)
348 TestApplication application;
350 tet_infoline("Testing Dali::Font::GetFamilyForText()");
352 std::string latinFont = Font::GetFamilyForText("Hello world");
353 DALI_TEST_CHECK( latinFont.size() != 0 );
355 latinFont = Font::GetFamilyForText( Text(std::string("Hello world")) );
356 DALI_TEST_CHECK( latinFont.size() != 0 );
358 std::string asianFont = Font::GetFamilyForText("繁體中文");
359 DALI_TEST_CHECK( asianFont.size() != 0 );
361 asianFont = Font::GetFamilyForText(Text(std::string("繁體中文")));
362 DALI_TEST_CHECK( asianFont.size() != 0 );
364 Text text = Text(std::string("繁體中文"))[0];
365 Character c = text[0];
367 asianFont = Font::GetFamilyForText(c );
368 DALI_TEST_CHECK( asianFont.size() != 0 );
373 int UtcFontGetFontLineHeightFromCapsHeight(void)
375 TestApplication application;
377 tet_infoline("Testing Dali::Font::GetLineHeightFromCapsHeight()");
379 CapsHeight capsHeight(10);
380 PixelSize pixelSize = Font::GetLineHeightFromCapsHeight("", "", capsHeight);
381 DALI_TEST_CHECK( capsHeight < pixelSize );
383 pixelSize = Font::GetLineHeightFromCapsHeight(Font::GetFamilyForText("Hello world"), "", capsHeight);
384 DALI_TEST_CHECK( capsHeight < pixelSize );
386 pixelSize = Font::GetLineHeightFromCapsHeight(Font::GetFamilyForText(Text(std::string("Hello world"))), "", capsHeight);
387 DALI_TEST_CHECK( capsHeight < pixelSize );
391 int UtcFontAllGlyphsSupported(void)
393 TestApplication application;
395 tet_infoline("Testing Dali::Font::AllGlyphsSupported()");
397 Font font = Font::New();
399 font.AllGlyphsSupported("Hello World\n");
401 DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
402 application.GetPlatform().ResetTrace();
404 font.AllGlyphsSupported(Text(std::string("Hello World\n")));
406 DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
408 application.GetPlatform().ResetTrace();
410 Character c = Text(std::string("t"))[0];
411 font.AllGlyphsSupported(c);
413 DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
417 int UtcFontGetMetrics(void)
419 TestApplication application;
421 tet_infoline("Testing Dali::Font::UtcFontGetMetrics()");
423 Font font = Font::New();
424 font.MeasureText(Text(std::string("Hello World"))); // Builds fake metrics in TestPlatformAbstraction.
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] );
432 // TODO VCC This TET case fails if there are some metrics cached.
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 );
444 int UtcFontIsDefault(void)
446 TestApplication application;
448 tet_infoline("Testing Dali::Font::UtcFontIsDefault()");
450 FontParameters fontParams("FreeSans", "", PointSize(10.f));
451 Font font1 = Font::New(fontParams);
453 DALI_TEST_CHECK( !font1.IsDefaultSystemFont() );
454 DALI_TEST_CHECK( !font1.IsDefaultSystemSize() );
456 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateFontFamilyNameFunc ) );
457 application.GetPlatform().ResetTrace();
459 Font font2 = Font::New();
461 DALI_TEST_CHECK( !font2.IsDefaultSystemFont() );
462 DALI_TEST_CHECK( font2.IsDefaultSystemSize() );
464 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateFontFamilyNameFunc ) );
469 int UtcFontGetInstalledFonts(void)
471 TestApplication application;
473 tet_infoline("Testing Dali::Font::GetInstalledFonts()");
475 // the default should only get installed and downloaded fonts
476 std::vector<std::string> fontList;
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 );
482 fontList = Font::GetInstalledFonts(Font::LIST_ALL_FONTS);
483 DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_ALL_FONTS );
485 fontList = Font::GetInstalledFonts(Font::LIST_APPLICATION_FONTS);
486 DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_APPLICATION_FONTS );
491 int UtcFontMetricsDefaultConstructor(void)
493 TestApplication application;
495 tet_infoline("Testing UtcFontMetricsDefaultConstructor");
497 Font::Metrics metrics;
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 );
507 int UtcFontMetricsCopyConstructor(void)
509 TestApplication application;
511 tet_infoline("Testing UtcFontMetricsCopyConstructor");
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 );
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 );
526 int UtcFontMetricsAssignmentOperator(void)
528 TestApplication application;
530 tet_infoline("Testing UtcFontMetricsAssignmentOperator");
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;
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 );