2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali-toolkit-test-suite-utils.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali/integration-api/events/key-event-integ.h>
27 using namespace Toolkit;
29 void utc_dali_toolkit_text_input_startup(void)
31 test_return_value = TET_UNDEF;
34 void utc_dali_toolkit_text_input_cleanup(void)
36 test_return_value = TET_PASS;
41 static bool gObjectCreatedCallBackCalled;
43 static void TestCallback(BaseHandle handle)
45 Actor actor = Actor::DownCast(handle);
49 TextInput handle = TextInput::DownCast(actor);
52 gObjectCreatedCallBackCalled = true;
57 static bool gHasEndSignalBeenReceived;
58 static bool gHasStartSignalBeenReceived;
60 // Callback test function
61 void OnStartInput(TextInput textInput)
63 gHasStartSignalBeenReceived = true;
66 // Callback test function
67 void OnEndInput(TextInput textInput)
69 gHasEndSignalBeenReceived = true;
74 // Positive test case for a method
75 int UtcDaliTextInputConstruction(void)
77 ToolkitTestApplication application;
79 tet_infoline("Testing New constructor");
81 TextInput textInput = TextInput::New();
82 DALI_TEST_CHECK(textInput);
84 //Additional check to ensure object is created by checking if it's registered
85 ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
86 DALI_TEST_CHECK( registry );
88 gObjectCreatedCallBackCalled = false;
89 registry.ObjectCreatedSignal().Connect(&TestCallback);
91 TextInput textInput = TextInput::New();
93 DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
98 static bool downCastToTextInput(Dali::Actor actor)
100 TextInput handle = TextInput::DownCast(actor);
103 tet_infoline("Downcasted to TextInput");
108 tet_infoline("Did not downcast to TextInput");
113 // Positive test case for a method
114 int UtcDaliTextInputDownCast(void)
116 ToolkitTestApplication application;
118 TextInput textInput = TextInput::New();
120 tet_infoline("Testing Downcasting with a TextInput");
121 DALI_TEST_EQUALS(true,downCastToTextInput(textInput), TEST_LOCATION); // downcast a TextInput
123 Dali::TextActor badHandle = Dali::TextActor::New("test");
125 tet_infoline("Testing Downcasting with the wrong actor");
126 DALI_TEST_EQUALS(false, downCastToTextInput(badHandle), TEST_LOCATION); // downcast a TextActor to TextInput
130 // Positive test case for a method
131 int UtcDaliTextInputGetText(void)
133 ToolkitTestApplication application;
135 tet_infoline("Testing GetText");
137 const std::string teststring = "test";
139 TextInput textInput = TextInput::New(); // create empty TextInput
141 DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION); // Get text which should be empty
143 textInput.SetInitialText(teststring);
145 DALI_TEST_EQUALS(teststring,textInput.GetText(), TEST_LOCATION); // Get text which should be test string
150 int UtcDaliTextInputGetMarkupText(void)
152 ToolkitTestApplication application;
154 tet_infoline("Testing retrieval of Markup text after style set");
156 const std::string markup = "<i>Text with italic style</i>" ;
157 const std::string teststring = "Text with italic style";
159 TextInput textInput = TextInput::New();
161 tet_infoline("Set initial text");
163 textInput.SetInitialText( teststring );
165 tet_infoline("Check initial text");
166 DALI_TEST_EQUALS( teststring,textInput.GetText(), TEST_LOCATION); // Get text which should be empty
169 style.SetItalics( true );
171 tet_infoline("Apply style to TextInput");
172 textInput.ApplyStyleToAll( style );
174 tet_infoline("Retreive Markup Text");
175 const std::string retreivedMarkupString = textInput.GetMarkupText();
177 tet_infoline("Test Retreived text and Markup text match");
178 DALI_TEST_EQUALS( retreivedMarkupString , retreivedMarkupString, TEST_LOCATION);
182 int UtcDaliTextInputSetMaxCharacterLength(void)
184 ToolkitTestApplication application;
186 tet_infoline("Testing Setting of max characters");
188 const int maxChars = 4;
189 const char* testChar = "v";
191 TextInput textInput = TextInput::New(); // create empty TextInput
192 Stage::GetCurrent().Add(textInput);
193 application.SendNotification();
194 application.Render();
196 textInput.SetMaxCharacterLength(maxChars);
198 Integration::KeyEvent event(testChar, testChar, 0, 0, 0, Integration::KeyEvent::Down );
200 std::string testString = "";
202 tet_infoline("Starting editmode");
203 textInput.SetEditable( true );
205 tet_infoline("Sending Key Events");
206 // Send max number of characters
207 for (int i=0; i < maxChars; i++)
209 application.ProcessEvent(event);
210 testString.append(testChar);
213 tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
215 DALI_TEST_EQUALS(testString, textInput.GetText(), TEST_LOCATION);
217 tet_infoline("Sending Key Event which exceeds max characters");
219 application.ProcessEvent(event); // try to append additional character
221 DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION);
223 tet_infoline("Increase max characters limit");
225 textInput.SetMaxCharacterLength(maxChars+1); // increment max characters by 1
227 tet_infoline("Send character again which should now fit");
228 application.ProcessEvent(event); // append additional character
229 testString.append(testChar);
231 DALI_TEST_EQUALS(testString,textInput.GetText(), TEST_LOCATION);
236 int UtcDaliTextInputSetAndGetNumberOfLines(void)
238 ToolkitTestApplication application;
240 tet_infoline("Ensuring API for setting and getting max number of lines is correct");
242 TextInput textInput = TextInput::New(); // create empty TextInput
244 unsigned int numberOfLines = 1;
246 textInput.SetNumberOfLinesLimit( numberOfLines );
248 DALI_TEST_EQUALS(numberOfLines ,textInput.GetNumberOfLinesLimit(), TEST_LOCATION);
252 int UtcDaliTextInputGetNumberOfCharacters(void)
254 ToolkitTestApplication application;
256 tet_infoline("Testing Getting number of characters");
258 const std::string initialString = "initial text";
259 const std::string newInitialString = "initial text new";
261 TextInput textInput = TextInput::New(); // create empty TextInput
263 textInput.SetInitialText( initialString );
265 tet_infoline("Testing TextInput contains correct number of characters ");
267 DALI_TEST_EQUALS( initialString.size() , textInput.GetNumberOfCharacters(), TEST_LOCATION);
269 tet_infoline("Testing TextInput contains correct number of characters second phase ");
271 textInput.SetInitialText( newInitialString );
273 DALI_TEST_EQUALS( newInitialString.size() , textInput.GetNumberOfCharacters(), TEST_LOCATION);
277 int UtcDaliTextInputSetAndGetPlaceholderText(void)
279 ToolkitTestApplication application;
281 tet_infoline("Testing Setting of PlaceholderText");
283 const std::string initialString = "initial text";
284 const std::string placeholderString = "placeholder";
286 TextInput textInput = TextInput::New(); // create empty TextInput
288 tet_infoline("Testing TextInput is empty at creation ");
290 DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION);
292 tet_infoline("Set placeholder text");
294 textInput.SetPlaceholderText( placeholderString );
296 tet_infoline("Testing TextInput contains placeholder text");
298 DALI_TEST_EQUALS( placeholderString , textInput.GetPlaceholderText(), TEST_LOCATION);
300 tet_infoline("Set initial text which should replace placeholder text");
302 textInput.SetInitialText( initialString );
304 tet_infoline("Testing TextInput contains initial text when placeholder text set");
306 DALI_TEST_EQUALS( initialString,textInput.GetText(), TEST_LOCATION);
310 // Positive test case for a method
311 int UtcDaliTextInputSetInitialText(void)
313 ToolkitTestApplication application;
315 tet_infoline("Testing Setting of Initial Text");
317 const std::string teststring = "test";
319 TextInput textInput = TextInput::New(); // create empty TextInput
321 tet_infoline("Testing TextInput is empty at creation ");
323 DALI_TEST_EQUALS("",textInput.GetText(), TEST_LOCATION);
325 tet_infoline("Set text to TextInput");
327 textInput.SetInitialText(teststring);
329 tet_infoline("Test TextInput contains set text");
331 DALI_TEST_EQUALS(teststring,textInput.GetText(), TEST_LOCATION);
335 int UtcDaliTextInputSetEditableAndIsEditable(void)
337 ToolkitTestApplication application;
339 tet_infoline("Testing SetEditable And IsEditable");
341 const std::string initialString = "initial text";
343 TextInput textInput = TextInput::New(); // create empty TextInput
344 textInput.SetInitialText( initialString );
346 Stage::GetCurrent().Add(textInput);
347 application.SendNotification();
348 application.Render();
350 bool editableStateFalse ( false );
351 bool editableStateTrue ( true );
353 textInput.SetEditable ( editableStateFalse );
354 application.SendNotification();
355 application.Render();
356 DALI_TEST_EQUALS( editableStateFalse, textInput.IsEditable() , TEST_LOCATION);
358 textInput.SetEditable ( editableStateTrue );
359 application.SendNotification();
360 application.Render();
361 DALI_TEST_EQUALS( editableStateTrue, textInput.IsEditable() , TEST_LOCATION);
365 int UtcDaliTextInputSetEditOnTouch(void)
367 ToolkitTestApplication application;
369 tet_infoline("Testing SetEditOnTouch And IsEditOnTouch");
371 TextInput textInput = TextInput::New();
373 bool editableOnTouchOn ( true );
374 bool editableOnTouchOff( false );
376 tet_infoline("Testing SetEditOnTouch disabled");
377 textInput.SetEditOnTouch ( editableOnTouchOff );
378 DALI_TEST_EQUALS( editableOnTouchOff, textInput.IsEditOnTouch() , TEST_LOCATION);
380 tet_infoline("Testing SetEditOnTouch enabled");
381 textInput.SetEditOnTouch ( editableOnTouchOn );
382 DALI_TEST_EQUALS( editableOnTouchOn, textInput.IsEditOnTouch() , TEST_LOCATION);
386 int UtcDaliTextInputSetTextSelectable(void)
388 ToolkitTestApplication application;
390 tet_infoline("Testing SetTextSelectable and IsTextSelectable");
392 const std::string initialString = "initial text";
394 TextInput textInput = TextInput::New();
395 textInput.SetInitialText( initialString );
397 tet_infoline("Testing SetTextSelectable");
398 textInput.SetTextSelectable();
399 DALI_TEST_EQUALS( true, textInput.IsTextSelectable() , TEST_LOCATION);
400 textInput.SetTextSelectable( false );
401 DALI_TEST_EQUALS( false, textInput.IsTextSelectable() , TEST_LOCATION);
405 int UtcDaliTextInputTextSelection(void)
407 ToolkitTestApplication application;
409 tet_infoline("Testing Text Selection");
411 const std::string initialString = "initial text";
413 TextInput textInput = TextInput::New();
414 textInput.SetInitialText( initialString );
416 Stage::GetCurrent().Add(textInput);
417 application.SendNotification();
418 application.Render();
420 textInput.SetEditable( true );
422 tet_infoline("Testing IsTextSelected negative");
423 DALI_TEST_EQUALS( false, textInput.IsTextSelected(), TEST_LOCATION);
425 textInput.SelectText(1,7);
426 DALI_TEST_EQUALS( true, textInput.IsTextSelected(), TEST_LOCATION);
428 textInput.DeSelectText();
429 DALI_TEST_EQUALS( false, textInput.IsTextSelected(), TEST_LOCATION);
433 int UtcDaliTextInputEnableGrabHandleAndIsGrabHandleEnabled(void)
435 ToolkitTestApplication application;
437 TextInput textInput = TextInput::New();
439 bool grabHandleState = false;
441 textInput.EnableGrabHandle( grabHandleState );
443 DALI_TEST_EQUALS( grabHandleState, textInput.IsGrabHandleEnabled(), TEST_LOCATION);
445 grabHandleState = true;
446 textInput.EnableGrabHandle( grabHandleState );
448 DALI_TEST_EQUALS( grabHandleState, textInput.IsGrabHandleEnabled(), TEST_LOCATION);
453 int UtcDaliTextInputSetAndGetBoundingRectangle(void)
455 ToolkitTestApplication application;
457 TextInput textInput = TextInput::New();
459 Stage::GetCurrent().Add(textInput);
460 Vector2 stageSize = Stage::GetCurrent().GetSize();
462 const Rect<float> boundingRectangle( 100.0f, 100.0f, stageSize.width, stageSize.height );
464 textInput.SetBoundingRectangle( boundingRectangle );
466 const Rect<float> retreievedBoundingRectangle = textInput.GetBoundingRectangle();
468 DALI_TEST_EQUALS( boundingRectangle.x, retreievedBoundingRectangle.x, TEST_LOCATION);
469 DALI_TEST_EQUALS( boundingRectangle.y, retreievedBoundingRectangle.y, TEST_LOCATION);
470 DALI_TEST_EQUALS( boundingRectangle.width, retreievedBoundingRectangle.width, TEST_LOCATION);
471 DALI_TEST_EQUALS( boundingRectangle.height, retreievedBoundingRectangle.height, TEST_LOCATION);
476 int UtcDaliTextInputSetAndGetTextAlignment(void)
478 ToolkitTestApplication application;
480 TextInput textInput = TextInput::New();
481 Stage::GetCurrent().Add(textInput);
482 application.SendNotification();
483 application.Render();
485 textInput.SetTextAlignment(static_cast<Alignment::Type>( Alignment::HorizontalCenter) );
486 application.SendNotification();
487 application.Render();
489 DALI_TEST_CHECK( static_cast<Alignment::Type>( Alignment::HorizontalCenter) & textInput.GetTextAlignment()) ;
494 int UtcDaliTextInputSetSortModifier(void)
496 tet_infoline("Testing SetSortModifier does not cause TextInput failure");
498 ToolkitTestApplication application;
500 TextInput textInput = TextInput::New();
502 const float offsetToUse = 1.5f;
504 textInput.SetSortModifier( offsetToUse );
506 DALI_TEST_CHECK( textInput );
510 int UtcDaliTextInputSetAndGetSnapshotModeEnabled(void)
512 ToolkitTestApplication application;
514 tet_infoline("Testing SetSnapshotModeEnabled and IsSnapshotModeEnabled");
516 TextInput textInput = TextInput::New(); // create empty TextInput
517 bool snapshotMode( true );
518 textInput.SetSnapshotModeEnabled( snapshotMode );
520 DALI_TEST_EQUALS( snapshotMode, textInput.IsSnapshotModeEnabled(), TEST_LOCATION);
522 snapshotMode = false;
523 textInput.SetSnapshotModeEnabled( snapshotMode );
525 DALI_TEST_EQUALS( snapshotMode, textInput.IsSnapshotModeEnabled(), TEST_LOCATION);
530 int UtcDaliTextInputEndSignalEmit(void)
532 ToolkitTestApplication application;
534 tet_infoline("Testing Set editable false emits end signal");
536 TextInput textInput = TextInput::New(); // create empty TextInput
538 Stage::GetCurrent().Add(textInput);
540 textInput.InputFinishedSignal().Connect( &OnEndInput );
542 textInput.SetEditable(true) ;
544 gHasEndSignalBeenReceived = false;
546 textInput.SetEditable(false) ;
548 DALI_TEST_EQUALS(true, gHasEndSignalBeenReceived, TEST_LOCATION);
554 int UtcDaliTextInputStartSignalEmit(void)
556 ToolkitTestApplication application;
558 tet_infoline("Testing SetEditable emits start signal");
560 TextInput textInput = TextInput::New(); // create empty TextInput
562 Stage::GetCurrent().Add(textInput);
564 textInput.InputStartedSignal().Connect( &OnStartInput );
566 gHasStartSignalBeenReceived = false;
568 textInput.SetEditable(true); // Set editable first time
570 DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
572 gHasStartSignalBeenReceived = false;
574 textInput.SetEditable(true); // Set editable second time, signal should not be sent again.
576 DALI_TEST_EQUALS(false, gHasStartSignalBeenReceived, TEST_LOCATION);
578 textInput.SetEditable(false);
580 gHasStartSignalBeenReceived = false;
582 textInput.SetEditable(true); // Set editable again
584 DALI_TEST_EQUALS(true, gHasStartSignalBeenReceived, TEST_LOCATION);
588 int UtcDaliTextInputExceedMaxCharacters(void)
590 ToolkitTestApplication application;
592 tet_infoline("Testing Max characters is obeyed when inputting key events ");
594 TextInput textInput = TextInput::New(); // create empty TextInput
596 Stage::GetCurrent().Add(textInput);
597 textInput.SetMaxCharacterLength(4);
598 textInput.SetInitialText("");
599 textInput.SetEditable(true);
601 application.SendNotification();
602 application.Render();
604 Integration::KeyEvent eventA("a", "a", 0, 0, 0, Integration::KeyEvent::Down );
605 Integration::KeyEvent eventB("b", "b", 0, 0, 0, Integration::KeyEvent::Down );
607 application.ProcessEvent(eventA);
608 application.ProcessEvent(eventB);
609 application.ProcessEvent(eventA);
610 application.ProcessEvent(eventB);
612 application.ProcessEvent(eventA);
613 application.ProcessEvent(eventB);
615 tet_printf( "Get text result : %s\n", textInput.GetText().c_str());
617 DALI_TEST_EQUALS("abab",textInput.GetText(), TEST_LOCATION); // Get text which should be only 4 characters
623 int UtcDaliTextInputSetAndGetFadeBoundary(void)
625 tet_infoline("UtcDaliTextViewSetAndGetFadeBoundary: ");
627 ToolkitTestApplication application;
629 TextView::FadeBoundary fadeBoundary( PixelSize( 0 ), PixelSize( 20 ), PixelSize( 0 ), PixelSize( 10 ) );
631 TextInput textInput = TextInput::New();
632 textInput.SetInitialText( "Hello world!" );
634 Stage::GetCurrent().Add(textInput);
635 application.SendNotification();
636 application.Render();
638 textInput.SetFadeBoundary( fadeBoundary );
640 TextView::FadeBoundary fadeBoundary2 = textInput.GetFadeBoundary();
642 DALI_TEST_EQUALS( fadeBoundary.mLeft, fadeBoundary2.mLeft, TEST_LOCATION );
643 DALI_TEST_EQUALS( fadeBoundary.mRight, fadeBoundary2.mRight, TEST_LOCATION );
644 DALI_TEST_EQUALS( fadeBoundary.mTop, fadeBoundary2.mTop, TEST_LOCATION );
645 DALI_TEST_EQUALS( fadeBoundary.mBottom, fadeBoundary2.mBottom, TEST_LOCATION );
649 int UtcDaliTextInputSetAndGetWidthExceedPolicy(void)
651 ToolkitTestApplication application;
653 tet_infoline("UtcDaliTextInputSetAndGetWidthExceedPolicy: ");
655 const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::Split, TextView::ShrinkToFit };
656 const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
658 TextInput textInput = TextInput::New();
659 textInput.SetInitialText( "Hello world!" );
661 for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
663 textInput.SetWidthExceedPolicy( EXCEED_POLICIES[epIndex] );
665 DALI_TEST_EQUALS( textInput.GetWidthExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
670 int UtcDaliTextInputSetAndGetHeightExceedPolicy(void)
672 ToolkitTestApplication application;
674 tet_infoline("UtcDaliTextInputSetAndGetHeightExceedPolicy: ");
676 const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::ShrinkToFit };
677 const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
679 TextInput textInput = TextInput::New();
680 textInput.SetInitialText( "Hello world!" );
682 for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
684 textInput.SetHeightExceedPolicy( EXCEED_POLICIES[epIndex] );
686 DALI_TEST_EQUALS( textInput.GetHeightExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
691 int UtcDaliTextInputScroll(void)
693 tet_infoline("UtcDaliTextInputScroll: ");
694 ToolkitTestApplication application;
696 // Avoids the frame buffer texture to throw an exception.
697 application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
699 TextInput view = TextInput::New();
700 view.SetMultilinePolicy( TextView::SplitByNewLineChar );
701 view.SetWidthExceedPolicy( TextView::Original );
702 view.SetHeightExceedPolicy( TextView::Original );
703 view.SetTextAlignment( static_cast<Toolkit::Alignment::Type>( Toolkit::Alignment::HorizontalCenter | Toolkit::Alignment::VerticalCenter ) );
704 view.SetInitialText( "Hello world! This is a scroll test." );
705 view.SetSize( 100.f, 100.f );
706 view.SetSnapshotModeEnabled( false );
708 Stage::GetCurrent().Add( view );
710 application.SendNotification();
711 application.Render();
713 DALI_TEST_CHECK( !view.IsScrollEnabled() ); // Scroll should be disabled by default.
715 view.SetScrollEnabled( true );
717 DALI_TEST_CHECK( view.IsScrollEnabled() );
718 DALI_TEST_CHECK( view.IsSnapshotModeEnabled() ); // Scroll should enable snapshot mode.
720 view.SetScrollPosition( Vector2( 400.f, 400.f ) );
722 application.SendNotification();
723 application.Render();
725 const Vector2& scrollPosition = view.GetScrollPosition();
726 DALI_TEST_EQUALS( scrollPosition, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );