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/dali.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 Text text = Text(std::string("t"))[0];
337 Character c = text[0];
338 size = font.MeasureText(c);
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);
347 int UtcFontGetFamilyForText(void)
349 TestApplication application;
351 tet_infoline("Testing Dali::Font::GetFamilyForText()");
353 std::string latinFont = Font::GetFamilyForText("Hello world");
354 DALI_TEST_CHECK( latinFont.size() != 0 );
356 latinFont = Font::GetFamilyForText( Text(std::string("Hello world")) );
357 DALI_TEST_CHECK( latinFont.size() != 0 );
359 std::string asianFont = Font::GetFamilyForText("繁體中文");
360 DALI_TEST_CHECK( asianFont.size() != 0 );
362 asianFont = Font::GetFamilyForText(Text(std::string("繁體中文")));
363 DALI_TEST_CHECK( asianFont.size() != 0 );
365 Text text = Text(std::string("繁體中文"))[0];
366 Character c = text[0];
368 asianFont = Font::GetFamilyForText(c );
369 DALI_TEST_CHECK( asianFont.size() != 0 );
374 int UtcFontGetFontLineHeightFromCapsHeight(void)
376 TestApplication application;
378 tet_infoline("Testing Dali::Font::GetLineHeightFromCapsHeight()");
380 CapsHeight capsHeight(10);
381 PixelSize pixelSize = Font::GetLineHeightFromCapsHeight("", "", capsHeight);
382 DALI_TEST_CHECK( capsHeight < pixelSize );
384 pixelSize = Font::GetLineHeightFromCapsHeight(Font::GetFamilyForText("Hello world"), "", capsHeight);
385 DALI_TEST_CHECK( capsHeight < pixelSize );
387 pixelSize = Font::GetLineHeightFromCapsHeight(Font::GetFamilyForText(Text(std::string("Hello world"))), "", capsHeight);
388 DALI_TEST_CHECK( capsHeight < pixelSize );
392 int UtcFontAllGlyphsSupported(void)
394 TestApplication application;
396 tet_infoline("Testing Dali::Font::AllGlyphsSupported()");
398 Font font = Font::New();
400 font.AllGlyphsSupported("Hello World\n");
402 DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
403 application.GetPlatform().ResetTrace();
405 font.AllGlyphsSupported(Text(std::string("Hello World\n")));
407 DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
409 application.GetPlatform().ResetTrace();
411 Text text = Text(std::string("t"))[0];
412 Character c = text[0];
413 font.AllGlyphsSupported(c);
415 DALI_TEST_CHECK(application.GetPlatform().WasCalled(TestPlatformAbstraction::AllGlyphsSupportedFunc));
419 int UtcFontGetMetrics(void)
421 TestApplication application;
423 tet_infoline("Testing Dali::Font::UtcFontGetMetrics()");
425 Font font = Font::New();
426 font.MeasureText(Text(std::string("Hello World"))); // Builds fake metrics in TestPlatformAbstraction.
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] );
434 // TODO VCC This TET case fails if there are some metrics cached.
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 );
446 int UtcFontIsDefault(void)
448 TestApplication application;
450 tet_infoline("Testing Dali::Font::UtcFontIsDefault()");
452 FontParameters fontParams("FreeSans", "", PointSize(10.f));
453 Font font1 = Font::New(fontParams);
455 DALI_TEST_CHECK( !font1.IsDefaultSystemFont() );
456 DALI_TEST_CHECK( !font1.IsDefaultSystemSize() );
458 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateFontFamilyNameFunc ) );
459 application.GetPlatform().ResetTrace();
461 Font font2 = Font::New();
463 DALI_TEST_CHECK( !font2.IsDefaultSystemFont() );
464 DALI_TEST_CHECK( font2.IsDefaultSystemSize() );
466 DALI_TEST_CHECK( application.GetPlatform().WasCalled( TestPlatformAbstraction::ValidateFontFamilyNameFunc ) );
471 int UtcFontGetInstalledFonts(void)
473 TestApplication application;
475 tet_infoline("Testing Dali::Font::GetInstalledFonts()");
477 // the default should only get installed and downloaded fonts
478 std::vector<std::string> fontList;
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 );
484 fontList = Font::GetInstalledFonts(Font::LIST_ALL_FONTS);
485 DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_ALL_FONTS );
487 fontList = Font::GetInstalledFonts(Font::LIST_APPLICATION_FONTS);
488 DALI_TEST_CHECK( application.GetPlatform().GetLastFontListMode() == Dali::Integration::PlatformAbstraction::LIST_APPLICATION_FONTS );
493 int UtcFontMetricsDefaultConstructor(void)
495 TestApplication application;
497 tet_infoline("Testing UtcFontMetricsDefaultConstructor");
499 Font::Metrics metrics;
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 );
509 int UtcFontMetricsCopyConstructor(void)
511 TestApplication application;
513 tet_infoline("Testing UtcFontMetricsCopyConstructor");
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 );
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 );
528 int UtcFontMetricsAssignmentOperator(void)
530 TestApplication application;
532 tet_infoline("Testing UtcFontMetricsAssignmentOperator");
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;
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 );