blob: cc7781c1866443ad4eabd3175800f66bf9d9cba0 [file] [log] [blame]
Darin Petkove4b27022012-05-16 13:28:50 +02001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkove0a312e2011-07-20 13:45:28 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <gtest/gtest.h>
6
7#include "shill/dbus_properties.h"
8
9using std::string;
Jason Glasgow9c09e362012-04-18 15:16:29 -040010using std::vector;
Darin Petkove0a312e2011-07-20 13:45:28 -070011using testing::Test;
12
13namespace shill {
14
15class DBusPropertiesTest : public Test {
16};
17
Jason Glasgow9c09e362012-04-18 15:16:29 -040018TEST_F(DBusPropertiesTest, GetObjectPath) {
19 static const char kTestProperty[] = "RandomKey";
20 static const char kTestValue[] = "/random/path";
21 static const char kOldValue[] = "/random/oldpath";
22 string value = kOldValue;
23 DBusPropertiesMap props;
24 EXPECT_FALSE(DBusProperties::GetObjectPath(props, kTestProperty, &value));
25 EXPECT_EQ(kOldValue, value);
26 props[kTestProperty].writer().append_path(kTestValue);
27 EXPECT_TRUE(DBusProperties::GetObjectPath(props, kTestProperty, &value));
28 EXPECT_EQ(kTestValue, value);
29}
30
Darin Petkove0a312e2011-07-20 13:45:28 -070031TEST_F(DBusPropertiesTest, GetString) {
32 static const char kTestProperty[] = "RandomKey";
33 static const char kTestValue[] = "random-value";
34 static const char kOldValue[] = "old-value";
35 string value = kOldValue;
36 DBusPropertiesMap props;
37 EXPECT_FALSE(DBusProperties::GetString(props, kTestProperty, &value));
38 EXPECT_EQ(kOldValue, value);
39 props[kTestProperty].writer().append_string(kTestValue);
40 EXPECT_TRUE(DBusProperties::GetString(props, kTestProperty, &value));
41 EXPECT_EQ(kTestValue, value);
42}
43
Jason Glasgow9c09e362012-04-18 15:16:29 -040044TEST_F(DBusPropertiesTest, GetStrings) {
45 static const char kTestProperty[] = "RandomKey";
46 static const char kTestValue[] = "random-value";
47 static const char kOldValue[] = "old-value";
48 vector<string> test_values;
49 test_values.push_back(kTestValue);
50 test_values.push_back(kTestValue);
51
52 vector<string> values;
53 DBusPropertiesMap props;
54 EXPECT_FALSE(DBusProperties::GetStrings(props, kTestProperty, &values));
55 EXPECT_EQ(0, values.size());
56 values.push_back(kOldValue);
57 EXPECT_FALSE(DBusProperties::GetStrings(props, kTestProperty, &values));
58 EXPECT_EQ(1, values.size());
59 EXPECT_EQ(kOldValue, values[0]);
60 ::DBus::Variant v;
61 ::DBus::MessageIter writer = v.writer();
62 writer << test_values;
63 props[kTestProperty] = v;
64 EXPECT_TRUE(DBusProperties::GetStrings(props, kTestProperty, &values));
65 EXPECT_EQ(2, values.size());
66 EXPECT_EQ(kTestValue, values[0]);
67 EXPECT_EQ(kTestValue, values[1]);
68}
69
Darin Petkove0a312e2011-07-20 13:45:28 -070070TEST_F(DBusPropertiesTest, GetUint32) {
71 static const char kTestProperty[] = "AKey";
72 const uint32 kTestValue = 35;
73 const uint32 kOldValue = 74;
74 uint32 value = kOldValue;
75 DBusPropertiesMap props;
76 EXPECT_FALSE(DBusProperties::GetUint32(props, kTestProperty, &value));
77 EXPECT_EQ(kOldValue, value);
78 props[kTestProperty].writer().append_uint32(kTestValue);
79 EXPECT_TRUE(DBusProperties::GetUint32(props, kTestProperty, &value));
80 EXPECT_EQ(kTestValue, value);
81}
82
Darin Petkovceb68172011-07-29 14:47:48 -070083TEST_F(DBusPropertiesTest, GetUint16) {
84 static const char kTestProperty[] = "version";
85 const uint16 kTestValue = 77;
86 const uint16 kOldValue = 88;
87 uint16 value = kOldValue;
88 DBusPropertiesMap props;
89 EXPECT_FALSE(DBusProperties::GetUint16(props, kTestProperty, &value));
90 EXPECT_EQ(kOldValue, value);
91 props[kTestProperty].writer().append_uint16(kTestValue);
92 EXPECT_TRUE(DBusProperties::GetUint16(props, kTestProperty, &value));
93 EXPECT_EQ(kTestValue, value);
94}
95
Darin Petkove4b27022012-05-16 13:28:50 +020096TEST_F(DBusPropertiesTest, GetRpcIdentifiers) {
97 static const char kTestProperty[] = "paths";
98 static const char kOldTestPath0[] = "/org/chromium/Something/Old";
99 static const char kOldTestPath1[] = "/org/chromium/Else/Old";
100 static const char kTestPath0[] = "/org/chromium/Something";
101 static const char kTestPath1[] = "/org/chromium/Else";
102
103 vector<RpcIdentifier> value;
104 value.push_back(kOldTestPath0);
105 value.push_back(kOldTestPath1);
106 DBusPropertiesMap props;
107 EXPECT_FALSE(DBusProperties::GetRpcIdentifiers(props, kTestProperty, &value));
108 ASSERT_EQ(2, value.size());
109 EXPECT_EQ(kOldTestPath0, value[0]);
110 EXPECT_EQ(kOldTestPath1, value[1]);
111
112 vector<DBus::Path> paths;
113 paths.push_back(kTestPath0);
114 paths.push_back(kTestPath1);
115 DBus::MessageIter writer = props[kTestProperty].writer();
116 writer << paths;
117 EXPECT_TRUE(DBusProperties::GetRpcIdentifiers(props, kTestProperty, &value));
118 ASSERT_EQ(2, value.size());
119 EXPECT_EQ(kTestPath0, value[0]);
120 EXPECT_EQ(kTestPath1, value[1]);
121}
122
Darin Petkove0a312e2011-07-20 13:45:28 -0700123} // namespace shill