~sil2100/location-service/gps-arm64

« back to all changes in this revision

Viewing changes to tests/acceptance_tests.cpp

  • Committer: Thomas Voß
  • Date: 2013-05-28 14:20:45 UTC
  • Revision ID: thomas.voss@canonical.com-20130528142045-kq5umqdmm4o53vwk
Initial push.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "cross_process_sync.h"
 
2
#include "fork_and_run.h"
 
3
 
 
4
#include <com/ubuntu/location/accuracy.h>
 
5
#include <com/ubuntu/location/criteria.h>
 
6
#include <com/ubuntu/location/clock.h>
 
7
#include <com/ubuntu/location/engine.h>
 
8
#include <com/ubuntu/location/heading.h>
 
9
#include <com/ubuntu/location/position.h>
 
10
#include <com/ubuntu/location/provider.h>
 
11
#include <com/ubuntu/location/update.h>
 
12
#include <com/ubuntu/location/velocity.h>
 
13
#include <com/ubuntu/location/wgs84/altitude.h>
 
14
#include <com/ubuntu/location/wgs84/latitude.h>
 
15
#include <com/ubuntu/location/wgs84/longitude.h>
 
16
 
 
17
#include <com/ubuntu/location/service/default_configuration.h>
 
18
#include <com/ubuntu/location/service/implementation.h>
 
19
#include <com/ubuntu/location/service/stub.h>
 
20
 
 
21
#include <org/freedesktop/dbus/announcer.h>
 
22
#include <org/freedesktop/dbus/resolver.h>
 
23
 
 
24
#include <org/freedesktop/dbus/asio/executor.h>
 
25
 
 
26
#include <gtest/gtest.h>
 
27
 
 
28
#include <bitset>
 
29
#include <chrono>
 
30
#include <iostream>
 
31
#include <memory>
 
32
#include <set>
 
33
#include <stdexcept>
 
34
 
 
35
namespace
 
36
{
 
37
 
 
38
org::freedesktop::dbus::Bus::Ptr the_session_bus()
 
39
{
 
40
    org::freedesktop::dbus::Bus::Ptr bus{
 
41
        new org::freedesktop::dbus::Bus{org::freedesktop::dbus::WellKnownBus::session}};
 
42
    return bus;
 
43
}
 
44
 
 
45
class DummyProvider : public com::ubuntu::location::Provider
 
46
{
 
47
public:
 
48
    DummyProvider()
 
49
    {
 
50
    }
 
51
 
 
52
    ~DummyProvider() noexcept
 
53
    {
 
54
    }
 
55
 
 
56
    void inject_update(const com::ubuntu::location::Update<com::ubuntu::location::Position>& update)
 
57
    {
 
58
        deliver_position_updates(update);
 
59
    }
 
60
 
 
61
    void inject_update(const com::ubuntu::location::Update<com::ubuntu::location::Velocity>& update)
 
62
    {
 
63
        deliver_velocity_updates(update);
 
64
    }
 
65
 
 
66
    void inject_update(const com::ubuntu::location::Update<com::ubuntu::location::Heading>& update)
 
67
    {
 
68
        deliver_heading_updates(update);
 
69
    }
 
70
 
 
71
    bool matches_criteria(const com::ubuntu::location::Criteria& /*criteria*/)
 
72
    {
 
73
        return true;
 
74
    }
 
75
};
 
76
 
 
77
struct AlwaysGrantingPermissionManager : public com::ubuntu::location::service::PermissionManager
 
78
{
 
79
    PermissionManager::Result
 
80
    check_permission_for_credentials(const com::ubuntu::location::Criteria&, 
 
81
                                     const com::ubuntu::location::service::Credentials&)
 
82
    {
 
83
        return PermissionManager::Result::granted;
 
84
    }
 
85
};
 
86
 
 
87
auto timestamp = com::ubuntu::location::Clock::now();
 
88
 
 
89
com::ubuntu::location::Update<com::ubuntu::location::Position> reference_position_update
 
90
{
 
91
    {
 
92
        com::ubuntu::location::wgs84::Latitude{9. * com::ubuntu::location::units::Degrees},
 
93
        com::ubuntu::location::wgs84::Longitude{53. * com::ubuntu::location::units::Degrees},
 
94
        com::ubuntu::location::wgs84::Altitude{-2. * com::ubuntu::location::units::Meters}
 
95
    },
 
96
    timestamp
 
97
};
 
98
 
 
99
com::ubuntu::location::Update<com::ubuntu::location::Velocity> reference_velocity_update
 
100
{
 
101
    {5. * com::ubuntu::location::units::MetersPerSecond},
 
102
    timestamp
 
103
};
 
104
 
 
105
com::ubuntu::location::Update<com::ubuntu::location::Heading> reference_heading_update
 
106
{
 
107
    {120. * com::ubuntu::location::units::Degrees},
 
108
    timestamp
 
109
};
 
110
}
 
111
 
 
112
TEST(LocationServiceStandalone, SessionsReceiveUpdatesViaDBus)
 
113
{
 
114
    test::CrossProcessSync sync_start;
 
115
    test::CrossProcessSync sync_session_created;
 
116
 
 
117
    auto server = [&sync_start, &sync_session_created]()
 
118
    {
 
119
        SCOPED_TRACE("Server");
 
120
        auto bus = the_session_bus();
 
121
        bus->install_executor(org::freedesktop::dbus::Executor::Ptr(new org::freedesktop::dbus::asio::Executor{bus}));
 
122
        auto dummy = new DummyProvider();
 
123
        com::ubuntu::location::Provider::Ptr helper(dummy);
 
124
        com::ubuntu::location::service::DefaultConfiguration config;
 
125
        
 
126
        auto location_service =
 
127
        org::freedesktop::dbus::announce_service_on_bus<
 
128
            com::ubuntu::location::service::Interface, 
 
129
            com::ubuntu::location::service::Implementation
 
130
        >(bus,
 
131
          config.the_engine(config.the_provider_set(helper), config.the_provider_selection_policy()),
 
132
          config.the_permission_manager());
 
133
 
 
134
        sync_start.signal_ready();
 
135
 
 
136
        std::thread t{[bus](){bus->run();}};
 
137
 
 
138
        sync_session_created.wait_for_signal_ready();
 
139
 
 
140
        dummy->inject_update(reference_position_update);
 
141
        dummy->inject_update(reference_velocity_update);
 
142
        dummy->inject_update(reference_heading_update);
 
143
 
 
144
        if (t.joinable())
 
145
            t.join();
 
146
    };
 
147
 
 
148
    auto client = [&sync_start, &sync_session_created]()
 
149
    {
 
150
        SCOPED_TRACE("Client");
 
151
 
 
152
        sync_start.wait_for_signal_ready();
 
153
 
 
154
        auto bus = the_session_bus();
 
155
        bus->install_executor(org::freedesktop::dbus::Executor::Ptr(new org::freedesktop::dbus::asio::Executor{bus}));
 
156
        std::thread t{[bus](){bus->run();}};
 
157
        auto location_service = org::freedesktop::dbus::resolve_service_on_bus<
 
158
            com::ubuntu::location::service::Interface, 
 
159
            com::ubuntu::location::service::Stub>(bus);
 
160
        
 
161
        auto s1 = location_service->create_session_for_criteria(com::ubuntu::location::Criteria{});
 
162
        
 
163
        com::ubuntu::location::Update<com::ubuntu::location::Position> position;
 
164
        s1->install_position_updates_handler(
 
165
            [&](const com::ubuntu::location::Update<com::ubuntu::location::Position>& new_position) {
 
166
                std::cout << "On position updated: " << new_position << std::endl;
 
167
                position = new_position;
 
168
            });
 
169
        com::ubuntu::location::Update<com::ubuntu::location::Velocity> velocity;
 
170
        s1->install_velocity_updates_handler(
 
171
            [&](const com::ubuntu::location::Update<com::ubuntu::location::Velocity>& new_velocity) {
 
172
                std::cout << "On velocity_changed " << new_velocity << std::endl;
 
173
                velocity = new_velocity;
 
174
            });
 
175
        com::ubuntu::location::Update<com::ubuntu::location::Heading> heading;
 
176
        s1->install_heading_updates_handler(
 
177
            [&](const com::ubuntu::location::Update<com::ubuntu::location::Heading>& new_heading) {
 
178
                std::cout << "On heading changed: " << new_heading << std::endl;
 
179
                heading = new_heading;
 
180
                bus->stop();
 
181
            });
 
182
        
 
183
        s1->start_position_updates();
 
184
        s1->start_velocity_updates();
 
185
        s1->start_heading_updates();
 
186
        
 
187
        sync_session_created.signal_ready();
 
188
 
 
189
        if (t.joinable())
 
190
            t.join();
 
191
 
 
192
        EXPECT_EQ(reference_position_update, position);
 
193
        EXPECT_EQ(reference_velocity_update, velocity);
 
194
        EXPECT_EQ(reference_heading_update, heading);
 
195
    };
 
196
 
 
197
    EXPECT_NO_FATAL_FAILURE(test::fork_and_run(server, client));
 
198
}