Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ui / base / ime / composition_text_util_pango_unittest.cc
blob957adf1d4a7287bf453fec2ac2b9e3d34cfe9ee8
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>
9 #include <string>
10 #include <utility>
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"
17 namespace {
19 struct AttributeInfo {
20 int type;
21 int value;
22 int start_offset;
23 int end_offset;
26 struct Underline {
27 unsigned start_offset;
28 unsigned end_offset;
29 uint32 color;
30 bool thick;
33 struct TestData {
34 const char* text;
35 const AttributeInfo attrs[10];
36 const Underline underlines[10];
39 const TestData kTestData[] = {
40 // Normal case
41 { "One Two Three",
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 },
46 { 0, 0, 0, 0 } },
47 { { 0, 3, SK_ColorBLACK, false },
48 { 4, 7, SK_ColorBLACK, true },
49 { 8, 13, SK_ColorBLACK, false },
50 { 0, 0, 0, false } }
53 // Offset overflow.
54 { "One Two Three",
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 },
58 { 0, 0, 0, 0 } },
59 { { 0, 3, SK_ColorBLACK, false },
60 { 4, 7, SK_ColorBLACK, true },
61 { 8, 13, SK_ColorBLACK, false },
62 { 0, 0, 0, false} }
65 // Error underline.
66 { "One Two Three",
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 },
70 { 0, 0, 0, 0 } },
71 { { 0, 3, SK_ColorBLACK, false },
72 { 4, 7, SK_ColorRED, false },
73 { 8, 13, SK_ColorBLACK, false },
74 { 0, 0, 0, false} }
77 // Default underline.
78 { "One Two Three",
79 { { 0, 0, 0, 0 } },
80 { { 0, 13, SK_ColorBLACK, false },
81 { 0, 0, 0, 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"
86 "\xE4\xB8\x81 456",
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 },
91 { 0, 0, 0, 0 } },
92 { { 0, 3, SK_ColorBLACK, false },
93 { 3, 5, SK_ColorBLACK, false },
94 { 5, 9, SK_ColorBLACK, true },
95 { 9, 15, SK_ColorBLACK, false },
96 { 0, 0, 0, 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));
122 break;
123 case PANGO_ATTR_BACKGROUND:
124 pango_attr = pango_attr_background_new(0, 0, 0);
125 break;
126 default:
127 NOTREACHED();
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);
150 } // namespace