summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/gtk_im_context_wrapper_unittest.cc
blob: 5b8fcb5e95df855edb34c696123aa4931b9e0192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/renderer_host/gtk_im_context_wrapper.h"

#include <string>
#include <utility>
#include <vector>

#include "base/basictypes.h"
#include "base/logging.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"

namespace {

struct AttributeInfo {
  int type;
  int value;
  int start_offset;
  int end_offset;
};

struct Underline {
  unsigned startOffset;
  unsigned endOffset;
  uint32 color;
  bool thick;
};

struct TestData {
  const char* text;
  const AttributeInfo attrs[10];
  const Underline underlines[10];
};

const TestData kTestData[] = {
  // Normal case
  { "One Two Three",
    { { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 0, 3 },
      { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_DOUBLE, 4, 7 },
      { PANGO_ATTR_BACKGROUND, 0, 4, 7 },
      { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 8, 13 },
      { 0, 0, 0, 0 } },
    { { 0, 3, SK_ColorBLACK, false },
      { 4, 7, SK_ColorBLACK, true },
      { 8, 13, SK_ColorBLACK, false },
      { 0, 0, 0, false } }
  },

  // Offset overflow.
  { "One Two Three",
    { { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 0, 3 },
      { PANGO_ATTR_BACKGROUND, 0, 4, 7 },
      { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 8, 20 },
      { 0, 0, 0, 0 } },
    { { 0, 3, SK_ColorBLACK, false },
      { 4, 7, SK_ColorBLACK, true },
      { 8, 13, SK_ColorBLACK, false },
      { 0, 0, 0, false} }
  },

  // Error underline.
  { "One Two Three",
    { { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 0, 3 },
      { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_ERROR, 4, 7 },
      { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 8, 13 },
      { 0, 0, 0, 0 } },
    { { 0, 3, SK_ColorBLACK, false },
      { 4, 7, SK_ColorRED, false },
      { 8, 13, SK_ColorBLACK, false },
      { 0, 0, 0, false} }
  },

  // Default underline.
  { "One Two Three",
    { { 0, 0, 0, 0 } },
    { { 0, 13, SK_ColorBLACK, false },
      { 0, 0, 0, false } }
  },

  // Unicode, including non-BMP characters: "123你好𠀀𠀁一丁 456"
  { "123\xE4\xBD\xA0\xE5\xA5\xBD\xF0\xA0\x80\x80\xF0\xA0\x80\x81\xE4\xB8\x80\xE4\xB8\x81 456",
    { { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 0, 3 },
      { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 3, 5 },
      { PANGO_ATTR_BACKGROUND, 0, 5, 7 },
      { PANGO_ATTR_UNDERLINE, PANGO_UNDERLINE_SINGLE, 7, 13 },
      { 0, 0, 0, 0 } },
    { { 0, 3, SK_ColorBLACK, false },
      { 3, 5, SK_ColorBLACK, false },
      { 5, 9, SK_ColorBLACK, true },
      { 9, 15, SK_ColorBLACK, false },
      { 0, 0, 0, false } }
  },
};

void CompareUnderline(const Underline& a,
                      const WebKit::WebCompositionUnderline& b) {
  EXPECT_EQ(a.startOffset, b.startOffset);
  EXPECT_EQ(a.endOffset, b.endOffset);
  EXPECT_EQ(a.color, b.color);
  EXPECT_EQ(a.thick, b.thick);
}

class GtkIMContextWrapperTest : public testing::Test {
};

}  // namespace

TEST(GtkIMContextWrapperTest, ExtractCompositionInfo) {
  for (size_t i = 0; i < arraysize(kTestData); ++i) {
    const char* text = kTestData[i].text;
    const AttributeInfo* attrs = kTestData[i].attrs;
    SCOPED_TRACE(testing::Message() << "Testing:" << i
                 << " text:" << text);

    PangoAttrList* pango_attrs = pango_attr_list_new();
    for (size_t a = 0; attrs[a].type; ++a) {
      PangoAttribute* pango_attr = NULL;
      switch (attrs[a].type) {
        case PANGO_ATTR_UNDERLINE:
          pango_attr = pango_attr_underline_new(
              static_cast<PangoUnderline>(attrs[a].value));
          break;
        case PANGO_ATTR_BACKGROUND:
          pango_attr = pango_attr_background_new(0, 0, 0);
          break;
        default:
          NOTREACHED();
      }
      pango_attr->start_index =
          g_utf8_offset_to_pointer(text, attrs[a].start_offset) - text;
      pango_attr->end_index =
          g_utf8_offset_to_pointer(text, attrs[a].end_offset) - text;
      pango_attr_list_insert(pango_attrs, pango_attr);
    }

    string16 utf16_text;
    std::vector<WebKit::WebCompositionUnderline> results;
    int selection_start;
    int selection_end;

    GtkIMContextWrapper::ExtractCompositionInfo(text, pango_attrs, 0,
        &utf16_text, &results, &selection_start, &selection_end);

    const Underline* underlines = kTestData[i].underlines;
    for (size_t u = 0; underlines[u].color && u < results.size(); ++u) {
      SCOPED_TRACE(testing::Message() << "Underline:" << u);
      CompareUnderline(underlines[u], results[u]);
    }

    pango_attr_list_unref(pango_attrs);
  }
}