2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
22 #include <dali/public-api/dali-core.h>
24 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali/integration-api/events/key-event-integ.h>
29 #include <dali-toolkit-test-suite-utils.h>
32 using namespace Toolkit;
34 static void Startup();
35 static void Cleanup();
39 static bool gObjectCreatedCallBackCalled;
41 static void TestCallback(BaseHandle handle)
43 Actor actor = Actor::DownCast(handle);
47 TextInput handle = TextInput::DownCast(actor);
50 gObjectCreatedCallBackCalled = true;
58 void (*tet_startup)() = Startup;
59 void (*tet_cleanup)() = Cleanup;
63 static bool gHasEndSignalBeenReceived;
64 static bool gHasStartSignalBeenReceived;
68 POSITIVE_TC_IDX = 0x01,
72 #define MAX_NUMBER_OF_TESTS 10000
74 struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
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 );
96 // Called only once before first test is run.
101 // Called only once after last test is run
102 static void Cleanup()
107 // Positive test case for a method
108 static void UtcDaliTextInputConstruction()
110 ToolkitTestApplication application;
112 tet_infoline("Testing New constructor");
114 TextInput textInput = TextInput::New();
115 DALI_TEST_CHECK(textInput);
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 );
121 gObjectCreatedCallBackCalled = false;
122 registry.ObjectCreatedSignal().Connect(&TestCallback);
124 TextInput textInput = TextInput::New();
126 DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
130 static bool downCastToTextInput(Dali::Actor actor)
132 TextInput handle = TextInput::DownCast(actor);
135 tet_infoline("Downcasted to TextInput");
140 tet_infoline("Did not downcast to TextInput");
145 // Positive test case for a method
146 static void UtcDaliTextInputDownCast()
148 ToolkitTestApplication application;
150 TextInput textInput = TextInput::New();
152 tet_infoline("Testing Downcasting with a TextInput");
153 DALI_TEST_EQUALS(true,downCastToTextInput(textInput), TEST_LOCATION); // downcast a TextInput
155 Dali::TextActor badHandle = Dali::TextActor::New("test");
157 tet_infoline("Testing Downcasting with the wrong actor");
158 DALI_TEST_EQUALS(false, downCastToTextInput(badHandle), TEST_LOCATION); // downcast a TextActor to TextInput
161 // Positive test case for a method
162 static void UtcDaliTextInputGetText()
164 ToolkitTestApplication application;
166 tet_infoline("Testing GetText");
168 const std::string teststring = "test";
170 TextInput textInput = TextInput::New(); // create empty TextInput
172 DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION); // Get text which should be empty
174 textInput.SetInitialText(teststring);
176 DALI_TEST_EQUALS(teststring,textInput.GetText(), TEST_LOCATION); // Get text which should be test string
180 // Positive test case for a method
181 static void UtcDaliTextInputSetMaxCharacterLength()
183 ToolkitTestApplication application;
185 tet_infoline("Testing Setting of max characters");
187 const int maxChars = 4;
188 const char* testChar = "v";
190 TextInput textInput = TextInput::New(); // create empty TextInput
192 Stage::GetCurrent().Add(textInput);
194 textInput.SetKeyInputFocus();
195 textInput.SetMaxCharacterLength(maxChars);
197 Integration::KeyEvent event(testChar, testChar, 0, 0, 0, Integration::KeyEvent::Down );
199 std::string testString = "";
200 // Send max number of characters
201 for (int i=0; i < maxChars; i++)
203 application.ProcessEvent(event);
204 testString.append(testChar);
207 DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
209 application.ProcessEvent(event); // try to append additional character
211 DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
213 textInput.SetMaxCharacterLength(maxChars+1); // increment max characters by 1
215 application.ProcessEvent(event); // append additional character
216 testString.append(testChar);
218 DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
221 // Positive test case for a method
222 static void UtcDaliTextInputSetInitialText()
224 ToolkitTestApplication application;
226 tet_infoline("Testing Setting of Initial Text");
228 const std::string teststring = "test";
230 TextInput textInput = TextInput::New(); // create empty TextInput
232 textInput.SetInitialText(teststring);
234 DALI_TEST_EQUALS(teststring,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
237 static void UtcDaliTextInputAddChars()
239 ToolkitTestApplication application;
241 tet_infoline("Testing Adding characters");
243 TextInput textInput = TextInput::New(); // create empty TextInput
245 Stage::GetCurrent().Add(textInput);
247 textInput.SetKeyInputFocus();
249 Integration::KeyEvent event("a", "a", 0, 0, 0, Integration::KeyEvent::Down);
250 application.ProcessEvent(event);
252 DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
254 Integration::KeyEvent event2("v", "v", 0, 0, 0, Integration::KeyEvent::Down);
255 application.ProcessEvent(event2);
257 DALI_TEST_EQUALS("av",textInput.GetText(), TEST_LOCATION); // Get text which should be "av"
260 static void UtcDaliTextInputRemoveChars()
262 ToolkitTestApplication application;
264 tet_infoline("Testing Removal of end characters");
266 TextInput textInput = TextInput::New(); // create empty TextInput
268 Stage::GetCurrent().Add(textInput);
270 textInput.SetKeyInputFocus();
272 Integration::KeyEvent event("a", "a", 0, 0, 0, Integration::KeyEvent::Down);
273 application.ProcessEvent(event);
275 DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
277 Integration::KeyEvent event2("BackSpace", "", 0, 0, 0, Integration::KeyEvent::Down);
278 application.ProcessEvent(event2);
280 DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION); // Get text which should be ""
282 application.ProcessEvent(event);
283 application.ProcessEvent(event);
285 DALI_TEST_EQUALS("aa",textInput.GetText(), TEST_LOCATION); // Get text which should be "aa"
287 application.ProcessEvent(event2);
289 DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
292 // Callback test function
293 void OnEndInput(TextInput textInput)
295 gHasEndSignalBeenReceived = true;
298 static void UtcDaliTextInputEndSignalEmit()
300 ToolkitTestApplication application;
302 tet_infoline("Testing Set editable false emits end signal");
304 TextInput textInput = TextInput::New(); // create empty TextInput
306 Stage::GetCurrent().Add(textInput);
308 textInput.InputFinishedSignal().Connect( &OnEndInput );
310 textInput.SetEditable(true) ;
312 gHasEndSignalBeenReceived = false;
314 textInput.SetEditable(false) ;
316 DALI_TEST_EQUALS(true, gHasEndSignalBeenReceived, TEST_LOCATION);
320 // Callback test function
321 void OnStartInput(TextInput textInput)
323 gHasStartSignalBeenReceived = true;
326 static void UtcDaliTextInputStartSignalEmit()
328 ToolkitTestApplication application;
330 tet_infoline("Testing SetEditable emits start signal");
332 TextInput textInput = TextInput::New(); // create empty TextInput
334 Stage::GetCurrent().Add(textInput);
336 textInput.InputStartedSignal().Connect( &OnStartInput );
338 gHasStartSignalBeenReceived = false;
340 textInput.SetEditable(true); // Set editable first time
342 DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
344 gHasStartSignalBeenReceived = false;
346 textInput.SetEditable(true); // Set editable second time, signal should not be sent again.
348 DALI_TEST_EQUALS(false, gHasStartSignalBeenReceived, TEST_LOCATION);
350 textInput.SetEditable(false);
352 gHasStartSignalBeenReceived = false;
354 textInput.SetEditable(true,Vector2(3.f,2.f)); // Set editable again
356 DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
359 static void UtcDaliTextInputExceedMaxCharactersInitial()
361 ToolkitTestApplication application;
363 tet_infoline("Testing Setting Initial Text obeys Max Character Limit");
365 TextInput textInput = TextInput::New(); // create empty TextInput
367 Stage::GetCurrent().Add(textInput);
369 textInput.SetMaxCharacterLength(4);
371 textInput.SetInitialText("TooBig");
373 tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
375 DALI_TEST_EQUALS("TooB",textInput.GetText(), TEST_LOCATION); // Get text which should be only 4 characters
379 static void UtcDaliTextInputExceedMaxCharacters()
381 ToolkitTestApplication application;
383 tet_infoline("Testing Max characters is obeyed when inputting key events ");
385 TextInput textInput = TextInput::New(); // create empty TextInput
387 Stage::GetCurrent().Add(textInput);
389 textInput.SetMaxCharacterLength(4);
391 textInput.SetInitialText("");
393 textInput.SetEditable(true);
395 Integration::KeyEvent eventA("a", "a", 0, 0, 0, Integration::KeyEvent::Down );
396 Integration::KeyEvent eventB("b", "b", 0, 0, 0, Integration::KeyEvent::Down );
398 application.ProcessEvent(eventA);
399 application.ProcessEvent(eventB);
400 application.ProcessEvent(eventA);
401 application.ProcessEvent(eventB);
403 application.ProcessEvent(eventA);
404 application.ProcessEvent(eventB);
406 tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
408 DALI_TEST_EQUALS("abab",textInput.GetText(), TEST_LOCATION); // Get text which should be only 4 characters
411 static void UtcDaliTextInputSetNumberOfLines()
413 ToolkitTestApplication application;
415 tet_infoline("Ensuring API for setting and getting max number of lines is correct");
417 TextInput textInput = TextInput::New(); // create empty TextInput
419 unsigned int numberOfLines = 1;
421 textInput.SetNumberOfLinesLimit( numberOfLines );
423 DALI_TEST_EQUALS(numberOfLines ,textInput.GetNumberOfLinesLimit(), TEST_LOCATION);
426 static void UtcDaliTextInputSetAndGetFadeBoundary()
428 tet_infoline("UtcDaliTextViewSetAndGetFadeBoundary: ");
430 ToolkitTestApplication application;
432 TextView::FadeBoundary fadeBoundary( PixelSize( 0 ), PixelSize( 20 ), PixelSize( 0 ), PixelSize( 10 ) );
434 TextInput textInput = TextInput::New();
435 textInput.SetInitialText( "Hello world!" );
437 textInput.SetFadeBoundary( fadeBoundary );
439 TextView::FadeBoundary fadeBoundary2 = textInput.GetFadeBoundary();
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 );
447 static void UtcDaliTextInputSetAndGetWidthExceedPolicy()
449 ToolkitTestApplication application;
451 tet_infoline("UtcDaliTextInputSetAndGetWidthExceedPolicy: ");
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 );
456 TextInput textInput = TextInput::New();
457 textInput.SetInitialText( "Hello world!" );
459 for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
461 textInput.SetWidthExceedPolicy( EXCEED_POLICIES[epIndex] );
463 DALI_TEST_EQUALS( textInput.GetWidthExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
467 static void UtcDaliTextInputSetAndGetHeightExceedPolicy()
469 ToolkitTestApplication application;
471 tet_infoline("UtcDaliTextInputSetAndGetHeightExceedPolicy: ");
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 );
476 TextInput textInput = TextInput::New();
477 textInput.SetInitialText( "Hello world!" );
479 for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
481 textInput.SetHeightExceedPolicy( EXCEED_POLICIES[epIndex] );
483 DALI_TEST_EQUALS( textInput.GetHeightExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
487 static void UtcDaliTextInputScroll()
489 tet_infoline("UtcDaliTextInputScroll: ");
490 ToolkitTestApplication application;
492 // Avoids the frame buffer texture to throw an exception.
493 application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
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 );
504 Stage::GetCurrent().Add( view );
506 application.SendNotification();
507 application.Render();
509 DALI_TEST_CHECK( !view.IsScrollEnabled() ); // Scroll should be disabled by default.
511 view.SetScrollEnabled( true );
513 DALI_TEST_CHECK( view.IsScrollEnabled() );
514 DALI_TEST_CHECK( view.IsSnapshotModeEnabled() ); // Scroll should enable snapshot mode.
516 view.SetScrollPosition( Vector2( 400.f, 400.f ) );
518 application.SendNotification();
519 application.Render();
521 const Vector2& scrollPosition = view.GetScrollPosition();
522 DALI_TEST_EQUALS( scrollPosition, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );