Merge pull request #58 from tripzero/master
[profile/ivi/automotive-message-broker.git] / tests / testProtocolClient.cpp
1 #include "testProtocolCommon.h"
2
3 #include <debugout.h>
4 #include <jsonprotocol.h>
5
6 #include <QLocalSocket>
7 #include <QLocalServer>
8 #include <QCoreApplication>
9 #include <QObject>
10
11 #include <memory>
12
13 void runTest(amb::AmbRemoteClient *c)
14 {
15         DebugOut(0) << "calling client->list()" << endl;
16         c->list([](std::vector<amb::Object::Ptr> supported)
17         {
18                 DebugOut(0) << "list call reply" << endl;
19                 g_assert(supported.size() == 2);
20         });
21
22         c->subscribe("interface1", [](amb::Object::Ptr obj)
23         {
24                 DebugOut(0) << obj->interfaceName << " changed!" << endl;
25         });
26
27         DebugOut(0) << "calling client->get()" << endl;
28         c->get("interface1", [](amb::Object::Ptr obj)
29         {
30                 DebugOut(0) << "get call reply" << endl;
31                 g_assert(obj->size() == 2);
32
33                 obj->emplace("vehicleSpeed", amb::make_shared(new VehicleProperty::VehicleSpeedType(69)));
34
35         });
36
37         amb::Object::Ptr obj = amb::Object::create();
38
39         obj->interfaceName = "interface1";
40         obj->emplace("vehicleSpeed", amb::make_shared(new VehicleProperty::VehicleSpeedType(22)));
41
42         c->set("interface1", obj, [](bool s)
43         {
44                 DebugOut(0) << "set call reply status: " << (s ? "success!" : "fail") << endl;
45                 g_assert(s);
46         });
47 }
48
49 int main(int argc, char** argv)
50 {
51         DebugOut::setDebugThreshhold(7);
52         DebugOut::setThrowErr(true);
53         DebugOut::setThrowWarn(false);
54
55         DebugOut(0) << "Testing AMB json server/client" << endl;
56
57         QCoreApplication app(argc, argv);
58
59         DomainSocket socket;
60
61         socket.open();
62
63         if(!socket.getSocket()->waitForConnected())
64         {
65                 DebugOut("Could not connect");
66                 return -1;
67         }
68
69         DebugOut(0) << "We are connected!" << endl;
70
71         amb::AmbRemoteClient client(&socket);
72
73         runTest(&client);
74
75         app.exec();
76 }