1 /* Copyright (C) 2013 BMW Group
2 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
3 * Author: Juergen Gehring (juergen.gehring@bmw.de)
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include <CommonAPI/DBus/DBusInputStream.h>
9 #include <CommonAPI/DBus/DBusMessage.h>
10 #include <CommonAPI/DBus/DBusProxy.h>
11 #include <CommonAPI/DBus/DBusConnection.h>
12 #include <CommonAPI/DBus/DBusStubAdapter.h>
15 #include <gtest/gtest.h>
21 class TestProxy: public CommonAPI::DBus::DBusProxy {
23 TestProxy(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection);
24 ~TestProxy() = default;
27 void getOwnVersion(uint16_t& ownVersionMajor, uint16_t& ownVersionMinor) const;
31 class TestStubAdapter: public CommonAPI::DBus::DBusStubAdapter {
33 TestStubAdapter(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection);
35 bool onInterfaceDBusMessage(const CommonAPI::DBus::DBusMessage& dbusMessage);
36 const char* getMethodsDBusIntrospectionXmlData() const;
39 const char* TestStubAdapter::getMethodsDBusIntrospectionXmlData() const {
43 bool TestStubAdapter::onInterfaceDBusMessage(const CommonAPI::DBus::DBusMessage& dbusMessage) {
47 TestStubAdapter::TestStubAdapter(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection) :
48 CommonAPI::DBus::DBusStubAdapter(
55 TestProxy::TestProxy(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection) :
56 CommonAPI::DBus::DBusProxy(
63 void TestProxy::getOwnVersion(uint16_t& ownVersionMajor, uint16_t& ownVersionMinor) const {
67 const static std::string ID = "com.bmw.test.Echo";
70 class ProxyTest: public ::testing::Test {
73 virtual void TearDown() {
74 dbusConnection_->disconnect();
78 dbusConnection_ = CommonAPI::DBus::DBusConnection::getSessionBus();
79 ASSERT_TRUE(dbusConnection_->connect());
80 proxy_ = std::make_shared<TestProxy>(dbusConnection_);
83 std::shared_ptr<CommonAPI::DBus::DBusConnection> dbusConnection_;
84 std::shared_ptr<TestProxy> proxy_;
87 TEST_F(ProxyTest, HasCorrectBusName) {
88 std::string actualName = proxy_->getDBusBusName();
89 EXPECT_EQ("com.bmw.test.Echo", actualName);
92 TEST_F(ProxyTest, HasCorrectObjectPath) {
93 std::string actualPath = proxy_->getDBusObjectPath();
94 EXPECT_EQ("/com/bmw/test/Echo", actualPath);
97 TEST_F(ProxyTest, HasCorrectInterfaceName) {
98 std::string actualName = proxy_->getInterfaceName();
99 EXPECT_EQ("com.bmw.test.Echo", actualName);
102 TEST_F(ProxyTest, IsNotAvailable) {
103 bool isAvailable = proxy_->isAvailable();
104 EXPECT_FALSE(isAvailable);
107 TEST_F(ProxyTest, ServiceRegistry) {
108 std::shared_ptr<CommonAPI::DBus::DBusProxyConnection> connection = proxy_->getDBusConnection();
109 auto registry = connection->getDBusServiceRegistry();
110 ASSERT_FALSE(!registry);
113 TEST_F(ProxyTest, ServiceStatus) {
114 std::shared_ptr<TestStubAdapter> stub_ = std::make_shared<TestStubAdapter>(dbusConnection_);
116 dbusConnection_->requestServiceNameAndBlock(ID);
118 std::vector<std::string> actuallyAvailableServices;
121 actuallyAvailableServices = dbusConnection_->getDBusServiceRegistry()->getAvailableServiceInstances("com.bmw.test.Echo",
124 std::string toFind = "com.bmw.test.Echo";
125 auto found = std::find(actuallyAvailableServices.begin(), actuallyAvailableServices.end(), toFind);
127 ASSERT_TRUE(actuallyAvailableServices.begin() != actuallyAvailableServices.end());
130 TEST_F(ProxyTest, IsAvailableBlocking) {
131 std::shared_ptr<TestStubAdapter> stub = std::make_shared<TestStubAdapter>(dbusConnection_);
133 dbusConnection_->requestServiceNameAndBlock(ID);
135 bool isAvailable = proxy_->isAvailableBlocking();
136 EXPECT_TRUE(isAvailable);
139 TEST_F(ProxyTest, HasNecessaryAttributesAndEvents) {
140 CommonAPI::InterfaceVersionAttribute& versionAttribute = (proxy_->getInterfaceVersionAttribute());
141 CommonAPI::ProxyStatusEvent& statusEvent = (proxy_->getProxyStatusEvent());
144 TEST_F(ProxyTest, IsConnected) {
145 ASSERT_TRUE(proxy_->getDBusConnection()->isConnected());
148 TEST_F(ProxyTest, TestInterfaceVersionAttribute) {
149 CommonAPI::InterfaceVersionAttribute& versionAttribute = proxy_->getInterfaceVersionAttribute();
150 CommonAPI::Version version;
151 CommonAPI::CallStatus status = versionAttribute.getValue(version);
152 ASSERT_EQ(CommonAPI::CallStatus::NOT_AVAILABLE, status);
155 int main(int argc, char** argv) {
156 ::testing::InitGoogleTest(&argc, argv);
157 return RUN_ALL_TESTS();