Resolved more conflicts
[profile/ivi/common-api-dbus-runtime.git] / src / test / DBusProxyTest.cpp
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/. */
7 #include <dbus/dbus.h>
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>
13 #include <stdint.h>
14 #include <vector>
15 #include <gtest/gtest.h>
16 #include <iostream>
17 #include <algorithm>
18 #include <string>
19
20
21 class TestProxy: public CommonAPI::DBus::DBusProxy {
22 public:
23   TestProxy(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection);
24   ~TestProxy() = default;
25
26 protected:
27   void getOwnVersion(uint16_t& ownVersionMajor, uint16_t& ownVersionMinor) const;
28
29 };
30
31 class TestStubAdapter: public CommonAPI::DBus::DBusStubAdapter {
32 public:
33     TestStubAdapter(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection);
34 protected:
35     bool onInterfaceDBusMessage(const CommonAPI::DBus::DBusMessage& dbusMessage);
36     const char* getMethodsDBusIntrospectionXmlData() const;
37 };
38
39 const char* TestStubAdapter::getMethodsDBusIntrospectionXmlData() const {
40     return "";
41 }
42
43 bool TestStubAdapter::onInterfaceDBusMessage(const CommonAPI::DBus::DBusMessage& dbusMessage) {
44     return true;
45 }
46
47 TestStubAdapter::TestStubAdapter(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection) :
48         CommonAPI::DBus::DBusStubAdapter(
49         "com.bmw.test.Echo",
50         "/com/bmw/test/Echo",
51         "com.bmw.test.Echo",
52         dbusConnection) {
53 }
54
55 TestProxy::TestProxy(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection) :
56                 CommonAPI::DBus::DBusProxy(
57         "com.bmw.test.Echo",
58         "/com/bmw/test/Echo",
59         "com.bmw.test.Echo",
60         dbusConnection) {
61 }
62
63 void TestProxy::getOwnVersion(uint16_t& ownVersionMajor, uint16_t& ownVersionMinor) const {
64 }
65
66
67 const static std::string ID = "com.bmw.test.Echo";
68
69
70 class ProxyTest: public ::testing::Test {
71 protected:
72
73     virtual void TearDown() {
74         dbusConnection_->disconnect();
75     }
76
77     void SetUp() {
78         dbusConnection_ = CommonAPI::DBus::DBusConnection::getSessionBus();
79         ASSERT_TRUE(dbusConnection_->connect());
80         proxy_ = std::make_shared<TestProxy>(dbusConnection_);
81     }
82
83     std::shared_ptr<CommonAPI::DBus::DBusConnection> dbusConnection_;
84     std::shared_ptr<TestProxy> proxy_;
85 };
86
87 TEST_F(ProxyTest, HasCorrectBusName) {
88   std::string actualName = proxy_->getDBusBusName();
89   EXPECT_EQ("com.bmw.test.Echo", actualName);
90 }
91
92 TEST_F(ProxyTest, HasCorrectObjectPath) {
93   std::string actualPath = proxy_->getDBusObjectPath();
94   EXPECT_EQ("/com/bmw/test/Echo", actualPath);
95 }
96
97 TEST_F(ProxyTest, HasCorrectInterfaceName) {
98   std::string actualName = proxy_->getInterfaceName();
99   EXPECT_EQ("com.bmw.test.Echo", actualName);
100 }
101
102 TEST_F(ProxyTest, IsNotAvailable) {
103         bool isAvailable = proxy_->isAvailable();
104         EXPECT_FALSE(isAvailable);
105 }
106
107 TEST_F(ProxyTest, ServiceRegistry) {
108         std::shared_ptr<CommonAPI::DBus::DBusProxyConnection> connection = proxy_->getDBusConnection();
109         auto registry = connection->getDBusServiceRegistry();
110         ASSERT_FALSE(!registry);
111 }
112
113 TEST_F(ProxyTest, ServiceStatus) {
114     std::shared_ptr<TestStubAdapter> stub_ = std::make_shared<TestStubAdapter>(dbusConnection_);
115         stub_->init();
116     dbusConnection_->requestServiceNameAndBlock(ID);
117
118         std::vector<std::string> actuallyAvailableServices;
119
120
121         actuallyAvailableServices = dbusConnection_->getDBusServiceRegistry()->getAvailableServiceInstances("com.bmw.test.Echo",
122                         "local");
123
124         std::string toFind = "com.bmw.test.Echo";
125         auto found = std::find(actuallyAvailableServices.begin(), actuallyAvailableServices.end(), toFind);
126
127         ASSERT_TRUE(actuallyAvailableServices.begin() != actuallyAvailableServices.end());
128 }
129
130 TEST_F(ProxyTest, IsAvailableBlocking) {
131     std::shared_ptr<TestStubAdapter> stub = std::make_shared<TestStubAdapter>(dbusConnection_);
132     stub->init();
133     dbusConnection_->requestServiceNameAndBlock(ID);
134
135     bool isAvailable = proxy_->isAvailableBlocking();
136     EXPECT_TRUE(isAvailable);
137 }
138
139 TEST_F(ProxyTest, HasNecessaryAttributesAndEvents) {
140         CommonAPI::InterfaceVersionAttribute& versionAttribute = (proxy_->getInterfaceVersionAttribute());
141         CommonAPI::ProxyStatusEvent& statusEvent = (proxy_->getProxyStatusEvent());
142 }
143
144 TEST_F(ProxyTest, IsConnected) {
145   ASSERT_TRUE(proxy_->getDBusConnection()->isConnected());
146 }
147
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);
153 }
154
155 int main(int argc, char** argv) {
156         ::testing::InitGoogleTest(&argc, argv);
157         return RUN_ALL_TESTS();
158 }