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