Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / chromeos / dbus / ibus / ibus_property_unittest.cc
blob37ff68512d155c7817f07a0e132c6f51fb8fb61d
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 #include "chromeos/dbus/ibus/ibus_property.h"
6 #include <string>
8 #include "base/compiler_specific.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/string_number_conversions.h"
12 #include "chromeos/dbus/ibus/ibus_object.h"
13 #include "dbus/message.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace chromeos {
18 namespace {
20 const char kSampleKey[] = "Key";
21 const IBusProperty::IBusPropertyType kSampleType =
22 IBusProperty::IBUS_PROPERTY_TYPE_RADIO;
23 const char kSampleLabel[] = "Label";
24 const char kSampleTooltip[] = "Tooltip";
25 const bool kSampleVisible = true;
26 const bool kSampleChecked = false;
28 // Sets testing data to |property| with |prefix|.
29 // This function clears IBusProperty::sub_properties_ and does not add any
30 // entries into it. The testing data can be checked with CheckProperty function
31 // with same |prefix|.
32 void SetProperty(const std::string& prefix, IBusProperty* property) {
33 property->set_key(prefix + kSampleKey);
34 property->set_type(kSampleType);
35 property->set_label(prefix + kSampleLabel);
36 property->set_tooltip(prefix + kSampleTooltip);
37 property->set_visible(kSampleVisible);
38 property->set_checked(kSampleChecked);
39 property->mutable_sub_properties()->clear();
42 // Checks testing data in |property| with |prefix|.
43 // This function does not check IBusProperty::sub_properties_.
44 bool CheckProperty(const std::string& prefix, const IBusProperty& property) {
45 if ((prefix + kSampleKey) != property.key()) {
46 LOG(ERROR) << "Does not match IBusProperty::key value: " << std::endl
47 << "Expected: " << (prefix + kSampleKey) << std::endl
48 << "Actual: " << property.key();
49 return false;
51 if (kSampleType != property.type()) {
52 LOG(ERROR) << "Does not match IBusProperty::type value: " << std::endl
53 << "Expected: " << kSampleType << std::endl
54 << "Actual: " << property.type();
55 return false;
57 if ((prefix + kSampleLabel) != property.label()) {
58 LOG(ERROR) << "Does not match IBusProperty::label value: " << std::endl
59 << "Expected: " << (prefix + kSampleLabel) << std::endl
60 << "Actual: " << property.label();
61 return false;
63 if ((prefix + kSampleTooltip) != property.tooltip()) {
64 LOG(ERROR) << "Does not match IBusProperty::tooltip value: " << std::endl
65 << "Expected: " << (prefix + kSampleTooltip) << std::endl
66 << "Actual: " << property.tooltip();
67 return false;
69 if (kSampleVisible != property.visible()) {
70 LOG(ERROR) << "Does not match IBusProperty::visible value: " << std::endl
71 << "Expected: " << kSampleVisible << std::endl
72 << "Actual: " << property.visible();
73 return false;
75 if (kSampleChecked != property.checked()) {
76 LOG(ERROR) << "Does not match IBusProperty::state value: " << std::endl
77 << "Expected: " << kSampleChecked << std::endl
78 << "Actual: " << property.checked();
79 return false;
81 return true;
84 } // namespace
86 TEST(IBusPropertyListTest, WriteReadIBusPropertyTest) {
87 const size_t kSubPropertyCount = 16;
89 // Create a IBusProperty.
90 IBusProperty property;
91 SetProperty("Root_", &property);
92 for (size_t i = 0; i < kSubPropertyCount; ++i) {
93 const std::string prefix = "Sub" + base::Uint64ToString(i);
94 IBusProperty* sub_property = new IBusProperty;
95 SetProperty(prefix, sub_property);
96 property.mutable_sub_properties()->push_back(sub_property);
99 // Write a IBusProperty.
100 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
101 dbus::MessageWriter writer(response.get());
102 AppendIBusProperty(property, &writer);
104 // Read a IBusProperty.
105 IBusProperty target_property;
106 dbus::MessageReader reader(response.get());
107 PopIBusProperty(&reader, &target_property);
109 // Check a result.
110 EXPECT_TRUE(CheckProperty("Root_", target_property));
111 const IBusPropertyList& sub_properties = target_property.sub_properties();
112 ASSERT_EQ(kSubPropertyCount, sub_properties.size());
113 for (size_t i = 0; i < kSubPropertyCount; ++i) {
114 const std::string prefix = "Sub" + base::Uint64ToString(i);
115 EXPECT_TRUE(CheckProperty(prefix, *(sub_properties[i])));
119 } // namespace chromeos