2 * Copyright (c) 2022 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.
22 #include <dali-toolkit-test-suite-utils.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/controls/text-controls/text-spannable.h>
25 #include <dali-toolkit/devel-api/text/spannable-string.h>
26 #include <dali-toolkit/devel-api/text/spans/foreground-color-span.h>
27 #include <dali-toolkit/internal/controls/text-controls/text-editor-impl.h>
28 #include <dali-toolkit/internal/controls/text-controls/text-field-impl.h>
29 #include <dali-toolkit/internal/controls/text-controls/text-label-impl.h>
32 using namespace Toolkit;
34 Text::SpannableString CreateSpannableStringForForegroundColorSpan()
36 Text::SpannableString spannableString = Text::SpannableString::New("Hello مرحبا");
37 DALI_TEST_CHECK(spannableString);
39 auto isAddedGreen = spannableString.AttachSpan(
40 Text::ForegroundColorSpan::New(Color::GREEN),
41 Text::Range::New(5u, 7u));
42 DALI_TEST_CHECK(isAddedGreen);
44 auto isAddedBlue = spannableString.AttachSpan(
45 Text::ForegroundColorSpan::New(Color::BLUE),
46 Text::Range::New(1u, 2u));
47 DALI_TEST_CHECK(isAddedBlue);
49 return spannableString;
52 void CheckColorIndices(const Text::ColorIndex* const colorIndicesBuffer,
53 uint32_t numberOfIndices,
54 std::vector<uint32_t> indicesToCheck,
55 std::vector<uint32_t> expectedValues)
57 DALI_TEST_CHECK(colorIndicesBuffer);
59 for(uint32_t i = 0u; i < numberOfIndices; i++)
61 DALI_TEST_EQUALS(colorIndicesBuffer[indicesToCheck[i]], expectedValues[i], TEST_LOCATION);
65 int UtcDaliToolkitTextLabelSetSpannedText(void)
67 ToolkitTestApplication application;
68 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText");
70 TextLabel textLabel = TextLabel::New();
71 DALI_TEST_CHECK(textLabel);
72 application.GetScene().Add(textLabel);
74 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
76 Text::SetSpannedText(textLabel, spannableString);
78 application.SendNotification();
81 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
82 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
84 CheckColorIndices(colorIndicesBuffer, 6u, {0u, 1u, 2u, 5u, 7u, 10u}, {0u, 2u, 2u, 1u, 1u, 0u});
89 int UtcDaliToolkitTextEditorSetSpannedText(void)
91 ToolkitTestApplication application;
92 tet_infoline(" UtcDaliToolkitTextEditorSetSpannedText");
94 TextEditor textEditor = TextEditor::New();
95 DALI_TEST_CHECK(textEditor);
96 application.GetScene().Add(textEditor);
98 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
100 Text::SetSpannedText(textEditor, spannableString);
102 application.SendNotification();
103 application.Render();
105 Toolkit::Internal::TextEditor& labelImpl = GetImpl(textEditor);
106 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
108 CheckColorIndices(colorIndicesBuffer, 6u, {0u, 1u, 2u, 5u, 7u, 10u}, {0u, 2u, 2u, 1u, 1u, 0u});
113 int UtcDaliToolkitTextFieldSetSpannedText(void)
115 ToolkitTestApplication application;
116 tet_infoline(" UtcDaliToolkitTextFieldSetSpannedText");
118 TextField textField = TextField::New();
119 DALI_TEST_CHECK(textField);
120 application.GetScene().Add(textField);
122 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
124 Text::SetSpannedText(textField, spannableString);
126 application.SendNotification();
127 application.Render();
129 Toolkit::Internal::TextField& labelImpl = GetImpl(textField);
130 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
132 CheckColorIndices(colorIndicesBuffer, 6u, {0u, 1u, 2u, 5u, 7u, 10u}, {0u, 2u, 2u, 1u, 1u, 0u});