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