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.
20 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali/integration-api/events/key-event-integ.h>
26 using namespace Toolkit;
28 void utc_dali_toolkit_text_input_startup(void)
30 test_return_value = TET_UNDEF;
33 void utc_dali_toolkit_text_input_cleanup(void)
35 test_return_value = TET_PASS;
40 static bool gObjectCreatedCallBackCalled;
42 static void TestCallback(BaseHandle handle)
44 Actor actor = Actor::DownCast(handle);
48 TextInput handle = TextInput::DownCast(actor);
51 gObjectCreatedCallBackCalled = true;
56 static bool gHasEndSignalBeenReceived;
57 static bool gHasStartSignalBeenReceived;
59 // Callback test function
60 void OnStartInput(TextInput textInput)
62 gHasStartSignalBeenReceived = true;
65 // Callback test function
66 void OnEndInput(TextInput textInput)
68 gHasEndSignalBeenReceived = true;
73 // Positive test case for a method
74 int UtcDaliTextInputConstruction(void)
76 ToolkitTestApplication application;
78 tet_infoline("Testing New constructor");
80 TextInput textInput = TextInput::New();
81 DALI_TEST_CHECK(textInput);
83 //Additional check to ensure object is created by checking if it's registered
84 ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
85 DALI_TEST_CHECK( registry );
87 gObjectCreatedCallBackCalled = false;
88 registry.ObjectCreatedSignal().Connect(&TestCallback);
90 TextInput textInput = TextInput::New();
92 DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
97 static bool downCastToTextInput(Dali::Actor actor)
99 TextInput handle = TextInput::DownCast(actor);
102 tet_infoline("Downcasted to TextInput");
107 tet_infoline("Did not downcast to TextInput");
112 // Positive test case for a method
113 int UtcDaliTextInputDownCast(void)
115 ToolkitTestApplication application;
117 TextInput textInput = TextInput::New();
119 tet_infoline("Testing Downcasting with a TextInput");
120 DALI_TEST_EQUALS(true,downCastToTextInput(textInput), TEST_LOCATION); // downcast a TextInput
122 Dali::TextActor badHandle = Dali::TextActor::New("test");
124 tet_infoline("Testing Downcasting with the wrong actor");
125 DALI_TEST_EQUALS(false, downCastToTextInput(badHandle), TEST_LOCATION); // downcast a TextActor to TextInput
129 // Positive test case for a method
130 int UtcDaliTextInputGetText(void)
132 ToolkitTestApplication application;
134 tet_infoline("Testing GetText");
136 const std::string teststring = "test";
138 TextInput textInput = TextInput::New(); // create empty TextInput
140 DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION); // Get text which should be empty
142 textInput.SetInitialText(teststring);
144 DALI_TEST_EQUALS(teststring,textInput.GetText(), TEST_LOCATION); // Get text which should be test string
149 int UtcDaliTextInputGetMarkupText(void)
151 ToolkitTestApplication application;
153 tet_infoline("Testing retrieval of Markup text after style set");
155 const std::string markup = "<i>Text with italic style</i>" ;
156 const std::string teststring = "Text with italic style";
158 TextInput textInput = TextInput::New();
160 tet_infoline("Set initial text");
162 textInput.SetInitialText( teststring );
164 tet_infoline("Check initial text");
165 DALI_TEST_EQUALS( teststring,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
168 style.SetItalics( true );
170 tet_infoline("Apply style to TextInput");
171 textInput.ApplyStyleToAll( style );
173 tet_infoline("Retreive Markup Text");
174 const std::string retreivedMarkupString = textInput.GetMarkupText();
176 tet_infoline("Test Retreived text and Markup text match");
177 DALI_TEST_EQUALS( retreivedMarkupString , retreivedMarkupString, TEST_LOCATION);
181 int UtcDaliTextInputSetMaxCharacterLength(void)
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
191 Stage::GetCurrent().Add(textInput);
192 application.SendNotification();
193 application.Render();
195 textInput.SetMaxCharacterLength(maxChars);
197 Integration::KeyEvent event(testChar, testChar, 0, 0, 0, Integration::KeyEvent::Down );
199 std::string testString = "";
201 tet_infoline("Starting editmode");
202 textInput.SetEditable( true );
204 tet_infoline("Sending Key Events");
205 // Send max number of characters
206 for (int i=0; i < maxChars; i++)
208 application.ProcessEvent(event);
209 testString.append(testChar);
212 tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
214 DALI_TEST_EQUALS(testString, textInput.GetText(), TEST_LOCATION);
216 tet_infoline("Sending Key Event which exceeds max characters");
218 application.ProcessEvent(event); // try to append additional character
220 DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION);
222 tet_infoline("Increase max characters limit");
224 textInput.SetMaxCharacterLength(maxChars+1); // increment max characters by 1
226 tet_infoline("Send character again which should now fit");
227 application.ProcessEvent(event); // append additional character
228 testString.append(testChar);
230 DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION);
235 int UtcDaliTextInputSetAndGetNumberOfLines(void)
237 ToolkitTestApplication application;
239 tet_infoline("Ensuring API for setting and getting max number of lines is correct");
241 TextInput textInput = TextInput::New(); // create empty TextInput
243 unsigned int numberOfLines = 1;
245 textInput.SetNumberOfLinesLimit( numberOfLines );
247 DALI_TEST_EQUALS(numberOfLines ,textInput.GetNumberOfLinesLimit(), TEST_LOCATION);
251 int UtcDaliTextInputGetNumberOfCharacters(void)
253 ToolkitTestApplication application;
255 tet_infoline("Testing Getting number of characters");
257 const std::string initialString = "initial text";
258 const std::string newInitialString = "initial text new";
260 TextInput textInput = TextInput::New(); // create empty TextInput
262 textInput.SetInitialText( initialString );
264 tet_infoline("Testing TextInput contains correct number of characters ");
266 DALI_TEST_EQUALS( initialString.size() , textInput.GetNumberOfCharacters(), TEST_LOCATION);
268 tet_infoline("Testing TextInput contains correct number of characters second phase ");
270 textInput.SetInitialText( newInitialString );
272 DALI_TEST_EQUALS( newInitialString.size() , textInput.GetNumberOfCharacters(), TEST_LOCATION);
276 int UtcDaliTextInputSetAndGetPlaceholderText(void)
278 ToolkitTestApplication application;
280 tet_infoline("Testing Setting of PlaceholderText");
282 const std::string initialString = "initial text";
283 const std::string placeholderString = "placeholder";
285 TextInput textInput = TextInput::New(); // create empty TextInput
287 tet_infoline("Testing TextInput is empty at creation ");
289 DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION);
291 tet_infoline("Set placeholder text");
293 textInput.SetPlaceholderText( placeholderString );
295 tet_infoline("Testing TextInput contains placeholder text");
297 DALI_TEST_EQUALS( placeholderString , textInput.GetPlaceholderText(), TEST_LOCATION);
299 tet_infoline("Set initial text which should replace placeholder text");
301 textInput.SetInitialText( initialString );
303 tet_infoline("Testing TextInput contains initial text when placeholder text set");
305 DALI_TEST_EQUALS( initialString,textInput.GetText(), TEST_LOCATION);
309 // Positive test case for a method
310 int UtcDaliTextInputSetInitialText(void)
312 ToolkitTestApplication application;
314 tet_infoline("Testing Setting of Initial Text");
316 const std::string teststring = "test";
318 TextInput textInput = TextInput::New(); // create empty TextInput
320 tet_infoline("Testing TextInput is empty at creation ");
322 DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION);
324 tet_infoline("Set text to TextInput");
326 textInput.SetInitialText(teststring);
328 tet_infoline("Test TextInput contains set text");
330 DALI_TEST_EQUALS(teststring,textInput.GetText(), TEST_LOCATION);
334 int UtcDaliTextInputSetEditableAndIsEditable(void)
336 ToolkitTestApplication application;
338 tet_infoline("Testing SetEditable And IsEditable");
340 const std::string initialString = "initial text";
342 TextInput textInput = TextInput::New(); // create empty TextInput
343 textInput.SetInitialText( initialString );
345 Stage::GetCurrent().Add(textInput);
346 application.SendNotification();
347 application.Render();
349 bool editableStateFalse ( false );
350 bool editableStateTrue ( true );
352 textInput.SetEditable ( editableStateFalse );
353 application.SendNotification();
354 application.Render();
355 DALI_TEST_EQUALS( editableStateFalse, textInput.IsEditable() , TEST_LOCATION);
357 textInput.SetEditable ( editableStateTrue );
358 application.SendNotification();
359 application.Render();
360 DALI_TEST_EQUALS( editableStateTrue, textInput.IsEditable() , TEST_LOCATION);
364 int UtcDaliTextInputSetEditOnTouch(void)
366 ToolkitTestApplication application;
368 tet_infoline("Testing SetEditOnTouch And IsEditOnTouch");
370 TextInput textInput = TextInput::New();
372 bool editableOnTouchOn ( true );
373 bool editableOnTouchOff( false );
375 tet_infoline("Testing SetEditOnTouch disabled");
376 textInput.SetEditOnTouch ( editableOnTouchOff );
377 DALI_TEST_EQUALS( editableOnTouchOff, textInput.IsEditOnTouch() , TEST_LOCATION);
379 tet_infoline("Testing SetEditOnTouch enabled");
380 textInput.SetEditOnTouch ( editableOnTouchOn );
381 DALI_TEST_EQUALS( editableOnTouchOn, textInput.IsEditOnTouch() , TEST_LOCATION);
385 int UtcDaliTextInputSetTextSelectable(void)
387 ToolkitTestApplication application;
389 tet_infoline("Testing SetTextSelectable and IsTextSelectable");
391 const std::string initialString = "initial text";
393 TextInput textInput = TextInput::New();
394 textInput.SetInitialText( initialString );
396 tet_infoline("Testing SetTextSelectable");
397 textInput.SetTextSelectable();
398 DALI_TEST_EQUALS( true, textInput.IsTextSelectable() , TEST_LOCATION);
399 textInput.SetTextSelectable( false );
400 DALI_TEST_EQUALS( false, textInput.IsTextSelectable() , TEST_LOCATION);
404 int UtcDaliTextInputTextSelection(void)
406 ToolkitTestApplication application;
408 tet_infoline("Testing Text Selection");
410 const std::string initialString = "initial text";
412 TextInput textInput = TextInput::New();
413 textInput.SetInitialText( initialString );
415 Stage::GetCurrent().Add(textInput);
416 application.SendNotification();
417 application.Render();
419 textInput.SetEditable( true );
421 tet_infoline("Testing IsTextSelected negative");
422 DALI_TEST_EQUALS( false, textInput.IsTextSelected(), TEST_LOCATION);
424 textInput.SelectText(1,7);
425 DALI_TEST_EQUALS( true, textInput.IsTextSelected(), TEST_LOCATION);
427 textInput.DeSelectText();
428 DALI_TEST_EQUALS( false, textInput.IsTextSelected(), TEST_LOCATION);
432 int UtcDaliTextInputEnableGrabHandleAndIsGrabHandleEnabled(void)
434 ToolkitTestApplication application;
436 TextInput textInput = TextInput::New();
438 bool grabHandleState = false;
440 textInput.EnableGrabHandle( grabHandleState );
442 DALI_TEST_EQUALS( grabHandleState, textInput.IsGrabHandleEnabled(), TEST_LOCATION);
444 grabHandleState = true;
445 textInput.EnableGrabHandle( grabHandleState );
447 DALI_TEST_EQUALS( grabHandleState, textInput.IsGrabHandleEnabled(), TEST_LOCATION);
452 int UtcDaliTextInputSetAndGetBoundingRectangle(void)
454 ToolkitTestApplication application;
456 TextInput textInput = TextInput::New();
458 Stage::GetCurrent().Add(textInput);
459 Vector2 stageSize = Stage::GetCurrent().GetSize();
461 const Rect<float> boundingRectangle( 100.0f, 100.0f, stageSize.width, stageSize.height );
463 textInput.SetBoundingRectangle( boundingRectangle );
465 const Rect<float> retreievedBoundingRectangle = textInput.GetBoundingRectangle();
467 DALI_TEST_EQUALS( boundingRectangle.x, retreievedBoundingRectangle.x, TEST_LOCATION);
468 DALI_TEST_EQUALS( boundingRectangle.y, retreievedBoundingRectangle.y, TEST_LOCATION);
469 DALI_TEST_EQUALS( boundingRectangle.width, retreievedBoundingRectangle.width, TEST_LOCATION);
470 DALI_TEST_EQUALS( boundingRectangle.height, retreievedBoundingRectangle.height, TEST_LOCATION);
475 int UtcDaliTextInputSetAndGetTextAlignment(void)
477 ToolkitTestApplication application;
479 TextInput textInput = TextInput::New();
480 Stage::GetCurrent().Add(textInput);
481 application.SendNotification();
482 application.Render();
484 textInput.SetTextAlignment(static_cast<Alignment::Type>( Alignment::HorizontalCenter) );
485 application.SendNotification();
486 application.Render();
488 DALI_TEST_CHECK( static_cast<Alignment::Type>( Alignment::HorizontalCenter) & textInput.GetTextAlignment()) ;
493 int UtcDaliTextInputSetSortModifier(void)
495 tet_infoline("Testing SetSortModifier does not cause TextInput failure");
497 ToolkitTestApplication application;
499 TextInput textInput = TextInput::New();
501 const float offsetToUse = 1.5f;
503 textInput.SetSortModifier( offsetToUse );
505 DALI_TEST_CHECK( textInput );
509 int UtcDaliTextInputSetAndGetSnapshotModeEnabled(void)
511 ToolkitTestApplication application;
513 tet_infoline("Testing SetSnapshotModeEnabled and IsSnapshotModeEnabled");
515 TextInput textInput = TextInput::New(); // create empty TextInput
516 bool snapshotMode( true );
517 textInput.SetSnapshotModeEnabled( snapshotMode );
519 DALI_TEST_EQUALS( snapshotMode, textInput.IsSnapshotModeEnabled(), TEST_LOCATION);
521 snapshotMode = false;
522 textInput.SetSnapshotModeEnabled( snapshotMode );
524 DALI_TEST_EQUALS( snapshotMode, textInput.IsSnapshotModeEnabled(), TEST_LOCATION);
529 int UtcDaliTextInputEndSignalEmit(void)
531 ToolkitTestApplication application;
533 tet_infoline("Testing Set editable false emits end signal");
535 TextInput textInput = TextInput::New(); // create empty TextInput
537 Stage::GetCurrent().Add(textInput);
539 textInput.InputFinishedSignal().Connect( &OnEndInput );
541 textInput.SetEditable(true) ;
543 gHasEndSignalBeenReceived = false;
545 textInput.SetEditable(false) ;
547 DALI_TEST_EQUALS(true, gHasEndSignalBeenReceived, TEST_LOCATION);
553 int UtcDaliTextInputStartSignalEmit(void)
555 ToolkitTestApplication application;
557 tet_infoline("Testing SetEditable emits start signal");
559 TextInput textInput = TextInput::New(); // create empty TextInput
561 Stage::GetCurrent().Add(textInput);
563 textInput.InputStartedSignal().Connect( &OnStartInput );
565 gHasStartSignalBeenReceived = false;
567 textInput.SetEditable(true); // Set editable first time
569 DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
571 gHasStartSignalBeenReceived = false;
573 textInput.SetEditable(true); // Set editable second time, signal should not be sent again.
575 DALI_TEST_EQUALS(false, gHasStartSignalBeenReceived, TEST_LOCATION);
577 textInput.SetEditable(false);
579 gHasStartSignalBeenReceived = false;
581 textInput.SetEditable(true); // Set editable again
583 DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
587 int UtcDaliTextInputExceedMaxCharacters(void)
589 ToolkitTestApplication application;
591 tet_infoline("Testing Max characters is obeyed when inputting key events ");
593 TextInput textInput = TextInput::New(); // create empty TextInput
595 Stage::GetCurrent().Add(textInput);
596 textInput.SetMaxCharacterLength(4);
597 textInput.SetInitialText("");
598 textInput.SetEditable(true);
600 application.SendNotification();
601 application.Render();
603 Integration::KeyEvent eventA("a", "a", 0, 0, 0, Integration::KeyEvent::Down );
604 Integration::KeyEvent eventB("b", "b", 0, 0, 0, Integration::KeyEvent::Down );
606 application.ProcessEvent(eventA);
607 application.ProcessEvent(eventB);
608 application.ProcessEvent(eventA);
609 application.ProcessEvent(eventB);
611 application.ProcessEvent(eventA);
612 application.ProcessEvent(eventB);
614 tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
616 DALI_TEST_EQUALS("abab",textInput.GetText(), TEST_LOCATION); // Get text which should be only 4 characters
622 int UtcDaliTextInputSetAndGetFadeBoundary(void)
624 tet_infoline("UtcDaliTextViewSetAndGetFadeBoundary: ");
626 ToolkitTestApplication application;
628 TextView::FadeBoundary fadeBoundary( PixelSize( 0 ), PixelSize( 20 ), PixelSize( 0 ), PixelSize( 10 ) );
630 TextInput textInput = TextInput::New();
631 textInput.SetInitialText( "Hello world!" );
633 Stage::GetCurrent().Add(textInput);
634 application.SendNotification();
635 application.Render();
637 textInput.SetFadeBoundary( fadeBoundary );
639 TextView::FadeBoundary fadeBoundary2 = textInput.GetFadeBoundary();
641 DALI_TEST_EQUALS( fadeBoundary.mLeft, fadeBoundary2.mLeft, TEST_LOCATION );
642 DALI_TEST_EQUALS( fadeBoundary.mRight, fadeBoundary2.mRight, TEST_LOCATION );
643 DALI_TEST_EQUALS( fadeBoundary.mTop, fadeBoundary2.mTop, TEST_LOCATION );
644 DALI_TEST_EQUALS( fadeBoundary.mBottom, fadeBoundary2.mBottom, TEST_LOCATION );
648 int UtcDaliTextInputSetAndGetWidthExceedPolicy(void)
650 ToolkitTestApplication application;
652 tet_infoline("UtcDaliTextInputSetAndGetWidthExceedPolicy: ");
654 const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::Split, TextView::ShrinkToFit };
655 const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
657 TextInput textInput = TextInput::New();
658 textInput.SetInitialText( "Hello world!" );
660 for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
662 textInput.SetWidthExceedPolicy( EXCEED_POLICIES[epIndex] );
664 DALI_TEST_EQUALS( textInput.GetWidthExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
669 int UtcDaliTextInputSetAndGetHeightExceedPolicy(void)
671 ToolkitTestApplication application;
673 tet_infoline("UtcDaliTextInputSetAndGetHeightExceedPolicy: ");
675 const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::ShrinkToFit };
676 const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
678 TextInput textInput = TextInput::New();
679 textInput.SetInitialText( "Hello world!" );
681 for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
683 textInput.SetHeightExceedPolicy( EXCEED_POLICIES[epIndex] );
685 DALI_TEST_EQUALS( textInput.GetHeightExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
690 int UtcDaliTextInputScroll(void)
692 tet_infoline("UtcDaliTextInputScroll: ");
693 ToolkitTestApplication application;
695 // Avoids the frame buffer texture to throw an exception.
696 application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
698 TextInput view = TextInput::New();
699 view.SetMultilinePolicy( TextView::SplitByNewLineChar );
700 view.SetWidthExceedPolicy( TextView::Original );
701 view.SetHeightExceedPolicy( TextView::Original );
702 view.SetTextAlignment( static_cast<Toolkit::Alignment::Type>( Toolkit::Alignment::HorizontalCenter | Toolkit::Alignment::VerticalCenter ) );
703 view.SetInitialText( "Hello world! This is a scroll test." );
704 view.SetSize( 100.f, 100.f );
705 view.SetSnapshotModeEnabled( false );
707 Stage::GetCurrent().Add( view );
709 application.SendNotification();
710 application.Render();
712 DALI_TEST_CHECK( !view.IsScrollEnabled() ); // Scroll should be disabled by default.
714 view.SetScrollEnabled( true );
716 DALI_TEST_CHECK( view.IsScrollEnabled() );
717 DALI_TEST_CHECK( view.IsSnapshotModeEnabled() ); // Scroll should enable snapshot mode.
719 view.SetScrollPosition( Vector2( 400.f, 400.f ) );
721 application.SendNotification();
722 application.Render();
724 const Vector2& scrollPosition = view.GetScrollPosition();
725 DALI_TEST_EQUALS( scrollPosition, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );