2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
21 #include <dali/dali.h>
22 #include <dali-test-suite-utils.h>
26 void utc_dali_font_startup(void)
28 test_return_value = TET_UNDEF;
31 void utc_dali_font_cleanup(void)
33 test_return_value = TET_PASS;
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;
44 static Font CreateFont( PointSize size )
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));
50 static Font CreateFont( PixelSize size )
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));
56 static Font CreateFont( CapsHeight size )
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));
64 int UtcDaliFontNew01(void)
66 TestApplication application;
68 tet_infoline("Testing Dali::Font::New() - with specific font family and pixel size");
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
73 DALI_TEST_CHECK(font);
75 tet_infoline("Testing Dali::Font::New() - with default font name and pixel size");
77 Font font2 = Font::New(FontParameters("", "", PixelSize(0.0f)));
79 DALI_TEST_CHECK(font2);
81 Font* ptrFont = new Font;
82 *ptrFont = Font::New(FontParameters("", "", PixelSize(0.0f)));
87 int UtcDaliFontNew02(void)
89 TestApplication application;
91 tet_infoline("Testing Dali::Font::New() - with specific font family and point size");
93 Font font = CreateFont(PointSize(8));
95 DALI_TEST_CHECK(font);
97 tet_infoline("Testing Dali::Font::New() - with default font family and point size");
100 font2 = Font::New(FontParameters("", "", PointSize(0)));
102 DALI_TEST_CHECK(font2);
106 int UtcDaliFontNew03(void)
108 TestApplication application;
110 tet_infoline("Testing Dali::Font::New() - with specific font family and caps-height");
112 Font font = CreateFont(CapsHeight(8));
114 DALI_TEST_CHECK(font);
115 DALI_TEST_CHECK(font.GetPixelSize() > 8.0f); // Pixel size should be bigger than requested CapsHeight
117 tet_infoline("Testing Dali::Font::New() - with default font family and point size");
119 Font font2 = Font::New(FontParameters("", "", CapsHeight(0)));
121 DALI_TEST_CHECK(font2);
125 int UtcDaliFontNew04(void)
127 TestApplication application;
129 tet_infoline("Testing Dali::Font::New() - with wrong font family or font style");
131 Font font = Font::New(FontParameters("gfagag", "fgafgafga",PointSize(0)));
133 DALI_TEST_CHECK(font);
137 int UtcDaliFontNew05(void)
139 TestApplication application;
141 tet_infoline("Testing Dali::Font::New() - with pixel size and weight");
143 PixelSize pixelSize(PIXEL_SIZE);
144 FontParameters fontParams(FAMILY_NAME, "", pixelSize);
145 Font font = Font::New( fontParams );
147 DALI_TEST_CHECK( font );
148 DALI_TEST_CHECK( font.GetName() == FAMILY_NAME );
149 DALI_TEST_CHECK( font.GetStyle().empty() );
153 int UtcDaliFontNew06(void)
155 TestApplication application;
157 tet_infoline("Testing Dali::Font::New() - with caps height");
159 CapsHeight capsHeight(10.f);
160 FontParameters fontParams(FAMILY_NAME, "", capsHeight);
161 Font font = Font::New( fontParams );
163 DALI_TEST_CHECK( font );
164 DALI_TEST_CHECK( font.GetName() == FAMILY_NAME );
165 DALI_TEST_CHECK( font.GetStyle().empty() );
170 int UtcDaliFontDownCast(void)
172 TestApplication application;
173 tet_infoline("Testing Dali::Font::DownCast()");
175 Font font = CreateFont(PixelSize(25.0f));
177 BaseHandle object(font);
179 Font font2 = Font::DownCast(object);
180 DALI_TEST_CHECK(font2);
182 Font font3 = DownCast< Font >(object);
183 DALI_TEST_CHECK(font3);
185 BaseHandle unInitializedObject;
186 Font font4 = Font::DownCast(unInitializedObject);
187 DALI_TEST_CHECK(!font4);
189 Font font5 = DownCast< Font >(unInitializedObject);
190 DALI_TEST_CHECK(!font5);
194 int UtcDaliFontGetPixelSize(void)
196 TestApplication application;
198 tet_infoline("Testing Dali::Font::GetPixelSize()");
200 Font font = CreateFont(PixelSize(32));
202 DALI_TEST_CHECK(32 == font.GetPixelSize());
206 int UtcDaliFontGetPointSize(void)
208 TestApplication application;
210 tet_infoline("Testing Dali::Font::GetPointSize)");
212 Font font = CreateFont(PointSize(8.0f));
214 DALI_TEST_EQUALS(8.f, font.GetPointSize(), Math::MACHINE_EPSILON_1000, TEST_LOCATION);
218 int UtcDaliFontPointsToPixels(void)
220 TestApplication application;
222 tet_infoline("Testing Dali::Font::PointsToPixels)");
224 unsigned int points= Font::PointsToPixels( 12.0f );
226 DALI_TEST_CHECK( points == 36 );
230 int UtcFontMeasureTextWidth(void)
232 TestApplication application;
234 tet_infoline("Testing Dali::Font::MeasureTextWidth()");
236 TraceCallStack& trace = application.GetPlatform().GetTrace();
239 Font font = CreateFont(PointSize(8));
241 float width = font.MeasureTextWidth("test me", 24.0f);
245 DALI_TEST_CHECK(trace.FindMethod("ReadMetricsFromCacheFile"));
246 DALI_TEST_CHECK(trace.FindMethod("WriteMetricsToCacheFile"));
247 DALI_TEST_CHECK(trace.FindMethod("GetGlyphData"));
250 DALI_TEST_EQUALS(width, 168.0f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
251 width = font.MeasureTextWidth(Text(std::string("test me")), 24.0f);
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"));
258 DALI_TEST_EQUALS(width, 168.0f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
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);
268 int UtcFontMeasureTextHeight(void)
270 TestApplication application;
272 tet_infoline("Testing Dali::Font::MeasureTextHeight()");
274 TraceCallStack& trace = application.GetPlatform().GetTrace();
277 Font font = CreateFont(PointSize(8));
279 float height = font.MeasureTextHeight("test me", 48.0f);
281 DALI_TEST_CHECK(trace.FindMethod("ReadMetricsFromCacheFile"));
282 DALI_TEST_CHECK(trace.FindMethod("WriteMetricsToCacheFile"));
283 DALI_TEST_CHECK(trace.FindMethod("GetGlyphData"));
286 DALI_TEST_EQUALS(height, 6.8571f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
288 height = font.MeasureTextHeight(Text(std::string("test me")), 48.0f);
290 DALI_TEST_CHECK( ! trace.FindMethod("ReadMetricsFromCacheFile"));
291 DALI_TEST_CHECK( ! trace.FindMethod("WriteMetricsToCacheFile"));
292 DALI_TEST_CHECK( ! trace.FindMethod("GetGlyphData"));
294 DALI_TEST_EQUALS(height, 6.8571f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
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);
304 int UtcFontMeasureText(void)
306 TestApplication application;
308 tet_infoline("Testing Dali::Font::MeasureText()");
310 TraceCallStack& trace = application.GetPlatform().GetTrace();
313 Font font = CreateFont(PointSize(8));
315 Vector3 size = font.MeasureText("test me");
317 DALI_TEST_CHECK(trace.FindMethod("ReadMetricsFromCacheFile"));
318 DALI_TEST_CHECK(trace.FindMethod("WriteMetricsToCacheFile"));
319 DALI_TEST_CHECK(trace.FindMethod("GetGlyphData"));
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);
325 size = font.MeasureText(Text(std::string("test me")));
327 DALI_TEST_CHECK( ! trace.FindMethod("ReadMetricsFromCacheFile"));
328 DALI_TEST_CHECK( ! trace.FindMethod("WriteMetricsToCacheFile"));
329 DALI_TEST_CHECK( ! trace.FindMethod("GetGlyphData"));
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);
335 Text text = Text(std::string("t"))[0];
336 Character c = text[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 Text text = Text(std::string("t"))[0];
411 Character c = text[0];
412 font.AllGlyphsSupported(c);
414 DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
418 int UtcFontGetMetrics(void)
420 TestApplication application;
422 tet_infoline("Testing Dali::Font::UtcFontGetMetrics()");
424 Font font = Font::New();
425 font.MeasureText(Text(std::string("Hello World"))); // Builds fake metrics in TestPlatformAbstraction.
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] );
433 // TODO VCC This TET case fails if there are some metrics cached.
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 );
445 int UtcFontIsDefault(void)
447 TestApplication application;
449 tet_infoline("Testing Dali::Font::UtcFontIsDefault()");
451 FontParameters fontParams("FreeSans", "", PointSize(10.f));
452 Font font1 = Font::New(fontParams);
454 DALI_TEST_CHECK( !font1.IsDefaultSystemFont() );
455 DALI_TEST_CHECK( !font1.IsDefaultSystemSize() );
457 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateFontFamilyNameFunc ) );
458 application.GetPlatform().ResetTrace();
460 Font font2 = Font::New();
462 DALI_TEST_CHECK( !font2.IsDefaultSystemFont() );
463 DALI_TEST_CHECK( font2.IsDefaultSystemSize() );
465 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateFontFamilyNameFunc ) );
470 int UtcFontGetInstalledFonts(void)
472 TestApplication application;
474 tet_infoline("Testing Dali::Font::GetInstalledFonts()");
476 // the default should only get installed and downloaded fonts
477 std::vector<std::string> fontList;
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 );
483 fontList = Font::GetInstalledFonts(Font::LIST_ALL_FONTS);
484 DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_ALL_FONTS );
486 fontList = Font::GetInstalledFonts(Font::LIST_APPLICATION_FONTS);
487 DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_APPLICATION_FONTS );
492 int UtcFontMetricsDefaultConstructor(void)
494 TestApplication application;
496 tet_infoline("Testing UtcFontMetricsDefaultConstructor");
498 Font::Metrics metrics;
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 );
508 int UtcFontMetricsCopyConstructor(void)
510 TestApplication application;
512 tet_infoline("Testing UtcFontMetricsCopyConstructor");
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 );
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 );
527 int UtcFontMetricsAssignmentOperator(void)
529 TestApplication application;
531 tet_infoline("Testing UtcFontMetricsAssignmentOperator");
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;
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 );