forked from crosswalk-project/crosswalk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbus_manager_unittest.cc
More file actions
123 lines (103 loc) · 3.84 KB
/
dbus_manager_unittest.cc
File metadata and controls
123 lines (103 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2012 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 <string>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/threading/thread.h"
#include "dbus/bus.h"
#include "dbus/exported_object.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "xwalk/dbus/dbus_manager.h"
#include "xwalk/dbus/test_client.h"
using xwalk::DBusManager;
const char kTestServiceName[] = "org.crosswalkproject.test_service";
const char kTestInterface[] = "org.crosswalkproject.test_service.Interface";
const char kTestMethod[] = "Method";
const dbus::ObjectPath kTestObjectPath("/test/path");
// Test whether initialization of a DBusManager succeeds.
TEST(DBusManagerTest, Initialize) {
base::MessageLoop message_loop;
DBusManager manager(kTestServiceName);
base::RunLoop run_loop;
manager.Initialize(run_loop.QuitClosure());
run_loop.Run();
ASSERT_TRUE(manager.session_bus());
ASSERT_TRUE(manager.session_bus()->is_connected());
}
void EmptyCallback() {}
// Uses a DBusManager to expose an object with a method. Provide hooks (on_*
// callbacks), so that calling code can track whether the object was exposed and
// the method was called.
class ExportObjectService {
public:
ExportObjectService()
: on_exported(base::Bind(EmptyCallback)),
on_method_called(base::Bind(EmptyCallback)),
manager_(kTestServiceName),
dbus_object_(NULL) {}
void Initialize() {
manager_.Initialize(
base::Bind(&ExportObjectService::OnInitialized,
base::Unretained(this)));
}
base::Closure on_exported;
base::Closure on_method_called;
private:
void OnInitialized() {
dbus_object_ = manager_.session_bus()->GetExportedObject(kTestObjectPath);
dbus_object_->ExportMethod(
kTestInterface, kTestMethod,
base::Bind(&ExportObjectService::OnMethodCalled,
base::Unretained(this)),
base::Bind(&ExportObjectService::OnExported,
base::Unretained(this)));
}
void OnExported(const std::string& interface_name,
const std::string& method_name,
bool success) {
on_exported.Run();
}
void OnMethodCalled(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
on_method_called.Run();
}
DBusManager manager_;
dbus::ExportedObject* dbus_object_;
};
// Test whether we can expose an object using DBusManager.
TEST(DBusManagerTest, ExportObject) {
base::MessageLoop message_loop;
base::RunLoop run_loop;
ExportObjectService test_service;
test_service.on_exported = base::Bind(run_loop.QuitClosure());
test_service.Initialize();
run_loop.Run();
}
// Call the method exposed by ExportObjectService.
class CallMethodClient : public dbus::TestClient {
public:
void CallTestMethod() {
dbus::ObjectProxy* object_proxy =
bus_->GetObjectProxy(kTestServiceName, kTestObjectPath);
dbus::MethodCall method_call(kTestInterface, kTestMethod);
object_proxy->CallMethod(&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
dbus::ObjectProxy::EmptyResponseCallback());
}
};
// Tests whether we can call a method exported by a bus created in DBusManager.
TEST(DBusManagerTest, CallMethod) {
base::MessageLoop message_loop;
base::RunLoop run_loop;
CallMethodClient test_client;
ExportObjectService test_service;
test_service.on_method_called = base::Bind(run_loop.QuitClosure());
test_service.on_exported = base::Bind(&CallMethodClient::CallTestMethod,
base::Unretained(&test_client));
test_service.Initialize();
run_loop.Run();
}