Support Multiple Storage for mode description
authorKiseok Chang <kiso.chang@samsung.com>
Mon, 20 May 2019 06:26:34 +0000 (15:26 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Wed, 18 Mar 2020 08:53:44 +0000 (17:53 +0900)
packaging/modes.spec
supervisor/ActionSchemaManager.cpp
supervisor/ModeManager.cpp
supervisor/ModeManager.h
unittest/modes_test_modemgr.cpp

index ec5bb66..5f695aa 100644 (file)
@@ -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
index f8d49f5..7ce1b36 100644 (file)
@@ -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)) {
index 7fd31ff..592e29f 100644 (file)
 #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<string>::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);
 }
index addd65b..ce15877 100644 (file)
@@ -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::set<std::string>list);
@@ -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<std::string> directoriesOfMode;
+
        std::map<std::string, std::string> modeMap;
        ActionSchemaManager schemaMgr;
        PluginManager pluginManager;
@@ -61,7 +63,7 @@ private:
        std::string actionSchemaDirPath;
 
 #ifdef MDS_TEST
-       friend class TestModeManager;
+       friend class ModeManagerTest;
 #endif
 };
 
index f7d8229..f9265c1 100644 (file)
 #include <iostream>
+#include <exception>
 #include <gtest/gtest.h>
 #include "supervisor/ModeManager.h"
 
+
 MODES_NAMESPACE_USE;
 using namespace std;
 
 MODES_NAMESPACE_BEGIN
-class TestModeManager {
+class ModeManagerTest{
 public:
-       string getDir(ModeManager& m);
-       map<string, string> getModeMap(ModeManager& m);
+       set<string> getDirectories(ModeManager &m);
+       map<string, string> getModeMap(ModeManager &m);
+       static void printModeMap(map<string, string> &modemap);
 };
 MODES_NAMESPACE_END
 
-string TestModeManager::getDir(ModeManager& m)
+set<string>  ModeManagerTest::getDirectories(ModeManager &m)
 {
-       return m.directoryOfMode;
+       return m.directoriesOfMode;
 }
 
-map<string, string> TestModeManager::getModeMap(ModeManager& m)
+map<string, string> ModeManagerTest::getModeMap(ModeManager &m)
 {
        return m.modeMap;
 }
 
-TEST(modemanager, getdefaultdir) {
-       ModeManager mdMgr;
-       TestModeManager mdmgrT;
+void ModeManagerTest::printModeMap(map<string, string> &modemap)
+{
+       if (!modemap.empty()) {
+               cout << "| "; cout.width(15); cout << "Mode Name" << " | " << "ModeXML file Path" << endl;
+
+               map<string, string>::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<string, string> 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<string, string> 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<string, string> 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() <<endl;
+               ADD_FAILURE();
+               return;
+       }
+       map<string, string> modemap = mdmgrtest.getModeMap(md_mgr);
+       EXPECT_FALSE(modemap.empty()) << "Mode Map is empty!" << endl;
 
-       if (!map.empty()) {
-               std::map<string, string>::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;
 }