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