[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-toolkit.git] / automated-tests / dali-test-suite / text-input / utc-Dali-TextInput.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include <iostream>
18
19 #include <stdlib.h>
20 #include <tet_api.h>
21
22 #include <dali/public-api/dali-core.h>
23
24 #include <dali-toolkit/dali-toolkit.h>
25
26 #include <dali/integration-api/events/key-event-integ.h>
27
28
29 #include <dali-toolkit-test-suite-utils.h>
30
31 using namespace Dali;
32 using namespace Toolkit;
33
34 static void Startup();
35 static void Cleanup();
36
37 namespace
38 {
39 static bool gObjectCreatedCallBackCalled;
40
41 static void TestCallback(BaseHandle handle)
42 {
43   Actor actor = Actor::DownCast(handle);
44
45   if(actor)
46   {
47     TextInput handle = TextInput::DownCast(actor);
48      if (handle)
49      {
50        gObjectCreatedCallBackCalled = true;
51      }
52   }
53 }
54
55 } // namespace
56
57 extern "C" {
58   void (*tet_startup)() = Startup;
59   void (*tet_cleanup)() = Cleanup;
60 }
61 namespace
62 {
63 static bool gHasEndSignalBeenReceived;
64 static bool gHasStartSignalBeenReceived;
65 }
66
67 enum {
68   POSITIVE_TC_IDX = 0x01,
69   NEGATIVE_TC_IDX,
70 };
71
72 #define MAX_NUMBER_OF_TESTS 10000
73 extern "C" {
74   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
75 }
76
77
78 // Add test functionality for all APIs in the class (Positive and Negative)
79 TEST_FUNCTION( UtcDaliTextInputConstruction, POSITIVE_TC_IDX );
80 TEST_FUNCTION( UtcDaliTextInputDownCast, POSITIVE_TC_IDX );
81 TEST_FUNCTION( UtcDaliTextInputGetText, POSITIVE_TC_IDX );
82 TEST_FUNCTION( UtcDaliTextInputSetMaxCharacterLength, POSITIVE_TC_IDX );
83 TEST_FUNCTION( UtcDaliTextInputSetInitialText, POSITIVE_TC_IDX );
84 TEST_FUNCTION( UtcDaliTextInputAddChars, POSITIVE_TC_IDX );
85 TEST_FUNCTION( UtcDaliTextInputRemoveChars, POSITIVE_TC_IDX );
86 TEST_FUNCTION( UtcDaliTextInputEndSignalEmit, POSITIVE_TC_IDX );
87 TEST_FUNCTION( UtcDaliTextInputStartSignalEmit, POSITIVE_TC_IDX );
88 TEST_FUNCTION( UtcDaliTextInputExceedMaxCharactersInitial, POSITIVE_TC_IDX );
89 TEST_FUNCTION( UtcDaliTextInputExceedMaxCharacters, POSITIVE_TC_IDX );
90 TEST_FUNCTION( UtcDaliTextInputSetNumberOfLines, POSITIVE_TC_IDX );
91 TEST_FUNCTION( UtcDaliTextInputSetAndGetFadeBoundary, POSITIVE_TC_IDX );
92 TEST_FUNCTION( UtcDaliTextInputSetAndGetWidthExceedPolicy, POSITIVE_TC_IDX );
93 TEST_FUNCTION( UtcDaliTextInputSetAndGetHeightExceedPolicy, POSITIVE_TC_IDX );
94 TEST_FUNCTION( UtcDaliTextInputScroll, POSITIVE_TC_IDX );
95
96 // Called only once before first test is run.
97 static void Startup()
98 {
99 }
100
101 // Called only once after last test is run
102 static void Cleanup()
103 {
104 }
105
106
107 // Positive test case for a method
108 static void UtcDaliTextInputConstruction()
109 {
110   ToolkitTestApplication application;
111
112   tet_infoline("Testing New constructor");
113
114   TextInput textInput = TextInput::New();
115   DALI_TEST_CHECK(textInput);
116
117   //Additional check to ensure object is created by checking if it's registered
118   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
119   DALI_TEST_CHECK( registry );
120
121   gObjectCreatedCallBackCalled = false;
122   registry.ObjectCreatedSignal().Connect(&TestCallback);
123   {
124     TextInput textInput = TextInput::New();
125   }
126   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
127 }
128
129
130 static bool downCastToTextInput(Dali::Actor actor)
131 {
132   TextInput handle = TextInput::DownCast(actor);
133   if (handle)
134   {
135     tet_infoline("Downcasted to TextInput");
136     return true;
137   }
138   else
139   {
140     tet_infoline("Did not downcast to TextInput");
141     return false;
142   }
143 }
144
145 // Positive test case for a method
146 static void UtcDaliTextInputDownCast()
147 {
148   ToolkitTestApplication application;
149
150   TextInput textInput = TextInput::New();
151
152   tet_infoline("Testing Downcasting with a TextInput");
153   DALI_TEST_EQUALS(true,downCastToTextInput(textInput), TEST_LOCATION); // downcast a TextInput
154
155   Dali::TextActor badHandle = Dali::TextActor::New("test");
156
157   tet_infoline("Testing Downcasting with the wrong actor");
158   DALI_TEST_EQUALS(false, downCastToTextInput(badHandle), TEST_LOCATION); // downcast a TextActor to TextInput
159 }
160
161 // Positive test case for a method
162 static void UtcDaliTextInputGetText()
163 {
164   ToolkitTestApplication application;
165
166   tet_infoline("Testing GetText");
167
168   const std::string teststring = "test";
169
170   TextInput textInput = TextInput::New();  // create empty TextInput
171
172   DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION); // Get text which should be empty
173
174   textInput.SetInitialText(teststring);
175
176   DALI_TEST_EQUALS(teststring,textInput.GetText(), TEST_LOCATION); // Get text which should be test string
177
178 }
179
180 // Positive test case for a method
181 static void UtcDaliTextInputSetMaxCharacterLength()
182 {
183   ToolkitTestApplication application;
184
185   tet_infoline("Testing Setting of max characters");
186
187   const int maxChars = 4;
188   const char* testChar  = "v";
189
190   Dali::Integration::Core& core = application.GetCore();
191
192   TextInput textInput = TextInput::New();  // create empty TextInput
193
194   Stage::GetCurrent().Add(textInput);
195
196   textInput.SetKeyInputFocus();
197   textInput.SetMaxCharacterLength(maxChars);
198
199   Integration::KeyEvent event(testChar, testChar, 0, 0, 0, Integration::KeyEvent::Down );
200
201   std::string testString = "";
202   // Send max number of characters
203   for (int i=0; i < maxChars; i++)
204     {
205       core.SendEvent(event);
206       testString.append(testChar);
207     }
208
209   DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
210
211   core.SendEvent(event); // try to append additional character
212
213   DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
214
215   textInput.SetMaxCharacterLength(maxChars+1); // increment max characters by 1
216
217   core.SendEvent(event); // append additional character
218   testString.append(testChar);
219
220   DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
221 }
222
223 // Positive test case for a method
224 static void UtcDaliTextInputSetInitialText()
225 {
226   ToolkitTestApplication application;
227
228   tet_infoline("Testing Setting of Initial Text");
229
230   const std::string teststring = "test";
231
232   TextInput textInput = TextInput::New();  // create empty TextInput
233
234   textInput.SetInitialText(teststring);
235
236   DALI_TEST_EQUALS(teststring,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
237 }
238
239 static void UtcDaliTextInputAddChars()
240 {
241   ToolkitTestApplication application;
242
243   tet_infoline("Testing Adding characters");
244
245   Dali::Integration::Core& core = application.GetCore();
246
247   TextInput textInput = TextInput::New();  // create empty TextInput
248
249   Stage::GetCurrent().Add(textInput);
250
251   textInput.SetKeyInputFocus();
252
253   Integration::KeyEvent event("a", "a", 0, 0, 0, Integration::KeyEvent::Down);
254   core.SendEvent(event);
255
256   DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
257
258   Integration::KeyEvent event2("v", "v", 0, 0, 0, Integration::KeyEvent::Down);
259   core.SendEvent(event2);
260
261   DALI_TEST_EQUALS("av",textInput.GetText(), TEST_LOCATION); // Get text which should be "av"
262 }
263
264 static void UtcDaliTextInputRemoveChars()
265 {
266   ToolkitTestApplication application;
267
268   tet_infoline("Testing Removal of end characters");
269
270   Dali::Integration::Core& core = application.GetCore();
271
272   TextInput textInput = TextInput::New();  // create empty TextInput
273
274   Stage::GetCurrent().Add(textInput);
275
276   textInput.SetKeyInputFocus();
277
278   Integration::KeyEvent event("a", "a", 0, 0, 0, Integration::KeyEvent::Down);
279   core.SendEvent(event);
280
281   DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
282
283   Integration::KeyEvent event2("BackSpace", "", 0, 0, 0, Integration::KeyEvent::Down);
284   core.SendEvent(event2);
285
286   DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION); // Get text which should be ""
287
288   core.SendEvent(event);
289   core.SendEvent(event);
290
291   DALI_TEST_EQUALS("aa",textInput.GetText(), TEST_LOCATION); // Get text which should be "aa"
292
293   core.SendEvent(event2);
294
295   DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
296 }
297
298 // Callback test function
299 void OnEndInput(TextInput textInput)
300 {
301   gHasEndSignalBeenReceived = true;
302 }
303
304 static void UtcDaliTextInputEndSignalEmit()
305 {
306   ToolkitTestApplication application;
307
308   tet_infoline("Testing Set editable false emits end signal");
309
310   TextInput textInput = TextInput::New();  // create empty TextInput
311
312   Stage::GetCurrent().Add(textInput);
313
314   textInput.InputFinishedSignal().Connect( &OnEndInput );
315
316   textInput.SetEditable(true) ;
317
318   gHasEndSignalBeenReceived = false;
319
320   textInput.SetEditable(false) ;
321
322   DALI_TEST_EQUALS(true, gHasEndSignalBeenReceived, TEST_LOCATION);
323 }
324
325
326 // Callback test function
327 void OnStartInput(TextInput textInput)
328 {
329   gHasStartSignalBeenReceived = true;
330 }
331
332 static void UtcDaliTextInputStartSignalEmit()
333 {
334   ToolkitTestApplication application;
335
336   tet_infoline("Testing SetEditable emits start signal");
337
338   TextInput textInput = TextInput::New();  // create empty TextInput
339
340   Stage::GetCurrent().Add(textInput);
341
342   textInput.InputStartedSignal().Connect( &OnStartInput );
343
344   gHasStartSignalBeenReceived = false;
345
346   textInput.SetEditable(true);  // Set editable first time
347
348   DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
349
350   gHasStartSignalBeenReceived = false;
351
352   textInput.SetEditable(true); // Set editable second time, signal should not be sent again.
353
354   DALI_TEST_EQUALS(false, gHasStartSignalBeenReceived, TEST_LOCATION);
355
356   textInput.SetEditable(false);
357
358   gHasStartSignalBeenReceived = false;
359
360   textInput.SetEditable(true,Vector2(3.f,2.f));  // Set editable again
361
362   DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
363 }
364
365 static void UtcDaliTextInputExceedMaxCharactersInitial()
366 {
367   ToolkitTestApplication application;
368
369   tet_infoline("Testing Setting Initial Text obeys Max Character Limit");
370
371   TextInput textInput = TextInput::New();  // create empty TextInput
372
373   Stage::GetCurrent().Add(textInput);
374
375   textInput.SetMaxCharacterLength(4);
376
377   textInput.SetInitialText("TooBig");
378
379   tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
380
381   DALI_TEST_EQUALS("TooB",textInput.GetText(), TEST_LOCATION); // Get text which should be only 4 characters
382 }
383
384
385 static void UtcDaliTextInputExceedMaxCharacters()
386 {
387   ToolkitTestApplication application;
388
389   Dali::Integration::Core& core = application.GetCore();
390
391   tet_infoline("Testing Max characters is obeyed when inputting key events ");
392
393   TextInput textInput = TextInput::New();  // create empty TextInput
394
395   Stage::GetCurrent().Add(textInput);
396
397   textInput.SetMaxCharacterLength(4);
398
399   textInput.SetInitialText("");
400
401   textInput.SetEditable(true);
402
403   Integration::KeyEvent eventA("a", "a", 0, 0, 0, Integration::KeyEvent::Down );
404   Integration::KeyEvent eventB("b", "b", 0, 0, 0, Integration::KeyEvent::Down );
405
406   core.SendEvent(eventA);
407   core.SendEvent(eventB);
408   core.SendEvent(eventA);
409   core.SendEvent(eventB);
410
411   core.SendEvent(eventA);
412   core.SendEvent(eventB);
413
414   tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
415
416   DALI_TEST_EQUALS("abab",textInput.GetText(), TEST_LOCATION); // Get text which should be only 4 characters
417 }
418
419 static void UtcDaliTextInputSetNumberOfLines()
420 {
421   ToolkitTestApplication application;
422
423   tet_infoline("Ensuring API for setting and getting max number of lines is correct");
424
425   TextInput textInput = TextInput::New();  // create empty TextInput
426
427   unsigned int numberOfLines = 1;
428
429   textInput.SetNumberOfLinesLimit( numberOfLines );
430
431   DALI_TEST_EQUALS(numberOfLines ,textInput.GetNumberOfLinesLimit(),  TEST_LOCATION);
432 }
433
434 static void UtcDaliTextInputSetAndGetFadeBoundary()
435 {
436   tet_infoline("UtcDaliTextViewSetAndGetFadeBoundary: ");
437
438   ToolkitTestApplication application;
439
440   TextView::FadeBoundary fadeBoundary( PixelSize( 0 ), PixelSize( 20 ), PixelSize( 0 ), PixelSize( 10 ) );
441
442   TextInput textInput = TextInput::New();
443   textInput.SetInitialText( "Hello world!" );
444
445   textInput.SetFadeBoundary( fadeBoundary );
446
447   TextView::FadeBoundary fadeBoundary2 = textInput.GetFadeBoundary();
448
449   DALI_TEST_EQUALS( fadeBoundary.mLeft, fadeBoundary2.mLeft, TEST_LOCATION );
450   DALI_TEST_EQUALS( fadeBoundary.mRight, fadeBoundary2.mRight, TEST_LOCATION );
451   DALI_TEST_EQUALS( fadeBoundary.mTop, fadeBoundary2.mTop, TEST_LOCATION );
452   DALI_TEST_EQUALS( fadeBoundary.mBottom, fadeBoundary2.mBottom, TEST_LOCATION );
453 }
454
455 static void UtcDaliTextInputSetAndGetWidthExceedPolicy()
456 {
457   ToolkitTestApplication application;
458
459   tet_infoline("UtcDaliTextInputSetAndGetWidthExceedPolicy: ");
460
461   const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::Split, TextView::ShrinkToFit };
462   const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
463
464   TextInput textInput = TextInput::New();
465   textInput.SetInitialText( "Hello world!" );
466
467   for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
468   {
469     textInput.SetWidthExceedPolicy( EXCEED_POLICIES[epIndex] );
470
471     DALI_TEST_EQUALS( textInput.GetWidthExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
472   }
473 }
474
475 static void UtcDaliTextInputSetAndGetHeightExceedPolicy()
476 {
477   ToolkitTestApplication application;
478
479   tet_infoline("UtcDaliTextInputSetAndGetHeightExceedPolicy: ");
480
481   const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::ShrinkToFit };
482   const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
483
484   TextInput textInput = TextInput::New();
485   textInput.SetInitialText( "Hello world!" );
486
487   for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
488   {
489     textInput.SetHeightExceedPolicy( EXCEED_POLICIES[epIndex] );
490
491     DALI_TEST_EQUALS( textInput.GetHeightExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
492   }
493 }
494
495 static void UtcDaliTextInputScroll()
496 {
497   tet_infoline("UtcDaliTextInputScroll: ");
498   ToolkitTestApplication application;
499
500   // Avoids the frame buffer texture to throw an exception.
501   application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
502
503   TextInput view = TextInput::New();
504   view.SetMultilinePolicy( TextView::SplitByNewLineChar );
505   view.SetWidthExceedPolicy( TextView::Original );
506   view.SetHeightExceedPolicy( TextView::Original );
507   view.SetTextAlignment( static_cast<Toolkit::Alignment::Type>( Toolkit::Alignment::HorizontalCenter | Toolkit::Alignment::VerticalCenter ) );
508   view.SetInitialText( "Hello world! This is a scroll test." );
509   view.SetSize( 100.f, 100.f );
510   view.SetSnapshotModeEnabled( false );
511
512   Stage::GetCurrent().Add( view );
513
514   application.SendNotification();
515   application.Render();
516
517   DALI_TEST_CHECK( !view.IsScrollEnabled() ); // Scroll should be disabled by default.
518
519   view.SetScrollEnabled( true );
520
521   DALI_TEST_CHECK( view.IsScrollEnabled() );
522   DALI_TEST_CHECK( view.IsSnapshotModeEnabled() ); // Scroll should enable snapshot mode.
523
524   view.SetScrollPosition( Vector2( 400.f, 400.f ) );
525
526   application.SendNotification();
527   application.Render();
528
529   const Vector2& scrollPosition = view.GetScrollPosition();
530   DALI_TEST_EQUALS( scrollPosition, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
531 }