Upload package dali_0.9.11.
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-Font.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
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 #include <iostream>
18
19 #include <stdlib.h>
20 #include <dali/dali.h>
21 #include <dali/integration-api/glyph-set.h>
22
23 #include <dali-test-suite-utils.h>
24
25 // Internal headers are allowed here
26
27 #include <dali/internal/event/text/font-impl.h>
28 #include <dali/internal/event/resources/resource-ticket.h>
29 #include <dali/internal/event/common/thread-local-storage.h>
30
31 using namespace Dali;
32
33 // Called only once before first test is run.
34 void utc_dali_internal_font_startup()
35 {
36   test_return_value = TET_UNDEF;
37 }
38
39 // Called only once after last test is run
40 void utc_dali_internal_font_cleanup()
41 {
42   test_return_value = TET_PASS;
43 }
44
45
46 namespace
47 {
48
49 static const char* TestText = "Some text";
50
51
52 Integration::GlyphMetrics characters[] =
53     {
54         {' ', 1,  0.0f,  0.0f, 0.0f, 0.0f, 10.0f},
55         {'S', 1, 10.0f, 20.0f, 0.0f, 1.0f, 12.0f},
56         {'o', 1, 11.0f, 20.0f, 0.0f, 1.0f, 13.0f},
57         {'m', 1, 12.0f, 20.0f, 0.0f, 1.0f, 14.0f},
58         {'e', 1, 13.0f, 20.0f, 0.0f, 1.0f, 15.0f},
59         {'t', 1, 14.0f, 20.0f, 0.0f, 1.0f, 16.0f},
60         {'x', 1, 15.0f, 20.0f, 0.0f, 1.0f, 17.0f}   };
61
62 static Integration::GlyphSet* BuildGlyphSet()
63 {
64   Integration::GlyphSet* set = new Integration::GlyphSet();
65   Integration::BitmapPtr bitmapData;
66
67   for (unsigned int index = 0; index < sizeof(characters)/sizeof(characters[0]); index++)
68   {
69     set->AddCharacter(bitmapData, characters[index]);
70   }
71
72   set->mLineHeight = 20.0f;
73   set->mUnitsPerEM = 2048.0f/64.0f;
74
75   return set;
76 }
77
78 static Font CreateFont(TestApplication& application)
79 {
80   Integration::GlyphSet* glyphSet = BuildGlyphSet();
81   Integration::ResourcePointer resourcePtr(glyphSet); // reference it
82
83   // Don't use a font which could be cached otherwise cached values will be used making measure text test to fail.
84   Font font = Font::New(FontParameters("TET-FreeSans", "Book", PointSize(8)));
85   application.SendNotification(); // Send to update thread
86   application.Render(16);         // Process request
87   application.Render(16);         // Resource complete
88   application.SendNotification(); // Update event objects
89   application.GetPlatform().DiscardRequest(); // Ensure load request is discarded
90   return font;
91 }
92
93 } //anonymous namespace
94
95
96 int UtcDaliFontMeasureTextWidth(void)
97 {
98   TestApplication application;
99
100   tet_infoline("Testing Dali::Font::MeasureTextWidth()");
101
102   Font testFont = CreateFont(application);
103   float width = testFont.MeasureTextWidth(TestText, 30.0f);
104
105   DALI_TEST_EQUALS(width, 270.0f, 0.001f, TEST_LOCATION);
106   END_TEST;
107 }
108
109 int UtcDaliFontMeasureTextWidthNegative(void)
110 {
111   TestApplication application;
112
113   tet_infoline("Testing Dali::Font::MeasureTextWidth() with negative height");
114
115   Font testFont = CreateFont(application);
116   float width = testFont.MeasureTextWidth(TestText, -30.0f);
117
118   DALI_TEST_EQUALS(width, 0.0f, TEST_LOCATION);
119   END_TEST;
120 }
121
122 int UtcDaliFontMeasureTextHeight(void)
123 {
124   TestApplication application;
125
126   tet_infoline("Testing Dali::Font::MeasureTextHeight()");
127
128   Font testFont = CreateFont(application);
129   float height = testFont.MeasureTextHeight(TestText, 200.0f);
130
131   DALI_TEST_EQUALS(height, 22.2222f, 0.001f, TEST_LOCATION);
132   END_TEST;
133 }
134
135 int UtcDaliFontMeasureTextHeightNegative(void)
136 {
137   TestApplication application;
138
139   tet_infoline("Testing Dali::Font::MeasureTextHeight() with negative width");
140
141   Font testFont = CreateFont(application);
142   float height = testFont.MeasureTextHeight(TestText, -200.0f);
143
144   DALI_TEST_EQUALS(height, 0.0f, TEST_LOCATION);
145   END_TEST;
146 }