Merge "Remove ResourceImages from Popup implementation" into devel/master
[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 <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 const std::string DEFAULT_FONT_DIR( "/resources/fonts" );
47
48 class ControlImpl : public ControlInterface, public Text::EditableControlInterface
49 {
50 public:
51   ControlImpl()
52   : ControlInterface()
53   {}
54
55   virtual ~ControlImpl()
56   {}
57
58   virtual void AddDecoration( Actor& actor, bool needsClipping )
59   {}
60
61   virtual void RequestTextRelayout()
62   {}
63
64   virtual void TextChanged()
65   {}
66
67   virtual void MaxLengthReached()
68   {}
69
70   virtual void InputStyleChanged( InputStyle::Mask inputStyleMask )
71   {}
72 };
73
74 std::string gClipboardText;
75 void ContentSelectedCallback( ClipboardEventNotifier& notifier )
76 {
77   gClipboardText = notifier.GetContent();
78 }
79
80 } // namespace
81
82 int UtcDaliTextController(void)
83 {
84   tet_infoline(" UtcDaliTextController");
85   ToolkitTestApplication application;
86
87   // Creates a text controller.
88   ControllerPtr controller = Controller::New();
89
90   DALI_TEST_CHECK( controller );
91
92   tet_result(TET_PASS);
93   END_TEST;
94 }
95
96 int UtcDaliTextControllerEnableCursorBlinking(void)
97 {
98   tet_infoline(" UtcDaliTextControllerEnableCursorBlinking");
99   ToolkitTestApplication application;
100
101   // Creates a text controller.
102   ControllerPtr controller = Controller::New();
103
104   DALI_TEST_CHECK( controller );
105
106   // There is no text input enabled.
107   DALI_TEST_CHECK( !controller->GetEnableCursorBlink() );
108
109   // Enable the text input.
110   // Creates a decorator.
111   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
112                                                        *controller );
113
114   // Enables the text input.
115   controller->EnableTextInput( decorator );
116
117   // Enables the cursor blink.
118   controller->SetEnableCursorBlink( true );
119
120   DALI_TEST_CHECK( controller->GetEnableCursorBlink() );
121
122   // Disables the cursor blink.
123   controller->SetEnableCursorBlink( false );
124
125   DALI_TEST_CHECK( !controller->GetEnableCursorBlink() );
126
127   tet_result(TET_PASS);
128   END_TEST;
129 }
130
131 int UtcDaliTextControllerImfEvent(void)
132 {
133   tet_infoline(" UtcDaliTextController");
134   ToolkitTestApplication application;
135
136   // Creates a text controller.
137   ControllerPtr controller = Controller::New();
138
139   std::string text;
140   ImfManager::ImfEventData imfEvent;
141
142   DALI_TEST_CHECK( controller );
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   // Creates an ImfManager.
153   ImfManager imfManager = ImfManager::Get();
154
155   // Send VOID event.
156   imfEvent = ImfManager::ImfEventData( ImfManager::VOID, "", 0, 0 );
157   controller->OnImfEvent( imfManager, imfEvent );
158
159   controller->GetText( text );
160   DALI_TEST_CHECK( text.empty() );
161
162   // Send COMMIT event.
163   imfEvent = ImfManager::ImfEventData( ImfManager::COMMIT, "Hello ", 0, 6 );
164   controller->OnImfEvent( imfManager, imfEvent );
165
166   // Force to update the model.
167   controller->GetNaturalSize();
168
169   controller->GetText( text );
170   DALI_TEST_EQUALS( "Hello ", text, TEST_LOCATION );
171
172   // Send PREEDIT event
173   imfEvent = ImfManager::ImfEventData( ImfManager::PREEDIT, "w", 6, 1 );
174   controller->OnImfEvent( imfManager, imfEvent );
175
176   // Force to update the model.
177   controller->GetNaturalSize();
178
179   controller->GetText( text );
180   DALI_TEST_EQUALS( "Hello w", text, TEST_LOCATION );
181
182   // Send DELETESURROUNDING event
183   imfEvent = ImfManager::ImfEventData( ImfManager::DELETESURROUNDING, "", -1, 1 );
184   controller->OnImfEvent( imfManager, imfEvent );
185
186   // Force to update the model.
187   controller->GetNaturalSize();
188
189   controller->GetText( text );
190   DALI_TEST_EQUALS( "Hello ", text, TEST_LOCATION );
191
192   // Send PREEDIT event
193   imfEvent = ImfManager::ImfEventData( ImfManager::PREEDIT, "wo", 6, 2 );
194   controller->OnImfEvent( imfManager, imfEvent );
195
196   // Force to update the model.
197   controller->GetNaturalSize();
198
199   controller->GetText( text );
200   DALI_TEST_EQUALS( "Hello wo", text, TEST_LOCATION );
201
202   // Send GETSURROUNDING event
203   imfEvent = ImfManager::ImfEventData( ImfManager::GETSURROUNDING, "", 0, 0 );
204   controller->OnImfEvent( imfManager, imfEvent );
205
206   controller->GetText( text );
207   DALI_TEST_EQUALS( "Hello wo", text, TEST_LOCATION );
208
209   tet_result(TET_PASS);
210   END_TEST;
211 }
212
213 int UtcDaliTextControllerTextPopupButtonTouched(void)
214 {
215   tet_infoline(" UtcDaliTextControllerTextPopupButtonTouched");
216   ToolkitTestApplication application;
217
218   // Creates a text controller.
219   ControllerPtr controller = Controller::New();
220
221   DALI_TEST_CHECK( controller );
222
223   std::string text;
224   PushButton button;
225   Property::Map attributes;
226
227   // Enable the text input.
228   // Creates a decorator.
229   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
230                                                        *controller );
231
232   // Enables the text input.
233   controller->EnableTextInput( decorator );
234
235   // Creates the text's popup.
236   TextSelectionPopupCallbackInterface& callbackInterface = *controller;
237   TextSelectionPopup textPopup = TextSelectionPopup::New( &callbackInterface );
238
239   Toolkit::TextSelectionPopup::Buttons buttonsToEnable = static_cast<Toolkit::TextSelectionPopup::Buttons>( TextSelectionPopup::CUT        |
240                                                                                                             TextSelectionPopup::COPY       |
241                                                                                                             TextSelectionPopup::PASTE      |
242                                                                                                             TextSelectionPopup::SELECT     |
243                                                                                                             TextSelectionPopup::SELECT_ALL |
244                                                                                                             TextSelectionPopup::CLIPBOARD );
245
246   textPopup.EnableButtons( buttonsToEnable );
247   Stage::GetCurrent().Add( textPopup );
248   textPopup.ShowPopup();
249
250   // Render and notify
251   application.SendNotification();
252   application.Render();
253
254   // Sets some text.
255   controller->SetText( "Hello world" );
256
257   // Select the whole text.
258   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_SELECT_ALL ) );
259   DALI_TEST_CHECK( button );
260
261   button.DoAction( "buttonClick", attributes );
262
263   // Call relayout to process the input events.
264   controller->Relayout( CONTROL_SIZE );
265
266   // Cut the text.
267   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CUT ) );
268   DALI_TEST_CHECK( button );
269
270   button.DoAction( "buttonClick", attributes );
271
272   // Force to update the model.
273   controller->GetNaturalSize();
274
275   controller->GetText( text );
276   DALI_TEST_CHECK( text.empty() );
277
278   // Set text again.
279   controller->SetText( "Hello world" );
280
281   // Select the whole text.
282   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_SELECT_ALL ) );
283   DALI_TEST_CHECK( button );
284
285   button.DoAction( "buttonClick", attributes );
286
287   // Call relayout to process the input events.
288   controller->Relayout( CONTROL_SIZE );
289
290   // Copy to the clipboard.
291   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_COPY ) );
292   DALI_TEST_CHECK( button );
293
294   button.DoAction( "buttonClick", attributes );
295
296   // Call relayout to process the input events.
297   controller->Relayout( CONTROL_SIZE );
298
299   // Cut the text.
300   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CUT ) );
301   DALI_TEST_CHECK( button );
302
303   button.DoAction( "buttonClick", attributes );
304
305   // Force to update the model.
306   controller->GetNaturalSize();
307
308   controller->GetText( text );
309   DALI_TEST_CHECK( text.empty() );
310
311   ClipboardEventNotifier clipboardEventNotifier = ClipboardEventNotifier::Get();
312   clipboardEventNotifier.ContentSelectedSignal().Connect( &ContentSelectedCallback );
313
314   // Paste the text.
315   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_PASTE ) );
316   DALI_TEST_CHECK( button );
317
318   button.DoAction( "buttonClick", attributes );
319
320   // Call relayout to process the input events.
321   controller->Relayout( CONTROL_SIZE );
322
323   DALI_TEST_EQUALS( "Hello world", gClipboardText, TEST_LOCATION );
324
325   // Show the clipboard.
326   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CLIPBOARD ) );
327   DALI_TEST_CHECK( button );
328
329   button.DoAction( "buttonClick", attributes );
330
331   tet_result(TET_PASS);
332   END_TEST;
333 }
334
335 int UtcDaliTextControllerGetInputShadowProperty(void)
336 {
337   tet_infoline(" UtcDaliTextControllerGetInputShadowProperty");
338   ToolkitTestApplication application;
339
340   // Creates a text controller.
341   ControllerPtr controller = Controller::New();
342
343   DALI_TEST_CHECK( controller );
344
345   const std::string& shadowProperties = controller->GetInputShadowProperties();
346
347   DALI_TEST_CHECK( shadowProperties.empty() );
348
349   tet_result(TET_PASS);
350   END_TEST;
351 }
352
353 int UtcDaliTextControllerGetInputUnderlineProperty(void)
354 {
355   tet_infoline(" UtcDaliTextControllerGetInputUnderlineProperty");
356   ToolkitTestApplication application;
357
358   // Creates a text controller.
359   ControllerPtr controller = Controller::New();
360
361   DALI_TEST_CHECK( controller );
362
363   const std::string& underlineProperties = controller->GetInputUnderlineProperties();
364
365   DALI_TEST_CHECK( underlineProperties.empty() );
366
367   tet_result(TET_PASS);
368   END_TEST;
369 }
370
371 int UtcDaliTextControllerSetGetAutoScrollEnabled(void)
372 {
373   tet_infoline(" UtcDaliTextControllerSetGetAutoScrollEnabled");
374   ToolkitTestApplication application;
375
376   // Creates a text controller.
377   ControllerPtr controller = Controller::New();
378
379   DALI_TEST_CHECK( controller );
380
381   DALI_TEST_CHECK( !controller->IsAutoScrollEnabled() );
382
383   // The auto scrolling shouldn't be enabled if the multi-line is enabled.
384
385   // Enable multi-line.
386   controller->SetMultiLineEnabled( true );
387
388   // Enable text scrolling.
389   controller->SetAutoScrollEnabled( true );
390
391   DALI_TEST_CHECK( !controller->IsAutoScrollEnabled() );
392
393   // Disable multi-line.
394   controller->SetMultiLineEnabled( false );
395
396   // Enable text scrolling.
397   controller->SetAutoScrollEnabled( true );
398
399   // Should be ebabled now.
400   DALI_TEST_CHECK( controller->IsAutoScrollEnabled() );
401
402   tet_result(TET_PASS);
403   END_TEST;
404 }