Remove redundant Core::SendEvent() & NotificationEvent
[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   TextInput textInput = TextInput::New();  // create empty TextInput
191
192   Stage::GetCurrent().Add(textInput);
193
194   textInput.SetKeyInputFocus();
195   textInput.SetMaxCharacterLength(maxChars);
196
197   Integration::KeyEvent event(testChar, testChar, 0, 0, 0, Integration::KeyEvent::Down );
198
199   std::string testString = "";
200   // Send max number of characters
201   for (int i=0; i < maxChars; i++)
202     {
203       application.ProcessEvent(event);
204       testString.append(testChar);
205     }
206
207   DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
208
209   application.ProcessEvent(event); // try to append additional character
210
211   DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
212
213   textInput.SetMaxCharacterLength(maxChars+1); // increment max characters by 1
214
215   application.ProcessEvent(event); // append additional character
216   testString.append(testChar);
217
218   DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
219 }
220
221 // Positive test case for a method
222 static void UtcDaliTextInputSetInitialText()
223 {
224   ToolkitTestApplication application;
225
226   tet_infoline("Testing Setting of Initial Text");
227
228   const std::string teststring = "test";
229
230   TextInput textInput = TextInput::New();  // create empty TextInput
231
232   textInput.SetInitialText(teststring);
233
234   DALI_TEST_EQUALS(teststring,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
235 }
236
237 static void UtcDaliTextInputAddChars()
238 {
239   ToolkitTestApplication application;
240
241   tet_infoline("Testing Adding characters");
242
243   TextInput textInput = TextInput::New();  // create empty TextInput
244
245   Stage::GetCurrent().Add(textInput);
246
247   textInput.SetKeyInputFocus();
248
249   Integration::KeyEvent event("a", "a", 0, 0, 0, Integration::KeyEvent::Down);
250   application.ProcessEvent(event);
251
252   DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
253
254   Integration::KeyEvent event2("v", "v", 0, 0, 0, Integration::KeyEvent::Down);
255   application.ProcessEvent(event2);
256
257   DALI_TEST_EQUALS("av",textInput.GetText(), TEST_LOCATION); // Get text which should be "av"
258 }
259
260 static void UtcDaliTextInputRemoveChars()
261 {
262   ToolkitTestApplication application;
263
264   tet_infoline("Testing Removal of end characters");
265
266   TextInput textInput = TextInput::New();  // create empty TextInput
267
268   Stage::GetCurrent().Add(textInput);
269
270   textInput.SetKeyInputFocus();
271
272   Integration::KeyEvent event("a", "a", 0, 0, 0, Integration::KeyEvent::Down);
273   application.ProcessEvent(event);
274
275   DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
276
277   Integration::KeyEvent event2("BackSpace", "", 0, 0, 0, Integration::KeyEvent::Down);
278   application.ProcessEvent(event2);
279
280   DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION); // Get text which should be ""
281
282   application.ProcessEvent(event);
283   application.ProcessEvent(event);
284
285   DALI_TEST_EQUALS("aa",textInput.GetText(), TEST_LOCATION); // Get text which should be "aa"
286
287   application.ProcessEvent(event2);
288
289   DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
290 }
291
292 // Callback test function
293 void OnEndInput(TextInput textInput)
294 {
295   gHasEndSignalBeenReceived = true;
296 }
297
298 static void UtcDaliTextInputEndSignalEmit()
299 {
300   ToolkitTestApplication application;
301
302   tet_infoline("Testing Set editable false emits end signal");
303
304   TextInput textInput = TextInput::New();  // create empty TextInput
305
306   Stage::GetCurrent().Add(textInput);
307
308   textInput.InputFinishedSignal().Connect( &OnEndInput );
309
310   textInput.SetEditable(true) ;
311
312   gHasEndSignalBeenReceived = false;
313
314   textInput.SetEditable(false) ;
315
316   DALI_TEST_EQUALS(true, gHasEndSignalBeenReceived, TEST_LOCATION);
317 }
318
319
320 // Callback test function
321 void OnStartInput(TextInput textInput)
322 {
323   gHasStartSignalBeenReceived = true;
324 }
325
326 static void UtcDaliTextInputStartSignalEmit()
327 {
328   ToolkitTestApplication application;
329
330   tet_infoline("Testing SetEditable emits start signal");
331
332   TextInput textInput = TextInput::New();  // create empty TextInput
333
334   Stage::GetCurrent().Add(textInput);
335
336   textInput.InputStartedSignal().Connect( &OnStartInput );
337
338   gHasStartSignalBeenReceived = false;
339
340   textInput.SetEditable(true);  // Set editable first time
341
342   DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
343
344   gHasStartSignalBeenReceived = false;
345
346   textInput.SetEditable(true); // Set editable second time, signal should not be sent again.
347
348   DALI_TEST_EQUALS(false, gHasStartSignalBeenReceived, TEST_LOCATION);
349
350   textInput.SetEditable(false);
351
352   gHasStartSignalBeenReceived = false;
353
354   textInput.SetEditable(true,Vector2(3.f,2.f));  // Set editable again
355
356   DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
357 }
358
359 static void UtcDaliTextInputExceedMaxCharactersInitial()
360 {
361   ToolkitTestApplication application;
362
363   tet_infoline("Testing Setting Initial Text obeys Max Character Limit");
364
365   TextInput textInput = TextInput::New();  // create empty TextInput
366
367   Stage::GetCurrent().Add(textInput);
368
369   textInput.SetMaxCharacterLength(4);
370
371   textInput.SetInitialText("TooBig");
372
373   tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
374
375   DALI_TEST_EQUALS("TooB",textInput.GetText(), TEST_LOCATION); // Get text which should be only 4 characters
376 }
377
378
379 static void UtcDaliTextInputExceedMaxCharacters()
380 {
381   ToolkitTestApplication application;
382
383   tet_infoline("Testing Max characters is obeyed when inputting key events ");
384
385   TextInput textInput = TextInput::New();  // create empty TextInput
386
387   Stage::GetCurrent().Add(textInput);
388
389   textInput.SetMaxCharacterLength(4);
390
391   textInput.SetInitialText("");
392
393   textInput.SetEditable(true);
394
395   Integration::KeyEvent eventA("a", "a", 0, 0, 0, Integration::KeyEvent::Down );
396   Integration::KeyEvent eventB("b", "b", 0, 0, 0, Integration::KeyEvent::Down );
397
398   application.ProcessEvent(eventA);
399   application.ProcessEvent(eventB);
400   application.ProcessEvent(eventA);
401   application.ProcessEvent(eventB);
402
403   application.ProcessEvent(eventA);
404   application.ProcessEvent(eventB);
405
406   tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
407
408   DALI_TEST_EQUALS("abab",textInput.GetText(), TEST_LOCATION); // Get text which should be only 4 characters
409 }
410
411 static void UtcDaliTextInputSetNumberOfLines()
412 {
413   ToolkitTestApplication application;
414
415   tet_infoline("Ensuring API for setting and getting max number of lines is correct");
416
417   TextInput textInput = TextInput::New();  // create empty TextInput
418
419   unsigned int numberOfLines = 1;
420
421   textInput.SetNumberOfLinesLimit( numberOfLines );
422
423   DALI_TEST_EQUALS(numberOfLines ,textInput.GetNumberOfLinesLimit(),  TEST_LOCATION);
424 }
425
426 static void UtcDaliTextInputSetAndGetFadeBoundary()
427 {
428   tet_infoline("UtcDaliTextViewSetAndGetFadeBoundary: ");
429
430   ToolkitTestApplication application;
431
432   TextView::FadeBoundary fadeBoundary( PixelSize( 0 ), PixelSize( 20 ), PixelSize( 0 ), PixelSize( 10 ) );
433
434   TextInput textInput = TextInput::New();
435   textInput.SetInitialText( "Hello world!" );
436
437   textInput.SetFadeBoundary( fadeBoundary );
438
439   TextView::FadeBoundary fadeBoundary2 = textInput.GetFadeBoundary();
440
441   DALI_TEST_EQUALS( fadeBoundary.mLeft, fadeBoundary2.mLeft, TEST_LOCATION );
442   DALI_TEST_EQUALS( fadeBoundary.mRight, fadeBoundary2.mRight, TEST_LOCATION );
443   DALI_TEST_EQUALS( fadeBoundary.mTop, fadeBoundary2.mTop, TEST_LOCATION );
444   DALI_TEST_EQUALS( fadeBoundary.mBottom, fadeBoundary2.mBottom, TEST_LOCATION );
445 }
446
447 static void UtcDaliTextInputSetAndGetWidthExceedPolicy()
448 {
449   ToolkitTestApplication application;
450
451   tet_infoline("UtcDaliTextInputSetAndGetWidthExceedPolicy: ");
452
453   const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::Split, TextView::ShrinkToFit };
454   const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
455
456   TextInput textInput = TextInput::New();
457   textInput.SetInitialText( "Hello world!" );
458
459   for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
460   {
461     textInput.SetWidthExceedPolicy( EXCEED_POLICIES[epIndex] );
462
463     DALI_TEST_EQUALS( textInput.GetWidthExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
464   }
465 }
466
467 static void UtcDaliTextInputSetAndGetHeightExceedPolicy()
468 {
469   ToolkitTestApplication application;
470
471   tet_infoline("UtcDaliTextInputSetAndGetHeightExceedPolicy: ");
472
473   const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::ShrinkToFit };
474   const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
475
476   TextInput textInput = TextInput::New();
477   textInput.SetInitialText( "Hello world!" );
478
479   for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
480   {
481     textInput.SetHeightExceedPolicy( EXCEED_POLICIES[epIndex] );
482
483     DALI_TEST_EQUALS( textInput.GetHeightExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
484   }
485 }
486
487 static void UtcDaliTextInputScroll()
488 {
489   tet_infoline("UtcDaliTextInputScroll: ");
490   ToolkitTestApplication application;
491
492   // Avoids the frame buffer texture to throw an exception.
493   application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
494
495   TextInput view = TextInput::New();
496   view.SetMultilinePolicy( TextView::SplitByNewLineChar );
497   view.SetWidthExceedPolicy( TextView::Original );
498   view.SetHeightExceedPolicy( TextView::Original );
499   view.SetTextAlignment( static_cast<Toolkit::Alignment::Type>( Toolkit::Alignment::HorizontalCenter | Toolkit::Alignment::VerticalCenter ) );
500   view.SetInitialText( "Hello world! This is a scroll test." );
501   view.SetSize( 100.f, 100.f );
502   view.SetSnapshotModeEnabled( false );
503
504   Stage::GetCurrent().Add( view );
505
506   application.SendNotification();
507   application.Render();
508
509   DALI_TEST_CHECK( !view.IsScrollEnabled() ); // Scroll should be disabled by default.
510
511   view.SetScrollEnabled( true );
512
513   DALI_TEST_CHECK( view.IsScrollEnabled() );
514   DALI_TEST_CHECK( view.IsSnapshotModeEnabled() ); // Scroll should enable snapshot mode.
515
516   view.SetScrollPosition( Vector2( 400.f, 400.f ) );
517
518   application.SendNotification();
519   application.Render();
520
521   const Vector2& scrollPosition = view.GetScrollPosition();
522   DALI_TEST_EQUALS( scrollPosition, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
523 }