[dali_1.9.13] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextField-internal.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <iostream>
19 #include <stdlib.h>
20
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
23
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>
27
28 using namespace Dali;
29 using namespace Toolkit;
30 using namespace Text;
31
32 int UtcDaliTextFieldMultipleBackgroundText(void)
33 {
34   ToolkitTestApplication application;
35   tet_infoline( "UtcDaliTextFieldMultipleBackgroundText" );
36
37   // Create a text field
38   TextField textField = TextField::New();
39   textField.SetSize( 400.f, 60.f );
40   textField.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
41   textField.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
42
43   // Add the text field to the stage
44   Stage::GetCurrent().Add( textField );
45
46   application.SendNotification();
47   application.Render();
48
49   Toolkit::Internal::TextField& textFieldImpl = GetImpl( textField );
50   ControllerPtr controller = textFieldImpl.GetTextController();
51   Controller::Impl& controllerImpl = Controller::Impl::GetImplementation( *controller.Get() );
52
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 );
59
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 );
65
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 );
71
72   // Check the case where there is only one character in the text
73   controller->SetText( "S" );
74
75   application.SendNotification();
76   application.Render();
77
78   // The offscreen root actor should have one child: the renderable.
79   Actor stencil = textField.GetChildAt( 0u );
80   DALI_TEST_CHECK( stencil.GetChildCount() == 1u );
81
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 );
85
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" );
90
91   // Change the text to contain more characters
92   controller->SetText( "Text Multiple Background Test" );
93
94   application.SendNotification();
95   application.Render();
96
97   // Highlight the whole text
98   textFieldImpl.SelectWholeText();
99
100   application.SendNotification();
101   application.Render();
102
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 );
107
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" );
112
113   END_TEST;
114 }