Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-unmanaged / utc-Dali-TextInput.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 #include <stdlib.h>
19 #include <dali-toolkit-test-suite-utils.h>
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <dali/integration-api/events/key-event-integ.h>
22
23 using namespace Dali;
24 using namespace Toolkit;
25
26
27 int UtcDaliTextInputSetActiveStyle(void)
28 {
29   ToolkitTestApplication application;
30
31   tet_infoline("Testing Setting of Style to newly added text");
32
33   TextInput textInput = TextInput::New();  // create empty TextInput
34
35   Stage::GetCurrent().Add(textInput);
36
37   const std::string styledString = "Test String<i>ab</i>" ;
38   const std::string plainString = "Test String";
39   textInput.SetInitialText( plainString );
40
41   application.SendNotification();
42   application.Render();
43
44   textInput.SetEditable(true);
45
46   std::string retreivedMarkupString = textInput.GetMarkupText();
47
48   tet_infoline("Confirm markup text is a plain string ");
49   DALI_TEST_EQUALS( plainString,textInput.GetText(), TEST_LOCATION);
50
51   TextStyle style;
52   style.SetItalics( true );
53
54   tet_infoline("Apply style to TextInput");
55   textInput.SetActiveStyle( style );
56
57   Integration::KeyEvent eventA("a", "a", 0, 0, 0, Integration::KeyEvent::Down );
58   Integration::KeyEvent eventB("b", "b", 0, 0, 0, Integration::KeyEvent::Down );
59
60   application.SendNotification();
61   application.Render();
62
63   application.ProcessEvent(eventA);
64   application.SendNotification();
65   application.Render();
66
67   application.ProcessEvent(eventB);
68   application.SendNotification();
69   application.Render();
70
71   retreivedMarkupString = textInput.GetMarkupText();
72
73   DALI_TEST_EQUALS( styledString, retreivedMarkupString, TEST_LOCATION);
74   END_TEST;
75 }
76
77 int UtcDaliTextInputApplyStyleToSelectedText(void)
78 {
79   ToolkitTestApplication application;
80
81   tet_infoline("Testing application of style to selected text ");
82
83   TextInput textInput = TextInput::New();  // create empty TextInput
84
85   Stage::GetCurrent().Add(textInput);
86
87   const std::string styledString = "Test <i>String</i> to style";
88   const std::string plainString = "Test String to style";
89   textInput.SetInitialText( plainString );
90
91   application.SendNotification();
92   application.Render();
93
94   textInput.SetEditable(true);
95
96   std::string retreivedMarkupString = textInput.GetMarkupText();
97
98   tet_infoline("Confirm markup text is a plain string ");
99   DALI_TEST_EQUALS( plainString,textInput.GetText(), TEST_LOCATION);
100
101   TextStyle style;
102   style.SetItalics( true );
103
104   textInput.SelectText( 5, 11 );
105
106   tet_infoline("Apply style to selected text");
107   textInput.ApplyStyle( style );
108
109   application.Render();
110
111   retreivedMarkupString = textInput.GetMarkupText();
112
113   DALI_TEST_EQUALS( styledString, retreivedMarkupString, TEST_LOCATION);
114   END_TEST;
115 }
116
117 int UtcDaliTextInputApplyStyleToAll(void)
118 {
119   ToolkitTestApplication application;
120
121   tet_infoline("Testing application of style to all text ");
122
123   TextInput textInput = TextInput::New();  // create empty TextInput
124
125   Stage::GetCurrent().Add(textInput);
126
127   const std::string styledString = "<i>Test String to style</i>";
128   const std::string plainString = "Test String to style";
129   textInput.SetInitialText( plainString );
130
131   application.SendNotification();
132   application.Render();
133
134   textInput.SetEditable(true);
135
136   std::string retreivedMarkupString = textInput.GetMarkupText();
137
138   tet_infoline("Confirm markup text is a plain string ");
139   DALI_TEST_EQUALS( plainString,textInput.GetText(), TEST_LOCATION);
140
141   TextStyle style;
142   style.SetItalics( true );
143
144   tet_infoline("Apply style to all text");
145   textInput.ApplyStyleToAll( style );
146
147   application.Render();
148
149   retreivedMarkupString = textInput.GetMarkupText();
150
151   DALI_TEST_EQUALS( styledString, retreivedMarkupString, TEST_LOCATION);
152   END_TEST;
153 }
154
155 int UtcDaliTextInputGetStyleAtCursor(void)
156 {
157   ToolkitTestApplication application;
158
159   tet_infoline("Test getting style at cursor");
160
161   TextInput textInput = TextInput::New();  // create empty TextInput
162
163   Stage::GetCurrent().Add(textInput);
164
165   const std::string styledString = "Test Stringa<i>b</i>" ;
166   const std::string plainString = "Test String";
167   textInput.SetInitialText( plainString );
168
169   application.SendNotification();
170   application.Render();
171
172   textInput.SetEditable(true);
173
174   tet_infoline("Confirm style at cursor is default(plain)");
175   TextStyle style;
176   Integration::KeyEvent eventA("a", "a", 0, 0, 0, Integration::KeyEvent::Down );
177   application.ProcessEvent(eventA);
178   application.SendNotification();
179   application.Render();
180
181   TextStyle retreivedStyleAtCursor = textInput.GetStyleAtCursor();
182
183   DALI_TEST_CHECK( style == retreivedStyleAtCursor );
184   DALI_TEST_CHECK( !retreivedStyleAtCursor.GetItalics() );
185
186   tet_infoline("Set style before adding new character");
187   style.SetItalics( true );
188   textInput.SetActiveStyle( style );
189
190   Integration::KeyEvent eventB("b", "b", 0, 0, 0, Integration::KeyEvent::Down );
191   application.ProcessEvent(eventB);
192   application.SendNotification();
193   application.Render();
194
195   tet_infoline("Confirm style at cursor is correct style");
196   retreivedStyleAtCursor = textInput.GetStyleAtCursor();
197
198   DALI_TEST_CHECK( retreivedStyleAtCursor.GetItalics() );
199
200   tet_infoline("Confirm style at cursor is not a style that was not set");
201   DALI_TEST_CHECK( !retreivedStyleAtCursor.GetUnderline() );
202
203   tet_infoline("Confirm markup text is correct");
204   DALI_TEST_EQUALS( styledString, textInput.GetMarkupText(), TEST_LOCATION);
205
206
207
208   END_TEST;
209 }
210
211 int UtcDaliTextInputSetAndGetTextAlignment(void)
212 {
213   ToolkitTestApplication application;
214
215   TextInput textInput = TextInput::New();
216   textInput.SetTextAlignment(static_cast<Alignment::Type>( Alignment::HorizontalCenter) );
217
218   bool result = ( textInput.GetTextAlignment() & Alignment::HorizontalCenter ) ;
219
220   DALI_TEST_CHECK( result );
221
222   result = ( textInput.GetTextAlignment() & Alignment::HorizontalRight );
223
224   DALI_TEST_CHECK( !result );
225   END_TEST;
226 }
227
228 int UtcDaliTextInputSetAndGetMultilinePolicy(void)
229 {
230   ToolkitTestApplication application;
231
232   const TextView::MultilinePolicy MULTILINE_POLICIES[] = { TextView::SplitByNewLineChar, TextView::SplitByWord, TextView::SplitByChar };
233   const unsigned int NUM_MULTILINE_POLICIES = sizeof( MULTILINE_POLICIES ) / sizeof( unsigned int );
234
235   TextInput textInput = TextInput::New();
236   Stage::GetCurrent().Add(textInput);
237   textInput.SetInitialText( "Hello world!" );
238
239   for( unsigned int epIndex = 0; epIndex < NUM_MULTILINE_POLICIES; ++epIndex )
240   {
241     textInput.SetMultilinePolicy( MULTILINE_POLICIES[epIndex] );
242
243     DALI_TEST_EQUALS( textInput.GetMultilinePolicy(), MULTILINE_POLICIES[epIndex], TEST_LOCATION );
244   }
245   END_TEST;
246 }
247
248 int UtcDaliTextInputSetAndGetExceedEnabled(void)
249 {
250   ToolkitTestApplication application;
251
252   const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::Split, TextView::ShrinkToFit };
253   const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
254
255   TextInput textInput = TextInput::New();
256   Stage::GetCurrent().Add(textInput);
257   textInput.SetInitialText( "Hello world!" );
258
259   for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
260   {
261     textInput.SetWidthExceedPolicy( EXCEED_POLICIES[epIndex] );
262
263     DALI_TEST_EQUALS( textInput.GetWidthExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
264   }
265   END_TEST;
266 }