[map] Add tests for user routes
Add more complete tests for user routes Fix some issues found through the tests Signed-off-by: Fábio Gomes <gabriel.gomes@tecnico.ulisboa.pt>
This commit is contained in:
parent
74fac52d5e
commit
3f4b28704c
3 changed files with 260 additions and 34 deletions
|
@ -1,14 +1,25 @@
|
|||
#include "testing/testing.hpp"
|
||||
|
||||
#include "map/routing_manager.hpp"
|
||||
#include "map/routing_mark.hpp"
|
||||
|
||||
#include "geometry/point2d.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
namespace user_routes_test
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
using Runner = Platform::ThreadRunner;
|
||||
|
||||
string const kTestRouteName1 = "My Test Route";
|
||||
string const kTestRouteName2 = "My Other Test Route";
|
||||
|
||||
#define RM_CALLBACKS { \
|
||||
static_cast<RoutingManager::Callbacks::DataSourceGetterFn>(nullptr), \
|
||||
static_cast<RoutingManager::Callbacks::CountryInfoGetterFn>(nullptr), \
|
||||
|
@ -17,6 +28,86 @@ using namespace std;
|
|||
static_cast<RoutingManager::Callbacks::PowerManagerGetter>(nullptr) \
|
||||
}
|
||||
|
||||
#define BM_CALLBACKS { \
|
||||
[]() -> StringsBundle const & \
|
||||
{ \
|
||||
static StringsBundle const dummyBundle; \
|
||||
return dummyBundle; \
|
||||
}, \
|
||||
static_cast<BookmarkManager::Callbacks::GetSeacrhAPIFn>(nullptr), \
|
||||
static_cast<BookmarkManager::Callbacks::CreatedBookmarksCallback>(nullptr), \
|
||||
static_cast<BookmarkManager::Callbacks::UpdatedBookmarksCallback>(nullptr), \
|
||||
static_cast<BookmarkManager::Callbacks::DeletedBookmarksCallback>(nullptr), \
|
||||
static_cast<BookmarkManager::Callbacks::AttachedBookmarksCallback>(nullptr), \
|
||||
static_cast<BookmarkManager::Callbacks::DetachedBookmarksCallback>(nullptr) \
|
||||
}
|
||||
|
||||
RouteMarkData getRouteMarkStart()
|
||||
{
|
||||
RouteMarkData mark;
|
||||
mark.m_title = "Title 1";
|
||||
mark.m_subTitle = "Sub 1";
|
||||
mark.m_position.x = 0;
|
||||
mark.m_position.y = 0;
|
||||
mark.m_pointType = RouteMarkType::Start;
|
||||
|
||||
return mark;
|
||||
}
|
||||
|
||||
RouteMarkData getRouteMarkFinish()
|
||||
{
|
||||
RouteMarkData mark;
|
||||
mark.m_title = "Title 2";
|
||||
mark.m_subTitle = "Sub 2";
|
||||
mark.m_position.x = 1;
|
||||
mark.m_position.y = 1;
|
||||
mark.m_pointType = RouteMarkType::Finish;
|
||||
|
||||
return mark;
|
||||
}
|
||||
|
||||
void awaitFileSaving(RoutingManager *rManager, string routeName)
|
||||
{
|
||||
for(int i = 1; i <= 5; i++)
|
||||
{
|
||||
cout << "Awaiting file saving " << i << endl;
|
||||
if (rManager->HasSavedUserRoute(routeName))
|
||||
{
|
||||
cout << routeName << " found" << endl;
|
||||
return;
|
||||
}
|
||||
this_thread::sleep_for(chrono::seconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
void awaitFileDeletion(RoutingManager *rManager, string routeName)
|
||||
{
|
||||
for(int i = 1; i <= 5; i++)
|
||||
{
|
||||
cout << "Awaiting file deletion " << i << endl;
|
||||
if (!rManager->HasSavedUserRoute(routeName))
|
||||
{
|
||||
cout << routeName << " deleted" << endl;
|
||||
return;
|
||||
}
|
||||
this_thread::sleep_for(chrono::seconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
void awaitFileLoading(RoutingManager *rManager)
|
||||
{
|
||||
for(int i = 1; i <= 5; i++)
|
||||
{
|
||||
cout << "Awaiting file loading " << i << endl;
|
||||
if (rManager->GetRoutePointsCount() != 0)
|
||||
{
|
||||
cout << "Route loaded" << endl;
|
||||
return;
|
||||
}
|
||||
this_thread::sleep_for(chrono::seconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
class TestDelegate : public RoutingManager::Delegate
|
||||
{
|
||||
void OnRouteFollow(routing::RouterType type) override
|
||||
|
@ -30,13 +121,145 @@ class TestDelegate : public RoutingManager::Delegate
|
|||
}
|
||||
};
|
||||
|
||||
UNIT_TEST(user_routes_test)
|
||||
UNIT_CLASS_TEST(Runner, user_routes_save_delete)
|
||||
{
|
||||
TestDelegate d = TestDelegate();
|
||||
TestDelegate & dRef = d;
|
||||
RoutingManager rManager(RM_CALLBACKS, dRef);
|
||||
|
||||
TEST(rManager.getUserRoutes().empty(),("Found User Routes before test start"));
|
||||
BookmarkManager bmManager(BM_CALLBACKS);
|
||||
rManager.SetBookmarkManager(&bmManager);
|
||||
|
||||
rManager.AddRoutePoint(getRouteMarkStart());
|
||||
rManager.AddRoutePoint(getRouteMarkFinish());
|
||||
|
||||
TEST(RoutingManager::GetUserRouteNames().empty(),("User routes found before test start"));
|
||||
|
||||
rManager.SaveUserRoutePoints(kTestRouteName1);
|
||||
awaitFileSaving(&rManager, kTestRouteName1);
|
||||
|
||||
TEST(rManager.HasSavedUserRoute(kTestRouteName1), ("Test route not found after saving it"));
|
||||
|
||||
rManager.DeleteUserRoute(kTestRouteName1);
|
||||
awaitFileDeletion(&rManager, kTestRouteName1);
|
||||
|
||||
TEST(!rManager.HasSavedUserRoute(kTestRouteName1), ("Test route found after deleting it"));
|
||||
}
|
||||
|
||||
UNIT_CLASS_TEST(Runner, user_routes_rename)
|
||||
{
|
||||
TestDelegate d = TestDelegate();
|
||||
TestDelegate & dRef = d;
|
||||
RoutingManager rManager(RM_CALLBACKS, dRef);
|
||||
BookmarkManager bmManager(BM_CALLBACKS);
|
||||
rManager.SetBookmarkManager(&bmManager);
|
||||
|
||||
rManager.AddRoutePoint(getRouteMarkStart());
|
||||
rManager.AddRoutePoint(getRouteMarkFinish());
|
||||
|
||||
TEST(RoutingManager::GetUserRouteNames().empty(),("User routes found before test start"));
|
||||
|
||||
rManager.SaveUserRoutePoints(kTestRouteName1);
|
||||
awaitFileSaving(&rManager, kTestRouteName1);
|
||||
|
||||
TEST(rManager.HasSavedUserRoute(kTestRouteName1), ("Test route 1 not found after saving it"));
|
||||
TEST(!rManager.HasSavedUserRoute(kTestRouteName2), ("Test route 2 found before naming it that"));
|
||||
|
||||
rManager.RenameUserRoute(kTestRouteName1, kTestRouteName2);
|
||||
awaitFileSaving(&rManager, kTestRouteName2);
|
||||
|
||||
TEST(!rManager.HasSavedUserRoute(kTestRouteName1), ("Test route 1 found after renaming it"));
|
||||
TEST(rManager.HasSavedUserRoute(kTestRouteName2), ("Test route 2 not found after naming it that"));
|
||||
|
||||
rManager.DeleteUserRoute(kTestRouteName2);
|
||||
awaitFileDeletion(&rManager, kTestRouteName2);
|
||||
|
||||
TEST(!rManager.HasSavedUserRoute(kTestRouteName1), ("Test route 1 found after deleting it"));
|
||||
TEST(!rManager.HasSavedUserRoute(kTestRouteName2), ("Test route 2 found after deleting it"));
|
||||
}
|
||||
|
||||
UNIT_CLASS_TEST(Runner, user_routes_list)
|
||||
{
|
||||
TestDelegate d = TestDelegate();
|
||||
TestDelegate & dRef = d;
|
||||
RoutingManager rManager(RM_CALLBACKS, dRef);
|
||||
BookmarkManager bmManager(BM_CALLBACKS);
|
||||
rManager.SetBookmarkManager(&bmManager);
|
||||
|
||||
rManager.AddRoutePoint(getRouteMarkStart());
|
||||
rManager.AddRoutePoint(getRouteMarkFinish());
|
||||
|
||||
TEST(RoutingManager::GetUserRouteNames().empty(),("User routes found before test start"));
|
||||
|
||||
rManager.SaveUserRoutePoints(kTestRouteName1);
|
||||
rManager.SaveUserRoutePoints(kTestRouteName2);
|
||||
awaitFileSaving(&rManager, kTestRouteName1);
|
||||
awaitFileSaving(&rManager, kTestRouteName2);
|
||||
|
||||
TEST(rManager.HasSavedUserRoute(kTestRouteName1), ("Test route 1 not found after saving it"));
|
||||
TEST(rManager.HasSavedUserRoute(kTestRouteName2), ("Test route 2 not found after saving it"));
|
||||
|
||||
auto routes = RoutingManager::GetUserRouteNames();
|
||||
|
||||
TEST_EQUAL(routes.size(), 2, ("Incorrect number of routes found"));
|
||||
|
||||
set<string> routesSet(routes.begin(), routes.end());
|
||||
|
||||
set<string> expectedRoutes;
|
||||
expectedRoutes.insert(kTestRouteName1);
|
||||
expectedRoutes.insert(kTestRouteName2);
|
||||
|
||||
TEST_EQUAL(routesSet, expectedRoutes, ("Unexpected route names found"));
|
||||
|
||||
rManager.DeleteUserRoute(kTestRouteName1);
|
||||
rManager.DeleteUserRoute(kTestRouteName2);
|
||||
awaitFileDeletion(&rManager, kTestRouteName1);
|
||||
awaitFileDeletion(&rManager, kTestRouteName2);
|
||||
|
||||
TEST(RoutingManager::GetUserRouteNames().empty(),("Found User Routes after deletion"));
|
||||
}
|
||||
|
||||
// TODO Solve problems regarding LoadRoutePoints' use of Platform::Thread::Gui, code inside it seems not to be running
|
||||
/*UNIT_CLASS_TEST(Runner, user_routes_load)
|
||||
{
|
||||
TestDelegate d = TestDelegate();
|
||||
TestDelegate & dRef = d;
|
||||
RoutingManager rManager(RM_CALLBACKS, dRef);
|
||||
BookmarkManager bmManager(BM_CALLBACKS);
|
||||
rManager.SetBookmarkManager(&bmManager);
|
||||
|
||||
rManager.AddRoutePoint(getRouteMarkStart());
|
||||
rManager.AddRoutePoint(getRouteMarkFinish());
|
||||
|
||||
TEST(RoutingManager::GetUserRouteNames().empty(),("User routes found before test start"));
|
||||
|
||||
rManager.SaveUserRoutePoints(kTestRouteName1);
|
||||
awaitFileSaving(&rManager, kTestRouteName1);
|
||||
|
||||
TEST(rManager.HasSavedUserRoute(kTestRouteName1), ("Test route not found after saving it"));
|
||||
|
||||
rManager.RemoveRoutePoints();
|
||||
|
||||
TEST(rManager.GetRoutePoints().empty(), ("Route points found before loading"));
|
||||
|
||||
rManager.LoadUserRoutePoints(nullptr, kTestRouteName1);
|
||||
awaitFileLoading(&rManager);
|
||||
|
||||
TEST_EQUAL(rManager.GetRoutePoints().size(), 2, ("Test route loaded incorrect number of points"));
|
||||
|
||||
for (const auto& point : rManager.GetRoutePoints())
|
||||
{
|
||||
if (point.m_pointType == RouteMarkType::Start)
|
||||
TEST_EQUAL(point.m_position, m2::PointD(0,0), ("Start point incorrect"));
|
||||
else if (point.m_pointType == RouteMarkType::Finish)
|
||||
TEST_EQUAL(point.m_position, m2::PointD(1,1), ("Finish point incorrect"));
|
||||
else
|
||||
TEST(false, ("Intermediate point found on a 2 point route"));
|
||||
}
|
||||
|
||||
rManager.DeleteUserRoute(kTestRouteName1);
|
||||
awaitFileDeletion(&rManager, kTestRouteName1);
|
||||
|
||||
TEST(!rManager.HasSavedUserRoute(kTestRouteName1), ("Test route found after deleting it"));
|
||||
}*/
|
||||
|
||||
} // namespace user_routes_test
|
||||
|
|
|
@ -50,7 +50,7 @@ double const kRouteScaleMultiplier = 1.5;
|
|||
|
||||
string const kRoutePointsFile = "route_points.dat";
|
||||
|
||||
string const kUserRoutesFolder = "user_routes/";
|
||||
string const kUserRoutesFileExtension = ".usrdat";
|
||||
|
||||
uint32_t constexpr kInvalidTransactionId = 0;
|
||||
|
||||
|
@ -1359,9 +1359,9 @@ void RoutingManager::CancelRoutePointsTransaction(uint32_t transactionId)
|
|||
routePoints.AddRoutePoint(std::move(markData));
|
||||
}
|
||||
|
||||
bool RoutingManager::HasSavedUserRoute(string const fileName) const
|
||||
bool RoutingManager::HasSavedUserRoute(string const routeName) const
|
||||
{
|
||||
return HasSavedRoutePoints(kUserRoutesFolder + fileName + ".dat");
|
||||
return HasSavedRoutePoints(routeName + kUserRoutesFileExtension);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1376,9 +1376,9 @@ bool RoutingManager::HasSavedRoutePoints(string const fileName) const
|
|||
return GetPlatform().IsFileExistsByFullPath(filePath);
|
||||
}
|
||||
|
||||
void RoutingManager::LoadUserRoutePoints(LoadRouteHandler const & handler, string const fileName)
|
||||
void RoutingManager::LoadUserRoutePoints(LoadRouteHandler const & handler, string const routeName)
|
||||
{
|
||||
LoadRoutePoints(handler, kUserRoutesFolder + fileName + ".dat", false);
|
||||
LoadRoutePoints(handler, routeName + kUserRoutesFileExtension, false);
|
||||
}
|
||||
|
||||
void RoutingManager::LoadRoutePoints(LoadRouteHandler const & handler)
|
||||
|
@ -1465,8 +1465,9 @@ void RoutingManager::LoadRoutePoints(LoadRouteHandler const & handler, string co
|
|||
});
|
||||
}
|
||||
|
||||
void RoutingManager::SaveUserRoutePoints(string const fileName) {
|
||||
SaveRoutePoints(kUserRoutesFolder + fileName + ".dat", false);
|
||||
void RoutingManager::SaveUserRoutePoints(string const routeName)
|
||||
{
|
||||
SaveRoutePoints(routeName + kUserRoutesFileExtension, false);
|
||||
}
|
||||
|
||||
void RoutingManager::SaveRoutePoints() {
|
||||
|
@ -1537,9 +1538,9 @@ void RoutingManager::OnExtrapolatedLocationUpdate(location::GpsInfo const & info
|
|||
routeMatchingInfo);
|
||||
}
|
||||
|
||||
void RoutingManager::DeleteUserRoute(string const fileName)
|
||||
void RoutingManager::DeleteUserRoute(string const routeName)
|
||||
{
|
||||
DeleteSavedRoutePoints(kUserRoutesFolder + fileName + ".dat");
|
||||
DeleteSavedRoutePoints(routeName + kUserRoutesFileExtension);
|
||||
}
|
||||
|
||||
void RoutingManager::DeleteSavedRoutePoints()
|
||||
|
@ -1559,16 +1560,16 @@ void RoutingManager::DeleteSavedRoutePoints(string const fileName)
|
|||
});
|
||||
}
|
||||
|
||||
void RoutingManager::RenameUserRoute(string const oldFileName, string const newFileName)
|
||||
void RoutingManager::RenameUserRoute(string const oldRouteName, string const newRouteName)
|
||||
{
|
||||
if (!HasSavedRoutePoints(kUserRoutesFolder + oldFileName + ".dat"))
|
||||
if (!HasSavedRoutePoints(oldRouteName + kUserRoutesFileExtension))
|
||||
return;
|
||||
|
||||
if (HasSavedRoutePoints(kUserRoutesFolder + newFileName + ".dat"))
|
||||
if (HasSavedRoutePoints(newRouteName + kUserRoutesFileExtension))
|
||||
return;
|
||||
|
||||
GetPlatform().RunTask(Platform::Thread::File, [oldFileName = kUserRoutesFolder + oldFileName + ".dat" ,
|
||||
newFileName = kUserRoutesFolder + newFileName + ".dat"]()
|
||||
GetPlatform().RunTask(Platform::Thread::File, [oldFileName = oldRouteName + kUserRoutesFileExtension ,
|
||||
newFileName = newRouteName + kUserRoutesFileExtension]()
|
||||
{
|
||||
auto const oldPath = GetPlatform().SettingsPathForFile(oldFileName);
|
||||
auto const newPath = GetPlatform().SettingsPathForFile(newFileName);
|
||||
|
@ -1576,19 +1577,21 @@ void RoutingManager::RenameUserRoute(string const oldFileName, string const newF
|
|||
});
|
||||
}
|
||||
|
||||
vector<string> RoutingManager::getUserRoutes()
|
||||
// static
|
||||
vector<string> RoutingManager::GetUserRouteNames()
|
||||
{
|
||||
vector<string> routeFileNames;
|
||||
vector<string> routeNames;
|
||||
GetPlatform().GetFilesByExt(GetPlatform().SettingsPathForFile(kUserRoutesFolder), ".dat", routeNames);
|
||||
Platform::GetFilesByExt(GetPlatform().SettingsDir(), kUserRoutesFileExtension, routeFileNames);
|
||||
|
||||
for(auto name : routeNames)
|
||||
for(const auto & name : routeFileNames)
|
||||
{
|
||||
size_t idx = name.rfind(".dat");
|
||||
size_t idx = name.rfind(kUserRoutesFileExtension);
|
||||
if (idx == string::npos)
|
||||
continue;
|
||||
name.erase(idx, 4); // erase file extension from the string
|
||||
routeNames.push_back(name.substr(0, idx)); // string without extension
|
||||
}
|
||||
return routeNames/*TODO find out how this return value is able to be interpreted in java*/;
|
||||
return routeNames;
|
||||
}
|
||||
|
||||
void RoutingManager::UpdatePreviewMode()
|
||||
|
|
|
@ -304,36 +304,36 @@ public:
|
|||
void CancelRoutePointsTransaction(uint32_t transactionId);
|
||||
static uint32_t InvalidRoutePointsTransactionId();
|
||||
|
||||
/// \returns true if there is a user route points saved with the given name and false otherwise.
|
||||
bool HasSavedUserRoute(std::string fileName) const;
|
||||
/// \returns true if there is a user route saved with the given name and false otherwise.
|
||||
bool HasSavedUserRoute(std::string routeName) const;
|
||||
/// \returns true if there are route points saved in the default file and false otherwise.
|
||||
bool HasSavedRoutePoints() const;
|
||||
/// \returns true if there are route points saved in file and false otherwise.
|
||||
bool HasSavedRoutePoints(std::string fileName) const;
|
||||
/// The result of the loading will be sent via SafeCallback.
|
||||
using LoadRouteHandler = platform::SafeCallback<void(bool success)>;
|
||||
/// \brief It loads road points from file in a user routes folder and keeps file after loading.
|
||||
void LoadUserRoutePoints(LoadRouteHandler const & handler, std::string fileName);
|
||||
/// \brief It loads road points from file with a user routes file extension and keeps file after loading.
|
||||
void LoadUserRoutePoints(LoadRouteHandler const & handler, std::string routeName);
|
||||
/// \brief It loads road points from file and delete file after loading.
|
||||
void LoadRoutePoints(LoadRouteHandler const & handler);
|
||||
/// \brief It loads road points from file and can be set to delete file after loading.
|
||||
void LoadRoutePoints(LoadRouteHandler const & handler, std::string const& fileName, bool deleteAfterLoading);
|
||||
/// \brief It saves route points to file in a user routes folder
|
||||
void SaveUserRoutePoints(std::string fileName);
|
||||
/// \brief It saves route points to file with a user routes file extension
|
||||
void SaveUserRoutePoints(std::string routeName);
|
||||
/// \brief It saves route points to default file.
|
||||
void SaveRoutePoints();
|
||||
/// \brief It saves route points to file with the given name.
|
||||
void SaveRoutePoints(std::string fileName, bool keepReplaceWithMyPositionAfterRestart);
|
||||
/// \brief It deletes the user route with the user route with the given name if it exists.
|
||||
void DeleteUserRoute(std::string fileName);
|
||||
/// \brief It deletes the user route with the given name if it exists.
|
||||
void DeleteUserRoute(std::string routeName);
|
||||
/// \brief It deletes the default file with saved route points if it exists.
|
||||
void DeleteSavedRoutePoints();
|
||||
/// \brief It deletes file with saved route points if it exists.
|
||||
void DeleteSavedRoutePoints(std::string fileName);
|
||||
/// \brief It renames a user route
|
||||
void RenameUserRoute(std::string oldFileName, std::string newFileName);
|
||||
/// \returns names of the files in the user routes folder without the .dat file extension.
|
||||
std::vector<std::string> getUserRoutes();
|
||||
void RenameUserRoute(std::string oldRouteName, std::string newRouteName);
|
||||
/// \returns names of the files with a user routes file extension without the .usrdat file extension.
|
||||
static std::vector<std::string> GetUserRouteNames();
|
||||
|
||||
void UpdatePreviewMode();
|
||||
void CancelPreviewMode();
|
||||
|
|
Reference in a new issue