| Index: net/dbus/dbus_unittest.cc
|
| diff --git a/net/dbus/dbus_unittest.cc b/net/dbus/dbus_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a1d956ba6da74fb52bfc5cfc77d89e7defe6f56d
|
| --- /dev/null
|
| +++ b/net/dbus/dbus_unittest.cc
|
| @@ -0,0 +1,155 @@
|
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/logging.h"
|
| +#include "base/message_loop.h"
|
| +#include "base/scoped_ptr.h"
|
| +#include "net/dbus/dbus.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/platform_test.h"
|
| +
|
| +namespace net {
|
| +
|
| +class DBusTest : public PlatformTest {
|
| + public:
|
| + void ResponseCallback(dbus::Response* response) {
|
| + dbus::MessageReader reader(response);
|
| + LOG(ERROR) << "@@ " << response->ToString();
|
| + MessageLoop::current()->Quit();
|
| + };
|
| +
|
| + protected:
|
| + MessageLoop* message_loop_;
|
| +};
|
| +
|
| +// Test that byte can be properly written and read.
|
| +TEST_F(DBusTest, AppendAndPopByte) {
|
| + dbus::Message message;
|
| + message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
|
| + dbus::MessageWriter writer(&message);
|
| + writer.AppendByte(123); // The input is 123.
|
| +
|
| + uint8 byte_value = 0;
|
| + bool bool_value = false;
|
| + dbus::MessageReader reader(&message);
|
| + ASSERT_TRUE(reader.HasMore()); // Should have data to read.
|
| +
|
| + // Should fail as the type is not bool here.
|
| + ASSERT_FALSE(reader.PopBool(&bool_value));
|
| +
|
| + ASSERT_TRUE(reader.PopByte(&byte_value));
|
| + EXPECT_EQ(123, byte_value); // Should match with the input.
|
| + ASSERT_FALSE(reader.HasMore()); // Should not have more data to read.
|
| + // Try to get another byte. Should fail without crashing.
|
| + ASSERT_FALSE(reader.PopByte(&byte_value));
|
| +}
|
| +
|
| +// This is a bit boring, but check all basic types can be properly written
|
| +// and read.
|
| +TEST_F(DBusTest, AppendAndPopBasicTypes) {
|
| + dbus::Message message;
|
| + message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
|
| + dbus::MessageWriter writer(&message);
|
| +
|
| + writer.AppendByte(0);
|
| + writer.AppendBool(true);
|
| + writer.AppendInt16(2);
|
| + writer.AppendUint16(3);
|
| + writer.AppendInt32(4);
|
| + writer.AppendUint32(5);
|
| + writer.AppendInt64(6);
|
| + writer.AppendUint64(7);
|
| + writer.AppendDouble(8.0);
|
| + writer.AppendString("string");
|
| + writer.AppendObjectPath("/object/path");
|
| +
|
| + uint8 byte_value = 0;
|
| + bool bool_value = false;
|
| + int16 int16_value = 0;
|
| + uint16 uint16_value = 0;
|
| + int32 int32_value = 0;
|
| + uint32 uint32_value = 0;
|
| + int64 int64_value = 0;
|
| + uint64 uint64_value = 0;
|
| + double double_value = 0;
|
| + std::string string_value;
|
| + std::string object_path_value;
|
| + LOG(ERROR) << "@@ ToString() " << message.ToString();
|
| +
|
| + dbus::MessageReader reader(&message);
|
| + ASSERT_TRUE(reader.HasMore());
|
| + ASSERT_TRUE(reader.PopByte(&byte_value));
|
| + ASSERT_TRUE(reader.PopBool(&bool_value));
|
| + ASSERT_TRUE(reader.PopInt16(&int16_value));
|
| + ASSERT_TRUE(reader.PopUint16(&uint16_value));
|
| + ASSERT_TRUE(reader.PopInt32(&int32_value));
|
| + ASSERT_TRUE(reader.PopUint32(&uint32_value));
|
| + ASSERT_TRUE(reader.PopInt64(&int64_value));
|
| + ASSERT_TRUE(reader.PopUint64(&uint64_value));
|
| + ASSERT_TRUE(reader.PopDouble(&double_value));
|
| + ASSERT_TRUE(reader.PopString(&string_value));
|
| + ASSERT_TRUE(reader.PopObjectPath(&object_path_value));
|
| + ASSERT_FALSE(reader.HasMore());
|
| +
|
| + EXPECT_EQ(0, byte_value);
|
| + EXPECT_EQ(true, bool_value);
|
| + EXPECT_EQ(2, int16_value);
|
| + EXPECT_EQ(3U, uint16_value);
|
| + EXPECT_EQ(4, int32_value);
|
| + EXPECT_EQ(5U, uint32_value);
|
| + EXPECT_EQ(6, int64_value);
|
| + EXPECT_EQ(7U, uint64_value);
|
| + EXPECT_DOUBLE_EQ(8.0, double_value);
|
| + EXPECT_EQ("string", string_value);
|
| + EXPECT_EQ("/object/path", object_path_value);
|
| +}
|
| +
|
| +TEST_F(DBusTest, TestSync) {
|
| + dbus::Bus::Options options;
|
| + options.bus_type = dbus::Bus::SESSION;
|
| + dbus::Bus bus(options);
|
| + scoped_ptr<dbus::ObjectProxy> proxy(bus.GetObjectProxy(
|
| + "com.example.SampleService",
|
| + "/SomeObject"));
|
| + dbus::MethodCall method_call("com.example.SampleInterface",
|
| + "HelloWorld");
|
| + dbus::MessageWriter builder(&method_call);
|
| + builder.AppendString("hello");
|
| + dbus::Response response;
|
| + CHECK(proxy->CallMethodSync(&method_call, &response));
|
| + LOG(ERROR) << "@@ response: " << response.ToString();
|
| + dbus::MessageReader reader(&response);
|
| + dbus::MessageReader sub_reader(&response);
|
| + CHECK(reader.PopArray(&sub_reader));
|
| + std::string string_value;
|
| + CHECK(sub_reader.PopString(&string_value));
|
| + LOG(ERROR) << string_value;
|
| + CHECK(sub_reader.PopString(&string_value));
|
| + LOG(ERROR) << string_value;
|
| +}
|
| +
|
| +TEST_F(DBusTest, TestAsync) {
|
| + LOG(ERROR) << "@@ " << __PRETTY_FUNCTION__;
|
| + dbus::Bus::Options bus_options;
|
| + bus_options.bus_type = dbus::Bus::SESSION;
|
| + dbus::Bus bus(bus_options);
|
| + scoped_ptr<dbus::ObjectProxy> proxy(bus.GetObjectProxy(
|
| + "com.example.SampleService",
|
| + "/SomeObject"));
|
| + dbus::MethodCall method_call("com.example.SampleInterface",
|
| + "HelloWorld");
|
| + dbus::MessageWriter builder(&method_call);
|
| + builder.AppendString("hello");
|
| + base::Callback<void(dbus::Response*)> response_callback =
|
| + base::Bind(&DBusTest::ResponseCallback,
|
| + base::Unretained(this));
|
| + LOG(ERROR) << "@@ " << __PRETTY_FUNCTION__;
|
| + proxy->CallMethodAsync(&method_call, response_callback);
|
| + LOG(ERROR) << "@@ " << __PRETTY_FUNCTION__;
|
| + MessageLoop::current()->Run();
|
| +}
|
| +
|
| +} // namespace net
|
|
|