Reorganise to remove redundant folder
[profile/ivi/common-api-dbus-runtime.git] / src / test / DBusProxyTest.cpp
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include <dbus/dbus.h>
6 #include <CommonAPI/DBus/DBusInputStream.h>
7 #include <CommonAPI/DBus/DBusMessage.h>
8 #include <CommonAPI/DBus/DBusProxy.h>
9 #include <CommonAPI/DBus/DBusConnection.h>
10 #include <CommonAPI/DBus/DBusStubAdapter.h>
11 #include <stdint.h>
12 #include <vector>
13 #include <gtest/gtest.h>
14 #include <iostream>
15 #include <algorithm>
16 #include <string>
17
18
19 class TestProxy: public CommonAPI::DBus::DBusProxy {
20 public:
21   TestProxy(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection);
22   ~TestProxy() = default;
23
24 protected:
25   void getOwnVersion(uint16_t& ownVersionMajor, uint16_t& ownVersionMinor) const;
26
27 };
28
29 class TestStubAdapter: public CommonAPI::DBus::DBusStubAdapter {
30 public:
31     TestStubAdapter(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection);
32 protected:
33     bool onInterfaceDBusMessage(const CommonAPI::DBus::DBusMessage& dbusMessage);
34     const char* getMethodsDBusIntrospectionXmlData() const;
35 };
36
37 const char* TestStubAdapter::getMethodsDBusIntrospectionXmlData() const {
38     return "";
39 }
40
41 bool TestStubAdapter::onInterfaceDBusMessage(const CommonAPI::DBus::DBusMessage& dbusMessage) {
42     return true;
43 }
44
45 TestStubAdapter::TestStubAdapter(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection) :
46         CommonAPI::DBus::DBusStubAdapter(
47         "com.bmw.test.Echo",
48         "/com/bmw/test/Echo",
49         "com.bmw.test.Echo",
50         dbusConnection) {
51 }
52
53 TestProxy::TestProxy(const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection) :
54                 CommonAPI::DBus::DBusProxy(
55         "com.bmw.test.Echo",
56         "/com/bmw/test/Echo",
57         "com.bmw.test.Echo",
58         dbusConnection) {
59 }
60
61 void TestProxy::getOwnVersion(uint16_t& ownVersionMajor, uint16_t& ownVersionMinor) const {
62 }
63
64
65 const static std::string ID = "com.bmw.test.Echo";
66
67
68 class ProxyTest: public ::testing::Test {
69 protected:
70
71     virtual void TearDown() {
72         dbusConnection_->disconnect();
73     }
74
75     void SetUp() {
76         dbusConnection_ = CommonAPI::DBus::DBusConnection::getSessionBus();
77         ASSERT_TRUE(dbusConnection_->connect());
78         proxy_ = std::make_shared<TestProxy>(dbusConnection_);
79     }
80
81     std::shared_ptr<CommonAPI::DBus::DBusConnection> dbusConnection_;
82     std::shared_ptr<TestProxy> proxy_;
83 };
84
85 TEST_F(ProxyTest, HasCorrectBusName) {
86   std::string actualName = proxy_->getDBusBusName();
87   EXPECT_EQ("com.bmw.test.Echo", actualName);
88 }
89
90 TEST_F(ProxyTest, HasCorrectObjectPath) {
91   std::string actualPath = proxy_->getDBusObjectPath();
92   EXPECT_EQ("/com/bmw/test/Echo", actualPath);
93 }
94
95 TEST_F(ProxyTest, HasCorrectInterfaceName) {
96   std::string actualName = proxy_->getInterfaceName();
97   EXPECT_EQ("com.bmw.test.Echo", actualName);
98 }
99
100 TEST_F(ProxyTest, IsNotAvailable) {
101         bool isAvailable = proxy_->isAvailable();
102         EXPECT_FALSE(isAvailable);
103 }
104
105 TEST_F(ProxyTest, ServiceRegistry) {
106         std::shared_ptr<CommonAPI::DBus::DBusProxyConnection> connection = proxy_->getDBusConnection();
107         auto registry = connection->getDBusServiceRegistry();
108         ASSERT_FALSE(!registry);
109 }
110
111 TEST_F(ProxyTest, ServiceStatus) {
112     std::shared_ptr<TestStubAdapter> stub_ = std::make_shared<TestStubAdapter>(dbusConnection_);
113         stub_->init();
114     dbusConnection_->requestServiceNameAndBlock(ID);
115
116         std::vector<std::string> actuallyAvailableServices;
117
118
119         actuallyAvailableServices = dbusConnection_->getDBusServiceRegistry()->getAvailableServiceInstances("com.bmw.test.Echo",
120                         "local");
121
122         std::string toFind = "com.bmw.test.Echo";
123         auto found = std::find(actuallyAvailableServices.begin(), actuallyAvailableServices.end(), toFind);
124
125         ASSERT_TRUE(actuallyAvailableServices.begin() != actuallyAvailableServices.end());
126 }
127
128 TEST_F(ProxyTest, IsAvailableBlocking) {
129     std::shared_ptr<TestStubAdapter> stub = std::make_shared<TestStubAdapter>(dbusConnection_);
130     stub->init();
131     dbusConnection_->requestServiceNameAndBlock(ID);
132
133     bool isAvailable = proxy_->isAvailableBlocking();
134     EXPECT_TRUE(isAvailable);
135 }
136
137 TEST_F(ProxyTest, HasNecessaryAttributesAndEvents) {
138         CommonAPI::InterfaceVersionAttribute& versionAttribute = (proxy_->getInterfaceVersionAttribute());
139         CommonAPI::ProxyStatusEvent& statusEvent = (proxy_->getProxyStatusEvent());
140 }
141
142 TEST_F(ProxyTest, IsConnected) {
143   ASSERT_TRUE(proxy_->getDBusConnection()->isConnected());
144 }
145
146 TEST_F(ProxyTest, TestInterfaceVersionAttribute) {
147         CommonAPI::InterfaceVersionAttribute& versionAttribute = proxy_->getInterfaceVersionAttribute();
148         CommonAPI::Version version;
149         CommonAPI::CallStatus status = versionAttribute.getValue(version);
150         ASSERT_EQ(CommonAPI::CallStatus::NOT_AVAILABLE, status);
151 }
152
153 int main(int argc, char** argv) {
154         ::testing::InitGoogleTest(&argc, argv);
155         return RUN_ALL_TESTS();
156 }