| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #include "chrome/browser/renderer_host/gtk_im_context_wrapper.h" | |
| 5 | 4 |
| 6 #include <string> | 5 #include <string> |
| 7 #include <utility> | 6 #include <utility> |
| 8 #include <vector> | |
| 9 | 7 |
| 10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 11 #include "base/logging.h" | 9 #include "base/logging.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/skia/include/core/SkColor.h" | 11 #include "ui/base/gtk/gtk_im_context_util.h" |
| 12 #include "ui/base/ime/composition_text.h" |
| 14 | 13 |
| 15 namespace { | 14 namespace { |
| 16 | 15 |
| 17 struct AttributeInfo { | 16 struct AttributeInfo { |
| 18 int type; | 17 int type; |
| 19 int value; | 18 int value; |
| 20 int start_offset; | 19 int start_offset; |
| 21 int end_offset; | 20 int end_offset; |
| 22 }; | 21 }; |
| 23 | 22 |
| 24 struct Underline { | 23 struct Underline { |
| 25 unsigned startOffset; | 24 unsigned start_offset; |
| 26 unsigned endOffset; | 25 unsigned end_offset; |
| 27 uint32 color; | 26 uint32 color; |
| 28 bool thick; | 27 bool thick; |
| 29 }; | 28 }; |
| 30 | 29 |
| 31 struct TestData { | 30 struct TestData { |
| 32 const char* text; | 31 const char* text; |
| 33 const AttributeInfo attrs[10]; | 32 const AttributeInfo attrs[10]; |
| 34 const Underline underlines[10]; | 33 const Underline underlines[10]; |
| 35 }; | 34 }; |
| 36 | 35 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 }, | 72 }, |
| 74 | 73 |
| 75 // Default underline. | 74 // Default underline. |
| 76 { "One Two Three", | 75 { "One Two Three", |
| 77 { { 0, 0, 0, 0 } }, | 76 { { 0, 0, 0, 0 } }, |
| 78 { { 0, 13, SK_ColorBLACK, false }, | 77 { { 0, 13, SK_ColorBLACK, false }, |
| 79 { 0, 0, 0, false } } | 78 { 0, 0, 0, false } } |
| 80 }, | 79 }, |
| 81 | 80 |
| 82 // Unicode, including non-BMP characters: "123你好𠀀𠀁一丁 456" | 81 // Unicode, including non-BMP characters: "123你好𠀀𠀁一丁 456" |
| 83 { "123\xE4\xBD\xA0\xE5\xA5\xBD\xF0\xA0\x80\x80\xF0\xA0\x80\x81\xE4\xB8\x80\xE4
\xB8\x81 456", | 82 { "123\xE4\xBD\xA0\xE5\xA5\xBD\xF0\xA0\x80\x80\xF0\xA0\x80\x81\xE4\xB8\x80" |
| 83 "\xE4\xB8\x81 456", |
| 84 { { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 0, 3 }, | 84 { { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 0, 3 }, |
| 85 { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 3, 5 }, | 85 { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 3, 5 }, |
| 86 { PANGO_ATTR_BACKGROUND, 0, 5, 7 }, | 86 { PANGO_ATTR_BACKGROUND, 0, 5, 7 }, |
| 87 { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 7, 13 }, | 87 { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 7, 13 }, |
| 88 { 0, 0, 0, 0 } }, | 88 { 0, 0, 0, 0 } }, |
| 89 { { 0, 3, SK_ColorBLACK, false }, | 89 { { 0, 3, SK_ColorBLACK, false }, |
| 90 { 3, 5, SK_ColorBLACK, false }, | 90 { 3, 5, SK_ColorBLACK, false }, |
| 91 { 5, 9, SK_ColorBLACK, true }, | 91 { 5, 9, SK_ColorBLACK, true }, |
| 92 { 9, 15, SK_ColorBLACK, false }, | 92 { 9, 15, SK_ColorBLACK, false }, |
| 93 { 0, 0, 0, false } } | 93 { 0, 0, 0, false } } |
| 94 }, | 94 }, |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 void CompareUnderline(const Underline& a, | 97 void CompareUnderline(const Underline& a, |
| 98 const WebKit::WebCompositionUnderline& b) { | 98 const ui::CompositionUnderline& b) { |
| 99 EXPECT_EQ(a.startOffset, b.startOffset); | 99 EXPECT_EQ(a.start_offset, b.start_offset); |
| 100 EXPECT_EQ(a.endOffset, b.endOffset); | 100 EXPECT_EQ(a.end_offset, b.end_offset); |
| 101 EXPECT_EQ(a.color, b.color); | 101 EXPECT_EQ(a.color, b.color); |
| 102 EXPECT_EQ(a.thick, b.thick); | 102 EXPECT_EQ(a.thick, b.thick); |
| 103 } | 103 } |
| 104 | 104 |
| 105 class GtkIMContextWrapperTest : public testing::Test { | 105 class GtkIMContextWrapperTest : public testing::Test { |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 } // namespace | 108 TEST(GtkIMContextUtilTest, ExtractCompositionText) { |
| 109 | |
| 110 TEST(GtkIMContextWrapperTest, ExtractCompositionInfo) { | |
| 111 for (size_t i = 0; i < arraysize(kTestData); ++i) { | 109 for (size_t i = 0; i < arraysize(kTestData); ++i) { |
| 112 const char* text = kTestData[i].text; | 110 const char* text = kTestData[i].text; |
| 113 const AttributeInfo* attrs = kTestData[i].attrs; | 111 const AttributeInfo* attrs = kTestData[i].attrs; |
| 114 SCOPED_TRACE(testing::Message() << "Testing:" << i | 112 SCOPED_TRACE(testing::Message() << "Testing:" << i |
| 115 << " text:" << text); | 113 << " text:" << text); |
| 116 | 114 |
| 117 PangoAttrList* pango_attrs = pango_attr_list_new(); | 115 PangoAttrList* pango_attrs = pango_attr_list_new(); |
| 118 for (size_t a = 0; attrs[a].type; ++a) { | 116 for (size_t a = 0; attrs[a].type; ++a) { |
| 119 PangoAttribute* pango_attr = NULL; | 117 PangoAttribute* pango_attr = NULL; |
| 120 switch (attrs[a].type) { | 118 switch (attrs[a].type) { |
| 121 case PANGO_ATTR_UNDERLINE: | 119 case PANGO_ATTR_UNDERLINE: |
| 122 pango_attr = pango_attr_underline_new( | 120 pango_attr = pango_attr_underline_new( |
| 123 static_cast<PangoUnderline>(attrs[a].value)); | 121 static_cast<PangoUnderline>(attrs[a].value)); |
| 124 break; | 122 break; |
| 125 case PANGO_ATTR_BACKGROUND: | 123 case PANGO_ATTR_BACKGROUND: |
| 126 pango_attr = pango_attr_background_new(0, 0, 0); | 124 pango_attr = pango_attr_background_new(0, 0, 0); |
| 127 break; | 125 break; |
| 128 default: | 126 default: |
| 129 NOTREACHED(); | 127 NOTREACHED(); |
| 130 } | 128 } |
| 131 pango_attr->start_index = | 129 pango_attr->start_index = |
| 132 g_utf8_offset_to_pointer(text, attrs[a].start_offset) - text; | 130 g_utf8_offset_to_pointer(text, attrs[a].start_offset) - text; |
| 133 pango_attr->end_index = | 131 pango_attr->end_index = |
| 134 g_utf8_offset_to_pointer(text, attrs[a].end_offset) - text; | 132 g_utf8_offset_to_pointer(text, attrs[a].end_offset) - text; |
| 135 pango_attr_list_insert(pango_attrs, pango_attr); | 133 pango_attr_list_insert(pango_attrs, pango_attr); |
| 136 } | 134 } |
| 137 | 135 |
| 138 string16 utf16_text; | 136 ui::CompositionText result; |
| 139 std::vector<WebKit::WebCompositionUnderline> results; | 137 ui::ExtractCompositionTextFromGtkPreedit(text, pango_attrs, 0, &result); |
| 140 int selection_start; | |
| 141 int selection_end; | |
| 142 | |
| 143 GtkIMContextWrapper::ExtractCompositionInfo(text, pango_attrs, 0, | |
| 144 &utf16_text, &results, &selection_start, &selection_end); | |
| 145 | 138 |
| 146 const Underline* underlines = kTestData[i].underlines; | 139 const Underline* underlines = kTestData[i].underlines; |
| 147 for (size_t u = 0; underlines[u].color && u < results.size(); ++u) { | 140 for (size_t u = 0; underlines[u].color && |
| 141 u < result.underlines.size(); ++u) { |
| 148 SCOPED_TRACE(testing::Message() << "Underline:" << u); | 142 SCOPED_TRACE(testing::Message() << "Underline:" << u); |
| 149 CompareUnderline(underlines[u], results[u]); | 143 CompareUnderline(underlines[u], result.underlines[u]); |
| 150 } | 144 } |
| 151 | 145 |
| 152 pango_attr_list_unref(pango_attrs); | 146 pango_attr_list_unref(pango_attrs); |
| 153 } | 147 } |
| 154 } | 148 } |
| 149 |
| 150 } // namespace |
| OLD | NEW |