1// Copyright (c) 2012 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.
4// TODO(nona): Add more tests.
5
6#include "chromeos/dbus/ibus/ibus_text.h"
7
8#include <string>
9
10#include "base/compiler_specific.h"
11#include "base/logging.h"
12#include "base/memory/scoped_ptr.h"
13#include "chromeos/dbus/ibus/ibus_object.h"
14#include "dbus/message.h"
15#include "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace chromeos {
19
20TEST(IBusTextTest, WriteReadTest) {
21  const char kSampleText[] = "Sample Text";
22  const char kAnnotation[] = "Annotation";
23  const char kDescriptionTitle[] = "Description Title";
24  const char kDescriptionBody[] = "Description Body";
25  const IBusText::UnderlineAttribute kSampleUnderlineAttribute1 = {
26    IBusText::IBUS_TEXT_UNDERLINE_SINGLE, 10, 20};
27
28  const IBusText::UnderlineAttribute kSampleUnderlineAttribute2 = {
29    IBusText::IBUS_TEXT_UNDERLINE_DOUBLE, 11, 21};
30
31  const IBusText::UnderlineAttribute kSampleUnderlineAttribute3 = {
32    IBusText::IBUS_TEXT_UNDERLINE_ERROR, 12, 22};
33
34  const IBusText::SelectionAttribute kSampleSelectionAttribute = {30, 40};
35
36  // Make IBusText
37  IBusText text;
38  text.set_text(kSampleText);
39  text.set_annotation(kAnnotation);
40  text.set_description_title(kDescriptionTitle);
41  text.set_description_body(kDescriptionBody);
42  std::vector<IBusText::UnderlineAttribute>* underline_attributes =
43      text.mutable_underline_attributes();
44  underline_attributes->push_back(kSampleUnderlineAttribute1);
45  underline_attributes->push_back(kSampleUnderlineAttribute2);
46  underline_attributes->push_back(kSampleUnderlineAttribute3);
47  std::vector<IBusText::SelectionAttribute>* selection_attributes =
48      text.mutable_selection_attributes();
49  selection_attributes->push_back(kSampleSelectionAttribute);
50
51  // Write to Response object.
52  scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
53  dbus::MessageWriter writer(response.get());
54  AppendIBusText(text, &writer);
55
56  // Read from Response object.
57  dbus::MessageReader reader(response.get());
58  IBusText expected_text;
59  ASSERT_TRUE(PopIBusText(&reader, &expected_text));
60  EXPECT_EQ(kSampleText, expected_text.text());
61  EXPECT_EQ(kAnnotation, expected_text.annotation());
62  EXPECT_EQ(kDescriptionTitle, expected_text.description_title());
63  EXPECT_EQ(kDescriptionBody, expected_text.description_body());
64  EXPECT_EQ(3U, expected_text.underline_attributes().size());
65  EXPECT_EQ(1U, expected_text.selection_attributes().size());
66}
67
68TEST(IBusTextTest, StringAsIBusTextTest) {
69  const char kSampleText[] = "Sample Text";
70
71  // Write to Response object.
72  scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
73  dbus::MessageWriter writer(response.get());
74  AppendStringAsIBusText(kSampleText, &writer);
75
76  // Read from Response object.
77  dbus::MessageReader reader(response.get());
78  IBusText ibus_text;
79  ASSERT_TRUE(PopIBusText(&reader, &ibus_text));
80  EXPECT_EQ(kSampleText, ibus_text.text());
81  EXPECT_TRUE(ibus_text.underline_attributes().empty());
82  EXPECT_TRUE(ibus_text.selection_attributes().empty());
83}
84
85TEST(IBusTextTest, PopStringFromIBusTextTest) {
86  const char kSampleText[] = "Sample Text";
87
88  // Write to Response object.
89  scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
90  dbus::MessageWriter writer(response.get());
91  AppendStringAsIBusText(kSampleText, &writer);
92
93  // Read from Response object.
94  dbus::MessageReader reader(response.get());
95  std::string result;
96  ASSERT_TRUE(PopStringFromIBusText(&reader, &result));
97  EXPECT_EQ(kSampleText, result);
98}
99
100}  // namespace chromeos
101