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"
5
6#include <string>
7
8#include "base/compiler_specific.h"
9#include "base/logging.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/strings/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"
16
17namespace chromeos {
18namespace {
19
20const char kSampleKey[] = "Key";
21const IBusProperty::IBusPropertyType kSampleType =
22    IBusProperty::IBUS_PROPERTY_TYPE_RADIO;
23const char kSampleLabel[] = "Label";
24const char kSampleTooltip[] = "Tooltip";
25const bool kSampleVisible = true;
26const bool kSampleChecked = false;
27
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|.
32void 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();
40}
41
42// Checks testing data in |property| with |prefix|.
43// This function does not check IBusProperty::sub_properties_.
44bool 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;
50  }
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;
56  }
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;
62  }
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;
68  }
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;
74  }
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;
80  }
81  return true;
82}
83
84}  // namespace
85
86TEST(IBusPropertyListTest, WriteReadIBusPropertyTest) {
87  const size_t kSubPropertyCount = 16;
88
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);
97  }
98
99  // Write a IBusProperty.
100  scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
101  dbus::MessageWriter writer(response.get());
102  AppendIBusProperty(property, &writer);
103
104  // Read a IBusProperty.
105  IBusProperty target_property;
106  dbus::MessageReader reader(response.get());
107  PopIBusProperty(&reader, &target_property);
108
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])));
116  }
117}
118
119}  // namespace chromeos
120