Merge "Text outline support in TextField & TextEditor" into devel/master
[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   // Set the placeholder text.
188   controller->SetPlaceholderText( Controller::PLACEHOLDER_TYPE_INACTIVE, "Hello Dali" );
189
190   // Creates an ImfManager.
191   ImfManager imfManager = ImfManager::Get();
192
193   // For coverage.
194   imfEvent = ImfManager::ImfEventData( ImfManager::GETSURROUNDING, "", 0, 0 );
195   controller->OnImfEvent( imfManager, imfEvent );
196
197   // Send VOID event.
198   imfEvent = ImfManager::ImfEventData( ImfManager::VOID, "", 0, 0 );
199   controller->OnImfEvent( imfManager, imfEvent );
200
201   controller->GetText( text );
202   DALI_TEST_CHECK( text.empty() );
203
204   imfEvent = ImfManager::ImfEventData( ImfManager::COMMIT, "Hello ", 0, 6 );
205   controller->OnImfEvent( imfManager, imfEvent );
206   controller->GetNaturalSize();
207
208   // Check 'Delete All' key which means the input panel send a big range
209   imfEvent = ImfManager::ImfEventData( ImfManager::DELETESURROUNDING, "", -100, 100 );
210   controller->OnImfEvent( imfManager, imfEvent );
211   controller->GetNaturalSize();
212
213   controller->GetText( text );
214   DALI_TEST_EQUALS( "", text, TEST_LOCATION );
215
216   // Send COMMIT event.
217   imfEvent = ImfManager::ImfEventData( ImfManager::COMMIT, "Hello ", 0, 6 );
218   controller->OnImfEvent( imfManager, imfEvent );
219
220   // Force to update the model.
221   controller->GetNaturalSize();
222
223   controller->GetText( text );
224   DALI_TEST_EQUALS( "Hello ", text, TEST_LOCATION );
225
226   // Send PREEDIT event
227   imfEvent = ImfManager::ImfEventData( ImfManager::PREEDIT, "w", 6, 1 );
228   controller->OnImfEvent( imfManager, imfEvent );
229
230   // Force to update the model.
231   controller->GetNaturalSize();
232
233   controller->GetText( text );
234   DALI_TEST_EQUALS( "Hello w", text, TEST_LOCATION );
235
236   // Send DELETESURROUNDING event
237   imfEvent = ImfManager::ImfEventData( ImfManager::DELETESURROUNDING, "", -1, 1 );
238   controller->OnImfEvent( imfManager, imfEvent );
239
240   // Force to update the model.
241   controller->GetNaturalSize();
242
243   controller->GetText( text );
244   DALI_TEST_EQUALS( "Hello ", text, TEST_LOCATION );
245
246   // Send PREEDIT event
247   imfEvent = ImfManager::ImfEventData( ImfManager::PREEDIT, "wo", 6, 2 );
248   controller->OnImfEvent( imfManager, imfEvent );
249
250   // Force to update the model.
251   controller->GetNaturalSize();
252
253   controller->GetText( text );
254   DALI_TEST_EQUALS( "Hello wo", text, TEST_LOCATION );
255
256   // Send GETSURROUNDING event
257   imfEvent = ImfManager::ImfEventData( ImfManager::GETSURROUNDING, "", 0, 0 );
258   controller->OnImfEvent( imfManager, imfEvent );
259
260   controller->GetText( text );
261   DALI_TEST_EQUALS( "Hello wo", text, TEST_LOCATION );
262
263   // Send PRIVATECOMMAND event
264   imfEvent = ImfManager::ImfEventData( ImfManager::PRIVATECOMMAND, "", 0, 0 );
265   controller->OnImfEvent( imfManager, imfEvent );
266
267   controller->GetText( text );
268   DALI_TEST_EQUALS( "Hello wo", text, TEST_LOCATION );
269
270   tet_result(TET_PASS);
271   END_TEST;
272 }
273
274 int UtcDaliTextControllerTextPopupButtonTouched(void)
275 {
276   tet_infoline(" UtcDaliTextControllerTextPopupButtonTouched");
277   ToolkitTestApplication application;
278
279   // Creates a text controller.
280   ControllerPtr controller = Controller::New();
281
282   DALI_TEST_CHECK( controller );
283
284   std::string text;
285   PushButton button;
286   Property::Map attributes;
287
288   // Enable the text input.
289   // Creates a decorator.
290   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
291                                                        *controller );
292
293   // Enables the text input.
294   controller->EnableTextInput( decorator );
295
296   // Creates the text's popup.
297   TextSelectionPopupCallbackInterface& callbackInterface = *controller;
298   TextSelectionPopup textPopup = TextSelectionPopup::New( &callbackInterface );
299
300   Toolkit::TextSelectionPopup::Buttons buttonsToEnable = static_cast<Toolkit::TextSelectionPopup::Buttons>( TextSelectionPopup::CUT        |
301                                                                                                             TextSelectionPopup::COPY       |
302                                                                                                             TextSelectionPopup::PASTE      |
303                                                                                                             TextSelectionPopup::SELECT     |
304                                                                                                             TextSelectionPopup::SELECT_ALL |
305                                                                                                             TextSelectionPopup::CLIPBOARD );
306
307   textPopup.EnableButtons( buttonsToEnable );
308   Stage::GetCurrent().Add( textPopup );
309   textPopup.ShowPopup();
310
311   // Render and notify
312   application.SendNotification();
313   application.Render();
314
315   // Sets some text.
316   controller->SetText( "Hello world" );
317
318   // Select the whole text.
319   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_SELECT_ALL ) );
320   DALI_TEST_CHECK( button );
321
322   button.DoAction( "buttonClick", attributes );
323
324   // Call relayout to process the input events.
325   controller->Relayout( CONTROL_SIZE );
326
327   // Cut the text.
328   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CUT ) );
329   DALI_TEST_CHECK( button );
330
331   button.DoAction( "buttonClick", attributes );
332
333   // Force to update the model.
334   controller->GetNaturalSize();
335
336   controller->GetText( text );
337   DALI_TEST_CHECK( text.empty() );
338
339   // Set text again.
340   controller->SetText( "Hello world" );
341
342   // Select the whole text.
343   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_SELECT_ALL ) );
344   DALI_TEST_CHECK( button );
345
346   button.DoAction( "buttonClick", attributes );
347
348   // Call relayout to process the input events.
349   controller->Relayout( CONTROL_SIZE );
350
351   // Copy to the clipboard.
352   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_COPY ) );
353   DALI_TEST_CHECK( button );
354
355   button.DoAction( "buttonClick", attributes );
356
357   // Call relayout to process the input events.
358   controller->Relayout( CONTROL_SIZE );
359
360   // Cut the text.
361   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CUT ) );
362   DALI_TEST_CHECK( button );
363
364   button.DoAction( "buttonClick", attributes );
365
366   // Force to update the model.
367   controller->GetNaturalSize();
368
369   controller->GetText( text );
370   DALI_TEST_CHECK( text.empty() );
371
372   ClipboardEventNotifier clipboardEventNotifier = ClipboardEventNotifier::Get();
373   clipboardEventNotifier.ContentSelectedSignal().Connect( &ContentSelectedCallback );
374
375   // Paste the text.
376   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_PASTE ) );
377   DALI_TEST_CHECK( button );
378
379   button.DoAction( "buttonClick", attributes );
380
381   // Call relayout to process the input events.
382   controller->Relayout( CONTROL_SIZE );
383
384   DALI_TEST_EQUALS( "Hello world", gClipboardText, TEST_LOCATION );
385
386   // Show the clipboard.
387   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CLIPBOARD ) );
388   DALI_TEST_CHECK( button );
389
390   button.DoAction( "buttonClick", attributes );
391
392   tet_result(TET_PASS);
393   END_TEST;
394 }
395
396 int UtcDaliTextControllerGetInputShadowProperty(void)
397 {
398   tet_infoline(" UtcDaliTextControllerGetInputShadowProperty");
399   ToolkitTestApplication application;
400
401   // Creates a text controller.
402   ControllerPtr controller = Controller::New();
403
404   DALI_TEST_CHECK( controller );
405
406   const std::string& shadowProperties = controller->GetInputShadowProperties();
407
408   DALI_TEST_CHECK( shadowProperties.empty() );
409
410   tet_result(TET_PASS);
411   END_TEST;
412 }
413
414 int UtcDaliTextControllerGetInputUnderlineProperty(void)
415 {
416   tet_infoline(" UtcDaliTextControllerGetInputUnderlineProperty");
417   ToolkitTestApplication application;
418
419   // Creates a text controller.
420   ControllerPtr controller = Controller::New();
421
422   DALI_TEST_CHECK( controller );
423
424   const std::string& underlineProperties = controller->GetInputUnderlineProperties();
425
426   DALI_TEST_CHECK( underlineProperties.empty() );
427
428   tet_result(TET_PASS);
429   END_TEST;
430 }
431
432 int UtcDaliTextControllerSetGetAutoScrollEnabled(void)
433 {
434   tet_infoline(" UtcDaliTextControllerSetGetAutoScrollEnabled");
435   ToolkitTestApplication application;
436
437   // Creates a text controller.
438   ControllerPtr controller = Controller::New();
439
440   DALI_TEST_CHECK( controller );
441
442   DALI_TEST_CHECK( !controller->IsAutoScrollEnabled() );
443
444   // The auto scrolling shouldn't be enabled if the multi-line is enabled.
445
446   // Enable multi-line.
447   controller->SetMultiLineEnabled( true );
448
449   // Enable text scrolling.
450   controller->SetAutoScrollEnabled( true );
451
452   DALI_TEST_CHECK( !controller->IsAutoScrollEnabled() );
453
454   // Disable multi-line.
455   controller->SetMultiLineEnabled( false );
456
457   // Enable text scrolling.
458   controller->SetAutoScrollEnabled( true );
459
460   // Should be ebabled now.
461   DALI_TEST_CHECK( controller->IsAutoScrollEnabled() );
462
463   tet_result(TET_PASS);
464   END_TEST;
465 }
466
467 int UtcDaliTextControllerSetGetCheckProperty(void)
468 {
469   tet_infoline(" UtcDaliTextControllerSetGetCheckProperty");
470   ToolkitTestApplication application;
471
472   // Creates a text controller.
473   ControllerPtr controller = Controller::New();
474
475   DALI_TEST_CHECK( controller );
476
477   // Enable the text input.
478   // Creates a decorator.
479   Text::DecoratorPtr decorator = Text::Decorator::New( *controller, *controller );
480
481   // Enables the text input.
482   controller->EnableTextInput( decorator );
483
484   DALI_TEST_CHECK( !controller->IsInputModePassword() );
485
486   // Set the text input to password.
487   controller->SetInputModePassword( true );
488
489   DALI_TEST_CHECK( controller->IsInputModePassword() );
490
491   // Unset the text input to password.
492   controller->SetInputModePassword( false );
493
494   DALI_TEST_CHECK( !controller->IsInputModePassword() );
495
496   tet_result(TET_PASS);
497   END_TEST;
498 }
499
500 int UtcDaliTextControllerSetGetTapLongPressAction(void)
501 {
502   tet_infoline(" UtcDaliTextControllerSetGetTapLongPressAction");
503   ToolkitTestApplication application;
504
505   // Creates a text controller.
506   ControllerPtr controller = Controller::New();
507
508   DALI_TEST_CHECK( controller );
509
510   // Test first with no decorator.
511
512   DALI_TEST_EQUALS( Controller::NoTextTap::NO_ACTION, controller->GetNoTextDoubleTapAction(), TEST_LOCATION );
513   controller->SetNoTextDoubleTapAction( Controller::NoTextTap::HIGHLIGHT );
514   DALI_TEST_EQUALS( Controller::NoTextTap::NO_ACTION, controller->GetNoTextDoubleTapAction(), TEST_LOCATION );
515
516   DALI_TEST_EQUALS( Controller::NoTextTap::NO_ACTION, controller->GetNoTextLongPressAction(), TEST_LOCATION );
517   controller->SetNoTextLongPressAction( Controller::NoTextTap::HIGHLIGHT );
518   DALI_TEST_EQUALS( Controller::NoTextTap::NO_ACTION, controller->GetNoTextLongPressAction(), TEST_LOCATION );
519
520   // Add a decorator and re-test.
521
522   // Creates a decorator.
523   Text::DecoratorPtr decorator = Text::Decorator::New( *controller, *controller );
524
525   // Enables the text input.
526   controller->EnableTextInput( decorator );
527
528   DALI_TEST_EQUALS( Controller::NoTextTap::NO_ACTION, controller->GetNoTextDoubleTapAction(), TEST_LOCATION );
529   controller->SetNoTextDoubleTapAction( Controller::NoTextTap::HIGHLIGHT );
530   DALI_TEST_EQUALS( Controller::NoTextTap::HIGHLIGHT, controller->GetNoTextDoubleTapAction(), TEST_LOCATION );
531
532   DALI_TEST_EQUALS( Controller::NoTextTap::SHOW_SELECTION_POPUP, controller->GetNoTextLongPressAction(), TEST_LOCATION ); // The default is SHOW_SELECTION_POPUP
533   controller->SetNoTextLongPressAction( Controller::NoTextTap::HIGHLIGHT );
534   DALI_TEST_EQUALS( Controller::NoTextTap::HIGHLIGHT, controller->GetNoTextLongPressAction(), TEST_LOCATION );
535
536   END_TEST;
537 }