ModelView implementation.
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-Controller.cpp
1 /*
2  * Copyright (c) 2016 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 <limits>
22 #include <unistd.h>
23
24 #include <dali-toolkit-test-suite-utils.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <toolkit-text-utils.h>
27 #include <dali-toolkit/internal/text/text-controller.h>
28 #include <dali-toolkit/internal/text/text-control-interface.h>
29 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
30
31 using namespace Dali;
32 using namespace Toolkit;
33 using namespace Text;
34
35 namespace
36 {
37
38 const char* const OPTION_SELECT_WORD("option-select_word"); // "Select Word" popup option.
39 const char* const OPTION_SELECT_ALL("option-select_all");   // "Select All" popup option.
40 const char* const OPTION_CUT("optionCut");                  // "Cut" popup option.
41 const char* const OPTION_COPY("optionCopy");                // "Copy" popup option.
42 const char* const OPTION_PASTE("optionPaste");              // "Paste" popup option.
43 const char* const OPTION_CLIPBOARD("optionClipboard");      // "Clipboard" popup option.
44
45 const Size CONTROL_SIZE( 300.f, 60.f );
46
47 const std::string DEFAULT_FONT_DIR( "/resources/fonts" );
48
49 std::string gClipboardText;
50 void ContentSelectedCallback( ClipboardEventNotifier& notifier )
51 {
52   gClipboardText = notifier.GetContent();
53 }
54
55 } // namespace
56
57 int UtcDaliTextController(void)
58 {
59   tet_infoline(" UtcDaliTextController");
60   ToolkitTestApplication application;
61
62   // Creates a text controller.
63   ControllerPtr controller = Controller::New();
64   DALI_TEST_CHECK( controller );
65
66   tet_result(TET_PASS);
67   END_TEST;
68 }
69
70 int UtcDaliTextControllerSetGetScrollEnabled(void)
71 {
72   tet_infoline(" UtcDaliTextControllerSetGetScrollEnabled");
73   ToolkitTestApplication application;
74
75   // Creates a text controller.
76   ControllerPtr controller = Controller::New();
77   DALI_TEST_CHECK( controller );
78
79   // Configures the text controller similarly to the text-editor.
80   ConfigureTextEditor( controller );
81
82   DALI_TEST_CHECK( !controller->IsHorizontalScrollEnabled() );
83   DALI_TEST_CHECK( controller->IsVerticalScrollEnabled() );
84
85   // Configures the text controller similarly to the text-field.
86   ConfigureTextField( controller );
87
88   DALI_TEST_CHECK( controller->IsHorizontalScrollEnabled() );
89   DALI_TEST_CHECK( !controller->IsVerticalScrollEnabled() );
90
91   // Configures the text controller similarly to the text-label.
92   ConfigureTextLabel( controller );
93
94   DALI_TEST_CHECK( !controller->IsHorizontalScrollEnabled() );
95   DALI_TEST_CHECK( !controller->IsVerticalScrollEnabled() );
96
97   tet_result(TET_PASS);
98   END_TEST;
99 }
100
101 int UtcDaliTextControllerSetIsTextElide(void)
102 {
103   tet_infoline(" UtcDaliTextControllerSetIsTextElide");
104   ToolkitTestApplication application;
105
106   // Creates a text controller.
107   ControllerPtr controller = Controller::New();
108   DALI_TEST_CHECK( controller );
109
110   // Configures the text controller similarly to the text-editor.
111   ConfigureTextEditor( controller );
112   DALI_TEST_EQUALS( false, controller->IsTextElideEnabled(), TEST_LOCATION );
113
114   controller->SetTextElideEnabled( true );
115   DALI_TEST_EQUALS( true, controller->IsTextElideEnabled(), TEST_LOCATION );
116
117   // Configures the text controller similarly to the text-field.
118   ConfigureTextField( controller );
119   DALI_TEST_EQUALS( false, controller->IsTextElideEnabled(), TEST_LOCATION );
120
121   controller->SetTextElideEnabled( true );
122   DALI_TEST_EQUALS( true, controller->IsTextElideEnabled(), TEST_LOCATION );
123
124   // Configures the text controller similarly to the text-label.
125   ConfigureTextLabel( controller );
126   DALI_TEST_EQUALS( true, controller->IsTextElideEnabled(), TEST_LOCATION );
127
128   controller->SetTextElideEnabled( false );
129   DALI_TEST_EQUALS( false, controller->IsTextElideEnabled(), TEST_LOCATION );
130
131   tet_result(TET_PASS);
132   END_TEST;
133 }
134
135 int UtcDaliTextControllerEnableCursorBlinking(void)
136 {
137   tet_infoline(" UtcDaliTextControllerEnableCursorBlinking");
138   ToolkitTestApplication application;
139
140   // Creates a text controller.
141   ControllerPtr controller = Controller::New();
142   DALI_TEST_CHECK( controller );
143
144   // There is no text input enabled.
145   DALI_TEST_CHECK( !controller->GetEnableCursorBlink() );
146
147   // Enable the text input.
148   // Creates a decorator.
149   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
150                                                        *controller );
151
152   // Enables the text input.
153   controller->EnableTextInput( decorator );
154
155   // Enables the cursor blink.
156   controller->SetEnableCursorBlink( true );
157
158   DALI_TEST_CHECK( controller->GetEnableCursorBlink() );
159
160   // Disables the cursor blink.
161   controller->SetEnableCursorBlink( false );
162
163   DALI_TEST_CHECK( !controller->GetEnableCursorBlink() );
164
165   tet_result(TET_PASS);
166   END_TEST;
167 }
168
169 int UtcDaliTextControllerImfEvent(void)
170 {
171   tet_infoline(" UtcDaliTextController");
172   ToolkitTestApplication application;
173
174   // Creates a text controller.
175   ControllerPtr controller = Controller::New();
176
177   std::string text;
178   ImfManager::ImfEventData imfEvent;
179
180   DALI_TEST_CHECK( controller );
181
182   // Enable the text input.
183   // Creates a decorator.
184   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
185                                                        *controller );
186
187   // Enables the text input.
188   controller->EnableTextInput( decorator );
189
190   // Creates an ImfManager.
191   ImfManager imfManager = ImfManager::Get();
192
193   // Send VOID event.
194   imfEvent = ImfManager::ImfEventData( ImfManager::VOID, "", 0, 0 );
195   controller->OnImfEvent( imfManager, imfEvent );
196
197   controller->GetText( text );
198   DALI_TEST_CHECK( text.empty() );
199
200   // Send COMMIT event.
201   imfEvent = ImfManager::ImfEventData( ImfManager::COMMIT, "Hello ", 0, 6 );
202   controller->OnImfEvent( imfManager, imfEvent );
203
204   // Force to update the model.
205   controller->GetNaturalSize();
206
207   controller->GetText( text );
208   DALI_TEST_EQUALS( "Hello ", text, TEST_LOCATION );
209
210   // Send PREEDIT event
211   imfEvent = ImfManager::ImfEventData( ImfManager::PREEDIT, "w", 6, 1 );
212   controller->OnImfEvent( imfManager, imfEvent );
213
214   // Force to update the model.
215   controller->GetNaturalSize();
216
217   controller->GetText( text );
218   DALI_TEST_EQUALS( "Hello w", text, TEST_LOCATION );
219
220   // Send DELETESURROUNDING event
221   imfEvent = ImfManager::ImfEventData( ImfManager::DELETESURROUNDING, "", -1, 1 );
222   controller->OnImfEvent( imfManager, imfEvent );
223
224   // Force to update the model.
225   controller->GetNaturalSize();
226
227   controller->GetText( text );
228   DALI_TEST_EQUALS( "Hello ", text, TEST_LOCATION );
229
230   // Send PREEDIT event
231   imfEvent = ImfManager::ImfEventData( ImfManager::PREEDIT, "wo", 6, 2 );
232   controller->OnImfEvent( imfManager, imfEvent );
233
234   // Force to update the model.
235   controller->GetNaturalSize();
236
237   controller->GetText( text );
238   DALI_TEST_EQUALS( "Hello wo", text, TEST_LOCATION );
239
240   // Send GETSURROUNDING event
241   imfEvent = ImfManager::ImfEventData( ImfManager::GETSURROUNDING, "", 0, 0 );
242   controller->OnImfEvent( imfManager, imfEvent );
243
244   controller->GetText( text );
245   DALI_TEST_EQUALS( "Hello wo", text, TEST_LOCATION );
246
247   tet_result(TET_PASS);
248   END_TEST;
249 }
250
251 int UtcDaliTextControllerTextPopupButtonTouched(void)
252 {
253   tet_infoline(" UtcDaliTextControllerTextPopupButtonTouched");
254   ToolkitTestApplication application;
255
256   // Creates a text controller.
257   ControllerPtr controller = Controller::New();
258
259   DALI_TEST_CHECK( controller );
260
261   std::string text;
262   PushButton button;
263   Property::Map attributes;
264
265   // Enable the text input.
266   // Creates a decorator.
267   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
268                                                        *controller );
269
270   // Enables the text input.
271   controller->EnableTextInput( decorator );
272
273   // Creates the text's popup.
274   TextSelectionPopupCallbackInterface& callbackInterface = *controller;
275   TextSelectionPopup textPopup = TextSelectionPopup::New( &callbackInterface );
276
277   Toolkit::TextSelectionPopup::Buttons buttonsToEnable = static_cast<Toolkit::TextSelectionPopup::Buttons>( TextSelectionPopup::CUT        |
278                                                                                                             TextSelectionPopup::COPY       |
279                                                                                                             TextSelectionPopup::PASTE      |
280                                                                                                             TextSelectionPopup::SELECT     |
281                                                                                                             TextSelectionPopup::SELECT_ALL |
282                                                                                                             TextSelectionPopup::CLIPBOARD );
283
284   textPopup.EnableButtons( buttonsToEnable );
285   Stage::GetCurrent().Add( textPopup );
286   textPopup.ShowPopup();
287
288   // Render and notify
289   application.SendNotification();
290   application.Render();
291
292   // Sets some text.
293   controller->SetText( "Hello world" );
294
295   // Select the whole text.
296   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_SELECT_ALL ) );
297   DALI_TEST_CHECK( button );
298
299   button.DoAction( "buttonClick", attributes );
300
301   // Call relayout to process the input events.
302   controller->Relayout( CONTROL_SIZE );
303
304   // Cut the text.
305   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CUT ) );
306   DALI_TEST_CHECK( button );
307
308   button.DoAction( "buttonClick", attributes );
309
310   // Force to update the model.
311   controller->GetNaturalSize();
312
313   controller->GetText( text );
314   DALI_TEST_CHECK( text.empty() );
315
316   // Set text again.
317   controller->SetText( "Hello world" );
318
319   // Select the whole text.
320   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_SELECT_ALL ) );
321   DALI_TEST_CHECK( button );
322
323   button.DoAction( "buttonClick", attributes );
324
325   // Call relayout to process the input events.
326   controller->Relayout( CONTROL_SIZE );
327
328   // Copy to the clipboard.
329   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_COPY ) );
330   DALI_TEST_CHECK( button );
331
332   button.DoAction( "buttonClick", attributes );
333
334   // Call relayout to process the input events.
335   controller->Relayout( CONTROL_SIZE );
336
337   // Cut the text.
338   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CUT ) );
339   DALI_TEST_CHECK( button );
340
341   button.DoAction( "buttonClick", attributes );
342
343   // Force to update the model.
344   controller->GetNaturalSize();
345
346   controller->GetText( text );
347   DALI_TEST_CHECK( text.empty() );
348
349   ClipboardEventNotifier clipboardEventNotifier = ClipboardEventNotifier::Get();
350   clipboardEventNotifier.ContentSelectedSignal().Connect( &ContentSelectedCallback );
351
352   // Paste the text.
353   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_PASTE ) );
354   DALI_TEST_CHECK( button );
355
356   button.DoAction( "buttonClick", attributes );
357
358   // Call relayout to process the input events.
359   controller->Relayout( CONTROL_SIZE );
360
361   DALI_TEST_EQUALS( "Hello world", gClipboardText, TEST_LOCATION );
362
363   // Show the clipboard.
364   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CLIPBOARD ) );
365   DALI_TEST_CHECK( button );
366
367   button.DoAction( "buttonClick", attributes );
368
369   tet_result(TET_PASS);
370   END_TEST;
371 }
372
373 int UtcDaliTextControllerGetInputShadowProperty(void)
374 {
375   tet_infoline(" UtcDaliTextControllerGetInputShadowProperty");
376   ToolkitTestApplication application;
377
378   // Creates a text controller.
379   ControllerPtr controller = Controller::New();
380
381   DALI_TEST_CHECK( controller );
382
383   const std::string& shadowProperties = controller->GetInputShadowProperties();
384
385   DALI_TEST_CHECK( shadowProperties.empty() );
386
387   tet_result(TET_PASS);
388   END_TEST;
389 }
390
391 int UtcDaliTextControllerGetInputUnderlineProperty(void)
392 {
393   tet_infoline(" UtcDaliTextControllerGetInputUnderlineProperty");
394   ToolkitTestApplication application;
395
396   // Creates a text controller.
397   ControllerPtr controller = Controller::New();
398
399   DALI_TEST_CHECK( controller );
400
401   const std::string& underlineProperties = controller->GetInputUnderlineProperties();
402
403   DALI_TEST_CHECK( underlineProperties.empty() );
404
405   tet_result(TET_PASS);
406   END_TEST;
407 }
408
409 int UtcDaliTextControllerSetGetAutoScrollEnabled(void)
410 {
411   tet_infoline(" UtcDaliTextControllerSetGetAutoScrollEnabled");
412   ToolkitTestApplication application;
413
414   // Creates a text controller.
415   ControllerPtr controller = Controller::New();
416
417   DALI_TEST_CHECK( controller );
418
419   DALI_TEST_CHECK( !controller->IsAutoScrollEnabled() );
420
421   // The auto scrolling shouldn't be enabled if the multi-line is enabled.
422
423   // Enable multi-line.
424   controller->SetMultiLineEnabled( true );
425
426   // Enable text scrolling.
427   controller->SetAutoScrollEnabled( true );
428
429   DALI_TEST_CHECK( !controller->IsAutoScrollEnabled() );
430
431   // Disable multi-line.
432   controller->SetMultiLineEnabled( false );
433
434   // Enable text scrolling.
435   controller->SetAutoScrollEnabled( true );
436
437   // Should be ebabled now.
438   DALI_TEST_CHECK( controller->IsAutoScrollEnabled() );
439
440   tet_result(TET_PASS);
441   END_TEST;
442 }