From 3f315f4c77be2849650886e6d50415a45dcf563e Mon Sep 17 00:00:00 2001 From: Kiseok Chang Date: Mon, 20 May 2019 15:26:34 +0900 Subject: [PATCH] Support Multiple Storage for mode description --- packaging/modes.spec | 5 ++ supervisor/ActionSchemaManager.cpp | 4 +- supervisor/ModeManager.cpp | 36 ++++++++---- supervisor/ModeManager.h | 8 ++- unittest/modes_test_modemgr.cpp | 110 ++++++++++++++++++++++++++++++------- 5 files changed, 129 insertions(+), 34 deletions(-) diff --git a/packaging/modes.spec b/packaging/modes.spec index ec5bb66..5f695aa 100644 --- a/packaging/modes.spec +++ b/packaging/modes.spec @@ -94,6 +94,11 @@ pushd example xmllint --noout --schema tizen_action_schema.xsd tizen_action_schema.xml xmllint --noout --schema tizen_mode.xsd tizen_*_mode.xml +mkdir extra +sed s/ex1/ex3/g tizen_ex1_mode.xml > extra/tizen_ex3_mode.xml +sed s/ex1/ex4/g tizen_ex1_mode.xml > extra/tizen_ex4_mode.xml + +../unittest/gtest-modes-modemgr ../unittest/gtest-modes-plugin ../unittest/gtest-modes-parser popd diff --git a/supervisor/ActionSchemaManager.cpp b/supervisor/ActionSchemaManager.cpp index f8d49f5..7ce1b36 100644 --- a/supervisor/ActionSchemaManager.cpp +++ b/supervisor/ActionSchemaManager.cpp @@ -50,8 +50,10 @@ void ActionSchemaManager::makeActionSchemaMap(const string &dirPath, const strin struct dirent *entry; string filePath; - if ((dir = opendir(dirPath.c_str())) == NULL) + if ((dir = opendir(dirPath.c_str())) == NULL) { ERR("%s dir open error! [%s]", dirPath.c_str(), strerror(errno)); + return; + } while ((entry = readdir(dir)) != NULL) { if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) { diff --git a/supervisor/ModeManager.cpp b/supervisor/ModeManager.cpp index 7fd31ff..592e29f 100644 --- a/supervisor/ModeManager.cpp +++ b/supervisor/ModeManager.cpp @@ -23,19 +23,22 @@ #include "ModeXMLParser.h" using std::string; +using std::set; MODES_NAMESPACE_USE; ModeManager::ModeManager() { - //directoryOfMode = MODES_MODE_DEFAULT_DIR; - directoryOfMode = "/usr/share/modes/"; + //string dirPath = MODES_MODE_DEFAULT_DIR; + string dirPath = "/usr/share/modes/"; - std::string xsdFile = directoryOfMode + "/" + ACTION_SCHEMA_XSD_PATH; + directoriesOfMode.insert(dirPath); + + std::string xsdFile = dirPath + "/" + ACTION_SCHEMA_XSD_PATH; if(!actionXsdFilePath.empty()) - xsdFile = directoryOfMode + "/" + getActionXsdFilePath(); + xsdFile = dirPath + "/" + getActionXsdFilePath(); - schemaMgr.makeActionSchemaMap(directoryOfMode, xsdFile); - makeModeMap(directoryOfMode); + schemaMgr.makeActionSchemaMap(dirPath, xsdFile); + makeModeMap(dirPath); } void ModeManager::loadPlugin() @@ -161,15 +164,16 @@ int ModeManager::applyMode(const string &modeName) return MODES_ERROR_NONE; } -void ModeManager::makeModeMap(const string &dirPath) +bool ModeManager::makeModeMap(const string &dirPath) { DBG("dirPath : [%s]", dirPath.c_str()); DIR *dir; struct dirent *entry; - if ((dir = opendir(dirPath.c_str())) == NULL){ + + if ((dir = opendir(dirPath.c_str())) == NULL) { ERR("%s dir open error! [%s]", dirPath.c_str(), strerror(errno)); - return; + return false; } while ((entry = readdir(dir)) != NULL) { @@ -194,5 +198,17 @@ void ModeManager::makeModeMap(const string &dirPath) } closedir(dir); - return; + return true; +} + +void ModeManager::addModeDirectory(const string &dirPath) +{ + set::iterator result = directoriesOfMode.find(dirPath); + if (result != directoriesOfMode.end()) + throw std::invalid_argument("Already registered directory (" + dirPath + ")"); + + if (!makeModeMap(dirPath)) + throw std::invalid_argument("Inaccessible directory (" + dirPath + ")"); + + directoriesOfMode.insert(dirPath); } diff --git a/supervisor/ModeManager.h b/supervisor/ModeManager.h index addd65b..ce15877 100644 --- a/supervisor/ModeManager.h +++ b/supervisor/ModeManager.h @@ -33,6 +33,7 @@ class ModeManager { public: ModeManager(); + void addModeDirectory(const std::string &dirPath); int applyMode(const std::string &modeName); void setPluginDirPath(std::string pluginDirPath); void setModeXMLDirPathList(std::setlist); @@ -41,7 +42,7 @@ public: void setActionSchemaDirPath(std::string dirPath); private: void loadPlugin(); - void makeModeMap(const std::string &dirPath); + bool makeModeMap(const std::string &dirPath); void addModeName(ModeParser *parser, std::string &path); ModeParser* getParser(const std::string &path); std::string getPluginDirPath(); @@ -50,7 +51,8 @@ private: std::string getActionXsdFilePath(); std::string getActionSchemaDirPath(); - std::string directoryOfMode; + std::set directoriesOfMode; + std::map modeMap; ActionSchemaManager schemaMgr; PluginManager pluginManager; @@ -61,7 +63,7 @@ private: std::string actionSchemaDirPath; #ifdef MDS_TEST - friend class TestModeManager; + friend class ModeManagerTest; #endif }; diff --git a/unittest/modes_test_modemgr.cpp b/unittest/modes_test_modemgr.cpp index f7d8229..f9265c1 100644 --- a/unittest/modes_test_modemgr.cpp +++ b/unittest/modes_test_modemgr.cpp @@ -1,47 +1,117 @@ #include +#include #include #include "supervisor/ModeManager.h" + MODES_NAMESPACE_USE; using namespace std; MODES_NAMESPACE_BEGIN -class TestModeManager { +class ModeManagerTest{ public: - string getDir(ModeManager& m); - map getModeMap(ModeManager& m); + set getDirectories(ModeManager &m); + map getModeMap(ModeManager &m); + static void printModeMap(map &modemap); }; MODES_NAMESPACE_END -string TestModeManager::getDir(ModeManager& m) +set ModeManagerTest::getDirectories(ModeManager &m) { - return m.directoryOfMode; + return m.directoriesOfMode; } -map TestModeManager::getModeMap(ModeManager& m) +map ModeManagerTest::getModeMap(ModeManager &m) { return m.modeMap; } -TEST(modemanager, getdefaultdir) { - ModeManager mdMgr; - TestModeManager mdmgrT; +void ModeManagerTest::printModeMap(map &modemap) +{ + if (!modemap.empty()) { + cout << "| "; cout.width(15); cout << "Mode Name" << " | " << "ModeXML file Path" << endl; + + map::iterator it; + for (it = modemap.begin(); it != modemap.end(); it++) { + cout << "| "; cout.width(15); cout << it->first.c_str() << " | " << it->second.c_str() << endl; + } + } +} + +TEST(ModeManagerTest, test_getModemap) { + ModeManagerTest mdmgrtest; + ModeManager md_mgr; + md_mgr.addModeDirectory("."); + + map modemap = mdmgrtest.getModeMap(md_mgr); + EXPECT_FALSE(modemap.empty()) << "Mode Map is empty!" << endl; + + mdmgrtest.printModeMap(modemap); +} + +TEST(ModeManagerTest, test_addModeDirectory) { + ModeManagerTest mdmgrtest; + ModeManager md_mgr; + md_mgr.addModeDirectory("."); + + string dir_extra = "./extra"; + try { + md_mgr.addModeDirectory(dir_extra); + } + catch (exception &e) { + ADD_FAILURE() << "Fail to append : " << e.what() << endl; + return; + } + + map modemap = mdmgrtest.getModeMap(md_mgr); + EXPECT_FALSE(modemap.empty()) << "Mode Map is empty!" << endl; + + mdmgrtest.printModeMap(modemap); +} + +TEST(ModeManagerTest, test_addModeDirectory_withwrongdir) { + ModeManager md_mgr; + md_mgr.addModeDirectory("."); + + string dir_wrong = "./dir_wrong"; + try { + md_mgr.addModeDirectory(dir_wrong); + } + catch (exception &e) { + cout << "Fail to append : " << e.what() << endl; + return; + } - EXPECT_EQ("/usr/share/modes/mode", mdmgrT.getDir(mdMgr)); + ADD_FAILURE() << "addModeDirectory() did not detect a wrong directory!" << endl; } -TEST(modemanager, getmodemap) { - ModeManager mdMgr; - TestModeManager mdmgrT; +TEST(ModeManagerTest, test_addModeDirectory_withappendeddir) { + ModeManagerTest mdmgrtest; + ModeManager md_mgr; + md_mgr.addModeDirectory("."); - map map = mdmgrT.getModeMap(mdMgr); - EXPECT_FALSE(map.empty()); + string dir_extra = "./extra"; + try { + md_mgr.addModeDirectory(dir_extra); + } + catch (exception &e) { + cout << "Fail to append : " << e.what() < modemap = mdmgrtest.getModeMap(md_mgr); + EXPECT_FALSE(modemap.empty()) << "Mode Map is empty!" << endl; - if (!map.empty()) { - std::map::iterator it; - printf("| %-15.15s | %s\n", "Mode Name", "ModeXML file Path"); + mdmgrtest.printModeMap(modemap); - for(it = map.begin(); it != map.end(); it++) - printf("| %-15.15s | %s\n", it->first.c_str(), it->second.c_str()); + string dir_appended = dir_extra; + try { + md_mgr.addModeDirectory(dir_appended); + } + catch (exception &e) { + cout << "Fail to append : " << e.what() << endl; + return; } + + ADD_FAILURE() << "addModeDirectory() did not detect a directory that is already attached!" << endl; } -- 2.7.4