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 Dali::Integration::Core& core = application.GetCore();
192 TextInput textInput = TextInput::New(); // create empty TextInput
194 Stage::GetCurrent().Add(textInput);
196 textInput.SetKeyInputFocus();
197 textInput.SetMaxCharacterLength(maxChars);
199 Integration::KeyEvent event(testChar, testChar, 0, 0, 0, Integration::KeyEvent::Down );
201 std::string testString = "";
202 // Send max number of characters
203 for (int i=0; i < maxChars; i++)
205 core.SendEvent(event);
206 testString.append(testChar);
209 DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
211 core.SendEvent(event); // try to append additional character
213 DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
215 textInput.SetMaxCharacterLength(maxChars+1); // increment max characters by 1
217 core.SendEvent(event); // append additional character
218 testString.append(testChar);
220 DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
223 // Positive test case for a method
224 static void UtcDaliTextInputSetInitialText()
226 ToolkitTestApplication application;
228 tet_infoline("Testing Setting of Initial Text");
230 const std::string teststring = "test";
232 TextInput textInput = TextInput::New(); // create empty TextInput
234 textInput.SetInitialText(teststring);
236 DALI_TEST_EQUALS(teststring,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
239 static void UtcDaliTextInputAddChars()
241 ToolkitTestApplication application;
243 tet_infoline("Testing Adding characters");
245 Dali::Integration::Core& core = application.GetCore();
247 TextInput textInput = TextInput::New(); // create empty TextInput
249 Stage::GetCurrent().Add(textInput);
251 textInput.SetKeyInputFocus();
253 Integration::KeyEvent event("a", "a", 0, 0, 0, Integration::KeyEvent::Down);
254 core.SendEvent(event);
256 DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
258 Integration::KeyEvent event2("v", "v", 0, 0, 0, Integration::KeyEvent::Down);
259 core.SendEvent(event2);
261 DALI_TEST_EQUALS("av",textInput.GetText(), TEST_LOCATION); // Get text which should be "av"
264 static void UtcDaliTextInputRemoveChars()
266 ToolkitTestApplication application;
268 tet_infoline("Testing Removal of end characters");
270 Dali::Integration::Core& core = application.GetCore();
272 TextInput textInput = TextInput::New(); // create empty TextInput
274 Stage::GetCurrent().Add(textInput);
276 textInput.SetKeyInputFocus();
278 Integration::KeyEvent event("a", "a", 0, 0, 0, Integration::KeyEvent::Down);
279 core.SendEvent(event);
281 DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
283 Integration::KeyEvent event2("BackSpace", "", 0, 0, 0, Integration::KeyEvent::Down);
284 core.SendEvent(event2);
286 DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION); // Get text which should be ""
288 core.SendEvent(event);
289 core.SendEvent(event);
291 DALI_TEST_EQUALS("aa",textInput.GetText(), TEST_LOCATION); // Get text which should be "aa"
293 core.SendEvent(event2);
295 DALI_TEST_EQUALS("a",textInput.GetText(), TEST_LOCATION); // Get text which should be "a"
298 // Callback test function
299 void OnEndInput(TextInput textInput)
301 gHasEndSignalBeenReceived = true;
304 static void UtcDaliTextInputEndSignalEmit()
306 ToolkitTestApplication application;
308 tet_infoline("Testing Set editable false emits end signal");
310 TextInput textInput = TextInput::New(); // create empty TextInput
312 Stage::GetCurrent().Add(textInput);
314 textInput.InputFinishedSignal().Connect( &OnEndInput );
316 textInput.SetEditable(true) ;
318 gHasEndSignalBeenReceived = false;
320 textInput.SetEditable(false) ;
322 DALI_TEST_EQUALS(true, gHasEndSignalBeenReceived, TEST_LOCATION);
326 // Callback test function
327 void OnStartInput(TextInput textInput)
329 gHasStartSignalBeenReceived = true;
332 static void UtcDaliTextInputStartSignalEmit()
334 ToolkitTestApplication application;
336 tet_infoline("Testing SetEditable emits start signal");
338 TextInput textInput = TextInput::New(); // create empty TextInput
340 Stage::GetCurrent().Add(textInput);
342 textInput.InputStartedSignal().Connect( &OnStartInput );
344 gHasStartSignalBeenReceived = false;
346 textInput.SetEditable(true); // Set editable first time
348 DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
350 gHasStartSignalBeenReceived = false;
352 textInput.SetEditable(true); // Set editable second time, signal should not be sent again.
354 DALI_TEST_EQUALS(false, gHasStartSignalBeenReceived, TEST_LOCATION);
356 textInput.SetEditable(false);
358 gHasStartSignalBeenReceived = false;
360 textInput.SetEditable(true,Vector2(3.f,2.f)); // Set editable again
362 DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
365 static void UtcDaliTextInputExceedMaxCharactersInitial()
367 ToolkitTestApplication application;
369 tet_infoline("Testing Setting Initial Text obeys Max Character Limit");
371 TextInput textInput = TextInput::New(); // create empty TextInput
373 Stage::GetCurrent().Add(textInput);
375 textInput.SetMaxCharacterLength(4);
377 textInput.SetInitialText("TooBig");
379 tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
381 DALI_TEST_EQUALS("TooB",textInput.GetText(), TEST_LOCATION); // Get text which should be only 4 characters
385 static void UtcDaliTextInputExceedMaxCharacters()
387 ToolkitTestApplication application;
389 Dali::Integration::Core& core = application.GetCore();
391 tet_infoline("Testing Max characters is obeyed when inputting key events ");
393 TextInput textInput = TextInput::New(); // create empty TextInput
395 Stage::GetCurrent().Add(textInput);
397 textInput.SetMaxCharacterLength(4);
399 textInput.SetInitialText("");
401 textInput.SetEditable(true);
403 Integration::KeyEvent eventA("a", "a", 0, 0, 0, Integration::KeyEvent::Down );
404 Integration::KeyEvent eventB("b", "b", 0, 0, 0, Integration::KeyEvent::Down );
406 core.SendEvent(eventA);
407 core.SendEvent(eventB);
408 core.SendEvent(eventA);
409 core.SendEvent(eventB);
411 core.SendEvent(eventA);
412 core.SendEvent(eventB);
414 tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
416 DALI_TEST_EQUALS("abab",textInput.GetText(), TEST_LOCATION); // Get text which should be only 4 characters
419 static void UtcDaliTextInputSetNumberOfLines()
421 ToolkitTestApplication application;
423 tet_infoline("Ensuring API for setting and getting max number of lines is correct");
425 TextInput textInput = TextInput::New(); // create empty TextInput
427 unsigned int numberOfLines = 1;
429 textInput.SetNumberOfLinesLimit( numberOfLines );
431 DALI_TEST_EQUALS(numberOfLines ,textInput.GetNumberOfLinesLimit(), TEST_LOCATION);
434 static void UtcDaliTextInputSetAndGetFadeBoundary()
436 tet_infoline("UtcDaliTextViewSetAndGetFadeBoundary: ");
438 ToolkitTestApplication application;
440 TextView::FadeBoundary fadeBoundary( PixelSize( 0 ), PixelSize( 20 ), PixelSize( 0 ), PixelSize( 10 ) );
442 TextInput textInput = TextInput::New();
443 textInput.SetInitialText( "Hello world!" );
445 textInput.SetFadeBoundary( fadeBoundary );
447 TextView::FadeBoundary fadeBoundary2 = textInput.GetFadeBoundary();
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 );
455 static void UtcDaliTextInputSetAndGetWidthExceedPolicy()
457 ToolkitTestApplication application;
459 tet_infoline("UtcDaliTextInputSetAndGetWidthExceedPolicy: ");
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 );
464 TextInput textInput = TextInput::New();
465 textInput.SetInitialText( "Hello world!" );
467 for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
469 textInput.SetWidthExceedPolicy( EXCEED_POLICIES[epIndex] );
471 DALI_TEST_EQUALS( textInput.GetWidthExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
475 static void UtcDaliTextInputSetAndGetHeightExceedPolicy()
477 ToolkitTestApplication application;
479 tet_infoline("UtcDaliTextInputSetAndGetHeightExceedPolicy: ");
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 );
484 TextInput textInput = TextInput::New();
485 textInput.SetInitialText( "Hello world!" );
487 for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
489 textInput.SetHeightExceedPolicy( EXCEED_POLICIES[epIndex] );
491 DALI_TEST_EQUALS( textInput.GetHeightExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
495 static void UtcDaliTextInputScroll()
497 tet_infoline("UtcDaliTextInputScroll: ");
498 ToolkitTestApplication application;
500 // Avoids the frame buffer texture to throw an exception.
501 application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
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 );
512 Stage::GetCurrent().Add( view );
514 application.SendNotification();
515 application.Render();
517 DALI_TEST_CHECK( !view.IsScrollEnabled() ); // Scroll should be disabled by default.
519 view.SetScrollEnabled( true );
521 DALI_TEST_CHECK( view.IsScrollEnabled() );
522 DALI_TEST_CHECK( view.IsSnapshotModeEnabled() ); // Scroll should enable snapshot mode.
524 view.SetScrollPosition( Vector2( 400.f, 400.f ) );
526 application.SendNotification();
527 application.Render();
529 const Vector2& scrollPosition = view.GetScrollPosition();
530 DALI_TEST_EQUALS( scrollPosition, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );