2 * Copyright (c) 2020 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>
22 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/internal/controls/text-controls/text-field-impl.h>
25 #include <dali-toolkit/internal/text/text-controller.h>
26 #include <dali-toolkit/internal/text/text-controller-impl.h>
29 using namespace Toolkit;
32 int UtcDaliTextFieldMultipleBackgroundText(void)
34 ToolkitTestApplication application;
35 tet_infoline( "UtcDaliTextFieldMultipleBackgroundText" );
37 // Create a text field
38 TextField textField = TextField::New();
39 textField.SetProperty( Actor::Property::SIZE, Vector2( 400.f, 60.f ) );
40 textField.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
41 textField.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
43 // Add the text field to the stage
44 application.GetScene().Add( textField );
46 application.SendNotification();
49 Toolkit::Internal::TextField& textFieldImpl = GetImpl( textField );
50 ControllerPtr controller = textFieldImpl.GetTextController();
51 Controller::Impl& controllerImpl = Controller::Impl::GetImplementation( *controller.Get() );
53 // Add multiple background colors for the text.
54 ColorRun backgroundColorRun1;
55 backgroundColorRun1.characterRun.characterIndex = 0u;
56 backgroundColorRun1.characterRun.numberOfCharacters = 1u;
57 backgroundColorRun1.color = Color::RED;
58 controllerImpl.mModel->mLogicalModel->mBackgroundColorRuns.PushBack( backgroundColorRun1 );
60 ColorRun backgroundColorRun2;
61 backgroundColorRun2.characterRun.characterIndex = 5u;
62 backgroundColorRun2.characterRun.numberOfCharacters = 8u;
63 backgroundColorRun2.color = Color::CYAN;
64 controllerImpl.mModel->mLogicalModel->mBackgroundColorRuns.PushBack( backgroundColorRun2 );
66 ColorRun backgroundColorRun3;
67 backgroundColorRun3.characterRun.characterIndex = 23u;
68 backgroundColorRun3.characterRun.numberOfCharacters = 6u;
69 backgroundColorRun3.color = Color::GREEN;
70 controllerImpl.mModel->mLogicalModel->mBackgroundColorRuns.PushBack( backgroundColorRun3 );
72 // Check the case where there is only one character in the text
73 controller->SetText( "S" );
75 application.SendNotification();
78 // The offscreen root actor should have one child: the renderable.
79 Actor stencil = textField.GetChildAt( 0u );
80 DALI_TEST_CHECK( stencil.GetChildCount() == 1u );
82 // The renderable actor should have two children: the text and the background.
83 Actor renderableActor = stencil.GetChildAt( 0u );
84 DALI_TEST_CHECK( renderableActor.GetChildCount() == 2u );
86 // Check that the background is created
87 Actor backgroundActor = renderableActor.GetChildAt( 0u );
88 DALI_TEST_CHECK( backgroundActor );
89 DALI_TEST_CHECK( backgroundActor.GetProperty< std::string >( Dali::Actor::Property::NAME ) == "TextBackgroundColorActor" );
91 // Change the text to contain more characters
92 controller->SetText( "Text Multiple Background Test" );
94 application.SendNotification();
97 // Highlight the whole text
98 textFieldImpl.SelectWholeText();
100 application.SendNotification();
101 application.Render();
103 // Now the offscreen root actor should have three children: the renderable, the highlight, and the background.
104 DALI_TEST_CHECK( stencil.GetChildCount() == 3u );
105 // The renderable actor should have one child only: the text
106 DALI_TEST_CHECK( renderableActor.GetChildCount() == 1u );
108 // The background should now be lowered below the highlight
109 backgroundActor = stencil.GetChildAt( 0u );
110 DALI_TEST_CHECK( backgroundActor );
111 DALI_TEST_CHECK( backgroundActor.GetProperty< std::string >( Dali::Actor::Property::NAME ) == "TextBackgroundColorActor" );
116 int UtcDaliTextFieldSelectText(void)
118 ToolkitTestApplication application;
119 tet_infoline( "UtcDaliTextFieldSelectText" );
121 // Create a text field
122 TextField textField = TextField::New();
123 textField.SetProperty( Actor::Property::SIZE, Vector2( 400.f, 60.f ) );
124 textField.SetProperty( TextField::Property::TEXT, "Hello World" );
126 // Add the text field to the stage
127 application.GetScene().Add( textField );
129 application.SendNotification();
130 application.Render();
132 Toolkit::Internal::TextField& textFieldImpl = GetImpl( textField );
134 application.SendNotification();
135 application.Render();
137 // Highlight the whole text
138 textFieldImpl.SelectWholeText();
140 application.SendNotification();
141 application.Render();
143 DALI_TEST_CHECK( textFieldImpl.GetSelectedText() == "Hello World" );
146 textFieldImpl.SelectNone();
148 application.SendNotification();
149 application.Render();
151 DALI_TEST_CHECK( textFieldImpl.GetSelectedText() == "" );
156 int UtcDaliTextFieldMarkupUnderline(void)
158 ToolkitTestApplication application;
159 tet_infoline(" UtcDaliTextFieldMarkupUnderline ");
161 TextField textField = TextField::New();
163 application.GetScene().Add( textField );
165 textField.SetProperty( TextField::Property::TEXT, "<u>ABC</u>EF<u>GH</u>" );
166 textField.SetProperty( TextField ::Property::ENABLE_MARKUP, true );
168 application.SendNotification();
169 application.Render();
171 uint32_t expectedNumberOfUnderlinedGlyphs = 5u;
173 Toolkit::Internal::TextField& textFieldImpl = GetImpl( textField );
174 const Text::Length numberOfUnderlineRuns = textFieldImpl.getController()->GetTextModel()->GetNumberOfUnderlineRuns();
176 DALI_TEST_EQUALS( numberOfUnderlineRuns, expectedNumberOfUnderlinedGlyphs, TEST_LOCATION );
178 Vector<GlyphRun> underlineRuns;
179 underlineRuns.Resize(numberOfUnderlineRuns);
180 textFieldImpl.getController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
183 DALI_TEST_EQUALS( underlineRuns[0u].glyphIndex, 0u, TEST_LOCATION);
184 DALI_TEST_EQUALS( underlineRuns[1u].glyphIndex, 1u, TEST_LOCATION);
185 DALI_TEST_EQUALS( underlineRuns[2u].glyphIndex, 2u, TEST_LOCATION);
188 DALI_TEST_EQUALS( underlineRuns[3u].glyphIndex, 5u, TEST_LOCATION);
189 DALI_TEST_EQUALS( underlineRuns[4u].glyphIndex, 6u, TEST_LOCATION);