1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/base/ime/composition_text_util_pango.h"
7 #include <pango/pango-attributes.h>
12 #include "base/basictypes.h"
13 #include "base/logging.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/ime/composition_text.h"
19 struct AttributeInfo
{
27 unsigned start_offset
;
35 const AttributeInfo attrs
[10];
36 const Underline underlines
[10];
39 const TestData kTestData
[] = {
42 { { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_SINGLE
, 0, 3 },
43 { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_DOUBLE
, 4, 7 },
44 { PANGO_ATTR_BACKGROUND
, 0, 4, 7 },
45 { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_SINGLE
, 8, 13 },
47 { { 0, 3, SK_ColorBLACK
, false },
48 { 4, 7, SK_ColorBLACK
, true },
49 { 8, 13, SK_ColorBLACK
, false },
55 { { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_SINGLE
, 0, 3 },
56 { PANGO_ATTR_BACKGROUND
, 0, 4, 7 },
57 { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_SINGLE
, 8, 20 },
59 { { 0, 3, SK_ColorBLACK
, false },
60 { 4, 7, SK_ColorBLACK
, true },
61 { 8, 13, SK_ColorBLACK
, false },
67 { { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_SINGLE
, 0, 3 },
68 { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_ERROR
, 4, 7 },
69 { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_SINGLE
, 8, 13 },
71 { { 0, 3, SK_ColorBLACK
, false },
72 { 4, 7, SK_ColorRED
, false },
73 { 8, 13, SK_ColorBLACK
, false },
80 { { 0, 13, SK_ColorBLACK
, false },
84 // Unicode, including non-BMP characters: "123你好𠀀𠀁一丁 456"
85 { "123\xE4\xBD\xA0\xE5\xA5\xBD\xF0\xA0\x80\x80\xF0\xA0\x80\x81\xE4\xB8\x80"
87 { { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_SINGLE
, 0, 3 },
88 { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_SINGLE
, 3, 5 },
89 { PANGO_ATTR_BACKGROUND
, 0, 5, 7 },
90 { PANGO_ATTR_UNDERLINE
, PANGO_UNDERLINE_SINGLE
, 7, 13 },
92 { { 0, 3, SK_ColorBLACK
, false },
93 { 3, 5, SK_ColorBLACK
, false },
94 { 5, 9, SK_ColorBLACK
, true },
95 { 9, 15, SK_ColorBLACK
, false },
100 void CompareUnderline(const Underline
& a
,
101 const ui::CompositionUnderline
& b
) {
102 EXPECT_EQ(a
.start_offset
, b
.start_offset
);
103 EXPECT_EQ(a
.end_offset
, b
.end_offset
);
104 EXPECT_EQ(a
.color
, b
.color
);
105 EXPECT_EQ(a
.thick
, b
.thick
);
108 TEST(CompositionTextUtilPangoTest
, ExtractCompositionText
) {
109 for (size_t i
= 0; i
< arraysize(kTestData
); ++i
) {
110 const char* text
= kTestData
[i
].text
;
111 const AttributeInfo
* attrs
= kTestData
[i
].attrs
;
112 SCOPED_TRACE(testing::Message() << "Testing:" << i
113 << " text:" << text
);
115 PangoAttrList
* pango_attrs
= pango_attr_list_new();
116 for (size_t a
= 0; attrs
[a
].type
; ++a
) {
117 PangoAttribute
* pango_attr
= NULL
;
118 switch (attrs
[a
].type
) {
119 case PANGO_ATTR_UNDERLINE
:
120 pango_attr
= pango_attr_underline_new(
121 static_cast<PangoUnderline
>(attrs
[a
].value
));
123 case PANGO_ATTR_BACKGROUND
:
124 pango_attr
= pango_attr_background_new(0, 0, 0);
129 pango_attr
->start_index
=
130 g_utf8_offset_to_pointer(text
, attrs
[a
].start_offset
) - text
;
131 pango_attr
->end_index
=
132 g_utf8_offset_to_pointer(text
, attrs
[a
].end_offset
) - text
;
133 pango_attr_list_insert(pango_attrs
, pango_attr
);
136 ui::CompositionText result
;
137 ui::ExtractCompositionTextFromGtkPreedit(text
, pango_attrs
, 0, &result
);
139 const Underline
* underlines
= kTestData
[i
].underlines
;
140 for (size_t u
= 0; underlines
[u
].color
&&
141 u
< result
.underlines
.size(); ++u
) {
142 SCOPED_TRACE(testing::Message() << "Underline:" << u
);
143 CompareUnderline(underlines
[u
], result
.underlines
[u
]);
146 pango_attr_list_unref(pango_attrs
);