Merge "Add Privilege tags to dali.doxy.in" 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
23 #include <dali-toolkit-test-suite-utils.h>
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/internal/text/text-controller.h>
26
27 using namespace Dali;
28 using namespace Toolkit;
29 using namespace Text;
30
31 namespace
32 {
33
34 const char* const OPTION_SELECT_WORD("option-select_word"); // "Select Word" popup option.
35 const char* const OPTION_SELECT_ALL("option-select_all");   // "Select All" popup option.
36 const char* const OPTION_CUT("optionCut");                  // "Cut" popup option.
37 const char* const OPTION_COPY("optionCopy");                // "Copy" popup option.
38 const char* const OPTION_PASTE("optionPaste");              // "Paste" popup option.
39 const char* const OPTION_CLIPBOARD("optionClipboard");      // "Clipboard" popup option.
40
41 const Size CONTROL_SIZE( 300.f, 60.f );
42
43 class ControlImpl : public ControlInterface
44 {
45 public:
46   ControlImpl()
47   : ControlInterface()
48   {}
49
50   virtual ~ControlImpl()
51   {}
52
53   virtual void AddDecoration( Actor& actor, bool needsClipping )
54   {}
55
56   virtual void RequestTextRelayout()
57   {}
58
59   virtual void TextChanged()
60   {}
61
62   virtual void MaxLengthReached()
63   {}
64
65   virtual void InputStyleChanged( InputStyle::Mask inputStyleMask )
66   {}
67 };
68
69 std::string gClipboardText;
70 void ContentSelectedCallback( ClipboardEventNotifier& notifier )
71 {
72   gClipboardText = notifier.GetContent();
73 }
74
75 } // namespace
76
77 int UtcDaliTextController(void)
78 {
79   tet_infoline(" UtcDaliTextController");
80   ToolkitTestApplication application;
81
82   // Creates a text controller.
83   ControlImpl controlImpl;
84   ControllerPtr controller = Controller::New( controlImpl );
85
86   DALI_TEST_CHECK( controller );
87
88   tet_result(TET_PASS);
89   END_TEST;
90 }
91
92 int UtcDaliTextControllerEnableCursorBlinking(void)
93 {
94   tet_infoline(" UtcDaliTextControllerEnableCursorBlinking");
95   ToolkitTestApplication application;
96
97   // Creates a text controller.
98   ControlImpl controlImpl;
99   ControllerPtr controller = Controller::New( controlImpl );
100
101   DALI_TEST_CHECK( controller );
102
103   // There is no text input enabled.
104   DALI_TEST_CHECK( !controller->GetEnableCursorBlink() );
105
106   // Enable the text input.
107   // Creates a decorator.
108   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
109                                                        *controller );
110
111   // Enables the text input.
112   controller->EnableTextInput( decorator );
113
114   // Enables the cursor blink.
115   controller->SetEnableCursorBlink( true );
116
117   DALI_TEST_CHECK( controller->GetEnableCursorBlink() );
118
119   // Disables the cursor blink.
120   controller->SetEnableCursorBlink( false );
121
122   DALI_TEST_CHECK( !controller->GetEnableCursorBlink() );
123
124   tet_result(TET_PASS);
125   END_TEST;
126 }
127
128 int UtcDaliTextControllerImfEvent(void)
129 {
130   tet_infoline(" UtcDaliTextController");
131   ToolkitTestApplication application;
132
133   // Creates a text controller.
134   ControlImpl controlImpl;
135   ControllerPtr controller = Controller::New( controlImpl );
136
137   std::string text;
138   ImfManager::ImfEventData imfEvent;
139
140   DALI_TEST_CHECK( controller );
141
142   // Enable the text input.
143   // Creates a decorator.
144   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
145                                                        *controller );
146
147   // Enables the text input.
148   controller->EnableTextInput( decorator );
149
150   // Creates an ImfManager.
151   ImfManager imfManager = ImfManager::Get();
152
153   // Send VOID event.
154   imfEvent = ImfManager::ImfEventData( ImfManager::VOID, "", 0, 0 );
155   controller->OnImfEvent( imfManager, imfEvent );
156
157   controller->GetText( text );
158   DALI_TEST_CHECK( text.empty() );
159
160   // Send COMMIT event.
161   imfEvent = ImfManager::ImfEventData( ImfManager::COMMIT, "Hello ", 0, 6 );
162   controller->OnImfEvent( imfManager, imfEvent );
163
164   // Force to update the model.
165   controller->GetNaturalSize();
166
167   controller->GetText( text );
168   DALI_TEST_EQUALS( "Hello ", text, TEST_LOCATION );
169
170   // Send PREEDIT event
171   imfEvent = ImfManager::ImfEventData( ImfManager::PREEDIT, "w", 6, 1 );
172   controller->OnImfEvent( imfManager, imfEvent );
173
174   // Force to update the model.
175   controller->GetNaturalSize();
176
177   controller->GetText( text );
178   DALI_TEST_EQUALS( "Hello w", text, TEST_LOCATION );
179
180   // Send DELETESURROUNDING event
181   imfEvent = ImfManager::ImfEventData( ImfManager::DELETESURROUNDING, "", -1, 1 );
182   controller->OnImfEvent( imfManager, imfEvent );
183
184   // Force to update the model.
185   controller->GetNaturalSize();
186
187   controller->GetText( text );
188   DALI_TEST_EQUALS( "Hello ", text, TEST_LOCATION );
189
190   // Send PREEDIT event
191   imfEvent = ImfManager::ImfEventData( ImfManager::PREEDIT, "wo", 6, 2 );
192   controller->OnImfEvent( imfManager, imfEvent );
193
194   // Force to update the model.
195   controller->GetNaturalSize();
196
197   controller->GetText( text );
198   DALI_TEST_EQUALS( "Hello wo", text, TEST_LOCATION );
199
200   // Send GETSURROUNDING event
201   imfEvent = ImfManager::ImfEventData( ImfManager::GETSURROUNDING, "", 0, 0 );
202   controller->OnImfEvent( imfManager, imfEvent );
203
204   controller->GetText( text );
205   DALI_TEST_EQUALS( "Hello wo", text, TEST_LOCATION );
206
207   tet_result(TET_PASS);
208   END_TEST;
209 }
210
211 int UtcDaliTextControllerTextPopupButtonTouched(void)
212 {
213   tet_infoline(" UtcDaliTextControllerTextPopupButtonTouched");
214   ToolkitTestApplication application;
215
216   // Creates a text controller.
217   ControlImpl controlImpl;
218   ControllerPtr controller = Controller::New( controlImpl );
219
220   DALI_TEST_CHECK( controller );
221
222   std::string text;
223   PushButton button;
224   Property::Map attributes;
225
226   // Enable the text input.
227   // Creates a decorator.
228   Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
229                                                        *controller );
230
231   // Enables the text input.
232   controller->EnableTextInput( decorator );
233
234   // Creates the text's popup.
235   TextSelectionPopupCallbackInterface& callbackInterface = *controller;
236   TextSelectionPopup textPopup = TextSelectionPopup::New( &callbackInterface );
237
238   Toolkit::TextSelectionPopup::Buttons buttonsToEnable = static_cast<Toolkit::TextSelectionPopup::Buttons>( TextSelectionPopup::CUT        |
239                                                                                                             TextSelectionPopup::COPY       |
240                                                                                                             TextSelectionPopup::PASTE      |
241                                                                                                             TextSelectionPopup::SELECT     |
242                                                                                                             TextSelectionPopup::SELECT_ALL |
243                                                                                                             TextSelectionPopup::CLIPBOARD );
244
245   textPopup.EnableButtons( buttonsToEnable );
246   Stage::GetCurrent().Add( textPopup );
247   textPopup.ShowPopup();
248
249   // Render and notify
250   application.SendNotification();
251   application.Render();
252
253   // Sets some text.
254   controller->SetText( "Hello world" );
255
256   // Select the whole text.
257   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_SELECT_ALL ) );
258   DALI_TEST_CHECK( button );
259
260   button.DoAction( "buttonClick", attributes );
261
262   // Call relayout to process the input events.
263   controller->Relayout( CONTROL_SIZE );
264
265   // Cut the text.
266   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CUT ) );
267   DALI_TEST_CHECK( button );
268
269   button.DoAction( "buttonClick", attributes );
270
271   // Force to update the model.
272   controller->GetNaturalSize();
273
274   controller->GetText( text );
275   DALI_TEST_CHECK( text.empty() );
276
277   // Set text again.
278   controller->SetText( "Hello world" );
279
280   // Select the whole text.
281   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_SELECT_ALL ) );
282   DALI_TEST_CHECK( button );
283
284   button.DoAction( "buttonClick", attributes );
285
286   // Call relayout to process the input events.
287   controller->Relayout( CONTROL_SIZE );
288
289   // Copy to the clipboard.
290   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_COPY ) );
291   DALI_TEST_CHECK( button );
292
293   button.DoAction( "buttonClick", attributes );
294
295   // Call relayout to process the input events.
296   controller->Relayout( CONTROL_SIZE );
297
298   // Cut the text.
299   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CUT ) );
300   DALI_TEST_CHECK( button );
301
302   button.DoAction( "buttonClick", attributes );
303
304   // Force to update the model.
305   controller->GetNaturalSize();
306
307   controller->GetText( text );
308   DALI_TEST_CHECK( text.empty() );
309
310   ClipboardEventNotifier clipboardEventNotifier = ClipboardEventNotifier::Get();
311   clipboardEventNotifier.ContentSelectedSignal().Connect( &ContentSelectedCallback );
312
313   // Paste the text.
314   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_PASTE ) );
315   DALI_TEST_CHECK( button );
316
317   button.DoAction( "buttonClick", attributes );
318
319   // Call relayout to process the input events.
320   controller->Relayout( CONTROL_SIZE );
321
322   DALI_TEST_EQUALS( "Hello world", gClipboardText, TEST_LOCATION );
323
324   // Show the clipboard.
325   button = PushButton::DownCast( textPopup.FindChildByName( OPTION_CLIPBOARD ) );
326   DALI_TEST_CHECK( button );
327
328   button.DoAction( "buttonClick", attributes );
329
330   tet_result(TET_PASS);
331   END_TEST;
332 }