1
#include "cross_process_sync.h"
2
#include "fork_and_run.h"
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>
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>
21
#include <org/freedesktop/dbus/announcer.h>
22
#include <org/freedesktop/dbus/resolver.h>
24
#include <org/freedesktop/dbus/asio/executor.h>
26
#include <gtest/gtest.h>
38
org::freedesktop::dbus::Bus::Ptr the_session_bus()
40
org::freedesktop::dbus::Bus::Ptr bus{
41
new org::freedesktop::dbus::Bus{org::freedesktop::dbus::WellKnownBus::session}};
45
class DummyProvider : public com::ubuntu::location::Provider
52
~DummyProvider() noexcept
56
void inject_update(const com::ubuntu::location::Update<com::ubuntu::location::Position>& update)
58
deliver_position_updates(update);
61
void inject_update(const com::ubuntu::location::Update<com::ubuntu::location::Velocity>& update)
63
deliver_velocity_updates(update);
66
void inject_update(const com::ubuntu::location::Update<com::ubuntu::location::Heading>& update)
68
deliver_heading_updates(update);
71
bool matches_criteria(const com::ubuntu::location::Criteria& /*criteria*/)
77
struct AlwaysGrantingPermissionManager : public com::ubuntu::location::service::PermissionManager
79
PermissionManager::Result
80
check_permission_for_credentials(const com::ubuntu::location::Criteria&,
81
const com::ubuntu::location::service::Credentials&)
83
return PermissionManager::Result::granted;
87
auto timestamp = com::ubuntu::location::Clock::now();
89
com::ubuntu::location::Update<com::ubuntu::location::Position> reference_position_update
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}
99
com::ubuntu::location::Update<com::ubuntu::location::Velocity> reference_velocity_update
101
{5. * com::ubuntu::location::units::MetersPerSecond},
105
com::ubuntu::location::Update<com::ubuntu::location::Heading> reference_heading_update
107
{120. * com::ubuntu::location::units::Degrees},
112
TEST(LocationServiceStandalone, SessionsReceiveUpdatesViaDBus)
114
test::CrossProcessSync sync_start;
115
test::CrossProcessSync sync_session_created;
117
auto server = [&sync_start, &sync_session_created]()
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;
126
auto location_service =
127
org::freedesktop::dbus::announce_service_on_bus<
128
com::ubuntu::location::service::Interface,
129
com::ubuntu::location::service::Implementation
131
config.the_engine(config.the_provider_set(helper), config.the_provider_selection_policy()),
132
config.the_permission_manager());
134
sync_start.signal_ready();
136
std::thread t{[bus](){bus->run();}};
138
sync_session_created.wait_for_signal_ready();
140
dummy->inject_update(reference_position_update);
141
dummy->inject_update(reference_velocity_update);
142
dummy->inject_update(reference_heading_update);
148
auto client = [&sync_start, &sync_session_created]()
150
SCOPED_TRACE("Client");
152
sync_start.wait_for_signal_ready();
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);
161
auto s1 = location_service->create_session_for_criteria(com::ubuntu::location::Criteria{});
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;
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;
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;
183
s1->start_position_updates();
184
s1->start_velocity_updates();
185
s1->start_heading_updates();
187
sync_session_created.signal_ready();
192
EXPECT_EQ(reference_position_update, position);
193
EXPECT_EQ(reference_velocity_update, velocity);
194
EXPECT_EQ(reference_heading_update, heading);
197
EXPECT_NO_FATAL_FAILURE(test::fork_and_run(server, client));