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