| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 |
| 5 #include "base/bind.h" |
| 6 #include "base/logging.h" |
| 7 #include "base/message_loop.h" |
| 8 #include "base/scoped_ptr.h" |
| 9 #include "net/dbus/dbus.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/platform_test.h" |
| 13 |
| 14 namespace net { |
| 15 |
| 16 class DBusTest : public PlatformTest { |
| 17 public: |
| 18 void ResponseCallback(dbus::Response* response) { |
| 19 dbus::MessageReader reader(response); |
| 20 LOG(ERROR) << "@@ " << response->ToString(); |
| 21 MessageLoop::current()->Quit(); |
| 22 }; |
| 23 |
| 24 protected: |
| 25 MessageLoop* message_loop_; |
| 26 }; |
| 27 |
| 28 // Test that byte can be properly written and read. |
| 29 TEST_F(DBusTest, AppendAndPopByte) { |
| 30 dbus::Message message; |
| 31 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL)); |
| 32 dbus::MessageWriter writer(&message); |
| 33 writer.AppendByte(123); // The input is 123. |
| 34 |
| 35 uint8 byte_value = 0; |
| 36 bool bool_value = false; |
| 37 dbus::MessageReader reader(&message); |
| 38 ASSERT_TRUE(reader.HasMore()); // Should have data to read. |
| 39 |
| 40 // Should fail as the type is not bool here. |
| 41 ASSERT_FALSE(reader.PopBool(&bool_value)); |
| 42 |
| 43 ASSERT_TRUE(reader.PopByte(&byte_value)); |
| 44 EXPECT_EQ(123, byte_value); // Should match with the input. |
| 45 ASSERT_FALSE(reader.HasMore()); // Should not have more data to read. |
| 46 // Try to get another byte. Should fail without crashing. |
| 47 ASSERT_FALSE(reader.PopByte(&byte_value)); |
| 48 } |
| 49 |
| 50 // This is a bit boring, but check all basic types can be properly written |
| 51 // and read. |
| 52 TEST_F(DBusTest, AppendAndPopBasicTypes) { |
| 53 dbus::Message message; |
| 54 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL)); |
| 55 dbus::MessageWriter writer(&message); |
| 56 |
| 57 writer.AppendByte(0); |
| 58 writer.AppendBool(true); |
| 59 writer.AppendInt16(2); |
| 60 writer.AppendUint16(3); |
| 61 writer.AppendInt32(4); |
| 62 writer.AppendUint32(5); |
| 63 writer.AppendInt64(6); |
| 64 writer.AppendUint64(7); |
| 65 writer.AppendDouble(8.0); |
| 66 writer.AppendString("string"); |
| 67 writer.AppendObjectPath("/object/path"); |
| 68 |
| 69 uint8 byte_value = 0; |
| 70 bool bool_value = false; |
| 71 int16 int16_value = 0; |
| 72 uint16 uint16_value = 0; |
| 73 int32 int32_value = 0; |
| 74 uint32 uint32_value = 0; |
| 75 int64 int64_value = 0; |
| 76 uint64 uint64_value = 0; |
| 77 double double_value = 0; |
| 78 std::string string_value; |
| 79 std::string object_path_value; |
| 80 LOG(ERROR) << "@@ ToString() " << message.ToString(); |
| 81 |
| 82 dbus::MessageReader reader(&message); |
| 83 ASSERT_TRUE(reader.HasMore()); |
| 84 ASSERT_TRUE(reader.PopByte(&byte_value)); |
| 85 ASSERT_TRUE(reader.PopBool(&bool_value)); |
| 86 ASSERT_TRUE(reader.PopInt16(&int16_value)); |
| 87 ASSERT_TRUE(reader.PopUint16(&uint16_value)); |
| 88 ASSERT_TRUE(reader.PopInt32(&int32_value)); |
| 89 ASSERT_TRUE(reader.PopUint32(&uint32_value)); |
| 90 ASSERT_TRUE(reader.PopInt64(&int64_value)); |
| 91 ASSERT_TRUE(reader.PopUint64(&uint64_value)); |
| 92 ASSERT_TRUE(reader.PopDouble(&double_value)); |
| 93 ASSERT_TRUE(reader.PopString(&string_value)); |
| 94 ASSERT_TRUE(reader.PopObjectPath(&object_path_value)); |
| 95 ASSERT_FALSE(reader.HasMore()); |
| 96 |
| 97 EXPECT_EQ(0, byte_value); |
| 98 EXPECT_EQ(true, bool_value); |
| 99 EXPECT_EQ(2, int16_value); |
| 100 EXPECT_EQ(3U, uint16_value); |
| 101 EXPECT_EQ(4, int32_value); |
| 102 EXPECT_EQ(5U, uint32_value); |
| 103 EXPECT_EQ(6, int64_value); |
| 104 EXPECT_EQ(7U, uint64_value); |
| 105 EXPECT_DOUBLE_EQ(8.0, double_value); |
| 106 EXPECT_EQ("string", string_value); |
| 107 EXPECT_EQ("/object/path", object_path_value); |
| 108 } |
| 109 |
| 110 TEST_F(DBusTest, TestSync) { |
| 111 dbus::Bus::Options options; |
| 112 options.bus_type = dbus::Bus::SESSION; |
| 113 dbus::Bus bus(options); |
| 114 scoped_ptr<dbus::ObjectProxy> proxy(bus.GetObjectProxy( |
| 115 "com.example.SampleService", |
| 116 "/SomeObject")); |
| 117 dbus::MethodCall method_call("com.example.SampleInterface", |
| 118 "HelloWorld"); |
| 119 dbus::MessageWriter builder(&method_call); |
| 120 builder.AppendString("hello"); |
| 121 dbus::Response response; |
| 122 CHECK(proxy->CallMethodSync(&method_call, &response)); |
| 123 LOG(ERROR) << "@@ response: " << response.ToString(); |
| 124 dbus::MessageReader reader(&response); |
| 125 dbus::MessageReader sub_reader(&response); |
| 126 CHECK(reader.PopArray(&sub_reader)); |
| 127 std::string string_value; |
| 128 CHECK(sub_reader.PopString(&string_value)); |
| 129 LOG(ERROR) << string_value; |
| 130 CHECK(sub_reader.PopString(&string_value)); |
| 131 LOG(ERROR) << string_value; |
| 132 } |
| 133 |
| 134 TEST_F(DBusTest, TestAsync) { |
| 135 LOG(ERROR) << "@@ " << __PRETTY_FUNCTION__; |
| 136 dbus::Bus::Options bus_options; |
| 137 bus_options.bus_type = dbus::Bus::SESSION; |
| 138 dbus::Bus bus(bus_options); |
| 139 scoped_ptr<dbus::ObjectProxy> proxy(bus.GetObjectProxy( |
| 140 "com.example.SampleService", |
| 141 "/SomeObject")); |
| 142 dbus::MethodCall method_call("com.example.SampleInterface", |
| 143 "HelloWorld"); |
| 144 dbus::MessageWriter builder(&method_call); |
| 145 builder.AppendString("hello"); |
| 146 base::Callback<void(dbus::Response*)> response_callback = |
| 147 base::Bind(&DBusTest::ResponseCallback, |
| 148 base::Unretained(this)); |
| 149 LOG(ERROR) << "@@ " << __PRETTY_FUNCTION__; |
| 150 proxy->CallMethodAsync(&method_call, response_callback); |
| 151 LOG(ERROR) << "@@ " << __PRETTY_FUNCTION__; |
| 152 MessageLoop::current()->Run(); |
| 153 } |
| 154 |
| 155 } // namespace net |
| OLD | NEW |