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