From f937657b57d8df32c6896100627220c45c41f3f8 Mon Sep 17 00:00:00 2001 From: Lev Dragunov Date: Wed, 20 Jan 2016 13:02:50 +0300 Subject: [PATCH] Extract following flag from routing session state. --- routing/routing_session.cpp | 14 ++-- routing/routing_session.hpp | 12 ++- .../routing_tests/routing_session_test.cpp | 79 +++++++++++++++++++ 3 files changed, 91 insertions(+), 14 deletions(-) diff --git a/routing/routing_session.cpp b/routing/routing_session.cpp index eaa2de8da7..f2977608ae 100644 --- a/routing/routing_session.cpp +++ b/routing/routing_session.cpp @@ -46,6 +46,7 @@ RoutingSession::RoutingSession() : m_router(nullptr), m_route(string()), m_state(RoutingNotActive), + m_isFollowing(false), m_endPoint(m2::PointD::Zero()), m_lastWarnedSpeedCameraIndex(0), m_lastCheckedSpeedCameraIndex(0), @@ -70,6 +71,7 @@ void RoutingSession::BuildRoute(m2::PointD const & startPoint, m2::PointD const m_lastGoodPosition = startPoint; m_endPoint = endPoint; m_router->ClearState(); + m_isFollowing = false; RebuildRoute(startPoint, readyCallback, progressCallback, timeoutSec); } @@ -140,6 +142,7 @@ void RoutingSession::Reset() m_lastCheckedSpeedCameraIndex = 0; m_lastFoundCamera = SpeedCameraRestriction(); m_speedWarningSignal = false; + m_isFollowing = false; } RoutingSession::State RoutingSession::OnLocationPositionChanged(GpsInfo const & info, Index const & index) @@ -173,8 +176,7 @@ RoutingSession::State RoutingSession::OnLocationPositionChanged(GpsInfo const & } else { - if (m_state != RouteFollowing) - m_state = OnRoute; + m_state = OnRoute; // Warning signals checks if (m_routingSettings.m_speedCameraWarning && !m_speedWarningSignal) @@ -370,6 +372,7 @@ bool RoutingSession::DisableFollowMode() if (m_state == RouteNotStarted || m_state == OnRoute) { m_state = RouteNoFollowing; + m_isFollowing = false; return true; } return false; @@ -378,11 +381,8 @@ bool RoutingSession::DisableFollowMode() bool RoutingSession::EnableFollowMode() { if (m_state == RouteNotStarted || m_state == OnRoute) - { - m_state = RouteFollowing; - return true; - } - return false; + m_isFollowing = true; + return m_isFollowing; } void RoutingSession::SetRoutingSettings(RoutingSettings const & routingSettings) diff --git a/routing/routing_session.hpp b/routing/routing_session.hpp index a6319f78c8..f2702869f8 100644 --- a/routing/routing_session.hpp +++ b/routing/routing_session.hpp @@ -45,8 +45,7 @@ public: OnRoute, // user follows the route RouteNeedRebuild, // user left the route RouteFinished, // destination point is reached but the session isn't closed - RouteNoFollowing, // route is built but following mode has been disabled - RouteFollowing // user driwes on road. Similar to OnRoute. Indicates that user pushed the start button. + RouteNoFollowing // route is built but following mode has been disabled }; /* @@ -58,8 +57,6 @@ public: * OnRoute -> RouteNeedRebuild // user moves away from route - need to rebuild * OnRoute -> RouteNoFollowing // following mode was disabled. Router doesn't track position. * OnRoute -> RouteFinished // user reached the end of route - * OnRoute -> RouteFollowing // user pushed the start button - * RouteFollowing -> RouteFinished // user reached the end of route * RouteNeedRebuild -> RouteNotReady // start rebuild route * RouteFinished -> RouteNotReady // start new route */ @@ -86,11 +83,11 @@ public: m2::PointD GetEndPoint() const { return m_endPoint; } bool IsActive() const { return (m_state != RoutingNotActive); } - bool IsNavigable() const { return (m_state == RouteNotStarted || m_state == OnRoute || m_state == RouteFinished || m_state == RouteFollowing); } + bool IsNavigable() const { return (m_state == RouteNotStarted || m_state == OnRoute || m_state == RouteFinished); } bool IsBuilt() const { return (IsNavigable() || m_state == RouteNeedRebuild || m_state == RouteFinished); } bool IsBuilding() const { return (m_state == RouteBuilding); } - bool IsOnRoute() const { return (m_state == OnRoute || m_state == RouteFollowing); } - bool IsFollowing() const { return (m_state == RouteFollowing); } + bool IsOnRoute() const { return (m_state == OnRoute); } + bool IsFollowing() const { return m_isFollowing; } void Reset(); Route const & GetRoute() const { return m_route; } @@ -155,6 +152,7 @@ private: unique_ptr m_router; Route m_route; atomic m_state; + atomic m_isFollowing; m2::PointD m_endPoint; size_t m_lastWarnedSpeedCameraIndex; SpeedCameraRestriction m_lastFoundCamera; diff --git a/routing/routing_tests/routing_session_test.cpp b/routing/routing_tests/routing_session_test.cpp index cb92a741c7..d06b8d52df 100644 --- a/routing/routing_tests/routing_session_test.cpp +++ b/routing/routing_tests/routing_session_test.cpp @@ -151,4 +151,83 @@ UNIT_TEST(TestRouteRebuilding) } TEST_EQUAL(code, RoutingSession::State::RouteNeedRebuild, ()); } + +UNIT_TEST(TestFollowRouteFlagPersistence) +{ + Index index; + RoutingSession session; + session.Init(nullptr, nullptr); + vector routePoints = kTestRoute; + Route masterRoute("dummy", routePoints.begin(), routePoints.end()); + size_t counter = 0; + unique_ptr router = make_unique(masterRoute, DummyRouter::NoError, counter); + session.SetRouter(move(router), nullptr); + + // Go along the route. + TimedSignal alongTimedSignal; + session.BuildRoute(kTestRoute.front(), kTestRoute.back(), + [&alongTimedSignal](Route const &, IRouter::ResultCode) + { + alongTimedSignal.Signal(); + }, + nullptr, 0); + // Manual check of the routeBuilded mutex to avoid spurious results. + auto time = steady_clock::now() + kRouteBuildingMaxDuration; + TEST(alongTimedSignal.WaitUntil(time), ("Route was not built.")); + + TEST(!session.IsFollowing(), ()); + session.EnableFollowMode(); + TEST(session.IsFollowing(), ()); + + location::GpsInfo info; + info.m_horizontalAccuracy = 0.01; + info.m_verticalAccuracy = 0.01; + info.m_longitude = 0.; + info.m_latitude = 1.; + RoutingSession::State code; + while (info.m_latitude < kTestRoute.back().y) + { + session.OnLocationPositionChanged(info, index); + TEST(session.IsOnRoute() , ()); + TEST(session.IsFollowing(), ()); + info.m_latitude += 0.01; + } + TEST_EQUAL(counter, 1, ()); + + // Rebuild route and go in opposite direction. So initiate a route rebuilding flag. + time = steady_clock::now() + kRouteBuildingMaxDuration; + counter = 0; + TimedSignal oppositeTimedSignal; + session.BuildRoute(kTestRoute.front(), kTestRoute.back(), + [&oppositeTimedSignal](Route const &, IRouter::ResultCode) + { + oppositeTimedSignal.Signal(); + }, + nullptr, 0); + TEST(oppositeTimedSignal.WaitUntil(time), ("Route was not built.")); + + // Manual route building resets the following flag. + TEST(!session.IsFollowing(), ()); + session.EnableFollowMode(); + TEST(session.IsFollowing(), ()); + info.m_longitude = 0.; + info.m_latitude = 1.; + for (size_t i = 0; i < 10; ++i) + { + code = session.OnLocationPositionChanged(info, index); + info.m_latitude -= 0.1; + } + TEST_EQUAL(code, RoutingSession::State::RouteNeedRebuild, ()); + TEST(session.IsFollowing(), ()); + + TimedSignal rebuildTimedSignal; + session.RebuildRoute(kTestRoute.front(), + [&rebuildTimedSignal](Route const &, IRouter::ResultCode) + { + rebuildTimedSignal.Signal(); + }, + nullptr, 0); + TEST(rebuildTimedSignal.WaitUntil(time), ("Route was not built.")); + TEST(session.IsFollowing(), ()); +} } // namespace routing